/[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 982 - (hide annotations)
Thu Aug 13 22:15:08 2009 UTC (10 years, 2 months ago) by michael
File MIME type: text/x-chdr
File size: 24566 byte(s)
- added a mersenne twister basedn prng

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

Properties

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

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