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.
7 * The authors takes no responsibility for any damage or loss
8 * of property which results from the use of this software.
10 * July 1999 - Rewrote a bunch of stuff here. Change hostent builder code,
11 * added callbacks and reference counting of returned hostents.
12 * --Bleep (Thomas Helvey <tomh@inxpress.net>)
14 * This was all needlessly complicated for irc. Simplified. No more hostent
15 * All we really care about is the IP -> hostname mappings. Thats all.
17 * Apr 28, 2003 --cryogen and Dianora
20 * @brief IRC resolver functions.
25 #include "ircd_alloc.h"
27 #include "ircd_osdep.h"
28 #include "ircd_reply.h"
29 #include "ircd_string.h"
30 #include "ircd_snprintf.h"
33 #include "fileio.h" /* for fbopen / fbclose / fbputs */
41 #include "ircd_reslib.h"
43 /* #include <assert.h> -- Now using assert in ircd_log.h */
46 #include <sys/socket.h>
50 #error this code needs to be able to address individual octets
53 /** Resolver UDP socket. */
54 static struct Socket res_socket;
55 /** Next DNS lookup timeout. */
56 static struct Timer res_timeout;
57 /** Check for whether the resolver has been initialized yet. */
58 #define resolver_started() (request_list.next != NULL)
60 /** Maximum DNS packet length.
61 * RFC says 512, but we add extra for expanded names.
63 #define MAXPACKET 1024
64 #define AR_TTL 600 /**< TTL in seconds for dns cache entries */
66 /* RFC 1104/1105 wasn't very helpful about what these fields
67 * should be named, so for now, we'll just name them this way.
68 * we probably should look at what named calls them or something.
70 /** Size of TYPE field of a DNS RR header. */
71 #define TYPE_SIZE (size_t)2
72 /** Size of CLASS field of a DNS RR header. */
73 #define CLASS_SIZE (size_t)2
74 /** Size of TTL field of a DNS RR header. */
75 #define TTL_SIZE (size_t)4
76 /** Size of RDLENGTH field of a DNS RR header. */
77 #define RDLENGTH_SIZE (size_t)2
78 /** Size of fixed-format part of a DNS RR header. */
79 #define ANSWER_FIXED_SIZE (TYPE_SIZE + CLASS_SIZE + TTL_SIZE + RDLENGTH_SIZE)
81 /** Current request state. */
84 REQ_IDLE, /**< We're doing not much at all. */
85 REQ_PTR, /**< Looking up a PTR. */
86 REQ_A, /**< Looking up an A, possibly because AAAA failed. */
87 REQ_AAAA, /**< Looking up an AAAA. */
88 REQ_CNAME, /**< We got a CNAME in response, we better get a real answer next. */
89 REQ_INT /**< ip6.arpa failed, falling back to ip6.int. */
92 /** Doubly linked list node. */
95 struct dlink *prev; /**< Previous element in list. */
96 struct dlink *next; /**< Next element in list. */
99 /** A single resolver request.
100 * (Do not be fooled by the "list" in the name.)
104 struct dlink node; /**< Doubly linked list node. */
105 int id; /**< Request ID (from request header). */
106 int sent; /**< Number of requests sent. */
107 request_state state; /**< State the resolver machine is in. */
108 char type; /**< Current request type. */
109 char retries; /**< Retry counter. */
110 char sends; /**< Number of sends (>1 means resent). */
111 char resend; /**< Send flag; 0 == dont resend. */
112 time_t sentat; /**< Timestamp we last sent this request. */
113 time_t timeout; /**< When this request times out. */
114 struct irc_in_addr addr; /**< Address for this request. */
115 char *name; /**< Hostname for this request. */
116 struct DNSQuery query; /**< Query callback for this request. */
119 /** Base of request list. */
120 static struct dlink request_list;
122 static void rem_request(struct reslist *request);
123 static struct reslist *make_request(const struct DNSQuery *query);
124 static void do_query_name(const struct DNSQuery *query,
125 const char* name, struct reslist *request, int);
126 static void do_query_number(const struct DNSQuery *query,
127 const struct irc_in_addr *,
128 struct reslist *request);
129 static void query_name(const char *name, int query_class, int query_type,
130 struct reslist *request);
131 static int send_res_msg(const char *buf, int len, int count);
132 static void resend_query(struct reslist *request);
133 static int proc_answer(struct reslist *request, HEADER *header, char *, char *);
134 static struct reslist *find_id(int id);
135 static struct DNSReply *make_dnsreply(struct reslist *request);
136 static void res_readreply(struct Event *ev);
137 static void timeout_resolver(struct Event *notused);
139 extern struct irc_sockaddr irc_nsaddr_list[IRCD_MAXNS];
140 extern int irc_nscount;
141 extern char irc_domain[HOSTLEN];
143 struct irc_sockaddr ResolverAddr;
145 /** Check whether \a inp is a nameserver we use.
146 * @param[in] inp Nameserver address.
147 * @return Non-zero if we trust \a inp; zero if not.
150 res_ourserver(const struct irc_sockaddr *inp)
154 for (ns = 0; ns < irc_nscount; ns++)
155 if (!irc_in_addr_cmp(&inp->addr, &irc_nsaddr_list[ns].addr)
156 && inp->port == irc_nsaddr_list[ns].port)
162 /** Start (or re-start) resolver.
163 * This means read resolv.conf, initialize the list of pending
164 * requests, open the resolver socket and initialize its timeout.
167 restart_resolver(void)
171 if (!request_list.next)
172 request_list.next = request_list.prev = &request_list;
174 if (!s_active(&res_socket))
176 struct irc_sockaddr *local;
178 local = irc_in_addr_valid(&ResolverAddr.addr) ? &ResolverAddr : &VirtualHost;
179 fd = os_socket(local, SOCK_DGRAM, "Resolver UDP socket");
181 if (!socket_add(&res_socket, res_readreply, NULL, SS_DATAGRAM,
182 SOCK_EVENT_READABLE, fd)) return;
183 timer_init(&res_timeout);
187 /** Append local domain to hostname if needed.
188 * If \a hname does not contain any '.'s, append #irc_domain to it.
189 * @param[in,out] hname Hostname to check.
190 * @param[in] size Length of \a hname buffer.
193 add_local_domain(char* hname, size_t size)
195 /* try to fix up unqualified names
197 if (strchr(hname, '.') == NULL)
201 size_t len = strlen(hname);
203 if ((strlen(irc_domain) + len + 2) < size)
206 strcpy(hname + len, irc_domain);
212 /** Add a node to a doubly linked list.
213 * @param[in,out] node Node to add to list.
214 * @param[in,out] next Add \a node before this one.
217 add_dlink(struct dlink *node, struct dlink *next)
219 node->prev = next->prev;
221 node->prev->next = node;
222 node->next->prev = node;
225 /** Remove a request from the list and free it.
226 * @param[in] request Node to free.
229 rem_request(struct reslist *request)
231 /* remove from dlist */
232 request->node.prev->next = request->node.next;
233 request->node.next->prev = request->node.prev;
235 MyFree(request->name);
239 /** Create a DNS request record for the server.
240 * @param[in] query Callback information for caller.
241 * @return Newly allocated and linked-in reslist.
243 static struct reslist *
244 make_request(const struct DNSQuery* query)
246 struct reslist *request;
248 if (!resolver_started())
251 request = (struct reslist *)MyMalloc(sizeof(struct reslist));
252 memset(request, 0, sizeof(struct reslist));
254 request->state = REQ_IDLE;
255 request->sentat = CurrentTime;
256 request->retries = feature_int(FEAT_IRCD_RES_RETRIES);
258 request->timeout = feature_int(FEAT_IRCD_RES_TIMEOUT);
259 memset(&request->addr, 0, sizeof(request->addr));
260 memcpy(&request->query, query, sizeof(request->query));
262 add_dlink(&request->node, &request_list);
266 /** Make sure that a timeout event will happen by the given time.
267 * @param[in] when Latest time for timeout to run.
270 check_resolver_timeout(time_t when)
272 if (when > CurrentTime + AR_TTL)
273 when = CurrentTime + AR_TTL;
274 if (!t_active(&res_timeout))
275 timer_add(&res_timeout, timeout_resolver, NULL, TT_ABSOLUTE, when);
276 else if (when < t_expire(&res_timeout))
277 timer_chg(&res_timeout, TT_ABSOLUTE, when);
280 /** Drop pending DNS lookups which have timed out.
281 * @param[in] notused Timer event data (ignored).
284 timeout_resolver(struct Event *notused)
286 struct dlink *ptr, *next_ptr;
287 struct reslist *request;
288 time_t next_time = 0;
291 for (ptr = request_list.next; ptr != &request_list; ptr = next_ptr)
293 next_ptr = ptr->next;
294 request = (struct reslist*)ptr;
295 timeout = request->sentat + request->timeout;
297 if (CurrentTime >= timeout)
299 if (--request->retries <= 0)
301 Debug((DEBUG_DNS, "Request %p out of retries; destroying", request));
302 (*request->query.callback)(request->query.vptr, 0);
303 rem_request(request);
308 request->sentat = CurrentTime;
309 request->timeout += request->timeout;
310 resend_query(request);
314 if ((next_time == 0) || timeout < next_time)
320 if (next_time <= CurrentTime)
321 next_time = CurrentTime + AR_TTL;
322 check_resolver_timeout(next_time);
325 /** Drop queries that are associated with a particular pointer.
326 * This is used to clean up lookups for clients or conf blocks
328 * @param[in] vptr User callback pointer to search for.
331 delete_resolver_queries(const void *vptr)
333 struct dlink *ptr, *next_ptr;
334 struct reslist *request;
336 if (request_list.next) {
337 for (ptr = request_list.next; ptr != &request_list; ptr = next_ptr)
339 next_ptr = ptr->next;
340 request = (struct reslist*)ptr;
341 if (vptr == request->query.vptr) {
342 Debug((DEBUG_DNS, "Removing request %p with vptr %p", request, vptr));
343 rem_request(request);
349 /** Send a message to all of our nameservers.
350 * @param[in] msg Message to send.
351 * @param[in] len Length of message.
352 * @param[in] rcount Maximum number of servers to ask.
353 * @return Number of servers that were successfully asked.
356 send_res_msg(const char *msg, int len, int rcount)
360 int max_queries = IRCD_MIN(irc_nscount, rcount);
362 /* RES_PRIMARY option is not implemented
363 * if (res.options & RES_PRIMARY || 0 == max_queries)
365 if (max_queries == 0)
368 for (i = 0; i < max_queries; i++)
369 if (os_sendto_nonb(s_fd(&res_socket), msg, len, NULL, 0, &irc_nsaddr_list[i]) == IO_SUCCESS)
375 /** Find a DNS request by ID.
376 * @param[in] id Identifier to find.
377 * @return Matching DNS request, or NULL if none are found.
379 static struct reslist *
383 struct reslist *request;
385 for (ptr = request_list.next; ptr != &request_list; ptr = ptr->next)
387 request = (struct reslist*)ptr;
389 if (request->id == id) {
390 Debug((DEBUG_DNS, "find_id(%d) -> %p", id, request));
395 Debug((DEBUG_DNS, "find_id(%d) -> NULL", id));
399 /** Try to look up address for a hostname, trying IPv6 (T_AAAA) first.
400 * @param[in] name Hostname to look up.
401 * @param[in] query Callback information.
404 gethost_byname(const char *name, const struct DNSQuery *query)
406 do_query_name(query, name, NULL, T_AAAA);
409 /** Try to look up hostname for an address.
410 * @param[in] addr Address to look up.
411 * @param[in] query Callback information.
414 gethost_byaddr(const struct irc_in_addr *addr, const struct DNSQuery *query)
416 do_query_number(query, addr, NULL);
419 /** Send a query to look up the address for a name.
420 * @param[in] query Callback information.
421 * @param[in] name Hostname to look up.
422 * @param[in] request DNS lookup structure (may be NULL).
423 * @param[in] type Preferred request type.
426 do_query_name(const struct DNSQuery *query, const char *name,
427 struct reslist *request, int type)
429 char host_name[HOSTLEN + 1];
431 ircd_strncpy(host_name, name, HOSTLEN);
432 add_local_domain(host_name, HOSTLEN);
436 request = make_request(query);
437 DupString(request->name, host_name);
440 request->state = REQ_AAAA;
443 request->state = REQ_A;
446 request->type = type;
447 Debug((DEBUG_DNS, "Requesting DNS %s %s as %p", (request->state == REQ_AAAA ? "AAAA" : "A"), host_name, request));
448 query_name(host_name, C_IN, type, request);
451 /** Send a query to look up the name for an address.
452 * @param[in] query Callback information.
453 * @param[in] addr Address to look up.
454 * @param[in] request DNS lookup structure (may be NULL).
457 do_query_number(const struct DNSQuery *query, const struct irc_in_addr *addr,
458 struct reslist *request)
461 const unsigned char *cp;
463 if (irc_in_addr_is_ipv4(addr))
465 cp = (const unsigned char*)&addr->in6_16[6];
466 ircd_snprintf(NULL, ipbuf, sizeof(ipbuf), "%u.%u.%u.%u.in-addr.arpa.",
467 (unsigned int)(cp[3]), (unsigned int)(cp[2]),
468 (unsigned int)(cp[1]), (unsigned int)(cp[0]));
474 if (request != NULL && request->state == REQ_INT)
479 cp = (const unsigned char *)&addr->in6_16[0];
480 ircd_snprintf(NULL, ipbuf, sizeof(ipbuf),
481 "%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x."
482 "%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.ip6.%s.",
483 (unsigned int)(cp[15]&0xf), (unsigned int)(cp[15]>>4),
484 (unsigned int)(cp[14]&0xf), (unsigned int)(cp[14]>>4),
485 (unsigned int)(cp[13]&0xf), (unsigned int)(cp[13]>>4),
486 (unsigned int)(cp[12]&0xf), (unsigned int)(cp[12]>>4),
487 (unsigned int)(cp[11]&0xf), (unsigned int)(cp[11]>>4),
488 (unsigned int)(cp[10]&0xf), (unsigned int)(cp[10]>>4),
489 (unsigned int)(cp[9]&0xf), (unsigned int)(cp[9]>>4),
490 (unsigned int)(cp[8]&0xf), (unsigned int)(cp[8]>>4),
491 (unsigned int)(cp[7]&0xf), (unsigned int)(cp[7]>>4),
492 (unsigned int)(cp[6]&0xf), (unsigned int)(cp[6]>>4),
493 (unsigned int)(cp[5]&0xf), (unsigned int)(cp[5]>>4),
494 (unsigned int)(cp[4]&0xf), (unsigned int)(cp[4]>>4),
495 (unsigned int)(cp[3]&0xf), (unsigned int)(cp[3]>>4),
496 (unsigned int)(cp[2]&0xf), (unsigned int)(cp[2]>>4),
497 (unsigned int)(cp[1]&0xf), (unsigned int)(cp[1]>>4),
498 (unsigned int)(cp[0]&0xf), (unsigned int)(cp[0]>>4), intarpa);
502 request = make_request(query);
503 request->type = T_PTR;
504 memcpy(&request->addr, addr, sizeof(request->addr));
505 request->name = (char *)MyMalloc(HOSTLEN + 1);
507 Debug((DEBUG_DNS, "Requesting DNS PTR %s as %p", ipbuf, request));
508 query_name(ipbuf, C_IN, T_PTR, request);
511 /** Generate a query based on class, type and name.
512 * @param[in] name Domain name to look up.
513 * @param[in] query_class Query class (see RFC 1035).
514 * @param[in] type Query type (see RFC 1035).
515 * @param[in] request DNS request structure.
518 query_name(const char *name, int query_class, int type,
519 struct reslist *request)
524 memset(buf, 0, sizeof(buf));
526 if ((request_len = irc_res_mkquery(name, query_class, type,
527 (unsigned char *)buf, sizeof(buf))) > 0)
529 HEADER *header = (HEADER *)buf;
532 * generate an unique id
533 * NOTE: we don't have to worry about converting this to and from
534 * network byte order, the nameserver does not interpret this value
535 * and returns it unchanged
539 header->id = (header->id + ircrandom()) & 0xffff;
540 } while (find_id(header->id));
541 request->id = header->id;
544 request->sent += send_res_msg(buf, request_len, request->sends);
545 check_resolver_timeout(request->sentat + request->timeout);
549 /** Send a failed DNS lookup request again.
550 * @param[in] request Request to resend.
553 resend_query(struct reslist *request)
555 if (request->resend == 0)
558 switch(request->type)
561 do_query_number(NULL, &request->addr, request);
564 do_query_name(NULL, request->name, request, request->type);
567 /* didnt work, try A */
568 if (request->state == REQ_AAAA)
569 do_query_name(NULL, request->name, request, T_A);
575 /** Process the answer for a lookup request.
576 * @param[in] request DNS request that got an answer.
577 * @param[in] header Header of DNS response.
578 * @param[in] buf DNS response body.
579 * @param[in] eob Pointer to end of DNS response.
580 * @return Number of answers read from \a buf.
583 proc_answer(struct reslist *request, HEADER* header, char* buf, char* eob)
585 char hostbuf[HOSTLEN + 100]; /* working buffer */
586 unsigned char *current; /* current position in buf */
587 int query_class; /* answer class */
588 int type; /* answer type */
589 int n; /* temp count */
592 current = (unsigned char *)buf + sizeof(HEADER);
594 for (; header->qdcount > 0; --header->qdcount)
596 if ((n = irc_dn_skipname(current, (unsigned char *)eob)) < 0)
599 current += (size_t) n + QFIXEDSZ;
603 * process each answer sent to us blech.
605 while (header->ancount > 0 && (char *)current < eob)
609 n = irc_dn_expand((unsigned char *)buf, (unsigned char *)eob, current,
610 hostbuf, sizeof(hostbuf));
622 * no more answers left
627 hostbuf[HOSTLEN] = '\0';
629 /* With Address arithmetic you have to be very anal
630 * this code was not working on alpha due to that
631 * (spotted by rodder/jailbird/dianora)
633 current += (size_t) n;
635 if (!(((char *)current + ANSWER_FIXED_SIZE) < eob))
638 type = irc_ns_get16(current);
639 current += TYPE_SIZE;
641 query_class = irc_ns_get16(current);
642 current += CLASS_SIZE;
646 rd_length = irc_ns_get16(current);
647 current += RDLENGTH_SIZE;
650 * Wait to set request->type until we verify this structure
655 if (request->type != T_A)
659 * check for invalid rd_length or too many addresses
661 if (rd_length != sizeof(struct in_addr))
663 memset(&request->addr, 0, sizeof(request->addr));
664 memcpy(&request->addr.in6_16[6], current, sizeof(struct in_addr));
668 if (request->type != T_AAAA)
670 if (rd_length != sizeof(struct irc_in_addr))
672 memcpy(&request->addr, current, sizeof(struct irc_in_addr));
676 if (request->type != T_PTR)
678 n = irc_dn_expand((unsigned char *)buf, (unsigned char *)eob,
679 current, hostbuf, sizeof(hostbuf));
681 return(0); /* broken message */
683 return(0); /* no more answers left */
685 ircd_strncpy(request->name, hostbuf, HOSTLEN);
689 case T_CNAME: /* first check we already havent started looking
691 if (request->type != T_PTR)
694 if (request->state == REQ_CNAME)
696 n = irc_dn_expand((unsigned char *)buf, (unsigned char *)eob,
697 current, hostbuf, sizeof(hostbuf));
704 request->state = REQ_CNAME;
705 current += rd_length;
709 /* XXX I'd rather just throw away the entire bogus thing
710 * but its possible its just a broken nameserver with still
711 * valid answers. But lets do some rudimentary logging for now...
713 log_write(LS_RESOLVER, L_ERROR, 0, "irc_res.c bogus type %d", type);
721 /** Read a DNS reply from the nameserver and process it.
722 * @param[in] ev I/O activity event for resolver socket.
725 res_readreply(struct Event *ev)
727 struct irc_sockaddr lsin;
729 char buf[sizeof(HEADER) + MAXPACKET];
731 struct reslist *request = NULL;
732 struct DNSReply *reply = NULL;
736 assert(ev_socket(ev) == &res_socket);
737 sock = ev_socket(ev);
739 if (IO_SUCCESS != os_recvfrom_nonb(s_fd(sock), buf, sizeof(buf), &rc, &lsin)
740 || (rc <= sizeof(HEADER)))
744 * convert DNS reply reader from Network byte order to CPU byte order.
746 header = (HEADER *)buf;
747 header->ancount = ntohs(header->ancount);
748 header->qdcount = ntohs(header->qdcount);
749 header->nscount = ntohs(header->nscount);
750 header->arcount = ntohs(header->arcount);
753 * response for an id which we have already received an answer for
754 * just ignore this response.
756 if (0 == (request = find_id(header->id)))
760 * check against possibly fake replies
762 if (!res_ourserver(&lsin))
765 if ((header->rcode != NO_ERRORS) || (header->ancount == 0))
767 if (SERVFAIL == header->rcode)
768 resend_query(request);
772 * If we havent already tried this, and we're looking up AAAA, try A
776 if (request->state == REQ_AAAA && request->type == T_AAAA)
778 request->timeout += feature_int(FEAT_IRCD_RES_TIMEOUT);
779 resend_query(request);
781 else if (request->type == T_PTR && request->state != REQ_INT &&
782 !irc_in_addr_is_ipv4(&request->addr))
784 request->state = REQ_INT;
785 request->timeout += feature_int(FEAT_IRCD_RES_TIMEOUT);
786 resend_query(request);
791 * If a bad error was returned, we stop here and dont send
792 * send any more (no retries granted).
794 Debug((DEBUG_DNS, "Request %p has bad response (state %d type %d)", request, request->state, request->type));
795 (*request->query.callback)(request->query.vptr, 0);
796 rem_request(request);
803 * If this fails there was an error decoding the received packet,
804 * try it again and hope it works the next time.
806 answer_count = proc_answer(request, header, buf, buf + rc);
810 if (request->type == T_PTR)
812 if (request->name == NULL)
815 * got a PTR response with no name, something bogus is happening
816 * don't bother trying again, the client address doesn't resolve
818 Debug((DEBUG_DNS, "Request %p PTR had empty name", request));
819 (*request->query.callback)(request->query.vptr, reply);
820 rem_request(request);
825 * Lookup the 'authoritative' name that we were given for the
829 if (!irc_in_addr_is_ipv4(&request->addr))
830 do_query_name(&request->query, request->name, NULL, T_AAAA);
833 do_query_name(&request->query, request->name, NULL, T_A);
834 Debug((DEBUG_DNS, "Request %p switching to forward resolution", request));
835 rem_request(request);
840 * got a name and address response, client resolved
842 reply = make_dnsreply(request);
843 (*request->query.callback)(request->query.vptr, (reply) ? reply : 0);
844 Debug((DEBUG_DNS, "Request %p got forward resolution", request));
845 rem_request(request);
848 else if (!request->sent)
850 /* XXX - we got a response for a query we didn't send with a valid id?
851 * this should never happen, bail here and leave the client unresolved
855 /* XXX don't leak it */
856 Debug((DEBUG_DNS, "Request %p was unexpected(!)", request));
857 rem_request(request);
861 /** Build a DNSReply for a completed request.
862 * @param[in] request Completed DNS request.
863 * @return Newly allocated DNSReply containing host name and address.
865 static struct DNSReply *
866 make_dnsreply(struct reslist *request)
869 assert(request != 0);
871 cp = (struct DNSReply *)MyMalloc(sizeof(struct DNSReply));
873 DupString(cp->h_name, request->name);
874 memcpy(&cp->addr, &request->addr, sizeof(cp->addr));
878 /** Statistics callback to list DNS servers.
879 * @param[in] source_p Client requesting statistics.
880 * @param[in] sd Stats descriptor for request (ignored).
881 * @param[in] param Extra parameter from user (ignored).
884 report_dns_servers(struct Client *source_p, const struct StatDesc *sd, char *param)
889 for (i = 0; i < irc_nscount; i++)
891 ircd_ntoa_r(ipaddr, &irc_nsaddr_list[i].addr);
892 send_reply(source_p, RPL_STATSALINE, ipaddr);
896 /** Report memory usage to a client.
897 * @param[in] sptr Client requesting information.
898 * @return Total memory used by pending requests.
901 cres_mem(struct Client* sptr)
904 struct reslist *request;
905 size_t request_mem = 0;
906 int request_count = 0;
908 if (request_list.next) {
909 for (dlink = request_list.next; dlink != &request_list; dlink = dlink->next) {
910 request = (struct reslist*)dlink;
911 request_mem += sizeof(*request);
913 request_mem += strlen(request->name) + 1;
918 send_reply(sptr, SND_EXPLICIT | RPL_STATSDEBUG,
919 ":Resolver: requests %d(%d)", request_count, request_mem);
923 /** Check whether an address looks valid.
924 * This means not all 0s and not all 1s.
925 * @param[in] addr Address to check for validity.
926 * @return Non-zero if the address looks valid.
928 int irc_in_addr_valid(const struct irc_in_addr *addr)
933 val = addr->in6_16[0];
934 if (val != 0 && val != 0xffff)
936 for (ii = 1; ii < 8; ii++)
937 if (addr->in6_16[ii] != val)
942 /** Compare two IP addresses.
943 * @param[in] a First address to compare.
944 * @param[in] b Second address to compare.
945 * @return Non-zero if the two addresses differ, zero if they are identical.
947 int irc_in_addr_cmp(const struct irc_in_addr *a, const struct irc_in_addr *b)
949 if (irc_in_addr_is_ipv4(a))
950 return a->in6_16[6] != b->in6_16[6]
951 || a->in6_16[7] != b->in6_16[7]
952 || !irc_in_addr_is_ipv4(b);
954 return memcmp(a, b, sizeof(*a));
957 /** Indicate whether an IP address is a loopback address.
958 * @param[in] addr Address to check.
959 * @return Non-zero if the address is loopback; zero if not.
961 int irc_in_addr_is_loopback(const struct irc_in_addr *addr)
963 if (addr->in6_16[0] != 0
964 || addr->in6_16[1] != 0
965 || addr->in6_16[2] != 0
966 || addr->in6_16[3] != 0
967 || addr->in6_16[4] != 0)
969 if ((addr->in6_16[5] == 0xffff) || (addr->in6_16[5] == 0 && addr->in6_16[6] != 0))
970 return (ntohs(addr->in6_16[6]) & 0xff00) == 0x7f00;
972 return addr->in6_16[5] == 0 && addr->in6_16[6] == 0 && htons(addr->in6_16[7]) == 1;