ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/svn/ircd-hybrid/branches/8.2.x/src/res.c
Revision: 1029
Committed: Sun Nov 8 13:10:50 2009 UTC (14 years, 5 months ago) by michael
Content type: text/x-csrc
Original Path: ircd-hybrid-7.3/src/irc_res.c
File size: 23057 byte(s)
Log Message:
- branch off trunk to create 7.3 branch

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 michael 1011 #include "list.h"
24 michael 998 #include "balloc.h"
25 adx 30 #include "client.h"
26     #include "event.h"
27     #include "irc_string.h"
28     #include "sprintf_irc.h"
29     #include "ircd.h"
30     #include "numeric.h"
31 michael 982 #include "rng_mt.h"
32 adx 30 #include "fdlist.h"
33     #include "fileio.h" /* for fbopen / fbclose / fbputs */
34     #include "s_bsd.h"
35     #include "s_log.h"
36     #include "send.h"
37     #include "memory.h"
38     #include "irc_res.h"
39     #include "irc_reslib.h"
40     #include "irc_getnameinfo.h"
41 michael 1011 #include "common.h"
42 adx 30
43     #if (CHAR_BIT != 8)
44     #error this code needs to be able to address individual octets
45     #endif
46    
47     static PF res_readreply;
48    
49     #define MAXPACKET 1024 /* rfc sez 512 but we expand names so ... */
50     #define RES_MAXALIASES 35 /* maximum aliases allowed */
51     #define RES_MAXADDRS 35 /* maximum addresses allowed */
52     #define AR_TTL 600 /* TTL in seconds for dns cache entries */
53    
54     /* RFC 1104/1105 wasn't very helpful about what these fields
55     * should be named, so for now, we'll just name them this way.
56     * we probably should look at what named calls them or something.
57     */
58     #define TYPE_SIZE (size_t)2
59     #define CLASS_SIZE (size_t)2
60     #define TTL_SIZE (size_t)4
61     #define RDLENGTH_SIZE (size_t)2
62     #define ANSWER_FIXED_SIZE (TYPE_SIZE + CLASS_SIZE + TTL_SIZE + RDLENGTH_SIZE)
63    
64     typedef enum
65     {
66     REQ_IDLE, /* We're doing not much at all */
67     REQ_PTR, /* Looking up a PTR */
68     REQ_A, /* Looking up an A, possibly because AAAA failed */
69     #ifdef IPV6
70     REQ_AAAA, /* Looking up an AAAA */
71     #endif
72 michael 985 REQ_CNAME /* We got a CNAME in response, we better get a real answer next */
73 adx 30 } request_state;
74    
75     struct reslist
76     {
77     dlink_node node;
78     int id;
79     int sent; /* number of requests sent */
80     request_state state; /* State the resolver machine is in */
81     time_t ttl;
82     char type;
83     char retries; /* retry counter */
84     char sends; /* number of sends (>1 means resent) */
85     char resend; /* send flag. 0 == dont resend */
86     time_t sentat;
87     time_t timeout;
88     struct irc_ssaddr addr;
89     char *name;
90 michael 992 dns_callback_fnc callback;
91     void *callback_ctx;
92 adx 30 };
93    
94     static fde_t ResolverFileDescriptor;
95 michael 986 static dlink_list request_list = { NULL, NULL, 0 };
96 michael 998 static BlockHeap *dns_heap = NULL;
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 michael 998 dns_heap = BlockHeapCreate("dns", sizeof(struct reslist), DNS_HEAP_SIZE);
256 adx 30 memset(&ResolverFileDescriptor, 0, sizeof(fde_t));
257     start_resolver();
258     }
259    
260     /*
261     * restart_resolver - reread resolv.conf, reopen socket
262     */
263     void
264     restart_resolver(void)
265     {
266     fd_close(&ResolverFileDescriptor);
267     eventDelete(timeout_resolver, NULL); /* -ddosen */
268     start_resolver();
269     }
270    
271     /*
272     * rem_request - remove a request from the list.
273     * This must also free any memory that has been allocated for
274     * temporary storage of DNS results.
275     */
276     static void
277     rem_request(struct reslist *request)
278     {
279     dlinkDelete(&request->node, &request_list);
280 michael 998
281 adx 30 MyFree(request->name);
282 michael 998 BlockHeapFree(dns_heap, request);
283 adx 30 }
284    
285     /*
286     * make_request - Create a DNS request record for the server.
287     */
288     static struct reslist *
289 michael 992 make_request(dns_callback_fnc callback, void *ctx)
290 adx 30 {
291 michael 998 struct reslist *request = BlockHeapAlloc(dns_heap);
292 adx 30
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 michael 998 return request;
303 adx 30 }
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 michael 997 dlink_node *ptr = NULL, *next_ptr = NULL;
313 adx 30
314     DLINK_FOREACH_SAFE(ptr, next_ptr, request_list.head)
315     {
316 michael 997 struct reslist *request = ptr->data;
317    
318     if (request->callback_ctx == vptr)
319     rem_request(request);
320 adx 30 }
321     }
322    
323     /*
324     * send_res_msg - sends msg to all nameservers found in the "_res" structure.
325     * This should reflect /etc/resolv.conf. We will get responses
326     * which arent needed but is easier than checking to see if nameserver
327     * isnt present. Returns number of messages successfully sent to
328     * nameservers or -1 if no successful sends.
329     */
330     static int
331     send_res_msg(const char *msg, int len, int rcount)
332     {
333     int i;
334     int sent = 0;
335     int max_queries = IRCD_MIN(irc_nscount, rcount);
336    
337     /* RES_PRIMARY option is not implemented
338     * if (res.options & RES_PRIMARY || 0 == max_queries)
339     */
340     if (max_queries == 0)
341     max_queries = 1;
342    
343     for (i = 0; i < max_queries; i++)
344     {
345     if (sendto(ResolverFileDescriptor.fd, msg, len, 0,
346     (struct sockaddr*)&(irc_nsaddr_list[i]),
347     irc_nsaddr_list[i].ss_len) == len)
348     ++sent;
349     }
350    
351     return(sent);
352     }
353    
354     /*
355     * find_id - find a dns request id (id is determined by dn_mkquery)
356     */
357     static struct reslist *
358     find_id(int id)
359     {
360     dlink_node *ptr;
361     struct reslist *request;
362    
363     DLINK_FOREACH(ptr, request_list.head)
364     {
365     request = ptr->data;
366    
367     if (request->id == id)
368     return(request);
369     }
370    
371     return(NULL);
372     }
373    
374     /*
375     * gethost_byname_type - get host address from name
376     *
377     */
378     void
379 michael 992 gethost_byname_type(dns_callback_fnc callback, void *ctx, const char *name, int type)
380 adx 30 {
381     assert(name != 0);
382 michael 992 do_query_name(callback, ctx, name, NULL, type);
383 adx 30 }
384    
385     /*
386     * gethost_byname - wrapper for _type - send T_AAAA first if IPV6 supported
387     */
388     void
389 michael 992 gethost_byname(dns_callback_fnc callback, void *ctx, const char *name)
390 adx 30 {
391     #ifdef IPV6
392 michael 992 gethost_byname_type(callback, ctx, name, T_AAAA);
393 adx 30 #else
394 michael 992 gethost_byname_type(callback, ctx, name, T_A);
395 adx 30 #endif
396     }
397    
398     /*
399     * gethost_byaddr - get host name from address
400     */
401     void
402 michael 992 gethost_byaddr(dns_callback_fnc callback, void *ctx, const struct irc_ssaddr *addr)
403 adx 30 {
404 michael 992 do_query_number(callback, ctx, addr, NULL);
405 adx 30 }
406    
407     /*
408     * do_query_name - nameserver lookup name
409     */
410     static void
411 michael 992 do_query_name(dns_callback_fnc callback, void *ctx, const char *name,
412 adx 30 struct reslist *request, int type)
413     {
414     char host_name[HOSTLEN + 1];
415    
416 michael 998 strlcpy(host_name, name, sizeof(host_name));
417 adx 30
418     if (request == NULL)
419     {
420 michael 992 request = make_request(callback, ctx);
421 michael 986 request->name = MyMalloc(strlen(host_name) + 1);
422 adx 30 request->type = type;
423     strcpy(request->name, host_name);
424     #ifdef IPV6
425 michael 984 if (type != T_A)
426     request->state = REQ_AAAA;
427 adx 30 else
428 michael 984 #endif
429 adx 30 request->state = REQ_A;
430     }
431    
432     request->type = type;
433     query_name(host_name, C_IN, type, request);
434     }
435    
436     /*
437     * do_query_number - Use this to do reverse IP# lookups.
438     */
439     static void
440 michael 992 do_query_number(dns_callback_fnc callback, void *ctx,
441     const struct irc_ssaddr *addr,
442 adx 30 struct reslist *request)
443     {
444     char ipbuf[128];
445     const unsigned char *cp;
446 michael 985
447 adx 30 if (addr->ss.ss_family == AF_INET)
448     {
449 michael 992 const struct sockaddr_in *v4 = (const struct sockaddr_in *)addr;
450 adx 30 cp = (const unsigned char*)&v4->sin_addr.s_addr;
451    
452     ircsprintf(ipbuf, "%u.%u.%u.%u.in-addr.arpa.",
453     (unsigned int)(cp[3]), (unsigned int)(cp[2]),
454     (unsigned int)(cp[1]), (unsigned int)(cp[0]));
455     }
456     #ifdef IPV6
457     else if (addr->ss.ss_family == AF_INET6)
458     {
459 michael 992 const struct sockaddr_in6 *v6 = (const struct sockaddr_in6 *)addr;
460 adx 30 cp = (const unsigned char *)&v6->sin6_addr.s6_addr;
461    
462 michael 985 sprintf(ipbuf, "%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x."
463     "%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.ip6.arpa.",
464 adx 30 (unsigned int)(cp[15]&0xf), (unsigned int)(cp[15]>>4),
465     (unsigned int)(cp[14]&0xf), (unsigned int)(cp[14]>>4),
466     (unsigned int)(cp[13]&0xf), (unsigned int)(cp[13]>>4),
467     (unsigned int)(cp[12]&0xf), (unsigned int)(cp[12]>>4),
468     (unsigned int)(cp[11]&0xf), (unsigned int)(cp[11]>>4),
469     (unsigned int)(cp[10]&0xf), (unsigned int)(cp[10]>>4),
470     (unsigned int)(cp[9]&0xf), (unsigned int)(cp[9]>>4),
471     (unsigned int)(cp[8]&0xf), (unsigned int)(cp[8]>>4),
472     (unsigned int)(cp[7]&0xf), (unsigned int)(cp[7]>>4),
473     (unsigned int)(cp[6]&0xf), (unsigned int)(cp[6]>>4),
474     (unsigned int)(cp[5]&0xf), (unsigned int)(cp[5]>>4),
475     (unsigned int)(cp[4]&0xf), (unsigned int)(cp[4]>>4),
476     (unsigned int)(cp[3]&0xf), (unsigned int)(cp[3]>>4),
477     (unsigned int)(cp[2]&0xf), (unsigned int)(cp[2]>>4),
478     (unsigned int)(cp[1]&0xf), (unsigned int)(cp[1]>>4),
479 michael 985 (unsigned int)(cp[0]&0xf), (unsigned int)(cp[0]>>4));
480 adx 30 }
481     #endif
482     if (request == NULL)
483     {
484 michael 992 request = make_request(callback, ctx);
485 adx 30 request->type = T_PTR;
486     memcpy(&request->addr, addr, sizeof(struct irc_ssaddr));
487 michael 986 request->name = MyMalloc(HOSTLEN + 1);
488 adx 30 }
489    
490     query_name(ipbuf, C_IN, T_PTR, request);
491     }
492    
493     /*
494     * query_name - generate a query based on class, type and name.
495     */
496     static void
497     query_name(const char *name, int query_class, int type,
498     struct reslist *request)
499     {
500     char buf[MAXPACKET];
501     int request_len = 0;
502    
503     memset(buf, 0, sizeof(buf));
504    
505     if ((request_len = irc_res_mkquery(name, query_class, type,
506     (unsigned char *)buf, sizeof(buf))) > 0)
507     {
508     HEADER *header = (HEADER *)buf;
509 michael 982
510 adx 30 /*
511     * generate an unique id
512     * NOTE: we don't have to worry about converting this to and from
513     * network byte order, the nameserver does not interpret this value
514     * and returns it unchanged
515     */
516     do
517 michael 982 header->id = (header->id + genrand_int32()) & 0xffff;
518     while (find_id(header->id));
519    
520 adx 30 request->id = header->id;
521     ++request->sends;
522    
523     request->sent += send_res_msg(buf, request_len, request->sends);
524     }
525     }
526    
527     static void
528     resend_query(struct reslist *request)
529     {
530     if (request->resend == 0)
531     return;
532    
533 michael 982 switch (request->type)
534 adx 30 {
535     case T_PTR:
536 michael 992 do_query_number(NULL, NULL, &request->addr, request);
537 adx 30 break;
538     case T_A:
539 michael 992 do_query_name(NULL, NULL, request->name, request, request->type);
540 adx 30 break;
541     #ifdef IPV6
542     case T_AAAA:
543     /* didnt work, try A */
544     if (request->state == REQ_AAAA)
545 michael 992 do_query_name(NULL, NULL, request->name, request, T_A);
546 adx 30 #endif
547     default:
548     break;
549     }
550     }
551    
552     /*
553     * proc_answer - process name server reply
554     */
555     static int
556     proc_answer(struct reslist *request, HEADER* header, char* buf, char* eob)
557     {
558     char hostbuf[HOSTLEN + 100]; /* working buffer */
559     unsigned char *current; /* current position in buf */
560     int query_class; /* answer class */
561     int type; /* answer type */
562     int n; /* temp count */
563     int rd_length;
564     struct sockaddr_in *v4; /* conversion */
565     #ifdef IPV6
566     struct sockaddr_in6 *v6;
567     #endif
568     current = (unsigned char *)buf + sizeof(HEADER);
569    
570     for (; header->qdcount > 0; --header->qdcount)
571     {
572     if ((n = irc_dn_skipname(current, (unsigned char *)eob)) < 0)
573     break;
574    
575     current += (size_t) n + QFIXEDSZ;
576     }
577    
578     /*
579     * process each answer sent to us blech.
580     */
581     while (header->ancount > 0 && (char *)current < eob)
582     {
583     header->ancount--;
584    
585     n = irc_dn_expand((unsigned char *)buf, (unsigned char *)eob, current,
586     hostbuf, sizeof(hostbuf));
587    
588     if (n < 0)
589     {
590     /*
591     * broken message
592     */
593     return(0);
594     }
595     else if (n == 0)
596     {
597     /*
598     * no more answers left
599     */
600     return(0);
601     }
602    
603     hostbuf[HOSTLEN] = '\0';
604    
605     /* With Address arithmetic you have to be very anal
606     * this code was not working on alpha due to that
607     * (spotted by rodder/jailbird/dianora)
608     */
609     current += (size_t) n;
610    
611     if (!(((char *)current + ANSWER_FIXED_SIZE) < eob))
612     break;
613    
614     type = irc_ns_get16(current);
615     current += TYPE_SIZE;
616    
617     query_class = irc_ns_get16(current);
618     current += CLASS_SIZE;
619    
620     request->ttl = irc_ns_get32(current);
621     current += TTL_SIZE;
622    
623     rd_length = irc_ns_get16(current);
624     current += RDLENGTH_SIZE;
625    
626     /*
627     * Wait to set request->type until we verify this structure
628     */
629     switch (type)
630     {
631     case T_A:
632     if (request->type != T_A)
633     return(0);
634    
635     /*
636     * check for invalid rd_length or too many addresses
637     */
638     if (rd_length != sizeof(struct in_addr))
639     return(0);
640     v4 = (struct sockaddr_in *)&request->addr;
641     request->addr.ss_len = sizeof(struct sockaddr_in);
642     v4->sin_family = AF_INET;
643     memcpy(&v4->sin_addr, current, sizeof(struct in_addr));
644     return(1);
645     break;
646     #ifdef IPV6
647     case T_AAAA:
648     if (request->type != T_AAAA)
649     return(0);
650     if (rd_length != sizeof(struct in6_addr))
651     return(0);
652     request->addr.ss_len = sizeof(struct sockaddr_in6);
653     v6 = (struct sockaddr_in6 *)&request->addr;
654     v6->sin6_family = AF_INET6;
655     memcpy(&v6->sin6_addr, current, sizeof(struct in6_addr));
656     return(1);
657     break;
658     #endif
659     case T_PTR:
660     if (request->type != T_PTR)
661     return(0);
662     n = irc_dn_expand((unsigned char *)buf, (unsigned char *)eob,
663     current, hostbuf, sizeof(hostbuf));
664     if (n < 0)
665     return(0); /* broken message */
666     else if (n == 0)
667     return(0); /* no more answers left */
668    
669 michael 986 strlcpy(request->name, hostbuf, HOSTLEN + 1);
670 adx 30
671     return(1);
672     break;
673     case T_CNAME: /* first check we already havent started looking
674     into a cname */
675     if (request->type != T_PTR)
676     return(0);
677    
678     if (request->state == REQ_CNAME)
679     {
680     n = irc_dn_expand((unsigned char *)buf, (unsigned char *)eob,
681     current, hostbuf, sizeof(hostbuf));
682    
683     if (n < 0)
684     return(0);
685     return(1);
686     }
687    
688     request->state = REQ_CNAME;
689     current += rd_length;
690     break;
691    
692     default:
693     /* XXX I'd rather just throw away the entire bogus thing
694     * but its possible its just a broken nameserver with still
695     * valid answers. But lets do some rudimentary logging for now...
696     */
697     ilog(L_ERROR, "irc_res.c bogus type %d", type);
698     break;
699     }
700     }
701    
702     return(1);
703     }
704    
705     /*
706     * res_readreply - read a dns reply from the nameserver and process it.
707     */
708     static void
709     res_readreply(fde_t *fd, void *data)
710     {
711 adx 411 char buf[sizeof(HEADER) + MAXPACKET]
712     /* Sparc and alpha need 16bit-alignment for accessing header->id
713     * (which is uint16_t). Because of the header = (HEADER*) buf;
714     * lateron, this is neeeded. --FaUl
715     */
716     #if defined(__sparc__) || defined(__alpha__)
717     __attribute__((aligned (16)))
718     #endif
719     ;
720 adx 30 HEADER *header;
721     struct reslist *request = NULL;
722     int rc;
723     socklen_t len = sizeof(struct irc_ssaddr);
724     struct irc_ssaddr lsin;
725    
726     rc = recvfrom(fd->fd, buf, sizeof(buf), 0, (struct sockaddr *)&lsin, &len);
727    
728     /* Re-schedule a read *after* recvfrom, or we'll be registering
729     * interest where it'll instantly be ready for read :-) -- adrian
730     */
731     comm_setselect(fd, COMM_SELECT_READ, res_readreply, NULL, 0);
732 michael 984
733 adx 30 /* Better to cast the sizeof instead of rc */
734     if (rc <= (int)(sizeof(HEADER)))
735     return;
736    
737     /*
738     * convert DNS reply reader from Network byte order to CPU byte order.
739     */
740     header = (HEADER *)buf;
741     header->ancount = ntohs(header->ancount);
742     header->qdcount = ntohs(header->qdcount);
743     header->nscount = ntohs(header->nscount);
744     header->arcount = ntohs(header->arcount);
745    
746     /*
747 michael 994 * check against possibly fake replies
748 adx 30 */
749 michael 994 if (!res_ourserver(&lsin))
750 adx 30 return;
751    
752     /*
753 michael 994 * response for an id which we have already received an answer for
754     * just ignore this response.
755 adx 30 */
756 michael 994 if (!(request = find_id(header->id)))
757 adx 30 return;
758    
759     if ((header->rcode != NO_ERRORS) || (header->ancount == 0))
760     {
761 michael 984 if (header->rcode == SERVFAIL || header->rcode == NXDOMAIN)
762 db 155 {
763 michael 984 /*
764     * If a bad error was returned, stop here and don't
765     * send any more (no retries granted).
766     */
767 michael 992 (*request->callback)(request->callback_ctx, NULL, NULL);
768 michael 984 rem_request(request);
769     }
770     #ifdef IPV6
771     else
772     {
773 adx 30 /*
774     * If we havent already tried this, and we're looking up AAAA, try A
775     * now
776     */
777     if (request->state == REQ_AAAA && request->type == T_AAAA)
778     {
779     request->timeout += 4;
780     resend_query(request);
781     }
782 michael 984 }
783 adx 30 #endif
784 michael 984
785 db 468 return;
786 adx 30 }
787 michael 984
788 adx 30 /*
789     * If this fails there was an error decoding the received packet,
790     * try it again and hope it works the next time.
791     */
792 michael 994 if (proc_answer(request, header, buf, buf + rc))
793 adx 30 {
794     if (request->type == T_PTR)
795     {
796     if (request->name == NULL)
797     {
798     /*
799     * got a PTR response with no name, something bogus is happening
800     * don't bother trying again, the client address doesn't resolve
801     */
802 michael 992 (*request->callback)(request->callback_ctx, NULL, NULL);
803 adx 30 rem_request(request);
804     return;
805     }
806    
807     /*
808     * Lookup the 'authoritative' name that we were given for the
809     * ip#.
810     *
811     */
812     #ifdef IPV6
813     if (request->addr.ss.ss_family == AF_INET6)
814 michael 992 gethost_byname_type(request->callback, request->callback_ctx, request->name, T_AAAA);
815 adx 30 else
816     #endif
817 michael 992 gethost_byname_type(request->callback, request->callback_ctx, request->name, T_A);
818 adx 30 rem_request(request);
819     }
820     else
821     {
822     /*
823     * got a name and address response, client resolved
824     */
825 michael 992 (*request->callback)(request->callback_ctx, &request->addr, request->name);
826 adx 30 rem_request(request);
827     }
828     }
829     else if (!request->sent)
830     {
831     /* XXX - we got a response for a query we didn't send with a valid id?
832     * this should never happen, bail here and leave the client unresolved
833     */
834     assert(0);
835    
836     /* XXX don't leak it */
837     rem_request(request);
838     }
839     }
840    
841     void
842     report_dns_servers(struct Client *source_p)
843     {
844     int i;
845 michael 992 char ipaddr[HOSTIPLEN + 1];
846 adx 30
847     for (i = 0; i < irc_nscount; i++)
848     {
849     irc_getnameinfo((struct sockaddr *)&(irc_nsaddr_list[i]),
850 michael 992 irc_nsaddr_list[i].ss_len, ipaddr,
851     sizeof(ipaddr), NULL, 0, NI_NUMERICHOST);
852 adx 30 sendto_one(source_p, form_str(RPL_STATSALINE),
853     me.name, source_p->name, ipaddr);
854     }
855     }

Properties

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