ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/svn/ircd-hybrid/trunk/src/res.c
Revision: 3235
Committed: Sat Mar 29 19:34:16 2014 UTC (11 years, 5 months ago) by michael
Content type: text/x-csrc
Original Path: ircd-hybrid/trunk/src/irc_res.c
File size: 23321 byte(s)
Log Message:
- Style corrections all over the place

File Contents

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

Properties

Name Value
svn:eol-style native
svn:keywords Id Revision