| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
 174
 175
 176
 177
 178
 179
 180
 181
 182
 183
 184
 185
 186
 187
 188
 189
 190
 191
 192
 193
 194
 195
 196
 197
 198
 199
 200
 201
 202
 203
 204
 205
 206
 207
 208
 209
 210
 211
 212
 213
 214
 215
 216
 217
 218
 219
 220
 221
 222
 223
 224
 225
 226
 227
 228
 229
 230
 231
 232
 233
 234
 235
 236
 237
 238
 239
 240
 241
 242
 243
 244
 245
 246
 247
 248
 249
 250
 251
 252
 253
 254
 255
 256
 257
 258
 259
 260
 261
 262
 263
 264
 265
 266
 267
 268
 269
 270
 271
 272
 273
 274
 275
 276
 277
 278
 279
 280
 281
 282
 283
 284
 285
 286
 287
 288
 289
 290
 291
 292
 293
 294
 295
 296
 297
 298
 299
 300
 301
 302
 303
 304
 305
 306
 307
 308
 309
 310
 311
 312
 313
 314
 315
 316
 317
 318
 319
 320
 321
 322
 323
 324
 325
 326
 327
 328
 329
 330
 331
 332
 333
 334
 335
 336
 337
 338
 339
 340
 341
 342
 343
 344
 345
 346
 347
 348
 349
 350
 351
 352
 353
 354
 355
 356
 357
 358
 359
 360
 361
 
 | /* Copyright (C) 2000-2013 Boris Wesslowski */
/* $Id: resolve.c 731 2013-05-17 14:15:23Z bw $ */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <netdb.h>
#include <ctype.h>
#ifdef HAVE_ADNS
#include <adns.h>
#include <errno.h>
#endif
#include "resolve.h"
#include "main.h"
#include "utils.h"
struct dns_cache *dns_first = NULL;
extern struct options opt;
#ifdef HAVE_ADNS
extern struct conn_data *first;
extern struct known_hosts *first_host;
adns_state adns;
struct adns_entry {
  struct in6_addr ip;
  adns_query query;
  struct adns_entry *next;
} *adnse_first = NULL;
#endif
char *resolve_protocol(int proto)
{
  struct protoent *protoent;
  protoent = getprotobynumber(proto);
  if (protoent != NULL) {
    return (protoent->p_name);
  } else {
    char *number;
    number = xmalloc(4);
    snprintf(number, 4, "%d", proto);
    return (number);
  }
}
char *resolve_service(int port, char *proto)
{
  struct servent *servent;
  int p;
  p = htons(port);
  servent = getservbyport(p, proto);
  if (servent != NULL) {
    p = ntohs(servent->s_port);
    if (p != port) {
      fprintf(stderr, _("port mismatch: %d != %d\n"), p, port);
    } else {
      return (servent->s_name);
    }
  }
  return ("-");
}
char *resolve_address_sync(struct in6_addr ip)
{
  char *fqdn;
  int r;
  char hbuf[NI_MAXHOST];
  socklen_t len;
  void *sa;
  struct sockaddr_in6 *sai6;
  struct in6_addr in6a;
  unsigned char buf[sizeof(struct sockaddr_in6)];
  memset(&buf, 0, sizeof(struct sockaddr_in6));
  fqdn = xmalloc(HOSTLEN);
  memcpy(&in6a, &ip, sizeof(struct in6_addr));
  sai6 = (struct sockaddr_in6 *) &buf;
  sai6->sin6_addr = in6a;
  sai6->sin6_family = AF_INET6;
  len = sizeof(struct sockaddr_in6);
  sa = sai6;
  r = getnameinfo((struct sockaddr *) sa, len, hbuf, sizeof(hbuf), NULL, 0, NI_NAMEREQD);
  if (r == EAI_NONAME) {
    snprintf(fqdn, HOSTLEN, "-");
  } else if (r == EAI_AGAIN) {
    xstrncpy(fqdn, _("[timeout]"), HOSTLEN);
  } else if (r == EAI_FAIL) {
    xstrncpy(fqdn, _("[server failure]"), HOSTLEN);
  } else if (r != 0) {
    snprintf(fqdn, HOSTLEN, "[%s]", gai_strerror(r));
  } else {
    {
      struct addrinfo hints, *res, *rp;
      int s;
      char dst[HOSTLEN], dst2[HOSTLEN];
      if (opt.verbose)
	fprintf(stderr, _("Resolving %s\n"), hbuf);
      memset(&hints, 0, sizeof(struct addrinfo));
      hints.ai_family = AF_UNSPEC;
      hints.ai_socktype = SOCK_STREAM;
      s = getaddrinfo(hbuf, NULL, &hints, &res);
      if (s != 0) {
#ifdef DNS_DEBUG
	snprintf(fqdn, HOSTLEN, "%s [%s]", hbuf, gai_strerror(s));
#else
	snprintf(fqdn, HOSTLEN, _("%s [forward lookup failed]"), hbuf);
#endif
      } else {
	snprintf(fqdn, HOSTLEN, "%s", hbuf);
	for (rp = res; rp != NULL; rp = rp->ai_next) {
	  if (rp->ai_family == AF_INET && isV4mappedV6addr(&ip)) {
	    struct sockaddr_in *sin;
	    sin = (void *) rp->ai_addr;
	    if (ip.s6_addr32[3] != sin->sin_addr.s_addr) {
	      snprintf(dst2, HOSTLEN, _(" [v4 forward lookup: %s]"), inet_ntop(rp->ai_family, &sin->sin_addr, dst, HOSTLEN));
	      strncat(fqdn, dst2, HOSTLEN - strlen(fqdn) - 1);
	    }
	  } else if (rp->ai_family == AF_INET6 && !isV4mappedV6addr(&ip)) {
	    struct sockaddr_in6 *sin6;
	    sin6 = (void *) rp->ai_addr;
	    if (compare_ipv6_equal(&ip, &sin6->sin6_addr) != 0) {
	      snprintf(dst2, HOSTLEN, _(" [v6 forward lookup: %s]"), inet_ntop(rp->ai_family, &sin6->sin6_addr, dst, HOSTLEN));
	      strncat(fqdn, dst2, HOSTLEN - strlen(fqdn) - 1);
	    }
	  }
	}
	freeaddrinfo(res);
      }
    }
  }
  return (fqdn);
}
#ifdef HAVE_ADNS
char *resolve_address_async(struct in6_addr ip)
{
  struct adns_entry *adnse;
  adns_answer *answer;
  char *fqdn;
  fqdn = xmalloc(HOSTLEN);
  adnse = adnse_first;
  while (adnse != NULL) {
    if (compare_ipv6_equal(&adnse->ip, &ip) == 0) {
      errno = adns_wait(adns, &adnse->query, &answer, NULL);
      if (!errno) {
	if (answer->status == adns_s_ok) {
	  xstrncpy(fqdn, *answer->rrs.str, HOSTLEN);
	} else if (answer->status == adns_s_inconsistent || answer->status == adns_s_prohibitedcname || answer->status == adns_s_answerdomaininvalid) {
	  char *fqdn_sync;
	  fqdn_sync = resolve_address_sync(ip);
	  xstrncpy(fqdn, fqdn_sync, HOSTLEN);
	  free(fqdn_sync);
	} else if (answer->status == adns_s_timeout) {
	  xstrncpy(fqdn, _("[timeout]"), HOSTLEN);
	} else if (answer->status == adns_s_rcodeservfail) {
	  xstrncpy(fqdn, _("[server failure]"), HOSTLEN);
	} else if (answer->status == adns_s_nxdomain) {
	  xstrncpy(fqdn, "-", HOSTLEN);
	} else if (answer->status == adns_s_nodata) {
	  xstrncpy(fqdn, "-", HOSTLEN);
	} else {
	  snprintf(fqdn, HOSTLEN, _("[adns status %d]"), answer->status);
	}
	free(answer);
	return (fqdn);
      } else {
	perror("adns_wait");
	break;
      }
    }
    adnse = adnse->next;
  }
  xstrncpy(fqdn, _("[adns error]"), HOSTLEN);
  return (fqdn);
}
#endif
char *resolve_address(struct in6_addr ip)
{
  struct dns_cache *dns;
  char *fqdn;
  dns = dns_first;
  while (dns != NULL) {
    if (compare_ipv6_equal(&ip, &dns->ip) == 0) {
      if (opt.verbose)
	fprintf(stderr, _("Resolving %s from cache\n"), my_inet_ntop(&ip));
      return (dns->fqdn);
    }
    dns = dns->next;
  }
#ifndef HAVE_ADNS
  if (opt.verbose)
    fprintf(stderr, _("Resolving %s\n"), my_inet_ntop(&ip));
  fqdn = resolve_address_sync(ip);
#else
  if (opt.verbose)
    fprintf(stderr, _("Resolving %s from adns\n"), my_inet_ntop(&ip));
  fqdn = resolve_address_async(ip);
#endif
  dns = xmalloc(sizeof(struct dns_cache));
  memcpy(&dns->ip, &ip, sizeof(struct in6_addr));
  dns->fqdn = xmalloc(strlen(fqdn) + 1);
  xstrncpy(dns->fqdn, fqdn, strlen(fqdn) + 1);
  dns->next = dns_first;
  dns_first = dns;
  free(fqdn);
  return (dns->fqdn);
}
void init_dns_cache(struct in6_addr *ip, char *hostname)
{
  struct dns_cache *dns;
  dns = dns_first;
  while (dns != NULL) {
    if (compare_ipv6_equal(ip, &dns->ip) == 0) {
      if (opt.verbose == 2)
	fprintf(stderr, _("IP address %s is already in DNS cache\n"), my_inet_ntop(ip));
      return;
    }
    dns = dns->next;
  }
  if (opt.verbose == 2)
    fprintf(stderr, _("Adding IP address '%s' with host name '%s' to DNS cache\n"), my_inet_ntop(ip), hostname);
  dns = xmalloc(sizeof(struct dns_cache));
  memcpy(&dns->ip, ip, sizeof(struct in6_addr));
  dns->fqdn = xmalloc(strlen(hostname) + 1);
  xstrncpy(dns->fqdn, hostname, strlen(hostname) + 1);
  dns->next = dns_first;
  dns_first = dns;
}
#ifdef HAVE_ADNS
void adns_list_add(struct in6_addr *ip)
{
  struct adns_entry *adnse;
  struct sockaddr_in sa;
  struct sockaddr_in6 sa6;
  adnse = xmalloc(sizeof(struct adns_entry));
  memcpy(&adnse->ip, ip, sizeof(struct in6_addr));
  if (isV4mappedV6addr(ip)) {
    char buf[INET6_ADDRSTRLEN];
    bzero(&sa, sizeof(struct sockaddr_in));
    sa.sin_family = AF_INET;
    inet_ntop(AF_INET, ip->s6_addr + 12, buf, INET_ADDRSTRLEN);
    inet_pton(AF_INET, buf, &sa.sin_addr);
    adns_submit_reverse(adns, (struct sockaddr *) &sa, adns_r_ptr, adns_qf_none, NULL, &adnse->query);
  } else {
    bzero(&sa6, sizeof(struct sockaddr_in6));
    sa6.sin6_family = AF_INET6;
    memcpy(&sa6.sin6_addr, ip, sizeof(struct in6_addr));
    adns_submit_reverse(adns, (struct sockaddr *) &sa6, adns_r_ptr, adns_qf_none, NULL, &adnse->query);
  }
  adnse->next = adnse_first;
  adnse_first = adnse;
  if (opt.verbose == 2)
    fprintf(stderr, _("Submitted %s to adns\n"), my_inet_ntop(&adnse->ip));
}
void adns_check_entry(struct in6_addr *ip)
{
  struct dns_cache *dns;
  struct adns_entry *adnse;
  unsigned char found = 0;
  dns = dns_first;
  while (dns != NULL) {
    if (compare_ipv6_equal(ip, &dns->ip) == 0) {
      found++;
      break;
    }
    dns = dns->next;
  }
  if (!found) {
    adnse = adnse_first;
    while (adnse != NULL) {
      if (compare_ipv6_equal(ip, &adnse->ip) == 0) {
	found++;
	break;
      }
      adnse = adnse->next;
    }
  }
  if (!found)
    adns_list_add(ip);
}
void adns_preresolve(unsigned char mode)
{
  if (mode == RES_ADNS_PC) {
    int max = 0;
    struct conn_data *this;
    this = first;
    while ((this != NULL) && (opt.max == 0 || max < opt.max)) {
      if (this->count >= opt.least) {
	if (opt.src_ip)
	  adns_check_entry(&this->shost);
	if (opt.dst_ip)
	  adns_check_entry(&this->dhost);
      }
      if (opt.max != 0)
	max++;
      this = this->next;
    }
  } else if (mode == RES_ADNS_HS) {
    struct known_hosts *this_host;
    this_host = first_host;
    while (this_host != NULL) {
      if (opt.src_ip)
	adns_check_entry(&this_host->shost);
      if (opt.dst_ip)
	adns_check_entry(&this_host->dhost);
      this_host = this_host->next;
    }
  }
}
#endif
struct in6_addr *resolve_hostname_from_cache(char *name)
{
  struct dns_cache *dns;
  dns = dns_first;
  while (dns != NULL) {
    if (strcmp(dns->fqdn, name) == 0) {
      if (opt.verbose == 2)
	fprintf(stderr, _("Resolving %s from cache\n"), name);
      return &dns->ip;
    }
    dns = dns->next;
  }
  return NULL;
}
 |