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 */
40 #include "ircd_reslib.h"
45 #include <sys/socket.h>
49 #error this code needs to be able to address individual octets
52 /** Resolver UDP socket. */
53 static struct Socket res_socket;
54 /** Next DNS lookup timeout. */
55 static struct Timer res_timeout;
57 /** Maximum DNS packet length.
58 * RFC says 512, but we add extra for expanded names.
60 #define MAXPACKET 1024
61 #define AR_TTL 600 /**< TTL in seconds for dns cache entries */
63 /* RFC 1104/1105 wasn't very helpful about what these fields
64 * should be named, so for now, we'll just name them this way.
65 * we probably should look at what named calls them or something.
67 /** Size of TYPE field of a DNS RR header. */
68 #define TYPE_SIZE (size_t)2
69 /** Size of CLASS field of a DNS RR header. */
70 #define CLASS_SIZE (size_t)2
71 /** Size of TTL field of a DNS RR header. */
72 #define TTL_SIZE (size_t)4
73 /** Size of RDLENGTH field of a DNS RR header. */
74 #define RDLENGTH_SIZE (size_t)2
75 /** Size of fixed-format part of a DNS RR header. */
76 #define ANSWER_FIXED_SIZE (TYPE_SIZE + CLASS_SIZE + TTL_SIZE + RDLENGTH_SIZE)
78 /** Current request state. */
81 REQ_IDLE, /**< We're doing not much at all. */
82 REQ_PTR, /**< Looking up a PTR. */
83 REQ_A, /**< Looking up an A, possibly because AAAA failed. */
84 REQ_AAAA, /**< Looking up an AAAA. */
85 REQ_CNAME, /**< We got a CNAME in response, we better get a real answer next. */
86 REQ_INT /**< ip6.arpa failed, falling back to ip6.int. */
89 /** Doubly linked list node. */
92 struct dlink *prev; /**< Previous element in list. */
93 struct dlink *next; /**< Next element in list. */
96 /** A single resolver request.
97 * (Do not be fooled by the "list" in the name.)
101 struct dlink node; /**< Doubly linked list node. */
102 int id; /**< Request ID (from request header). */
103 int sent; /**< Number of requests sent. */
104 request_state state; /**< State the resolver machine is in. */
105 char type; /**< Current request type. */
106 char retries; /**< Retry counter. */
107 char sends; /**< Number of sends (>1 means resent). */
108 char resend; /**< Send flag; 0 == dont resend. */
109 time_t sentat; /**< Timestamp we last sent this request. */
110 time_t timeout; /**< When this request times out. */
111 struct irc_in_addr addr; /**< Address for this request. */
112 char *name; /**< Hostname for this request. */
113 struct DNSQuery query; /**< Query callback for this request. */
116 /** Base of request list. */
117 static struct dlink request_list;
119 static void rem_request(struct reslist *request);
120 static struct reslist *make_request(const struct DNSQuery *query);
121 static void do_query_name(const struct DNSQuery *query,
122 const char* name, struct reslist *request, int);
123 static void do_query_number(const struct DNSQuery *query,
124 const struct irc_in_addr *,
125 struct reslist *request);
126 static void query_name(const char *name, int query_class, int query_type,
127 struct reslist *request);
128 static int send_res_msg(const char *buf, int len, int count);
129 static void resend_query(struct reslist *request);
130 static int proc_answer(struct reslist *request, HEADER *header, char *, char *);
131 static struct reslist *find_id(int id);
132 static struct DNSReply *make_dnsreply(struct reslist *request);
133 static void res_readreply(struct Event *ev);
134 static void timeout_resolver(struct Event *notused);
136 extern struct irc_sockaddr irc_nsaddr_list[IRCD_MAXNS];
137 extern int irc_nscount;
138 extern char irc_domain[HOSTLEN];
140 /** Check whether \a inp is a nameserver we use.
141 * @param[in] inp Nameserver address.
142 * @return Non-zero if we trust \a inp; zero if not.
145 res_ourserver(const struct irc_sockaddr *inp)
149 for (ns = 0; ns < irc_nscount; ns++)
150 if (!irc_in_addr_cmp(&inp->addr, &irc_nsaddr_list[ns].addr)
151 && inp->port == irc_nsaddr_list[ns].port)
157 /** Start (or re-start) resolver.
158 * This means read resolv.conf, initialize the list of pending
159 * requests, open the resolver socket and initialize its timeout.
162 restart_resolver(void)
166 if (!request_list.next)
167 request_list.next = request_list.prev = &request_list;
169 if (!s_active(&res_socket))
172 fd = os_socket(NULL, SOCK_DGRAM, "Resolver UDP socket");
174 if (!socket_add(&res_socket, res_readreply, NULL, SS_DATAGRAM,
175 SOCK_EVENT_READABLE, fd)) return;
176 timer_init(&res_timeout);
180 /** Initialize resolver.
181 * This seends the pseudo-random number generator and calls
182 * restart_resolver().
183 * @return Resolver socket file descriptor.
190 return(s_fd(&res_socket));
193 /** Append local domain to hostname if needed.
194 * If \a hname does not contain any '.'s, append #irc_domain to it.
195 * @param[in,out] hname Hostname to check.
196 * @param[in] size Length of \a hname buffer.
199 add_local_domain(char* hname, size_t size)
201 /* try to fix up unqualified names
203 if (strchr(hname, '.') == NULL)
207 size_t len = strlen(hname);
209 if ((strlen(irc_domain) + len + 2) < size)
212 strcpy(hname + len, irc_domain);
218 /** Add a node to a doubly linked list.
219 * @param[in,out] node Node to add to list.
220 * @param[in,out] next Add \a node before this one.
223 add_dlink(struct dlink *node, struct dlink *next)
225 node->prev = next->prev;
227 node->prev->next = node;
228 node->next->prev = node;
231 /** Remove a request from the list and free it.
232 * @param[in] request Node to free.
235 rem_request(struct reslist *request)
237 /* remove from dlist */
238 request->node.prev->next = request->node.next;
239 request->node.next->prev = request->node.prev;
241 MyFree(request->name);
245 /** Create a DNS request record for the server.
246 * @param[in] query Callback information for caller.
247 * @return Newly allocated and linked-in reslist.
249 static struct reslist *
250 make_request(const struct DNSQuery* query)
252 struct reslist *request;
254 request = (struct reslist *)MyMalloc(sizeof(struct reslist));
255 memset(request, 0, sizeof(struct reslist));
257 request->state = REQ_IDLE;
258 request->sentat = CurrentTime;
259 request->retries = feature_int(FEAT_IRCD_RES_RETRIES);
261 request->timeout = feature_int(FEAT_IRCD_RES_TIMEOUT);
262 memset(&request->addr, 0, sizeof(request->addr));
263 memcpy(&request->query, query, sizeof(request->query));
265 add_dlink(&request->node, &request_list);
269 /** Make sure that a timeout event will happen by the given time.
270 * @param[in] when Latest time for timeout to run.
273 check_resolver_timeout(time_t when)
275 if (when > CurrentTime + AR_TTL)
276 when = CurrentTime + AR_TTL;
277 if (!t_active(&res_timeout))
278 timer_add(&res_timeout, timeout_resolver, NULL, TT_ABSOLUTE, when);
279 else if (when < t_expire(&res_timeout))
280 timer_chg(&res_timeout, TT_ABSOLUTE, when);
283 /** Drop pending DNS lookups which have timed out.
284 * @param[in] notused Timer event data (ignored).
287 timeout_resolver(struct Event *notused)
289 struct dlink *ptr, *next_ptr;
290 struct reslist *request;
291 time_t next_time = 0;
294 for (ptr = request_list.next; ptr != &request_list; ptr = next_ptr)
296 next_ptr = ptr->next;
297 request = (struct reslist*)ptr;
298 timeout = request->sentat + request->timeout;
300 if (CurrentTime >= timeout)
302 if (--request->retries <= 0)
304 Debug((DEBUG_DNS, "Request %p out of retries; destroying", request));
305 (*request->query.callback)(request->query.vptr, 0);
306 rem_request(request);
311 request->sentat = CurrentTime;
312 request->timeout += request->timeout;
313 resend_query(request);
317 if ((next_time == 0) || timeout < next_time)
323 if (next_time <= CurrentTime)
324 next_time = CurrentTime + AR_TTL;
325 check_resolver_timeout(next_time);
328 /** Drop queries that are associated with a particular pointer.
329 * This is used to clean up lookups for clients or conf blocks
331 * @param[in] vptr User callback pointer to search for.
334 delete_resolver_queries(const void *vptr)
336 struct dlink *ptr, *next_ptr;
337 struct reslist *request;
339 for (ptr = request_list.next; ptr != &request_list; ptr = next_ptr)
341 next_ptr = ptr->next;
342 request = (struct reslist*)ptr;
343 if (vptr == request->query.vptr) {
344 Debug((DEBUG_DNS, "Removing request %p with vptr %p", request, vptr));
345 rem_request(request);
350 /** Send a message to all of our nameservers.
351 * @param[in] msg Message to send.
352 * @param[in] len Length of message.
353 * @param[in] rcount Maximum number of servers to ask.
354 * @return Number of servers that were successfully asked.
357 send_res_msg(const char *msg, int len, int rcount)
361 int max_queries = IRCD_MIN(irc_nscount, rcount);
363 /* RES_PRIMARY option is not implemented
364 * if (res.options & RES_PRIMARY || 0 == max_queries)
366 if (max_queries == 0)
369 for (i = 0; i < max_queries; i++)
370 if (os_sendto_nonb(s_fd(&res_socket), msg, len, NULL, 0, &irc_nsaddr_list[i]) == IO_SUCCESS)
376 /** Find a DNS request by ID.
377 * @param[in] id Identifier to find.
378 * @return Matching DNS request, or NULL if none are found.
380 static struct reslist *
384 struct reslist *request;
386 for (ptr = request_list.next; ptr != &request_list; ptr = ptr->next)
388 request = (struct reslist*)ptr;
390 if (request->id == id) {
391 Debug((DEBUG_DNS, "find_id(%d) -> %p", id, request));
396 Debug((DEBUG_DNS, "find_id(%d) -> NULL", id));
400 /** Try to look up address for a hostname, trying IPv6 (T_AAAA) first.
401 * @param[in] name Hostname to look up.
402 * @param[in] query Callback information.
405 gethost_byname(const char *name, const struct DNSQuery *query)
407 do_query_name(query, name, NULL, T_AAAA);
410 /** Try to look up hostname for an address.
411 * @param[in] addr Address to look up.
412 * @param[in] query Callback information.
415 gethost_byaddr(const struct irc_in_addr *addr, const struct DNSQuery *query)
417 do_query_number(query, addr, NULL);
420 /** Send a query to look up the address for a name.
421 * @param[in] query Callback information.
422 * @param[in] name Hostname to look up.
423 * @param[in] request DNS lookup structure (may be NULL).
424 * @param[in] type Preferred request type.
427 do_query_name(const struct DNSQuery *query, const char *name,
428 struct reslist *request, int type)
430 char host_name[HOSTLEN + 1];
432 ircd_strncpy(host_name, name, HOSTLEN);
433 add_local_domain(host_name, HOSTLEN);
437 request = make_request(query);
438 DupString(request->name, host_name);
441 request->state = REQ_AAAA;
444 request->state = REQ_A;
447 request->type = type;
448 Debug((DEBUG_DNS, "Requesting DNS %s %s as %p", (request->state == REQ_AAAA ? "AAAA" : "A"), host_name, request));
449 query_name(host_name, C_IN, type, request);
452 /** Send a query to look up the name for an address.
453 * @param[in] query Callback information.
454 * @param[in] addr Address to look up.
455 * @param[in] request DNS lookup structure (may be NULL).
458 do_query_number(const struct DNSQuery *query, const struct irc_in_addr *addr,
459 struct reslist *request)
462 const unsigned char *cp;
464 if (irc_in_addr_is_ipv4(addr))
466 cp = (const unsigned char*)&addr->in6_16[6];
467 ircd_snprintf(NULL, ipbuf, sizeof(ipbuf), "%u.%u.%u.%u.in-addr.arpa.",
468 (unsigned int)(cp[3]), (unsigned int)(cp[2]),
469 (unsigned int)(cp[1]), (unsigned int)(cp[0]));
475 if (request != NULL && request->state == REQ_INT)
480 cp = (const unsigned char *)&addr->in6_16[0];
481 ircd_snprintf(NULL, ipbuf, sizeof(ipbuf),
482 "%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x."
483 "%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.ip6.%s.",
484 (unsigned int)(cp[15]&0xf), (unsigned int)(cp[15]>>4),
485 (unsigned int)(cp[14]&0xf), (unsigned int)(cp[14]>>4),
486 (unsigned int)(cp[13]&0xf), (unsigned int)(cp[13]>>4),
487 (unsigned int)(cp[12]&0xf), (unsigned int)(cp[12]>>4),
488 (unsigned int)(cp[11]&0xf), (unsigned int)(cp[11]>>4),
489 (unsigned int)(cp[10]&0xf), (unsigned int)(cp[10]>>4),
490 (unsigned int)(cp[9]&0xf), (unsigned int)(cp[9]>>4),
491 (unsigned int)(cp[8]&0xf), (unsigned int)(cp[8]>>4),
492 (unsigned int)(cp[7]&0xf), (unsigned int)(cp[7]>>4),
493 (unsigned int)(cp[6]&0xf), (unsigned int)(cp[6]>>4),
494 (unsigned int)(cp[5]&0xf), (unsigned int)(cp[5]>>4),
495 (unsigned int)(cp[4]&0xf), (unsigned int)(cp[4]>>4),
496 (unsigned int)(cp[3]&0xf), (unsigned int)(cp[3]>>4),
497 (unsigned int)(cp[2]&0xf), (unsigned int)(cp[2]>>4),
498 (unsigned int)(cp[1]&0xf), (unsigned int)(cp[1]>>4),
499 (unsigned int)(cp[0]&0xf), (unsigned int)(cp[0]>>4), intarpa);
503 request = make_request(query);
504 request->type = T_PTR;
505 memcpy(&request->addr, addr, sizeof(request->addr));
506 request->name = (char *)MyMalloc(HOSTLEN + 1);
508 Debug((DEBUG_DNS, "Requesting DNS PTR %s as %p", ipbuf, request));
509 query_name(ipbuf, C_IN, T_PTR, request);
512 /** Generate a query based on class, type and name.
513 * @param[in] name Domain name to look up.
514 * @param[in] query_class Query class (see RFC 1035).
515 * @param[in] type Query type (see RFC 1035).
516 * @param[in] request DNS request structure.
519 query_name(const char *name, int query_class, int type,
520 struct reslist *request)
525 memset(buf, 0, sizeof(buf));
527 if ((request_len = irc_res_mkquery(name, query_class, type,
528 (unsigned char *)buf, sizeof(buf))) > 0)
530 HEADER *header = (HEADER *)buf;
533 * generate an unique id
534 * NOTE: we don't have to worry about converting this to and from
535 * network byte order, the nameserver does not interpret this value
536 * and returns it unchanged
540 header->id = (header->id + rand()) & 0xffff;
541 } while (find_id(header->id));
542 request->id = header->id;
545 request->sent += send_res_msg(buf, request_len, request->sends);
546 check_resolver_timeout(request->sentat + request->timeout);
550 /** Send a failed DNS lookup request again.
551 * @param[in] request Request to resend.
554 resend_query(struct reslist *request)
556 if (request->resend == 0)
559 switch(request->type)
562 do_query_number(NULL, &request->addr, request);
565 do_query_name(NULL, request->name, request, request->type);
568 /* didnt work, try A */
569 if (request->state == REQ_AAAA)
570 do_query_name(NULL, request->name, request, T_A);
576 /** Process the answer for a lookup request.
577 * @param[in] request DNS request that got an answer.
578 * @param[in] header Header of DNS response.
579 * @param[in] buf DNS response body.
580 * @param[in] eob Pointer to end of DNS response.
581 * @return Number of answers read from \a buf.
584 proc_answer(struct reslist *request, HEADER* header, char* buf, char* eob)
586 char hostbuf[HOSTLEN + 100]; /* working buffer */
587 unsigned char *current; /* current position in buf */
588 int query_class; /* answer class */
589 int type; /* answer type */
590 int n; /* temp count */
593 current = (unsigned char *)buf + sizeof(HEADER);
595 for (; header->qdcount > 0; --header->qdcount)
597 if ((n = irc_dn_skipname(current, (unsigned char *)eob)) < 0)
600 current += (size_t) n + QFIXEDSZ;
604 * process each answer sent to us blech.
606 while (header->ancount > 0 && (char *)current < eob)
610 n = irc_dn_expand((unsigned char *)buf, (unsigned char *)eob, current,
611 hostbuf, sizeof(hostbuf));
623 * no more answers left
628 hostbuf[HOSTLEN] = '\0';
630 /* With Address arithmetic you have to be very anal
631 * this code was not working on alpha due to that
632 * (spotted by rodder/jailbird/dianora)
634 current += (size_t) n;
636 if (!(((char *)current + ANSWER_FIXED_SIZE) < eob))
639 type = irc_ns_get16(current);
640 current += TYPE_SIZE;
642 query_class = irc_ns_get16(current);
643 current += CLASS_SIZE;
647 rd_length = irc_ns_get16(current);
648 current += RDLENGTH_SIZE;
651 * Wait to set request->type until we verify this structure
656 if (request->type != T_A)
660 * check for invalid rd_length or too many addresses
662 if (rd_length != sizeof(struct in_addr))
664 memset(&request->addr, 0, sizeof(request->addr));
665 memcpy(&request->addr.in6_16[6], current, sizeof(struct in_addr));
669 if (request->type != T_AAAA)
671 if (rd_length != sizeof(struct irc_in_addr))
673 memcpy(&request->addr, current, sizeof(struct irc_in_addr));
677 if (request->type != T_PTR)
679 n = irc_dn_expand((unsigned char *)buf, (unsigned char *)eob,
680 current, hostbuf, sizeof(hostbuf));
682 return(0); /* broken message */
684 return(0); /* no more answers left */
686 ircd_strncpy(request->name, hostbuf, HOSTLEN);
690 case T_CNAME: /* first check we already havent started looking
692 if (request->type != T_PTR)
695 if (request->state == REQ_CNAME)
697 n = irc_dn_expand((unsigned char *)buf, (unsigned char *)eob,
698 current, hostbuf, sizeof(hostbuf));
705 request->state = REQ_CNAME;
706 current += rd_length;
710 /* XXX I'd rather just throw away the entire bogus thing
711 * but its possible its just a broken nameserver with still
712 * valid answers. But lets do some rudimentary logging for now...
714 log_write(LS_RESOLVER, L_ERROR, 0, "irc_res.c bogus type %d", type);
722 /** Read a DNS reply from the nameserver and process it.
723 * @param[in] ev I/O activity event for resolver socket.
726 res_readreply(struct Event *ev)
728 struct irc_sockaddr lsin;
730 char buf[sizeof(HEADER) + MAXPACKET];
732 struct reslist *request = NULL;
733 struct DNSReply *reply = NULL;
737 assert(ev_socket(ev) == &res_socket);
738 sock = ev_socket(ev);
740 if (IO_SUCCESS != os_recvfrom_nonb(s_fd(sock), buf, sizeof(buf), &rc, &lsin)
741 || (rc <= sizeof(HEADER)))
745 * convert DNS reply reader from Network byte order to CPU byte order.
747 header = (HEADER *)buf;
748 header->ancount = ntohs(header->ancount);
749 header->qdcount = ntohs(header->qdcount);
750 header->nscount = ntohs(header->nscount);
751 header->arcount = ntohs(header->arcount);
754 * response for an id which we have already received an answer for
755 * just ignore this response.
757 if (0 == (request = find_id(header->id)))
761 * check against possibly fake replies
763 if (!res_ourserver(&lsin))
766 if ((header->rcode != NO_ERRORS) || (header->ancount == 0))
768 if (SERVFAIL == header->rcode)
769 resend_query(request);
773 * If we havent already tried this, and we're looking up AAAA, try A
777 if (request->state == REQ_AAAA && request->type == T_AAAA)
779 request->timeout += feature_int(FEAT_IRCD_RES_TIMEOUT);
780 resend_query(request);
782 else if (request->type == T_PTR && request->state != REQ_INT &&
783 !irc_in_addr_is_ipv4(&request->addr))
785 request->state = REQ_INT;
786 request->timeout += feature_int(FEAT_IRCD_RES_TIMEOUT);
787 resend_query(request);
792 * If a bad error was returned, we stop here and dont send
793 * send any more (no retries granted).
795 Debug((DEBUG_DNS, "Request %p has bad response (state %d type %d)", request, request->state, request->type));
796 (*request->query.callback)(request->query.vptr, 0);
797 rem_request(request);
804 * If this fails there was an error decoding the received packet,
805 * try it again and hope it works the next time.
807 answer_count = proc_answer(request, header, buf, buf + rc);
811 if (request->type == T_PTR)
813 if (request->name == NULL)
816 * got a PTR response with no name, something bogus is happening
817 * don't bother trying again, the client address doesn't resolve
819 Debug((DEBUG_DNS, "Request %p PTR had empty name", request));
820 (*request->query.callback)(request->query.vptr, reply);
821 rem_request(request);
826 * Lookup the 'authoritative' name that we were given for the
830 if (!irc_in_addr_is_ipv4(&request->addr))
831 do_query_name(&request->query, request->name, NULL, T_AAAA);
834 do_query_name(&request->query, request->name, NULL, T_A);
835 Debug((DEBUG_DNS, "Request %p switching to forward resolution", request));
836 rem_request(request);
841 * got a name and address response, client resolved
843 reply = make_dnsreply(request);
844 (*request->query.callback)(request->query.vptr, (reply) ? reply : 0);
845 Debug((DEBUG_DNS, "Request %p got forward resolution", request));
846 rem_request(request);
849 else if (!request->sent)
851 /* XXX - we got a response for a query we didn't send with a valid id?
852 * this should never happen, bail here and leave the client unresolved
856 /* XXX don't leak it */
857 Debug((DEBUG_DNS, "Request %p was unexpected(!)", request));
858 rem_request(request);
862 /** Build a DNSReply for a completed request.
863 * @param[in] request Completed DNS request.
864 * @return Newly allocated DNSReply containing host name and address.
866 static struct DNSReply *
867 make_dnsreply(struct reslist *request)
870 assert(request != 0);
872 cp = (struct DNSReply *)MyMalloc(sizeof(struct DNSReply));
874 DupString(cp->h_name, request->name);
875 memcpy(&cp->addr, &request->addr, sizeof(cp->addr));
879 /** Statistics callback to list DNS servers.
880 * @param[in] source_p Client requesting statistics.
881 * @param[in] sd Stats descriptor for request (ignored).
882 * @param[in] param Extra parameter from user (ignored).
885 report_dns_servers(struct Client *source_p, const struct StatDesc *sd, char *param)
890 for (i = 0; i < irc_nscount; i++)
892 ircd_ntoa_r(ipaddr, &irc_nsaddr_list[i].addr);
893 send_reply(source_p, RPL_STATSALINE, ipaddr);
897 /** Report memory usage to a client.
898 * @param[in] sptr Client requesting information.
899 * @return Total memory used by pending requests.
902 cres_mem(struct Client* sptr)
905 struct reslist *request;
906 size_t request_mem = 0;
907 int request_count = 0;
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;
917 send_reply(sptr, SND_EXPLICIT | RPL_STATSDEBUG,
918 ":Resolver: requests %d(%d)", request_count, request_mem);
922 /** Check whether an address looks valid.
923 * This means not all 0s and not all 1s.
924 * @param[in] addr Address to check for validity.
925 * @return Non-zero if the address looks valid.
927 int irc_in_addr_valid(const struct irc_in_addr *addr)
932 val = addr->in6_16[0];
933 if (val != 0 && val != 0xffff)
935 for (ii = 1; ii < 8; ii++)
936 if (addr->in6_16[ii] != val)
941 /** Compare two IP addresses.
942 * @param[in] a First address to compare.
943 * @param[in] b Second address to compare.
944 * @return Non-zero if the two addresses differ, zero if they are identical.
946 int irc_in_addr_cmp(const struct irc_in_addr *a, const struct irc_in_addr *b)
948 if (irc_in_addr_is_ipv4(a))
949 return a->in6_16[6] != b->in6_16[6]
950 || a->in6_16[7] != b->in6_16[7]
951 || !irc_in_addr_is_ipv4(b);
953 return memcmp(a, b, sizeof(*a));
956 /** Indicate whether an IP address is a loopback address.
957 * @param[in] addr Address to check.
958 * @return Non-zero if the address is loopback; zero if not.
960 int irc_in_addr_is_loopback(const struct irc_in_addr *addr)
962 if (addr->in6_16[0] != 0
963 || addr->in6_16[1] != 0
964 || addr->in6_16[2] != 0
965 || addr->in6_16[3] != 0
966 || addr->in6_16[4] != 0)
968 if ((addr->in6_16[5] == 0xffff) || (addr->in6_16[5] == 0 && addr->in6_16[6] != 0))
969 return (ntohs(addr->in6_16[6]) & 0xff00) == 0x7f00;
971 return addr->in6_16[5] == 0 && addr->in6_16[6] == 0 && htons(addr->in6_16[7]) == 1;