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