2 * Copyright (C) 2009, 2012-2014 Internet Systems Consortium, Inc. ("ISC")
4 * Permission to use, copy, modify, and/or distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
9 * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
10 * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
11 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
12 * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
13 * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
14 * PERFORMANCE OF THIS SOFTWARE.
17 /* $Id: getaddrinfo.c,v 1.3 2009/09/02 23:48:02 tbox Exp $ */
22 * getaddrinfo() is used to get a list of IP addresses and port
23 * numbers for host hostname and service servname as defined in RFC3493.
24 * hostname and servname are pointers to null-terminated strings
25 * or NULL. hostname is either a host name or a numeric host address
26 * string: a dotted decimal IPv4 address or an IPv6 address. servname is
27 * either a decimal port number or a service name as listed in
30 * If the operating system does not provide a struct addrinfo, the
31 * following structure is used:
35 * int ai_flags; // AI_PASSIVE, AI_CANONNAME
36 * int ai_family; // PF_xxx
37 * int ai_socktype; // SOCK_xxx
38 * int ai_protocol; // 0 or IPPROTO_xxx for IPv4 and IPv6
39 * size_t ai_addrlen; // length of ai_addr
40 * char *ai_canonname; // canonical name for hostname
41 * struct sockaddr *ai_addr; // binary address
42 * struct addrinfo *ai_next; // next structure in linked list
47 * hints is an optional pointer to a struct addrinfo. This structure can
48 * be used to provide hints concerning the type of socket that the caller
49 * supports or wishes to use. The caller can supply the following
50 * structure elements in *hints:
54 * The protocol family that should be used. When ai_family is set
55 * to PF_UNSPEC, it means the caller will accept any protocol
56 * family supported by the operating system.</li>
59 * denotes the type of socket -- SOCK_STREAM, SOCK_DGRAM or
60 * SOCK_RAW -- that is wanted. When ai_socktype is zero the caller
61 * will accept any socket type.</li>
64 * indicates which transport protocol is wanted: IPPROTO_UDP or
65 * IPPROTO_TCP. If ai_protocol is zero the caller will accept any
69 * Flag bits. If the AI_CANONNAME bit is set, a successful call to
70 * getaddrinfo() will return a null-terminated string
71 * containing the canonical name of the specified hostname in
72 * ai_canonname of the first addrinfo structure returned. Setting
73 * the AI_PASSIVE bit indicates that the returned socket address
74 * structure is intended for used in a call to bind(2). In this
75 * case, if the hostname argument is a NULL pointer, then the IP
76 * address portion of the socket address structure will be set to
77 * INADDR_ANY for an IPv4 address or IN6ADDR_ANY_INIT for an IPv6
78 * address.<br /><br />
80 * When ai_flags does not set the AI_PASSIVE bit, the returned
81 * socket address structure will be ready for use in a call to
82 * connect(2) for a connection-oriented protocol or connect(2),
83 * sendto(2), or sendmsg(2) if a connectionless protocol was
84 * chosen. The IP address portion of the socket address structure
85 * will be set to the loopback address if hostname is a NULL
86 * pointer and AI_PASSIVE is not set in ai_flags.<br /><br />
88 * If ai_flags is set to AI_NUMERICHOST it indicates that hostname
89 * should be treated as a numeric string defining an IPv4 or IPv6
90 * address and no name resolution should be attempted.
93 * All other elements of the struct addrinfo passed via hints must be
96 * A hints of NULL is treated as if the caller provided a struct addrinfo
97 * initialized to zero with ai_familyset to PF_UNSPEC.
99 * After a successful call to getaddrinfo(), *res is a pointer to a
100 * linked list of one or more addrinfo structures. Each struct addrinfo
101 * in this list cn be processed by following the ai_next pointer, until a
102 * NULL pointer is encountered. The three members ai_family, ai_socktype,
103 * and ai_protocol in each returned addrinfo structure contain the
104 * corresponding arguments for a call to socket(2). For each addrinfo
105 * structure in the list, the ai_addr member points to a filled-in socket
106 * address structure of length ai_addrlen.
108 * All of the information returned by getaddrinfo() is dynamically
109 * allocated: the addrinfo structures, and the socket address structures
110 * and canonical host name strings pointed to by the addrinfostructures.
111 * Memory allocated for the dynamically allocated structures created by a
112 * successful call to getaddrinfo() is released by freeaddrinfo().
113 * ai is a pointer to a struct addrinfo created by a call to getaddrinfo().
115 * \section irsreturn RETURN VALUES
117 * getaddrinfo() returns zero on success or one of the error codes
118 * listed in gai_strerror() if an error occurs. If both hostname and
119 * servname are NULL getaddrinfo() returns #EAI_NONAME.
121 * \section irssee SEE ALSO
123 * getaddrinfo(), freeaddrinfo(),
124 * gai_strerror(), RFC3493, getservbyname(3), connect(2),
125 * sendto(2), sendmsg(2), socket(2).
135 #include <isc/buffer.h>
138 #include <isc/sockaddr.h>
139 #include <isc/string.h>
140 #include <isc/util.h>
142 #include <dns/client.h>
143 #include <dns/fixedname.h>
144 #include <dns/name.h>
145 #include <dns/rdata.h>
146 #include <dns/rdataset.h>
147 #include <dns/rdatastruct.h>
148 #include <dns/rdatatype.h>
149 #include <dns/result.h>
151 #include <irs/context.h>
152 #include <irs/netdb.h>
153 #include <irs/resconf.h>
155 #define SA(addr) ((struct sockaddr *)(addr))
156 #define SIN(addr) ((struct sockaddr_in *)(addr))
157 #define SIN6(addr) ((struct sockaddr_in6 *)(addr))
158 #define SLOCAL(addr) ((struct sockaddr_un *)(addr))
162 static struct addrinfo
163 *ai_concat(struct addrinfo *ai1, struct addrinfo *ai2),
164 *ai_reverse(struct addrinfo *oai),
165 *ai_clone(struct addrinfo *oai, int family),
166 *ai_alloc(int family, int addrlen);
168 static int get_local(const char *name, int socktype, struct addrinfo **res);
172 resolve_name(int family, const char *hostname, int flags,
173 struct addrinfo **aip, int socktype, int port);
175 static int add_ipv4(const char *hostname, int flags, struct addrinfo **aip,
176 int socktype, int port);
177 static int add_ipv6(const char *hostname, int flags, struct addrinfo **aip,
178 int socktype, int port);
179 static void set_order(int, int (**)(const char *, int, struct addrinfo **,
182 #define FOUND_IPV4 0x1
183 #define FOUND_IPV6 0x2
186 #define ISC_AI_MASK (AI_PASSIVE|AI_CANONNAME|AI_NUMERICHOST)
188 * Get a list of IP addresses and port numbers for host hostname and
192 getaddrinfo(const char *hostname, const char *servname,
193 const struct addrinfo *hints, struct addrinfo **res)
197 int family, socktype, flags, protocol;
198 struct addrinfo *ai, *ai_list;
201 int (*net_order[FOUND_MAX+1])(const char *, int, struct addrinfo **,
204 if (hostname == NULL && servname == NULL)
209 if ((hints->ai_flags & ~(ISC_AI_MASK)) != 0)
210 return (EAI_BADFLAGS);
211 if (hints->ai_addrlen || hints->ai_canonname ||
212 hints->ai_addr || hints->ai_next) {
216 family = hints->ai_family;
217 socktype = hints->ai_socktype;
218 protocol = hints->ai_protocol;
219 flags = hints->ai_flags;
222 switch (hints->ai_socktype) {
233 switch (hints->ai_socktype) {
245 return (EAI_SOCKTYPE);
250 switch (hints->ai_socktype) {
258 return (EAI_SOCKTYPE);
274 * First, deal with AF_LOCAL. If the family was not set,
275 * then assume AF_LOCAL if the first character of the
276 * hostname/servname is '/'.
279 if (hostname != NULL &&
280 (family == AF_LOCAL || (family == 0 && *hostname == '/')))
281 return (get_local(hostname, socktype, res));
283 if (servname != NULL &&
284 (family == AF_LOCAL || (family == 0 && *servname == '/')))
285 return (get_local(servname, socktype, res));
289 * Ok, only AF_INET and AF_INET6 left.
294 * First, look up the service name (port) if it was
295 * requested. If the socket type wasn't specified, then
296 * try and figure it out.
298 if (servname != NULL) {
301 port = strtol(servname, &e, 10);
304 return (EAI_SOCKTYPE);
305 if (port < 0 || port > 65535)
306 return (EAI_SERVICE);
307 port = htons((unsigned short) port);
309 sp = getservbyname(servname, proto);
311 return (EAI_SERVICE);
314 if (strcmp(sp->s_proto, "tcp") == 0)
315 socktype = SOCK_STREAM;
316 else if (strcmp(sp->s_proto, "udp") == 0)
317 socktype = SOCK_DGRAM;
324 * Next, deal with just a service name, and no hostname.
325 * (we verified that one of them was non-null up above).
327 if (hostname == NULL && (flags & AI_PASSIVE) != 0) {
328 if (family == AF_INET || family == 0) {
329 ai = ai_alloc(AF_INET, sizeof(struct sockaddr_in));
332 ai->ai_socktype = socktype;
333 ai->ai_protocol = protocol;
334 SIN(ai->ai_addr)->sin_port = port;
335 ai->ai_next = ai_list;
339 if (family == AF_INET6 || family == 0) {
340 ai = ai_alloc(AF_INET6, sizeof(struct sockaddr_in6));
342 freeaddrinfo(ai_list);
345 ai->ai_socktype = socktype;
346 ai->ai_protocol = protocol;
347 SIN6(ai->ai_addr)->sin6_port = port;
348 ai->ai_next = ai_list;
357 * If the family isn't specified or AI_NUMERICHOST specified, check
358 * first to see if it is a numeric address.
359 * Though the gethostbyname2() routine will recognize numeric addresses,
360 * it will only recognize the format that it is being called for. Thus,
361 * a numeric AF_INET address will be treated by the AF_INET6 call as
362 * a domain name, and vice versa. Checking for both numerics here
365 if (hostname != NULL &&
366 (family == 0 || (flags & AI_NUMERICHOST) != 0)) {
367 char abuf[sizeof(struct in6_addr)];
368 char nbuf[NI_MAXHOST];
369 int addrsize, addroff;
370 #ifdef IRS_HAVE_SIN6_SCOPE_ID
372 char ntmp[NI_MAXHOST];
373 isc_uint32_t scopeid;
376 #ifdef IRS_HAVE_SIN6_SCOPE_ID
378 * Scope identifier portion.
381 if (strchr(hostname, '%') != NULL) {
382 strncpy(ntmp, hostname, sizeof(ntmp) - 1);
383 ntmp[sizeof(ntmp) - 1] = '\0';
384 p = strchr(ntmp, '%');
388 * Vendors may want to support non-numeric
389 * scopeid around here.
393 scopeid = (isc_uint32_t)strtoul(p + 1,
395 if (p != NULL && ep != NULL && ep[0] == '\0')
405 if (inet_pton(AF_INET, hostname, (struct in_addr *)abuf)
407 if (family == AF_INET6) {
409 * Convert to a V4 mapped address.
411 struct in6_addr *a6 = (struct in6_addr *)abuf;
412 memmove(&a6->s6_addr[12], &a6->s6_addr[0], 4);
413 memset(&a6->s6_addr[10], 0xff, 2);
414 memset(&a6->s6_addr[0], 0, 10);
417 addrsize = sizeof(struct in_addr);
418 addroff = (char *)(&SIN(0)->sin_addr) - (char *)0;
421 #ifdef IRS_HAVE_SIN6_SCOPE_ID
422 } else if (ntmp[0] != '\0' &&
423 inet_pton(AF_INET6, ntmp, abuf) == 1) {
424 if (family && family != AF_INET6)
426 addrsize = sizeof(struct in6_addr);
427 addroff = (char *)(&SIN6(0)->sin6_addr) - (char *)0;
431 } else if (inet_pton(AF_INET6, hostname, abuf) == 1) {
432 if (family != 0 && family != AF_INET6)
435 addrsize = sizeof(struct in6_addr);
436 addroff = (char *)(&SIN6(0)->sin6_addr) - (char *)0;
440 ai = ai_alloc(family,
441 ((family == AF_INET6) ?
442 sizeof(struct sockaddr_in6) :
443 sizeof(struct sockaddr_in)));
447 ai->ai_socktype = socktype;
448 SIN(ai->ai_addr)->sin_port = port;
449 memmove((char *)ai->ai_addr + addroff, abuf, addrsize);
450 if ((flags & AI_CANONNAME) != 0) {
451 #ifdef IRS_HAVE_SIN6_SCOPE_ID
452 if (ai->ai_family == AF_INET6)
453 SIN6(ai->ai_addr)->sin6_scope_id =
456 if (getnameinfo(ai->ai_addr, ai->ai_addrlen,
457 nbuf, sizeof(nbuf), NULL, 0,
458 NI_NUMERICHOST) == 0) {
459 ai->ai_canonname = strdup(nbuf);
460 if (ai->ai_canonname == NULL) {
465 /* XXX raise error? */
466 ai->ai_canonname = NULL;
470 } else if ((flags & AI_NUMERICHOST) != 0) {
475 if (hostname == NULL && (flags & AI_PASSIVE) == 0) {
476 set_order(family, net_order);
477 for (i = 0; i < FOUND_MAX; i++) {
478 if (net_order[i] == NULL)
480 err = (net_order[i])(hostname, flags, &ai_list,
483 if (ai_list != NULL) {
484 freeaddrinfo(ai_list);
491 err = resolve_name(family, hostname, flags, &ai_list,
494 if (ai_list == NULL) {
501 ai_list = ai_reverse(ai_list);
507 typedef struct gai_restrans {
508 dns_clientrestrans_t *xid;
509 isc_boolean_t is_inprogress;
511 struct addrinfo ai_sentinel;
512 struct gai_resstate *resstate;
515 typedef struct gai_resstate {
517 struct gai_statehead *head;
518 dns_fixedname_t fixedname;
520 gai_restrans_t *trans4;
521 gai_restrans_t *trans6;
522 ISC_LINK(struct gai_resstate) link;
525 typedef struct gai_statehead {
531 dns_client_t *dnsclient;
532 ISC_LIST(struct gai_resstate) resstates;
533 unsigned int activestates;
537 make_resstate(isc_mem_t *mctx, gai_statehead_t *head, const char *hostname,
538 const char *domain, gai_resstate_t **statep)
541 gai_resstate_t *state;
542 dns_fixedname_t fixeddomain;
546 isc_boolean_t need_v4 = ISC_FALSE;
547 isc_boolean_t need_v6 = ISC_FALSE;
549 state = isc_mem_get(mctx, sizeof(*state));
551 return (ISC_R_NOMEMORY);
553 /* Construct base domain name */
554 namelen = strlen(domain);
555 isc_buffer_constinit(&b, domain, namelen);
556 isc_buffer_add(&b, namelen);
557 dns_fixedname_init(&fixeddomain);
558 qdomain = dns_fixedname_name(&fixeddomain);
559 result = dns_name_fromtext(qdomain, &b, dns_rootname, 0, NULL);
560 if (result != ISC_R_SUCCESS) {
561 isc_mem_put(mctx, state, sizeof(*state));
565 /* Construct query name */
566 namelen = strlen(hostname);
567 isc_buffer_constinit(&b, hostname, namelen);
568 isc_buffer_add(&b, namelen);
569 dns_fixedname_init(&state->fixedname);
570 state->qname = dns_fixedname_name(&state->fixedname);
571 result = dns_name_fromtext(state->qname, &b, qdomain, 0, NULL);
572 if (result != ISC_R_SUCCESS) {
573 isc_mem_put(mctx, state, sizeof(*state));
577 if (head->ai_family == AF_UNSPEC || head->ai_family == AF_INET)
579 if (head->ai_family == AF_UNSPEC || head->ai_family == AF_INET6)
582 state->trans6 = NULL;
583 state->trans4 = NULL;
585 state->trans4 = isc_mem_get(mctx, sizeof(gai_restrans_t));
586 if (state->trans4 == NULL) {
587 isc_mem_put(mctx, state, sizeof(*state));
588 return (ISC_R_NOMEMORY);
590 state->trans4->error = 0;
591 state->trans4->xid = NULL;
592 state->trans4->resstate = state;
593 state->trans4->is_inprogress = ISC_TRUE;
594 state->trans4->ai_sentinel.ai_next = NULL;
597 state->trans6 = isc_mem_get(mctx, sizeof(gai_restrans_t));
598 if (state->trans6 == NULL) {
599 if (state->trans4 != NULL)
600 isc_mem_put(mctx, state->trans4,
601 sizeof(*state->trans4));
602 isc_mem_put(mctx, state, sizeof(*state));
603 return (ISC_R_NOMEMORY);
605 state->trans6->error = 0;
606 state->trans6->xid = NULL;
607 state->trans6->resstate = state;
608 state->trans6->is_inprogress = ISC_TRUE;
609 state->trans6->ai_sentinel.ai_next = NULL;
614 ISC_LINK_INIT(state, link);
618 return (ISC_R_SUCCESS);
622 make_resstates(isc_mem_t *mctx, const char *hostname, gai_statehead_t *head,
623 irs_resconf_t *resconf)
626 irs_resconf_searchlist_t *searchlist;
627 irs_resconf_search_t *searchent;
628 gai_resstate_t *resstate, *resstate0;
631 result = make_resstate(mctx, head, hostname, ".", &resstate0);
632 if (result != ISC_R_SUCCESS)
635 searchlist = irs_resconf_getsearchlist(resconf);
636 for (searchent = ISC_LIST_HEAD(*searchlist); searchent != NULL;
637 searchent = ISC_LIST_NEXT(searchent, link)) {
639 result = make_resstate(mctx, head, hostname,
640 (const char *)searchent->domain,
642 if (result != ISC_R_SUCCESS)
645 ISC_LIST_APPEND(head->resstates, resstate, link);
646 head->activestates++;
650 * Insert the original hostname either at the head or the tail of the
651 * state list, depending on the number of labels contained in the
652 * original name and the 'ndots' configuration parameter.
654 if (dns_name_countlabels(resstate0->qname) >
655 irs_resconf_getndots(resconf) + 1) {
656 ISC_LIST_PREPEND(head->resstates, resstate0, link);
658 ISC_LIST_APPEND(head->resstates, resstate0, link);
659 head->activestates++;
661 if (result != ISC_R_SUCCESS) {
662 while ((resstate = ISC_LIST_HEAD(head->resstates)) != NULL) {
663 ISC_LIST_UNLINK(head->resstates, resstate, link);
664 if (resstate->trans4 != NULL) {
665 isc_mem_put(mctx, resstate->trans4,
666 sizeof(*resstate->trans4));
668 if (resstate->trans6 != NULL) {
669 isc_mem_put(mctx, resstate->trans6,
670 sizeof(*resstate->trans6));
673 isc_mem_put(mctx, resstate, sizeof(*resstate));
681 process_answer(isc_task_t *task, isc_event_t *event) {
682 int error = 0, family;
683 gai_restrans_t *trans = event->ev_arg;
684 gai_resstate_t *resstate;
685 dns_clientresevent_t *rev = (dns_clientresevent_t *)event;
686 dns_rdatatype_t qtype;
689 REQUIRE(trans != NULL);
690 resstate = trans->resstate;
691 REQUIRE(resstate != NULL);
692 REQUIRE(task != NULL);
694 if (trans == resstate->trans4) {
696 qtype = dns_rdatatype_a;
698 INSIST(trans == resstate->trans6);
700 qtype = dns_rdatatype_aaaa;
703 INSIST(trans->is_inprogress);
704 trans->is_inprogress = ISC_FALSE;
706 switch (rev->result) {
708 case DNS_R_NCACHENXDOMAIN: /* treat this as a fatal error? */
709 case DNS_R_NCACHENXRRSET:
712 switch (rev->vresult) {
713 case DNS_R_SIGINVALID:
714 case DNS_R_SIGEXPIRED:
715 case DNS_R_SIGFUTURE:
716 case DNS_R_KEYUNAUTHORIZED:
717 case DNS_R_MUSTBESECURE:
718 case DNS_R_COVERINGNSEC:
719 case DNS_R_NOTAUTHORITATIVE:
720 case DNS_R_NOVALIDKEY:
721 case DNS_R_NOVALIDDS:
722 case DNS_R_NOVALIDSIG:
723 error = EAI_INSECUREDATA;
731 /* Parse the response and construct the addrinfo chain */
732 for (name = ISC_LIST_HEAD(rev->answerlist); name != NULL;
733 name = ISC_LIST_NEXT(name, link)) {
735 dns_rdataset_t *rdataset;
740 for (rdataset = ISC_LIST_HEAD(name->list);
742 rdataset = ISC_LIST_NEXT(rdataset, link)) {
743 if (!dns_rdataset_isassociated(rdataset))
745 if (rdataset->type != qtype)
748 if ((resstate->head->ai_flags & AI_CANONNAME) != 0) {
749 isc_buffer_init(&b, t, sizeof(t));
750 result = dns_name_totext(name, ISC_TRUE, &b);
751 if (result != ISC_R_SUCCESS) {
755 isc_buffer_putuint8(&b, '\0');
756 isc_buffer_usedregion(&b, &r);
759 for (result = dns_rdataset_first(rdataset);
760 result == ISC_R_SUCCESS;
761 result = dns_rdataset_next(rdataset)) {
764 dns_rdata_in_a_t rdata_a;
765 dns_rdata_in_aaaa_t rdata_aaaa;
767 ai = ai_alloc(family,
768 ((family == AF_INET6) ?
769 sizeof(struct sockaddr_in6) :
770 sizeof(struct sockaddr_in)));
775 ai->ai_socktype = resstate->head->ai_socktype;
776 ai->ai_next = trans->ai_sentinel.ai_next;
777 trans->ai_sentinel.ai_next = ai;
780 * Set AF-specific parameters
781 * (IPv4/v6 address/port)
783 dns_rdata_init(&rdata);
786 dns_rdataset_current(rdataset, &rdata);
787 result = dns_rdata_tostruct(&rdata, &rdata_a,
789 RUNTIME_CHECK(result == ISC_R_SUCCESS);
790 SIN(ai->ai_addr)->sin_port =
791 resstate->head->ai_port;
792 memmove(&SIN(ai->ai_addr)->sin_addr,
793 &rdata_a.in_addr, 4);
794 dns_rdata_freestruct(&rdata_a);
797 dns_rdataset_current(rdataset, &rdata);
798 result = dns_rdata_tostruct(&rdata, &rdata_aaaa,
800 RUNTIME_CHECK(result == ISC_R_SUCCESS);
801 SIN6(ai->ai_addr)->sin6_port =
802 resstate->head->ai_port;
803 memmove(&SIN6(ai->ai_addr)->sin6_addr,
804 &rdata_aaaa.in6_addr, 16);
805 dns_rdata_freestruct(&rdata_aaaa);
809 if ((resstate->head->ai_flags & AI_CANONNAME)
812 strdup((const char *)r.base);
813 if (ai->ai_canonname == NULL) {
823 dns_client_freeresanswer(resstate->head->dnsclient, &rev->answerlist);
824 dns_client_destroyrestrans(&trans->xid);
826 isc_event_free(&event);
828 /* Make sure that error == 0 iff we have a non-empty list */
830 if (trans->ai_sentinel.ai_next == NULL)
833 if (trans->ai_sentinel.ai_next != NULL) {
834 freeaddrinfo(trans->ai_sentinel.ai_next);
835 trans->ai_sentinel.ai_next = NULL;
838 trans->error = error;
840 /* Check whether we are done */
841 if ((resstate->trans4 == NULL || !resstate->trans4->is_inprogress) &&
842 (resstate->trans6 == NULL || !resstate->trans6->is_inprogress)) {
844 * We're done for this state. If there is no other outstanding
845 * state, we can exit.
847 resstate->head->activestates--;
848 if (resstate->head->activestates == 0) {
849 isc_app_ctxsuspend(resstate->head->actx);
854 * There are outstanding states, but if we are at the head
855 * of the state list (i.e., at the highest search priority)
856 * and have any answer, we can stop now by canceling the
859 if (resstate == ISC_LIST_HEAD(resstate->head->resstates)) {
860 if ((resstate->trans4 != NULL &&
861 resstate->trans4->ai_sentinel.ai_next != NULL) ||
862 (resstate->trans6 != NULL &&
863 resstate->trans6->ai_sentinel.ai_next != NULL)) {
864 gai_resstate_t *rest;
866 for (rest = ISC_LIST_NEXT(resstate, link);
868 rest = ISC_LIST_NEXT(rest, link)) {
869 if (rest->trans4 != NULL &&
870 rest->trans4->xid != NULL)
871 dns_client_cancelresolve(
873 if (rest->trans6 != NULL &&
874 rest->trans6->xid != NULL)
875 dns_client_cancelresolve(
880 * This search fails, so we move to the tail
881 * of the list so that the next entry will
882 * have the highest priority.
884 ISC_LIST_UNLINK(resstate->head->resstates,
886 ISC_LIST_APPEND(resstate->head->resstates,
894 resolve_name(int family, const char *hostname, int flags,
895 struct addrinfo **aip, int socktype, int port)
898 irs_context_t *irsctx;
905 dns_client_t *client;
906 gai_resstate_t *resstate;
907 gai_statehead_t head;
908 isc_boolean_t all_fail = ISC_TRUE;
910 /* get IRS context and the associated parameters */
912 result = irs_context_get(&irsctx);
913 if (result != ISC_R_SUCCESS)
915 actx = irs_context_getappctx(irsctx);
917 mctx = irs_context_getmctx(irsctx);
918 task = irs_context_gettask(irsctx);
919 conf = irs_context_getresconf(irsctx);
920 client = irs_context_getdnsclient(irsctx);
922 /* construct resolution states */
923 head.activestates = 0;
924 head.ai_family = family;
925 head.ai_socktype = socktype;
926 head.ai_flags = flags;
929 head.dnsclient = client;
930 ISC_LIST_INIT(head.resstates);
931 result = make_resstates(mctx, hostname, &head, conf);
932 if (result != ISC_R_SUCCESS)
935 for (resstate = ISC_LIST_HEAD(head.resstates);
936 resstate != NULL; resstate = ISC_LIST_NEXT(resstate, link)) {
937 if (resstate->trans4 != NULL) {
938 result = dns_client_startresolve(client,
945 &resstate->trans4->xid);
946 if (result == ISC_R_SUCCESS) {
947 resstate->trans4->is_inprogress = ISC_TRUE;
948 all_fail = ISC_FALSE;
950 resstate->trans4->is_inprogress = ISC_FALSE;
952 if (resstate->trans6 != NULL) {
953 result = dns_client_startresolve(client,
960 &resstate->trans6->xid);
961 if (result == ISC_R_SUCCESS) {
962 resstate->trans6->is_inprogress = ISC_TRUE;
963 all_fail = ISC_FALSE;
965 resstate->trans6->is_inprogress= ISC_FALSE;
969 /* Start all the events */
970 isc_app_ctxrun(actx);
975 while ((resstate = ISC_LIST_HEAD(head.resstates)) != NULL) {
976 int terror4 = 0, terror6 = 0;
978 ISC_LIST_UNLINK(head.resstates, resstate, link);
981 struct addrinfo *sentinel4 = NULL;
982 struct addrinfo *sentinel6 = NULL;
984 if (resstate->trans4 != NULL) {
986 resstate->trans4->ai_sentinel.ai_next;
987 resstate->trans4->ai_sentinel.ai_next = NULL;
989 if (resstate->trans6 != NULL) {
991 resstate->trans6->ai_sentinel.ai_next;
992 resstate->trans6->ai_sentinel.ai_next = NULL;
994 *aip = ai_concat(sentinel4, sentinel6);
997 if (resstate->trans4 != NULL) {
998 INSIST(resstate->trans4->xid == NULL);
999 terror4 = resstate->trans4->error;
1000 isc_mem_put(mctx, resstate->trans4,
1001 sizeof(*resstate->trans4));
1003 if (resstate->trans6 != NULL) {
1004 INSIST(resstate->trans6->xid == NULL);
1005 terror6 = resstate->trans6->error;
1006 isc_mem_put(mctx, resstate->trans6,
1007 sizeof(*resstate->trans6));
1011 * If the entire lookup fails, we need to choose an appropriate
1012 * error code from individual codes. We'll try to provide as
1013 * specific a code as possible. In general, we are going to
1014 * find an error code other than EAI_NONAME (which is too
1015 * generic and may actually not be problematic in some cases).
1016 * EAI_NONAME will be set below if no better code is found.
1018 if (terror == 0 || terror == EAI_NONAME) {
1019 if (terror4 != 0 && terror4 != EAI_NONAME)
1021 else if (terror6 != 0 && terror6 != EAI_NONAME)
1025 isc_mem_put(mctx, resstate, sizeof(*resstate));
1034 #if 1 /* XXX: enabled for finding leaks. should be cleaned up later. */
1035 isc_app_ctxfinish(actx);
1036 irs_context_destroy(&irsctx);
1043 irs_strsep(char **stringp, const char *delim) {
1044 char *string = *stringp;
1052 for (s = string; *s != '\0'; s++) {
1054 for (d = delim; (dc = *d) != '\0'; d++)
1066 set_order(int family, int (**net_order)(const char *, int, struct addrinfo **,
1075 *net_order++ = add_ipv4;
1078 *net_order++ = add_ipv6;
1082 order = getenv("NET_ORDER");
1084 while (order != NULL) {
1086 * We ignore any unknown names.
1088 tok = irs_strsep(&order, ":");
1089 if (strcasecmp(tok, "inet6") == 0) {
1090 if ((found & FOUND_IPV6) == 0)
1091 *net_order++ = add_ipv6;
1092 found |= FOUND_IPV6;
1093 } else if (strcasecmp(tok, "inet") == 0 ||
1094 strcasecmp(tok, "inet4") == 0) {
1095 if ((found & FOUND_IPV4) == 0)
1096 *net_order++ = add_ipv4;
1097 found |= FOUND_IPV4;
1102 * Add in anything that we didn't find.
1104 if ((found & FOUND_IPV4) == 0)
1105 *net_order++ = add_ipv4;
1106 if ((found & FOUND_IPV6) == 0)
1107 *net_order++ = add_ipv6;
1113 static char v4_loop[4] = { 127, 0, 0, 1 };
1116 add_ipv4(const char *hostname, int flags, struct addrinfo **aip,
1117 int socktype, int port)
1119 struct addrinfo *ai;
1124 ai = ai_clone(*aip, AF_INET); /* don't use ai_clone() */
1127 return (EAI_MEMORY);
1131 ai->ai_socktype = socktype;
1132 SIN(ai->ai_addr)->sin_port = port;
1133 memmove(&SIN(ai->ai_addr)->sin_addr, v4_loop, 4);
1138 static char v6_loop[16] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 };
1141 add_ipv6(const char *hostname, int flags, struct addrinfo **aip,
1142 int socktype, int port)
1144 struct addrinfo *ai;
1149 ai = ai_clone(*aip, AF_INET6); /* don't use ai_clone() */
1151 return (EAI_MEMORY);
1154 ai->ai_socktype = socktype;
1155 SIN6(ai->ai_addr)->sin6_port = port;
1156 memmove(&SIN6(ai->ai_addr)->sin6_addr, v6_loop, 16);
1161 /*% Free address info. */
1163 freeaddrinfo(struct addrinfo *ai) {
1164 struct addrinfo *ai_next;
1166 while (ai != NULL) {
1167 ai_next = ai->ai_next;
1168 if (ai->ai_addr != NULL)
1170 if (ai->ai_canonname)
1171 free(ai->ai_canonname);
1179 get_local(const char *name, int socktype, struct addrinfo **res) {
1180 struct addrinfo *ai;
1181 struct sockaddr_un *slocal;
1184 return (EAI_SOCKTYPE);
1186 ai = ai_alloc(AF_LOCAL, sizeof(*slocal));
1188 return (EAI_MEMORY);
1190 slocal = SLOCAL(ai->ai_addr);
1191 strlcpy(slocal->sun_path, name, sizeof(slocal->sun_path));
1193 ai->ai_socktype = socktype;
1195 * ai->ai_flags, ai->ai_protocol, ai->ai_canonname,
1196 * and ai->ai_next were initialized to zero.
1205 * Allocate an addrinfo structure, and a sockaddr structure
1206 * of the specificed length. We initialize:
1210 * ai_addr->sa_family
1211 * ai_addr->sa_len (IRS_PLATFORM_HAVESALEN)
1212 * and everything else is initialized to zero.
1214 static struct addrinfo *
1215 ai_alloc(int family, int addrlen) {
1216 struct addrinfo *ai;
1218 ai = (struct addrinfo *)calloc(1, sizeof(*ai));
1222 ai->ai_addr = SA(calloc(1, addrlen));
1223 if (ai->ai_addr == NULL) {
1227 ai->ai_addrlen = addrlen;
1228 ai->ai_family = family;
1229 ai->ai_addr->sa_family = family;
1230 #ifdef IRS_PLATFORM_HAVESALEN
1231 ai->ai_addr->sa_len = addrlen;
1236 static struct addrinfo *
1237 ai_clone(struct addrinfo *oai, int family) {
1238 struct addrinfo *ai;
1240 ai = ai_alloc(family, ((family == AF_INET6) ?
1241 sizeof(struct sockaddr_in6) : sizeof(struct sockaddr_in)));
1251 ai->ai_flags = oai->ai_flags;
1252 ai->ai_socktype = oai->ai_socktype;
1253 ai->ai_protocol = oai->ai_protocol;
1254 ai->ai_canonname = NULL;
1259 static struct addrinfo *
1260 ai_reverse(struct addrinfo *oai) {
1261 struct addrinfo *nai, *tai;
1265 while (oai != NULL) {
1267 * Grab one off the old list.
1272 * Put it on the front of the new list.
1281 static struct addrinfo *
1282 ai_concat(struct addrinfo *ai1, struct addrinfo *ai2) {
1283 struct addrinfo *ai_tmp;
1287 else if (ai2 == NULL)
1290 for (ai_tmp = ai1; ai_tmp != NULL && ai_tmp->ai_next != NULL;
1291 ai_tmp = ai_tmp->ai_next)
1294 ai_tmp->ai_next = ai2;