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 986 by michael, Sat Aug 15 21:12:34 2009 UTC vs.
ircd-hybrid/trunk/src/res.c (file contents), Revision 4460 by michael, Mon Aug 11 19:45:11 2014 UTC

# Line 1 | Line 1
1   /*
2 < * A rewrite of Darren Reeds original res.c As there is nothing
3 < * left of Darrens original code, this is now licensed by the hybrid group.
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 Reed's original res.c As there is nothing
29 > * left of Darren's 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..)
31   * You can use it where it is useful, free even. Buy us a beer and stuff.
32   *
33   * The authors takes no responsibility for any damage or loss
34   * of property which results from the use of this software.
35   *
10 * $Id$
11 *
36   * July 1999 - Rewrote a bunch of stuff here. Change hostent builder code,
37   *     added callbacks and reference counting of returned hostents.
38   *     --Bleep (Thomas Helvey <tomh@inxpress.net>)
39   *
40   * This was all needlessly complicated for irc. Simplified. No more hostent
41 < * All we really care about is the IP -> hostname mappings. Thats all.
41 > * All we really care about is the IP -> hostname mappings. That's all.
42   *
43   * Apr 28, 2003 --cryogen and Dianora
44   */
45  
46   #include "stdinc.h"
23 #include "tools.h"
24 #include "client.h"
47   #include "list.h"
48 < #include "common.h"
48 > #include "client.h"
49   #include "event.h"
50   #include "irc_string.h"
29 #include "sprintf_irc.h"
51   #include "ircd.h"
52   #include "numeric.h"
32 #include "restart.h"
53   #include "rng_mt.h"
54   #include "fdlist.h"
35 #include "fileio.h" /* for fbopen / fbclose / fbputs */
55   #include "s_bsd.h"
56 < #include "s_log.h"
56 > #include "log.h"
57 > #include "misc.h"
58   #include "send.h"
59   #include "memory.h"
60 < #include "irc_res.h"
61 < #include "irc_reslib.h"
62 < #include "irc_getnameinfo.h"
60 > #include "mempool.h"
61 > #include "res.h"
62 > #include "reslib.h"
63  
64   #if (CHAR_BIT != 8)
65 < #error this code needs to be able to address individual octets
65 > #error this code needs to be able to address individual octets
66   #endif
67  
68   static PF res_readreply;
69  
70 < #define MAXPACKET      1024  /* rfc sez 512 but we expand names so ... */
71 < #define RES_MAXALIASES 35    /* maximum aliases allowed */
52 < #define RES_MAXADDRS   35    /* maximum addresses allowed */
53 < #define AR_TTL         600   /* TTL in seconds for dns cache entries */
70 > #define MAXPACKET      1024  /**< rfc says 512 but we expand names so ... */
71 > #define AR_TTL         600   /**< TTL in seconds for dns cache entries */
72  
73 < /* RFC 1104/1105 wasn't very helpful about what these fields
73 > /*
74 > * RFC 1104/1105 wasn't very helpful about what these fields
75   * should be named, so for now, we'll just name them this way.
76 < * we probably should look at what named calls them or something.
76 > * We probably should look at what named calls them or something.
77   */
78   #define TYPE_SIZE         (size_t)2
79   #define CLASS_SIZE        (size_t)2
# Line 62 | Line 81 | static PF res_readreply;
81   #define RDLENGTH_SIZE     (size_t)2
82   #define ANSWER_FIXED_SIZE (TYPE_SIZE + CLASS_SIZE + TTL_SIZE + RDLENGTH_SIZE)
83  
84 < typedef enum
66 < {
67 <  REQ_IDLE,  /* We're doing not much at all */
68 <  REQ_PTR,   /* Looking up a PTR */
69 <  REQ_A,     /* Looking up an A, possibly because AAAA failed */
70 < #ifdef IPV6
71 <  REQ_AAAA,  /* Looking up an AAAA */
72 < #endif
73 <  REQ_CNAME  /* We got a CNAME in response, we better get a real answer next */
74 < } request_state;
75 <
76 < struct reslist
84 > struct reslist
85   {
86 <  dlink_node node;
87 <  int id;
88 <  int sent;                /* number of requests sent */
89 <  request_state state;     /* State the resolver machine is in */
90 <  time_t ttl;
91 <  char type;
92 <  char retries;            /* retry counter */
93 <  char sends;              /* number of sends (>1 means resent) */
94 <  char resend;             /* send flag. 0 == dont resend */
95 <  time_t sentat;
96 <  time_t timeout;
97 <  struct irc_ssaddr addr;
98 <  char *name;
91 <  struct DNSQuery *query;  /* query callback for this request */
86 >  dlink_node node;                           /**< Doubly linked list node. */
87 >  int id;                                    /**< Request ID (from request header). */
88 >  int sent;                                  /**< Number of requests sent */
89 >  char type;                                 /**< Current request type. */
90 >  char retries;                              /**< Retry counter */
91 >  unsigned int sends;                        /**< Number of sends (>1 means resent). */
92 >  time_t sentat;                             /**< Timestamp we last sent this request. */
93 >  time_t timeout;                            /**< When this request times out. */
94 >  struct irc_ssaddr addr;                    /**< Address for this request. */
95 >  char name[RFC1035_MAX_DOMAIN_LENGTH + 1];  /**< Hostname for this request. */
96 >  size_t namelength;                         /**< Actual hostname length. */
97 >  dns_callback_fnc callback;                 /**< Callback function on completion. */
98 >  void *callback_ctx;                        /**< Context pointer for callback. */
99   };
100  
101   static fde_t ResolverFileDescriptor;
102 < static dlink_list request_list = { NULL, NULL, 0 };
102 > static dlink_list request_list;
103 > static mp_pool_t *dns_pool;
104 >
105 >
106 > /*
107 > * rem_request - remove a request from the list.
108 > * This must also free any memory that has been allocated for
109 > * temporary storage of DNS results.
110 > */
111 > static void
112 > rem_request(struct reslist *request)
113 > {
114 >  dlinkDelete(&request->node, &request_list);
115 >  mp_pool_release(request);
116 > }
117 >
118 > /*
119 > * make_request - Create a DNS request record for the server.
120 > */
121 > static struct reslist *
122 > make_request(dns_callback_fnc callback, void *ctx)
123 > {
124 >  struct reslist *request = mp_pool_get(dns_pool);
125  
126 < static void rem_request(struct reslist *request);
127 < static struct reslist *make_request(struct DNSQuery *query);
128 < static void do_query_name(struct DNSQuery *query,
129 <                          const char* name, struct reslist *request, int);
130 < static void do_query_number(struct DNSQuery *query,
102 <                            const struct irc_ssaddr *,
103 <                            struct reslist *request);
104 < static void query_name(const char *name, int query_class, int query_type,
105 <                       struct reslist *request);
106 < static int send_res_msg(const char *buf, int len, int count);
107 < static void resend_query(struct reslist *request);
108 < static int proc_answer(struct reslist *request, HEADER *header, char *, char *);
109 < static struct reslist *find_id(int id);
110 < static struct DNSReply *make_dnsreply(struct reslist *request);
111 <
112 < extern struct irc_ssaddr irc_nsaddr_list[IRCD_MAXNS];
113 < extern int irc_nscount;
114 < extern char irc_domain[HOSTLEN+1];
126 >  request->sentat       = CurrentTime;
127 >  request->retries      = 2;
128 >  request->timeout      = 4;  /* Start at 4 and exponential inc. */
129 >  request->callback     = callback;
130 >  request->callback_ctx = ctx;
131  
132 +  dlinkAdd(request, &request->node, &request_list);
133 +  return request;
134 + }
135  
136   /*
137   * int
# Line 126 | Line 145 | extern char irc_domain[HOSTLEN+1];
145   *      revised for ircd, cryogen(stu) may03
146   */
147   static int
148 < res_ourserver(const struct irc_ssaddr *inp)
148 > res_ourserver(const struct irc_ssaddr *inp)
149   {
150 < #ifdef IPV6
151 <  struct sockaddr_in6 *v6;
152 <  struct sockaddr_in6 *v6in = (struct sockaddr_in6 *)inp;
153 < #endif
154 <  struct sockaddr_in *v4;
155 <  struct sockaddr_in *v4in = (struct sockaddr_in *)inp;
156 <  int ns;
157 <
158 <  for (ns = 0; ns < irc_nscount; ns++)
159 <  {
141 <    const struct irc_ssaddr *srv = &irc_nsaddr_list[ns];
142 < #ifdef IPV6
143 <    v6 = (struct sockaddr_in6 *)srv;
144 < #endif
145 <    v4 = (struct sockaddr_in *)srv;
150 >  const struct sockaddr_in6 *v6;
151 >  const struct sockaddr_in6 *v6in = (const struct sockaddr_in6 *)inp;
152 >  const struct sockaddr_in *v4;
153 >  const struct sockaddr_in *v4in = (const struct sockaddr_in *)inp;
154 >
155 >  for (unsigned int i = 0; i < irc_nscount; ++i)
156 >  {
157 >    const struct irc_ssaddr *srv = &irc_nsaddr_list[i];
158 >    v6 = (const struct sockaddr_in6 *)srv;
159 >    v4 = (const struct sockaddr_in *)srv;
160  
161 <    /* could probably just memcmp(srv, inp, srv.ss_len) here
161 >    /*
162 >     * Could probably just memcmp(srv, inp, srv.ss_len) here
163       * but we'll air on the side of caution - stu
149     *
164       */
165      switch (srv->ss.ss_family)
166      {
153 #ifdef IPV6
167        case AF_INET6:
168          if (srv->ss.ss_family == inp->ss.ss_family)
169            if (v6->sin6_port == v6in->sin6_port)
170 <            if ((memcmp(&v6->sin6_addr.s6_addr, &v6in->sin6_addr.s6_addr,
171 <                    sizeof(struct in6_addr)) == 0) ||
172 <                (memcmp(&v6->sin6_addr.s6_addr, &in6addr_any,
160 <                        sizeof(struct in6_addr)) == 0))
161 <              return(1);
170 >            if (!memcmp(&v6->sin6_addr.s6_addr, &v6in->sin6_addr.s6_addr,
171 >                        sizeof(struct in6_addr)))
172 >              return 1;
173          break;
163 #endif
174        case AF_INET:
175          if (srv->ss.ss_family == inp->ss.ss_family)
176            if (v4->sin_port == v4in->sin_port)
177 <            if ((v4->sin_addr.s_addr == INADDR_ANY) ||
178 <                (v4->sin_addr.s_addr == v4in->sin_addr.s_addr))
169 <              return(1);
177 >            if (v4->sin_addr.s_addr == v4in->sin_addr.s_addr)
178 >              return 1;
179          break;
180        default:
181          break;
182      }
183    }
184  
185 <  return(0);
177 < }
178 <
179 < /*
180 < * timeout_query_list - Remove queries from the list which have been
181 < * there too long without being resolved.
182 < */
183 < static time_t
184 < timeout_query_list(time_t now)
185 < {
186 <  dlink_node *ptr;
187 <  dlink_node *next_ptr;
188 <  struct reslist *request;
189 <  time_t next_time = 0;
190 <  time_t timeout   = 0;
191 <
192 <  DLINK_FOREACH_SAFE(ptr, next_ptr, request_list.head)
193 <  {
194 <    request = ptr->data;
195 <    timeout = request->sentat + request->timeout;
196 <
197 <    if (now >= timeout)
198 <    {
199 <      if (--request->retries <= 0)
200 <      {
201 <        (*request->query->callback)(request->query->ptr, NULL);
202 <        rem_request(request);
203 <        continue;
204 <      }
205 <      else
206 <      {
207 <        request->sentat = now;
208 <        request->timeout += request->timeout;
209 <        resend_query(request);
210 <      }
211 <    }
212 <
213 <    if ((next_time == 0) || timeout < next_time)
214 <    {
215 <      next_time = timeout;
216 <    }
217 <  }
218 <
219 <  return((next_time > now) ? next_time : (now + AR_TTL));
220 < }
221 <
222 < /*
223 < * timeout_resolver - check request list
224 < */
225 < static void
226 < timeout_resolver(void *notused)
227 < {
228 <  timeout_query_list(CurrentTime);
185 >  return 0;
186   }
187  
188   /*
# Line 240 | Line 197 | start_resolver(void)
197    if (!ResolverFileDescriptor.flags.open)
198    {
199      if (comm_open(&ResolverFileDescriptor, irc_nsaddr_list[0].ss.ss_family,
200 <                  SOCK_DGRAM, 0, "Resolver socket") == -1)
200 >                  SOCK_DGRAM, 0, "UDP resolver socket") == -1)
201        return;
202  
203      /* At the moment, the resolver FD data is global .. */
204 <    comm_setselect(&ResolverFileDescriptor, COMM_SELECT_READ,
248 <        res_readreply, NULL, 0);
249 <    eventAdd("timeout_resolver", timeout_resolver, NULL, 1);
204 >    comm_setselect(&ResolverFileDescriptor, COMM_SELECT_READ, res_readreply, NULL, 0);
205    }
206   }
207  
208   /*
254 * init_resolver - initialize resolver and resolver library
255 */
256 void
257 init_resolver(void)
258 {
259  memset(&ResolverFileDescriptor, 0, sizeof(fde_t));
260  start_resolver();
261 }
262
263 /*
209   * restart_resolver - reread resolv.conf, reopen socket
210   */
211   void
212   restart_resolver(void)
213   {
214    fd_close(&ResolverFileDescriptor);
270  eventDelete(timeout_resolver, NULL); /* -ddosen */
215    start_resolver();
216   }
217  
218   /*
219 < * add_local_domain - Add the domain to hostname, if it is missing
276 < * (as suggested by eps@TOASTER.SFSU.EDU)
277 < */
278 < void
279 < add_local_domain(char *hname, size_t size)
280 < {
281 <  /* try to fix up unqualified names
282 <   */
283 <  if (strchr(hname, '.') == NULL)
284 <  {
285 <    if (irc_domain[0])
286 <    {
287 <      size_t len = strlen(hname);
288 <
289 <      if ((strlen(irc_domain) + len + 2) < size)
290 <      {
291 <        hname[len++] = '.';
292 <        strcpy(hname + len, irc_domain);
293 <      }
294 <    }
295 <  }
296 < }
297 <
298 < /*
299 < * rem_request - remove a request from the list.
300 < * This must also free any memory that has been allocated for
301 < * temporary storage of DNS results.
302 < */
303 < static void
304 < rem_request(struct reslist *request)
305 < {
306 <  dlinkDelete(&request->node, &request_list);
307 <  MyFree(request->name);
308 <  MyFree(request);
309 < }
310 <
311 < /*
312 < * make_request - Create a DNS request record for the server.
313 < */
314 < static struct reslist *
315 < make_request(struct DNSQuery* query)
316 < {
317 <  struct reslist *request;
318 <
319 <  request = (struct reslist *)MyMalloc(sizeof(struct reslist));
320 <
321 <  request->sentat  = CurrentTime;
322 <  request->retries = 3;
323 <  request->resend  = 1;
324 <  request->timeout = 4;    /* start at 4 and exponential inc. */
325 <  request->query   = query;
326 <  request->state   = REQ_IDLE;
327 <
328 <  dlinkAdd(request, &request->node, &request_list);
329 <  return(request);
330 < }
331 <
332 < /*
333 < * delete_resolver_queries - cleanup outstanding queries
219 > * delete_resolver_queries - cleanup outstanding queries
220   * for which there no longer exist clients or conf lines.
221   */
222   void
223 < delete_resolver_queries(const struct DNSQuery *query)
223 > delete_resolver_queries(const void *vptr)
224   {
225 <  dlink_node *ptr;
340 <  dlink_node *next_ptr;
341 <  struct reslist *request;
225 >  dlink_node *ptr = NULL, *ptr_next = NULL;
226  
227 <  DLINK_FOREACH_SAFE(ptr, next_ptr, request_list.head)
227 >  DLINK_FOREACH_SAFE(ptr, ptr_next, request_list.head)
228    {
229 <    if ((request = ptr->data) != NULL)
230 <    {
231 <      if (query == request->query)
232 <        rem_request(request);
349 <    }
229 >    struct reslist *request = ptr->data;
230 >
231 >    if (request->callback_ctx == vptr)
232 >      rem_request(request);
233    }
234   }
235  
# Line 354 | Line 237 | delete_resolver_queries(const struct DNS
237   * send_res_msg - sends msg to all nameservers found in the "_res" structure.
238   * This should reflect /etc/resolv.conf. We will get responses
239   * which arent needed but is easier than checking to see if nameserver
240 < * isnt present. Returns number of messages successfully sent to
240 > * isn't present. Returns number of messages successfully sent to
241   * nameservers or -1 if no successful sends.
242   */
243   static int
244 < send_res_msg(const char *msg, int len, int rcount)
244 > send_res_msg(const char *msg, int len, unsigned int rcount)
245   {
363  int i;
246    int sent = 0;
247 <  int max_queries = IRCD_MIN(irc_nscount, rcount);
247 >  unsigned int max_queries = IRCD_MIN(irc_nscount, rcount);
248  
249    /* RES_PRIMARY option is not implemented
250     * if (res.options & RES_PRIMARY || 0 == max_queries)
# Line 370 | Line 252 | send_res_msg(const char *msg, int len, i
252    if (max_queries == 0)
253      max_queries = 1;
254  
255 <  for (i = 0; i < max_queries; i++)
255 >  for (unsigned int i = 0; i < max_queries; ++i)
256    {
257 <    if (sendto(ResolverFileDescriptor.fd, msg, len, 0,
258 <        (struct sockaddr*)&(irc_nsaddr_list[i]),
259 <        irc_nsaddr_list[i].ss_len) == len)
257 >    if (sendto(ResolverFileDescriptor.fd, msg, len, 0,
258 >        (struct sockaddr*)&(irc_nsaddr_list[i]),
259 >        irc_nsaddr_list[i].ss_len) == len)
260        ++sent;
261    }
262  
263 <  return(sent);
263 >  return sent;
264   }
265  
266   /*
# Line 387 | Line 269 | send_res_msg(const char *msg, int len, i
269   static struct reslist *
270   find_id(int id)
271   {
272 <  dlink_node *ptr;
391 <  struct reslist *request;
272 >  dlink_node *ptr = NULL;
273  
274    DLINK_FOREACH(ptr, request_list.head)
275    {
276 <    request = ptr->data;
276 >    struct reslist *request = ptr->data;
277  
278      if (request->id == id)
279 <      return(request);
279 >      return request;
280    }
281  
282 <  return(NULL);
402 < }
403 <
404 < /*
405 < * gethost_byname_type - get host address from name
406 < *
407 < */
408 < void
409 < gethost_byname_type(const char *name, struct DNSQuery *query, int type)
410 < {
411 <  assert(name != 0);
412 <  do_query_name(query, name, NULL, type);
282 >  return NULL;
283   }
284  
285   /*
286 < * gethost_byname - wrapper for _type - send T_AAAA first if IPV6 supported
286 > * query_name - generate a query based on class, type and name.
287   */
288 < void
289 < gethost_byname(const char *name, struct DNSQuery *query)
288 > static void
289 > query_name(const char *name, int query_class, int type,
290 >           struct reslist *request)
291   {
292 < #ifdef IPV6
293 <  gethost_byname_type(name, query, T_AAAA);
423 < #else
424 <  gethost_byname_type(name, query, T_A);
425 < #endif
426 < }
292 >  char buf[MAXPACKET];
293 >  int request_len = 0;
294  
295 < /*
296 < * gethost_byaddr - get host name from address
297 < */
298 < void
299 < gethost_byaddr(const struct irc_ssaddr *addr, struct DNSQuery *query)
300 < {
301 <  do_query_number(query, addr, NULL);
295 >  memset(buf, 0, sizeof(buf));
296 >
297 >  if ((request_len = irc_res_mkquery(name, query_class, type,
298 >      (unsigned char *)buf, sizeof(buf))) > 0)
299 >  {
300 >    HEADER *header = (HEADER *)buf;
301 >
302 >    /*
303 >     * Generate an unique id.
304 >     * NOTE: we don't have to worry about converting this to and from
305 >     * network byte order, the nameserver does not interpret this value
306 >     * and returns it unchanged.
307 >     */
308 >    do
309 >      header->id = (header->id + genrand_int32()) & 0xFFFF;
310 >    while (find_id(header->id));
311 >
312 >    request->id = header->id;
313 >    ++request->sends;
314 >
315 >    request->sent += send_res_msg(buf, request_len, request->sends);
316 >  }
317   }
318  
319   /*
320   * do_query_name - nameserver lookup name
321   */
322   static void
323 < do_query_name(struct DNSQuery *query, const char *name,
323 > do_query_name(dns_callback_fnc callback, void *ctx, const char *name,
324                struct reslist *request, int type)
325   {
326 <  char host_name[HOSTLEN + 1];
326 >  char host_name[RFC1035_MAX_DOMAIN_LENGTH + 1];
327  
328 <  strlcpy(host_name, name, HOSTLEN + 1);
447 <  add_local_domain(host_name, HOSTLEN + 1);
328 >  strlcpy(host_name, name, sizeof(host_name));
329  
330    if (request == NULL)
331    {
332 <    request       = make_request(query);
333 <    request->name = MyMalloc(strlen(host_name) + 1);
334 <    request->type = type;
454 <    strcpy(request->name, host_name);
455 < #ifdef IPV6
456 <    if (type != T_A)
457 <      request->state = REQ_AAAA;
458 <    else
459 < #endif
460 <    request->state = REQ_A;
332 >    request             = make_request(callback, ctx);
333 >    request->type       = type;
334 >    request->namelength = strlcpy(request->name, host_name, sizeof(request->name));
335    }
336  
337    request->type = type;
# Line 468 | Line 342 | do_query_name(struct DNSQuery *query, co
342   * do_query_number - Use this to do reverse IP# lookups.
343   */
344   static void
345 < do_query_number(struct DNSQuery *query, const struct irc_ssaddr *addr,
345 > do_query_number(dns_callback_fnc callback, void *ctx,
346 >                const struct irc_ssaddr *addr,
347                  struct reslist *request)
348   {
349 <  char ipbuf[128];
475 <  const unsigned char *cp;
349 >  char ipbuf[128] = "";
350  
351    if (addr->ss.ss_family == AF_INET)
352    {
353 <    struct sockaddr_in *v4 = (struct sockaddr_in *)addr;
354 <    cp = (const unsigned char*)&v4->sin_addr.s_addr;
353 >    const struct sockaddr_in *v4 = (const struct sockaddr_in *)addr;
354 >    const unsigned char *cp = (const unsigned char *)&v4->sin_addr.s_addr;
355  
356 <    ircsprintf(ipbuf, "%u.%u.%u.%u.in-addr.arpa.",
357 <               (unsigned int)(cp[3]), (unsigned int)(cp[2]),
358 <               (unsigned int)(cp[1]), (unsigned int)(cp[0]));
356 >    snprintf(ipbuf, sizeof(ipbuf), "%u.%u.%u.%u.in-addr.arpa.",
357 >             (unsigned int)(cp[3]), (unsigned int)(cp[2]),
358 >             (unsigned int)(cp[1]), (unsigned int)(cp[0]));
359    }
486 #ifdef IPV6
360    else if (addr->ss.ss_family == AF_INET6)
361    {
362 <    struct sockaddr_in6 *v6 = (struct sockaddr_in6 *)addr;
363 <    cp = (const unsigned char *)&v6->sin6_addr.s6_addr;
362 >    const struct sockaddr_in6 *v6 = (const struct sockaddr_in6 *)addr;
363 >    const unsigned char *cp = (const unsigned char *)&v6->sin6_addr.s6_addr;
364  
365 <    sprintf(ipbuf, "%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x."
366 <                   "%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.ip6.arpa.",
367 <                  (unsigned int)(cp[15]&0xf), (unsigned int)(cp[15]>>4),
368 <                  (unsigned int)(cp[14]&0xf), (unsigned int)(cp[14]>>4),
369 <                  (unsigned int)(cp[13]&0xf), (unsigned int)(cp[13]>>4),
370 <                  (unsigned int)(cp[12]&0xf), (unsigned int)(cp[12]>>4),
371 <                  (unsigned int)(cp[11]&0xf), (unsigned int)(cp[11]>>4),
372 <                  (unsigned int)(cp[10]&0xf), (unsigned int)(cp[10]>>4),
373 <                  (unsigned int)(cp[9]&0xf), (unsigned int)(cp[9]>>4),
374 <                  (unsigned int)(cp[8]&0xf), (unsigned int)(cp[8]>>4),
375 <                  (unsigned int)(cp[7]&0xf), (unsigned int)(cp[7]>>4),
376 <                  (unsigned int)(cp[6]&0xf), (unsigned int)(cp[6]>>4),
377 <                  (unsigned int)(cp[5]&0xf), (unsigned int)(cp[5]>>4),
378 <                  (unsigned int)(cp[4]&0xf), (unsigned int)(cp[4]>>4),
379 <                  (unsigned int)(cp[3]&0xf), (unsigned int)(cp[3]>>4),
380 <                  (unsigned int)(cp[2]&0xf), (unsigned int)(cp[2]>>4),
381 <                  (unsigned int)(cp[1]&0xf), (unsigned int)(cp[1]>>4),
382 <                  (unsigned int)(cp[0]&0xf), (unsigned int)(cp[0]>>4));
365 >    snprintf(ipbuf, sizeof(ipbuf),
366 >             "%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x."
367 >             "%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.ip6.arpa.",
368 >             (unsigned int)(cp[15] & 0xf), (unsigned int)(cp[15] >> 4),
369 >             (unsigned int)(cp[14] & 0xf), (unsigned int)(cp[14] >> 4),
370 >             (unsigned int)(cp[13] & 0xf), (unsigned int)(cp[13] >> 4),
371 >             (unsigned int)(cp[12] & 0xf), (unsigned int)(cp[12] >> 4),
372 >             (unsigned int)(cp[11] & 0xf), (unsigned int)(cp[11] >> 4),
373 >             (unsigned int)(cp[10] & 0xf), (unsigned int)(cp[10] >> 4),
374 >             (unsigned int)(cp[9] & 0xf), (unsigned int)(cp[9] >> 4),
375 >             (unsigned int)(cp[8] & 0xf), (unsigned int)(cp[8] >> 4),
376 >             (unsigned int)(cp[7] & 0xf), (unsigned int)(cp[7] >> 4),
377 >             (unsigned int)(cp[6] & 0xf), (unsigned int)(cp[6] >> 4),
378 >             (unsigned int)(cp[5] & 0xf), (unsigned int)(cp[5] >> 4),
379 >             (unsigned int)(cp[4] & 0xf), (unsigned int)(cp[4] >> 4),
380 >             (unsigned int)(cp[3] & 0xf), (unsigned int)(cp[3] >> 4),
381 >             (unsigned int)(cp[2] & 0xf), (unsigned int)(cp[2] >> 4),
382 >             (unsigned int)(cp[1] & 0xf), (unsigned int)(cp[1] >> 4),
383 >             (unsigned int)(cp[0] & 0xf), (unsigned int)(cp[0] >> 4));
384    }
385 < #endif
385 >
386    if (request == NULL)
387    {
388 <    request       = make_request(query);
388 >    request       = make_request(callback, ctx);
389      request->type = T_PTR;
390      memcpy(&request->addr, addr, sizeof(struct irc_ssaddr));
517    request->name = MyMalloc(HOSTLEN + 1);
391    }
392  
393    query_name(ipbuf, C_IN, T_PTR, request);
394   }
395  
396   /*
397 < * query_name - generate a query based on class, type and name.
397 > * gethost_byname_type - get host address from name
398 > *
399   */
400 < static void
401 < query_name(const char *name, int query_class, int type,
528 <           struct reslist *request)
400 > void
401 > gethost_byname_type(dns_callback_fnc callback, void *ctx, const char *name, int type)
402   {
403 <  char buf[MAXPACKET];
404 <  int request_len = 0;
405 <
533 <  memset(buf, 0, sizeof(buf));
534 <
535 <  if ((request_len = irc_res_mkquery(name, query_class, type,
536 <      (unsigned char *)buf, sizeof(buf))) > 0)
537 <  {
538 <    HEADER *header = (HEADER *)buf;
539 <
540 <    /*
541 <     * generate an unique id
542 <     * NOTE: we don't have to worry about converting this to and from
543 <     * network byte order, the nameserver does not interpret this value
544 <     * and returns it unchanged
545 <     */
546 <    do
547 <      header->id = (header->id + genrand_int32()) & 0xffff;
548 <    while (find_id(header->id));
549 <
550 <    request->id = header->id;
551 <    ++request->sends;
403 >  assert(name);
404 >  do_query_name(callback, ctx, name, NULL, type);
405 > }
406  
407 <    request->sent += send_res_msg(buf, request_len, request->sends);
408 <  }
407 > /*
408 > * gethost_byaddr - get host name from address
409 > */
410 > void
411 > gethost_byaddr(dns_callback_fnc callback, void *ctx, const struct irc_ssaddr *addr)
412 > {
413 >  do_query_number(callback, ctx, addr, NULL);
414   }
415  
416   static void
417   resend_query(struct reslist *request)
418   {
560  if (request->resend == 0)
561    return;
562
419    switch (request->type)
420    {
421      case T_PTR:
422 <      do_query_number(NULL, &request->addr, request);
422 >      do_query_number(NULL, NULL, &request->addr, request);
423        break;
424      case T_A:
569      do_query_name(NULL, request->name, request, request->type);
570      break;
571 #ifdef IPV6
425      case T_AAAA:
426 <      /* didnt work, try A */
427 <      if (request->state == REQ_AAAA)
575 <        do_query_name(NULL, request->name, request, T_A);
576 < #endif
426 >      do_query_name(NULL, NULL, request->name, request, request->type);
427 >      break;
428      default:
429        break;
430    }
# Line 583 | Line 434 | resend_query(struct reslist *request)
434   * proc_answer - process name server reply
435   */
436   static int
437 < proc_answer(struct reslist *request, HEADER* header, char* buf, char* eob)
437 > proc_answer(struct reslist *request, HEADER *header, unsigned char *buf, unsigned char *eob)
438   {
439 <  char hostbuf[HOSTLEN + 100]; /* working buffer */
440 <  unsigned char *current;      /* current position in buf */
441 <  int query_class;             /* answer class */
442 <  int type;                    /* answer type */
439 >  char hostbuf[RFC1035_MAX_DOMAIN_LENGTH + 100]; /* working buffer */
440 >  unsigned char *current = buf + sizeof(HEADER); /* current position in buf */
441 >  unsigned int type = 0;       /* answer type */
442 >  unsigned int rd_length = 0;
443    int n;                       /* temp count */
593  int rd_length;
444    struct sockaddr_in *v4;      /* conversion */
595 #ifdef IPV6
445    struct sockaddr_in6 *v6;
597 #endif
598  current = (unsigned char *)buf + sizeof(HEADER);
446  
447    for (; header->qdcount > 0; --header->qdcount)
448    {
449 <    if ((n = irc_dn_skipname(current, (unsigned char *)eob)) < 0)
449 >    if ((n = irc_dn_skipname(current, eob)) < 0)
450        break;
451  
452 <    current += (size_t) n + QFIXEDSZ;
452 >    current += (size_t)n + QFIXEDSZ;
453    }
454  
455    /*
456 <   * process each answer sent to us blech.
456 >   * Process each answer sent to us blech.
457     */
458 <  while (header->ancount > 0 && (char *)current < eob)
458 >  while (header->ancount > 0 && current < eob)
459    {
460      header->ancount--;
461  
462 <    n = irc_dn_expand((unsigned char *)buf, (unsigned char *)eob, current,
616 <        hostbuf, sizeof(hostbuf));
462 >    n = irc_dn_expand(buf, eob, current, hostbuf, sizeof(hostbuf));
463  
464 <    if (n < 0)
465 <    {
620 <      /*
621 <       * broken message
622 <       */
623 <      return(0);
624 <    }
625 <    else if (n == 0)
626 <    {
627 <      /*
628 <       * no more answers left
629 <       */
630 <      return(0);
631 <    }
464 >    if (n < 0  /* Broken message */ || n == 0  /* No more answers left */)
465 >      return 0;
466  
467 <    hostbuf[HOSTLEN] = '\0';
467 >    hostbuf[RFC1035_MAX_DOMAIN_LENGTH] = '\0';
468  
469 <    /* With Address arithmetic you have to be very anal
469 >    /*
470 >     * With Address arithmetic you have to be very anal
471       * this code was not working on alpha due to that
472       * (spotted by rodder/jailbird/dianora)
473       */
474 <    current += (size_t) n;
474 >    current += (size_t)n;
475  
476 <    if (!(((char *)current + ANSWER_FIXED_SIZE) < eob))
476 >    if (!((current + ANSWER_FIXED_SIZE) < eob))
477        break;
478  
479      type = irc_ns_get16(current);
480      current += TYPE_SIZE;
646
647    query_class = irc_ns_get16(current);
481      current += CLASS_SIZE;
649
650    request->ttl = irc_ns_get32(current);
482      current += TTL_SIZE;
652
483      rd_length = irc_ns_get16(current);
484      current += RDLENGTH_SIZE;
485  
486 <    /*
487 <     * Wait to set request->type until we verify this structure
486 >    /*
487 >     * Wait to set request->type until we verify this structure
488       */
489      switch (type)
490      {
491        case T_A:
492          if (request->type != T_A)
493 <          return(0);
493 >          return 0;
494  
495          /*
496 <         * check for invalid rd_length or too many addresses
496 >         * Check for invalid rd_length or too many addresses
497           */
498          if (rd_length != sizeof(struct in_addr))
499 <          return(0);
499 >          return 0;
500 >
501          v4 = (struct sockaddr_in *)&request->addr;
502          request->addr.ss_len = sizeof(struct sockaddr_in);
503          v4->sin_family = AF_INET;
504          memcpy(&v4->sin_addr, current, sizeof(struct in_addr));
505 <        return(1);
505 >        return 1;
506          break;
676 #ifdef IPV6
507        case T_AAAA:
508          if (request->type != T_AAAA)
509 <          return(0);
509 >          return 0;
510 >
511          if (rd_length != sizeof(struct in6_addr))
512 <          return(0);
512 >          return 0;
513 >
514          request->addr.ss_len = sizeof(struct sockaddr_in6);
515          v6 = (struct sockaddr_in6 *)&request->addr;
516          v6->sin6_family = AF_INET6;
517          memcpy(&v6->sin6_addr, current, sizeof(struct in6_addr));
518 <        return(1);
518 >        return 1;
519          break;
688 #endif
520        case T_PTR:
521          if (request->type != T_PTR)
522 <          return(0);
692 <        n = irc_dn_expand((unsigned char *)buf, (unsigned char *)eob,
693 <            current, hostbuf, sizeof(hostbuf));
694 <        if (n < 0)
695 <          return(0); /* broken message */
696 <        else if (n == 0)
697 <          return(0); /* no more answers left */
522 >          return 0;
523  
524 <        strlcpy(request->name, hostbuf, HOSTLEN + 1);
524 >        n = irc_dn_expand(buf, eob, current, hostbuf, sizeof(hostbuf));
525 >        if (n < 0  /* Broken message */ || n == 0  /* No more answers left */)
526 >          return 0;
527  
528 <        return(1);
528 >        request->namelength = strlcpy(request->name, hostbuf, sizeof(request->name));
529 >        return 1;
530          break;
531 <      case T_CNAME: /* first check we already havent started looking
704 <                       into a cname */
705 <        if (request->type != T_PTR)
706 <          return(0);
707 <
708 <        if (request->state == REQ_CNAME)
709 <        {
710 <          n = irc_dn_expand((unsigned char *)buf, (unsigned char *)eob,
711 <                            current, hostbuf, sizeof(hostbuf));
712 <
713 <          if (n < 0)
714 <            return(0);
715 <          return(1);
716 <        }
717 <
718 <        request->state = REQ_CNAME;
531 >      case T_CNAME:
532          current += rd_length;
533          break;
534 <        
534 >
535        default:
536          /* XXX I'd rather just throw away the entire bogus thing
537           * but its possible its just a broken nameserver with still
538           * valid answers. But lets do some rudimentary logging for now...
539           */
540 <        ilog(L_ERROR, "irc_res.c bogus type %d", type);
540 >        ilog(LOG_TYPE_IRCD, "irc_res.c bogus type %d", type);
541          break;
542      }
543    }
544  
545 <  return(1);
545 >  return 1;
546   }
547  
548   /*
# Line 738 | Line 551 | proc_answer(struct reslist *request, HEA
551   static void
552   res_readreply(fde_t *fd, void *data)
553   {
554 <  char buf[sizeof(HEADER) + MAXPACKET]
742 <        /* Sparc and alpha need 16bit-alignment for accessing header->id
743 <         * (which is uint16_t). Because of the header = (HEADER*) buf;
744 <         * lateron, this is neeeded. --FaUl
745 <         */
746 < #if defined(__sparc__) || defined(__alpha__)  
747 <          __attribute__((aligned (16)))
748 < #endif
749 <          ;
554 >  unsigned char buf[sizeof(HEADER) + MAXPACKET];
555    HEADER *header;
556    struct reslist *request = NULL;
557 <  struct DNSReply *reply  = NULL;
753 <  int rc;
754 <  int answer_count;
557 >  ssize_t rc = 0;
558    socklen_t len = sizeof(struct irc_ssaddr);
559    struct irc_ssaddr lsin;
560  
561 <  rc = recvfrom(fd->fd, buf, sizeof(buf), 0, (struct sockaddr *)&lsin, &len);
562 <
563 <  /* Re-schedule a read *after* recvfrom, or we'll be registering
564 <   * interest where it'll instantly be ready for read :-) -- adrian
762 <   */
763 <  comm_setselect(fd, COMM_SELECT_READ, res_readreply, NULL, 0);
764 <
765 <  /* Better to cast the sizeof instead of rc */
766 <  if (rc <= (int)(sizeof(HEADER)))
767 <    return;
561 >  while ((rc = recvfrom(fd->fd, buf, sizeof(buf), 0, (struct sockaddr *)&lsin, &len)) != -1)
562 >  {
563 >    if (rc <= (ssize_t)sizeof(HEADER))
564 >      continue;
565  
566 <  /*
567 <   * convert DNS reply reader from Network byte order to CPU byte order.
568 <   */
569 <  header = (HEADER *)buf;
570 <  header->ancount = ntohs(header->ancount);
774 <  header->qdcount = ntohs(header->qdcount);
775 <  header->nscount = ntohs(header->nscount);
776 <  header->arcount = ntohs(header->arcount);
566 >    /*
567 >     * Check against possibly fake replies
568 >     */
569 >    if (!res_ourserver(&lsin))
570 >      continue;
571  
572 <  /*
573 <   * response for an id which we have already received an answer for
574 <   * just ignore this response.
575 <   */
576 <  if (0 == (request = find_id(header->id)))
577 <    return;
572 >    /*
573 >     * Convert DNS reply reader from Network byte order to CPU byte order.
574 >     */
575 >    header = (HEADER *)buf;
576 >    header->ancount = ntohs(header->ancount);
577 >    header->qdcount = ntohs(header->qdcount);
578 >    header->nscount = ntohs(header->nscount);
579 >    header->arcount = ntohs(header->arcount);
580  
581 <  /*
582 <   * check against possibly fake replies
583 <   */
584 <  if (!res_ourserver(&lsin))
585 <    return;
581 >    /*
582 >     * Response for an id which we have already received an answer for
583 >     * just ignore this response.
584 >     */
585 >    if ((request = find_id(header->id)) == NULL)
586 >      continue;
587  
588 <  if ((header->rcode != NO_ERRORS) || (header->ancount == 0))
792 <  {
793 <    if (header->rcode == SERVFAIL || header->rcode == NXDOMAIN)
588 >    if (header->rcode != NO_ERRORS || header->ancount == 0)
589      {
590        /*
591         * If a bad error was returned, stop here and don't
592         * send any more (no retries granted).
593         */
594 <      (*request->query->callback)(request->query->ptr, NULL);
594 >      (*request->callback)(request->callback_ctx, NULL, NULL, 0);
595        rem_request(request);
596 +      continue;
597      }
598 < #ifdef IPV6
599 <    else
598 >
599 >    /*
600 >     * If this fails there was an error decoding the received packet.
601 >     * We only give it one shot. If it fails, just leave the client
602 >     * unresolved.
603 >     */
604 >    if (!proc_answer(request, header, buf, buf + rc))
605      {
606 <      /*
607 <       * If we havent already tried this, and we're looking up AAAA, try A
608 <       * now
808 <       */
809 <      if (request->state == REQ_AAAA && request->type == T_AAAA)
810 <      {
811 <        request->timeout += 4;
812 <        resend_query(request);
813 <      }
606 >      (*request->callback)(request->callback_ctx, NULL, NULL, 0);
607 >      rem_request(request);
608 >      continue;
609      }
815 #endif
816
817    return;
818  }
819
820  /*
821   * If this fails there was an error decoding the received packet,
822   * try it again and hope it works the next time.
823   */
824  answer_count = proc_answer(request, header, buf, buf + rc);
610  
826  if (answer_count)
827  {
611      if (request->type == T_PTR)
612      {
613 <      if (request->name == NULL)
613 >      if (request->namelength == 0)
614        {
615          /*
616 <         * got a PTR response with no name, something bogus is happening
616 >         * Got a PTR response with no name, something bogus is happening
617           * don't bother trying again, the client address doesn't resolve
618           */
619 <        (*request->query->callback)(request->query->ptr, reply);
619 >        (*request->callback)(request->callback_ctx, NULL, NULL, 0);
620          rem_request(request);
621 <        return;
621 >        continue;
622        }
623  
624        /*
625 <       * Lookup the 'authoritative' name that we were given for the
843 <       * ip#.
844 <       *
625 >       * Lookup the 'authoritative' name that we were given for the ip#.
626         */
846 #ifdef IPV6
627        if (request->addr.ss.ss_family == AF_INET6)
628 <        gethost_byname_type(request->name, request->query, T_AAAA);
628 >        gethost_byname_type(request->callback, request->callback_ctx, request->name, T_AAAA);
629        else
630 < #endif
851 <      gethost_byname_type(request->name, request->query, T_A);
630 >        gethost_byname_type(request->callback, request->callback_ctx, request->name, T_A);
631        rem_request(request);
632      }
633      else
634      {
635        /*
636 <       * got a name and address response, client resolved
636 >       * Got a name and address response, client resolved
637         */
638 <      reply = make_dnsreply(request);
860 <      (*request->query->callback)(request->query->ptr, reply);
861 <      MyFree(reply);
638 >      (*request->callback)(request->callback_ctx, &request->addr, request->name, request->namelength);
639        rem_request(request);
640      }
641 +
642 +    continue;
643    }
865  else if (!request->sent)
866  {
867    /* XXX - we got a response for a query we didn't send with a valid id?
868     * this should never happen, bail here and leave the client unresolved
869     */
870    assert(0);
644  
645 <    /* XXX don't leak it */
646 <    rem_request(request);
645 >  comm_setselect(fd, COMM_SELECT_READ, res_readreply, NULL, 0);
646 > }
647 >
648 > void
649 > report_dns_servers(struct Client *source_p)
650 > {
651 >  char ipaddr[HOSTIPLEN + 1] = "";
652 >
653 >  for (unsigned int i = 0; i < irc_nscount; ++i)
654 >  {
655 >    getnameinfo((struct sockaddr *)&(irc_nsaddr_list[i]),
656 >                irc_nsaddr_list[i].ss_len, ipaddr,
657 >                sizeof(ipaddr), NULL, 0, NI_NUMERICHOST);
658 >    sendto_one_numeric(source_p, &me, RPL_STATSALINE, ipaddr);
659    }
660   }
661  
662 < static struct DNSReply *
663 < make_dnsreply(struct reslist *request)
662 > /*
663 > * timeout_query_list - Remove queries from the list which have been
664 > * there too long without being resolved.
665 > */
666 > static time_t
667 > timeout_query_list(void)
668   {
669 <  struct DNSReply *cp;
670 <  assert(request != 0);
669 >  dlink_node *ptr = NULL, *ptr_next = NULL;
670 >  struct reslist *request = NULL;
671 >  time_t next_time = 0;
672 >  time_t timeout   = 0;
673 >
674 >  DLINK_FOREACH_SAFE(ptr, ptr_next, request_list.head)
675 >  {
676 >    request = ptr->data;
677 >    timeout = request->sentat + request->timeout;
678  
679 <  cp = (struct DNSReply *)MyMalloc(sizeof(struct DNSReply));
679 >    if (CurrentTime >= timeout)
680 >    {
681 >      if (--request->retries <= 0)
682 >      {
683 >        (*request->callback)(request->callback_ctx, NULL, NULL, 0);
684 >        rem_request(request);
685 >        continue;
686 >      }
687 >      else
688 >      {
689 >        request->sentat = CurrentTime;
690 >        request->timeout += request->timeout;
691 >        resend_query(request);
692 >      }
693 >    }
694  
695 <  cp->h_name = request->name;
696 <  memcpy(&cp->addr, &request->addr, sizeof(cp->addr));
697 <  return(cp);
695 >    if (next_time == 0 || timeout < next_time)
696 >      next_time = timeout;
697 >  }
698 >
699 >  return (next_time > CurrentTime) ? next_time : (CurrentTime + AR_TTL);
700   }
701  
702 < void
703 < report_dns_servers(struct Client *source_p)
702 > /*
703 > * timeout_resolver - check request list
704 > */
705 > static void
706 > timeout_resolver(void *unused)
707   {
708 <  int i;
709 <  char ipaddr[HOSTIPLEN];
708 >  timeout_query_list();
709 > }
710  
711 <  for (i = 0; i < irc_nscount; i++)
711 > /*
712 > * init_resolver - initialize resolver and resolver library
713 > */
714 > void
715 > init_resolver(void)
716 > {
717 >  static struct event event_timeout_resolver =
718    {
719 <    irc_getnameinfo((struct sockaddr *)&(irc_nsaddr_list[i]),
720 <                    irc_nsaddr_list[i].ss_len, ipaddr, HOSTIPLEN, NULL, 0,
721 <                    NI_NUMERICHOST);
722 <    sendto_one(source_p, form_str(RPL_STATSALINE),
723 <               me.name, source_p->name, ipaddr);
724 <  }
719 >    .name = "timeout_resolver",
720 >    .handler = timeout_resolver,
721 >    .when = 1
722 >  };
723 >
724 >  dns_pool = mp_pool_new(sizeof(struct reslist), MP_CHUNK_SIZE_DNS);
725 >
726 >  start_resolver();
727 >  event_add(&event_timeout_resolver, NULL);
728   }

Diff Legend

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