diff options
Diffstat (limited to 'src/lib/libc/net/getaddrinfo.c')
-rw-r--r-- | src/lib/libc/net/getaddrinfo.c | 1757 |
1 files changed, 1757 insertions, 0 deletions
diff --git a/src/lib/libc/net/getaddrinfo.c b/src/lib/libc/net/getaddrinfo.c new file mode 100644 index 0000000000..7040fa7c37 --- /dev/null +++ b/src/lib/libc/net/getaddrinfo.c | |||
@@ -0,0 +1,1757 @@ | |||
1 | /* $OpenBSD: getaddrinfo.c,v 1.71 2009/11/18 07:43:22 guenther Exp $ */ | ||
2 | /* $KAME: getaddrinfo.c,v 1.31 2000/08/31 17:36:43 itojun Exp $ */ | ||
3 | |||
4 | /* | ||
5 | * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project. | ||
6 | * All rights reserved. | ||
7 | * | ||
8 | * Redistribution and use in source and binary forms, with or without | ||
9 | * modification, are permitted provided that the following conditions | ||
10 | * are met: | ||
11 | * 1. Redistributions of source code must retain the above copyright | ||
12 | * notice, this list of conditions and the following disclaimer. | ||
13 | * 2. Redistributions in binary form must reproduce the above copyright | ||
14 | * notice, this list of conditions and the following disclaimer in the | ||
15 | * documentation and/or other materials provided with the distribution. | ||
16 | * 3. Neither the name of the project nor the names of its contributors | ||
17 | * may be used to endorse or promote products derived from this software | ||
18 | * without specific prior written permission. | ||
19 | * | ||
20 | * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND | ||
21 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
22 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
23 | * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE | ||
24 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
25 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
26 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
27 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
28 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
29 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
30 | * SUCH DAMAGE. | ||
31 | */ | ||
32 | |||
33 | /* | ||
34 | * Issues to be discussed: | ||
35 | * - Thread safe-ness must be checked. | ||
36 | * - Return values. There are nonstandard return values defined and used | ||
37 | * in the source code. This is because RFC2553 is silent about which error | ||
38 | * code must be returned for which situation. | ||
39 | * - IPv4 classful (shortened) form. RFC2553 is silent about it. XNET 5.2 | ||
40 | * says to use inet_aton() to convert IPv4 numeric to binary (allows | ||
41 | * classful form as a result). | ||
42 | * current code - disallow classful form for IPv4 (due to use of inet_pton). | ||
43 | * - freeaddrinfo(NULL). RFC2553 is silent about it. XNET 5.2 says it is | ||
44 | * invalid. | ||
45 | * current code - SEGV on freeaddrinfo(NULL) | ||
46 | * Note: | ||
47 | * - We use getipnodebyname() just for thread-safeness. There's no intent | ||
48 | * to let it do PF_UNSPEC (actually we never pass PF_UNSPEC to | ||
49 | * getipnodebyname(). | ||
50 | * - The code filters out AFs that are not supported by the kernel, | ||
51 | * when globbing NULL hostname (to loopback, or wildcard). Is it the right | ||
52 | * thing to do? What is the relationship with post-RFC2553 AI_ADDRCONFIG | ||
53 | * in ai_flags? | ||
54 | * - (post-2553) semantics of AI_ADDRCONFIG itself is too vague. | ||
55 | * (1) what should we do against numeric hostname (2) what should we do | ||
56 | * against NULL hostname (3) what is AI_ADDRCONFIG itself. AF not ready? | ||
57 | * non-loopback address configured? global address configured? | ||
58 | * - To avoid search order issue, we have a big amount of code duplicate | ||
59 | * from gethnamaddr.c and some other places. The issues that there's no | ||
60 | * lower layer function to lookup "IPv4 or IPv6" record. Calling | ||
61 | * gethostbyname2 from getaddrinfo will end up in wrong search order, as | ||
62 | * follows: | ||
63 | * - The code makes use of following calls when asked to resolver with | ||
64 | * ai_family = PF_UNSPEC: | ||
65 | * getipnodebyname(host, AF_INET6); | ||
66 | * getipnodebyname(host, AF_INET); | ||
67 | * This will result in the following queries if the node is configure to | ||
68 | * prefer /etc/hosts than DNS: | ||
69 | * lookup /etc/hosts for IPv6 address | ||
70 | * lookup DNS for IPv6 address | ||
71 | * lookup /etc/hosts for IPv4 address | ||
72 | * lookup DNS for IPv4 address | ||
73 | * which may not meet people's requirement. | ||
74 | * The right thing to happen is to have underlying layer which does | ||
75 | * PF_UNSPEC lookup (lookup both) and return chain of addrinfos. | ||
76 | * This would result in a bit of code duplicate with _dns_ghbyname() and | ||
77 | * friends. | ||
78 | */ | ||
79 | |||
80 | #ifndef INET6 | ||
81 | #define INET6 | ||
82 | #endif | ||
83 | |||
84 | #include <sys/types.h> | ||
85 | #include <sys/param.h> | ||
86 | #include <sys/socket.h> | ||
87 | #include <net/if.h> | ||
88 | #include <netinet/in.h> | ||
89 | #include <arpa/inet.h> | ||
90 | #include <arpa/nameser.h> | ||
91 | #include <netdb.h> | ||
92 | #include <resolv.h> | ||
93 | #include <string.h> | ||
94 | #include <stdint.h> | ||
95 | #include <stdlib.h> | ||
96 | #include <stddef.h> | ||
97 | #include <ctype.h> | ||
98 | #include <unistd.h> | ||
99 | #include <stdio.h> | ||
100 | #include <errno.h> | ||
101 | |||
102 | #include <syslog.h> | ||
103 | #include <stdarg.h> | ||
104 | |||
105 | #ifdef YP | ||
106 | #include <rpc/rpc.h> | ||
107 | #include <rpcsvc/yp.h> | ||
108 | #include <rpcsvc/ypclnt.h> | ||
109 | #include "ypinternal.h" | ||
110 | #endif | ||
111 | |||
112 | #include "thread_private.h" | ||
113 | |||
114 | #define SUCCESS 0 | ||
115 | #define ANY 0 | ||
116 | #define YES 1 | ||
117 | #define NO 0 | ||
118 | |||
119 | static const char in_addrany[] = { 0, 0, 0, 0 }; | ||
120 | static const char in6_addrany[] = { | ||
121 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 | ||
122 | }; | ||
123 | static const char in_loopback[] = { 127, 0, 0, 1 }; | ||
124 | static const char in6_loopback[] = { | ||
125 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 | ||
126 | }; | ||
127 | |||
128 | static const struct afd { | ||
129 | int a_af; | ||
130 | int a_addrlen; | ||
131 | int a_socklen; | ||
132 | int a_off; | ||
133 | const char *a_addrany; | ||
134 | const char *a_loopback; | ||
135 | int a_scoped; | ||
136 | } afdl [] = { | ||
137 | #ifdef INET6 | ||
138 | {PF_INET6, sizeof(struct in6_addr), | ||
139 | sizeof(struct sockaddr_in6), | ||
140 | offsetof(struct sockaddr_in6, sin6_addr), | ||
141 | in6_addrany, in6_loopback, 1}, | ||
142 | #endif | ||
143 | {PF_INET, sizeof(struct in_addr), | ||
144 | sizeof(struct sockaddr_in), | ||
145 | offsetof(struct sockaddr_in, sin_addr), | ||
146 | in_addrany, in_loopback, 0}, | ||
147 | {0, 0, 0, 0, NULL, NULL, 0}, | ||
148 | }; | ||
149 | |||
150 | struct explore { | ||
151 | int e_af; | ||
152 | int e_socktype; | ||
153 | int e_protocol; | ||
154 | const char *e_protostr; | ||
155 | int e_wild; | ||
156 | #define WILD_AF(ex) ((ex)->e_wild & 0x01) | ||
157 | #define WILD_SOCKTYPE(ex) ((ex)->e_wild & 0x02) | ||
158 | #define WILD_PROTOCOL(ex) ((ex)->e_wild & 0x04) | ||
159 | }; | ||
160 | |||
161 | static const struct explore explore[] = { | ||
162 | #if 0 | ||
163 | { PF_LOCAL, 0, ANY, ANY, NULL, 0x01 }, | ||
164 | #endif | ||
165 | #ifdef INET6 | ||
166 | { PF_INET6, SOCK_DGRAM, IPPROTO_UDP, "udp", 0x07 }, | ||
167 | { PF_INET6, SOCK_STREAM, IPPROTO_TCP, "tcp", 0x07 }, | ||
168 | { PF_INET6, SOCK_RAW, ANY, NULL, 0x05 }, | ||
169 | #endif | ||
170 | { PF_INET, SOCK_DGRAM, IPPROTO_UDP, "udp", 0x07 }, | ||
171 | { PF_INET, SOCK_STREAM, IPPROTO_TCP, "tcp", 0x07 }, | ||
172 | { PF_INET, SOCK_RAW, ANY, NULL, 0x05 }, | ||
173 | { PF_UNSPEC, SOCK_DGRAM, IPPROTO_UDP, "udp", 0x07 }, | ||
174 | { PF_UNSPEC, SOCK_STREAM, IPPROTO_TCP, "tcp", 0x07 }, | ||
175 | { PF_UNSPEC, SOCK_RAW, ANY, NULL, 0x05 }, | ||
176 | { -1, 0, 0, NULL, 0 }, | ||
177 | }; | ||
178 | |||
179 | #ifdef INET6 | ||
180 | #define PTON_MAX 16 | ||
181 | #else | ||
182 | #define PTON_MAX 4 | ||
183 | #endif | ||
184 | |||
185 | #define MAXPACKET (64*1024) | ||
186 | |||
187 | typedef union { | ||
188 | HEADER hdr; | ||
189 | u_char buf[MAXPACKET]; | ||
190 | } querybuf; | ||
191 | |||
192 | struct res_target { | ||
193 | struct res_target *next; | ||
194 | const char *name; /* domain name */ | ||
195 | int qclass, qtype; /* class and type of query */ | ||
196 | u_char *answer; /* buffer to put answer */ | ||
197 | int anslen; /* size of answer buffer */ | ||
198 | int n; /* result length */ | ||
199 | }; | ||
200 | |||
201 | static int explore_fqdn(const struct addrinfo *, const char *, | ||
202 | const char *, struct addrinfo **); | ||
203 | static int explore_null(const struct addrinfo *, | ||
204 | const char *, struct addrinfo **); | ||
205 | static int explore_numeric(const struct addrinfo *, const char *, | ||
206 | const char *, struct addrinfo **, const char *); | ||
207 | static int explore_numeric_scope(const struct addrinfo *, const char *, | ||
208 | const char *, struct addrinfo **); | ||
209 | static int get_canonname(const struct addrinfo *, | ||
210 | struct addrinfo *, const char *); | ||
211 | static struct addrinfo *get_ai(const struct addrinfo *, | ||
212 | const struct afd *, const char *); | ||
213 | static int get_portmatch(const struct addrinfo *, const char *); | ||
214 | static int get_port(struct addrinfo *, const char *, int); | ||
215 | static const struct afd *find_afd(int); | ||
216 | #ifdef INET6 | ||
217 | static int ip6_str2scopeid(char *, struct sockaddr_in6 *, u_int32_t *); | ||
218 | #endif | ||
219 | |||
220 | static struct addrinfo * _gethtent(const char *, const struct addrinfo *, | ||
221 | FILE *); | ||
222 | static struct addrinfo *_files_getaddrinfo(const char *, | ||
223 | const struct addrinfo *); | ||
224 | |||
225 | #ifdef YP | ||
226 | static struct addrinfo *_yphostent(char *, const struct addrinfo *); | ||
227 | static struct addrinfo *_yp_getaddrinfo(const char *, | ||
228 | const struct addrinfo *); | ||
229 | #endif | ||
230 | |||
231 | static struct addrinfo *getanswer(const querybuf *, int, const char *, int, | ||
232 | const struct addrinfo *); | ||
233 | static int res_queryN(const char *, struct res_target *); | ||
234 | static int res_searchN(const char *, struct res_target *); | ||
235 | static int res_querydomainN(const char *, const char *, struct res_target *); | ||
236 | static struct addrinfo *_dns_getaddrinfo(const char *, const struct addrinfo *, | ||
237 | const struct __res_state *); | ||
238 | |||
239 | |||
240 | /* XXX macros that make external reference is BAD. */ | ||
241 | |||
242 | #define GET_AI(ai, afd, addr) \ | ||
243 | do { \ | ||
244 | /* external reference: pai, error, and label free */ \ | ||
245 | (ai) = get_ai(pai, (afd), (addr)); \ | ||
246 | if ((ai) == NULL) { \ | ||
247 | error = EAI_MEMORY; \ | ||
248 | goto free; \ | ||
249 | } \ | ||
250 | } while (/*CONSTCOND*/0) | ||
251 | |||
252 | #define GET_PORT(ai, serv) \ | ||
253 | do { \ | ||
254 | /* external reference: error and label free */ \ | ||
255 | error = get_port((ai), (serv), 0); \ | ||
256 | if (error != 0) \ | ||
257 | goto free; \ | ||
258 | } while (/*CONSTCOND*/0) | ||
259 | |||
260 | #define GET_CANONNAME(ai, str) \ | ||
261 | do { \ | ||
262 | /* external reference: pai, error and label free */ \ | ||
263 | error = get_canonname(pai, (ai), (str)); \ | ||
264 | if (error != 0) \ | ||
265 | goto free; \ | ||
266 | } while (/*CONSTCOND*/0) | ||
267 | |||
268 | #define ERR(err) \ | ||
269 | do { \ | ||
270 | /* external reference: error, and label bad */ \ | ||
271 | error = (err); \ | ||
272 | goto bad; \ | ||
273 | /*NOTREACHED*/ \ | ||
274 | } while (/*CONSTCOND*/0) | ||
275 | |||
276 | #define MATCH_FAMILY(x, y, w) \ | ||
277 | ((x) == (y) || (/*CONSTCOND*/(w) && ((x) == PF_UNSPEC || (y) == PF_UNSPEC))) | ||
278 | #define MATCH(x, y, w) \ | ||
279 | ((x) == (y) || (/*CONSTCOND*/(w) && ((x) == ANY || (y) == ANY))) | ||
280 | |||
281 | int | ||
282 | getaddrinfo(const char *hostname, const char *servname, | ||
283 | const struct addrinfo *hints, struct addrinfo **res) | ||
284 | { | ||
285 | struct addrinfo sentinel; | ||
286 | struct addrinfo *cur; | ||
287 | int error = 0; | ||
288 | struct addrinfo ai; | ||
289 | struct addrinfo ai0; | ||
290 | struct addrinfo *pai; | ||
291 | const struct explore *ex; | ||
292 | |||
293 | memset(&sentinel, 0, sizeof(sentinel)); | ||
294 | cur = &sentinel; | ||
295 | pai = &ai; | ||
296 | pai->ai_flags = 0; | ||
297 | pai->ai_family = PF_UNSPEC; | ||
298 | pai->ai_socktype = ANY; | ||
299 | pai->ai_protocol = ANY; | ||
300 | pai->ai_addrlen = 0; | ||
301 | pai->ai_canonname = NULL; | ||
302 | pai->ai_addr = NULL; | ||
303 | pai->ai_next = NULL; | ||
304 | |||
305 | if (hostname == NULL && servname == NULL) | ||
306 | return EAI_NONAME; | ||
307 | if (hints) { | ||
308 | /* error check for hints */ | ||
309 | if (hints->ai_addrlen || hints->ai_canonname || | ||
310 | hints->ai_addr || hints->ai_next) | ||
311 | ERR(EAI_BADHINTS); /* xxx */ | ||
312 | if (hints->ai_flags & ~AI_MASK) | ||
313 | ERR(EAI_BADFLAGS); | ||
314 | switch (hints->ai_family) { | ||
315 | case PF_UNSPEC: | ||
316 | case PF_INET: | ||
317 | #ifdef INET6 | ||
318 | case PF_INET6: | ||
319 | #endif | ||
320 | break; | ||
321 | default: | ||
322 | ERR(EAI_FAMILY); | ||
323 | } | ||
324 | memcpy(pai, hints, sizeof(*pai)); | ||
325 | |||
326 | /* | ||
327 | * if both socktype/protocol are specified, check if they | ||
328 | * are meaningful combination. | ||
329 | */ | ||
330 | if (pai->ai_socktype != ANY && pai->ai_protocol != ANY) { | ||
331 | for (ex = explore; ex->e_af >= 0; ex++) { | ||
332 | if (pai->ai_family != ex->e_af) | ||
333 | continue; | ||
334 | if (ex->e_socktype == ANY) | ||
335 | continue; | ||
336 | if (ex->e_protocol == ANY) | ||
337 | continue; | ||
338 | if (pai->ai_socktype == ex->e_socktype | ||
339 | && pai->ai_protocol != ex->e_protocol) { | ||
340 | ERR(EAI_BADHINTS); | ||
341 | } | ||
342 | } | ||
343 | } | ||
344 | } | ||
345 | |||
346 | /* | ||
347 | * check for special cases. (1) numeric servname is disallowed if | ||
348 | * socktype/protocol are left unspecified. (2) servname is disallowed | ||
349 | * for raw and other inet{,6} sockets. | ||
350 | */ | ||
351 | if (MATCH_FAMILY(pai->ai_family, PF_INET, 1) | ||
352 | #ifdef PF_INET6 | ||
353 | || MATCH_FAMILY(pai->ai_family, PF_INET6, 1) | ||
354 | #endif | ||
355 | ) { | ||
356 | ai0 = *pai; /* backup *pai */ | ||
357 | |||
358 | if (pai->ai_family == PF_UNSPEC) { | ||
359 | #ifdef PF_INET6 | ||
360 | pai->ai_family = PF_INET6; | ||
361 | #else | ||
362 | pai->ai_family = PF_INET; | ||
363 | #endif | ||
364 | } | ||
365 | error = get_portmatch(pai, servname); | ||
366 | if (error) | ||
367 | ERR(error); | ||
368 | |||
369 | *pai = ai0; | ||
370 | } | ||
371 | |||
372 | ai0 = *pai; | ||
373 | |||
374 | /* NULL hostname, or numeric hostname */ | ||
375 | for (ex = explore; ex->e_af >= 0; ex++) { | ||
376 | *pai = ai0; | ||
377 | |||
378 | /* PF_UNSPEC entries are prepared for DNS queries only */ | ||
379 | if (ex->e_af == PF_UNSPEC) | ||
380 | continue; | ||
381 | |||
382 | if (!MATCH_FAMILY(pai->ai_family, ex->e_af, WILD_AF(ex))) | ||
383 | continue; | ||
384 | if (!MATCH(pai->ai_socktype, ex->e_socktype, WILD_SOCKTYPE(ex))) | ||
385 | continue; | ||
386 | if (!MATCH(pai->ai_protocol, ex->e_protocol, WILD_PROTOCOL(ex))) | ||
387 | continue; | ||
388 | |||
389 | if (pai->ai_family == PF_UNSPEC) | ||
390 | pai->ai_family = ex->e_af; | ||
391 | if (pai->ai_socktype == ANY && ex->e_socktype != ANY) | ||
392 | pai->ai_socktype = ex->e_socktype; | ||
393 | if (pai->ai_protocol == ANY && ex->e_protocol != ANY) | ||
394 | pai->ai_protocol = ex->e_protocol; | ||
395 | |||
396 | if (hostname == NULL) | ||
397 | error = explore_null(pai, servname, &cur->ai_next); | ||
398 | else | ||
399 | error = explore_numeric_scope(pai, hostname, servname, | ||
400 | &cur->ai_next); | ||
401 | |||
402 | if (error) | ||
403 | goto free; | ||
404 | |||
405 | while (cur && cur->ai_next) | ||
406 | cur = cur->ai_next; | ||
407 | } | ||
408 | |||
409 | /* | ||
410 | * XXX | ||
411 | * If numeric representation of AF1 can be interpreted as FQDN | ||
412 | * representation of AF2, we need to think again about the code below. | ||
413 | */ | ||
414 | if (sentinel.ai_next) | ||
415 | goto good; | ||
416 | |||
417 | if (hostname == NULL) | ||
418 | ERR(EAI_NODATA); | ||
419 | if (pai->ai_flags & AI_NUMERICHOST) | ||
420 | ERR(EAI_NONAME); | ||
421 | |||
422 | /* | ||
423 | * hostname as alphabetical name. | ||
424 | * we would like to prefer AF_INET6 than AF_INET, so we'll make an | ||
425 | * outer loop by AFs. | ||
426 | */ | ||
427 | for (ex = explore; ex->e_af >= 0; ex++) { | ||
428 | *pai = ai0; | ||
429 | |||
430 | /* require exact match for family field */ | ||
431 | if (pai->ai_family != ex->e_af) | ||
432 | continue; | ||
433 | |||
434 | if (!MATCH(pai->ai_socktype, ex->e_socktype, | ||
435 | WILD_SOCKTYPE(ex))) { | ||
436 | continue; | ||
437 | } | ||
438 | if (!MATCH(pai->ai_protocol, ex->e_protocol, | ||
439 | WILD_PROTOCOL(ex))) { | ||
440 | continue; | ||
441 | } | ||
442 | |||
443 | if (pai->ai_socktype == ANY && ex->e_socktype != ANY) | ||
444 | pai->ai_socktype = ex->e_socktype; | ||
445 | if (pai->ai_protocol == ANY && ex->e_protocol != ANY) | ||
446 | pai->ai_protocol = ex->e_protocol; | ||
447 | |||
448 | error = explore_fqdn(pai, hostname, servname, | ||
449 | &cur->ai_next); | ||
450 | |||
451 | while (cur && cur->ai_next) | ||
452 | cur = cur->ai_next; | ||
453 | } | ||
454 | |||
455 | /* XXX */ | ||
456 | if (sentinel.ai_next) | ||
457 | error = 0; | ||
458 | |||
459 | if (error == 0) { | ||
460 | if (sentinel.ai_next) { | ||
461 | good: | ||
462 | *res = sentinel.ai_next; | ||
463 | return SUCCESS; | ||
464 | } else | ||
465 | error = EAI_FAIL; | ||
466 | } | ||
467 | free: | ||
468 | bad: | ||
469 | if (sentinel.ai_next) | ||
470 | freeaddrinfo(sentinel.ai_next); | ||
471 | *res = NULL; | ||
472 | return error; | ||
473 | } | ||
474 | |||
475 | /* | ||
476 | * FQDN hostname, DNS lookup | ||
477 | */ | ||
478 | |||
479 | static int | ||
480 | explore_fqdn(const struct addrinfo *pai, const char *hostname, | ||
481 | const char *servname, struct addrinfo **res) | ||
482 | { | ||
483 | struct __res_state *_resp = _THREAD_PRIVATE(_res, _res, &_res); | ||
484 | struct addrinfo *result; | ||
485 | struct addrinfo *cur; | ||
486 | int error = 0; | ||
487 | char lookups[MAXDNSLUS]; | ||
488 | int i; | ||
489 | _THREAD_PRIVATE_MUTEX(_explore_mutex); | ||
490 | |||
491 | result = NULL; | ||
492 | |||
493 | /* | ||
494 | * if the servname does not match socktype/protocol, ignore it. | ||
495 | */ | ||
496 | if (get_portmatch(pai, servname) != 0) { | ||
497 | return 0; | ||
498 | } | ||
499 | |||
500 | if (_res_init(0) == -1) | ||
501 | strlcpy(lookups, "f", sizeof lookups); | ||
502 | else { | ||
503 | bcopy(_resp->lookups, lookups, sizeof lookups); | ||
504 | if (lookups[0] == '\0') | ||
505 | strlcpy(lookups, "bf", sizeof lookups); | ||
506 | } | ||
507 | |||
508 | /* | ||
509 | * The yp/dns/files getaddrinfo functions are not thread safe. | ||
510 | * Protect them with a mutex. | ||
511 | */ | ||
512 | _THREAD_PRIVATE_MUTEX_LOCK(_explore_mutex); | ||
513 | for (i = 0; i < MAXDNSLUS && result == NULL && lookups[i]; i++) { | ||
514 | switch (lookups[i]) { | ||
515 | #ifdef YP | ||
516 | case 'y': | ||
517 | result = _yp_getaddrinfo(hostname, pai); | ||
518 | break; | ||
519 | #endif | ||
520 | case 'b': | ||
521 | result = _dns_getaddrinfo(hostname, pai, _resp); | ||
522 | break; | ||
523 | case 'f': | ||
524 | result = _files_getaddrinfo(hostname, pai); | ||
525 | break; | ||
526 | } | ||
527 | } | ||
528 | _THREAD_PRIVATE_MUTEX_UNLOCK(_explore_mutex); | ||
529 | if (result) { | ||
530 | for (cur = result; cur; cur = cur->ai_next) { | ||
531 | GET_PORT(cur, servname); | ||
532 | /* canonname should be filled already */ | ||
533 | } | ||
534 | *res = result; | ||
535 | return 0; | ||
536 | } else { | ||
537 | /* translate error code */ | ||
538 | switch (h_errno) { | ||
539 | case NETDB_SUCCESS: | ||
540 | error = EAI_FAIL; /*XXX strange */ | ||
541 | break; | ||
542 | case HOST_NOT_FOUND: | ||
543 | error = EAI_NODATA; | ||
544 | break; | ||
545 | case TRY_AGAIN: | ||
546 | error = EAI_AGAIN; | ||
547 | break; | ||
548 | case NO_RECOVERY: | ||
549 | error = EAI_FAIL; | ||
550 | break; | ||
551 | case NO_DATA: | ||
552 | #if NO_ADDRESS != NO_DATA | ||
553 | case NO_ADDRESS: | ||
554 | #endif | ||
555 | error = EAI_NODATA; | ||
556 | break; | ||
557 | default: /* unknown ones */ | ||
558 | error = EAI_FAIL; | ||
559 | break; | ||
560 | } | ||
561 | } | ||
562 | |||
563 | free: | ||
564 | if (result) | ||
565 | freeaddrinfo(result); | ||
566 | return error; | ||
567 | } | ||
568 | |||
569 | /* | ||
570 | * hostname == NULL. | ||
571 | * passive socket -> anyaddr (0.0.0.0 or ::) | ||
572 | * non-passive socket -> localhost (127.0.0.1 or ::1) | ||
573 | */ | ||
574 | static int | ||
575 | explore_null(const struct addrinfo *pai, const char *servname, | ||
576 | struct addrinfo **res) | ||
577 | { | ||
578 | int s; | ||
579 | const struct afd *afd; | ||
580 | struct addrinfo *cur; | ||
581 | struct addrinfo sentinel; | ||
582 | int error; | ||
583 | |||
584 | *res = NULL; | ||
585 | sentinel.ai_next = NULL; | ||
586 | cur = &sentinel; | ||
587 | |||
588 | /* | ||
589 | * filter out AFs that are not supported by the kernel | ||
590 | * XXX errno? | ||
591 | */ | ||
592 | s = socket(pai->ai_family, SOCK_DGRAM, 0); | ||
593 | if (s < 0) { | ||
594 | if (errno != EMFILE) | ||
595 | return 0; | ||
596 | } else | ||
597 | close(s); | ||
598 | |||
599 | /* | ||
600 | * if the servname does not match socktype/protocol, ignore it. | ||
601 | */ | ||
602 | if (get_portmatch(pai, servname) != 0) | ||
603 | return 0; | ||
604 | |||
605 | afd = find_afd(pai->ai_family); | ||
606 | if (afd == NULL) | ||
607 | return 0; | ||
608 | |||
609 | if (pai->ai_flags & AI_PASSIVE) { | ||
610 | GET_AI(cur->ai_next, afd, afd->a_addrany); | ||
611 | /* xxx meaningless? | ||
612 | * GET_CANONNAME(cur->ai_next, "anyaddr"); | ||
613 | */ | ||
614 | } else { | ||
615 | GET_AI(cur->ai_next, afd, afd->a_loopback); | ||
616 | /* xxx meaningless? | ||
617 | * GET_CANONNAME(cur->ai_next, "localhost"); | ||
618 | */ | ||
619 | } | ||
620 | GET_PORT(cur->ai_next, servname); | ||
621 | cur = cur->ai_next; | ||
622 | |||
623 | *res = sentinel.ai_next; | ||
624 | return 0; | ||
625 | |||
626 | free: | ||
627 | if (sentinel.ai_next) | ||
628 | freeaddrinfo(sentinel.ai_next); | ||
629 | return error; | ||
630 | } | ||
631 | |||
632 | /* | ||
633 | * numeric hostname | ||
634 | */ | ||
635 | static int | ||
636 | explore_numeric(const struct addrinfo *pai, const char *hostname, | ||
637 | const char *servname, struct addrinfo **res, const char *canonname) | ||
638 | { | ||
639 | const struct afd *afd; | ||
640 | struct addrinfo *cur; | ||
641 | struct addrinfo sentinel; | ||
642 | int error; | ||
643 | char pton[PTON_MAX]; | ||
644 | |||
645 | *res = NULL; | ||
646 | sentinel.ai_next = NULL; | ||
647 | cur = &sentinel; | ||
648 | |||
649 | /* | ||
650 | * if the servname does not match socktype/protocol, ignore it. | ||
651 | */ | ||
652 | if (get_portmatch(pai, servname) != 0) | ||
653 | return 0; | ||
654 | |||
655 | afd = find_afd(pai->ai_family); | ||
656 | if (afd == NULL) | ||
657 | return 0; | ||
658 | |||
659 | switch (afd->a_af) { | ||
660 | #if 0 /*X/Open spec*/ | ||
661 | case AF_INET: | ||
662 | error = inet_aton(hostname, (struct in_addr *)pton); | ||
663 | break; | ||
664 | #endif | ||
665 | default: | ||
666 | error = inet_pton(afd->a_af, hostname, pton); | ||
667 | break; | ||
668 | } | ||
669 | if (error == 1) { | ||
670 | if (pai->ai_family == afd->a_af || | ||
671 | pai->ai_family == PF_UNSPEC /*?*/) { | ||
672 | GET_AI(cur->ai_next, afd, pton); | ||
673 | GET_PORT(cur->ai_next, servname); | ||
674 | if ((pai->ai_flags & AI_CANONNAME)) { | ||
675 | /* | ||
676 | * Set the numeric address itself as | ||
677 | * the canonical name, based on a | ||
678 | * clarification in rfc2553bis-03. | ||
679 | */ | ||
680 | GET_CANONNAME(cur->ai_next, canonname); | ||
681 | } | ||
682 | while (cur && cur->ai_next) | ||
683 | cur = cur->ai_next; | ||
684 | } else | ||
685 | ERR(EAI_FAMILY); /*xxx*/ | ||
686 | } | ||
687 | |||
688 | *res = sentinel.ai_next; | ||
689 | return 0; | ||
690 | |||
691 | free: | ||
692 | bad: | ||
693 | if (sentinel.ai_next) | ||
694 | freeaddrinfo(sentinel.ai_next); | ||
695 | return error; | ||
696 | } | ||
697 | |||
698 | /* | ||
699 | * numeric hostname with scope | ||
700 | */ | ||
701 | static int | ||
702 | explore_numeric_scope(const struct addrinfo *pai, const char *hostname, | ||
703 | const char *servname, struct addrinfo **res) | ||
704 | { | ||
705 | #if !defined(SCOPE_DELIMITER) || !defined(INET6) | ||
706 | return explore_numeric(pai, hostname, servname, res, hostname); | ||
707 | #else | ||
708 | const struct afd *afd; | ||
709 | struct addrinfo *cur; | ||
710 | int error; | ||
711 | char *cp, *hostname2 = NULL, *scope, *addr; | ||
712 | struct sockaddr_in6 *sin6; | ||
713 | |||
714 | /* | ||
715 | * if the servname does not match socktype/protocol, ignore it. | ||
716 | */ | ||
717 | if (get_portmatch(pai, servname) != 0) | ||
718 | return 0; | ||
719 | |||
720 | afd = find_afd(pai->ai_family); | ||
721 | if (afd == NULL) | ||
722 | return 0; | ||
723 | |||
724 | if (!afd->a_scoped) | ||
725 | return explore_numeric(pai, hostname, servname, res, hostname); | ||
726 | |||
727 | cp = strchr(hostname, SCOPE_DELIMITER); | ||
728 | if (cp == NULL) | ||
729 | return explore_numeric(pai, hostname, servname, res, hostname); | ||
730 | |||
731 | /* | ||
732 | * Handle special case of <scoped_address><delimiter><scope id> | ||
733 | */ | ||
734 | hostname2 = strdup(hostname); | ||
735 | if (hostname2 == NULL) | ||
736 | return EAI_MEMORY; | ||
737 | /* terminate at the delimiter */ | ||
738 | hostname2[cp - hostname] = '\0'; | ||
739 | addr = hostname2; | ||
740 | scope = cp + 1; | ||
741 | |||
742 | error = explore_numeric(pai, addr, servname, res, hostname); | ||
743 | if (error == 0) { | ||
744 | u_int32_t scopeid; | ||
745 | |||
746 | for (cur = *res; cur; cur = cur->ai_next) { | ||
747 | if (cur->ai_family != AF_INET6) | ||
748 | continue; | ||
749 | sin6 = (struct sockaddr_in6 *)(void *)cur->ai_addr; | ||
750 | if (ip6_str2scopeid(scope, sin6, &scopeid) == -1) { | ||
751 | free(hostname2); | ||
752 | return(EAI_NODATA); /* XXX: is return OK? */ | ||
753 | } | ||
754 | sin6->sin6_scope_id = scopeid; | ||
755 | } | ||
756 | } | ||
757 | |||
758 | free(hostname2); | ||
759 | |||
760 | return error; | ||
761 | #endif | ||
762 | } | ||
763 | |||
764 | static int | ||
765 | get_canonname(const struct addrinfo *pai, struct addrinfo *ai, const char *str) | ||
766 | { | ||
767 | if ((pai->ai_flags & AI_CANONNAME) != 0) { | ||
768 | ai->ai_canonname = strdup(str); | ||
769 | if (ai->ai_canonname == NULL) | ||
770 | return EAI_MEMORY; | ||
771 | } | ||
772 | return 0; | ||
773 | } | ||
774 | |||
775 | static struct addrinfo * | ||
776 | get_ai(const struct addrinfo *pai, const struct afd *afd, const char *addr) | ||
777 | { | ||
778 | char *p; | ||
779 | struct addrinfo *ai; | ||
780 | |||
781 | ai = (struct addrinfo *)malloc(sizeof(struct addrinfo) | ||
782 | + (afd->a_socklen)); | ||
783 | if (ai == NULL) | ||
784 | return NULL; | ||
785 | |||
786 | memcpy(ai, pai, sizeof(struct addrinfo)); | ||
787 | ai->ai_addr = (struct sockaddr *)(void *)(ai + 1); | ||
788 | memset(ai->ai_addr, 0, (size_t)afd->a_socklen); | ||
789 | ai->ai_addr->sa_len = afd->a_socklen; | ||
790 | ai->ai_addrlen = afd->a_socklen; | ||
791 | ai->ai_addr->sa_family = ai->ai_family = afd->a_af; | ||
792 | p = (char *)(void *)(ai->ai_addr); | ||
793 | memcpy(p + afd->a_off, addr, (size_t)afd->a_addrlen); | ||
794 | return ai; | ||
795 | } | ||
796 | |||
797 | static int | ||
798 | get_portmatch(const struct addrinfo *ai, const char *servname) | ||
799 | { | ||
800 | |||
801 | /* get_port does not touch first argument. when matchonly == 1. */ | ||
802 | /* LINTED const cast */ | ||
803 | return get_port((struct addrinfo *)ai, servname, 1); | ||
804 | } | ||
805 | |||
806 | static int | ||
807 | get_port(struct addrinfo *ai, const char *servname, int matchonly) | ||
808 | { | ||
809 | const char *errstr, *proto; | ||
810 | int port; | ||
811 | int allownumeric; | ||
812 | |||
813 | if (servname == NULL) | ||
814 | return 0; | ||
815 | switch (ai->ai_family) { | ||
816 | case AF_INET: | ||
817 | #ifdef AF_INET6 | ||
818 | case AF_INET6: | ||
819 | #endif | ||
820 | break; | ||
821 | default: | ||
822 | return 0; | ||
823 | } | ||
824 | |||
825 | switch (ai->ai_socktype) { | ||
826 | case SOCK_RAW: | ||
827 | return EAI_SERVICE; | ||
828 | case SOCK_DGRAM: | ||
829 | case SOCK_STREAM: | ||
830 | case ANY: | ||
831 | allownumeric = 1; | ||
832 | break; | ||
833 | default: | ||
834 | return EAI_SOCKTYPE; | ||
835 | } | ||
836 | |||
837 | port = (int)strtonum(servname, 0, USHRT_MAX, &errstr); | ||
838 | if (!errstr) { | ||
839 | if (!allownumeric) | ||
840 | return EAI_SERVICE; | ||
841 | port = htons(port); | ||
842 | } else { | ||
843 | struct servent sp; | ||
844 | struct servent_data sd; | ||
845 | |||
846 | if (errno == ERANGE) | ||
847 | return EAI_SERVICE; | ||
848 | if (ai->ai_flags & AI_NUMERICSERV) | ||
849 | return EAI_NONAME; | ||
850 | |||
851 | switch (ai->ai_socktype) { | ||
852 | case SOCK_DGRAM: | ||
853 | proto = "udp"; | ||
854 | break; | ||
855 | case SOCK_STREAM: | ||
856 | proto = "tcp"; | ||
857 | break; | ||
858 | default: | ||
859 | proto = NULL; | ||
860 | break; | ||
861 | } | ||
862 | |||
863 | (void)memset(&sd, 0, sizeof(sd)); | ||
864 | if (getservbyname_r(servname, proto, &sp, &sd) == -1) | ||
865 | return EAI_SERVICE; | ||
866 | port = sp.s_port; | ||
867 | endservent_r(&sd); | ||
868 | } | ||
869 | |||
870 | if (!matchonly) { | ||
871 | switch (ai->ai_family) { | ||
872 | case AF_INET: | ||
873 | ((struct sockaddr_in *)(void *) | ||
874 | ai->ai_addr)->sin_port = port; | ||
875 | break; | ||
876 | #ifdef INET6 | ||
877 | case AF_INET6: | ||
878 | ((struct sockaddr_in6 *)(void *) | ||
879 | ai->ai_addr)->sin6_port = port; | ||
880 | break; | ||
881 | #endif | ||
882 | } | ||
883 | } | ||
884 | |||
885 | return 0; | ||
886 | } | ||
887 | |||
888 | static const struct afd * | ||
889 | find_afd(int af) | ||
890 | { | ||
891 | const struct afd *afd; | ||
892 | |||
893 | if (af == PF_UNSPEC) | ||
894 | return NULL; | ||
895 | for (afd = afdl; afd->a_af; afd++) { | ||
896 | if (afd->a_af == af) | ||
897 | return afd; | ||
898 | } | ||
899 | return NULL; | ||
900 | } | ||
901 | |||
902 | #ifdef INET6 | ||
903 | /* convert a string to a scope identifier. XXX: IPv6 specific */ | ||
904 | static int | ||
905 | ip6_str2scopeid(char *scope, struct sockaddr_in6 *sin6, u_int32_t *scopeid) | ||
906 | { | ||
907 | struct in6_addr *a6 = &sin6->sin6_addr; | ||
908 | const char *errstr; | ||
909 | |||
910 | /* empty scopeid portion is invalid */ | ||
911 | if (*scope == '\0') | ||
912 | return -1; | ||
913 | |||
914 | if (IN6_IS_ADDR_LINKLOCAL(a6) || IN6_IS_ADDR_MC_LINKLOCAL(a6) || | ||
915 | IN6_IS_ADDR_MC_INTFACELOCAL(a6)) { | ||
916 | /* | ||
917 | * We currently assume a one-to-one mapping between links | ||
918 | * and interfaces, so we simply use interface indices for | ||
919 | * like-local scopes. | ||
920 | */ | ||
921 | *scopeid = if_nametoindex(scope); | ||
922 | if (*scopeid == 0) | ||
923 | goto trynumeric; | ||
924 | return 0; | ||
925 | } | ||
926 | |||
927 | /* still unclear about literal, allow numeric only - placeholder */ | ||
928 | if (IN6_IS_ADDR_SITELOCAL(a6) || IN6_IS_ADDR_MC_SITELOCAL(a6)) | ||
929 | goto trynumeric; | ||
930 | if (IN6_IS_ADDR_MC_ORGLOCAL(a6)) | ||
931 | goto trynumeric; | ||
932 | else | ||
933 | goto trynumeric; /* global */ | ||
934 | |||
935 | /* try to convert to a numeric id as a last resort */ | ||
936 | trynumeric: | ||
937 | *scopeid = (u_int32_t)strtonum(scope, 0, UINT32_MAX, &errstr); | ||
938 | if (errstr) | ||
939 | return (-1); | ||
940 | return (0); | ||
941 | } | ||
942 | #endif | ||
943 | |||
944 | /* code duplicate with gethnamaddr.c */ | ||
945 | |||
946 | static const char AskedForGot[] = | ||
947 | "gethostby*.getanswer: asked for \"%s\", got \"%s\""; | ||
948 | |||
949 | static struct addrinfo * | ||
950 | getanswer(const querybuf *answer, int anslen, const char *qname, int qtype, | ||
951 | const struct addrinfo *pai) | ||
952 | { | ||
953 | struct addrinfo sentinel, *cur; | ||
954 | struct addrinfo ai; | ||
955 | const struct afd *afd; | ||
956 | char *canonname; | ||
957 | const HEADER *hp; | ||
958 | const u_char *cp; | ||
959 | int n; | ||
960 | const u_char *eom; | ||
961 | char *bp, *ep; | ||
962 | int type, class, ancount, qdcount; | ||
963 | int haveanswer, had_error; | ||
964 | char tbuf[MAXDNAME]; | ||
965 | int (*name_ok)(const char *); | ||
966 | char hostbuf[8*1024]; | ||
967 | |||
968 | memset(&sentinel, 0, sizeof(sentinel)); | ||
969 | cur = &sentinel; | ||
970 | |||
971 | canonname = NULL; | ||
972 | eom = answer->buf + anslen; | ||
973 | switch (qtype) { | ||
974 | case T_A: | ||
975 | case T_AAAA: | ||
976 | case T_ANY: /*use T_ANY only for T_A/T_AAAA lookup*/ | ||
977 | name_ok = res_hnok; | ||
978 | break; | ||
979 | default: | ||
980 | return (NULL); /* XXX should be abort() -- but that is illegal */ | ||
981 | } | ||
982 | /* | ||
983 | * find first satisfactory answer | ||
984 | */ | ||
985 | hp = &answer->hdr; | ||
986 | ancount = ntohs(hp->ancount); | ||
987 | qdcount = ntohs(hp->qdcount); | ||
988 | bp = hostbuf; | ||
989 | ep = hostbuf + sizeof hostbuf; | ||
990 | cp = answer->buf + HFIXEDSZ; | ||
991 | if (qdcount != 1) { | ||
992 | h_errno = NO_RECOVERY; | ||
993 | return (NULL); | ||
994 | } | ||
995 | n = dn_expand(answer->buf, eom, cp, bp, ep - bp); | ||
996 | if ((n < 0) || !(*name_ok)(bp)) { | ||
997 | h_errno = NO_RECOVERY; | ||
998 | return (NULL); | ||
999 | } | ||
1000 | cp += n + QFIXEDSZ; | ||
1001 | if (qtype == T_A || qtype == T_AAAA || qtype == T_ANY) { | ||
1002 | size_t len; | ||
1003 | |||
1004 | /* res_send() has already verified that the query name is the | ||
1005 | * same as the one we sent; this just gets the expanded name | ||
1006 | * (i.e., with the succeeding search-domain tacked on). | ||
1007 | */ | ||
1008 | len = strlen(bp) + 1; /* for the \0 */ | ||
1009 | if (len >= MAXHOSTNAMELEN) { | ||
1010 | h_errno = NO_RECOVERY; | ||
1011 | return (NULL); | ||
1012 | } | ||
1013 | canonname = bp; | ||
1014 | bp += len; | ||
1015 | /* The qname can be abbreviated, but h_name is now absolute. */ | ||
1016 | qname = canonname; | ||
1017 | } | ||
1018 | haveanswer = 0; | ||
1019 | had_error = 0; | ||
1020 | while (ancount-- > 0 && cp < eom && !had_error) { | ||
1021 | n = dn_expand(answer->buf, eom, cp, bp, ep - bp); | ||
1022 | if ((n < 0) || !(*name_ok)(bp)) { | ||
1023 | had_error++; | ||
1024 | continue; | ||
1025 | } | ||
1026 | cp += n; /* name */ | ||
1027 | type = _getshort(cp); | ||
1028 | cp += INT16SZ; /* type */ | ||
1029 | class = _getshort(cp); | ||
1030 | cp += INT16SZ + INT32SZ; /* class, TTL */ | ||
1031 | n = _getshort(cp); | ||
1032 | cp += INT16SZ; /* len */ | ||
1033 | if (class != C_IN) { | ||
1034 | /* XXX - debug? syslog? */ | ||
1035 | cp += n; | ||
1036 | continue; /* XXX - had_error++ ? */ | ||
1037 | } | ||
1038 | if ((qtype == T_A || qtype == T_AAAA || qtype == T_ANY) && | ||
1039 | type == T_CNAME) { | ||
1040 | size_t len; | ||
1041 | |||
1042 | n = dn_expand(answer->buf, eom, cp, tbuf, sizeof tbuf); | ||
1043 | if ((n < 0) || !(*name_ok)(tbuf)) { | ||
1044 | had_error++; | ||
1045 | continue; | ||
1046 | } | ||
1047 | cp += n; | ||
1048 | /* Get canonical name. */ | ||
1049 | len = strlen(tbuf) + 1; /* for the \0 */ | ||
1050 | if (len > ep - bp || len >= MAXHOSTNAMELEN) { | ||
1051 | had_error++; | ||
1052 | continue; | ||
1053 | } | ||
1054 | strlcpy(bp, tbuf, ep - bp); | ||
1055 | canonname = bp; | ||
1056 | bp += len; | ||
1057 | continue; | ||
1058 | } | ||
1059 | if (qtype == T_ANY) { | ||
1060 | if (!(type == T_A || type == T_AAAA)) { | ||
1061 | cp += n; | ||
1062 | continue; | ||
1063 | } | ||
1064 | } else if (type != qtype) { | ||
1065 | #ifndef NO_LOG_BAD_DNS_RESPONSES | ||
1066 | if (type != T_KEY && type != T_SIG) { | ||
1067 | struct syslog_data sdata = SYSLOG_DATA_INIT; | ||
1068 | |||
1069 | syslog_r(LOG_NOTICE|LOG_AUTH, &sdata, | ||
1070 | "gethostby*.getanswer: asked for \"%s %s %s\", got type \"%s\"", | ||
1071 | qname, p_class(C_IN), p_type(qtype), | ||
1072 | p_type(type)); | ||
1073 | } | ||
1074 | #endif /* NO_LOG_BAD_DNS_RESPONSES */ | ||
1075 | cp += n; | ||
1076 | continue; /* XXX - had_error++ ? */ | ||
1077 | } | ||
1078 | switch (type) { | ||
1079 | case T_A: | ||
1080 | case T_AAAA: | ||
1081 | if (strcasecmp(canonname, bp) != 0) { | ||
1082 | struct syslog_data sdata = SYSLOG_DATA_INIT; | ||
1083 | |||
1084 | syslog_r(LOG_NOTICE|LOG_AUTH, &sdata, | ||
1085 | AskedForGot, canonname, bp); | ||
1086 | cp += n; | ||
1087 | continue; /* XXX - had_error++ ? */ | ||
1088 | } | ||
1089 | if (type == T_A && n != INADDRSZ) { | ||
1090 | cp += n; | ||
1091 | continue; | ||
1092 | } | ||
1093 | if (type == T_AAAA && n != IN6ADDRSZ) { | ||
1094 | cp += n; | ||
1095 | continue; | ||
1096 | } | ||
1097 | if (type == T_AAAA) { | ||
1098 | struct in6_addr in6; | ||
1099 | memcpy(&in6, cp, IN6ADDRSZ); | ||
1100 | if (IN6_IS_ADDR_V4MAPPED(&in6)) { | ||
1101 | cp += n; | ||
1102 | continue; | ||
1103 | } | ||
1104 | } | ||
1105 | if (!haveanswer) { | ||
1106 | canonname = bp; | ||
1107 | bp += strlen(bp) + 1; /* for the \0 */ | ||
1108 | } | ||
1109 | |||
1110 | /* don't overwrite pai */ | ||
1111 | ai = *pai; | ||
1112 | ai.ai_family = (type == T_A) ? AF_INET : AF_INET6; | ||
1113 | afd = find_afd(ai.ai_family); | ||
1114 | if (afd == NULL) { | ||
1115 | cp += n; | ||
1116 | continue; | ||
1117 | } | ||
1118 | cur->ai_next = get_ai(&ai, afd, (const char *)cp); | ||
1119 | if (cur->ai_next == NULL) | ||
1120 | had_error++; | ||
1121 | while (cur && cur->ai_next) | ||
1122 | cur = cur->ai_next; | ||
1123 | cp += n; | ||
1124 | break; | ||
1125 | default: | ||
1126 | abort(); /* XXX abort illegal in library */ | ||
1127 | } | ||
1128 | if (!had_error) | ||
1129 | haveanswer++; | ||
1130 | } | ||
1131 | if (haveanswer) { | ||
1132 | if (!canonname) | ||
1133 | (void)get_canonname(pai, sentinel.ai_next, qname); | ||
1134 | else | ||
1135 | (void)get_canonname(pai, sentinel.ai_next, canonname); | ||
1136 | h_errno = NETDB_SUCCESS; | ||
1137 | return sentinel.ai_next; | ||
1138 | } | ||
1139 | |||
1140 | h_errno = NO_RECOVERY; | ||
1141 | return NULL; | ||
1142 | } | ||
1143 | |||
1144 | /*ARGSUSED*/ | ||
1145 | static struct addrinfo * | ||
1146 | _dns_getaddrinfo(const char *name, const struct addrinfo *pai, | ||
1147 | const struct __res_state *_resp) | ||
1148 | { | ||
1149 | struct addrinfo *ai; | ||
1150 | querybuf *buf, *buf2; | ||
1151 | struct addrinfo sentinel, *cur; | ||
1152 | struct res_target q, q2; | ||
1153 | |||
1154 | memset(&q, 0, sizeof(q)); | ||
1155 | memset(&q2, 0, sizeof(q2)); | ||
1156 | memset(&sentinel, 0, sizeof(sentinel)); | ||
1157 | cur = &sentinel; | ||
1158 | |||
1159 | buf = malloc(sizeof(*buf)); | ||
1160 | if (buf == NULL) { | ||
1161 | h_errno = NETDB_INTERNAL; | ||
1162 | return NULL; | ||
1163 | } | ||
1164 | buf2 = malloc(sizeof(*buf2)); | ||
1165 | if (buf2 == NULL) { | ||
1166 | free(buf); | ||
1167 | h_errno = NETDB_INTERNAL; | ||
1168 | return NULL; | ||
1169 | } | ||
1170 | |||
1171 | switch (pai->ai_family) { | ||
1172 | case AF_UNSPEC: | ||
1173 | /* respect user supplied order */ | ||
1174 | q.qclass = C_IN; | ||
1175 | q.qtype = (_resp->family[0] == AF_INET6) ? T_AAAA : T_A; | ||
1176 | q.answer = buf->buf; | ||
1177 | q.anslen = sizeof(buf->buf); | ||
1178 | q.next = &q2; | ||
1179 | |||
1180 | if (_resp->family[1] == -1) { | ||
1181 | /* stop here if only one family was given */ | ||
1182 | q.next = NULL; | ||
1183 | break; | ||
1184 | } | ||
1185 | |||
1186 | q2.qclass = C_IN; | ||
1187 | q2.qtype = (_resp->family[1] == AF_INET6) ? T_AAAA : T_A; | ||
1188 | q2.answer = buf2->buf; | ||
1189 | q2.anslen = sizeof(buf2->buf); | ||
1190 | break; | ||
1191 | case AF_INET: | ||
1192 | q.qclass = C_IN; | ||
1193 | q.qtype = T_A; | ||
1194 | q.answer = buf->buf; | ||
1195 | q.anslen = sizeof(buf->buf); | ||
1196 | break; | ||
1197 | case AF_INET6: | ||
1198 | q.qclass = C_IN; | ||
1199 | q.qtype = T_AAAA; | ||
1200 | q.answer = buf->buf; | ||
1201 | q.anslen = sizeof(buf->buf); | ||
1202 | break; | ||
1203 | default: | ||
1204 | free(buf); | ||
1205 | free(buf2); | ||
1206 | return NULL; | ||
1207 | } | ||
1208 | if (res_searchN(name, &q) < 0) { | ||
1209 | free(buf); | ||
1210 | free(buf2); | ||
1211 | return NULL; | ||
1212 | } | ||
1213 | ai = getanswer(buf, q.n, q.name, q.qtype, pai); | ||
1214 | if (ai) { | ||
1215 | cur->ai_next = ai; | ||
1216 | while (cur && cur->ai_next) | ||
1217 | cur = cur->ai_next; | ||
1218 | } | ||
1219 | if (q.next) { | ||
1220 | ai = getanswer(buf2, q2.n, q2.name, q2.qtype, pai); | ||
1221 | if (ai) | ||
1222 | cur->ai_next = ai; | ||
1223 | } | ||
1224 | free(buf); | ||
1225 | free(buf2); | ||
1226 | return sentinel.ai_next; | ||
1227 | } | ||
1228 | |||
1229 | static struct addrinfo * | ||
1230 | _gethtent(const char *name, const struct addrinfo *pai, FILE *hostf) | ||
1231 | { | ||
1232 | char *p; | ||
1233 | char *cp, *tname, *cname; | ||
1234 | struct addrinfo hints, *res0, *res; | ||
1235 | int error; | ||
1236 | const char *addr; | ||
1237 | char hostbuf[8*1024]; | ||
1238 | |||
1239 | again: | ||
1240 | if (!(p = fgets(hostbuf, sizeof hostbuf, hostf))) | ||
1241 | return (NULL); | ||
1242 | if (*p == '#') | ||
1243 | goto again; | ||
1244 | if (!(cp = strpbrk(p, "#\n"))) | ||
1245 | goto again; | ||
1246 | *cp = '\0'; | ||
1247 | if (!(cp = strpbrk(p, " \t"))) | ||
1248 | goto again; | ||
1249 | *cp++ = '\0'; | ||
1250 | addr = p; | ||
1251 | /* if this is not something we're looking for, skip it. */ | ||
1252 | cname = NULL; | ||
1253 | while (cp && *cp) { | ||
1254 | if (*cp == ' ' || *cp == '\t') { | ||
1255 | cp++; | ||
1256 | continue; | ||
1257 | } | ||
1258 | if (!cname) | ||
1259 | cname = cp; | ||
1260 | tname = cp; | ||
1261 | if ((cp = strpbrk(cp, " \t")) != NULL) | ||
1262 | *cp++ = '\0'; | ||
1263 | if (strcasecmp(name, tname) == 0) | ||
1264 | goto found; | ||
1265 | } | ||
1266 | goto again; | ||
1267 | |||
1268 | found: | ||
1269 | hints = *pai; | ||
1270 | hints.ai_flags = AI_NUMERICHOST; | ||
1271 | error = getaddrinfo(addr, NULL, &hints, &res0); | ||
1272 | if (error) | ||
1273 | goto again; | ||
1274 | for (res = res0; res; res = res->ai_next) { | ||
1275 | /* cover it up */ | ||
1276 | res->ai_flags = pai->ai_flags; | ||
1277 | |||
1278 | if (pai->ai_flags & AI_CANONNAME) { | ||
1279 | if (get_canonname(pai, res, cname) != 0) { | ||
1280 | freeaddrinfo(res0); | ||
1281 | goto again; | ||
1282 | } | ||
1283 | } | ||
1284 | } | ||
1285 | return res0; | ||
1286 | } | ||
1287 | |||
1288 | /*ARGSUSED*/ | ||
1289 | static struct addrinfo * | ||
1290 | _files_getaddrinfo(const char *name, const struct addrinfo *pai) | ||
1291 | { | ||
1292 | struct addrinfo sentinel, *cur; | ||
1293 | struct addrinfo *p; | ||
1294 | FILE *hostf; | ||
1295 | |||
1296 | hostf = fopen(_PATH_HOSTS, "r"); | ||
1297 | if (hostf == NULL) | ||
1298 | return NULL; | ||
1299 | |||
1300 | memset(&sentinel, 0, sizeof(sentinel)); | ||
1301 | cur = &sentinel; | ||
1302 | |||
1303 | while ((p = _gethtent(name, pai, hostf)) != NULL) { | ||
1304 | cur->ai_next = p; | ||
1305 | while (cur && cur->ai_next) | ||
1306 | cur = cur->ai_next; | ||
1307 | } | ||
1308 | fclose(hostf); | ||
1309 | |||
1310 | return sentinel.ai_next; | ||
1311 | } | ||
1312 | |||
1313 | #ifdef YP | ||
1314 | static char *__ypdomain; | ||
1315 | |||
1316 | /*ARGSUSED*/ | ||
1317 | static struct addrinfo * | ||
1318 | _yphostent(char *line, const struct addrinfo *pai) | ||
1319 | { | ||
1320 | struct addrinfo sentinel, *cur; | ||
1321 | struct addrinfo hints, *res, *res0; | ||
1322 | int error; | ||
1323 | char *p = line; | ||
1324 | const char *addr, *canonname; | ||
1325 | char *nextline; | ||
1326 | char *cp; | ||
1327 | |||
1328 | addr = canonname = NULL; | ||
1329 | |||
1330 | memset(&sentinel, 0, sizeof(sentinel)); | ||
1331 | cur = &sentinel; | ||
1332 | |||
1333 | nextline: | ||
1334 | /* terminate line */ | ||
1335 | cp = strchr(p, '\n'); | ||
1336 | if (cp) { | ||
1337 | *cp++ = '\0'; | ||
1338 | nextline = cp; | ||
1339 | } else | ||
1340 | nextline = NULL; | ||
1341 | |||
1342 | cp = strpbrk(p, " \t"); | ||
1343 | if (cp == NULL) { | ||
1344 | if (canonname == NULL) | ||
1345 | return (NULL); | ||
1346 | else | ||
1347 | goto done; | ||
1348 | } | ||
1349 | *cp++ = '\0'; | ||
1350 | |||
1351 | addr = p; | ||
1352 | |||
1353 | while (cp && *cp) { | ||
1354 | if (*cp == ' ' || *cp == '\t') { | ||
1355 | cp++; | ||
1356 | continue; | ||
1357 | } | ||
1358 | if (!canonname) | ||
1359 | canonname = cp; | ||
1360 | if ((cp = strpbrk(cp, " \t")) != NULL) | ||
1361 | *cp++ = '\0'; | ||
1362 | } | ||
1363 | |||
1364 | hints = *pai; | ||
1365 | hints.ai_flags = AI_NUMERICHOST; | ||
1366 | error = getaddrinfo(addr, NULL, &hints, &res0); | ||
1367 | if (error == 0) { | ||
1368 | for (res = res0; res; res = res->ai_next) { | ||
1369 | /* cover it up */ | ||
1370 | res->ai_flags = pai->ai_flags; | ||
1371 | |||
1372 | if (pai->ai_flags & AI_CANONNAME) | ||
1373 | (void)get_canonname(pai, res, canonname); | ||
1374 | } | ||
1375 | } else | ||
1376 | res0 = NULL; | ||
1377 | if (res0) { | ||
1378 | cur->ai_next = res0; | ||
1379 | while (cur && cur->ai_next) | ||
1380 | cur = cur->ai_next; | ||
1381 | } | ||
1382 | |||
1383 | if (nextline) { | ||
1384 | p = nextline; | ||
1385 | goto nextline; | ||
1386 | } | ||
1387 | |||
1388 | done: | ||
1389 | return sentinel.ai_next; | ||
1390 | } | ||
1391 | |||
1392 | /*ARGSUSED*/ | ||
1393 | static struct addrinfo * | ||
1394 | _yp_getaddrinfo(const char *name, const struct addrinfo *pai) | ||
1395 | { | ||
1396 | struct addrinfo sentinel, *cur; | ||
1397 | struct addrinfo *ai = NULL; | ||
1398 | static char *__ypcurrent; | ||
1399 | int __ypcurrentlen, r; | ||
1400 | |||
1401 | memset(&sentinel, 0, sizeof(sentinel)); | ||
1402 | cur = &sentinel; | ||
1403 | |||
1404 | if (!__ypdomain) { | ||
1405 | if (_yp_check(&__ypdomain) == 0) | ||
1406 | return NULL; | ||
1407 | } | ||
1408 | if (__ypcurrent) | ||
1409 | free(__ypcurrent); | ||
1410 | __ypcurrent = NULL; | ||
1411 | |||
1412 | /* hosts.byname is only for IPv4 (Solaris8) */ | ||
1413 | if (pai->ai_family == PF_UNSPEC || pai->ai_family == PF_INET) { | ||
1414 | r = yp_match(__ypdomain, "hosts.byname", name, | ||
1415 | (int)strlen(name), &__ypcurrent, &__ypcurrentlen); | ||
1416 | if (r == 0) { | ||
1417 | struct addrinfo ai4; | ||
1418 | |||
1419 | ai4 = *pai; | ||
1420 | ai4.ai_family = AF_INET; | ||
1421 | ai = _yphostent(__ypcurrent, &ai4); | ||
1422 | if (ai) { | ||
1423 | cur->ai_next = ai; | ||
1424 | while (cur && cur->ai_next) | ||
1425 | cur = cur->ai_next; | ||
1426 | } | ||
1427 | } | ||
1428 | } | ||
1429 | |||
1430 | /* ipnodes.byname can hold both IPv4/v6 */ | ||
1431 | r = yp_match(__ypdomain, "ipnodes.byname", name, | ||
1432 | (int)strlen(name), &__ypcurrent, &__ypcurrentlen); | ||
1433 | if (r == 0) { | ||
1434 | ai = _yphostent(__ypcurrent, pai); | ||
1435 | if (ai) { | ||
1436 | cur->ai_next = ai; | ||
1437 | while (cur && cur->ai_next) | ||
1438 | cur = cur->ai_next; | ||
1439 | } | ||
1440 | } | ||
1441 | |||
1442 | return sentinel.ai_next; | ||
1443 | } | ||
1444 | #endif | ||
1445 | |||
1446 | |||
1447 | /* resolver logic */ | ||
1448 | |||
1449 | extern const char *__hostalias(const char *); | ||
1450 | extern int h_errno; | ||
1451 | extern int res_opt(int, u_char *, int, int); | ||
1452 | |||
1453 | /* | ||
1454 | * Formulate a normal query, send, and await answer. | ||
1455 | * Returned answer is placed in supplied buffer "answer". | ||
1456 | * Perform preliminary check of answer, returning success only | ||
1457 | * if no error is indicated and the answer count is nonzero. | ||
1458 | * Return the size of the response on success, -1 on error. | ||
1459 | * Error number is left in h_errno. | ||
1460 | * | ||
1461 | * Caller must parse answer and determine whether it answers the question. | ||
1462 | */ | ||
1463 | static int | ||
1464 | res_queryN(const char *name, struct res_target *target) | ||
1465 | { | ||
1466 | struct __res_state *_resp = _THREAD_PRIVATE(_res, _res, &_res); | ||
1467 | u_char *buf; | ||
1468 | HEADER *hp; | ||
1469 | int n; | ||
1470 | struct res_target *t; | ||
1471 | int rcode; | ||
1472 | int ancount; | ||
1473 | |||
1474 | buf = malloc(MAXPACKET); | ||
1475 | if (buf == NULL) { | ||
1476 | h_errno = NETDB_INTERNAL; | ||
1477 | return (-1); | ||
1478 | } | ||
1479 | |||
1480 | rcode = NOERROR; | ||
1481 | ancount = 0; | ||
1482 | |||
1483 | if (_res_init(0) == -1) { | ||
1484 | h_errno = NETDB_INTERNAL; | ||
1485 | free(buf); | ||
1486 | return (-1); | ||
1487 | } | ||
1488 | |||
1489 | for (t = target; t; t = t->next) { | ||
1490 | int class, type; | ||
1491 | u_char *answer; | ||
1492 | int anslen; | ||
1493 | |||
1494 | hp = (HEADER *)(void *)t->answer; | ||
1495 | hp->rcode = NOERROR; /* default */ | ||
1496 | |||
1497 | /* make it easier... */ | ||
1498 | class = t->qclass; | ||
1499 | type = t->qtype; | ||
1500 | answer = t->answer; | ||
1501 | anslen = t->anslen; | ||
1502 | #ifdef DEBUG | ||
1503 | if (_resp->options & RES_DEBUG) | ||
1504 | printf(";; res_query(%s, %d, %d)\n", name, class, type); | ||
1505 | #endif | ||
1506 | |||
1507 | n = res_mkquery(QUERY, name, class, type, NULL, 0, NULL, | ||
1508 | buf, MAXPACKET); | ||
1509 | if (n > 0 && (_resp->options & RES_USE_EDNS0) != 0) | ||
1510 | n = res_opt(n, buf, MAXPACKET, anslen); | ||
1511 | if (n <= 0) { | ||
1512 | #ifdef DEBUG | ||
1513 | if (_resp->options & RES_DEBUG) | ||
1514 | printf(";; res_query: mkquery failed\n"); | ||
1515 | #endif | ||
1516 | h_errno = NO_RECOVERY; | ||
1517 | free(buf); | ||
1518 | return (n); | ||
1519 | } | ||
1520 | n = res_send(buf, n, answer, anslen); | ||
1521 | #if 0 | ||
1522 | if (n < 0) { | ||
1523 | #ifdef DEBUG | ||
1524 | if (_resp->options & RES_DEBUG) | ||
1525 | printf(";; res_query: send error\n"); | ||
1526 | #endif | ||
1527 | h_errno = TRY_AGAIN; | ||
1528 | free(buf); | ||
1529 | return (n); | ||
1530 | } | ||
1531 | #endif | ||
1532 | |||
1533 | if (n < 0 || hp->rcode != NOERROR || ntohs(hp->ancount) == 0) { | ||
1534 | rcode = hp->rcode; /* record most recent error */ | ||
1535 | #ifdef DEBUG | ||
1536 | if (_resp->options & RES_DEBUG) | ||
1537 | printf(";; rcode = %u, ancount=%u\n", hp->rcode, | ||
1538 | ntohs(hp->ancount)); | ||
1539 | #endif | ||
1540 | continue; | ||
1541 | } | ||
1542 | |||
1543 | ancount += ntohs(hp->ancount); | ||
1544 | |||
1545 | t->n = n; | ||
1546 | } | ||
1547 | |||
1548 | if (ancount == 0) { | ||
1549 | switch (rcode) { | ||
1550 | case NXDOMAIN: | ||
1551 | h_errno = HOST_NOT_FOUND; | ||
1552 | break; | ||
1553 | case SERVFAIL: | ||
1554 | h_errno = TRY_AGAIN; | ||
1555 | break; | ||
1556 | case NOERROR: | ||
1557 | h_errno = NO_DATA; | ||
1558 | break; | ||
1559 | case FORMERR: | ||
1560 | case NOTIMP: | ||
1561 | case REFUSED: | ||
1562 | default: | ||
1563 | h_errno = NO_RECOVERY; | ||
1564 | break; | ||
1565 | } | ||
1566 | free(buf); | ||
1567 | return (-1); | ||
1568 | } | ||
1569 | free(buf); | ||
1570 | return (ancount); | ||
1571 | } | ||
1572 | |||
1573 | /* | ||
1574 | * Formulate a normal query, send, and retrieve answer in supplied buffer. | ||
1575 | * Return the size of the response on success, -1 on error. | ||
1576 | * If enabled, implement search rules until answer or unrecoverable failure | ||
1577 | * is detected. Error code, if any, is left in h_errno. | ||
1578 | */ | ||
1579 | static int | ||
1580 | res_searchN(const char *name, struct res_target *target) | ||
1581 | { | ||
1582 | struct __res_state *_resp = _THREAD_PRIVATE(_res, _res, &_res); | ||
1583 | const char *cp, * const *domain; | ||
1584 | HEADER *hp = (HEADER *)(void *)target->answer; /*XXX*/ | ||
1585 | u_int dots; | ||
1586 | int trailing_dot, ret, saved_herrno; | ||
1587 | int got_nodata = 0, got_servfail = 0, tried_as_is = 0; | ||
1588 | |||
1589 | if (_res_init(0) == -1) { | ||
1590 | h_errno = NETDB_INTERNAL; | ||
1591 | return (-1); | ||
1592 | } | ||
1593 | |||
1594 | errno = 0; | ||
1595 | h_errno = HOST_NOT_FOUND; /* default, if we never query */ | ||
1596 | dots = 0; | ||
1597 | for (cp = name; *cp; cp++) | ||
1598 | dots += (*cp == '.'); | ||
1599 | trailing_dot = 0; | ||
1600 | if (cp > name && *--cp == '.') | ||
1601 | trailing_dot++; | ||
1602 | |||
1603 | /* | ||
1604 | * if there aren't any dots, it could be a user-level alias | ||
1605 | */ | ||
1606 | if (!dots && (cp = __hostalias(name)) != NULL) | ||
1607 | return (res_queryN(cp, target)); | ||
1608 | |||
1609 | /* | ||
1610 | * If there are dots in the name already, let's just give it a try | ||
1611 | * 'as is'. The threshold can be set with the "ndots" option. | ||
1612 | */ | ||
1613 | saved_herrno = -1; | ||
1614 | if (dots >= _resp->ndots) { | ||
1615 | ret = res_querydomainN(name, NULL, target); | ||
1616 | if (ret > 0) | ||
1617 | return (ret); | ||
1618 | saved_herrno = h_errno; | ||
1619 | tried_as_is++; | ||
1620 | } | ||
1621 | |||
1622 | /* | ||
1623 | * We do at least one level of search if | ||
1624 | * - there is no dot and RES_DEFNAME is set, or | ||
1625 | * - there is at least one dot, there is no trailing dot, | ||
1626 | * and RES_DNSRCH is set. | ||
1627 | */ | ||
1628 | if ((!dots && (_resp->options & RES_DEFNAMES)) || | ||
1629 | (dots && !trailing_dot && (_resp->options & RES_DNSRCH))) { | ||
1630 | int done = 0; | ||
1631 | |||
1632 | for (domain = (const char * const *)_resp->dnsrch; | ||
1633 | *domain && !done; | ||
1634 | domain++) { | ||
1635 | |||
1636 | ret = res_querydomainN(name, *domain, target); | ||
1637 | if (ret > 0) | ||
1638 | return (ret); | ||
1639 | |||
1640 | /* | ||
1641 | * If no server present, give up. | ||
1642 | * If name isn't found in this domain, | ||
1643 | * keep trying higher domains in the search list | ||
1644 | * (if that's enabled). | ||
1645 | * On a NO_DATA error, keep trying, otherwise | ||
1646 | * a wildcard entry of another type could keep us | ||
1647 | * from finding this entry higher in the domain. | ||
1648 | * If we get some other error (negative answer or | ||
1649 | * server failure), then stop searching up, | ||
1650 | * but try the input name below in case it's | ||
1651 | * fully-qualified. | ||
1652 | */ | ||
1653 | if (errno == ECONNREFUSED) { | ||
1654 | h_errno = TRY_AGAIN; | ||
1655 | return (-1); | ||
1656 | } | ||
1657 | |||
1658 | switch (h_errno) { | ||
1659 | case NO_DATA: | ||
1660 | got_nodata++; | ||
1661 | /* FALLTHROUGH */ | ||
1662 | case HOST_NOT_FOUND: | ||
1663 | /* keep trying */ | ||
1664 | break; | ||
1665 | case TRY_AGAIN: | ||
1666 | if (hp->rcode == SERVFAIL) { | ||
1667 | /* try next search element, if any */ | ||
1668 | got_servfail++; | ||
1669 | break; | ||
1670 | } | ||
1671 | /* FALLTHROUGH */ | ||
1672 | default: | ||
1673 | /* anything else implies that we're done */ | ||
1674 | done++; | ||
1675 | } | ||
1676 | /* | ||
1677 | * if we got here for some reason other than DNSRCH, | ||
1678 | * we only wanted one iteration of the loop, so stop. | ||
1679 | */ | ||
1680 | if (!(_resp->options & RES_DNSRCH)) | ||
1681 | done++; | ||
1682 | } | ||
1683 | } | ||
1684 | |||
1685 | /* | ||
1686 | * if we have not already tried the name "as is", do that now. | ||
1687 | * note that we do this regardless of how many dots were in the | ||
1688 | * name or whether it ends with a dot. | ||
1689 | */ | ||
1690 | if (!tried_as_is) { | ||
1691 | ret = res_querydomainN(name, NULL, target); | ||
1692 | if (ret > 0) | ||
1693 | return (ret); | ||
1694 | } | ||
1695 | |||
1696 | /* | ||
1697 | * if we got here, we didn't satisfy the search. | ||
1698 | * if we did an initial full query, return that query's h_errno | ||
1699 | * (note that we wouldn't be here if that query had succeeded). | ||
1700 | * else if we ever got a nodata, send that back as the reason. | ||
1701 | * else send back meaningless h_errno, that being the one from | ||
1702 | * the last DNSRCH we did. | ||
1703 | */ | ||
1704 | if (saved_herrno != -1) | ||
1705 | h_errno = saved_herrno; | ||
1706 | else if (got_nodata) | ||
1707 | h_errno = NO_DATA; | ||
1708 | else if (got_servfail) | ||
1709 | h_errno = TRY_AGAIN; | ||
1710 | return (-1); | ||
1711 | } | ||
1712 | |||
1713 | /* | ||
1714 | * Perform a call on res_query on the concatenation of name and domain, | ||
1715 | * removing a trailing dot from name if domain is NULL. | ||
1716 | */ | ||
1717 | static int | ||
1718 | res_querydomainN(const char *name, const char *domain, | ||
1719 | struct res_target *target) | ||
1720 | { | ||
1721 | struct __res_state *_resp = _THREAD_PRIVATE(_res, _res, &_res); | ||
1722 | char nbuf[MAXDNAME]; | ||
1723 | const char *longname = nbuf; | ||
1724 | size_t len; | ||
1725 | |||
1726 | if (_res_init(0) == -1) { | ||
1727 | h_errno = NETDB_INTERNAL; | ||
1728 | return (-1); | ||
1729 | } | ||
1730 | #ifdef DEBUG | ||
1731 | if (_resp->options & RES_DEBUG) | ||
1732 | printf(";; res_querydomain(%s, %s)\n", | ||
1733 | name, domain?domain:"<Nil>"); | ||
1734 | #endif | ||
1735 | if (domain == NULL) { | ||
1736 | /* | ||
1737 | * Check for trailing '.'; | ||
1738 | * copy without '.' if present. | ||
1739 | */ | ||
1740 | len = strlcpy(nbuf, name, sizeof(nbuf)); | ||
1741 | if (len >= sizeof(nbuf)) { | ||
1742 | h_errno = NO_RECOVERY; | ||
1743 | return (-1); | ||
1744 | } | ||
1745 | if (len > 0 && nbuf[len - 1] == '.') | ||
1746 | nbuf[len - 1] = '\0'; | ||
1747 | } else { | ||
1748 | int i; | ||
1749 | |||
1750 | i = snprintf(nbuf, sizeof(nbuf), "%s.%s", name, domain); | ||
1751 | if (i < 0 || i >= sizeof(nbuf)) { | ||
1752 | h_errno = NO_RECOVERY; | ||
1753 | return (-1); | ||
1754 | } | ||
1755 | } | ||
1756 | return (res_queryN(longname, target)); | ||
1757 | } | ||