/[svn]/ircd-hybrid-7.2/src/irc_res.c
ViewVC logotype

Annotation of /ircd-hybrid-7.2/src/irc_res.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 995 - (hide annotations)
Fri Aug 21 10:11:14 2009 UTC (10 years, 2 months ago) by michael
File MIME type: text/x-chdr
File size: 23025 byte(s)
- remove add_local_domain

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

Properties

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

svnadmin@ircd-hybrid.org
ViewVC Help
Powered by ViewVC 1.1.26