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

Comparing:
ircd-hybrid-7.2/src/irc_res.c (file contents), Revision 468 by db, Fri Feb 17 02:42:23 2006 UTC vs.
ircd-hybrid/branches/8.2.x/src/res.c (file contents), Revision 4235 by michael, Tue Jul 15 17:32:14 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"
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 "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 ... */
50 #define RES_MAXALIASES 35    /* maximum aliases allowed */
51 #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 61 | 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 69 | 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 */
73 <  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 82 | 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 *);
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];
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 126 | 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;
137 <  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 154 | 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,
160 <                        sizeof(struct in6_addr)) == 0))
161 <              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))
169 <              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)
196 > timeout_query_list(void)
197   {
198 <  dlink_node *ptr;
199 <  dlink_node *next_ptr;
188 <  struct reslist *request;
198 >  dlink_node *ptr = NULL, *ptr_next = NULL;
199 >  struct reslist *request = NULL;
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;
207  
208 <    if (now >= timeout)
208 >    if (CurrentTime >= timeout)
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        }
216        else
217        {
218 <        request->sentat = now;
218 >        request->sentat = CurrentTime;
219          request->timeout += request->timeout;
220          resend_query(request);
221        }
222      }
223  
224      if ((next_time == 0) || timeout < next_time)
214    {
225        next_time = timeout;
216    }
226    }
227  
228 <  return((next_time > now) ? next_time : (now + AR_TTL));
228 >  return (next_time > CurrentTime) ? next_time : (CurrentTime + AR_TTL);
229   }
230  
231   /*
# Line 225 | Line 234 | timeout_query_list(time_t now)
234   static void
235   timeout_resolver(void *notused)
236   {
237 <  timeout_query_list(CurrentTime);
237 >  timeout_query_list();
238   }
239  
240   /*
# Line 244 | 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,
248 <        res_readreply, NULL, 0);
249 <    eventAdd("timeout_resolver", timeout_resolver, NULL, 1);
256 >    comm_setselect(&ResolverFileDescriptor, COMM_SELECT_READ, res_readreply, NULL, 0);
257    }
258   }
259  
# Line 256 | Line 263 | start_resolver(void)
263   void
264   init_resolver(void)
265   {
266 < #ifdef HAVE_SRAND48
267 <  srand48(CurrentTime);
268 < #endif
266 >  static struct event event_timeout_resolver =
267 >  {
268 >    .name = "timeout_resolver",
269 >    .handler = timeout_resolver,
270 >    .when = 1
271 >  };
272 >
273 >  dns_pool = mp_pool_new(sizeof(struct reslist), MP_CHUNK_SIZE_DNS);
274    memset(&ResolverFileDescriptor, 0, sizeof(fde_t));
275    start_resolver();
276 +
277 +  event_add(&event_timeout_resolver, NULL);
278   }
279  
280   /*
# Line 270 | Line 284 | void
284   restart_resolver(void)
285   {
286    fd_close(&ResolverFileDescriptor);
273  eventDelete(timeout_resolver, NULL); /* -ddosen */
287    start_resolver();
288   }
289  
290   /*
291 < * add_local_domain - Add the domain to hostname, if it is missing
292 < * (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
291 > * rem_request - remove a request from the list.
292 > * This must also free any memory that has been allocated for
293   * temporary storage of DNS results.
294   */
295   static void
296   rem_request(struct reslist *request)
297   {
298    dlinkDelete(&request->node, &request_list);
299 +
300    MyFree(request->name);
301 <  MyFree(request);
301 >  mp_pool_release(request);
302   }
303  
304   /*
305   * make_request - Create a DNS request record for the server.
306   */
307   static struct reslist *
308 < make_request(struct DNSQuery* query)
308 > make_request(dns_callback_fnc callback, void *ctx)
309   {
310 <  struct reslist *request;
310 >  struct reslist *request = mp_pool_get(dns_pool);
311  
312 <  request = (struct reslist *)MyMalloc(sizeof(struct reslist));
313 <
314 <  request->sentat  = CurrentTime;
315 <  request->retries = 3;
316 <  request->resend  = 1;
317 <  request->timeout = 4;    /* start at 4 and exponential inc. */
318 <  request->query   = query;
329 <  request->state   = REQ_IDLE;
312 >  request->sentat       = CurrentTime;
313 >  request->retries      = 2;
314 >  request->resend       = 1;
315 >  request->timeout      = 4;  /* Start at 4 and exponential inc. */
316 >  request->state        = REQ_IDLE;
317 >  request->callback     = callback;
318 >  request->callback_ctx = ctx;
319  
320    dlinkAdd(request, &request->node, &request_list);
321 <  return(request);
321 >  return request;
322   }
323  
324   /*
325 < * delete_resolver_queries - cleanup outstanding queries
325 > * delete_resolver_queries - cleanup outstanding queries
326   * for which there no longer exist clients or conf lines.
327   */
328   void
329 < delete_resolver_queries(const struct DNSQuery *query)
329 > delete_resolver_queries(const void *vptr)
330   {
331 <  dlink_node *ptr;
343 <  dlink_node *next_ptr;
344 <  struct reslist *request;
331 >  dlink_node *ptr = NULL, *ptr_next = NULL;
332  
333 <  DLINK_FOREACH_SAFE(ptr, next_ptr, request_list.head)
333 >  DLINK_FOREACH_SAFE(ptr, ptr_next, request_list.head)
334    {
335 <    if ((request = ptr->data) != NULL)
336 <    {
337 <      if (query == request->query)
338 <        rem_request(request);
352 <    }
335 >    struct reslist *request = ptr->data;
336 >
337 >    if (request->callback_ctx == vptr)
338 >      rem_request(request);
339    }
340   }
341  
# Line 357 | Line 343 | delete_resolver_queries(const struct DNS
343   * send_res_msg - sends msg to all nameservers found in the "_res" structure.
344   * This should reflect /etc/resolv.conf. We will get responses
345   * which arent needed but is easier than checking to see if nameserver
346 < * isnt present. Returns number of messages successfully sent to
346 > * isnt present. Returns number of messages successfully sent to
347   * nameservers or -1 if no successful sends.
348   */
349   static int
350 < send_res_msg(const char *msg, int len, int rcount)
350 > send_res_msg(const char *msg, int len, unsigned int rcount)
351   {
366  int i;
352    int sent = 0;
353 <  int max_queries = IRCD_MIN(irc_nscount, rcount);
353 >  unsigned int max_queries = IRCD_MIN(irc_nscount, rcount);
354  
355    /* RES_PRIMARY option is not implemented
356     * if (res.options & RES_PRIMARY || 0 == max_queries)
# Line 373 | Line 358 | send_res_msg(const char *msg, int len, i
358    if (max_queries == 0)
359      max_queries = 1;
360  
361 <  for (i = 0; i < max_queries; i++)
361 >  for (unsigned int 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);
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 = MyCalloc(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;
481 < #ifdef IPV6
482 <  const char *intarpa;
483 < #endif
461 >  char ipbuf[128] = "";
462 >
463    if (addr->ss.ss_family == AF_INET)
464    {
465 <    struct sockaddr_in *v4 = (struct sockaddr_in *)addr;
466 <    cp = (const unsigned char*)&v4->sin_addr.s_addr;
465 >    const struct sockaddr_in *v4 = (const struct sockaddr_in *)addr;
466 >    const unsigned char *cp = (const unsigned char *)&v4->sin_addr.s_addr;
467  
468 <    ircsprintf(ipbuf, "%u.%u.%u.%u.in-addr.arpa.",
469 <               (unsigned int)(cp[3]), (unsigned int)(cp[2]),
470 <               (unsigned int)(cp[1]), (unsigned int)(cp[0]));
468 >    snprintf(ipbuf, sizeof(ipbuf), "%u.%u.%u.%u.in-addr.arpa.",
469 >             (unsigned int)(cp[3]), (unsigned int)(cp[2]),
470 >             (unsigned int)(cp[1]), (unsigned int)(cp[0]));
471    }
472   #ifdef IPV6
473    else if (addr->ss.ss_family == AF_INET6)
474    {
475 <    struct sockaddr_in6 *v6 = (struct sockaddr_in6 *)addr;
476 <    cp = (const unsigned char *)&v6->sin6_addr.s6_addr;
498 <
499 <    if (request != NULL && request->state == REQ_INT)
500 <      intarpa = "int";
501 <    else
502 <      intarpa = "arpa";
475 >    const struct sockaddr_in6 *v6 = (const struct sockaddr_in6 *)addr;
476 >    const unsigned char *cp = (const unsigned char *)&v6->sin6_addr.s6_addr;
477  
478 <    (void)sprintf(ipbuf, "%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x."
479 <                  "%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.ip6.%s.",
480 <                  (unsigned int)(cp[15]&0xf), (unsigned int)(cp[15]>>4),
481 <                  (unsigned int)(cp[14]&0xf), (unsigned int)(cp[14]>>4),
482 <                  (unsigned int)(cp[13]&0xf), (unsigned int)(cp[13]>>4),
483 <                  (unsigned int)(cp[12]&0xf), (unsigned int)(cp[12]>>4),
484 <                  (unsigned int)(cp[11]&0xf), (unsigned int)(cp[11]>>4),
485 <                  (unsigned int)(cp[10]&0xf), (unsigned int)(cp[10]>>4),
486 <                  (unsigned int)(cp[9]&0xf), (unsigned int)(cp[9]>>4),
487 <                  (unsigned int)(cp[8]&0xf), (unsigned int)(cp[8]>>4),
488 <                  (unsigned int)(cp[7]&0xf), (unsigned int)(cp[7]>>4),
489 <                  (unsigned int)(cp[6]&0xf), (unsigned int)(cp[6]>>4),
490 <                  (unsigned int)(cp[5]&0xf), (unsigned int)(cp[5]>>4),
491 <                  (unsigned int)(cp[4]&0xf), (unsigned int)(cp[4]>>4),
492 <                  (unsigned int)(cp[3]&0xf), (unsigned int)(cp[3]>>4),
493 <                  (unsigned int)(cp[2]&0xf), (unsigned int)(cp[2]>>4),
494 <                  (unsigned int)(cp[1]&0xf), (unsigned int)(cp[1]>>4),
495 <                  (unsigned int)(cp[0]&0xf), (unsigned int)(cp[0]>>4), intarpa);
478 >    snprintf(ipbuf, sizeof(ipbuf),
479 >             "%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x."
480 >             "%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.ip6.arpa.",
481 >             (unsigned int)(cp[15] & 0xf), (unsigned int)(cp[15] >> 4),
482 >             (unsigned int)(cp[14] & 0xf), (unsigned int)(cp[14] >> 4),
483 >             (unsigned int)(cp[13] & 0xf), (unsigned int)(cp[13] >> 4),
484 >             (unsigned int)(cp[12] & 0xf), (unsigned int)(cp[12] >> 4),
485 >             (unsigned int)(cp[11] & 0xf), (unsigned int)(cp[11] >> 4),
486 >             (unsigned int)(cp[10] & 0xf), (unsigned int)(cp[10] >> 4),
487 >             (unsigned int)(cp[9] & 0xf), (unsigned int)(cp[9] >> 4),
488 >             (unsigned int)(cp[8] & 0xf), (unsigned int)(cp[8] >> 4),
489 >             (unsigned int)(cp[7] & 0xf), (unsigned int)(cp[7] >> 4),
490 >             (unsigned int)(cp[6] & 0xf), (unsigned int)(cp[6] >> 4),
491 >             (unsigned int)(cp[5] & 0xf), (unsigned int)(cp[5] >> 4),
492 >             (unsigned int)(cp[4] & 0xf), (unsigned int)(cp[4] >> 4),
493 >             (unsigned int)(cp[3] & 0xf), (unsigned int)(cp[3] >> 4),
494 >             (unsigned int)(cp[2] & 0xf), (unsigned int)(cp[2] >> 4),
495 >             (unsigned int)(cp[1] & 0xf), (unsigned int)(cp[1] >> 4),
496 >             (unsigned int)(cp[0] & 0xf), (unsigned int)(cp[0] >> 4));
497    }
498   #endif
499    if (request == NULL)
500    {
501 <    request       = make_request(query);
501 >    request       = make_request(callback, ctx);
502      request->type = T_PTR;
503      memcpy(&request->addr, addr, sizeof(struct irc_ssaddr));
504 <    request->name = (char *)MyMalloc(HOSTLEN + 1);
504 >    request->name = MyCalloc(HOSTLEN + 1);
505    }
506  
507    query_name(ipbuf, C_IN, T_PTR, request);
# Line 544 | Line 519 | query_name(const char *name, int query_c
519  
520    memset(buf, 0, sizeof(buf));
521  
522 <  if ((request_len = irc_res_mkquery(name, query_class, type,
522 >  if ((request_len = irc_res_mkquery(name, query_class, type,
523        (unsigned char *)buf, sizeof(buf))) > 0)
524    {
525      HEADER *header = (HEADER *)buf;
526 < #ifndef HAVE_LRAND48
552 <    int k = 0;
553 <    struct timeval tv;
554 < #endif
526 >
527      /*
528       * generate an unique id
529       * NOTE: we don't have to worry about converting this to and from
530       * network byte order, the nameserver does not interpret this value
531       * and returns it unchanged
532       */
561 #ifdef HAVE_LRAND48
533      do
534 <    {
535 <      header->id = (header->id + lrand48()) & 0xffff;
536 <    } 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 */
534 >      header->id = (header->id + genrand_int32()) & 0xffff;
535 >    while (find_id(header->id));
536 >
537      request->id = header->id;
538      ++request->sends;
539  
# Line 584 | Line 547 | resend_query(struct reslist *request)
547    if (request->resend == 0)
548      return;
549  
550 <  switch(request->type)
550 >  switch (request->type)
551    {
552      case T_PTR:
553 <      do_query_number(NULL, &request->addr, request);
553 >      do_query_number(NULL, NULL, &request->addr, request);
554        break;
555      case T_A:
556 <      do_query_name(NULL, request->name, request, request->type);
556 >      do_query_name(NULL, NULL, request->name, request, request->type);
557        break;
558   #ifdef IPV6
559      case T_AAAA:
560        /* didnt work, try A */
561        if (request->state == REQ_AAAA)
562 <        do_query_name(NULL, request->name, request, T_A);
562 >        do_query_name(NULL, NULL, request->name, request, T_A);
563   #endif
564      default:
565        break;
# Line 607 | Line 570 | resend_query(struct reslist *request)
570   * proc_answer - process name server reply
571   */
572   static int
573 < proc_answer(struct reslist *request, HEADER* header, char* buf, char* eob)
573 > proc_answer(struct reslist *request, HEADER *header, char *buf, char *eob)
574   {
575    char hostbuf[HOSTLEN + 100]; /* working buffer */
576    unsigned char *current;      /* current position in buf */
# Line 626 | Line 589 | proc_answer(struct reslist *request, HEA
589      if ((n = irc_dn_skipname(current, (unsigned char *)eob)) < 0)
590        break;
591  
592 <    current += (size_t) n + QFIXEDSZ;
592 >    current += (size_t)n + QFIXEDSZ;
593    }
594  
595    /*
# Line 639 | Line 602 | proc_answer(struct reslist *request, HEA
602      n = irc_dn_expand((unsigned char *)buf, (unsigned char *)eob, current,
603          hostbuf, sizeof(hostbuf));
604  
605 <    if (n < 0)
606 <    {
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 <    }
605 >    if (n < 0 /* Broken message */ || n == 0 /* No more answers left */)
606 >      return 0;
607  
608      hostbuf[HOSTLEN] = '\0';
609  
# Line 660 | Line 611 | proc_answer(struct reslist *request, HEA
611       * this code was not working on alpha due to that
612       * (spotted by rodder/jailbird/dianora)
613       */
614 <    current += (size_t) n;
614 >    current += (size_t)n;
615  
616      if (!(((char *)current + ANSWER_FIXED_SIZE) < eob))
617        break;
# Line 677 | Line 628 | proc_answer(struct reslist *request, HEA
628      rd_length = irc_ns_get16(current);
629      current += RDLENGTH_SIZE;
630  
631 <    /*
632 <     * Wait to set request->type until we verify this structure
631 >    /*
632 >     * Wait to set request->type until we verify this structure
633       */
634      switch (type)
635      {
636        case T_A:
637          if (request->type != T_A)
638 <          return(0);
638 >          return 0;
639  
640          /*
641 <         * check for invalid rd_length or too many addresses
641 >         * Check for invalid rd_length or too many addresses
642           */
643          if (rd_length != sizeof(struct in_addr))
644 <          return(0);
644 >          return 0;
645 >
646          v4 = (struct sockaddr_in *)&request->addr;
647          request->addr.ss_len = sizeof(struct sockaddr_in);
648          v4->sin_family = AF_INET;
649          memcpy(&v4->sin_addr, current, sizeof(struct in_addr));
650 <        return(1);
650 >        return 1;
651          break;
652   #ifdef IPV6
653        case T_AAAA:
654          if (request->type != T_AAAA)
655 <          return(0);
655 >          return 0;
656 >
657          if (rd_length != sizeof(struct in6_addr))
658 <          return(0);
658 >          return 0;
659 >
660          request->addr.ss_len = sizeof(struct sockaddr_in6);
661          v6 = (struct sockaddr_in6 *)&request->addr;
662          v6->sin6_family = AF_INET6;
663          memcpy(&v6->sin6_addr, current, sizeof(struct in6_addr));
664 <        return(1);
664 >        return 1;
665          break;
666   #endif
667        case T_PTR:
668          if (request->type != T_PTR)
669 <          return(0);
716 <        n = irc_dn_expand((unsigned char *)buf, (unsigned char *)eob,
717 <            current, hostbuf, sizeof(hostbuf));
718 <        if (n < 0)
719 <          return(0); /* broken message */
720 <        else if (n == 0)
721 <          return(0); /* no more answers left */
669 >          return 0;
670  
671 <        strlcpy(request->name, hostbuf, HOSTLEN);
671 >        n = irc_dn_expand((unsigned char *)buf, (unsigned char *)eob,
672 >                          current, hostbuf, sizeof(hostbuf));
673 >        if (n < 0 /* Broken message */ || n == 0 /* No more answers left */)
674 >          return 0;
675  
676 <        return(1);
676 >        strlcpy(request->name, hostbuf, HOSTLEN + 1);
677 >        return 1;
678          break;
679 <      case T_CNAME: /* first check we already havent started looking
679 >      case T_CNAME: /* first check we already havent started looking
680                         into a cname */
681 <        if (request->type != T_PTR)
682 <          return(0);
681 >        if (request->type != T_PTR)
682 >          return 0;
683  
684          if (request->state == REQ_CNAME)
685          {
# Line 735 | Line 687 | proc_answer(struct reslist *request, HEA
687                              current, hostbuf, sizeof(hostbuf));
688  
689            if (n < 0)
690 <            return(0);
691 <          return(1);
690 >            return 0;
691 >          return 1;
692          }
693  
694          request->state = REQ_CNAME;
695          current += rd_length;
696          break;
697 <        
697 >
698        default:
699          /* XXX I'd rather just throw away the entire bogus thing
700           * but its possible its just a broken nameserver with still
701           * valid answers. But lets do some rudimentary logging for now...
702           */
703 <        ilog(L_ERROR, "irc_res.c bogus type %d", type);
703 >        ilog(LOG_TYPE_IRCD, "irc_res.c bogus type %d", type);
704          break;
705      }
706    }
707  
708 <  return(1);
708 >  return 1;
709   }
710  
711   /*
# Line 763 | Line 715 | static void
715   res_readreply(fde_t *fd, void *data)
716   {
717    char buf[sizeof(HEADER) + MAXPACKET]
718 <        /* Sparc and alpha need 16bit-alignment for accessing header->id
719 <         * (which is uint16_t). Because of the header = (HEADER*) buf;
718 >        /* Sparc and alpha need 16bit-alignment for accessing header->id
719 >         * (which is uint16_t). Because of the header = (HEADER*) buf;
720           * lateron, this is neeeded. --FaUl
721           */
722 < #if defined(__sparc__) || defined(__alpha__)  
722 > #if defined(__sparc__) || defined(__alpha__)
723            __attribute__((aligned (16)))
724 < #endif
724 > #endif
725            ;
726    HEADER *header;
727    struct reslist *request = NULL;
776  struct DNSReply *reply  = NULL;
728    int rc;
778  int answer_count;
729    socklen_t len = sizeof(struct irc_ssaddr);
730    struct irc_ssaddr lsin;
731  
# Line 785 | Line 735 | res_readreply(fde_t *fd, void *data)
735     * interest where it'll instantly be ready for read :-) -- adrian
736     */
737    comm_setselect(fd, COMM_SELECT_READ, res_readreply, NULL, 0);
738 +
739    /* Better to cast the sizeof instead of rc */
740    if (rc <= (int)(sizeof(HEADER)))
741      return;
742  
743    /*
744 <   * convert DNS reply reader from Network byte order to CPU byte order.
744 >   * Convert DNS reply reader from Network byte order to CPU byte order.
745     */
746    header = (HEADER *)buf;
747    header->ancount = ntohs(header->ancount);
# Line 799 | Line 750 | res_readreply(fde_t *fd, void *data)
750    header->arcount = ntohs(header->arcount);
751  
752    /*
753 <   * response for an id which we have already received an answer for
803 <   * just ignore this response.
753 >   * Check against possibly fake replies
754     */
755 <  if (0 == (request = find_id(header->id)))
755 >  if (!res_ourserver(&lsin))
756      return;
757  
758    /*
759 <   * check against possibly fake replies
759 >   * Response for an id which we have already received an answer for
760 >   * just ignore this response.
761     */
762 <  if (!res_ourserver(&lsin))
762 >  if (!(request = find_id(header->id)))
763      return;
764  
765    if ((header->rcode != NO_ERRORS) || (header->ancount == 0))
766    {
767 <    if (NXDOMAIN == header->rcode)
767 >    if (header->rcode == SERVFAIL || header->rcode == NXDOMAIN)
768      {
769 <      /*
769 >      /*
770 >       * If a bad error was returned, stop here and don't
771 >       * send any more (no retries granted).
772 >       */
773 >      (*request->callback)(request->callback_ctx, NULL, NULL);
774 >      rem_request(request);
775 >    }
776 > #ifdef IPV6
777 >    else
778 >    {
779 >      /*
780         * If we havent already tried this, and we're looking up AAAA, try A
781         * now
782         */
822 #ifdef IPV6
783        if (request->state == REQ_AAAA && request->type == T_AAAA)
784        {
785          request->timeout += 4;
786          resend_query(request);
787        }
828      else if (request->type == T_PTR && request->state != REQ_INT &&
829               request->addr.ss.ss_family == AF_INET6)
830      {
831        request->state = REQ_INT;
832        request->timeout += 4;
833        request->retries--;
834        resend_query(request);
835      }
836      else      /* It's NXDOMAIN but not IPV6 */
837 #endif
838      {
839        /*
840         * If a bad error was returned, stop here and don't
841         * send any more (no retries granted).
842         */
843        (*request->query->callback)(request->query->ptr, NULL);
844        rem_request(request);
845      }
846    }
847    else        /* Some other error other than NXDOMAIN */
848    {
849      /*
850       * If a bad error was returned, stop here and don't
851       * send any more (no retries granted).
852       */
853      (*request->query->callback)(request->query->ptr, NULL);
854      rem_request(request);
788      }
789 + #endif
790 +
791      return;
792    }
793 +
794    /*
795 <   * If this fails there was an error decoding the received packet,
795 >   * If this fails there was an error decoding the received packet,
796     * try it again and hope it works the next time.
797     */
798 <  answer_count = proc_answer(request, header, buf, buf + rc);
863 <
864 <  if (answer_count)
798 >  if (proc_answer(request, header, buf, buf + rc))
799    {
800      if (request->type == T_PTR)
801      {
802        if (request->name == NULL)
803        {
804          /*
805 <         * got a PTR response with no name, something bogus is happening
805 >         * Got a PTR response with no name, something bogus is happening
806           * don't bother trying again, the client address doesn't resolve
807           */
808 <        (*request->query->callback)(request->query->ptr, reply);
808 >        (*request->callback)(request->callback_ctx, NULL, NULL);
809          rem_request(request);
810          return;
811        }
812  
813        /*
814 <       * Lookup the 'authoritative' name that we were given for the
881 <       * ip#.
882 <       *
814 >       * Lookup the 'authoritative' name that we were given for the ip#.
815         */
816   #ifdef IPV6
817        if (request->addr.ss.ss_family == AF_INET6)
818 <        gethost_byname_type(request->name, request->query, T_AAAA);
818 >        gethost_byname_type(request->callback, request->callback_ctx, request->name, T_AAAA);
819        else
820   #endif
821 <      gethost_byname_type(request->name, request->query, T_A);
821 >      gethost_byname_type(request->callback, request->callback_ctx, request->name, T_A);
822        rem_request(request);
823      }
824      else
825      {
826        /*
827 <       * got a name and address response, client resolved
827 >       * Got a name and address response, client resolved
828         */
829 <      reply = make_dnsreply(request);
898 <      (*request->query->callback)(request->query->ptr, reply);
899 <      MyFree(reply);
829 >      (*request->callback)(request->callback_ctx, &request->addr, request->name);
830        rem_request(request);
831      }
832    }
# Line 907 | Line 837 | res_readreply(fde_t *fd, void *data)
837       */
838      assert(0);
839  
840 +    (*request->callback)(request->callback_ctx, NULL, NULL);
841      /* XXX don't leak it */
842      rem_request(request);
843    }
844   }
845  
915 static struct DNSReply *
916 make_dnsreply(struct reslist *request)
917 {
918  struct DNSReply *cp;
919  assert(request != 0);
920
921  cp = (struct DNSReply *)MyMalloc(sizeof(struct DNSReply));
922
923  cp->h_name = request->name;
924  memcpy(&cp->addr, &request->addr, sizeof(cp->addr));
925  return(cp);
926 }
927
846   void
847   report_dns_servers(struct Client *source_p)
848   {
849 <  int i;
932 <  char ipaddr[HOSTIPLEN];
849 >  char ipaddr[HOSTIPLEN + 1] = "";
850  
851 <  for (i = 0; i < irc_nscount; i++)
851 >  for (unsigned int i = 0; i < irc_nscount; ++i)
852    {
853 <    irc_getnameinfo((struct sockaddr *)&(irc_nsaddr_list[i]),
854 <                    irc_nsaddr_list[i].ss_len, ipaddr, HOSTIPLEN, NULL, 0,
855 <                    NI_NUMERICHOST);
856 <    sendto_one(source_p, form_str(RPL_STATSALINE),
940 <               me.name, source_p->name, ipaddr);
853 >    getnameinfo((struct sockaddr *)&(irc_nsaddr_list[i]),
854 >                irc_nsaddr_list[i].ss_len, ipaddr,
855 >                sizeof(ipaddr), NULL, 0, NI_NUMERICHOST);
856 >    sendto_one_numeric(source_p, &me, RPL_STATSALINE, ipaddr);
857    }
858   }

Diff Legend

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