ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/svn/ircd-hybrid-7.2/src/irc_res.c
Revision: 984
Committed: Fri Aug 14 13:18:14 2009 UTC (14 years, 7 months ago) by michael
Content type: text/x-csrc
File size: 24235 byte(s)
Log Message:
- res_readreply(): fix broken logic with ipv6 enabled where we ONLY sent a
                   second A QUERY in case a -broken- nameserver returns NXDOMAIN
                   for AAAA when A record exists. fixed and removed workaround
                   for broken nameservers.

File Contents

# User Rev Content
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 michael 984 if (type != T_A)
458     request->state = REQ_AAAA;
459 adx 30 else
460 michael 984 #endif
461 adx 30 request->state = REQ_A;
462     }
463    
464     request->type = type;
465     query_name(host_name, C_IN, type, request);
466     }
467    
468     /*
469     * do_query_number - Use this to do reverse IP# lookups.
470     */
471     static void
472     do_query_number(struct DNSQuery *query, const struct irc_ssaddr *addr,
473     struct reslist *request)
474     {
475     char ipbuf[128];
476     const unsigned char *cp;
477     #ifdef IPV6
478     const char *intarpa;
479     #endif
480     if (addr->ss.ss_family == AF_INET)
481     {
482     struct sockaddr_in *v4 = (struct sockaddr_in *)addr;
483     cp = (const unsigned char*)&v4->sin_addr.s_addr;
484    
485     ircsprintf(ipbuf, "%u.%u.%u.%u.in-addr.arpa.",
486     (unsigned int)(cp[3]), (unsigned int)(cp[2]),
487     (unsigned int)(cp[1]), (unsigned int)(cp[0]));
488     }
489     #ifdef IPV6
490     else if (addr->ss.ss_family == AF_INET6)
491     {
492     struct sockaddr_in6 *v6 = (struct sockaddr_in6 *)addr;
493     cp = (const unsigned char *)&v6->sin6_addr.s6_addr;
494    
495     if (request != NULL && request->state == REQ_INT)
496     intarpa = "int";
497     else
498     intarpa = "arpa";
499    
500     (void)sprintf(ipbuf, "%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x."
501     "%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.ip6.%s.",
502     (unsigned int)(cp[15]&0xf), (unsigned int)(cp[15]>>4),
503     (unsigned int)(cp[14]&0xf), (unsigned int)(cp[14]>>4),
504     (unsigned int)(cp[13]&0xf), (unsigned int)(cp[13]>>4),
505     (unsigned int)(cp[12]&0xf), (unsigned int)(cp[12]>>4),
506     (unsigned int)(cp[11]&0xf), (unsigned int)(cp[11]>>4),
507     (unsigned int)(cp[10]&0xf), (unsigned int)(cp[10]>>4),
508     (unsigned int)(cp[9]&0xf), (unsigned int)(cp[9]>>4),
509     (unsigned int)(cp[8]&0xf), (unsigned int)(cp[8]>>4),
510     (unsigned int)(cp[7]&0xf), (unsigned int)(cp[7]>>4),
511     (unsigned int)(cp[6]&0xf), (unsigned int)(cp[6]>>4),
512     (unsigned int)(cp[5]&0xf), (unsigned int)(cp[5]>>4),
513     (unsigned int)(cp[4]&0xf), (unsigned int)(cp[4]>>4),
514     (unsigned int)(cp[3]&0xf), (unsigned int)(cp[3]>>4),
515     (unsigned int)(cp[2]&0xf), (unsigned int)(cp[2]>>4),
516     (unsigned int)(cp[1]&0xf), (unsigned int)(cp[1]>>4),
517     (unsigned int)(cp[0]&0xf), (unsigned int)(cp[0]>>4), intarpa);
518     }
519     #endif
520     if (request == NULL)
521     {
522     request = make_request(query);
523     request->type = T_PTR;
524     memcpy(&request->addr, addr, sizeof(struct irc_ssaddr));
525     request->name = (char *)MyMalloc(HOSTLEN + 1);
526     }
527    
528     query_name(ipbuf, C_IN, T_PTR, request);
529     }
530    
531     /*
532     * query_name - generate a query based on class, type and name.
533     */
534     static void
535     query_name(const char *name, int query_class, int type,
536     struct reslist *request)
537     {
538     char buf[MAXPACKET];
539     int request_len = 0;
540    
541     memset(buf, 0, sizeof(buf));
542    
543     if ((request_len = irc_res_mkquery(name, query_class, type,
544     (unsigned char *)buf, sizeof(buf))) > 0)
545     {
546     HEADER *header = (HEADER *)buf;
547 michael 982
548 adx 30 /*
549     * generate an unique id
550     * NOTE: we don't have to worry about converting this to and from
551     * network byte order, the nameserver does not interpret this value
552     * and returns it unchanged
553     */
554     do
555 michael 982 header->id = (header->id + genrand_int32()) & 0xffff;
556     while (find_id(header->id));
557    
558 adx 30 request->id = header->id;
559     ++request->sends;
560    
561     request->sent += send_res_msg(buf, request_len, request->sends);
562     }
563     }
564    
565     static void
566     resend_query(struct reslist *request)
567     {
568     if (request->resend == 0)
569     return;
570    
571 michael 982 switch (request->type)
572 adx 30 {
573     case T_PTR:
574     do_query_number(NULL, &request->addr, request);
575     break;
576     case T_A:
577     do_query_name(NULL, request->name, request, request->type);
578     break;
579     #ifdef IPV6
580     case T_AAAA:
581     /* didnt work, try A */
582     if (request->state == REQ_AAAA)
583     do_query_name(NULL, request->name, request, T_A);
584     #endif
585     default:
586     break;
587     }
588     }
589    
590     /*
591     * proc_answer - process name server reply
592     */
593     static int
594     proc_answer(struct reslist *request, HEADER* header, char* buf, char* eob)
595     {
596     char hostbuf[HOSTLEN + 100]; /* working buffer */
597     unsigned char *current; /* current position in buf */
598     int query_class; /* answer class */
599     int type; /* answer type */
600     int n; /* temp count */
601     int rd_length;
602     struct sockaddr_in *v4; /* conversion */
603     #ifdef IPV6
604     struct sockaddr_in6 *v6;
605     #endif
606     current = (unsigned char *)buf + sizeof(HEADER);
607    
608     for (; header->qdcount > 0; --header->qdcount)
609     {
610     if ((n = irc_dn_skipname(current, (unsigned char *)eob)) < 0)
611     break;
612    
613     current += (size_t) n + QFIXEDSZ;
614     }
615    
616     /*
617     * process each answer sent to us blech.
618     */
619     while (header->ancount > 0 && (char *)current < eob)
620     {
621     header->ancount--;
622    
623     n = irc_dn_expand((unsigned char *)buf, (unsigned char *)eob, current,
624     hostbuf, sizeof(hostbuf));
625    
626     if (n < 0)
627     {
628     /*
629     * broken message
630     */
631     return(0);
632     }
633     else if (n == 0)
634     {
635     /*
636     * no more answers left
637     */
638     return(0);
639     }
640    
641     hostbuf[HOSTLEN] = '\0';
642    
643     /* With Address arithmetic you have to be very anal
644     * this code was not working on alpha due to that
645     * (spotted by rodder/jailbird/dianora)
646     */
647     current += (size_t) n;
648    
649     if (!(((char *)current + ANSWER_FIXED_SIZE) < eob))
650     break;
651    
652     type = irc_ns_get16(current);
653     current += TYPE_SIZE;
654    
655     query_class = irc_ns_get16(current);
656     current += CLASS_SIZE;
657    
658     request->ttl = irc_ns_get32(current);
659     current += TTL_SIZE;
660    
661     rd_length = irc_ns_get16(current);
662     current += RDLENGTH_SIZE;
663    
664     /*
665     * Wait to set request->type until we verify this structure
666     */
667     switch (type)
668     {
669     case T_A:
670     if (request->type != T_A)
671     return(0);
672    
673     /*
674     * check for invalid rd_length or too many addresses
675     */
676     if (rd_length != sizeof(struct in_addr))
677     return(0);
678     v4 = (struct sockaddr_in *)&request->addr;
679     request->addr.ss_len = sizeof(struct sockaddr_in);
680     v4->sin_family = AF_INET;
681     memcpy(&v4->sin_addr, current, sizeof(struct in_addr));
682     return(1);
683     break;
684     #ifdef IPV6
685     case T_AAAA:
686     if (request->type != T_AAAA)
687     return(0);
688     if (rd_length != sizeof(struct in6_addr))
689     return(0);
690     request->addr.ss_len = sizeof(struct sockaddr_in6);
691     v6 = (struct sockaddr_in6 *)&request->addr;
692     v6->sin6_family = AF_INET6;
693     memcpy(&v6->sin6_addr, current, sizeof(struct in6_addr));
694     return(1);
695     break;
696     #endif
697     case T_PTR:
698     if (request->type != T_PTR)
699     return(0);
700     n = irc_dn_expand((unsigned char *)buf, (unsigned char *)eob,
701     current, hostbuf, sizeof(hostbuf));
702     if (n < 0)
703     return(0); /* broken message */
704     else if (n == 0)
705     return(0); /* no more answers left */
706    
707     strlcpy(request->name, hostbuf, HOSTLEN);
708    
709     return(1);
710     break;
711     case T_CNAME: /* first check we already havent started looking
712     into a cname */
713     if (request->type != T_PTR)
714     return(0);
715    
716     if (request->state == REQ_CNAME)
717     {
718     n = irc_dn_expand((unsigned char *)buf, (unsigned char *)eob,
719     current, hostbuf, sizeof(hostbuf));
720    
721     if (n < 0)
722     return(0);
723     return(1);
724     }
725    
726     request->state = REQ_CNAME;
727     current += rd_length;
728     break;
729    
730     default:
731     /* XXX I'd rather just throw away the entire bogus thing
732     * but its possible its just a broken nameserver with still
733     * valid answers. But lets do some rudimentary logging for now...
734     */
735     ilog(L_ERROR, "irc_res.c bogus type %d", type);
736     break;
737     }
738     }
739    
740     return(1);
741     }
742    
743     /*
744     * res_readreply - read a dns reply from the nameserver and process it.
745     */
746     static void
747     res_readreply(fde_t *fd, void *data)
748     {
749 adx 411 char buf[sizeof(HEADER) + MAXPACKET]
750     /* Sparc and alpha need 16bit-alignment for accessing header->id
751     * (which is uint16_t). Because of the header = (HEADER*) buf;
752     * lateron, this is neeeded. --FaUl
753     */
754     #if defined(__sparc__) || defined(__alpha__)
755     __attribute__((aligned (16)))
756     #endif
757     ;
758 adx 30 HEADER *header;
759     struct reslist *request = NULL;
760     struct DNSReply *reply = NULL;
761     int rc;
762     int answer_count;
763     socklen_t len = sizeof(struct irc_ssaddr);
764     struct irc_ssaddr lsin;
765    
766     rc = recvfrom(fd->fd, buf, sizeof(buf), 0, (struct sockaddr *)&lsin, &len);
767    
768     /* Re-schedule a read *after* recvfrom, or we'll be registering
769     * interest where it'll instantly be ready for read :-) -- adrian
770     */
771     comm_setselect(fd, COMM_SELECT_READ, res_readreply, NULL, 0);
772 michael 984
773 adx 30 /* Better to cast the sizeof instead of rc */
774     if (rc <= (int)(sizeof(HEADER)))
775     return;
776    
777     /*
778     * convert DNS reply reader from Network byte order to CPU byte order.
779     */
780     header = (HEADER *)buf;
781     header->ancount = ntohs(header->ancount);
782     header->qdcount = ntohs(header->qdcount);
783     header->nscount = ntohs(header->nscount);
784     header->arcount = ntohs(header->arcount);
785    
786     /*
787     * response for an id which we have already received an answer for
788     * just ignore this response.
789     */
790     if (0 == (request = find_id(header->id)))
791     return;
792    
793     /*
794     * check against possibly fake replies
795     */
796     if (!res_ourserver(&lsin))
797     return;
798    
799     if ((header->rcode != NO_ERRORS) || (header->ancount == 0))
800     {
801 michael 984 if (header->rcode == SERVFAIL || header->rcode == NXDOMAIN)
802 db 155 {
803 michael 984 /*
804     * If a bad error was returned, stop here and don't
805     * send any more (no retries granted).
806     */
807     (*request->query->callback)(request->query->ptr, NULL);
808     rem_request(request);
809     }
810     #ifdef IPV6
811     else
812     {
813 adx 30 /*
814     * If we havent already tried this, and we're looking up AAAA, try A
815     * now
816     */
817     if (request->state == REQ_AAAA && request->type == T_AAAA)
818     {
819     request->timeout += 4;
820     resend_query(request);
821     }
822     else if (request->type == T_PTR && request->state != REQ_INT &&
823     request->addr.ss.ss_family == AF_INET6)
824     {
825     request->state = REQ_INT;
826     request->timeout += 4;
827 db 463 request->retries--;
828 adx 30 resend_query(request);
829     }
830 michael 984 }
831 adx 30 #endif
832 michael 984
833 db 468 return;
834 adx 30 }
835 michael 984
836 adx 30 /*
837     * If this fails there was an error decoding the received packet,
838     * try it again and hope it works the next time.
839     */
840     answer_count = proc_answer(request, header, buf, buf + rc);
841    
842     if (answer_count)
843     {
844     if (request->type == T_PTR)
845     {
846     if (request->name == NULL)
847     {
848     /*
849     * got a PTR response with no name, something bogus is happening
850     * don't bother trying again, the client address doesn't resolve
851     */
852     (*request->query->callback)(request->query->ptr, reply);
853     rem_request(request);
854     return;
855     }
856    
857     /*
858     * Lookup the 'authoritative' name that we were given for the
859     * ip#.
860     *
861     */
862     #ifdef IPV6
863     if (request->addr.ss.ss_family == AF_INET6)
864     gethost_byname_type(request->name, request->query, T_AAAA);
865     else
866     #endif
867     gethost_byname_type(request->name, request->query, T_A);
868     rem_request(request);
869     }
870     else
871     {
872     /*
873     * got a name and address response, client resolved
874     */
875     reply = make_dnsreply(request);
876     (*request->query->callback)(request->query->ptr, reply);
877     MyFree(reply);
878     rem_request(request);
879     }
880     }
881     else if (!request->sent)
882     {
883     /* XXX - we got a response for a query we didn't send with a valid id?
884     * this should never happen, bail here and leave the client unresolved
885     */
886     assert(0);
887    
888     /* XXX don't leak it */
889     rem_request(request);
890     }
891     }
892    
893     static struct DNSReply *
894     make_dnsreply(struct reslist *request)
895     {
896     struct DNSReply *cp;
897     assert(request != 0);
898    
899     cp = (struct DNSReply *)MyMalloc(sizeof(struct DNSReply));
900    
901     cp->h_name = request->name;
902     memcpy(&cp->addr, &request->addr, sizeof(cp->addr));
903     return(cp);
904     }
905    
906     void
907     report_dns_servers(struct Client *source_p)
908     {
909     int i;
910     char ipaddr[HOSTIPLEN];
911    
912     for (i = 0; i < irc_nscount; i++)
913     {
914     irc_getnameinfo((struct sockaddr *)&(irc_nsaddr_list[i]),
915     irc_nsaddr_list[i].ss_len, ipaddr, HOSTIPLEN, NULL, 0,
916     NI_NUMERICHOST);
917     sendto_one(source_p, form_str(RPL_STATSALINE),
918     me.name, source_p->name, ipaddr);
919     }
920     }

Properties

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