1 /*
2 * Copyright (C) Internet Systems Consortium, Inc. ("ISC")
3 *
4 * This Source Code Form is subject to the terms of the Mozilla Public
5 * License, v. 2.0. If a copy of the MPL was not distributed with this
6 * file, you can obtain one at https://mozilla.org/MPL/2.0/.
7 *
8 * See the COPYRIGHT file distributed with this work for additional
9 * information regarding copyright ownership.
10 */
11
12 /*! \file */
13
14 /**
15 * getaddrinfo() is used to get a list of IP addresses and port
16 * numbers for host hostname and service servname as defined in RFC3493.
17 * hostname and servname are pointers to null-terminated strings
18 * or NULL. hostname is either a host name or a numeric host address
19 * string: a dotted decimal IPv4 address or an IPv6 address. servname is
20 * either a decimal port number or a service name as listed in
21 * /etc/services.
22 *
23 * If the operating system does not provide a struct addrinfo, the
24 * following structure is used:
25 *
26 * \code
27 * struct addrinfo {
28 * int ai_flags; // AI_PASSIVE, AI_CANONNAME
29 * int ai_family; // PF_xxx
30 * int ai_socktype; // SOCK_xxx
31 * int ai_protocol; // 0 or IPPROTO_xxx for IPv4 and IPv6
32 * size_t ai_addrlen; // length of ai_addr
33 * char *ai_canonname; // canonical name for hostname
34 * struct sockaddr *ai_addr; // binary address
35 * struct addrinfo *ai_next; // next structure in linked list
36 * };
37 * \endcode
38 *
39 *
40 * hints is an optional pointer to a struct addrinfo. This structure can
41 * be used to provide hints concerning the type of socket that the caller
42 * supports or wishes to use. The caller can supply the following
43 * structure elements in *hints:
44 *
45 * <ul>
46 * <li>ai_family:
47 * The protocol family that should be used. When ai_family is set
48 * to PF_UNSPEC, it means the caller will accept any protocol
49 * family supported by the operating system.</li>
50 *
51 * <li>ai_socktype:
52 * denotes the type of socket -- SOCK_STREAM, SOCK_DGRAM or
53 * SOCK_RAW -- that is wanted. When ai_socktype is zero the caller
54 * will accept any socket type.</li>
55 *
56 * <li>ai_protocol:
57 * indicates which transport protocol is wanted: IPPROTO_UDP or
58 * IPPROTO_TCP. If ai_protocol is zero the caller will accept any
59 * protocol.</li>
60 *
61 * <li>ai_flags:
62 * Flag bits. If the AI_CANONNAME bit is set, a successful call to
63 * getaddrinfo() will return a null-terminated string
64 * containing the canonical name of the specified hostname in
65 * ai_canonname of the first addrinfo structure returned. Setting
66 * the AI_PASSIVE bit indicates that the returned socket address
67 * structure is intended for used in a call to bind(2). In this
68 * case, if the hostname argument is a NULL pointer, then the IP
69 * address portion of the socket address structure will be set to
70 * INADDR_ANY for an IPv4 address or IN6ADDR_ANY_INIT for an IPv6
71 * address.<br /><br />
72 *
73 * When ai_flags does not set the AI_PASSIVE bit, the returned
74 * socket address structure will be ready for use in a call to
75 * connect(2) for a connection-oriented protocol or connect(2),
76 * sendto(2), or sendmsg(2) if a connectionless protocol was
77 * chosen. The IP address portion of the socket address structure
78 * will be set to the loopback address if hostname is a NULL
79 * pointer and AI_PASSIVE is not set in ai_flags.<br /><br />
80 *
81 * If ai_flags is set to AI_NUMERICHOST it indicates that hostname
82 * should be treated as a numeric string defining an IPv4 or IPv6
83 * address and no name resolution should be attempted.
84 * </li></ul>
85 *
86 * All other elements of the struct addrinfo passed via hints must be
87 * zero.
88 *
89 * A hints of NULL is treated as if the caller provided a struct addrinfo
90 * initialized to zero with ai_familyset to PF_UNSPEC.
91 *
92 * After a successful call to getaddrinfo(), *res is a pointer to a
93 * linked list of one or more addrinfo structures. Each struct addrinfo
94 * in this list cn be processed by following the ai_next pointer, until a
95 * NULL pointer is encountered. The three members ai_family, ai_socktype,
96 * and ai_protocol in each returned addrinfo structure contain the
97 * corresponding arguments for a call to socket(2). For each addrinfo
98 * structure in the list, the ai_addr member points to a filled-in socket
99 * address structure of length ai_addrlen.
100 *
101 * All of the information returned by getaddrinfo() is dynamically
102 * allocated: the addrinfo structures, and the socket address structures
103 * and canonical host name strings pointed to by the addrinfostructures.
104 * Memory allocated for the dynamically allocated structures created by a
105 * successful call to getaddrinfo() is released by freeaddrinfo().
106 * ai is a pointer to a struct addrinfo created by a call to getaddrinfo().
107 *
108 * \section irsreturn RETURN VALUES
109 *
110 * getaddrinfo() returns zero on success or one of the error codes
111 * listed in gai_strerror() if an error occurs. If both hostname and
112 * servname are NULL getaddrinfo() returns #EAI_NONAME.
113 *
114 * \section irssee SEE ALSO
115 *
116 * getaddrinfo(), freeaddrinfo(),
117 * gai_strerror(), RFC3493, getservbyname(3), connect(2),
118 * sendto(2), sendmsg(2), socket(2).
119 */
120
121 #include <config.h>
122
123 #include <inttypes.h>
124 #include <stdbool.h>
125 #include <stdlib.h>
126 #include <string.h>
127 #include <errno.h>
128
129 #ifdef _WIN32
130 #include <windows.h>
131 #include <winsock2.h>
132 #include <ws2tcpip.h>
133 #endif
134
135 #include <isc/app.h>
136 #include <isc/buffer.h>
137 #include <isc/lib.h>
138 #include <isc/mem.h>
139 #include <isc/print.h>
140 #include <isc/sockaddr.h>
141 #include <isc/string.h>
142 #include <isc/util.h>
143 #include <isc/mutex.h>
144
145 #include <dns/client.h>
146 #include <dns/fixedname.h>
147 #include <dns/name.h>
148 #include <dns/rdata.h>
149 #include <dns/rdataset.h>
150 #include <dns/rdatastruct.h>
151 #include <dns/rdatatype.h>
152 #include <dns/result.h>
153
154 #include <irs/context.h>
155 #include <irs/netdb.h>
156 #include <irs/resconf.h>
157
158 #define SA(addr) ((struct sockaddr *)(addr))
159 #define SIN(addr) ((struct sockaddr_in *)(addr))
160 #define SIN6(addr) ((struct sockaddr_in6 *)(addr))
161 #define SLOCAL(addr) ((struct sockaddr_un *)(addr))
162
163 /*! \struct addrinfo
164 */
165 static struct addrinfo
166 *ai_concat(struct addrinfo *ai1, struct addrinfo *ai2),
167 *ai_reverse(struct addrinfo *oai),
168 *ai_clone(struct addrinfo *oai, int family),
169 *ai_alloc(int family, int addrlen);
170 #ifdef AF_LOCAL
171 static int get_local(const char *name, int socktype, struct addrinfo **res);
172 #endif
173
174 static int
175 resolve_name(int family, const char *hostname, int flags,
176 struct addrinfo **aip, int socktype, int port);
177
178 static int add_ipv4(const char *hostname, int flags, struct addrinfo **aip,
179 int socktype, int port);
180 static int add_ipv6(const char *hostname, int flags, struct addrinfo **aip,
181 int socktype, int port);
182 static void set_order(int, int (**)(const char *, int, struct addrinfo **,
183 int, int));
184 static void _freeaddrinfo(struct addrinfo *ai);
185
186 #define FOUND_IPV4 0x1
187 #define FOUND_IPV6 0x2
188 #define FOUND_MAX 2
189
190 #define ISC_AI_MASK (AI_PASSIVE|AI_CANONNAME|AI_NUMERICHOST)
191 /*%
192 * Get a list of IP addresses and port numbers for host hostname and
193 * service servname.
194 */
195 int
getaddrinfo(const char * hostname,const char * servname,const struct addrinfo * hints,struct addrinfo ** res)196 getaddrinfo(const char *hostname, const char *servname,
197 const struct addrinfo *hints, struct addrinfo **res)
198 {
199 struct servent *sp;
200 const char *proto;
201 int family, socktype, flags, protocol;
202 struct addrinfo *ai, *ai_list;
203 int err = 0;
204 int port, i;
205 int (*net_order[FOUND_MAX+1])(const char *, int, struct addrinfo **,
206 int, int);
207
208 if (hostname == NULL && servname == NULL)
209 return (EAI_NONAME);
210
211 proto = NULL;
212 if (hints != NULL) {
213 if ((hints->ai_flags & ~(ISC_AI_MASK)) != 0)
214 return (EAI_BADFLAGS);
215 if (hints->ai_addrlen || hints->ai_canonname ||
216 hints->ai_addr || hints->ai_next) {
217 errno = EINVAL;
218 return (EAI_SYSTEM);
219 }
220 family = hints->ai_family;
221 socktype = hints->ai_socktype;
222 protocol = hints->ai_protocol;
223 flags = hints->ai_flags;
224 switch (family) {
225 case AF_UNSPEC:
226 switch (hints->ai_socktype) {
227 case SOCK_STREAM:
228 proto = "tcp";
229 break;
230 case SOCK_DGRAM:
231 proto = "udp";
232 break;
233 }
234 break;
235 case AF_INET:
236 case AF_INET6:
237 switch (hints->ai_socktype) {
238 case 0:
239 break;
240 case SOCK_STREAM:
241 proto = "tcp";
242 break;
243 case SOCK_DGRAM:
244 proto = "udp";
245 break;
246 case SOCK_RAW:
247 break;
248 default:
249 return (EAI_SOCKTYPE);
250 }
251 break;
252 #ifdef AF_LOCAL
253 case AF_LOCAL:
254 switch (hints->ai_socktype) {
255 case 0:
256 break;
257 case SOCK_STREAM:
258 break;
259 case SOCK_DGRAM:
260 break;
261 default:
262 return (EAI_SOCKTYPE);
263 }
264 break;
265 #endif
266 default:
267 return (EAI_FAMILY);
268 }
269 } else {
270 protocol = 0;
271 family = 0;
272 socktype = 0;
273 flags = 0;
274 }
275
276 #ifdef AF_LOCAL
277 /*!
278 * First, deal with AF_LOCAL. If the family was not set,
279 * then assume AF_LOCAL if the first character of the
280 * hostname/servname is '/'.
281 */
282
283 if (hostname != NULL &&
284 (family == AF_LOCAL || (family == 0 && *hostname == '/')))
285 return (get_local(hostname, socktype, res));
286
287 if (servname != NULL &&
288 (family == AF_LOCAL || (family == 0 && *servname == '/')))
289 return (get_local(servname, socktype, res));
290 #endif
291
292 /*
293 * Ok, only AF_INET and AF_INET6 left.
294 */
295 ai_list = NULL;
296
297 /*
298 * First, look up the service name (port) if it was
299 * requested. If the socket type wasn't specified, then
300 * try and figure it out.
301 */
302 if (servname != NULL) {
303 char *e;
304
305 port = strtol(servname, &e, 10);
306 if (*e == '\0') {
307 if (socktype == 0) {
308 return (EAI_SOCKTYPE);
309 }
310 if (port < 0 || port > 65535) {
311 return (EAI_SERVICE);
312 }
313 port = htons((unsigned short) port);
314 } else {
315 #ifdef _WIN32
316 WORD wVersionRequested;
317 WSADATA wsaData;
318
319 wVersionRequested = MAKEWORD(2, 0);
320
321 err = WSAStartup(wVersionRequested, &wsaData );
322 if (err != 0) {
323 return (EAI_FAIL);
324 }
325 #endif
326 sp = getservbyname(servname, proto);
327 if (sp != NULL)
328 port = sp->s_port;
329 #ifdef _WIN32
330 WSACleanup();
331 #endif
332 if (sp == NULL) {
333 return (EAI_SERVICE);
334 }
335 if (socktype == 0) {
336 if (strcmp(sp->s_proto, "tcp") == 0) {
337 socktype = SOCK_STREAM;
338 } else if (strcmp(sp->s_proto, "udp") == 0) {
339 socktype = SOCK_DGRAM;
340 }
341 }
342 }
343 } else {
344 port = 0;
345 }
346
347 /*
348 * Next, deal with just a service name, and no hostname.
349 * (we verified that one of them was non-null up above).
350 */
351 if (hostname == NULL && (flags & AI_PASSIVE) != 0) {
352 if (family == AF_INET || family == 0) {
353 ai = ai_alloc(AF_INET, sizeof(struct sockaddr_in));
354 if (ai == NULL)
355 return (EAI_MEMORY);
356 ai->ai_socktype = socktype;
357 ai->ai_protocol = protocol;
358 SIN(ai->ai_addr)->sin_port = port;
359 ai->ai_next = ai_list;
360 ai_list = ai;
361 }
362
363 if (family == AF_INET6 || family == 0) {
364 ai = ai_alloc(AF_INET6, sizeof(struct sockaddr_in6));
365 if (ai == NULL) {
366 _freeaddrinfo(ai_list);
367 return (EAI_MEMORY);
368 }
369 ai->ai_socktype = socktype;
370 ai->ai_protocol = protocol;
371 SIN6(ai->ai_addr)->sin6_port = port;
372 ai->ai_next = ai_list;
373 ai_list = ai;
374 }
375
376 *res = ai_list;
377 return (0);
378 }
379
380 /*
381 * If the family isn't specified or AI_NUMERICHOST specified, check
382 * first to see if it is a numeric address.
383 * Though the gethostbyname2() routine will recognize numeric addresses,
384 * it will only recognize the format that it is being called for. Thus,
385 * a numeric AF_INET address will be treated by the AF_INET6 call as
386 * a domain name, and vice versa. Checking for both numerics here
387 * avoids that.
388 */
389 if (hostname != NULL &&
390 (family == 0 || (flags & AI_NUMERICHOST) != 0)) {
391 char abuf[sizeof(struct in6_addr)];
392 char nbuf[NI_MAXHOST];
393 int addrsize, addroff;
394 #ifdef IRS_HAVE_SIN6_SCOPE_ID
395 char *p, *ep;
396 char ntmp[NI_MAXHOST];
397 uint32_t scopeid;
398 #endif
399
400 #ifdef IRS_HAVE_SIN6_SCOPE_ID
401 /*
402 * Scope identifier portion.
403 */
404 ntmp[0] = '\0';
405 if (strchr(hostname, '%') != NULL) {
406 strlcpy(ntmp, hostname, sizeof(ntmp));
407 p = strchr(ntmp, '%');
408 ep = NULL;
409
410 /*
411 * Vendors may want to support non-numeric
412 * scopeid around here.
413 */
414
415 if (p != NULL)
416 scopeid = (uint32_t)strtoul(p + 1,
417 &ep, 10);
418 if (p != NULL && ep != NULL && ep[0] == '\0')
419 *p = '\0';
420 else {
421 ntmp[0] = '\0';
422 scopeid = 0;
423 }
424 } else
425 scopeid = 0;
426 #endif
427
428 if (inet_pton(AF_INET, hostname, (struct in_addr *)abuf)
429 == 1) {
430 if (family == AF_INET6) {
431 /*
432 * Convert to a V4 mapped address.
433 */
434 struct in6_addr *a6 = (struct in6_addr *)abuf;
435 memmove(&a6->s6_addr[12], &a6->s6_addr[0], 4);
436 memset(&a6->s6_addr[10], 0xff, 2);
437 memset(&a6->s6_addr[0], 0, 10);
438 goto inet6_addr;
439 }
440 addrsize = sizeof(struct in_addr);
441 addroff = offsetof(struct sockaddr_in, sin_addr);
442 family = AF_INET;
443 goto common;
444 #ifdef IRS_HAVE_SIN6_SCOPE_ID
445 } else if (ntmp[0] != '\0' &&
446 inet_pton(AF_INET6, ntmp, abuf) == 1) {
447 if (family && family != AF_INET6)
448 return (EAI_NONAME);
449 addrsize = sizeof(struct in6_addr);
450 addroff = offsetof(struct sockaddr_in6, sin6_addr);
451 family = AF_INET6;
452 goto common;
453 #endif
454 } else if (inet_pton(AF_INET6, hostname, abuf) == 1) {
455 if (family != 0 && family != AF_INET6)
456 return (EAI_NONAME);
457 inet6_addr:
458 addrsize = sizeof(struct in6_addr);
459 addroff = offsetof(struct sockaddr_in6, sin6_addr);
460 family = AF_INET6;
461
462 common:
463 ai = ai_alloc(family,
464 ((family == AF_INET6) ?
465 sizeof(struct sockaddr_in6) :
466 sizeof(struct sockaddr_in)));
467 if (ai == NULL)
468 return (EAI_MEMORY);
469 ai_list = ai;
470 ai->ai_socktype = socktype;
471 SIN(ai->ai_addr)->sin_port = port;
472 memmove((char *)ai->ai_addr + addroff, abuf, addrsize);
473 if ((flags & AI_CANONNAME) != 0) {
474 #ifdef IRS_HAVE_SIN6_SCOPE_ID
475 if (ai->ai_family == AF_INET6)
476 SIN6(ai->ai_addr)->sin6_scope_id =
477 scopeid;
478 #endif
479 if (getnameinfo(ai->ai_addr,
480 (socklen_t)ai->ai_addrlen,
481 nbuf, sizeof(nbuf), NULL, 0,
482 NI_NUMERICHOST) == 0) {
483 ai->ai_canonname = strdup(nbuf);
484 if (ai->ai_canonname == NULL) {
485 _freeaddrinfo(ai);
486 return (EAI_MEMORY);
487 }
488 } else {
489 /* XXX raise error? */
490 ai->ai_canonname = NULL;
491 }
492 }
493 goto done;
494 } else if ((flags & AI_NUMERICHOST) != 0) {
495 return (EAI_NONAME);
496 }
497 }
498
499 if (hostname == NULL && (flags & AI_PASSIVE) == 0) {
500 set_order(family, net_order);
501 for (i = 0; i < FOUND_MAX; i++) {
502 if (net_order[i] == NULL)
503 break;
504 err = (net_order[i])(hostname, flags, &ai_list,
505 socktype, port);
506 if (err != 0) {
507 if (ai_list != NULL) {
508 _freeaddrinfo(ai_list);
509 ai_list = NULL;
510 }
511 break;
512 }
513 }
514 } else
515 err = resolve_name(family, hostname, flags, &ai_list,
516 socktype, port);
517
518 if (ai_list == NULL) {
519 if (err == 0)
520 err = EAI_NONAME;
521 return (err);
522 }
523
524 done:
525 ai_list = ai_reverse(ai_list);
526
527 *res = ai_list;
528 return (0);
529 }
530
531 typedef struct gai_restrans {
532 dns_clientrestrans_t *xid;
533 bool is_inprogress;
534 int error;
535 struct addrinfo ai_sentinel;
536 struct gai_resstate *resstate;
537 } gai_restrans_t;
538
539 typedef struct gai_resstate {
540 isc_mem_t *mctx;
541 struct gai_statehead *head;
542 dns_fixedname_t fixedname;
543 dns_name_t *qname;
544 gai_restrans_t *trans4;
545 gai_restrans_t *trans6;
546 ISC_LINK(struct gai_resstate) link;
547 } gai_resstate_t;
548
549 typedef struct gai_statehead {
550 int ai_family;
551 int ai_flags;
552 int ai_socktype;
553 int ai_port;
554 isc_appctx_t *actx;
555 dns_client_t *dnsclient;
556 isc_mutex_t list_lock;
557 ISC_LIST(struct gai_resstate) resstates;
558 unsigned int activestates;
559 } gai_statehead_t;
560
561 static isc_result_t
make_resstate(isc_mem_t * mctx,gai_statehead_t * head,const char * hostname,const char * domain,gai_resstate_t ** statep)562 make_resstate(isc_mem_t *mctx, gai_statehead_t *head, const char *hostname,
563 const char *domain, gai_resstate_t **statep)
564 {
565 isc_result_t result;
566 gai_resstate_t *state;
567 dns_fixedname_t fixeddomain;
568 dns_name_t *qdomain;
569 unsigned int namelen;
570 isc_buffer_t b;
571 bool need_v4 = false;
572 bool need_v6 = false;
573
574 state = isc_mem_get(mctx, sizeof(*state));
575 if (state == NULL)
576 return (ISC_R_NOMEMORY);
577
578 /* Construct base domain name */
579 namelen = strlen(domain);
580 isc_buffer_constinit(&b, domain, namelen);
581 isc_buffer_add(&b, namelen);
582 qdomain = dns_fixedname_initname(&fixeddomain);
583 result = dns_name_fromtext(qdomain, &b, dns_rootname, 0, NULL);
584 if (result != ISC_R_SUCCESS) {
585 isc_mem_put(mctx, state, sizeof(*state));
586 return (result);
587 }
588
589 /* Construct query name */
590 namelen = strlen(hostname);
591 isc_buffer_constinit(&b, hostname, namelen);
592 isc_buffer_add(&b, namelen);
593 state->qname = dns_fixedname_initname(&state->fixedname);
594 result = dns_name_fromtext(state->qname, &b, qdomain, 0, NULL);
595 if (result != ISC_R_SUCCESS) {
596 isc_mem_put(mctx, state, sizeof(*state));
597 return (result);
598 }
599
600 if (head->ai_family == AF_UNSPEC || head->ai_family == AF_INET)
601 need_v4 = true;
602 if (head->ai_family == AF_UNSPEC || head->ai_family == AF_INET6)
603 need_v6 = true;
604
605 state->trans6 = NULL;
606 state->trans4 = NULL;
607 if (need_v4) {
608 state->trans4 = isc_mem_get(mctx, sizeof(gai_restrans_t));
609 if (state->trans4 == NULL) {
610 isc_mem_put(mctx, state, sizeof(*state));
611 return (ISC_R_NOMEMORY);
612 }
613 state->trans4->error = 0;
614 state->trans4->xid = NULL;
615 state->trans4->resstate = state;
616 state->trans4->is_inprogress = true;
617 state->trans4->ai_sentinel.ai_next = NULL;
618 }
619 if (need_v6) {
620 state->trans6 = isc_mem_get(mctx, sizeof(gai_restrans_t));
621 if (state->trans6 == NULL) {
622 if (state->trans4 != NULL)
623 isc_mem_put(mctx, state->trans4,
624 sizeof(*state->trans4));
625 isc_mem_put(mctx, state, sizeof(*state));
626 return (ISC_R_NOMEMORY);
627 }
628 state->trans6->error = 0;
629 state->trans6->xid = NULL;
630 state->trans6->resstate = state;
631 state->trans6->is_inprogress = true;
632 state->trans6->ai_sentinel.ai_next = NULL;
633 }
634
635 state->mctx = mctx;
636 state->head = head;
637 ISC_LINK_INIT(state, link);
638
639 *statep = state;
640
641 return (ISC_R_SUCCESS);
642 }
643
644 static isc_result_t
make_resstates(isc_mem_t * mctx,const char * hostname,gai_statehead_t * head,irs_resconf_t * resconf)645 make_resstates(isc_mem_t *mctx, const char *hostname, gai_statehead_t *head,
646 irs_resconf_t *resconf)
647 {
648 isc_result_t result;
649 irs_resconf_searchlist_t *searchlist;
650 irs_resconf_search_t *searchent;
651 gai_resstate_t *resstate, *resstate0;
652
653 resstate0 = NULL;
654 result = make_resstate(mctx, head, hostname, ".", &resstate0);
655 if (result != ISC_R_SUCCESS)
656 return (result);
657
658 searchlist = irs_resconf_getsearchlist(resconf);
659 for (searchent = ISC_LIST_HEAD(*searchlist); searchent != NULL;
660 searchent = ISC_LIST_NEXT(searchent, link)) {
661 resstate = NULL;
662 result = make_resstate(mctx, head, hostname,
663 (const char *)searchent->domain,
664 &resstate);
665 if (result != ISC_R_SUCCESS)
666 break;
667
668 ISC_LIST_APPEND(head->resstates, resstate, link);
669 head->activestates++;
670 }
671
672 /*
673 * Insert the original hostname either at the head or the tail of the
674 * state list, depending on the number of labels contained in the
675 * original name and the 'ndots' configuration parameter.
676 */
677 if (dns_name_countlabels(resstate0->qname) >
678 irs_resconf_getndots(resconf) + 1) {
679 ISC_LIST_PREPEND(head->resstates, resstate0, link);
680 } else
681 ISC_LIST_APPEND(head->resstates, resstate0, link);
682 head->activestates++;
683
684 if (result != ISC_R_SUCCESS) {
685 while ((resstate = ISC_LIST_HEAD(head->resstates)) != NULL) {
686 ISC_LIST_UNLINK(head->resstates, resstate, link);
687 if (resstate->trans4 != NULL) {
688 isc_mem_put(mctx, resstate->trans4,
689 sizeof(*resstate->trans4));
690 }
691 if (resstate->trans6 != NULL) {
692 isc_mem_put(mctx, resstate->trans6,
693 sizeof(*resstate->trans6));
694 }
695
696 isc_mem_put(mctx, resstate, sizeof(*resstate));
697 }
698 }
699
700 return (result);
701 }
702
703 static void
process_answer(isc_task_t * task,isc_event_t * event)704 process_answer(isc_task_t *task, isc_event_t *event) {
705 int error = 0, family;
706 gai_restrans_t *trans = event->ev_arg;
707 gai_resstate_t *resstate;
708 dns_clientresevent_t *rev = (dns_clientresevent_t *)event;
709 dns_rdatatype_t qtype;
710 dns_name_t *name;
711 bool wantcname;
712
713 REQUIRE(trans != NULL);
714 resstate = trans->resstate;
715 REQUIRE(resstate != NULL);
716 REQUIRE(task != NULL);
717
718 if (trans == resstate->trans4) {
719 family = AF_INET;
720 qtype = dns_rdatatype_a;
721 } else {
722 INSIST(trans == resstate->trans6);
723 family = AF_INET6;
724 qtype = dns_rdatatype_aaaa;
725 }
726
727 INSIST(trans->is_inprogress);
728 trans->is_inprogress = false;
729
730 switch (rev->result) {
731 case ISC_R_SUCCESS:
732 case DNS_R_NCACHENXDOMAIN: /* treat this as a fatal error? */
733 case DNS_R_NCACHENXRRSET:
734 break;
735 default:
736 switch (rev->vresult) {
737 case DNS_R_SIGINVALID:
738 case DNS_R_SIGEXPIRED:
739 case DNS_R_SIGFUTURE:
740 case DNS_R_KEYUNAUTHORIZED:
741 case DNS_R_MUSTBESECURE:
742 case DNS_R_COVERINGNSEC:
743 case DNS_R_NOTAUTHORITATIVE:
744 case DNS_R_NOVALIDKEY:
745 case DNS_R_NOVALIDDS:
746 case DNS_R_NOVALIDSIG:
747 error = EAI_INSECUREDATA;
748 break;
749 default:
750 error = EAI_FAIL;
751 }
752 goto done;
753 }
754
755 wantcname = ((resstate->head->ai_flags & AI_CANONNAME) != 0);
756
757 /* Parse the response and construct the addrinfo chain */
758 for (name = ISC_LIST_HEAD(rev->answerlist); name != NULL;
759 name = ISC_LIST_NEXT(name, link)) {
760 isc_result_t result;
761 dns_rdataset_t *rdataset;
762 char cname[1024];
763
764 if (wantcname) {
765 isc_buffer_t b;
766
767 isc_buffer_init(&b, cname, sizeof(cname));
768 result = dns_name_totext(name, true, &b);
769 if (result != ISC_R_SUCCESS) {
770 error = EAI_FAIL;
771 goto done;
772 }
773 isc_buffer_putuint8(&b, '\0');
774 }
775
776 for (rdataset = ISC_LIST_HEAD(name->list);
777 rdataset != NULL;
778 rdataset = ISC_LIST_NEXT(rdataset, link)) {
779 if (!dns_rdataset_isassociated(rdataset))
780 continue;
781 if (rdataset->type != qtype)
782 continue;
783
784 for (result = dns_rdataset_first(rdataset);
785 result == ISC_R_SUCCESS;
786 result = dns_rdataset_next(rdataset)) {
787 struct addrinfo *ai;
788 dns_rdata_t rdata;
789 dns_rdata_in_a_t rdata_a;
790 dns_rdata_in_aaaa_t rdata_aaaa;
791
792 ai = ai_alloc(family,
793 ((family == AF_INET6) ?
794 sizeof(struct sockaddr_in6) :
795 sizeof(struct sockaddr_in)));
796 if (ai == NULL) {
797 error = EAI_MEMORY;
798 goto done;
799 }
800 ai->ai_socktype = resstate->head->ai_socktype;
801 ai->ai_next = trans->ai_sentinel.ai_next;
802 trans->ai_sentinel.ai_next = ai;
803
804 /*
805 * Set AF-specific parameters
806 * (IPv4/v6 address/port)
807 */
808 dns_rdata_init(&rdata);
809 switch (family) {
810 case AF_INET:
811 dns_rdataset_current(rdataset, &rdata);
812 result = dns_rdata_tostruct(&rdata,
813 &rdata_a,
814 NULL);
815 RUNTIME_CHECK(result == ISC_R_SUCCESS);
816 SIN(ai->ai_addr)->sin_port =
817 resstate->head->ai_port;
818 memmove(&SIN(ai->ai_addr)->sin_addr,
819 &rdata_a.in_addr, 4);
820 dns_rdata_freestruct(&rdata_a);
821 break;
822 case AF_INET6:
823 dns_rdataset_current(rdataset, &rdata);
824 result = dns_rdata_tostruct(&rdata,
825 &rdata_aaaa,
826 NULL);
827 RUNTIME_CHECK(result == ISC_R_SUCCESS);
828 SIN6(ai->ai_addr)->sin6_port =
829 resstate->head->ai_port;
830 memmove(&SIN6(ai->ai_addr)->sin6_addr,
831 &rdata_aaaa.in6_addr, 16);
832 dns_rdata_freestruct(&rdata_aaaa);
833 break;
834 }
835
836 if (wantcname) {
837 ai->ai_canonname = strdup(cname);
838 if (ai->ai_canonname == NULL) {
839 error = EAI_MEMORY;
840 goto done;
841 }
842 }
843 }
844 }
845 }
846
847 done:
848 dns_client_freeresanswer(resstate->head->dnsclient, &rev->answerlist);
849 dns_client_destroyrestrans(&trans->xid);
850
851 isc_event_free(&event);
852
853 /* Make sure that error == 0 iff we have a non-empty list */
854 if (error == 0) {
855 if (trans->ai_sentinel.ai_next == NULL)
856 error = EAI_NONAME;
857 } else {
858 if (trans->ai_sentinel.ai_next != NULL) {
859 _freeaddrinfo(trans->ai_sentinel.ai_next);
860 trans->ai_sentinel.ai_next = NULL;
861 }
862 }
863 trans->error = error;
864
865 /* Check whether we are done */
866 if ((resstate->trans4 == NULL || !resstate->trans4->is_inprogress) &&
867 (resstate->trans6 == NULL || !resstate->trans6->is_inprogress)) {
868 /*
869 * We're done for this state. If there is no other outstanding
870 * state, we can exit.
871 */
872 resstate->head->activestates--;
873 if (resstate->head->activestates == 0) {
874 isc_app_ctxsuspend(resstate->head->actx);
875 return;
876 }
877
878 /*
879 * There are outstanding states, but if we are at the head
880 * of the state list (i.e., at the highest search priority)
881 * and have any answer, we can stop now by canceling the
882 * others.
883 */
884 LOCK(&resstate->head->list_lock);
885 if (resstate == ISC_LIST_HEAD(resstate->head->resstates)) {
886 if ((resstate->trans4 != NULL &&
887 resstate->trans4->ai_sentinel.ai_next != NULL) ||
888 (resstate->trans6 != NULL &&
889 resstate->trans6->ai_sentinel.ai_next != NULL)) {
890 gai_resstate_t *rest;
891
892 for (rest = ISC_LIST_NEXT(resstate, link);
893 rest != NULL;
894 rest = ISC_LIST_NEXT(rest, link)) {
895 if (rest->trans4 != NULL &&
896 rest->trans4->xid != NULL)
897 dns_client_cancelresolve(
898 rest->trans4->xid);
899 if (rest->trans6 != NULL &&
900 rest->trans6->xid != NULL)
901 dns_client_cancelresolve(
902 rest->trans6->xid);
903 }
904 } else {
905 /*
906 * This search fails, so we move to the tail
907 * of the list so that the next entry will
908 * have the highest priority.
909 */
910 ISC_LIST_UNLINK(resstate->head->resstates,
911 resstate, link);
912 ISC_LIST_APPEND(resstate->head->resstates,
913 resstate, link);
914 }
915 }
916 UNLOCK(&resstate->head->list_lock);
917 }
918 }
919
920 static int
resolve_name(int family,const char * hostname,int flags,struct addrinfo ** aip,int socktype,int port)921 resolve_name(int family, const char *hostname, int flags,
922 struct addrinfo **aip, int socktype, int port)
923 {
924 isc_result_t result;
925 irs_context_t *irsctx;
926 irs_resconf_t *conf;
927 isc_mem_t *mctx;
928 isc_appctx_t *actx;
929 isc_task_t *task;
930 int terror = 0;
931 int error = 0;
932 dns_client_t *client;
933 gai_resstate_t *resstate;
934 gai_statehead_t head;
935 bool all_fail = true;
936
937 /* get IRS context and the associated parameters */
938 irsctx = NULL;
939 result = irs_context_get(&irsctx);
940 if (result != ISC_R_SUCCESS)
941 return (EAI_FAIL);
942 actx = irs_context_getappctx(irsctx);
943
944 mctx = irs_context_getmctx(irsctx);
945 task = irs_context_gettask(irsctx);
946 conf = irs_context_getresconf(irsctx);
947 client = irs_context_getdnsclient(irsctx);
948
949 /* construct resolution states */
950 head.activestates = 0;
951 head.ai_family = family;
952 head.ai_socktype = socktype;
953 head.ai_flags = flags;
954 head.ai_port = port;
955 head.actx = actx;
956 head.dnsclient = client;
957 result = isc_mutex_init(&head.list_lock);
958 if (result != ISC_R_SUCCESS) {
959 return (EAI_FAIL);
960 }
961
962 ISC_LIST_INIT(head.resstates);
963 result = make_resstates(mctx, hostname, &head, conf);
964 if (result != ISC_R_SUCCESS) {
965 DESTROYLOCK(&head.list_lock);
966 return (EAI_FAIL);
967 }
968
969 LOCK(&head.list_lock);
970 for (resstate = ISC_LIST_HEAD(head.resstates);
971 resstate != NULL; resstate = ISC_LIST_NEXT(resstate, link)) {
972 if (resstate->trans4 != NULL) {
973 result = dns_client_startresolve(client,
974 resstate->qname,
975 dns_rdataclass_in,
976 dns_rdatatype_a,
977 0, task,
978 process_answer,
979 resstate->trans4,
980 &resstate->trans4->xid);
981 if (result == ISC_R_SUCCESS) {
982 resstate->trans4->is_inprogress = true;
983 all_fail = false;
984 } else
985 resstate->trans4->is_inprogress = false;
986 }
987 if (resstate->trans6 != NULL) {
988 result = dns_client_startresolve(client,
989 resstate->qname,
990 dns_rdataclass_in,
991 dns_rdatatype_aaaa,
992 0, task,
993 process_answer,
994 resstate->trans6,
995 &resstate->trans6->xid);
996 if (result == ISC_R_SUCCESS) {
997 resstate->trans6->is_inprogress = true;
998 all_fail = false;
999 } else
1000 resstate->trans6->is_inprogress= false;
1001 }
1002 }
1003 UNLOCK(&head.list_lock);
1004
1005 if (!all_fail) {
1006 /* Start all the events */
1007 isc_app_ctxrun(actx);
1008 } else
1009 error = EAI_FAIL;
1010
1011 /* Cleanup */
1012 while ((resstate = ISC_LIST_HEAD(head.resstates)) != NULL) {
1013 int terror4 = 0, terror6 = 0;
1014
1015 ISC_LIST_UNLINK(head.resstates, resstate, link);
1016
1017 if (*aip == NULL) {
1018 struct addrinfo *sentinel4 = NULL;
1019 struct addrinfo *sentinel6 = NULL;
1020
1021 if (resstate->trans4 != NULL) {
1022 sentinel4 =
1023 resstate->trans4->ai_sentinel.ai_next;
1024 resstate->trans4->ai_sentinel.ai_next = NULL;
1025 }
1026 if (resstate->trans6 != NULL) {
1027 sentinel6 =
1028 resstate->trans6->ai_sentinel.ai_next;
1029 resstate->trans6->ai_sentinel.ai_next = NULL;
1030 }
1031 *aip = ai_concat(sentinel4, sentinel6);
1032 }
1033
1034 if (resstate->trans4 != NULL) {
1035 INSIST(resstate->trans4->xid == NULL);
1036 terror4 = resstate->trans4->error;
1037 isc_mem_put(mctx, resstate->trans4,
1038 sizeof(*resstate->trans4));
1039 }
1040 if (resstate->trans6 != NULL) {
1041 INSIST(resstate->trans6->xid == NULL);
1042 terror6 = resstate->trans6->error;
1043 isc_mem_put(mctx, resstate->trans6,
1044 sizeof(*resstate->trans6));
1045 }
1046
1047 /*
1048 * If the entire lookup fails, we need to choose an appropriate
1049 * error code from individual codes. We'll try to provide as
1050 * specific a code as possible. In general, we are going to
1051 * find an error code other than EAI_NONAME (which is too
1052 * generic and may actually not be problematic in some cases).
1053 * EAI_NONAME will be set below if no better code is found.
1054 */
1055 if (terror == 0 || terror == EAI_NONAME) {
1056 if (terror4 != 0 && terror4 != EAI_NONAME)
1057 terror = terror4;
1058 else if (terror6 != 0 && terror6 != EAI_NONAME)
1059 terror = terror6;
1060 }
1061
1062 isc_mem_put(mctx, resstate, sizeof(*resstate));
1063 }
1064
1065 if (*aip == NULL) {
1066 error = terror;
1067 if (error == 0)
1068 error = EAI_NONAME;
1069 }
1070
1071 #if 1 /* XXX: enabled for finding leaks. should be cleaned up later. */
1072 isc_app_ctxfinish(actx);
1073 irs_context_destroy(&irsctx);
1074 #endif
1075
1076 DESTROYLOCK(&head.list_lock);
1077 return (error);
1078 }
1079
1080 static char *
irs_strsep(char ** stringp,const char * delim)1081 irs_strsep(char **stringp, const char *delim) {
1082 char *string = *stringp;
1083 char *s;
1084 const char *d;
1085 char sc, dc;
1086
1087 if (string == NULL)
1088 return (NULL);
1089
1090 for (s = string; *s != '\0'; s++) {
1091 sc = *s;
1092 for (d = delim; (dc = *d) != '\0'; d++)
1093 if (sc == dc) {
1094 *s++ = '\0';
1095 *stringp = s;
1096 return (string);
1097 }
1098 }
1099 *stringp = NULL;
1100 return (string);
1101 }
1102
1103 static void
set_order(int family,int (** net_order)(const char *,int,struct addrinfo **,int,int))1104 set_order(int family, int (**net_order)(const char *, int, struct addrinfo **,
1105 int, int))
1106 {
1107 char *order, *tok;
1108 int found;
1109
1110 if (family) {
1111 switch (family) {
1112 case AF_INET:
1113 *net_order++ = add_ipv4;
1114 break;
1115 case AF_INET6:
1116 *net_order++ = add_ipv6;
1117 break;
1118 }
1119 } else {
1120 order = getenv("NET_ORDER");
1121 found = 0;
1122 while (order != NULL) {
1123 /*
1124 * We ignore any unknown names.
1125 */
1126 tok = irs_strsep(&order, ":");
1127 if (strcasecmp(tok, "inet6") == 0) {
1128 if ((found & FOUND_IPV6) == 0)
1129 *net_order++ = add_ipv6;
1130 found |= FOUND_IPV6;
1131 } else if (strcasecmp(tok, "inet") == 0 ||
1132 strcasecmp(tok, "inet4") == 0) {
1133 if ((found & FOUND_IPV4) == 0)
1134 *net_order++ = add_ipv4;
1135 found |= FOUND_IPV4;
1136 }
1137 }
1138
1139 /*
1140 * Add in anything that we didn't find.
1141 */
1142 if ((found & FOUND_IPV4) == 0)
1143 *net_order++ = add_ipv4;
1144 if ((found & FOUND_IPV6) == 0)
1145 *net_order++ = add_ipv6;
1146 }
1147 *net_order = NULL;
1148 return;
1149 }
1150
1151 static char v4_loop[4] = { 127, 0, 0, 1 };
1152
1153 static int
add_ipv4(const char * hostname,int flags,struct addrinfo ** aip,int socktype,int port)1154 add_ipv4(const char *hostname, int flags, struct addrinfo **aip,
1155 int socktype, int port)
1156 {
1157 struct addrinfo *ai;
1158
1159 UNUSED(hostname);
1160 UNUSED(flags);
1161
1162 ai = ai_clone(*aip, AF_INET); /* don't use ai_clone() */
1163 if (ai == NULL)
1164 return (EAI_MEMORY);
1165
1166 *aip = ai;
1167 ai->ai_socktype = socktype;
1168 SIN(ai->ai_addr)->sin_port = port;
1169 memmove(&SIN(ai->ai_addr)->sin_addr, v4_loop, 4);
1170
1171 return (0);
1172 }
1173
1174 static char v6_loop[16] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 };
1175
1176 static int
add_ipv6(const char * hostname,int flags,struct addrinfo ** aip,int socktype,int port)1177 add_ipv6(const char *hostname, int flags, struct addrinfo **aip,
1178 int socktype, int port)
1179 {
1180 struct addrinfo *ai;
1181
1182 UNUSED(hostname);
1183 UNUSED(flags);
1184
1185 ai = ai_clone(*aip, AF_INET6); /* don't use ai_clone() */
1186 if (ai == NULL)
1187 return (EAI_MEMORY);
1188
1189 *aip = ai;
1190 ai->ai_socktype = socktype;
1191 SIN6(ai->ai_addr)->sin6_port = port;
1192 memmove(&SIN6(ai->ai_addr)->sin6_addr, v6_loop, 16);
1193
1194 return (0);
1195 }
1196
1197 /*% Free address info. */
1198 void
freeaddrinfo(struct addrinfo * ai)1199 freeaddrinfo(struct addrinfo *ai) {
1200 _freeaddrinfo(ai);
1201 }
1202
1203 static void
_freeaddrinfo(struct addrinfo * ai)1204 _freeaddrinfo(struct addrinfo *ai) {
1205 struct addrinfo *ai_next;
1206
1207 while (ai != NULL) {
1208 ai_next = ai->ai_next;
1209 if (ai->ai_addr != NULL)
1210 free(ai->ai_addr);
1211 if (ai->ai_canonname)
1212 free(ai->ai_canonname);
1213 free(ai);
1214 ai = ai_next;
1215 }
1216 }
1217
1218 #ifdef AF_LOCAL
1219 static int
get_local(const char * name,int socktype,struct addrinfo ** res)1220 get_local(const char *name, int socktype, struct addrinfo **res) {
1221 struct addrinfo *ai;
1222 struct sockaddr_un *slocal;
1223
1224 if (socktype == 0)
1225 return (EAI_SOCKTYPE);
1226
1227 ai = ai_alloc(AF_LOCAL, sizeof(*slocal));
1228 if (ai == NULL)
1229 return (EAI_MEMORY);
1230
1231 slocal = SLOCAL(ai->ai_addr);
1232 strlcpy(slocal->sun_path, name, sizeof(slocal->sun_path));
1233
1234 ai->ai_socktype = socktype;
1235 /*
1236 * ai->ai_flags, ai->ai_protocol, ai->ai_canonname,
1237 * and ai->ai_next were initialized to zero.
1238 */
1239
1240 *res = ai;
1241 return (0);
1242 }
1243 #endif
1244
1245 /*!
1246 * Allocate an addrinfo structure, and a sockaddr structure
1247 * of the specified length. We initialize:
1248 * ai_addrlen
1249 * ai_family
1250 * ai_addr
1251 * ai_addr->sa_family
1252 * ai_addr->sa_len (IRS_PLATFORM_HAVESALEN)
1253 * and everything else is initialized to zero.
1254 */
1255 static struct addrinfo *
ai_alloc(int family,int addrlen)1256 ai_alloc(int family, int addrlen) {
1257 struct addrinfo *ai;
1258
1259 ai = (struct addrinfo *)calloc(1, sizeof(*ai));
1260 if (ai == NULL)
1261 return (NULL);
1262
1263 ai->ai_addr = SA(calloc(1, addrlen));
1264 if (ai->ai_addr == NULL) {
1265 free(ai);
1266 return (NULL);
1267 }
1268 ai->ai_addrlen = addrlen;
1269 ai->ai_family = family;
1270 ai->ai_addr->sa_family = family;
1271 #ifdef IRS_PLATFORM_HAVESALEN
1272 ai->ai_addr->sa_len = addrlen;
1273 #endif
1274 return (ai);
1275 }
1276
1277 static struct addrinfo *
ai_clone(struct addrinfo * oai,int family)1278 ai_clone(struct addrinfo *oai, int family) {
1279 struct addrinfo *ai;
1280
1281 ai = ai_alloc(family, ((family == AF_INET6) ?
1282 sizeof(struct sockaddr_in6) : sizeof(struct sockaddr_in)));
1283
1284 if (ai == NULL)
1285 return (NULL);
1286 if (oai == NULL)
1287 return (ai);
1288
1289 ai->ai_flags = oai->ai_flags;
1290 ai->ai_socktype = oai->ai_socktype;
1291 ai->ai_protocol = oai->ai_protocol;
1292 ai->ai_canonname = NULL;
1293 ai->ai_next = oai;
1294 return (ai);
1295 }
1296
1297 static struct addrinfo *
ai_reverse(struct addrinfo * oai)1298 ai_reverse(struct addrinfo *oai) {
1299 struct addrinfo *nai, *tai;
1300
1301 nai = NULL;
1302
1303 while (oai != NULL) {
1304 /*
1305 * Grab one off the old list.
1306 */
1307 tai = oai;
1308 oai = oai->ai_next;
1309 /*
1310 * Put it on the front of the new list.
1311 */
1312 tai->ai_next = nai;
1313 nai = tai;
1314 }
1315 return (nai);
1316 }
1317
1318
1319 static struct addrinfo *
ai_concat(struct addrinfo * ai1,struct addrinfo * ai2)1320 ai_concat(struct addrinfo *ai1, struct addrinfo *ai2) {
1321 struct addrinfo *ai_tmp;
1322
1323 if (ai1 == NULL)
1324 return (ai2);
1325 else if (ai2 == NULL)
1326 return (ai1);
1327
1328 for (ai_tmp = ai1; ai_tmp != NULL && ai_tmp->ai_next != NULL;
1329 ai_tmp = ai_tmp->ai_next)
1330 ;
1331
1332 ai_tmp->ai_next = ai2;
1333
1334 return (ai1);
1335 }
1336