ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/svn/ircd-hybrid/libio/net/res.c
Revision: 65
Committed: Mon Oct 3 23:33:16 2005 UTC (20 years, 10 months ago) by adx
Content type: text/x-csrc
File size: 24479 byte(s)
Log Message:
- removed external references from libio/misc
- imported s_misc.c to libio, moved CurrentTime there

File Contents

# User Rev Content
1 adx 60 /*
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     * $Id: irc_res.c 33 2005-10-02 20:50:00Z knight $
11     *
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     #include "fdlist.h"
34     #include "fileio.h" /* for fbopen / fbclose / fbputs */
35     #include "s_bsd.h"
36     #include "s_log.h"
37     #include "send.h"
38     #include "memory.h"
39     #include "irc_res.h"
40     #include "reslib.h"
41     #include "irc_getnameinfo.h"
42 adx 65 #include "s_misc.h"
43 adx 60
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     for (ns = 0; ns < irc_nscount; ns++)
141     {
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     #ifdef HAVE_SRAND48
261     srand48(CurrentTime);
262     #endif
263     memset(&ResolverFileDescriptor, 0, sizeof(fde_t));
264     start_resolver();
265     }
266    
267     /*
268     * restart_resolver - reread resolv.conf, reopen socket
269     */
270     void
271     restart_resolver(void)
272     {
273     fd_close(&ResolverFileDescriptor);
274     eventDelete(timeout_resolver, NULL); /* -ddosen */
275     start_resolver();
276     }
277    
278     /*
279     * add_local_domain - Add the domain to hostname, if it is missing
280     * (as suggested by eps@TOASTER.SFSU.EDU)
281     */
282     void
283     add_local_domain(char *hname, size_t size)
284     {
285     /* try to fix up unqualified names
286     */
287     if (strchr(hname, '.') == NULL)
288     {
289     if (irc_domain[0])
290     {
291     size_t len = strlen(hname);
292    
293     if ((strlen(irc_domain) + len + 2) < size)
294     {
295     hname[len++] = '.';
296     strcpy(hname + len, irc_domain);
297     }
298     }
299     }
300     }
301    
302     /*
303     * rem_request - remove a request from the list.
304     * This must also free any memory that has been allocated for
305     * temporary storage of DNS results.
306     */
307     static void
308     rem_request(struct reslist *request)
309     {
310     dlinkDelete(&request->node, &request_list);
311     MyFree(request->name);
312     MyFree(request);
313     }
314    
315     /*
316     * make_request - Create a DNS request record for the server.
317     */
318     static struct reslist *
319     make_request(struct DNSQuery* query)
320     {
321     struct reslist *request;
322    
323     request = (struct reslist *)MyMalloc(sizeof(struct reslist));
324     memset(request, 0, sizeof(struct reslist));
325    
326     request->sentat = CurrentTime;
327     request->retries = 3;
328     request->resend = 1;
329     request->timeout = 4; /* start at 4 and exponential inc. */
330     memset(&request->addr, 0, sizeof(request->addr));
331     request->query = query;
332     request->state = REQ_IDLE;
333    
334     dlinkAdd(request, &request->node, &request_list);
335     return(request);
336     }
337    
338     /*
339     * delete_resolver_queries - cleanup outstanding queries
340     * for which there no longer exist clients or conf lines.
341     */
342     void
343     delete_resolver_queries(const struct DNSQuery *query)
344     {
345     dlink_node *ptr;
346     dlink_node *next_ptr;
347     struct reslist *request;
348    
349     DLINK_FOREACH_SAFE(ptr, next_ptr, request_list.head)
350     {
351     if ((request = ptr->data) != NULL)
352     {
353     if (query == request->query)
354     rem_request(request);
355     }
356     }
357     }
358    
359     /*
360     * send_res_msg - sends msg to all nameservers found in the "_res" structure.
361     * This should reflect /etc/resolv.conf. We will get responses
362     * which arent needed but is easier than checking to see if nameserver
363     * isnt present. Returns number of messages successfully sent to
364     * nameservers or -1 if no successful sends.
365     */
366     static int
367     send_res_msg(const char *msg, int len, int rcount)
368     {
369     int i;
370     int sent = 0;
371     int max_queries = IRCD_MIN(irc_nscount, rcount);
372    
373     /* RES_PRIMARY option is not implemented
374     * if (res.options & RES_PRIMARY || 0 == max_queries)
375     */
376     if (max_queries == 0)
377     max_queries = 1;
378    
379     for (i = 0; i < max_queries; i++)
380     {
381     if (sendto(ResolverFileDescriptor.fd, msg, len, 0,
382     (struct sockaddr*)&(irc_nsaddr_list[i]),
383     irc_nsaddr_list[i].ss_len) == len)
384     ++sent;
385     }
386    
387     return(sent);
388     }
389    
390     /*
391     * find_id - find a dns request id (id is determined by dn_mkquery)
392     */
393     static struct reslist *
394     find_id(int id)
395     {
396     dlink_node *ptr;
397     struct reslist *request;
398    
399     DLINK_FOREACH(ptr, request_list.head)
400     {
401     request = ptr->data;
402    
403     if (request->id == id)
404     return(request);
405     }
406    
407     return(NULL);
408     }
409    
410     /*
411     * gethost_byname_type - get host address from name
412     *
413     */
414     void
415     gethost_byname_type(const char *name, struct DNSQuery *query, int type)
416     {
417     assert(name != 0);
418     do_query_name(query, name, NULL, type);
419     }
420    
421     /*
422     * gethost_byname - wrapper for _type - send T_AAAA first if IPV6 supported
423     */
424     void
425     gethost_byname(const char *name, struct DNSQuery *query)
426     {
427     #ifdef IPV6
428     gethost_byname_type(name, query, T_AAAA);
429     #else
430     gethost_byname_type(name, query, T_A);
431     #endif
432     }
433    
434     /*
435     * gethost_byaddr - get host name from address
436     */
437     void
438     gethost_byaddr(const struct irc_ssaddr *addr, struct DNSQuery *query)
439     {
440     do_query_number(query, addr, NULL);
441     }
442    
443     /*
444     * do_query_name - nameserver lookup name
445     */
446     static void
447     do_query_name(struct DNSQuery *query, const char *name,
448     struct reslist *request, int type)
449     {
450     char host_name[HOSTLEN + 1];
451    
452     strlcpy(host_name, name, HOSTLEN);
453     add_local_domain(host_name, HOSTLEN);
454    
455     if (request == NULL)
456     {
457     request = make_request(query);
458     request->name = (char *)MyMalloc(strlen(host_name) + 1);
459     request->type = type;
460     strcpy(request->name, host_name);
461     #ifdef IPV6
462     if (type == T_A)
463     request->state = REQ_A;
464     else
465     request->state = REQ_AAAA;
466     #else
467     request->state = REQ_A;
468     #endif
469     }
470    
471     request->type = type;
472     query_name(host_name, C_IN, type, request);
473     }
474    
475     /*
476     * do_query_number - Use this to do reverse IP# lookups.
477     */
478     static void
479     do_query_number(struct DNSQuery *query, const struct irc_ssaddr *addr,
480     struct reslist *request)
481     {
482     char ipbuf[128];
483     const unsigned char *cp;
484     #ifdef IPV6
485     const char *intarpa;
486     #endif
487     if (addr->ss.ss_family == AF_INET)
488     {
489     struct sockaddr_in *v4 = (struct sockaddr_in *)addr;
490     cp = (const unsigned char*)&v4->sin_addr.s_addr;
491    
492     ircsprintf(ipbuf, "%u.%u.%u.%u.in-addr.arpa.",
493     (unsigned int)(cp[3]), (unsigned int)(cp[2]),
494     (unsigned int)(cp[1]), (unsigned int)(cp[0]));
495     }
496     #ifdef IPV6
497     else if (addr->ss.ss_family == AF_INET6)
498     {
499     struct sockaddr_in6 *v6 = (struct sockaddr_in6 *)addr;
500     cp = (const unsigned char *)&v6->sin6_addr.s6_addr;
501    
502     if (request != NULL && request->state == REQ_INT)
503     intarpa = "int";
504     else
505     intarpa = "arpa";
506    
507     (void)sprintf(ipbuf, "%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x."
508     "%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.ip6.%s.",
509     (unsigned int)(cp[15]&0xf), (unsigned int)(cp[15]>>4),
510     (unsigned int)(cp[14]&0xf), (unsigned int)(cp[14]>>4),
511     (unsigned int)(cp[13]&0xf), (unsigned int)(cp[13]>>4),
512     (unsigned int)(cp[12]&0xf), (unsigned int)(cp[12]>>4),
513     (unsigned int)(cp[11]&0xf), (unsigned int)(cp[11]>>4),
514     (unsigned int)(cp[10]&0xf), (unsigned int)(cp[10]>>4),
515     (unsigned int)(cp[9]&0xf), (unsigned int)(cp[9]>>4),
516     (unsigned int)(cp[8]&0xf), (unsigned int)(cp[8]>>4),
517     (unsigned int)(cp[7]&0xf), (unsigned int)(cp[7]>>4),
518     (unsigned int)(cp[6]&0xf), (unsigned int)(cp[6]>>4),
519     (unsigned int)(cp[5]&0xf), (unsigned int)(cp[5]>>4),
520     (unsigned int)(cp[4]&0xf), (unsigned int)(cp[4]>>4),
521     (unsigned int)(cp[3]&0xf), (unsigned int)(cp[3]>>4),
522     (unsigned int)(cp[2]&0xf), (unsigned int)(cp[2]>>4),
523     (unsigned int)(cp[1]&0xf), (unsigned int)(cp[1]>>4),
524     (unsigned int)(cp[0]&0xf), (unsigned int)(cp[0]>>4), intarpa);
525     }
526     #endif
527     if (request == NULL)
528     {
529     request = make_request(query);
530     request->type = T_PTR;
531     memcpy(&request->addr, addr, sizeof(struct irc_ssaddr));
532     request->name = (char *)MyMalloc(HOSTLEN + 1);
533     }
534    
535     query_name(ipbuf, C_IN, T_PTR, request);
536     }
537    
538     /*
539     * query_name - generate a query based on class, type and name.
540     */
541     static void
542     query_name(const char *name, int query_class, int type,
543     struct reslist *request)
544     {
545     char buf[MAXPACKET];
546     int request_len = 0;
547    
548     memset(buf, 0, sizeof(buf));
549    
550     if ((request_len = irc_res_mkquery(name, query_class, type,
551     (unsigned char *)buf, sizeof(buf))) > 0)
552     {
553     HEADER *header = (HEADER *)buf;
554     #ifndef HAVE_LRAND48
555     int k = 0;
556     struct timeval tv;
557     #endif
558     /*
559     * generate an unique id
560     * NOTE: we don't have to worry about converting this to and from
561     * network byte order, the nameserver does not interpret this value
562     * and returns it unchanged
563     */
564     #ifdef HAVE_LRAND48
565     do
566     {
567     header->id = (header->id + lrand48()) & 0xffff;
568     } while (find_id(header->id));
569     #else
570     gettimeofday(&tv, NULL);
571     do
572     {
573     header->id = (header->id + k + tv.tv_usec) & 0xffff;
574     k++;
575     } while (find_id(header->id));
576     #endif /* HAVE_LRAND48 */
577     request->id = header->id;
578     ++request->sends;
579    
580     request->sent += send_res_msg(buf, request_len, request->sends);
581     }
582     }
583    
584     static void
585     resend_query(struct reslist *request)
586     {
587     if (request->resend == 0)
588     return;
589    
590     switch(request->type)
591     {
592     case T_PTR:
593     do_query_number(NULL, &request->addr, request);
594     break;
595     case T_A:
596     do_query_name(NULL, request->name, request, request->type);
597     break;
598     #ifdef IPV6
599     case T_AAAA:
600     /* didnt work, try A */
601     if (request->state == REQ_AAAA)
602     do_query_name(NULL, request->name, request, T_A);
603     #endif
604     default:
605     break;
606     }
607     }
608    
609     /*
610     * proc_answer - process name server reply
611     */
612     static int
613     proc_answer(struct reslist *request, HEADER* header, char* buf, char* eob)
614     {
615     char hostbuf[HOSTLEN + 100]; /* working buffer */
616     unsigned char *current; /* current position in buf */
617     int query_class; /* answer class */
618     int type; /* answer type */
619     int n; /* temp count */
620     int rd_length;
621     struct sockaddr_in *v4; /* conversion */
622     #ifdef IPV6
623     struct sockaddr_in6 *v6;
624     #endif
625     current = (unsigned char *)buf + sizeof(HEADER);
626    
627     for (; header->qdcount > 0; --header->qdcount)
628     {
629     if ((n = irc_dn_skipname(current, (unsigned char *)eob)) < 0)
630     break;
631    
632     current += (size_t) n + QFIXEDSZ;
633     }
634    
635     /*
636     * process each answer sent to us blech.
637     */
638     while (header->ancount > 0 && (char *)current < eob)
639     {
640     header->ancount--;
641    
642     n = irc_dn_expand((unsigned char *)buf, (unsigned char *)eob, current,
643     hostbuf, sizeof(hostbuf));
644    
645     if (n < 0)
646     {
647     /*
648     * broken message
649     */
650     return(0);
651     }
652     else if (n == 0)
653     {
654     /*
655     * no more answers left
656     */
657     return(0);
658     }
659    
660     hostbuf[HOSTLEN] = '\0';
661    
662     /* With Address arithmetic you have to be very anal
663     * this code was not working on alpha due to that
664     * (spotted by rodder/jailbird/dianora)
665     */
666     current += (size_t) n;
667    
668     if (!(((char *)current + ANSWER_FIXED_SIZE) < eob))
669     break;
670    
671     type = irc_ns_get16(current);
672     current += TYPE_SIZE;
673    
674     query_class = irc_ns_get16(current);
675     current += CLASS_SIZE;
676    
677     request->ttl = irc_ns_get32(current);
678     current += TTL_SIZE;
679    
680     rd_length = irc_ns_get16(current);
681     current += RDLENGTH_SIZE;
682    
683     /*
684     * Wait to set request->type until we verify this structure
685     */
686     switch (type)
687     {
688     case T_A:
689     if (request->type != T_A)
690     return(0);
691    
692     /*
693     * check for invalid rd_length or too many addresses
694     */
695     if (rd_length != sizeof(struct in_addr))
696     return(0);
697     v4 = (struct sockaddr_in *)&request->addr;
698     request->addr.ss_len = sizeof(struct sockaddr_in);
699     v4->sin_family = AF_INET;
700     memcpy(&v4->sin_addr, current, sizeof(struct in_addr));
701     return(1);
702     break;
703     #ifdef IPV6
704     case T_AAAA:
705     if (request->type != T_AAAA)
706     return(0);
707     if (rd_length != sizeof(struct in6_addr))
708     return(0);
709     request->addr.ss_len = sizeof(struct sockaddr_in6);
710     v6 = (struct sockaddr_in6 *)&request->addr;
711     v6->sin6_family = AF_INET6;
712     memcpy(&v6->sin6_addr, current, sizeof(struct in6_addr));
713     return(1);
714     break;
715     #endif
716     case T_PTR:
717     if (request->type != T_PTR)
718     return(0);
719     n = irc_dn_expand((unsigned char *)buf, (unsigned char *)eob,
720     current, hostbuf, sizeof(hostbuf));
721     if (n < 0)
722     return(0); /* broken message */
723     else if (n == 0)
724     return(0); /* no more answers left */
725    
726     strlcpy(request->name, hostbuf, HOSTLEN);
727    
728     return(1);
729     break;
730     case T_CNAME: /* first check we already havent started looking
731     into a cname */
732     if (request->type != T_PTR)
733     return(0);
734    
735     if (request->state == REQ_CNAME)
736     {
737     n = irc_dn_expand((unsigned char *)buf, (unsigned char *)eob,
738     current, hostbuf, sizeof(hostbuf));
739    
740     if (n < 0)
741     return(0);
742     return(1);
743     }
744    
745     request->state = REQ_CNAME;
746     current += rd_length;
747     break;
748    
749     default:
750     /* XXX I'd rather just throw away the entire bogus thing
751     * but its possible its just a broken nameserver with still
752     * valid answers. But lets do some rudimentary logging for now...
753     */
754     ilog(L_ERROR, "irc_res.c bogus type %d", type);
755     break;
756     }
757     }
758    
759     return(1);
760     }
761    
762     /*
763     * res_readreply - read a dns reply from the nameserver and process it.
764     */
765     static void
766     res_readreply(fde_t *fd, void *data)
767     {
768     char buf[sizeof(HEADER) + MAXPACKET];
769     HEADER *header;
770     struct reslist *request = NULL;
771     struct DNSReply *reply = NULL;
772     int rc;
773     int answer_count;
774     socklen_t len = sizeof(struct irc_ssaddr);
775     struct irc_ssaddr lsin;
776    
777     rc = recvfrom(fd->fd, buf, sizeof(buf), 0, (struct sockaddr *)&lsin, &len);
778    
779     /* Re-schedule a read *after* recvfrom, or we'll be registering
780     * interest where it'll instantly be ready for read :-) -- adrian
781     */
782     comm_setselect(fd, COMM_SELECT_READ, res_readreply, NULL, 0);
783     /* Better to cast the sizeof instead of rc */
784     if (rc <= (int)(sizeof(HEADER)))
785     return;
786    
787     /*
788     * convert DNS reply reader from Network byte order to CPU byte order.
789     */
790     header = (HEADER *)buf;
791     header->ancount = ntohs(header->ancount);
792     header->qdcount = ntohs(header->qdcount);
793     header->nscount = ntohs(header->nscount);
794     header->arcount = ntohs(header->arcount);
795    
796     /*
797     * response for an id which we have already received an answer for
798     * just ignore this response.
799     */
800     if (0 == (request = find_id(header->id)))
801     return;
802    
803     /*
804     * check against possibly fake replies
805     */
806     if (!res_ourserver(&lsin))
807     return;
808    
809     if ((header->rcode != NO_ERRORS) || (header->ancount == 0))
810     {
811     if (SERVFAIL == header->rcode)
812     resend_query(request);
813     else
814     {
815     /*
816     * If we havent already tried this, and we're looking up AAAA, try A
817     * now
818     */
819    
820     #ifdef IPV6
821     if (request->state == REQ_AAAA && request->type == T_AAAA)
822     {
823     request->timeout += 4;
824     resend_query(request);
825     }
826     else if (request->type == T_PTR && request->state != REQ_INT &&
827     request->addr.ss.ss_family == AF_INET6)
828     {
829     request->state = REQ_INT;
830     request->timeout += 4;
831     request->retries--;
832     resend_query(request);
833     }
834     else
835     #endif
836     {
837     /*
838     * If a bad error was returned, we stop here and dont send
839     * send any more (no retries granted).
840     */
841     (*request->query->callback)(request->query->ptr, NULL);
842     rem_request(request);
843     }
844     }
845    
846     return;
847     }
848     /*
849     * If this fails there was an error decoding the received packet,
850     * try it again and hope it works the next time.
851     */
852     answer_count = proc_answer(request, header, buf, buf + rc);
853    
854     if (answer_count)
855     {
856     if (request->type == T_PTR)
857     {
858     if (request->name == NULL)
859     {
860     /*
861     * got a PTR response with no name, something bogus is happening
862     * don't bother trying again, the client address doesn't resolve
863     */
864     (*request->query->callback)(request->query->ptr, reply);
865     rem_request(request);
866     return;
867     }
868    
869     /*
870     * Lookup the 'authoritative' name that we were given for the
871     * ip#.
872     *
873     */
874     #ifdef IPV6
875     if (request->addr.ss.ss_family == AF_INET6)
876     gethost_byname_type(request->name, request->query, T_AAAA);
877     else
878     #endif
879     gethost_byname_type(request->name, request->query, T_A);
880     rem_request(request);
881     }
882     else
883     {
884     /*
885     * got a name and address response, client resolved
886     */
887     reply = make_dnsreply(request);
888     (*request->query->callback)(request->query->ptr, reply);
889     MyFree(reply);
890     rem_request(request);
891     }
892     }
893     else if (!request->sent)
894     {
895     /* XXX - we got a response for a query we didn't send with a valid id?
896     * this should never happen, bail here and leave the client unresolved
897     */
898     assert(0);
899    
900     /* XXX don't leak it */
901     rem_request(request);
902     }
903     }
904    
905     static struct DNSReply *
906     make_dnsreply(struct reslist *request)
907     {
908     struct DNSReply *cp;
909     assert(request != 0);
910    
911     cp = (struct DNSReply *)MyMalloc(sizeof(struct DNSReply));
912    
913     cp->h_name = request->name;
914     memcpy(&cp->addr, &request->addr, sizeof(cp->addr));
915     return(cp);
916     }
917    
918     void
919     report_dns_servers(struct Client *source_p)
920     {
921     int i;
922     char ipaddr[HOSTIPLEN];
923    
924     for (i = 0; i < irc_nscount; i++)
925     {
926     irc_getnameinfo((struct sockaddr *)&(irc_nsaddr_list[i]),
927     irc_nsaddr_list[i].ss_len, ipaddr, HOSTIPLEN, NULL, 0,
928     NI_NUMERICHOST);
929     sendto_one(source_p, form_str(RPL_STATSALINE),
930     me.name, source_p->name, ipaddr);
931     }
932     }

Properties

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