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-7.2/src/irc_res.c (file contents), Revision 447 by db, Sun Feb 12 02:59:16 2006 UTC vs.
ircd-hybrid/trunk/src/irc_res.c (file contents), Revision 2916 by michael, Sat Jan 25 21:09:18 2014 UTC

# Line 1 | Line 1
1   /*
2 + *  ircd-hybrid: an advanced, lightweight Internet Relay Chat Daemon (ircd)
3 + *
4 + *  Copyright (c) 1997-2014 ircd-hybrid development team
5 + *
6 + *  This program is free software; you can redistribute it and/or modify
7 + *  it under the terms of the GNU General Public License as published by
8 + *  the Free Software Foundation; either version 2 of the License, or
9 + *  (at your option) any later version.
10 + *
11 + *  This program is distributed in the hope that it will be useful,
12 + *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13 + *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 + *  GNU General Public License for more details.
15 + *
16 + *  You should have received a copy of the GNU General Public License
17 + *  along with this program; if not, write to the Free Software
18 + *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
19 + *  USA
20 + */
21 +
22 + /*! \file irc_res.c
23 + * \brief ircd resolver functions
24 + * \version $Id$
25 + */
26 +
27 + /*
28   * A rewrite of Darren Reeds original res.c As there is nothing
29   * left of Darrens original code, this is now licensed by the hybrid group.
30   * (Well, some of the function names are the same, and bits of the structs..)
# Line 14 | Line 40
40   *     --Bleep (Thomas Helvey <tomh@inxpress.net>)
41   *
42   * This was all needlessly complicated for irc. Simplified. No more hostent
43 < * All we really care about is the IP -> hostname mappings. Thats all.
43 > * All we really care about is the IP -> hostname mappings. Thats all.
44   *
45   * Apr 28, 2003 --cryogen and Dianora
46   */
47  
48   #include "stdinc.h"
23 #include "tools.h"
24 #include "client.h"
49   #include "list.h"
50 < #include "common.h"
50 > #include "client.h"
51   #include "event.h"
52   #include "irc_string.h"
29 #include "sprintf_irc.h"
53   #include "ircd.h"
54   #include "numeric.h"
55 < #include "restart.h"
55 > #include "rng_mt.h"
56   #include "fdlist.h"
34 #include "fileio.h" /* for fbopen / fbclose / fbputs */
57   #include "s_bsd.h"
58 < #include "s_log.h"
58 > #include "log.h"
59 > #include "s_misc.h"
60   #include "send.h"
61   #include "memory.h"
62 + #include "mempool.h"
63   #include "irc_res.h"
64   #include "irc_reslib.h"
41 #include "irc_getnameinfo.h"
65  
66   #if (CHAR_BIT != 8)
67 < #error this code needs to be able to address individual octets
67 > #error this code needs to be able to address individual octets
68   #endif
69  
70   static PF res_readreply;
# Line 61 | Line 84 | static PF res_readreply;
84   #define RDLENGTH_SIZE     (size_t)2
85   #define ANSWER_FIXED_SIZE (TYPE_SIZE + CLASS_SIZE + TTL_SIZE + RDLENGTH_SIZE)
86  
87 < typedef enum
87 > typedef enum
88   {
89    REQ_IDLE,  /* We're doing not much at all */
90    REQ_PTR,   /* Looking up a PTR */
# Line 69 | Line 92 | typedef enum
92   #ifdef IPV6
93    REQ_AAAA,  /* Looking up an AAAA */
94   #endif
95 <  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 */
95 >  REQ_CNAME  /* We got a CNAME in response, we better get a real answer next */
96   } request_state;
97  
98 < struct reslist
98 > struct reslist
99   {
100    dlink_node node;
101    int id;
# Line 88 | Line 110 | struct reslist
110    time_t timeout;
111    struct irc_ssaddr addr;
112    char *name;
113 <  struct DNSQuery *query;  /* query callback for this request */
113 >  dns_callback_fnc callback;
114 >  void *callback_ctx;
115   };
116  
117   static fde_t ResolverFileDescriptor;
118 < static dlink_list request_list    = { NULL, NULL, 0 };
118 > static dlink_list request_list = { NULL, NULL, 0 };
119 > static mp_pool_t *dns_pool = NULL;
120  
121 < static void rem_request(struct reslist *request);
122 < static struct reslist *make_request(struct DNSQuery *query);
123 < static void do_query_name(struct DNSQuery *query,
124 <                          const char* name, struct reslist *request, int);
125 < static void do_query_number(struct DNSQuery *query,
121 > static void rem_request(struct reslist *);
122 > static struct reslist *make_request(dns_callback_fnc, void *);
123 > static void do_query_name(dns_callback_fnc, void *,
124 >                          const char *, struct reslist *, int);
125 > static void do_query_number(dns_callback_fnc, void *,
126                              const struct irc_ssaddr *,
127 <                            struct reslist *request);
128 < static void query_name(const char *name, int query_class, int query_type,
129 <                       struct reslist *request);
130 < static int send_res_msg(const char *buf, int len, int count);
131 < static void resend_query(struct reslist *request);
132 < 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];
127 >                            struct reslist *);
128 > static void query_name(const char *, int, int, struct reslist *);
129 > static int send_res_msg(const char *, int, int);
130 > static void resend_query(struct reslist *);
131 > static int proc_answer(struct reslist *, HEADER *, char *, char *);
132 > static struct reslist *find_id(int);
133  
134  
135   /*
# Line 126 | Line 144 | extern char irc_domain[HOSTLEN+1];
144   *      revised for ircd, cryogen(stu) may03
145   */
146   static int
147 < res_ourserver(const struct irc_ssaddr *inp)
147 > res_ourserver(const struct irc_ssaddr *inp)
148   {
149   #ifdef IPV6
150 <  struct sockaddr_in6 *v6;
151 <  struct sockaddr_in6 *v6in = (struct sockaddr_in6 *)inp;
150 >  const struct sockaddr_in6 *v6;
151 >  const struct sockaddr_in6 *v6in = (const struct sockaddr_in6 *)inp;
152   #endif
153 <  struct sockaddr_in *v4;
154 <  struct sockaddr_in *v4in = (struct sockaddr_in *)inp;
153 >  const struct sockaddr_in *v4;
154 >  const struct sockaddr_in *v4in = (const struct sockaddr_in *)inp;
155    int ns;
156  
157 <  for (ns = 0;  ns < irc_nscount;  ns++)
157 >  for (ns = 0; ns < irc_nscount; ++ns)
158    {
159      const struct irc_ssaddr *srv = &irc_nsaddr_list[ns];
160   #ifdef IPV6
161 <    v6 = (struct sockaddr_in6 *)srv;
161 >    v6 = (const struct sockaddr_in6 *)srv;
162   #endif
163 <    v4 = (struct sockaddr_in *)srv;
163 >    v4 = (const struct sockaddr_in *)srv;
164  
165      /* could probably just memcmp(srv, inp, srv.ss_len) here
166       * but we'll air on the side of caution - stu
# Line 154 | Line 172 | res_ourserver(const struct irc_ssaddr *i
172        case AF_INET6:
173          if (srv->ss.ss_family == inp->ss.ss_family)
174            if (v6->sin6_port == v6in->sin6_port)
175 <            if ((memcmp(&v6->sin6_addr.s6_addr, &v6in->sin6_addr.s6_addr,
176 <                    sizeof(struct in6_addr)) == 0) ||
177 <                (memcmp(&v6->sin6_addr.s6_addr, &in6addr_any,
160 <                        sizeof(struct in6_addr)) == 0))
161 <              return(1);
175 >            if (!memcmp(&v6->sin6_addr.s6_addr, &v6in->sin6_addr.s6_addr,
176 >                        sizeof(struct in6_addr)))
177 >              return 1;
178          break;
179   #endif
180        case AF_INET:
181          if (srv->ss.ss_family == inp->ss.ss_family)
182            if (v4->sin_port == v4in->sin_port)
183 <            if ((v4->sin_addr.s_addr == INADDR_ANY) ||
184 <                (v4->sin_addr.s_addr == v4in->sin_addr.s_addr))
169 <              return(1);
183 >            if (v4->sin_addr.s_addr == v4in->sin_addr.s_addr)
184 >              return 1;
185          break;
186        default:
187          break;
188      }
189    }
190  
191 <  return(0);
191 >  return 0;
192   }
193  
194   /*
195 < * timeout_query_list - Remove queries from the list which have been
195 > * timeout_query_list - Remove queries from the list which have been
196   * there too long without being resolved.
197   */
198   static time_t
# Line 198 | Line 213 | timeout_query_list(time_t now)
213      {
214        if (--request->retries <= 0)
215        {
216 <        (*request->query->callback)(request->query->ptr, NULL);
216 >        (*request->callback)(request->callback_ctx, NULL, NULL);
217          rem_request(request);
218          continue;
219        }
# Line 211 | Line 226 | timeout_query_list(time_t now)
226      }
227  
228      if ((next_time == 0) || timeout < next_time)
214    {
229        next_time = timeout;
216    }
230    }
231  
232 <  return((next_time > now) ? next_time : (now + AR_TTL));
232 >  return (next_time > now) ? next_time : (now + AR_TTL);
233   }
234  
235   /*
# Line 256 | Line 269 | start_resolver(void)
269   void
270   init_resolver(void)
271   {
272 < #ifdef HAVE_SRAND48
260 <  srand48(CurrentTime);
261 < #endif
272 >  dns_pool = mp_pool_new(sizeof(struct reslist), MP_CHUNK_SIZE_DNS);
273    memset(&ResolverFileDescriptor, 0, sizeof(fde_t));
274    start_resolver();
275   }
# Line 275 | Line 286 | restart_resolver(void)
286   }
287  
288   /*
289 < * add_local_domain - Add the domain to hostname, if it is missing
290 < * (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 < /*
302 < * rem_request - remove a request from the list.
303 < * This must also free any memory that has been allocated for
289 > * rem_request - remove a request from the list.
290 > * This must also free any memory that has been allocated for
291   * temporary storage of DNS results.
292   */
293   static void
294   rem_request(struct reslist *request)
295   {
296    dlinkDelete(&request->node, &request_list);
297 +
298    MyFree(request->name);
299 <  MyFree(request);
299 >  mp_pool_release(request);
300   }
301  
302   /*
303   * make_request - Create a DNS request record for the server.
304   */
305   static struct reslist *
306 < make_request(struct DNSQuery* query)
306 > make_request(dns_callback_fnc callback, void *ctx)
307   {
308 <  struct reslist *request;
308 >  struct reslist *request = mp_pool_get(dns_pool);
309  
310 <  request = (struct reslist *)MyMalloc(sizeof(struct reslist));
311 <
312 <  request->sentat  = CurrentTime;
313 <  request->retries = 3;
314 <  request->resend  = 1;
315 <  request->timeout = 4;    /* start at 4 and exponential inc. */
316 <  request->query   = query;
317 <  request->state   = REQ_IDLE;
310 >  memset(request, 0, sizeof(*request));
311 >  request->sentat       = CurrentTime;
312 >  request->retries      = 3;
313 >  request->resend       = 1;
314 >  request->timeout      = 4;    /* start at 4 and exponential inc. */
315 >  request->state        = REQ_IDLE;
316 >  request->callback     = callback;
317 >  request->callback_ctx = ctx;
318  
319    dlinkAdd(request, &request->node, &request_list);
320 <  return(request);
320 >  return request;
321   }
322  
323   /*
324 < * delete_resolver_queries - cleanup outstanding queries
324 > * delete_resolver_queries - cleanup outstanding queries
325   * for which there no longer exist clients or conf lines.
326   */
327   void
328 < delete_resolver_queries(const struct DNSQuery *query)
328 > delete_resolver_queries(const void *vptr)
329   {
330 <  dlink_node *ptr;
343 <  dlink_node *next_ptr;
344 <  struct reslist *request;
330 >  dlink_node *ptr = NULL, *next_ptr = NULL;
331  
332    DLINK_FOREACH_SAFE(ptr, next_ptr, request_list.head)
333    {
334 <    if ((request = ptr->data) != NULL)
335 <    {
336 <      if (query == request->query)
337 <        rem_request(request);
352 <    }
334 >    struct reslist *request = ptr->data;
335 >
336 >    if (request->callback_ctx == vptr)
337 >      rem_request(request);
338    }
339   }
340  
# Line 357 | Line 342 | delete_resolver_queries(const struct DNS
342   * send_res_msg - sends msg to all nameservers found in the "_res" structure.
343   * This should reflect /etc/resolv.conf. We will get responses
344   * which arent needed but is easier than checking to see if nameserver
345 < * isnt present. Returns number of messages successfully sent to
345 > * isnt present. Returns number of messages successfully sent to
346   * nameservers or -1 if no successful sends.
347   */
348   static int
# Line 375 | Line 360 | send_res_msg(const char *msg, int len, i
360  
361    for (i = 0; i < max_queries; i++)
362    {
363 <    if (sendto(ResolverFileDescriptor.fd, msg, len, 0,
364 <        (struct sockaddr*)&(irc_nsaddr_list[i]),
365 <        irc_nsaddr_list[i].ss_len) == len)
363 >    if (sendto(ResolverFileDescriptor.fd, msg, len, 0,
364 >        (struct sockaddr*)&(irc_nsaddr_list[i]),
365 >        irc_nsaddr_list[i].ss_len) == len)
366        ++sent;
367    }
368  
369 <  return(sent);
369 >  return sent;
370   }
371  
372   /*
# Line 390 | Line 375 | send_res_msg(const char *msg, int len, i
375   static struct reslist *
376   find_id(int id)
377   {
378 <  dlink_node *ptr;
394 <  struct reslist *request;
378 >  dlink_node *ptr = NULL;
379  
380    DLINK_FOREACH(ptr, request_list.head)
381    {
382 <    request = ptr->data;
382 >    struct reslist *request = ptr->data;
383  
384      if (request->id == id)
385 <      return(request);
385 >      return request;
386    }
387  
388 <  return(NULL);
388 >  return NULL;
389   }
390  
391 < /*
391 > /*
392   * gethost_byname_type - get host address from name
393   *
394   */
395   void
396 < gethost_byname_type(const char *name, struct DNSQuery *query, int type)
396 > gethost_byname_type(dns_callback_fnc callback, void *ctx, const char *name, int type)
397   {
398 <  assert(name != 0);
399 <  do_query_name(query, name, NULL, type);
398 >  assert(name != NULL);
399 >  do_query_name(callback, ctx, name, NULL, type);
400   }
401  
402   /*
403   * gethost_byname - wrapper for _type - send T_AAAA first if IPV6 supported
404   */
405   void
406 < gethost_byname(const char *name, struct DNSQuery *query)
406 > gethost_byname(dns_callback_fnc callback, void *ctx, const char *name)
407   {
408   #ifdef IPV6
409 <  gethost_byname_type(name, query, T_AAAA);
409 >  gethost_byname_type(callback, ctx, name, T_AAAA);
410   #else
411 <  gethost_byname_type(name, query, T_A);
411 >  gethost_byname_type(callback, ctx, name, T_A);
412   #endif
413   }
414  
# Line 432 | Line 416 | gethost_byname(const char *name, struct
416   * gethost_byaddr - get host name from address
417   */
418   void
419 < gethost_byaddr(const struct irc_ssaddr *addr, struct DNSQuery *query)
419 > gethost_byaddr(dns_callback_fnc callback, void *ctx, const struct irc_ssaddr *addr)
420   {
421 <  do_query_number(query, addr, NULL);
421 >  do_query_number(callback, ctx, addr, NULL);
422   }
423  
424   /*
425   * do_query_name - nameserver lookup name
426   */
427   static void
428 < do_query_name(struct DNSQuery *query, const char *name,
428 > do_query_name(dns_callback_fnc callback, void *ctx, const char *name,
429                struct reslist *request, int type)
430   {
431    char host_name[HOSTLEN + 1];
432  
433 <  strlcpy(host_name, name, HOSTLEN);
450 <  add_local_domain(host_name, HOSTLEN);
433 >  strlcpy(host_name, name, sizeof(host_name));
434  
435    if (request == NULL)
436    {
437 <    request       = make_request(query);
438 <    request->name = (char *)MyMalloc(strlen(host_name) + 1);
437 >    request       = make_request(callback, ctx);
438 >    request->name = MyMalloc(strlen(host_name) + 1);
439      request->type = type;
440      strcpy(request->name, host_name);
441   #ifdef IPV6
442 <    if (type == T_A)
460 <      request->state = REQ_A;
461 <    else
442 >    if (type != T_A)
443        request->state = REQ_AAAA;
444 < #else
464 <    request->state = REQ_A;
444 >    else
445   #endif
446 +    request->state = REQ_A;
447    }
448  
449    request->type = type;
# Line 473 | Line 454 | do_query_name(struct DNSQuery *query, co
454   * do_query_number - Use this to do reverse IP# lookups.
455   */
456   static void
457 < do_query_number(struct DNSQuery *query, const struct irc_ssaddr *addr,
457 > do_query_number(dns_callback_fnc callback, void *ctx,
458 >                const struct irc_ssaddr *addr,
459                  struct reslist *request)
460   {
461    char ipbuf[128];
462    const unsigned char *cp;
463 < #ifdef IPV6
482 <  const char *intarpa;
483 < #endif
463 >
464    if (addr->ss.ss_family == AF_INET)
465    {
466 <    struct sockaddr_in *v4 = (struct sockaddr_in *)addr;
467 <    cp = (const unsigned char*)&v4->sin_addr.s_addr;
466 >    const struct sockaddr_in *v4 = (const struct sockaddr_in *)addr;
467 >    cp = (const unsigned char *)&v4->sin_addr.s_addr;
468  
469 <    ircsprintf(ipbuf, "%u.%u.%u.%u.in-addr.arpa.",
470 <               (unsigned int)(cp[3]), (unsigned int)(cp[2]),
471 <               (unsigned int)(cp[1]), (unsigned int)(cp[0]));
469 >    snprintf(ipbuf, sizeof(ipbuf), "%u.%u.%u.%u.in-addr.arpa.",
470 >             (unsigned int)(cp[3]), (unsigned int)(cp[2]),
471 >             (unsigned int)(cp[1]), (unsigned int)(cp[0]));
472    }
473   #ifdef IPV6
474    else if (addr->ss.ss_family == AF_INET6)
475    {
476 <    struct sockaddr_in6 *v6 = (struct sockaddr_in6 *)addr;
476 >    const struct sockaddr_in6 *v6 = (const struct sockaddr_in6 *)addr;
477      cp = (const unsigned char *)&v6->sin6_addr.s6_addr;
478  
479 <    if (request != NULL && request->state == REQ_INT)
480 <      intarpa = "int";
481 <    else
482 <      intarpa = "arpa";
483 <
484 <    (void)sprintf(ipbuf, "%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x."
485 <                  "%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.ip6.%s.",
486 <                  (unsigned int)(cp[15]&0xf), (unsigned int)(cp[15]>>4),
487 <                  (unsigned int)(cp[14]&0xf), (unsigned int)(cp[14]>>4),
488 <                  (unsigned int)(cp[13]&0xf), (unsigned int)(cp[13]>>4),
489 <                  (unsigned int)(cp[12]&0xf), (unsigned int)(cp[12]>>4),
490 <                  (unsigned int)(cp[11]&0xf), (unsigned int)(cp[11]>>4),
491 <                  (unsigned int)(cp[10]&0xf), (unsigned int)(cp[10]>>4),
492 <                  (unsigned int)(cp[9]&0xf), (unsigned int)(cp[9]>>4),
493 <                  (unsigned int)(cp[8]&0xf), (unsigned int)(cp[8]>>4),
494 <                  (unsigned int)(cp[7]&0xf), (unsigned int)(cp[7]>>4),
495 <                  (unsigned int)(cp[6]&0xf), (unsigned int)(cp[6]>>4),
496 <                  (unsigned int)(cp[5]&0xf), (unsigned int)(cp[5]>>4),
497 <                  (unsigned int)(cp[4]&0xf), (unsigned int)(cp[4]>>4),
518 <                  (unsigned int)(cp[3]&0xf), (unsigned int)(cp[3]>>4),
519 <                  (unsigned int)(cp[2]&0xf), (unsigned int)(cp[2]>>4),
520 <                  (unsigned int)(cp[1]&0xf), (unsigned int)(cp[1]>>4),
521 <                  (unsigned int)(cp[0]&0xf), (unsigned int)(cp[0]>>4), intarpa);
479 >    snprintf(ipbuf, sizeof(ipbuf),
480 >             "%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x."
481 >             "%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.ip6.arpa.",
482 >             (unsigned int)(cp[15] & 0xf), (unsigned int)(cp[15] >> 4),
483 >             (unsigned int)(cp[14] & 0xf), (unsigned int)(cp[14] >> 4),
484 >             (unsigned int)(cp[13] & 0xf), (unsigned int)(cp[13] >> 4),
485 >             (unsigned int)(cp[12] & 0xf), (unsigned int)(cp[12] >> 4),
486 >             (unsigned int)(cp[11] & 0xf), (unsigned int)(cp[11] >> 4),
487 >             (unsigned int)(cp[10] & 0xf), (unsigned int)(cp[10] >> 4),
488 >             (unsigned int)(cp[9] & 0xf), (unsigned int)(cp[9] >> 4),
489 >             (unsigned int)(cp[8] & 0xf), (unsigned int)(cp[8] >> 4),
490 >             (unsigned int)(cp[7] & 0xf), (unsigned int)(cp[7] >> 4),
491 >             (unsigned int)(cp[6] & 0xf), (unsigned int)(cp[6] >> 4),
492 >             (unsigned int)(cp[5] & 0xf), (unsigned int)(cp[5] >> 4),
493 >             (unsigned int)(cp[4] & 0xf), (unsigned int)(cp[4] >> 4),
494 >             (unsigned int)(cp[3] & 0xf), (unsigned int)(cp[3] >> 4),
495 >             (unsigned int)(cp[2] & 0xf), (unsigned int)(cp[2] >> 4),
496 >             (unsigned int)(cp[1] & 0xf), (unsigned int)(cp[1] >> 4),
497 >             (unsigned int)(cp[0] & 0xf), (unsigned int)(cp[0] >> 4));
498    }
499   #endif
500    if (request == NULL)
501    {
502 <    request       = make_request(query);
502 >    request       = make_request(callback, ctx);
503      request->type = T_PTR;
504      memcpy(&request->addr, addr, sizeof(struct irc_ssaddr));
505 <    request->name = (char *)MyMalloc(HOSTLEN + 1);
505 >    request->name = MyMalloc(HOSTLEN + 1);
506    }
507  
508    query_name(ipbuf, C_IN, T_PTR, request);
# Line 544 | Line 520 | query_name(const char *name, int query_c
520  
521    memset(buf, 0, sizeof(buf));
522  
523 <  if ((request_len = irc_res_mkquery(name, query_class, type,
523 >  if ((request_len = irc_res_mkquery(name, query_class, type,
524        (unsigned char *)buf, sizeof(buf))) > 0)
525    {
526      HEADER *header = (HEADER *)buf;
527 < #ifndef HAVE_LRAND48
552 <    int k = 0;
553 <    struct timeval tv;
554 < #endif
527 >
528      /*
529       * generate an unique id
530       * NOTE: we don't have to worry about converting this to and from
531       * network byte order, the nameserver does not interpret this value
532       * and returns it unchanged
533       */
561 #ifdef HAVE_LRAND48
534      do
535 <    {
536 <      header->id = (header->id + lrand48()) & 0xffff;
537 <    } while (find_id(header->id));
566 < #else
567 <    gettimeofday(&tv, NULL);
568 <    do
569 <    {
570 <      header->id = (header->id + k + tv.tv_usec) & 0xffff;
571 <      k++;
572 <    } while (find_id(header->id));
573 < #endif /* HAVE_LRAND48 */
535 >      header->id = (header->id + genrand_int32()) & 0xffff;
536 >    while (find_id(header->id));
537 >
538      request->id = header->id;
539      ++request->sends;
540  
# Line 584 | Line 548 | resend_query(struct reslist *request)
548    if (request->resend == 0)
549      return;
550  
551 <  switch(request->type)
551 >  switch (request->type)
552    {
553      case T_PTR:
554 <      do_query_number(NULL, &request->addr, request);
554 >      do_query_number(NULL, NULL, &request->addr, request);
555        break;
556      case T_A:
557 <      do_query_name(NULL, request->name, request, request->type);
557 >      do_query_name(NULL, NULL, request->name, request, request->type);
558        break;
559   #ifdef IPV6
560      case T_AAAA:
561        /* didnt work, try A */
562        if (request->state == REQ_AAAA)
563 <        do_query_name(NULL, request->name, request, T_A);
563 >        do_query_name(NULL, NULL, request->name, request, T_A);
564   #endif
565      default:
566        break;
# Line 607 | Line 571 | resend_query(struct reslist *request)
571   * proc_answer - process name server reply
572   */
573   static int
574 < proc_answer(struct reslist *request, HEADER* header, char* buf, char* eob)
574 > proc_answer(struct reslist *request, HEADER *header, char *buf, char *eob)
575   {
576    char hostbuf[HOSTLEN + 100]; /* working buffer */
577    unsigned char *current;      /* current position in buf */
# Line 626 | Line 590 | proc_answer(struct reslist *request, HEA
590      if ((n = irc_dn_skipname(current, (unsigned char *)eob)) < 0)
591        break;
592  
593 <    current += (size_t) n + QFIXEDSZ;
593 >    current += (size_t)n + QFIXEDSZ;
594    }
595  
596    /*
# Line 639 | Line 603 | proc_answer(struct reslist *request, HEA
603      n = irc_dn_expand((unsigned char *)buf, (unsigned char *)eob, current,
604          hostbuf, sizeof(hostbuf));
605  
606 <    if (n < 0)
607 <    {
644 <      /*
645 <       * broken message
646 <       */
647 <      return(0);
648 <    }
649 <    else if (n == 0)
650 <    {
651 <      /*
652 <       * no more answers left
653 <       */
654 <      return(0);
655 <    }
606 >    if (n < 0 /* broken message */ || n == 0 /* no more answers left */)
607 >      return 0;
608  
609      hostbuf[HOSTLEN] = '\0';
610  
# Line 677 | Line 629 | proc_answer(struct reslist *request, HEA
629      rd_length = irc_ns_get16(current);
630      current += RDLENGTH_SIZE;
631  
632 <    /*
633 <     * Wait to set request->type until we verify this structure
632 >    /*
633 >     * Wait to set request->type until we verify this structure
634       */
635      switch (type)
636      {
637        case T_A:
638          if (request->type != T_A)
639 <          return(0);
639 >          return 0;
640  
641          /*
642           * check for invalid rd_length or too many addresses
643           */
644          if (rd_length != sizeof(struct in_addr))
645 <          return(0);
645 >          return 0;
646 >
647          v4 = (struct sockaddr_in *)&request->addr;
648          request->addr.ss_len = sizeof(struct sockaddr_in);
649          v4->sin_family = AF_INET;
650          memcpy(&v4->sin_addr, current, sizeof(struct in_addr));
651 <        return(1);
651 >        return 1;
652          break;
653   #ifdef IPV6
654        case T_AAAA:
655          if (request->type != T_AAAA)
656 <          return(0);
656 >          return 0;
657 >
658          if (rd_length != sizeof(struct in6_addr))
659 <          return(0);
659 >          return 0;
660 >
661          request->addr.ss_len = sizeof(struct sockaddr_in6);
662          v6 = (struct sockaddr_in6 *)&request->addr;
663          v6->sin6_family = AF_INET6;
664          memcpy(&v6->sin6_addr, current, sizeof(struct in6_addr));
665 <        return(1);
665 >        return 1;
666          break;
667   #endif
668        case T_PTR:
669          if (request->type != T_PTR)
670 <          return(0);
670 >          return 0;
671 >
672          n = irc_dn_expand((unsigned char *)buf, (unsigned char *)eob,
673              current, hostbuf, sizeof(hostbuf));
674 <        if (n < 0)
675 <          return(0); /* broken message */
720 <        else if (n == 0)
721 <          return(0); /* no more answers left */
722 <
723 <        strlcpy(request->name, hostbuf, HOSTLEN);
674 >        if (n < 0 /* broken message */ || n == 0 /* no more answers left */)
675 >          return 0;
676  
677 <        return(1);
677 >        strlcpy(request->name, hostbuf, HOSTLEN + 1);
678 >        return 1;
679          break;
680 <      case T_CNAME: /* first check we already havent started looking
680 >      case T_CNAME: /* first check we already havent started looking
681                         into a cname */
682 <        if (request->type != T_PTR)
683 <          return(0);
682 >        if (request->type != T_PTR)
683 >          return 0;
684  
685          if (request->state == REQ_CNAME)
686          {
# Line 735 | Line 688 | proc_answer(struct reslist *request, HEA
688                              current, hostbuf, sizeof(hostbuf));
689  
690            if (n < 0)
691 <            return(0);
692 <          return(1);
691 >            return 0;
692 >          return 1;
693          }
694  
695          request->state = REQ_CNAME;
696          current += rd_length;
697          break;
698 <        
698 >
699        default:
700          /* XXX I'd rather just throw away the entire bogus thing
701           * but its possible its just a broken nameserver with still
702           * valid answers. But lets do some rudimentary logging for now...
703           */
704 <        ilog(L_ERROR, "irc_res.c bogus type %d", type);
704 >        ilog(LOG_TYPE_IRCD, "irc_res.c bogus type %d", type);
705          break;
706      }
707    }
708  
709 <  return(1);
709 >  return 1;
710   }
711  
712   /*
# Line 763 | Line 716 | static void
716   res_readreply(fde_t *fd, void *data)
717   {
718    char buf[sizeof(HEADER) + MAXPACKET]
719 <        /* Sparc and alpha need 16bit-alignment for accessing header->id
720 <         * (which is uint16_t). Because of the header = (HEADER*) buf;
719 >        /* Sparc and alpha need 16bit-alignment for accessing header->id
720 >         * (which is uint16_t). Because of the header = (HEADER*) buf;
721           * lateron, this is neeeded. --FaUl
722           */
723 < #if defined(__sparc__) || defined(__alpha__)  
723 > #if defined(__sparc__) || defined(__alpha__)
724            __attribute__((aligned (16)))
725 < #endif
725 > #endif
726            ;
727    HEADER *header;
728    struct reslist *request = NULL;
776  struct DNSReply *reply  = NULL;
729    int rc;
778  int answer_count;
730    socklen_t len = sizeof(struct irc_ssaddr);
731    struct irc_ssaddr lsin;
732  
# Line 785 | Line 736 | res_readreply(fde_t *fd, void *data)
736     * interest where it'll instantly be ready for read :-) -- adrian
737     */
738    comm_setselect(fd, COMM_SELECT_READ, res_readreply, NULL, 0);
739 +
740    /* Better to cast the sizeof instead of rc */
741    if (rc <= (int)(sizeof(HEADER)))
742      return;
# Line 799 | Line 751 | res_readreply(fde_t *fd, void *data)
751    header->arcount = ntohs(header->arcount);
752  
753    /*
754 <   * response for an id which we have already received an answer for
803 <   * just ignore this response.
754 >   * check against possibly fake replies
755     */
756 <  if (0 == (request = find_id(header->id)))
756 >  if (!res_ourserver(&lsin))
757      return;
758  
759    /*
760 <   * check against possibly fake replies
760 >   * response for an id which we have already received an answer for
761 >   * just ignore this response.
762     */
763 <  if (!res_ourserver(&lsin))
763 >  if (!(request = find_id(header->id)))
764      return;
765  
766    if ((header->rcode != NO_ERRORS) || (header->ancount == 0))
767    {
768 <    if ((SERVFAIL == header->rcode) || (NXDOMAIN == header->rcode))
768 >    if (header->rcode == SERVFAIL || header->rcode == NXDOMAIN)
769      {
770        /*
771 <       * If a bad error was returned, we stop here and dont send
771 >       * If a bad error was returned, stop here and don't
772         * send any more (no retries granted).
773         */
774 <      (*request->query->callback)(request->query->ptr, NULL);
774 >      (*request->callback)(request->callback_ctx, NULL, NULL);
775        rem_request(request);
776 <    }
776 >    }
777 > #ifdef IPV6
778      else
779      {
780 <      /*
780 >      /*
781         * If we havent already tried this, and we're looking up AAAA, try A
782         * now
783         */
831
832 #ifdef IPV6
784        if (request->state == REQ_AAAA && request->type == T_AAAA)
785        {
786          request->timeout += 4;
787          resend_query(request);
788        }
838      else if (request->type == T_PTR && request->state != REQ_INT &&
839               request->addr.ss.ss_family == AF_INET6)
840      {
841        request->state = REQ_INT;
842        request->timeout += 4;
843        request->retries--;
844        resend_query(request);
845      }
846 #endif
789      }
790 + #endif
791  
792      return;
793    }
794 +
795    /*
796 <   * If this fails there was an error decoding the received packet,
796 >   * If this fails there was an error decoding the received packet,
797     * try it again and hope it works the next time.
798     */
799 <  answer_count = proc_answer(request, header, buf, buf + rc);
856 <
857 <  if (answer_count)
799 >  if (proc_answer(request, header, buf, buf + rc))
800    {
801      if (request->type == T_PTR)
802      {
# Line 864 | Line 806 | res_readreply(fde_t *fd, void *data)
806           * got a PTR response with no name, something bogus is happening
807           * don't bother trying again, the client address doesn't resolve
808           */
809 <        (*request->query->callback)(request->query->ptr, reply);
809 >        (*request->callback)(request->callback_ctx, NULL, NULL);
810          rem_request(request);
811          return;
812        }
813  
814        /*
815         * Lookup the 'authoritative' name that we were given for the
816 <       * ip#.
816 >       * ip#.
817         *
818         */
819   #ifdef IPV6
820        if (request->addr.ss.ss_family == AF_INET6)
821 <        gethost_byname_type(request->name, request->query, T_AAAA);
821 >        gethost_byname_type(request->callback, request->callback_ctx, request->name, T_AAAA);
822        else
823   #endif
824 <      gethost_byname_type(request->name, request->query, T_A);
824 >      gethost_byname_type(request->callback, request->callback_ctx, request->name, T_A);
825        rem_request(request);
826      }
827      else
# Line 887 | Line 829 | res_readreply(fde_t *fd, void *data)
829        /*
830         * got a name and address response, client resolved
831         */
832 <      reply = make_dnsreply(request);
891 <      (*request->query->callback)(request->query->ptr, reply);
892 <      MyFree(reply);
832 >      (*request->callback)(request->callback_ctx, &request->addr, request->name);
833        rem_request(request);
834      }
835    }
# Line 905 | Line 845 | res_readreply(fde_t *fd, void *data)
845    }
846   }
847  
908 static struct DNSReply *
909 make_dnsreply(struct reslist *request)
910 {
911  struct DNSReply *cp;
912  assert(request != 0);
913
914  cp = (struct DNSReply *)MyMalloc(sizeof(struct DNSReply));
915
916  cp->h_name = request->name;
917  memcpy(&cp->addr, &request->addr, sizeof(cp->addr));
918  return(cp);
919 }
920
848   void
849   report_dns_servers(struct Client *source_p)
850   {
851    int i;
852 <  char ipaddr[HOSTIPLEN];
852 >  char ipaddr[HOSTIPLEN + 1];
853  
854    for (i = 0; i < irc_nscount; i++)
855    {
856 <    irc_getnameinfo((struct sockaddr *)&(irc_nsaddr_list[i]),
857 <                    irc_nsaddr_list[i].ss_len, ipaddr, HOSTIPLEN, NULL, 0,
858 <                    NI_NUMERICHOST);
856 >    getnameinfo((struct sockaddr *)&(irc_nsaddr_list[i]),
857 >                irc_nsaddr_list[i].ss_len, ipaddr,
858 >                sizeof(ipaddr), NULL, 0, NI_NUMERICHOST);
859      sendto_one(source_p, form_str(RPL_STATSALINE),
860 <               me.name, source_p->name, ipaddr);
860 >               me.name, source_p->name, ipaddr);
861    }
862   }

Diff Legend

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