ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/svn/ircd-hybrid/trunk/src/irc_res.c
(Generate patch)

Comparing:
ircd-hybrid/src/irc_res.c (file contents), Revision 31 by knight, Sun Oct 2 20:34:05 2005 UTC vs.
ircd-hybrid-8/src/irc_res.c (file contents), Revision 1247 by michael, Sat Oct 1 07:54:24 2011 UTC

# Line 20 | Line 20
20   */
21  
22   #include "stdinc.h"
23 #include "tools.h"
24 #include "client.h"
23   #include "list.h"
24 < #include "common.h"
24 > #include "balloc.h"
25 > #include "client.h"
26   #include "event.h"
27   #include "irc_string.h"
28   #include "sprintf_irc.h"
29   #include "ircd.h"
30   #include "numeric.h"
31 < #include "restart.h"
31 > #include "rng_mt.h"
32   #include "fdlist.h"
33   #include "fileio.h" /* for fbopen / fbclose / fbputs */
34   #include "s_bsd.h"
35   #include "s_log.h"
36 + #include "s_misc.h"
37   #include "send.h"
38   #include "memory.h"
39   #include "irc_res.h"
40   #include "irc_reslib.h"
41 #include "irc_getnameinfo.h"
41  
42   #if (CHAR_BIT != 8)
43   #error this code needs to be able to address individual octets
# Line 69 | Line 68 | typedef enum
68   #ifdef IPV6
69    REQ_AAAA,  /* Looking up an AAAA */
70   #endif
71 <  REQ_CNAME, /* We got a CNAME in response, we better get a real answer next */
73 <  REQ_INT    /* ip6.arpa failed, falling back to ip6.int */
71 >  REQ_CNAME  /* We got a CNAME in response, we better get a real answer next */
72   } request_state;
73  
74   struct reslist
# Line 88 | Line 86 | struct reslist
86    time_t timeout;
87    struct irc_ssaddr addr;
88    char *name;
89 <  struct DNSQuery *query;  /* query callback for this request */
89 >  dns_callback_fnc callback;
90 >  void *callback_ctx;
91   };
92  
93   static fde_t ResolverFileDescriptor;
94 < static dlink_list request_list    = { NULL, NULL, 0 };
94 > static dlink_list request_list = { NULL, NULL, 0 };
95 > static BlockHeap *dns_heap = NULL;
96  
97 < static void rem_request(struct reslist *request);
98 < static struct reslist *make_request(struct DNSQuery *query);
99 < static void do_query_name(struct DNSQuery *query,
100 <                          const char* name, struct reslist *request, int);
101 < static void do_query_number(struct DNSQuery *query,
97 > static void rem_request(struct reslist *);
98 > static struct reslist *make_request(dns_callback_fnc, void *);
99 > static void do_query_name(dns_callback_fnc, void *,
100 >                          const char *, struct reslist *, int);
101 > static void do_query_number(dns_callback_fnc, void *,
102                              const struct irc_ssaddr *,
103 <                            struct reslist *request);
104 < static void query_name(const char *name, int query_class, int query_type,
105 <                       struct reslist *request);
106 < static int send_res_msg(const char *buf, int len, int count);
107 < static void resend_query(struct reslist *request);
108 < static int proc_answer(struct reslist *request, HEADER *header, char *, char *);
109 < static struct reslist *find_id(int id);
110 < static struct DNSReply *make_dnsreply(struct reslist *request);
111 <
112 < extern struct irc_ssaddr irc_nsaddr_list[IRCD_MAXNS];
113 < extern int irc_nscount;
114 < extern char irc_domain[HOSTLEN+1];
103 >                            struct reslist *);
104 > static void query_name(const char *, int, int, struct reslist *);
105 > static int send_res_msg(const char *, int, int);
106 > static void resend_query(struct reslist *);
107 > static int proc_answer(struct reslist *, HEADER *, char *, char *);
108 > static struct reslist *find_id(int);
109  
110  
111   /*
# Line 129 | Line 123 | static int
123   res_ourserver(const struct irc_ssaddr *inp)
124   {
125   #ifdef IPV6
126 <  struct sockaddr_in6 *v6;
127 <  struct sockaddr_in6 *v6in = (struct sockaddr_in6 *)inp;
126 >  const struct sockaddr_in6 *v6;
127 >  const struct sockaddr_in6 *v6in = (const struct sockaddr_in6 *)inp;
128   #endif
129 <  struct sockaddr_in *v4;
130 <  struct sockaddr_in *v4in = (struct sockaddr_in *)inp;
129 >  const struct sockaddr_in *v4;
130 >  const struct sockaddr_in *v4in = (const struct sockaddr_in *)inp;
131    int ns;
132  
133 <  for (ns = 0;  ns < irc_nscount;  ns++)
133 >  for (ns = 0; ns < irc_nscount; ns++)
134    {
135      const struct irc_ssaddr *srv = &irc_nsaddr_list[ns];
136   #ifdef IPV6
137 <    v6 = (struct sockaddr_in6 *)srv;
137 >    v6 = (const struct sockaddr_in6 *)srv;
138   #endif
139 <    v4 = (struct sockaddr_in *)srv;
139 >    v4 = (const struct sockaddr_in *)srv;
140  
141      /* could probably just memcmp(srv, inp, srv.ss_len) here
142       * but we'll air on the side of caution - stu
# Line 158 | Line 152 | res_ourserver(const struct irc_ssaddr *i
152                      sizeof(struct in6_addr)) == 0) ||
153                  (memcmp(&v6->sin6_addr.s6_addr, &in6addr_any,
154                          sizeof(struct in6_addr)) == 0))
155 <              return(1);
155 >              return 1;
156          break;
157   #endif
158        case AF_INET:
# Line 166 | Line 160 | res_ourserver(const struct irc_ssaddr *i
160            if (v4->sin_port == v4in->sin_port)
161              if ((v4->sin_addr.s_addr == INADDR_ANY) ||
162                  (v4->sin_addr.s_addr == v4in->sin_addr.s_addr))
163 <              return(1);
163 >              return 1;
164          break;
165        default:
166          break;
167      }
168    }
169  
170 <  return(0);
170 >  return 0;
171   }
172  
173   /*
# Line 198 | Line 192 | timeout_query_list(time_t now)
192      {
193        if (--request->retries <= 0)
194        {
195 <        (*request->query->callback)(request->query->ptr, NULL);
195 >        (*request->callback)(request->callback_ctx, NULL, NULL);
196          rem_request(request);
197          continue;
198        }
# Line 211 | Line 205 | timeout_query_list(time_t now)
205      }
206  
207      if ((next_time == 0) || timeout < next_time)
214    {
208        next_time = timeout;
216    }
209    }
210  
211 <  return((next_time > now) ? next_time : (now + AR_TTL));
211 >  return (next_time > now) ? next_time : (now + AR_TTL);
212   }
213  
214   /*
# Line 256 | Line 248 | start_resolver(void)
248   void
249   init_resolver(void)
250   {
251 < #ifdef HAVE_SRAND48
260 <  srand48(CurrentTime);
261 < #endif
251 >  dns_heap = BlockHeapCreate("dns", sizeof(struct reslist), DNS_HEAP_SIZE);
252    memset(&ResolverFileDescriptor, 0, sizeof(fde_t));
253    start_resolver();
254   }
# Line 275 | Line 265 | restart_resolver(void)
265   }
266  
267   /*
278 * add_local_domain - Add the domain to hostname, if it is missing
279 * (as suggested by eps@TOASTER.SFSU.EDU)
280 */
281 void
282 add_local_domain(char *hname, size_t size)
283 {
284  /* try to fix up unqualified names
285   */
286  if (strchr(hname, '.') == NULL)
287  {
288    if (irc_domain[0])
289    {
290      size_t len = strlen(hname);
291
292      if ((strlen(irc_domain) + len + 2) < size)
293      {
294        hname[len++] = '.';
295        strcpy(hname + len, irc_domain);
296      }
297    }
298  }
299 }
300
301 /*
268   * rem_request - remove a request from the list.
269   * This must also free any memory that has been allocated for
270   * temporary storage of DNS results.
# Line 307 | Line 273 | static void
273   rem_request(struct reslist *request)
274   {
275    dlinkDelete(&request->node, &request_list);
276 +
277    MyFree(request->name);
278 <  MyFree(request);
278 >  BlockHeapFree(dns_heap, request);
279   }
280  
281   /*
282   * make_request - Create a DNS request record for the server.
283   */
284   static struct reslist *
285 < make_request(struct DNSQuery* query)
285 > make_request(dns_callback_fnc callback, void *ctx)
286   {
287 <  struct reslist *request;
287 >  struct reslist *request = BlockHeapAlloc(dns_heap);
288  
289 <  request = (struct reslist *)MyMalloc(sizeof(struct reslist));
290 <  memset(request, 0, sizeof(struct reslist));
291 <
292 <  request->sentat  = CurrentTime;
293 <  request->retries = 3;
294 <  request->resend  = 1;
295 <  request->timeout = 4;    /* start at 4 and exponential inc. */
329 <  memset(&request->addr, 0, sizeof(request->addr));
330 <  request->query   = query;
331 <  request->state   = REQ_IDLE;
289 >  request->sentat       = CurrentTime;
290 >  request->retries      = 3;
291 >  request->resend       = 1;
292 >  request->timeout      = 4;    /* start at 4 and exponential inc. */
293 >  request->state        = REQ_IDLE;
294 >  request->callback     = callback;
295 >  request->callback_ctx = ctx;
296  
297    dlinkAdd(request, &request->node, &request_list);
298 <  return(request);
298 >  return request;
299   }
300  
301   /*
# Line 339 | Line 303 | make_request(struct DNSQuery* query)
303   * for which there no longer exist clients or conf lines.
304   */
305   void
306 < delete_resolver_queries(const struct DNSQuery *query)
306 > delete_resolver_queries(const void *vptr)
307   {
308 <  dlink_node *ptr;
345 <  dlink_node *next_ptr;
346 <  struct reslist *request;
308 >  dlink_node *ptr = NULL, *next_ptr = NULL;
309  
310    DLINK_FOREACH_SAFE(ptr, next_ptr, request_list.head)
311    {
312 <    if ((request = ptr->data) != NULL)
313 <    {
314 <      if (query == request->query)
315 <        rem_request(request);
354 <    }
312 >    struct reslist *request = ptr->data;
313 >
314 >    if (request->callback_ctx == vptr)
315 >      rem_request(request);
316    }
317   }
318  
# Line 383 | Line 344 | send_res_msg(const char *msg, int len, i
344        ++sent;
345    }
346  
347 <  return(sent);
347 >  return sent;
348   }
349  
350   /*
# Line 392 | Line 353 | send_res_msg(const char *msg, int len, i
353   static struct reslist *
354   find_id(int id)
355   {
356 <  dlink_node *ptr;
396 <  struct reslist *request;
356 >  dlink_node *ptr = NULL;
357  
358    DLINK_FOREACH(ptr, request_list.head)
359    {
360 <    request = ptr->data;
360 >    struct reslist *request = ptr->data;
361  
362      if (request->id == id)
363 <      return(request);
363 >      return request;
364    }
365  
366 <  return(NULL);
366 >  return NULL;
367   }
368  
369   /*
# Line 411 | Line 371 | find_id(int id)
371   *
372   */
373   void
374 < gethost_byname_type(const char *name, struct DNSQuery *query, int type)
374 > gethost_byname_type(dns_callback_fnc callback, void *ctx, const char *name, int type)
375   {
376 <  assert(name != 0);
377 <  do_query_name(query, name, NULL, type);
376 >  assert(name != NULL);
377 >  do_query_name(callback, ctx, name, NULL, type);
378   }
379  
380   /*
381   * gethost_byname - wrapper for _type - send T_AAAA first if IPV6 supported
382   */
383   void
384 < gethost_byname(const char *name, struct DNSQuery *query)
384 > gethost_byname(dns_callback_fnc callback, void *ctx, const char *name)
385   {
386   #ifdef IPV6
387 <  gethost_byname_type(name, query, T_AAAA);
387 >  gethost_byname_type(callback, ctx, name, T_AAAA);
388   #else
389 <  gethost_byname_type(name, query, T_A);
389 >  gethost_byname_type(callback, ctx, name, T_A);
390   #endif
391   }
392  
# Line 434 | Line 394 | gethost_byname(const char *name, struct
394   * gethost_byaddr - get host name from address
395   */
396   void
397 < gethost_byaddr(const struct irc_ssaddr *addr, struct DNSQuery *query)
397 > gethost_byaddr(dns_callback_fnc callback, void *ctx, const struct irc_ssaddr *addr)
398   {
399 <  do_query_number(query, addr, NULL);
399 >  do_query_number(callback, ctx, addr, NULL);
400   }
401  
402   /*
403   * do_query_name - nameserver lookup name
404   */
405   static void
406 < do_query_name(struct DNSQuery *query, const char *name,
406 > do_query_name(dns_callback_fnc callback, void *ctx, const char *name,
407                struct reslist *request, int type)
408   {
409    char host_name[HOSTLEN + 1];
410  
411 <  strlcpy(host_name, name, HOSTLEN);
452 <  add_local_domain(host_name, HOSTLEN);
411 >  strlcpy(host_name, name, sizeof(host_name));
412  
413    if (request == NULL)
414    {
415 <    request       = make_request(query);
416 <    request->name = (char *)MyMalloc(strlen(host_name) + 1);
415 >    request       = make_request(callback, ctx);
416 >    request->name = MyMalloc(strlen(host_name) + 1);
417      request->type = type;
418      strcpy(request->name, host_name);
419   #ifdef IPV6
420 <    if (type == T_A)
462 <      request->state = REQ_A;
463 <    else
420 >    if (type != T_A)
421        request->state = REQ_AAAA;
422 < #else
466 <    request->state = REQ_A;
422 >    else
423   #endif
424 +    request->state = REQ_A;
425    }
426  
427    request->type = type;
# Line 475 | Line 432 | do_query_name(struct DNSQuery *query, co
432   * do_query_number - Use this to do reverse IP# lookups.
433   */
434   static void
435 < do_query_number(struct DNSQuery *query, const struct irc_ssaddr *addr,
435 > do_query_number(dns_callback_fnc callback, void *ctx,
436 >                const struct irc_ssaddr *addr,
437                  struct reslist *request)
438   {
439    char ipbuf[128];
440    const unsigned char *cp;
441 < #ifdef IPV6
484 <  const char *intarpa;
485 < #endif
441 >
442    if (addr->ss.ss_family == AF_INET)
443    {
444 <    struct sockaddr_in *v4 = (struct sockaddr_in *)addr;
445 <    cp = (const unsigned char*)&v4->sin_addr.s_addr;
444 >    const struct sockaddr_in *v4 = (const struct sockaddr_in *)addr;
445 >    cp = (const unsigned char *)&v4->sin_addr.s_addr;
446  
447 <    ircsprintf(ipbuf, "%u.%u.%u.%u.in-addr.arpa.",
448 <               (unsigned int)(cp[3]), (unsigned int)(cp[2]),
449 <               (unsigned int)(cp[1]), (unsigned int)(cp[0]));
447 >    snprintf(ipbuf, sizeof(ipbuf), "%u.%u.%u.%u.in-addr.arpa.",
448 >             (unsigned int)(cp[3]), (unsigned int)(cp[2]),
449 >             (unsigned int)(cp[1]), (unsigned int)(cp[0]));
450    }
451   #ifdef IPV6
452    else if (addr->ss.ss_family == AF_INET6)
453    {
454 <    struct sockaddr_in6 *v6 = (struct sockaddr_in6 *)addr;
454 >    const struct sockaddr_in6 *v6 = (const struct sockaddr_in6 *)addr;
455      cp = (const unsigned char *)&v6->sin6_addr.s6_addr;
456  
457 <    if (request != NULL && request->state == REQ_INT)
458 <      intarpa = "int";
459 <    else
460 <      intarpa = "arpa";
461 <
462 <    (void)sprintf(ipbuf, "%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x."
463 <                  "%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.ip6.%s.",
464 <                  (unsigned int)(cp[15]&0xf), (unsigned int)(cp[15]>>4),
465 <                  (unsigned int)(cp[14]&0xf), (unsigned int)(cp[14]>>4),
466 <                  (unsigned int)(cp[13]&0xf), (unsigned int)(cp[13]>>4),
467 <                  (unsigned int)(cp[12]&0xf), (unsigned int)(cp[12]>>4),
468 <                  (unsigned int)(cp[11]&0xf), (unsigned int)(cp[11]>>4),
469 <                  (unsigned int)(cp[10]&0xf), (unsigned int)(cp[10]>>4),
470 <                  (unsigned int)(cp[9]&0xf), (unsigned int)(cp[9]>>4),
471 <                  (unsigned int)(cp[8]&0xf), (unsigned int)(cp[8]>>4),
472 <                  (unsigned int)(cp[7]&0xf), (unsigned int)(cp[7]>>4),
473 <                  (unsigned int)(cp[6]&0xf), (unsigned int)(cp[6]>>4),
474 <                  (unsigned int)(cp[5]&0xf), (unsigned int)(cp[5]>>4),
475 <                  (unsigned int)(cp[4]&0xf), (unsigned int)(cp[4]>>4),
520 <                  (unsigned int)(cp[3]&0xf), (unsigned int)(cp[3]>>4),
521 <                  (unsigned int)(cp[2]&0xf), (unsigned int)(cp[2]>>4),
522 <                  (unsigned int)(cp[1]&0xf), (unsigned int)(cp[1]>>4),
523 <                  (unsigned int)(cp[0]&0xf), (unsigned int)(cp[0]>>4), intarpa);
457 >    snprintf(ipbuf, sizeof(ipbuf),
458 >             "%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x."
459 >             "%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.ip6.arpa.",
460 >             (unsigned int)(cp[15] & 0xf), (unsigned int)(cp[15] >> 4),
461 >             (unsigned int)(cp[14] & 0xf), (unsigned int)(cp[14] >> 4),
462 >             (unsigned int)(cp[13] & 0xf), (unsigned int)(cp[13] >> 4),
463 >             (unsigned int)(cp[12] & 0xf), (unsigned int)(cp[12] >> 4),
464 >             (unsigned int)(cp[11] & 0xf), (unsigned int)(cp[11] >> 4),
465 >             (unsigned int)(cp[10] & 0xf), (unsigned int)(cp[10] >> 4),
466 >             (unsigned int)(cp[9] & 0xf), (unsigned int)(cp[9] >> 4),
467 >             (unsigned int)(cp[8] & 0xf), (unsigned int)(cp[8] >> 4),
468 >             (unsigned int)(cp[7] & 0xf), (unsigned int)(cp[7] >> 4),
469 >             (unsigned int)(cp[6] & 0xf), (unsigned int)(cp[6] >> 4),
470 >             (unsigned int)(cp[5] & 0xf), (unsigned int)(cp[5] >> 4),
471 >             (unsigned int)(cp[4] & 0xf), (unsigned int)(cp[4] >> 4),
472 >             (unsigned int)(cp[3] & 0xf), (unsigned int)(cp[3] >> 4),
473 >             (unsigned int)(cp[2] & 0xf), (unsigned int)(cp[2] >> 4),
474 >             (unsigned int)(cp[1] & 0xf), (unsigned int)(cp[1] >> 4),
475 >             (unsigned int)(cp[0] & 0xf), (unsigned int)(cp[0] >> 4));
476    }
477   #endif
478    if (request == NULL)
479    {
480 <    request       = make_request(query);
480 >    request       = make_request(callback, ctx);
481      request->type = T_PTR;
482      memcpy(&request->addr, addr, sizeof(struct irc_ssaddr));
483 <    request->name = (char *)MyMalloc(HOSTLEN + 1);
483 >    request->name = MyMalloc(HOSTLEN + 1);
484    }
485  
486    query_name(ipbuf, C_IN, T_PTR, request);
# Line 550 | Line 502 | query_name(const char *name, int query_c
502        (unsigned char *)buf, sizeof(buf))) > 0)
503    {
504      HEADER *header = (HEADER *)buf;
505 < #ifndef HAVE_LRAND48
554 <    int k = 0;
555 <    struct timeval tv;
556 < #endif
505 >
506      /*
507       * generate an unique id
508       * NOTE: we don't have to worry about converting this to and from
509       * network byte order, the nameserver does not interpret this value
510       * and returns it unchanged
511       */
563 #ifdef HAVE_LRAND48
564    do
565    {
566      header->id = (header->id + lrand48()) & 0xffff;
567    } while (find_id(header->id));
568 #else
569    gettimeofday(&tv, NULL);
512      do
513 <    {
514 <      header->id = (header->id + k + tv.tv_usec) & 0xffff;
515 <      k++;
574 <    } while (find_id(header->id));
575 < #endif /* HAVE_LRAND48 */
513 >      header->id = (header->id + genrand_int32()) & 0xffff;
514 >    while (find_id(header->id));
515 >
516      request->id = header->id;
517      ++request->sends;
518  
# Line 586 | Line 526 | resend_query(struct reslist *request)
526    if (request->resend == 0)
527      return;
528  
529 <  switch(request->type)
529 >  switch (request->type)
530    {
531      case T_PTR:
532 <      do_query_number(NULL, &request->addr, request);
532 >      do_query_number(NULL, NULL, &request->addr, request);
533        break;
534      case T_A:
535 <      do_query_name(NULL, request->name, request, request->type);
535 >      do_query_name(NULL, NULL, request->name, request, request->type);
536        break;
537   #ifdef IPV6
538      case T_AAAA:
539        /* didnt work, try A */
540        if (request->state == REQ_AAAA)
541 <        do_query_name(NULL, request->name, request, T_A);
541 >        do_query_name(NULL, NULL, request->name, request, T_A);
542   #endif
543      default:
544        break;
# Line 609 | Line 549 | resend_query(struct reslist *request)
549   * proc_answer - process name server reply
550   */
551   static int
552 < proc_answer(struct reslist *request, HEADER* header, char* buf, char* eob)
552 > proc_answer(struct reslist *request, HEADER *header, char *buf, char *eob)
553   {
554    char hostbuf[HOSTLEN + 100]; /* working buffer */
555    unsigned char *current;      /* current position in buf */
# Line 628 | Line 568 | proc_answer(struct reslist *request, HEA
568      if ((n = irc_dn_skipname(current, (unsigned char *)eob)) < 0)
569        break;
570  
571 <    current += (size_t) n + QFIXEDSZ;
571 >    current += (size_t)n + QFIXEDSZ;
572    }
573  
574    /*
# Line 641 | Line 581 | proc_answer(struct reslist *request, HEA
581      n = irc_dn_expand((unsigned char *)buf, (unsigned char *)eob, current,
582          hostbuf, sizeof(hostbuf));
583  
584 <    if (n < 0)
585 <    {
646 <      /*
647 <       * broken message
648 <       */
649 <      return(0);
650 <    }
651 <    else if (n == 0)
652 <    {
653 <      /*
654 <       * no more answers left
655 <       */
656 <      return(0);
657 <    }
584 >    if (n < 0 /* broken message */ || n == 0 /* no more answers left */)
585 >      return 0;
586  
587      hostbuf[HOSTLEN] = '\0';
588  
# Line 686 | Line 614 | proc_answer(struct reslist *request, HEA
614      {
615        case T_A:
616          if (request->type != T_A)
617 <          return(0);
617 >          return 0;
618  
619          /*
620           * check for invalid rd_length or too many addresses
621           */
622          if (rd_length != sizeof(struct in_addr))
623 <          return(0);
623 >          return 0;
624 >
625          v4 = (struct sockaddr_in *)&request->addr;
626          request->addr.ss_len = sizeof(struct sockaddr_in);
627          v4->sin_family = AF_INET;
628          memcpy(&v4->sin_addr, current, sizeof(struct in_addr));
629 <        return(1);
629 >        return 1;
630          break;
631   #ifdef IPV6
632        case T_AAAA:
633          if (request->type != T_AAAA)
634 <          return(0);
634 >          return 0;
635 >
636          if (rd_length != sizeof(struct in6_addr))
637 <          return(0);
637 >          return 0;
638 >
639          request->addr.ss_len = sizeof(struct sockaddr_in6);
640          v6 = (struct sockaddr_in6 *)&request->addr;
641          v6->sin6_family = AF_INET6;
642          memcpy(&v6->sin6_addr, current, sizeof(struct in6_addr));
643 <        return(1);
643 >        return 1;
644          break;
645   #endif
646        case T_PTR:
647          if (request->type != T_PTR)
648 <          return(0);
648 >          return 0;
649 >
650          n = irc_dn_expand((unsigned char *)buf, (unsigned char *)eob,
651              current, hostbuf, sizeof(hostbuf));
652 <        if (n < 0)
653 <          return(0); /* broken message */
722 <        else if (n == 0)
723 <          return(0); /* no more answers left */
724 <
725 <        strlcpy(request->name, hostbuf, HOSTLEN);
652 >        if (n < 0 /* broken message */ || n == 0 /* no more answers left */)
653 >          return 0;
654  
655 <        return(1);
655 >        strlcpy(request->name, hostbuf, HOSTLEN + 1);
656 >        return 1;
657          break;
658        case T_CNAME: /* first check we already havent started looking
659                         into a cname */
660          if (request->type != T_PTR)
661 <          return(0);
661 >          return 0;
662  
663          if (request->state == REQ_CNAME)
664          {
# Line 737 | Line 666 | proc_answer(struct reslist *request, HEA
666                              current, hostbuf, sizeof(hostbuf));
667  
668            if (n < 0)
669 <            return(0);
670 <          return(1);
669 >            return 0;
670 >          return 1;
671          }
672  
673          request->state = REQ_CNAME;
# Line 750 | Line 679 | proc_answer(struct reslist *request, HEA
679           * but its possible its just a broken nameserver with still
680           * valid answers. But lets do some rudimentary logging for now...
681           */
682 <        ilog(L_ERROR, "irc_res.c bogus type %d", type);
682 >        ilog(LOG_TYPE_IRCD, "irc_res.c bogus type %d", type);
683          break;
684      }
685    }
686  
687 <  return(1);
687 >  return 1;
688   }
689  
690   /*
# Line 764 | Line 693 | proc_answer(struct reslist *request, HEA
693   static void
694   res_readreply(fde_t *fd, void *data)
695   {
696 <  char buf[sizeof(HEADER) + MAXPACKET];
696 >  char buf[sizeof(HEADER) + MAXPACKET]
697 >        /* Sparc and alpha need 16bit-alignment for accessing header->id
698 >         * (which is uint16_t). Because of the header = (HEADER*) buf;
699 >         * lateron, this is neeeded. --FaUl
700 >         */
701 > #if defined(__sparc__) || defined(__alpha__)  
702 >          __attribute__((aligned (16)))
703 > #endif
704 >          ;
705    HEADER *header;
706    struct reslist *request = NULL;
770  struct DNSReply *reply  = NULL;
707    int rc;
772  int answer_count;
708    socklen_t len = sizeof(struct irc_ssaddr);
709    struct irc_ssaddr lsin;
710  
# Line 779 | Line 714 | res_readreply(fde_t *fd, void *data)
714     * interest where it'll instantly be ready for read :-) -- adrian
715     */
716    comm_setselect(fd, COMM_SELECT_READ, res_readreply, NULL, 0);
717 +
718    /* Better to cast the sizeof instead of rc */
719    if (rc <= (int)(sizeof(HEADER)))
720      return;
# Line 793 | Line 729 | res_readreply(fde_t *fd, void *data)
729    header->arcount = ntohs(header->arcount);
730  
731    /*
732 <   * response for an id which we have already received an answer for
797 <   * just ignore this response.
732 >   * check against possibly fake replies
733     */
734 <  if (0 == (request = find_id(header->id)))
734 >  if (!res_ourserver(&lsin))
735      return;
736  
737    /*
738 <   * check against possibly fake replies
738 >   * response for an id which we have already received an answer for
739 >   * just ignore this response.
740     */
741 <  if (!res_ourserver(&lsin))
741 >  if (!(request = find_id(header->id)))
742      return;
743  
744    if ((header->rcode != NO_ERRORS) || (header->ancount == 0))
745    {
746 <    if (SERVFAIL == header->rcode)
747 <      resend_query(request);
746 >    if (header->rcode == SERVFAIL || header->rcode == NXDOMAIN)
747 >    {
748 >      /*
749 >       * If a bad error was returned, stop here and don't
750 >       * send any more (no retries granted).
751 >       */
752 >      (*request->callback)(request->callback_ctx, NULL, NULL);
753 >      rem_request(request);
754 >    }
755 > #ifdef IPV6
756      else
757      {
758        /*
759         * If we havent already tried this, and we're looking up AAAA, try A
760         * now
761         */
818
819 #ifdef IPV6
762        if (request->state == REQ_AAAA && request->type == T_AAAA)
763        {
764          request->timeout += 4;
765          resend_query(request);
766        }
825      else if (request->type == T_PTR && request->state != REQ_INT &&
826               request->addr.ss.ss_family == AF_INET6)
827      {
828        request->state = REQ_INT;
829        request->timeout += 4;
830        request->retries--;
831        resend_query(request);
832      }
833      else
834 #endif
835      {
836        /*
837         * If a bad error was returned, we stop here and dont send
838         * send any more (no retries granted).
839         */
840        (*request->query->callback)(request->query->ptr, NULL);
841        rem_request(request);
842      }
767      }
768 + #endif
769  
770      return;
771    }
772 +
773    /*
774     * If this fails there was an error decoding the received packet,
775     * try it again and hope it works the next time.
776     */
777 <  answer_count = proc_answer(request, header, buf, buf + rc);
852 <
853 <  if (answer_count)
777 >  if (proc_answer(request, header, buf, buf + rc))
778    {
779      if (request->type == T_PTR)
780      {
# Line 860 | Line 784 | res_readreply(fde_t *fd, void *data)
784           * got a PTR response with no name, something bogus is happening
785           * don't bother trying again, the client address doesn't resolve
786           */
787 <        (*request->query->callback)(request->query->ptr, reply);
787 >        (*request->callback)(request->callback_ctx, NULL, NULL);
788          rem_request(request);
789          return;
790        }
# Line 872 | Line 796 | res_readreply(fde_t *fd, void *data)
796         */
797   #ifdef IPV6
798        if (request->addr.ss.ss_family == AF_INET6)
799 <        gethost_byname_type(request->name, request->query, T_AAAA);
799 >        gethost_byname_type(request->callback, request->callback_ctx, request->name, T_AAAA);
800        else
801   #endif
802 <      gethost_byname_type(request->name, request->query, T_A);
802 >      gethost_byname_type(request->callback, request->callback_ctx, request->name, T_A);
803        rem_request(request);
804      }
805      else
# Line 883 | Line 807 | res_readreply(fde_t *fd, void *data)
807        /*
808         * got a name and address response, client resolved
809         */
810 <      reply = make_dnsreply(request);
887 <      (*request->query->callback)(request->query->ptr, reply);
888 <      MyFree(reply);
810 >      (*request->callback)(request->callback_ctx, &request->addr, request->name);
811        rem_request(request);
812      }
813    }
# Line 901 | Line 823 | res_readreply(fde_t *fd, void *data)
823    }
824   }
825  
904 static struct DNSReply *
905 make_dnsreply(struct reslist *request)
906 {
907  struct DNSReply *cp;
908  assert(request != 0);
909
910  cp = (struct DNSReply *)MyMalloc(sizeof(struct DNSReply));
911
912  cp->h_name = request->name;
913  memcpy(&cp->addr, &request->addr, sizeof(cp->addr));
914  return(cp);
915 }
916
826   void
827   report_dns_servers(struct Client *source_p)
828   {
829    int i;
830 <  char ipaddr[HOSTIPLEN];
830 >  char ipaddr[HOSTIPLEN + 1];
831  
832    for (i = 0; i < irc_nscount; i++)
833    {
834 <    irc_getnameinfo((struct sockaddr *)&(irc_nsaddr_list[i]),
835 <                    irc_nsaddr_list[i].ss_len, ipaddr, HOSTIPLEN, NULL, 0,
836 <                    NI_NUMERICHOST);
834 >    getnameinfo((struct sockaddr *)&(irc_nsaddr_list[i]),
835 >                irc_nsaddr_list[i].ss_len, ipaddr,
836 >                sizeof(ipaddr), NULL, 0, NI_NUMERICHOST);
837      sendto_one(source_p, form_str(RPL_STATSALINE),
838                 me.name, source_p->name, ipaddr);
839    }

Comparing:
ircd-hybrid/src/irc_res.c (property svn:keywords), Revision 31 by knight, Sun Oct 2 20:34:05 2005 UTC vs.
ircd-hybrid-8/src/irc_res.c (property svn:keywords), Revision 1247 by michael, Sat Oct 1 07:54:24 2011 UTC

# Line 1 | Line 1
1 < "Id Revision"
1 > Id Revision

Diff Legend

Removed lines
+ Added lines
< Changed lines (old)
> Changed lines (new)