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

Comparing:
ircd-hybrid-7.2/src/irc_res.c (file contents), Revision 982 by michael, Thu Aug 13 22:15:08 2009 UTC vs.
ircd-hybrid/trunk/src/res.c (file contents), Revision 3652 by michael, Mon May 26 15:41:42 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 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"
32 #include "restart.h"
55   #include "rng_mt.h"
56   #include "fdlist.h"
35 #include "fileio.h" /* for fbopen / fbclose / fbputs */
57   #include "s_bsd.h"
58 < #include "s_log.h"
58 > #include "log.h"
59 > #include "misc.h"
60   #include "send.h"
61   #include "memory.h"
62 < #include "irc_res.h"
63 < #include "irc_reslib.h"
64 < #include "irc_getnameinfo.h"
62 > #include "mempool.h"
63 > #include "res.h"
64 > #include "reslib.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;
71  
72   #define MAXPACKET      1024  /* rfc sez 512 but we expand names so ... */
51 #define RES_MAXALIASES 35    /* maximum aliases allowed */
52 #define RES_MAXADDRS   35    /* maximum addresses allowed */
73   #define AR_TTL         600   /* TTL in seconds for dns cache entries */
74  
75   /* RFC 1104/1105 wasn't very helpful about what these fields
# Line 62 | Line 82 | static PF res_readreply;
82   #define RDLENGTH_SIZE     (size_t)2
83   #define ANSWER_FIXED_SIZE (TYPE_SIZE + CLASS_SIZE + TTL_SIZE + RDLENGTH_SIZE)
84  
85 < typedef enum
85 > typedef enum
86   {
87    REQ_IDLE,  /* We're doing not much at all */
88    REQ_PTR,   /* Looking up a PTR */
# Line 70 | Line 90 | typedef enum
90   #ifdef IPV6
91    REQ_AAAA,  /* Looking up an AAAA */
92   #endif
93 <  REQ_CNAME, /* We got a CNAME in response, we better get a real answer next */
74 <  REQ_INT    /* ip6.arpa failed, falling back to ip6.int */
93 >  REQ_CNAME  /* We got a CNAME in response, we better get a real answer next */
94   } request_state;
95  
96 < struct reslist
96 > struct reslist
97   {
98    dlink_node node;
99    int id;
# Line 83 | Line 102 | struct reslist
102    time_t ttl;
103    char type;
104    char retries;            /* retry counter */
105 <  char sends;              /* number of sends (>1 means resent) */
105 >  unsigned int sends;      /* number of sends (>1 means resent) */
106    char resend;             /* send flag. 0 == dont resend */
107    time_t sentat;
108    time_t timeout;
109    struct irc_ssaddr addr;
110    char *name;
111 <  struct DNSQuery *query;  /* query callback for this request */
111 >  dns_callback_fnc callback;
112 >  void *callback_ctx;
113   };
114  
115   static fde_t ResolverFileDescriptor;
116 < static dlink_list request_list    = { NULL, NULL, 0 };
116 > static dlink_list request_list;
117 > static mp_pool_t *dns_pool;
118  
119 < static void rem_request(struct reslist *request);
120 < static struct reslist *make_request(struct DNSQuery *query);
121 < static void do_query_name(struct DNSQuery *query,
122 <                          const char* name, struct reslist *request, int);
123 < static void do_query_number(struct DNSQuery *query,
119 > static void rem_request(struct reslist *);
120 > static struct reslist *make_request(dns_callback_fnc, void *);
121 > static void do_query_name(dns_callback_fnc, void *,
122 >                          const char *, struct reslist *, int);
123 > static void do_query_number(dns_callback_fnc, void *,
124                              const struct irc_ssaddr *,
125 <                            struct reslist *request);
126 < static void query_name(const char *name, int query_class, int query_type,
127 <                       struct reslist *request);
128 < static int send_res_msg(const char *buf, int len, int count);
129 < static void resend_query(struct reslist *request);
130 < static int proc_answer(struct reslist *request, HEADER *header, char *, char *);
110 < static struct reslist *find_id(int id);
111 < static struct DNSReply *make_dnsreply(struct reslist *request);
112 <
113 < extern struct irc_ssaddr irc_nsaddr_list[IRCD_MAXNS];
114 < extern int irc_nscount;
115 < extern char irc_domain[HOSTLEN+1];
125 >                            struct reslist *);
126 > static void query_name(const char *, int, int, struct reslist *);
127 > static int send_res_msg(const char *, int, unsigned int);
128 > static void resend_query(struct reslist *);
129 > static int proc_answer(struct reslist *, HEADER *, char *, char *);
130 > static struct reslist *find_id(int);
131  
132  
133   /*
# Line 127 | Line 142 | extern char irc_domain[HOSTLEN+1];
142   *      revised for ircd, cryogen(stu) may03
143   */
144   static int
145 < res_ourserver(const struct irc_ssaddr *inp)
145 > res_ourserver(const struct irc_ssaddr *inp)
146   {
147   #ifdef IPV6
148 <  struct sockaddr_in6 *v6;
149 <  struct sockaddr_in6 *v6in = (struct sockaddr_in6 *)inp;
148 >  const struct sockaddr_in6 *v6;
149 >  const struct sockaddr_in6 *v6in = (const struct sockaddr_in6 *)inp;
150   #endif
151 <  struct sockaddr_in *v4;
152 <  struct sockaddr_in *v4in = (struct sockaddr_in *)inp;
138 <  int ns;
151 >  const struct sockaddr_in *v4;
152 >  const struct sockaddr_in *v4in = (const struct sockaddr_in *)inp;
153  
154 <  for (ns = 0; ns < irc_nscount; ns++)
154 >  for (unsigned int i = 0; i < irc_nscount; ++i)
155    {
156 <    const struct irc_ssaddr *srv = &irc_nsaddr_list[ns];
156 >    const struct irc_ssaddr *srv = &irc_nsaddr_list[i];
157   #ifdef IPV6
158 <    v6 = (struct sockaddr_in6 *)srv;
158 >    v6 = (const struct sockaddr_in6 *)srv;
159   #endif
160 <    v4 = (struct sockaddr_in *)srv;
160 >    v4 = (const struct sockaddr_in *)srv;
161  
162      /* could probably just memcmp(srv, inp, srv.ss_len) here
163       * but we'll air on the side of caution - stu
# Line 155 | Line 169 | res_ourserver(const struct irc_ssaddr *i
169        case AF_INET6:
170          if (srv->ss.ss_family == inp->ss.ss_family)
171            if (v6->sin6_port == v6in->sin6_port)
172 <            if ((memcmp(&v6->sin6_addr.s6_addr, &v6in->sin6_addr.s6_addr,
173 <                    sizeof(struct in6_addr)) == 0) ||
174 <                (memcmp(&v6->sin6_addr.s6_addr, &in6addr_any,
161 <                        sizeof(struct in6_addr)) == 0))
162 <              return(1);
172 >            if (!memcmp(&v6->sin6_addr.s6_addr, &v6in->sin6_addr.s6_addr,
173 >                        sizeof(struct in6_addr)))
174 >              return 1;
175          break;
176   #endif
177        case AF_INET:
178          if (srv->ss.ss_family == inp->ss.ss_family)
179            if (v4->sin_port == v4in->sin_port)
180 <            if ((v4->sin_addr.s_addr == INADDR_ANY) ||
181 <                (v4->sin_addr.s_addr == v4in->sin_addr.s_addr))
170 <              return(1);
180 >            if (v4->sin_addr.s_addr == v4in->sin_addr.s_addr)
181 >              return 1;
182          break;
183        default:
184          break;
185      }
186    }
187  
188 <  return(0);
188 >  return 0;
189   }
190  
191   /*
192 < * timeout_query_list - Remove queries from the list which have been
192 > * timeout_query_list - Remove queries from the list which have been
193   * there too long without being resolved.
194   */
195   static time_t
196   timeout_query_list(time_t now)
197   {
198 <  dlink_node *ptr;
188 <  dlink_node *next_ptr;
198 >  dlink_node *ptr = NULL, *ptr_next = NULL;
199    struct reslist *request;
200    time_t next_time = 0;
201    time_t timeout   = 0;
202  
203 <  DLINK_FOREACH_SAFE(ptr, next_ptr, request_list.head)
203 >  DLINK_FOREACH_SAFE(ptr, ptr_next, request_list.head)
204    {
205      request = ptr->data;
206      timeout = request->sentat + request->timeout;
# Line 199 | Line 209 | timeout_query_list(time_t now)
209      {
210        if (--request->retries <= 0)
211        {
212 <        (*request->query->callback)(request->query->ptr, NULL);
212 >        (*request->callback)(request->callback_ctx, NULL, NULL);
213          rem_request(request);
214          continue;
215        }
# Line 212 | Line 222 | timeout_query_list(time_t now)
222      }
223  
224      if ((next_time == 0) || timeout < next_time)
215    {
225        next_time = timeout;
217    }
226    }
227  
228 <  return((next_time > now) ? next_time : (now + AR_TTL));
228 >  return (next_time > now) ? next_time : (now + AR_TTL);
229   }
230  
231   /*
# Line 245 | Line 253 | start_resolver(void)
253        return;
254  
255      /* At the moment, the resolver FD data is global .. */
256 <    comm_setselect(&ResolverFileDescriptor, COMM_SELECT_READ,
249 <        res_readreply, NULL, 0);
256 >    comm_setselect(&ResolverFileDescriptor, COMM_SELECT_READ, res_readreply, NULL, 0);
257      eventAdd("timeout_resolver", timeout_resolver, NULL, 1);
258    }
259   }
# Line 257 | Line 264 | start_resolver(void)
264   void
265   init_resolver(void)
266   {
267 +  dns_pool = mp_pool_new(sizeof(struct reslist), MP_CHUNK_SIZE_DNS);
268    memset(&ResolverFileDescriptor, 0, sizeof(fde_t));
269    start_resolver();
270   }
# Line 268 | Line 276 | void
276   restart_resolver(void)
277   {
278    fd_close(&ResolverFileDescriptor);
279 <  eventDelete(timeout_resolver, NULL); /* -ddosen */
279 >  eventDelete(timeout_resolver, NULL);
280    start_resolver();
281   }
282  
283   /*
284 < * add_local_domain - Add the domain to hostname, if it is missing
285 < * (as suggested by eps@TOASTER.SFSU.EDU)
278 < */
279 < void
280 < add_local_domain(char *hname, size_t size)
281 < {
282 <  /* try to fix up unqualified names
283 <   */
284 <  if (strchr(hname, '.') == NULL)
285 <  {
286 <    if (irc_domain[0])
287 <    {
288 <      size_t len = strlen(hname);
289 <
290 <      if ((strlen(irc_domain) + len + 2) < size)
291 <      {
292 <        hname[len++] = '.';
293 <        strcpy(hname + len, irc_domain);
294 <      }
295 <    }
296 <  }
297 < }
298 <
299 < /*
300 < * rem_request - remove a request from the list.
301 < * This must also free any memory that has been allocated for
284 > * rem_request - remove a request from the list.
285 > * This must also free any memory that has been allocated for
286   * temporary storage of DNS results.
287   */
288   static void
289   rem_request(struct reslist *request)
290   {
291    dlinkDelete(&request->node, &request_list);
292 +
293    MyFree(request->name);
294 <  MyFree(request);
294 >  mp_pool_release(request);
295   }
296  
297   /*
298   * make_request - Create a DNS request record for the server.
299   */
300   static struct reslist *
301 < make_request(struct DNSQuery* query)
301 > make_request(dns_callback_fnc callback, void *ctx)
302   {
303 <  struct reslist *request;
303 >  struct reslist *request = mp_pool_get(dns_pool);
304  
305 <  request = (struct reslist *)MyMalloc(sizeof(struct reslist));
306 <
307 <  request->sentat  = CurrentTime;
308 <  request->retries = 3;
309 <  request->resend  = 1;
310 <  request->timeout = 4;    /* start at 4 and exponential inc. */
311 <  request->query   = query;
312 <  request->state   = REQ_IDLE;
305 >  memset(request, 0, sizeof(*request));
306 >  request->sentat       = CurrentTime;
307 >  request->retries      = 2;
308 >  request->resend       = 1;
309 >  request->timeout      = 4;  /* Start at 4 and exponential inc. */
310 >  request->state        = REQ_IDLE;
311 >  request->callback     = callback;
312 >  request->callback_ctx = ctx;
313  
314    dlinkAdd(request, &request->node, &request_list);
315 <  return(request);
315 >  return request;
316   }
317  
318   /*
319 < * delete_resolver_queries - cleanup outstanding queries
319 > * delete_resolver_queries - cleanup outstanding queries
320   * for which there no longer exist clients or conf lines.
321   */
322   void
323 < delete_resolver_queries(const struct DNSQuery *query)
323 > delete_resolver_queries(const void *vptr)
324   {
325 <  dlink_node *ptr;
341 <  dlink_node *next_ptr;
342 <  struct reslist *request;
325 >  dlink_node *ptr = NULL, *ptr_next = NULL;
326  
327 <  DLINK_FOREACH_SAFE(ptr, next_ptr, request_list.head)
327 >  DLINK_FOREACH_SAFE(ptr, ptr_next, request_list.head)
328    {
329 <    if ((request = ptr->data) != NULL)
330 <    {
331 <      if (query == request->query)
332 <        rem_request(request);
350 <    }
329 >    struct reslist *request = ptr->data;
330 >
331 >    if (request->callback_ctx == vptr)
332 >      rem_request(request);
333    }
334   }
335  
# Line 355 | Line 337 | delete_resolver_queries(const struct DNS
337   * send_res_msg - sends msg to all nameservers found in the "_res" structure.
338   * This should reflect /etc/resolv.conf. We will get responses
339   * which arent needed but is easier than checking to see if nameserver
340 < * isnt present. Returns number of messages successfully sent to
340 > * isnt present. Returns number of messages successfully sent to
341   * nameservers or -1 if no successful sends.
342   */
343   static int
344 < send_res_msg(const char *msg, int len, int rcount)
344 > send_res_msg(const char *msg, int len, unsigned int rcount)
345   {
364  int i;
346    int sent = 0;
347 <  int max_queries = IRCD_MIN(irc_nscount, rcount);
347 >  unsigned int max_queries = IRCD_MIN(irc_nscount, rcount);
348  
349    /* RES_PRIMARY option is not implemented
350     * if (res.options & RES_PRIMARY || 0 == max_queries)
# Line 371 | Line 352 | send_res_msg(const char *msg, int len, i
352    if (max_queries == 0)
353      max_queries = 1;
354  
355 <  for (i = 0; i < max_queries; i++)
355 >  for (unsigned int i = 0; i < max_queries; ++i)
356    {
357 <    if (sendto(ResolverFileDescriptor.fd, msg, len, 0,
358 <        (struct sockaddr*)&(irc_nsaddr_list[i]),
359 <        irc_nsaddr_list[i].ss_len) == len)
357 >    if (sendto(ResolverFileDescriptor.fd, msg, len, 0,
358 >        (struct sockaddr*)&(irc_nsaddr_list[i]),
359 >        irc_nsaddr_list[i].ss_len) == len)
360        ++sent;
361    }
362  
363 <  return(sent);
363 >  return sent;
364   }
365  
366   /*
# Line 388 | Line 369 | send_res_msg(const char *msg, int len, i
369   static struct reslist *
370   find_id(int id)
371   {
372 <  dlink_node *ptr;
392 <  struct reslist *request;
372 >  dlink_node *ptr = NULL;
373  
374    DLINK_FOREACH(ptr, request_list.head)
375    {
376 <    request = ptr->data;
376 >    struct reslist *request = ptr->data;
377  
378      if (request->id == id)
379 <      return(request);
379 >      return request;
380    }
381  
382 <  return(NULL);
382 >  return NULL;
383   }
384  
385 < /*
385 > /*
386   * gethost_byname_type - get host address from name
387   *
388   */
389   void
390 < gethost_byname_type(const char *name, struct DNSQuery *query, int type)
390 > gethost_byname_type(dns_callback_fnc callback, void *ctx, const char *name, int type)
391   {
392 <  assert(name != 0);
393 <  do_query_name(query, name, NULL, type);
392 >  assert(name);
393 >  do_query_name(callback, ctx, name, NULL, type);
394   }
395  
396   /*
397   * gethost_byname - wrapper for _type - send T_AAAA first if IPV6 supported
398   */
399   void
400 < gethost_byname(const char *name, struct DNSQuery *query)
400 > gethost_byname(dns_callback_fnc callback, void *ctx, const char *name)
401   {
402   #ifdef IPV6
403 <  gethost_byname_type(name, query, T_AAAA);
403 >  gethost_byname_type(callback, ctx, name, T_AAAA);
404   #else
405 <  gethost_byname_type(name, query, T_A);
405 >  gethost_byname_type(callback, ctx, name, T_A);
406   #endif
407   }
408  
# Line 430 | Line 410 | gethost_byname(const char *name, struct
410   * gethost_byaddr - get host name from address
411   */
412   void
413 < gethost_byaddr(const struct irc_ssaddr *addr, struct DNSQuery *query)
413 > gethost_byaddr(dns_callback_fnc callback, void *ctx, const struct irc_ssaddr *addr)
414   {
415 <  do_query_number(query, addr, NULL);
415 >  do_query_number(callback, ctx, addr, NULL);
416   }
417  
418   /*
419   * do_query_name - nameserver lookup name
420   */
421   static void
422 < do_query_name(struct DNSQuery *query, const char *name,
422 > do_query_name(dns_callback_fnc callback, void *ctx, const char *name,
423                struct reslist *request, int type)
424   {
425    char host_name[HOSTLEN + 1];
426  
427 <  strlcpy(host_name, name, HOSTLEN);
448 <  add_local_domain(host_name, HOSTLEN);
427 >  strlcpy(host_name, name, sizeof(host_name));
428  
429    if (request == NULL)
430    {
431 <    request       = make_request(query);
432 <    request->name = (char *)MyMalloc(strlen(host_name) + 1);
431 >    request       = make_request(callback, ctx);
432 >    request->name = MyCalloc(strlen(host_name) + 1);
433      request->type = type;
434      strcpy(request->name, host_name);
435   #ifdef IPV6
436 <    if (type == T_A)
458 <      request->state = REQ_A;
459 <    else
436 >    if (type != T_A)
437        request->state = REQ_AAAA;
438 < #else
462 <    request->state = REQ_A;
438 >    else
439   #endif
440 +    request->state = REQ_A;
441    }
442  
443    request->type = type;
# Line 471 | Line 448 | do_query_name(struct DNSQuery *query, co
448   * do_query_number - Use this to do reverse IP# lookups.
449   */
450   static void
451 < do_query_number(struct DNSQuery *query, const struct irc_ssaddr *addr,
451 > do_query_number(dns_callback_fnc callback, void *ctx,
452 >                const struct irc_ssaddr *addr,
453                  struct reslist *request)
454   {
455 <  char ipbuf[128];
456 <  const unsigned char *cp;
479 < #ifdef IPV6
480 <  const char *intarpa;
481 < #endif
455 >  char ipbuf[128] = "";
456 >
457    if (addr->ss.ss_family == AF_INET)
458    {
459 <    struct sockaddr_in *v4 = (struct sockaddr_in *)addr;
460 <    cp = (const unsigned char*)&v4->sin_addr.s_addr;
459 >    const struct sockaddr_in *v4 = (const struct sockaddr_in *)addr;
460 >    const unsigned char *cp = (const unsigned char *)&v4->sin_addr.s_addr;
461  
462 <    ircsprintf(ipbuf, "%u.%u.%u.%u.in-addr.arpa.",
463 <               (unsigned int)(cp[3]), (unsigned int)(cp[2]),
464 <               (unsigned int)(cp[1]), (unsigned int)(cp[0]));
462 >    snprintf(ipbuf, sizeof(ipbuf), "%u.%u.%u.%u.in-addr.arpa.",
463 >             (unsigned int)(cp[3]), (unsigned int)(cp[2]),
464 >             (unsigned int)(cp[1]), (unsigned int)(cp[0]));
465    }
466   #ifdef IPV6
467    else if (addr->ss.ss_family == AF_INET6)
468    {
469 <    struct sockaddr_in6 *v6 = (struct sockaddr_in6 *)addr;
470 <    cp = (const unsigned char *)&v6->sin6_addr.s6_addr;
469 >    const struct sockaddr_in6 *v6 = (const struct sockaddr_in6 *)addr;
470 >    const unsigned char *cp = (const unsigned char *)&v6->sin6_addr.s6_addr;
471  
472 <    if (request != NULL && request->state == REQ_INT)
473 <      intarpa = "int";
474 <    else
475 <      intarpa = "arpa";
476 <
477 <    (void)sprintf(ipbuf, "%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x."
478 <                  "%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.ip6.%s.",
479 <                  (unsigned int)(cp[15]&0xf), (unsigned int)(cp[15]>>4),
480 <                  (unsigned int)(cp[14]&0xf), (unsigned int)(cp[14]>>4),
481 <                  (unsigned int)(cp[13]&0xf), (unsigned int)(cp[13]>>4),
482 <                  (unsigned int)(cp[12]&0xf), (unsigned int)(cp[12]>>4),
483 <                  (unsigned int)(cp[11]&0xf), (unsigned int)(cp[11]>>4),
484 <                  (unsigned int)(cp[10]&0xf), (unsigned int)(cp[10]>>4),
485 <                  (unsigned int)(cp[9]&0xf), (unsigned int)(cp[9]>>4),
486 <                  (unsigned int)(cp[8]&0xf), (unsigned int)(cp[8]>>4),
487 <                  (unsigned int)(cp[7]&0xf), (unsigned int)(cp[7]>>4),
488 <                  (unsigned int)(cp[6]&0xf), (unsigned int)(cp[6]>>4),
489 <                  (unsigned int)(cp[5]&0xf), (unsigned int)(cp[5]>>4),
490 <                  (unsigned int)(cp[4]&0xf), (unsigned int)(cp[4]>>4),
516 <                  (unsigned int)(cp[3]&0xf), (unsigned int)(cp[3]>>4),
517 <                  (unsigned int)(cp[2]&0xf), (unsigned int)(cp[2]>>4),
518 <                  (unsigned int)(cp[1]&0xf), (unsigned int)(cp[1]>>4),
519 <                  (unsigned int)(cp[0]&0xf), (unsigned int)(cp[0]>>4), intarpa);
472 >    snprintf(ipbuf, sizeof(ipbuf),
473 >             "%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x."
474 >             "%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.ip6.arpa.",
475 >             (unsigned int)(cp[15] & 0xf), (unsigned int)(cp[15] >> 4),
476 >             (unsigned int)(cp[14] & 0xf), (unsigned int)(cp[14] >> 4),
477 >             (unsigned int)(cp[13] & 0xf), (unsigned int)(cp[13] >> 4),
478 >             (unsigned int)(cp[12] & 0xf), (unsigned int)(cp[12] >> 4),
479 >             (unsigned int)(cp[11] & 0xf), (unsigned int)(cp[11] >> 4),
480 >             (unsigned int)(cp[10] & 0xf), (unsigned int)(cp[10] >> 4),
481 >             (unsigned int)(cp[9] & 0xf), (unsigned int)(cp[9] >> 4),
482 >             (unsigned int)(cp[8] & 0xf), (unsigned int)(cp[8] >> 4),
483 >             (unsigned int)(cp[7] & 0xf), (unsigned int)(cp[7] >> 4),
484 >             (unsigned int)(cp[6] & 0xf), (unsigned int)(cp[6] >> 4),
485 >             (unsigned int)(cp[5] & 0xf), (unsigned int)(cp[5] >> 4),
486 >             (unsigned int)(cp[4] & 0xf), (unsigned int)(cp[4] >> 4),
487 >             (unsigned int)(cp[3] & 0xf), (unsigned int)(cp[3] >> 4),
488 >             (unsigned int)(cp[2] & 0xf), (unsigned int)(cp[2] >> 4),
489 >             (unsigned int)(cp[1] & 0xf), (unsigned int)(cp[1] >> 4),
490 >             (unsigned int)(cp[0] & 0xf), (unsigned int)(cp[0] >> 4));
491    }
492   #endif
493    if (request == NULL)
494    {
495 <    request       = make_request(query);
495 >    request       = make_request(callback, ctx);
496      request->type = T_PTR;
497      memcpy(&request->addr, addr, sizeof(struct irc_ssaddr));
498 <    request->name = (char *)MyMalloc(HOSTLEN + 1);
498 >    request->name = MyCalloc(HOSTLEN + 1);
499    }
500  
501    query_name(ipbuf, C_IN, T_PTR, request);
# Line 542 | Line 513 | query_name(const char *name, int query_c
513  
514    memset(buf, 0, sizeof(buf));
515  
516 <  if ((request_len = irc_res_mkquery(name, query_class, type,
516 >  if ((request_len = irc_res_mkquery(name, query_class, type,
517        (unsigned char *)buf, sizeof(buf))) > 0)
518    {
519      HEADER *header = (HEADER *)buf;
# Line 573 | Line 544 | resend_query(struct reslist *request)
544    switch (request->type)
545    {
546      case T_PTR:
547 <      do_query_number(NULL, &request->addr, request);
547 >      do_query_number(NULL, NULL, &request->addr, request);
548        break;
549      case T_A:
550 <      do_query_name(NULL, request->name, request, request->type);
550 >      do_query_name(NULL, NULL, request->name, request, request->type);
551        break;
552   #ifdef IPV6
553      case T_AAAA:
554        /* didnt work, try A */
555        if (request->state == REQ_AAAA)
556 <        do_query_name(NULL, request->name, request, T_A);
556 >        do_query_name(NULL, NULL, request->name, request, T_A);
557   #endif
558      default:
559        break;
# Line 593 | Line 564 | resend_query(struct reslist *request)
564   * proc_answer - process name server reply
565   */
566   static int
567 < proc_answer(struct reslist *request, HEADER* header, char* buf, char* eob)
567 > proc_answer(struct reslist *request, HEADER *header, char *buf, char *eob)
568   {
569    char hostbuf[HOSTLEN + 100]; /* working buffer */
570    unsigned char *current;      /* current position in buf */
# Line 612 | Line 583 | proc_answer(struct reslist *request, HEA
583      if ((n = irc_dn_skipname(current, (unsigned char *)eob)) < 0)
584        break;
585  
586 <    current += (size_t) n + QFIXEDSZ;
586 >    current += (size_t)n + QFIXEDSZ;
587    }
588  
589    /*
# Line 625 | Line 596 | proc_answer(struct reslist *request, HEA
596      n = irc_dn_expand((unsigned char *)buf, (unsigned char *)eob, current,
597          hostbuf, sizeof(hostbuf));
598  
599 <    if (n < 0)
600 <    {
630 <      /*
631 <       * broken message
632 <       */
633 <      return(0);
634 <    }
635 <    else if (n == 0)
636 <    {
637 <      /*
638 <       * no more answers left
639 <       */
640 <      return(0);
641 <    }
599 >    if (n < 0 /* Broken message */ || n == 0 /* No more answers left */)
600 >      return 0;
601  
602      hostbuf[HOSTLEN] = '\0';
603  
# Line 646 | Line 605 | proc_answer(struct reslist *request, HEA
605       * this code was not working on alpha due to that
606       * (spotted by rodder/jailbird/dianora)
607       */
608 <    current += (size_t) n;
608 >    current += (size_t)n;
609  
610      if (!(((char *)current + ANSWER_FIXED_SIZE) < eob))
611        break;
# Line 663 | Line 622 | proc_answer(struct reslist *request, HEA
622      rd_length = irc_ns_get16(current);
623      current += RDLENGTH_SIZE;
624  
625 <    /*
626 <     * Wait to set request->type until we verify this structure
625 >    /*
626 >     * Wait to set request->type until we verify this structure
627       */
628      switch (type)
629      {
630        case T_A:
631          if (request->type != T_A)
632 <          return(0);
632 >          return 0;
633  
634          /*
635 <         * check for invalid rd_length or too many addresses
635 >         * Check for invalid rd_length or too many addresses
636           */
637          if (rd_length != sizeof(struct in_addr))
638 <          return(0);
638 >          return 0;
639 >
640          v4 = (struct sockaddr_in *)&request->addr;
641          request->addr.ss_len = sizeof(struct sockaddr_in);
642          v4->sin_family = AF_INET;
643          memcpy(&v4->sin_addr, current, sizeof(struct in_addr));
644 <        return(1);
644 >        return 1;
645          break;
646   #ifdef IPV6
647        case T_AAAA:
648          if (request->type != T_AAAA)
649 <          return(0);
649 >          return 0;
650 >
651          if (rd_length != sizeof(struct in6_addr))
652 <          return(0);
652 >          return 0;
653 >
654          request->addr.ss_len = sizeof(struct sockaddr_in6);
655          v6 = (struct sockaddr_in6 *)&request->addr;
656          v6->sin6_family = AF_INET6;
657          memcpy(&v6->sin6_addr, current, sizeof(struct in6_addr));
658 <        return(1);
658 >        return 1;
659          break;
660   #endif
661        case T_PTR:
662          if (request->type != T_PTR)
663 <          return(0);
702 <        n = irc_dn_expand((unsigned char *)buf, (unsigned char *)eob,
703 <            current, hostbuf, sizeof(hostbuf));
704 <        if (n < 0)
705 <          return(0); /* broken message */
706 <        else if (n == 0)
707 <          return(0); /* no more answers left */
663 >          return 0;
664  
665 <        strlcpy(request->name, hostbuf, HOSTLEN);
665 >        n = irc_dn_expand((unsigned char *)buf, (unsigned char *)eob,
666 >                          current, hostbuf, sizeof(hostbuf));
667 >        if (n < 0 /* Broken message */ || n == 0 /* No more answers left */)
668 >          return 0;
669  
670 <        return(1);
670 >        strlcpy(request->name, hostbuf, HOSTLEN + 1);
671 >        return 1;
672          break;
673 <      case T_CNAME: /* first check we already havent started looking
673 >      case T_CNAME: /* first check we already havent started looking
674                         into a cname */
675 <        if (request->type != T_PTR)
676 <          return(0);
675 >        if (request->type != T_PTR)
676 >          return 0;
677  
678          if (request->state == REQ_CNAME)
679          {
# Line 721 | Line 681 | proc_answer(struct reslist *request, HEA
681                              current, hostbuf, sizeof(hostbuf));
682  
683            if (n < 0)
684 <            return(0);
685 <          return(1);
684 >            return 0;
685 >          return 1;
686          }
687  
688          request->state = REQ_CNAME;
689          current += rd_length;
690          break;
691 <        
691 >
692        default:
693          /* XXX I'd rather just throw away the entire bogus thing
694           * but its possible its just a broken nameserver with still
695           * valid answers. But lets do some rudimentary logging for now...
696           */
697 <        ilog(L_ERROR, "irc_res.c bogus type %d", type);
697 >        ilog(LOG_TYPE_IRCD, "irc_res.c bogus type %d", type);
698          break;
699      }
700    }
701  
702 <  return(1);
702 >  return 1;
703   }
704  
705   /*
# Line 749 | Line 709 | static void
709   res_readreply(fde_t *fd, void *data)
710   {
711    char buf[sizeof(HEADER) + MAXPACKET]
712 <        /* Sparc and alpha need 16bit-alignment for accessing header->id
713 <         * (which is uint16_t). Because of the header = (HEADER*) buf;
712 >        /* Sparc and alpha need 16bit-alignment for accessing header->id
713 >         * (which is uint16_t). Because of the header = (HEADER*) buf;
714           * lateron, this is neeeded. --FaUl
715           */
716 < #if defined(__sparc__) || defined(__alpha__)  
716 > #if defined(__sparc__) || defined(__alpha__)
717            __attribute__((aligned (16)))
718 < #endif
718 > #endif
719            ;
720    HEADER *header;
721    struct reslist *request = NULL;
762  struct DNSReply *reply  = NULL;
722    int rc;
764  int answer_count;
723    socklen_t len = sizeof(struct irc_ssaddr);
724    struct irc_ssaddr lsin;
725  
# Line 771 | Line 729 | res_readreply(fde_t *fd, void *data)
729     * interest where it'll instantly be ready for read :-) -- adrian
730     */
731    comm_setselect(fd, COMM_SELECT_READ, res_readreply, NULL, 0);
732 +
733    /* Better to cast the sizeof instead of rc */
734    if (rc <= (int)(sizeof(HEADER)))
735      return;
736  
737    /*
738 <   * convert DNS reply reader from Network byte order to CPU byte order.
738 >   * Convert DNS reply reader from Network byte order to CPU byte order.
739     */
740    header = (HEADER *)buf;
741    header->ancount = ntohs(header->ancount);
# Line 785 | Line 744 | res_readreply(fde_t *fd, void *data)
744    header->arcount = ntohs(header->arcount);
745  
746    /*
747 <   * response for an id which we have already received an answer for
789 <   * just ignore this response.
747 >   * Check against possibly fake replies
748     */
749 <  if (0 == (request = find_id(header->id)))
749 >  if (!res_ourserver(&lsin))
750      return;
751  
752    /*
753 <   * check against possibly fake replies
753 >   * Response for an id which we have already received an answer for
754 >   * just ignore this response.
755     */
756 <  if (!res_ourserver(&lsin))
756 >  if (!(request = find_id(header->id)))
757      return;
758  
759    if ((header->rcode != NO_ERRORS) || (header->ancount == 0))
760    {
761 <    if (NXDOMAIN == header->rcode)
761 >    if (header->rcode == SERVFAIL || header->rcode == NXDOMAIN)
762      {
763 <      /*
763 >      /*
764 >       * If a bad error was returned, stop here and don't
765 >       * send any more (no retries granted).
766 >       */
767 >      (*request->callback)(request->callback_ctx, NULL, NULL);
768 >      rem_request(request);
769 >    }
770 > #ifdef IPV6
771 >    else
772 >    {
773 >      /*
774         * If we havent already tried this, and we're looking up AAAA, try A
775         * now
776         */
808 #ifdef IPV6
777        if (request->state == REQ_AAAA && request->type == T_AAAA)
778        {
779          request->timeout += 4;
780          resend_query(request);
781        }
814      else if (request->type == T_PTR && request->state != REQ_INT &&
815               request->addr.ss.ss_family == AF_INET6)
816      {
817        request->state = REQ_INT;
818        request->timeout += 4;
819        request->retries--;
820        resend_query(request);
821      }
822      else      /* It's NXDOMAIN but not IPV6 */
823 #endif
824      {
825        /*
826         * If a bad error was returned, stop here and don't
827         * send any more (no retries granted).
828         */
829        (*request->query->callback)(request->query->ptr, NULL);
830        rem_request(request);
831      }
832    }
833    else        /* Some other error other than NXDOMAIN */
834    {
835      /*
836       * If a bad error was returned, stop here and don't
837       * send any more (no retries granted).
838       */
839      (*request->query->callback)(request->query->ptr, NULL);
840      rem_request(request);
782      }
783 + #endif
784 +
785      return;
786    }
787 +
788    /*
789 <   * If this fails there was an error decoding the received packet,
789 >   * If this fails there was an error decoding the received packet,
790     * try it again and hope it works the next time.
791     */
792 <  answer_count = proc_answer(request, header, buf, buf + rc);
849 <
850 <  if (answer_count)
792 >  if (proc_answer(request, header, buf, buf + rc))
793    {
794      if (request->type == T_PTR)
795      {
796        if (request->name == NULL)
797        {
798          /*
799 <         * got a PTR response with no name, something bogus is happening
799 >         * Got a PTR response with no name, something bogus is happening
800           * don't bother trying again, the client address doesn't resolve
801           */
802 <        (*request->query->callback)(request->query->ptr, reply);
802 >        (*request->callback)(request->callback_ctx, NULL, NULL);
803          rem_request(request);
804          return;
805        }
806  
807        /*
808 <       * Lookup the 'authoritative' name that we were given for the
867 <       * ip#.
868 <       *
808 >       * Lookup the 'authoritative' name that we were given for the ip#.
809         */
810   #ifdef IPV6
811        if (request->addr.ss.ss_family == AF_INET6)
812 <        gethost_byname_type(request->name, request->query, T_AAAA);
812 >        gethost_byname_type(request->callback, request->callback_ctx, request->name, T_AAAA);
813        else
814   #endif
815 <      gethost_byname_type(request->name, request->query, T_A);
815 >      gethost_byname_type(request->callback, request->callback_ctx, request->name, T_A);
816        rem_request(request);
817      }
818      else
819      {
820        /*
821 <       * got a name and address response, client resolved
821 >       * Got a name and address response, client resolved
822         */
823 <      reply = make_dnsreply(request);
884 <      (*request->query->callback)(request->query->ptr, reply);
885 <      MyFree(reply);
823 >      (*request->callback)(request->callback_ctx, &request->addr, request->name);
824        rem_request(request);
825      }
826    }
# Line 898 | Line 836 | res_readreply(fde_t *fd, void *data)
836    }
837   }
838  
901 static struct DNSReply *
902 make_dnsreply(struct reslist *request)
903 {
904  struct DNSReply *cp;
905  assert(request != 0);
906
907  cp = (struct DNSReply *)MyMalloc(sizeof(struct DNSReply));
908
909  cp->h_name = request->name;
910  memcpy(&cp->addr, &request->addr, sizeof(cp->addr));
911  return(cp);
912 }
913
839   void
840   report_dns_servers(struct Client *source_p)
841   {
842 <  int i;
918 <  char ipaddr[HOSTIPLEN];
842 >  char ipaddr[HOSTIPLEN + 1] = "";
843  
844 <  for (i = 0; i < irc_nscount; i++)
844 >  for (unsigned int i = 0; i < irc_nscount; ++i)
845    {
846 <    irc_getnameinfo((struct sockaddr *)&(irc_nsaddr_list[i]),
847 <                    irc_nsaddr_list[i].ss_len, ipaddr, HOSTIPLEN, NULL, 0,
848 <                    NI_NUMERICHOST);
849 <    sendto_one(source_p, form_str(RPL_STATSALINE),
926 <               me.name, source_p->name, ipaddr);
846 >    getnameinfo((struct sockaddr *)&(irc_nsaddr_list[i]),
847 >                irc_nsaddr_list[i].ss_len, ipaddr,
848 >                sizeof(ipaddr), NULL, 0, NI_NUMERICHOST);
849 >    sendto_one_numeric(source_p, &me, RPL_STATSALINE, ipaddr);
850    }
851   }

Diff Legend

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