getaddrinfo.c revision 1.125 1 1.125 christos /* $NetBSD: getaddrinfo.c,v 1.125 2024/01/20 14:52:48 christos Exp $ */
2 1.48 itojun /* $KAME: getaddrinfo.c,v 1.29 2000/08/31 17:26:57 itojun Exp $ */
3 1.6 itojun
4 1.1 itojun /*
5 1.1 itojun * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
6 1.1 itojun * All rights reserved.
7 1.42 itojun *
8 1.1 itojun * Redistribution and use in source and binary forms, with or without
9 1.1 itojun * modification, are permitted provided that the following conditions
10 1.1 itojun * are met:
11 1.1 itojun * 1. Redistributions of source code must retain the above copyright
12 1.1 itojun * notice, this list of conditions and the following disclaimer.
13 1.1 itojun * 2. Redistributions in binary form must reproduce the above copyright
14 1.1 itojun * notice, this list of conditions and the following disclaimer in the
15 1.1 itojun * documentation and/or other materials provided with the distribution.
16 1.1 itojun * 3. Neither the name of the project nor the names of its contributors
17 1.1 itojun * may be used to endorse or promote products derived from this software
18 1.1 itojun * without specific prior written permission.
19 1.42 itojun *
20 1.1 itojun * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
21 1.1 itojun * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 1.1 itojun * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 1.1 itojun * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
24 1.1 itojun * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 1.1 itojun * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 1.1 itojun * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 1.1 itojun * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 1.1 itojun * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 1.1 itojun * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 1.1 itojun * SUCH DAMAGE.
31 1.1 itojun */
32 1.1 itojun
33 1.1 itojun /*
34 1.1 itojun * Issues to be discussed:
35 1.1 itojun * - Return values. There are nonstandard return values defined and used
36 1.14 itojun * in the source code. This is because RFC2553 is silent about which error
37 1.1 itojun * code must be returned for which situation.
38 1.27 itojun * - IPv4 classful (shortened) form. RFC2553 is silent about it. XNET 5.2
39 1.122 riastrad * says to use inet_aton() to convert IPv4 numeric to binary (allows
40 1.27 itojun * classful form as a result).
41 1.27 itojun * current code - disallow classful form for IPv4 (due to use of inet_pton).
42 1.27 itojun * - freeaddrinfo(NULL). RFC2553 is silent about it. XNET 5.2 says it is
43 1.27 itojun * invalid.
44 1.27 itojun * current code - SEGV on freeaddrinfo(NULL)
45 1.14 itojun * Note:
46 1.14 itojun * - The code filters out AFs that are not supported by the kernel,
47 1.22 itojun * when globbing NULL hostname (to loopback, or wildcard). Is it the right
48 1.14 itojun * thing to do? What is the relationship with post-RFC2553 AI_ADDRCONFIG
49 1.14 itojun * in ai_flags?
50 1.30 itojun * - (post-2553) semantics of AI_ADDRCONFIG itself is too vague.
51 1.30 itojun * (1) what should we do against numeric hostname (2) what should we do
52 1.30 itojun * against NULL hostname (3) what is AI_ADDRCONFIG itself. AF not ready?
53 1.30 itojun * non-loopback address configured? global address configured?
54 1.1 itojun */
55 1.39 itojun
56 1.39 itojun #include <sys/cdefs.h>
57 1.39 itojun #if defined(LIBC_SCCS) && !defined(lint)
58 1.125 christos __RCSID("$NetBSD: getaddrinfo.c,v 1.125 2024/01/20 14:52:48 christos Exp $");
59 1.39 itojun #endif /* LIBC_SCCS and not lint */
60 1.1 itojun
61 1.107 ozaki #ifndef RUMP_ACTION
62 1.38 itojun #include "namespace.h"
63 1.107 ozaki #endif
64 1.14 itojun #include <sys/types.h>
65 1.1 itojun #include <sys/param.h>
66 1.1 itojun #include <sys/socket.h>
67 1.112 christos #include <sys/ioctl.h>
68 1.112 christos #include <sys/sysctl.h>
69 1.14 itojun #include <net/if.h>
70 1.1 itojun #include <netinet/in.h>
71 1.112 christos #include <netinet6/in6_var.h>
72 1.1 itojun #include <arpa/inet.h>
73 1.1 itojun #include <arpa/nameser.h>
74 1.51 lukem #include <assert.h>
75 1.51 lukem #include <ctype.h>
76 1.51 lukem #include <errno.h>
77 1.1 itojun #include <netdb.h>
78 1.1 itojun #include <resolv.h>
79 1.51 lukem #include <stddef.h>
80 1.51 lukem #include <stdio.h>
81 1.51 lukem #include <stdlib.h>
82 1.14 itojun #include <string.h>
83 1.1 itojun #include <unistd.h>
84 1.98 christos #include <ifaddrs.h>
85 1.14 itojun
86 1.32 itojun #include <syslog.h>
87 1.32 itojun #include <stdarg.h>
88 1.32 itojun #include <nsswitch.h>
89 1.32 itojun
90 1.32 itojun #ifdef YP
91 1.32 itojun #include <rpc/rpc.h>
92 1.32 itojun #include <rpcsvc/yp_prot.h>
93 1.32 itojun #include <rpcsvc/ypclnt.h>
94 1.38 itojun #endif
95 1.38 itojun
96 1.84 christos #include "servent.h"
97 1.84 christos
98 1.107 ozaki #ifndef RUMP_ACTION
99 1.38 itojun #ifdef __weak_alias
100 1.38 itojun __weak_alias(getaddrinfo,_getaddrinfo)
101 1.109 christos __weak_alias(allocaddrinfo,_allocaddrinfo)
102 1.38 itojun __weak_alias(freeaddrinfo,_freeaddrinfo)
103 1.38 itojun __weak_alias(gai_strerror,_gai_strerror)
104 1.32 itojun #endif
105 1.107 ozaki #endif
106 1.32 itojun
107 1.1 itojun #define SUCCESS 0
108 1.1 itojun #define ANY 0
109 1.1 itojun #define YES 1
110 1.1 itojun #define NO 0
111 1.1 itojun
112 1.115 christos #define sa4addr(sa) ((void *)&((struct sockaddr_in *)(void *)sa)->sin_addr)
113 1.115 christos #define sa6addr(sa) ((void *)&((struct sockaddr_in6 *)(void *)sa)->sin6_addr)
114 1.115 christos
115 1.1 itojun static const char in_addrany[] = { 0, 0, 0, 0 };
116 1.49 lukem static const char in_loopback[] = { 127, 0, 0, 1 };
117 1.49 lukem #ifdef INET6
118 1.1 itojun static const char in6_addrany[] = {
119 1.1 itojun 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
120 1.1 itojun };
121 1.1 itojun static const char in6_loopback[] = {
122 1.1 itojun 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1
123 1.1 itojun };
124 1.49 lukem #endif
125 1.1 itojun
126 1.112 christos struct policyqueue {
127 1.112 christos TAILQ_ENTRY(policyqueue) pc_entry;
128 1.112 christos #ifdef INET6
129 1.112 christos struct in6_addrpolicy pc_policy;
130 1.112 christos #endif
131 1.112 christos };
132 1.112 christos TAILQ_HEAD(policyhead, policyqueue);
133 1.112 christos
134 1.14 itojun static const struct afd {
135 1.1 itojun int a_af;
136 1.1 itojun int a_addrlen;
137 1.1 itojun int a_socklen;
138 1.1 itojun int a_off;
139 1.1 itojun const char *a_addrany;
140 1.102 christos const char *a_loopback;
141 1.14 itojun int a_scoped;
142 1.1 itojun } afdl [] = {
143 1.1 itojun #ifdef INET6
144 1.1 itojun {PF_INET6, sizeof(struct in6_addr),
145 1.1 itojun sizeof(struct sockaddr_in6),
146 1.1 itojun offsetof(struct sockaddr_in6, sin6_addr),
147 1.14 itojun in6_addrany, in6_loopback, 1},
148 1.1 itojun #endif
149 1.1 itojun {PF_INET, sizeof(struct in_addr),
150 1.1 itojun sizeof(struct sockaddr_in),
151 1.1 itojun offsetof(struct sockaddr_in, sin_addr),
152 1.14 itojun in_addrany, in_loopback, 0},
153 1.14 itojun {0, 0, 0, 0, NULL, NULL, 0},
154 1.14 itojun };
155 1.14 itojun
156 1.14 itojun struct explore {
157 1.14 itojun int e_af;
158 1.14 itojun int e_socktype;
159 1.14 itojun int e_protocol;
160 1.14 itojun const char *e_protostr;
161 1.14 itojun int e_wild;
162 1.14 itojun #define WILD_AF(ex) ((ex)->e_wild & 0x01)
163 1.14 itojun #define WILD_SOCKTYPE(ex) ((ex)->e_wild & 0x02)
164 1.14 itojun #define WILD_PROTOCOL(ex) ((ex)->e_wild & 0x04)
165 1.14 itojun };
166 1.14 itojun
167 1.14 itojun static const struct explore explore[] = {
168 1.14 itojun #if 0
169 1.14 itojun { PF_LOCAL, 0, ANY, ANY, NULL, 0x01 },
170 1.14 itojun #endif
171 1.14 itojun #ifdef INET6
172 1.14 itojun { PF_INET6, SOCK_DGRAM, IPPROTO_UDP, "udp", 0x07 },
173 1.14 itojun { PF_INET6, SOCK_STREAM, IPPROTO_TCP, "tcp", 0x07 },
174 1.14 itojun { PF_INET6, SOCK_RAW, ANY, NULL, 0x05 },
175 1.14 itojun #endif
176 1.14 itojun { PF_INET, SOCK_DGRAM, IPPROTO_UDP, "udp", 0x07 },
177 1.14 itojun { PF_INET, SOCK_STREAM, IPPROTO_TCP, "tcp", 0x07 },
178 1.14 itojun { PF_INET, SOCK_RAW, ANY, NULL, 0x05 },
179 1.32 itojun { PF_UNSPEC, SOCK_DGRAM, IPPROTO_UDP, "udp", 0x07 },
180 1.32 itojun { PF_UNSPEC, SOCK_STREAM, IPPROTO_TCP, "tcp", 0x07 },
181 1.32 itojun { PF_UNSPEC, SOCK_RAW, ANY, NULL, 0x05 },
182 1.14 itojun { -1, 0, 0, NULL, 0 },
183 1.1 itojun };
184 1.1 itojun
185 1.1 itojun #ifdef INET6
186 1.1 itojun #define PTON_MAX 16
187 1.1 itojun #else
188 1.1 itojun #define PTON_MAX 4
189 1.1 itojun #endif
190 1.1 itojun
191 1.112 christos #define AIO_SRCFLAG_DEPRECATED 0x1
192 1.112 christos
193 1.112 christos struct ai_order {
194 1.112 christos union {
195 1.112 christos struct sockaddr_storage aiou_ss;
196 1.112 christos struct sockaddr aiou_sa;
197 1.112 christos } aio_src_un;
198 1.112 christos #define aio_srcsa aio_src_un.aiou_sa
199 1.112 christos u_int32_t aio_srcflag;
200 1.112 christos int aio_srcscope;
201 1.112 christos int aio_dstscope;
202 1.112 christos struct policyqueue *aio_srcpolicy;
203 1.112 christos struct policyqueue *aio_dstpolicy;
204 1.112 christos struct addrinfo *aio_ai;
205 1.112 christos int aio_matchlen;
206 1.112 christos };
207 1.112 christos
208 1.32 itojun static const ns_src default_dns_files[] = {
209 1.102 christos { NSSRC_FILES, NS_SUCCESS },
210 1.102 christos { NSSRC_DNS, NS_SUCCESS },
211 1.87 christos { 0, 0 }
212 1.32 itojun };
213 1.32 itojun
214 1.63 itojun #define MAXPACKET (64*1024)
215 1.32 itojun
216 1.32 itojun typedef union {
217 1.32 itojun HEADER hdr;
218 1.32 itojun u_char buf[MAXPACKET];
219 1.32 itojun } querybuf;
220 1.32 itojun
221 1.32 itojun struct res_target {
222 1.32 itojun struct res_target *next;
223 1.32 itojun const char *name; /* domain name */
224 1.43 itojun int qclass, qtype; /* class and type of query */
225 1.32 itojun u_char *answer; /* buffer to put answer */
226 1.32 itojun int anslen; /* size of answer buffer */
227 1.32 itojun int n; /* result length */
228 1.32 itojun };
229 1.1 itojun
230 1.103 christos struct srvinfo {
231 1.103 christos struct srvinfo *next;
232 1.103 christos char name[MAXDNAME];
233 1.103 christos int port, pri, weight;
234 1.103 christos };
235 1.103 christos
236 1.103 christos static int gai_srvok(const char *);
237 1.70 christos static int str2number(const char *);
238 1.70 christos static int explore_fqdn(const struct addrinfo *, const char *,
239 1.102 christos const char *, struct addrinfo **, struct servent_data *);
240 1.70 christos static int explore_null(const struct addrinfo *,
241 1.102 christos const char *, struct addrinfo **, struct servent_data *);
242 1.70 christos static int explore_numeric(const struct addrinfo *, const char *,
243 1.102 christos const char *, struct addrinfo **, const char *, struct servent_data *);
244 1.70 christos static int explore_numeric_scope(const struct addrinfo *, const char *,
245 1.102 christos const char *, struct addrinfo **, struct servent_data *);
246 1.70 christos static int get_canonname(const struct addrinfo *,
247 1.102 christos struct addrinfo *, const char *);
248 1.70 christos static struct addrinfo *get_ai(const struct addrinfo *,
249 1.102 christos const struct afd *, const char *);
250 1.89 christos static int get_portmatch(const struct addrinfo *, const char *,
251 1.89 christos struct servent_data *);
252 1.89 christos static int get_port(const struct addrinfo *, const char *, int,
253 1.89 christos struct servent_data *);
254 1.70 christos static const struct afd *find_afd(int);
255 1.98 christos static int addrconfig(uint64_t *);
256 1.112 christos static void set_source(struct ai_order *, struct policyhead *,
257 1.112 christos struct servent_data *);
258 1.112 christos static int comp_dst(const void *, const void *);
259 1.27 itojun #ifdef INET6
260 1.70 christos static int ip6_str2scopeid(char *, struct sockaddr_in6 *, u_int32_t *);
261 1.42 itojun #endif
262 1.112 christos static int gai_addr2scopetype(struct sockaddr *);
263 1.112 christos
264 1.112 christos static int reorder(struct addrinfo *, struct servent_data *);
265 1.112 christos static int get_addrselectpolicy(struct policyhead *);
266 1.112 christos static void free_addrselectpolicy(struct policyhead *);
267 1.112 christos static struct policyqueue *match_addrselectpolicy(struct sockaddr *,
268 1.112 christos struct policyhead *);
269 1.112 christos static int matchlen(struct sockaddr *, struct sockaddr *);
270 1.14 itojun
271 1.110 christos static struct addrinfo *getanswer(res_state, const querybuf *, int,
272 1.110 christos const char *, int, const struct addrinfo *);
273 1.73 tsarna static void aisort(struct addrinfo *s, res_state res);
274 1.103 christos static struct addrinfo * _dns_query(struct res_target *,
275 1.103 christos const struct addrinfo *, res_state, int);
276 1.103 christos static struct addrinfo * _dns_srv_lookup(const char *, const char *,
277 1.103 christos const struct addrinfo *);
278 1.103 christos static struct addrinfo * _dns_host_lookup(const char *,
279 1.103 christos const struct addrinfo *);
280 1.70 christos static int _dns_getaddrinfo(void *, void *, va_list);
281 1.70 christos static void _sethtent(FILE **);
282 1.70 christos static void _endhtent(FILE **);
283 1.70 christos static struct addrinfo *_gethtent(FILE **, const char *,
284 1.70 christos const struct addrinfo *);
285 1.70 christos static int _files_getaddrinfo(void *, void *, va_list);
286 1.32 itojun #ifdef YP
287 1.70 christos static struct addrinfo *_yphostent(char *, const struct addrinfo *);
288 1.70 christos static int _yp_getaddrinfo(void *, void *, va_list);
289 1.32 itojun #endif
290 1.32 itojun
291 1.70 christos static int res_queryN(const char *, struct res_target *, res_state);
292 1.73 tsarna static int res_searchN(const char *, struct res_target *, res_state);
293 1.70 christos static int res_querydomainN(const char *, const char *,
294 1.102 christos struct res_target *, res_state);
295 1.32 itojun
296 1.53 jdolecek static const char * const ai_errlist[] = {
297 1.7 lukem "Success",
298 1.7 lukem "Address family for hostname not supported", /* EAI_ADDRFAMILY */
299 1.102 christos "Temporary failure in name resolution", /* EAI_AGAIN */
300 1.102 christos "Invalid value for ai_flags", /* EAI_BADFLAGS */
301 1.102 christos "Non-recoverable failure in name resolution", /* EAI_FAIL */
302 1.102 christos "ai_family not supported", /* EAI_FAMILY */
303 1.102 christos "Memory allocation failure", /* EAI_MEMORY */
304 1.102 christos "No address associated with hostname", /* EAI_NODATA */
305 1.119 dholland "hostname or servname not provided or not known", /* EAI_NONAME */
306 1.102 christos "servname not supported for ai_socktype", /* EAI_SERVICE */
307 1.102 christos "ai_socktype not supported", /* EAI_SOCKTYPE */
308 1.102 christos "System error returned in errno", /* EAI_SYSTEM */
309 1.7 lukem "Invalid value for hints", /* EAI_BADHINTS */
310 1.102 christos "Resolved protocol is unknown", /* EAI_PROTOCOL */
311 1.102 christos "Argument buffer overflow", /* EAI_OVERFLOW */
312 1.102 christos "Unknown error", /* EAI_MAX */
313 1.1 itojun };
314 1.1 itojun
315 1.14 itojun /* XXX macros that make external reference is BAD. */
316 1.14 itojun
317 1.102 christos #define GET_AI(ai, afd, addr) \
318 1.102 christos do { \
319 1.102 christos /* external reference: pai, error, and label free */ \
320 1.102 christos (ai) = get_ai(pai, (afd), (addr)); \
321 1.102 christos if ((ai) == NULL) { \
322 1.102 christos error = EAI_MEMORY; \
323 1.102 christos goto free; \
324 1.102 christos } \
325 1.123 rillig } while (0)
326 1.14 itojun
327 1.102 christos #define GET_PORT(ai, serv, svd) \
328 1.102 christos do { \
329 1.102 christos /* external reference: error and label free */ \
330 1.102 christos error = get_port((ai), (serv), 0, (svd)); \
331 1.102 christos if (error != 0) \
332 1.102 christos goto free; \
333 1.123 rillig } while (0)
334 1.14 itojun
335 1.102 christos #define GET_CANONNAME(ai, str) \
336 1.102 christos do { \
337 1.102 christos /* external reference: pai, error and label free */ \
338 1.102 christos error = get_canonname(pai, (ai), (str)); \
339 1.102 christos if (error != 0) \
340 1.102 christos goto free; \
341 1.123 rillig } while (0)
342 1.14 itojun
343 1.102 christos #define ERR(err) \
344 1.102 christos do { \
345 1.102 christos /* external reference: error, and label bad */ \
346 1.102 christos error = (err); \
347 1.102 christos goto bad; \
348 1.102 christos /*NOTREACHED*/ \
349 1.123 rillig } while (0)
350 1.14 itojun
351 1.102 christos #define MATCH_FAMILY(x, y, w) \
352 1.102 christos ((x) == (y) || (/*CONSTCOND*/(w) && ((x) == PF_UNSPEC || \
353 1.102 christos (y) == PF_UNSPEC)))
354 1.102 christos #define MATCH(x, y, w) \
355 1.20 mycroft ((x) == (y) || (/*CONSTCOND*/(w) && ((x) == ANY || (y) == ANY)))
356 1.1 itojun
357 1.70 christos const char *
358 1.70 christos gai_strerror(int ecode)
359 1.1 itojun {
360 1.1 itojun if (ecode < 0 || ecode > EAI_MAX)
361 1.1 itojun ecode = EAI_MAX;
362 1.70 christos return ai_errlist[ecode];
363 1.1 itojun }
364 1.1 itojun
365 1.1 itojun void
366 1.70 christos freeaddrinfo(struct addrinfo *ai)
367 1.1 itojun {
368 1.1 itojun struct addrinfo *next;
369 1.1 itojun
370 1.51 lukem _DIAGASSERT(ai != NULL);
371 1.51 lukem
372 1.26 itojun do {
373 1.1 itojun next = ai->ai_next;
374 1.1 itojun if (ai->ai_canonname)
375 1.1 itojun free(ai->ai_canonname);
376 1.1 itojun /* no need to free(ai->ai_addr) */
377 1.1 itojun free(ai);
378 1.27 itojun ai = next;
379 1.27 itojun } while (ai);
380 1.1 itojun }
381 1.1 itojun
382 1.103 christos /*
383 1.103 christos * We don't want localization to affect us
384 1.103 christos */
385 1.103 christos #define PERIOD '.'
386 1.103 christos #define hyphenchar(c) ((c) == '-')
387 1.103 christos #define periodchar(c) ((c) == PERIOD)
388 1.103 christos #define underschar(c) ((c) == '_')
389 1.103 christos #define alphachar(c) (((c) >= 'a' && (c) <= 'z') || ((c) >= 'A' && (c) <= 'Z'))
390 1.103 christos #define digitchar(c) ((c) >= '0' && (c) <= '9')
391 1.103 christos
392 1.103 christos #define firstchar(c) (alphachar(c) || digitchar(c) || underschar(c))
393 1.103 christos #define lastchar(c) (alphachar(c) || digitchar(c))
394 1.103 christos #define middlechar(c) (lastchar(c) || hyphenchar(c))
395 1.103 christos
396 1.103 christos static int
397 1.103 christos gai_srvok(const char *dn)
398 1.103 christos {
399 1.103 christos int nch, pch, ch;
400 1.103 christos
401 1.103 christos for (pch = PERIOD, nch = ch = *dn++; ch != '\0'; pch = ch, ch = nch) {
402 1.103 christos if (periodchar(ch))
403 1.103 christos continue;
404 1.103 christos if (periodchar(pch)) {
405 1.103 christos if (!firstchar(ch))
406 1.103 christos return 0;
407 1.103 christos } else if (periodchar(nch) || nch == '\0') {
408 1.103 christos if (!lastchar(ch))
409 1.103 christos return 0;
410 1.103 christos } else if (!middlechar(ch))
411 1.103 christos return 0;
412 1.103 christos }
413 1.103 christos return 1;
414 1.103 christos }
415 1.103 christos
416 1.103 christos static in_port_t *
417 1.103 christos getport(struct addrinfo *ai) {
418 1.103 christos static in_port_t p;
419 1.103 christos
420 1.103 christos switch (ai->ai_family) {
421 1.103 christos case AF_INET:
422 1.103 christos return &((struct sockaddr_in *)(void *)ai->ai_addr)->sin_port;
423 1.103 christos #ifdef INET6
424 1.103 christos case AF_INET6:
425 1.103 christos return &((struct sockaddr_in6 *)(void *)ai->ai_addr)->sin6_port;
426 1.103 christos #endif
427 1.103 christos default:
428 1.103 christos p = 0;
429 1.103 christos /* XXX: abort()? */
430 1.103 christos return &p;
431 1.103 christos }
432 1.103 christos }
433 1.103 christos
434 1.1 itojun static int
435 1.70 christos str2number(const char *p)
436 1.1 itojun {
437 1.45 itojun char *ep;
438 1.67 itojun unsigned long v;
439 1.45 itojun
440 1.51 lukem _DIAGASSERT(p != NULL);
441 1.51 lukem
442 1.46 itojun if (*p == '\0')
443 1.67 itojun return -1;
444 1.45 itojun ep = NULL;
445 1.59 itojun errno = 0;
446 1.67 itojun v = strtoul(p, &ep, 10);
447 1.97 christos if (errno == 0 && ep && *ep == '\0' && v <= INT_MAX)
448 1.97 christos return (int)v;
449 1.45 itojun else
450 1.67 itojun return -1;
451 1.1 itojun }
452 1.1 itojun
453 1.1 itojun int
454 1.70 christos getaddrinfo(const char *hostname, const char *servname,
455 1.70 christos const struct addrinfo *hints, struct addrinfo **res)
456 1.1 itojun {
457 1.1 itojun struct addrinfo sentinel;
458 1.1 itojun struct addrinfo *cur;
459 1.14 itojun int error = 0;
460 1.1 itojun struct addrinfo ai;
461 1.14 itojun struct addrinfo ai0;
462 1.1 itojun struct addrinfo *pai;
463 1.14 itojun const struct explore *ex;
464 1.89 christos struct servent_data svd;
465 1.98 christos uint64_t mask = (uint64_t)~0ULL;
466 1.112 christos int numeric = 0;
467 1.1 itojun
468 1.51 lukem /* hostname is allowed to be NULL */
469 1.51 lukem /* servname is allowed to be NULL */
470 1.51 lukem /* hints is allowed to be NULL */
471 1.51 lukem _DIAGASSERT(res != NULL);
472 1.51 lukem
473 1.89 christos (void)memset(&svd, 0, sizeof(svd));
474 1.32 itojun memset(&sentinel, 0, sizeof(sentinel));
475 1.1 itojun cur = &sentinel;
476 1.86 christos memset(&ai, 0, sizeof(ai));
477 1.1 itojun pai = &ai;
478 1.1 itojun pai->ai_flags = 0;
479 1.1 itojun pai->ai_family = PF_UNSPEC;
480 1.1 itojun pai->ai_socktype = ANY;
481 1.1 itojun pai->ai_protocol = ANY;
482 1.1 itojun pai->ai_addrlen = 0;
483 1.1 itojun pai->ai_canonname = NULL;
484 1.1 itojun pai->ai_addr = NULL;
485 1.1 itojun pai->ai_next = NULL;
486 1.102 christos
487 1.1 itojun if (hostname == NULL && servname == NULL)
488 1.1 itojun return EAI_NONAME;
489 1.1 itojun if (hints) {
490 1.1 itojun /* error check for hints */
491 1.1 itojun if (hints->ai_addrlen || hints->ai_canonname ||
492 1.1 itojun hints->ai_addr || hints->ai_next)
493 1.1 itojun ERR(EAI_BADHINTS); /* xxx */
494 1.1 itojun if (hints->ai_flags & ~AI_MASK)
495 1.1 itojun ERR(EAI_BADFLAGS);
496 1.1 itojun switch (hints->ai_family) {
497 1.1 itojun case PF_UNSPEC:
498 1.1 itojun case PF_INET:
499 1.1 itojun #ifdef INET6
500 1.1 itojun case PF_INET6:
501 1.1 itojun #endif
502 1.1 itojun break;
503 1.1 itojun default:
504 1.1 itojun ERR(EAI_FAMILY);
505 1.1 itojun }
506 1.1 itojun memcpy(pai, hints, sizeof(*pai));
507 1.14 itojun
508 1.14 itojun /*
509 1.14 itojun * if both socktype/protocol are specified, check if they
510 1.14 itojun * are meaningful combination.
511 1.14 itojun */
512 1.14 itojun if (pai->ai_socktype != ANY && pai->ai_protocol != ANY) {
513 1.14 itojun for (ex = explore; ex->e_af >= 0; ex++) {
514 1.14 itojun if (pai->ai_family != ex->e_af)
515 1.14 itojun continue;
516 1.14 itojun if (ex->e_socktype == ANY)
517 1.14 itojun continue;
518 1.14 itojun if (ex->e_protocol == ANY)
519 1.14 itojun continue;
520 1.14 itojun if (pai->ai_socktype == ex->e_socktype
521 1.14 itojun && pai->ai_protocol != ex->e_protocol) {
522 1.14 itojun ERR(EAI_BADHINTS);
523 1.14 itojun }
524 1.1 itojun }
525 1.1 itojun }
526 1.1 itojun }
527 1.1 itojun
528 1.98 christos if ((pai->ai_flags & AI_ADDRCONFIG) != 0 && addrconfig(&mask) == -1)
529 1.98 christos ERR(EAI_FAIL);
530 1.98 christos
531 1.1 itojun /*
532 1.14 itojun * check for special cases. (1) numeric servname is disallowed if
533 1.14 itojun * socktype/protocol are left unspecified. (2) servname is disallowed
534 1.14 itojun * for raw and other inet{,6} sockets.
535 1.1 itojun */
536 1.14 itojun if (MATCH_FAMILY(pai->ai_family, PF_INET, 1)
537 1.27 itojun #ifdef PF_INET6
538 1.27 itojun || MATCH_FAMILY(pai->ai_family, PF_INET6, 1)
539 1.27 itojun #endif
540 1.27 itojun ) {
541 1.23 itojun ai0 = *pai; /* backup *pai */
542 1.21 mycroft
543 1.27 itojun if (pai->ai_family == PF_UNSPEC) {
544 1.27 itojun #ifdef PF_INET6
545 1.14 itojun pai->ai_family = PF_INET6;
546 1.27 itojun #else
547 1.27 itojun pai->ai_family = PF_INET;
548 1.27 itojun #endif
549 1.27 itojun }
550 1.89 christos error = get_portmatch(pai, servname, &svd);
551 1.14 itojun if (error)
552 1.101 martin goto bad;
553 1.21 mycroft
554 1.21 mycroft *pai = ai0;
555 1.14 itojun }
556 1.14 itojun
557 1.14 itojun ai0 = *pai;
558 1.14 itojun
559 1.14 itojun /* NULL hostname, or numeric hostname */
560 1.14 itojun for (ex = explore; ex->e_af >= 0; ex++) {
561 1.14 itojun *pai = ai0;
562 1.14 itojun
563 1.98 christos /* ADDRCONFIG check */
564 1.98 christos if ((((uint64_t)1 << ex->e_af) & mask) == 0)
565 1.98 christos continue;
566 1.98 christos
567 1.32 itojun /* PF_UNSPEC entries are prepared for DNS queries only */
568 1.32 itojun if (ex->e_af == PF_UNSPEC)
569 1.32 itojun continue;
570 1.32 itojun
571 1.14 itojun if (!MATCH_FAMILY(pai->ai_family, ex->e_af, WILD_AF(ex)))
572 1.14 itojun continue;
573 1.14 itojun if (!MATCH(pai->ai_socktype, ex->e_socktype, WILD_SOCKTYPE(ex)))
574 1.14 itojun continue;
575 1.14 itojun if (!MATCH(pai->ai_protocol, ex->e_protocol, WILD_PROTOCOL(ex)))
576 1.14 itojun continue;
577 1.14 itojun if (pai->ai_family == PF_UNSPEC)
578 1.14 itojun pai->ai_family = ex->e_af;
579 1.14 itojun if (pai->ai_socktype == ANY && ex->e_socktype != ANY)
580 1.14 itojun pai->ai_socktype = ex->e_socktype;
581 1.14 itojun if (pai->ai_protocol == ANY && ex->e_protocol != ANY)
582 1.14 itojun pai->ai_protocol = ex->e_protocol;
583 1.14 itojun
584 1.14 itojun if (hostname == NULL)
585 1.89 christos error = explore_null(pai, servname, &cur->ai_next,
586 1.89 christos &svd);
587 1.14 itojun else
588 1.65 itojun error = explore_numeric_scope(pai, hostname, servname,
589 1.89 christos &cur->ai_next, &svd);
590 1.14 itojun
591 1.14 itojun if (error)
592 1.14 itojun goto free;
593 1.14 itojun
594 1.79 christos while (cur->ai_next)
595 1.14 itojun cur = cur->ai_next;
596 1.14 itojun }
597 1.14 itojun
598 1.14 itojun /*
599 1.14 itojun * XXX
600 1.76 ginsbach * If numeric representation of AF1 can be interpreted as FQDN
601 1.14 itojun * representation of AF2, we need to think again about the code below.
602 1.14 itojun */
603 1.112 christos if (sentinel.ai_next) {
604 1.112 christos numeric = 1;
605 1.14 itojun goto good;
606 1.112 christos }
607 1.14 itojun
608 1.14 itojun if (hostname == NULL)
609 1.43 itojun ERR(EAI_NODATA);
610 1.64 itojun if (pai->ai_flags & AI_NUMERICHOST)
611 1.64 itojun ERR(EAI_NONAME);
612 1.14 itojun
613 1.14 itojun /*
614 1.14 itojun * hostname as alphabetical name.
615 1.14 itojun * we would like to prefer AF_INET6 than AF_INET, so we'll make a
616 1.14 itojun * outer loop by AFs.
617 1.14 itojun */
618 1.32 itojun for (ex = explore; ex->e_af >= 0; ex++) {
619 1.14 itojun *pai = ai0;
620 1.14 itojun
621 1.100 kardel
622 1.98 christos /* ADDRCONFIG check */
623 1.100 kardel /* PF_UNSPEC entries are prepared for DNS queries only */
624 1.100 kardel if (ex->e_af != PF_UNSPEC &&
625 1.100 kardel (((uint64_t)1 << ex->e_af) & mask) == 0)
626 1.98 christos continue;
627 1.98 christos
628 1.32 itojun /* require exact match for family field */
629 1.32 itojun if (pai->ai_family != ex->e_af)
630 1.14 itojun continue;
631 1.14 itojun
632 1.32 itojun if (!MATCH(pai->ai_socktype, ex->e_socktype,
633 1.32 itojun WILD_SOCKTYPE(ex))) {
634 1.32 itojun continue;
635 1.32 itojun }
636 1.32 itojun if (!MATCH(pai->ai_protocol, ex->e_protocol,
637 1.32 itojun WILD_PROTOCOL(ex))) {
638 1.32 itojun continue;
639 1.32 itojun }
640 1.14 itojun
641 1.32 itojun if (pai->ai_socktype == ANY && ex->e_socktype != ANY)
642 1.32 itojun pai->ai_socktype = ex->e_socktype;
643 1.32 itojun if (pai->ai_protocol == ANY && ex->e_protocol != ANY)
644 1.32 itojun pai->ai_protocol = ex->e_protocol;
645 1.1 itojun
646 1.89 christos error = explore_fqdn(pai, hostname, servname, &cur->ai_next,
647 1.89 christos &svd);
648 1.14 itojun
649 1.32 itojun while (cur && cur->ai_next)
650 1.32 itojun cur = cur->ai_next;
651 1.1 itojun }
652 1.14 itojun
653 1.14 itojun /* XXX */
654 1.14 itojun if (sentinel.ai_next)
655 1.14 itojun error = 0;
656 1.14 itojun
657 1.14 itojun if (error)
658 1.14 itojun goto free;
659 1.89 christos
660 1.89 christos if (sentinel.ai_next) {
661 1.14 itojun good:
662 1.112 christos /*
663 1.112 christos * If the returned entry is for an active connection,
664 1.112 christos * and the given name is not numeric, reorder the
665 1.112 christos * list, so that the application would try the list
666 1.112 christos * in the most efficient order. Since the head entry
667 1.112 christos * of the original list may contain ai_canonname and
668 1.112 christos * that entry may be moved elsewhere in the new list,
669 1.112 christos * we keep the pointer and will restore it in the new
670 1.112 christos * head entry. (Note that RFC3493 requires the head
671 1.112 christos * entry store it when requested by the caller).
672 1.112 christos */
673 1.112 christos if (hints == NULL || !(hints->ai_flags & AI_PASSIVE)) {
674 1.112 christos if (!numeric) {
675 1.112 christos char *canonname;
676 1.112 christos
677 1.112 christos canonname = sentinel.ai_next->ai_canonname;
678 1.112 christos sentinel.ai_next->ai_canonname = NULL;
679 1.112 christos (void)reorder(&sentinel, &svd);
680 1.112 christos if (sentinel.ai_next->ai_canonname == NULL) {
681 1.112 christos sentinel.ai_next->ai_canonname
682 1.112 christos = canonname;
683 1.112 christos } else if (canonname != NULL)
684 1.112 christos free(canonname);
685 1.112 christos }
686 1.112 christos }
687 1.89 christos endservent_r(&svd);
688 1.89 christos *res = sentinel.ai_next;
689 1.89 christos return SUCCESS;
690 1.89 christos } else
691 1.89 christos error = EAI_FAIL;
692 1.14 itojun free:
693 1.14 itojun bad:
694 1.89 christos endservent_r(&svd);
695 1.14 itojun if (sentinel.ai_next)
696 1.14 itojun freeaddrinfo(sentinel.ai_next);
697 1.14 itojun *res = NULL;
698 1.14 itojun return error;
699 1.14 itojun }
700 1.14 itojun
701 1.112 christos static int
702 1.112 christos reorder(struct addrinfo *sentinel, struct servent_data *svd)
703 1.112 christos {
704 1.112 christos struct addrinfo *ai, **aip;
705 1.112 christos struct ai_order *aio;
706 1.112 christos int i, n;
707 1.112 christos struct policyhead policyhead;
708 1.112 christos
709 1.112 christos /* count the number of addrinfo elements for sorting. */
710 1.112 christos for (n = 0, ai = sentinel->ai_next; ai != NULL; ai = ai->ai_next, n++)
711 1.112 christos ;
712 1.112 christos
713 1.112 christos /*
714 1.112 christos * If the number is small enough, we can skip the reordering process.
715 1.112 christos */
716 1.112 christos if (n <= 1)
717 1.115 christos return n;
718 1.112 christos
719 1.112 christos /* allocate a temporary array for sort and initialization of it. */
720 1.121 nia if ((aio = calloc(n, sizeof(*aio))) == NULL)
721 1.115 christos return n; /* give up reordering */
722 1.112 christos
723 1.112 christos /* retrieve address selection policy from the kernel */
724 1.112 christos TAILQ_INIT(&policyhead);
725 1.112 christos if (!get_addrselectpolicy(&policyhead)) {
726 1.112 christos /* no policy is installed into kernel, we don't sort. */
727 1.112 christos free(aio);
728 1.115 christos return n;
729 1.112 christos }
730 1.112 christos
731 1.112 christos for (i = 0, ai = sentinel->ai_next; i < n; ai = ai->ai_next, i++) {
732 1.112 christos aio[i].aio_ai = ai;
733 1.112 christos aio[i].aio_dstscope = gai_addr2scopetype(ai->ai_addr);
734 1.112 christos aio[i].aio_dstpolicy = match_addrselectpolicy(ai->ai_addr,
735 1.112 christos &policyhead);
736 1.112 christos set_source(&aio[i], &policyhead, svd);
737 1.112 christos }
738 1.112 christos
739 1.112 christos /* perform sorting. */
740 1.112 christos qsort(aio, n, sizeof(*aio), comp_dst);
741 1.112 christos
742 1.112 christos /* reorder the addrinfo chain. */
743 1.112 christos for (i = 0, aip = &sentinel->ai_next; i < n; i++) {
744 1.112 christos *aip = aio[i].aio_ai;
745 1.112 christos aip = &aio[i].aio_ai->ai_next;
746 1.112 christos }
747 1.112 christos *aip = NULL;
748 1.112 christos
749 1.112 christos /* cleanup and return */
750 1.112 christos free(aio);
751 1.112 christos free_addrselectpolicy(&policyhead);
752 1.115 christos return n;
753 1.112 christos }
754 1.112 christos
755 1.112 christos static int
756 1.112 christos get_addrselectpolicy(struct policyhead *head)
757 1.112 christos {
758 1.112 christos #ifdef INET6
759 1.115 christos static const int mib[] = {
760 1.115 christos CTL_NET, PF_INET6, IPPROTO_IPV6, IPV6CTL_ADDRCTLPOLICY };
761 1.115 christos static const u_int miblen = (u_int)__arraycount(mib);
762 1.112 christos size_t l;
763 1.112 christos char *buf;
764 1.112 christos struct in6_addrpolicy *pol, *ep;
765 1.112 christos
766 1.115 christos if (sysctl(mib, miblen, NULL, &l, NULL, 0) < 0)
767 1.115 christos return 0;
768 1.112 christos if (l == 0)
769 1.115 christos return 0;
770 1.112 christos if ((buf = malloc(l)) == NULL)
771 1.115 christos return 0;
772 1.115 christos if (sysctl(mib, miblen, buf, &l, NULL, 0) < 0) {
773 1.112 christos free(buf);
774 1.115 christos return 0;
775 1.112 christos }
776 1.112 christos
777 1.115 christos ep = (void *)(buf + l);
778 1.115 christos for (pol = (void *)buf; pol + 1 <= ep; pol++) {
779 1.112 christos struct policyqueue *new;
780 1.112 christos
781 1.112 christos if ((new = malloc(sizeof(*new))) == NULL) {
782 1.112 christos free_addrselectpolicy(head); /* make the list empty */
783 1.112 christos break;
784 1.112 christos }
785 1.112 christos new->pc_policy = *pol;
786 1.112 christos TAILQ_INSERT_TAIL(head, new, pc_entry);
787 1.112 christos }
788 1.112 christos
789 1.112 christos free(buf);
790 1.115 christos return 1;
791 1.112 christos #else
792 1.115 christos return 0;
793 1.112 christos #endif
794 1.112 christos }
795 1.112 christos
796 1.112 christos static void
797 1.112 christos free_addrselectpolicy(struct policyhead *head)
798 1.112 christos {
799 1.112 christos struct policyqueue *ent, *nent;
800 1.112 christos
801 1.112 christos for (ent = TAILQ_FIRST(head); ent; ent = nent) {
802 1.112 christos nent = TAILQ_NEXT(ent, pc_entry);
803 1.112 christos TAILQ_REMOVE(head, ent, pc_entry);
804 1.112 christos free(ent);
805 1.112 christos }
806 1.112 christos }
807 1.112 christos
808 1.112 christos static struct policyqueue *
809 1.112 christos match_addrselectpolicy(struct sockaddr *addr, struct policyhead *head)
810 1.112 christos {
811 1.112 christos #ifdef INET6
812 1.112 christos struct policyqueue *ent, *bestent = NULL;
813 1.112 christos struct in6_addrpolicy *pol;
814 1.114 riastrad int curmatchlen, bestmatchlen = -1;
815 1.115 christos u_char *mp, *ep, *k, *p;
816 1.115 christos u_int m;
817 1.112 christos struct sockaddr_in6 key;
818 1.112 christos
819 1.112 christos switch(addr->sa_family) {
820 1.112 christos case AF_INET6:
821 1.115 christos memcpy(&key, addr, sizeof(key));
822 1.112 christos break;
823 1.112 christos case AF_INET:
824 1.112 christos /* convert the address into IPv4-mapped IPv6 address. */
825 1.112 christos memset(&key, 0, sizeof(key));
826 1.112 christos key.sin6_family = AF_INET6;
827 1.112 christos key.sin6_len = sizeof(key);
828 1.112 christos key.sin6_addr.s6_addr[10] = 0xff;
829 1.112 christos key.sin6_addr.s6_addr[11] = 0xff;
830 1.115 christos memcpy(&key.sin6_addr.s6_addr[12], sa4addr(addr), 4);
831 1.112 christos break;
832 1.112 christos default:
833 1.115 christos return NULL;
834 1.112 christos }
835 1.112 christos
836 1.112 christos for (ent = TAILQ_FIRST(head); ent; ent = TAILQ_NEXT(ent, pc_entry)) {
837 1.112 christos pol = &ent->pc_policy;
838 1.114 riastrad curmatchlen = 0;
839 1.112 christos
840 1.115 christos mp = (void *)&pol->addrmask.sin6_addr;
841 1.112 christos ep = mp + 16; /* XXX: scope field? */
842 1.115 christos k = (void *)&key.sin6_addr;
843 1.115 christos p = (void *)&pol->addr.sin6_addr;
844 1.112 christos for (; mp < ep && *mp; mp++, k++, p++) {
845 1.112 christos m = *mp;
846 1.112 christos if ((*k & m) != *p)
847 1.112 christos goto next; /* not match */
848 1.112 christos if (m == 0xff) /* short cut for a typical case */
849 1.114 riastrad curmatchlen += 8;
850 1.112 christos else {
851 1.112 christos while (m >= 0x80) {
852 1.114 riastrad curmatchlen++;
853 1.112 christos m <<= 1;
854 1.112 christos }
855 1.112 christos }
856 1.112 christos }
857 1.112 christos
858 1.112 christos /* matched. check if this is better than the current best. */
859 1.114 riastrad if (curmatchlen > bestmatchlen) {
860 1.112 christos bestent = ent;
861 1.114 riastrad bestmatchlen = curmatchlen;
862 1.112 christos }
863 1.112 christos
864 1.112 christos next:
865 1.112 christos continue;
866 1.112 christos }
867 1.112 christos
868 1.115 christos return bestent;
869 1.112 christos #else
870 1.115 christos return NULL;
871 1.112 christos #endif
872 1.112 christos
873 1.112 christos }
874 1.112 christos
875 1.112 christos static void
876 1.112 christos set_source(struct ai_order *aio, struct policyhead *ph,
877 1.112 christos struct servent_data *svd)
878 1.112 christos {
879 1.112 christos struct addrinfo ai = *aio->aio_ai;
880 1.112 christos struct sockaddr_storage ss;
881 1.112 christos socklen_t srclen;
882 1.112 christos int s;
883 1.112 christos
884 1.112 christos /* set unspec ("no source is available"), just in case */
885 1.112 christos aio->aio_srcsa.sa_family = AF_UNSPEC;
886 1.112 christos aio->aio_srcscope = -1;
887 1.112 christos
888 1.112 christos switch(ai.ai_family) {
889 1.112 christos case AF_INET:
890 1.112 christos #ifdef INET6
891 1.112 christos case AF_INET6:
892 1.112 christos #endif
893 1.112 christos break;
894 1.112 christos default: /* ignore unsupported AFs explicitly */
895 1.112 christos return;
896 1.112 christos }
897 1.112 christos
898 1.112 christos /* XXX: make a dummy addrinfo to call connect() */
899 1.112 christos ai.ai_socktype = SOCK_DGRAM;
900 1.112 christos ai.ai_protocol = IPPROTO_UDP; /* is UDP too specific? */
901 1.112 christos ai.ai_next = NULL;
902 1.112 christos memset(&ss, 0, sizeof(ss));
903 1.112 christos memcpy(&ss, ai.ai_addr, ai.ai_addrlen);
904 1.115 christos ai.ai_addr = (void *)&ss;
905 1.112 christos get_port(&ai, "1", 0, svd);
906 1.112 christos
907 1.112 christos /* open a socket to get the source address for the given dst */
908 1.112 christos if ((s = socket(ai.ai_family, ai.ai_socktype | SOCK_CLOEXEC,
909 1.112 christos ai.ai_protocol)) < 0)
910 1.112 christos return; /* give up */
911 1.112 christos if (connect(s, ai.ai_addr, ai.ai_addrlen) < 0)
912 1.112 christos goto cleanup;
913 1.112 christos srclen = ai.ai_addrlen;
914 1.112 christos if (getsockname(s, &aio->aio_srcsa, &srclen) < 0) {
915 1.112 christos aio->aio_srcsa.sa_family = AF_UNSPEC;
916 1.112 christos goto cleanup;
917 1.112 christos }
918 1.112 christos aio->aio_srcscope = gai_addr2scopetype(&aio->aio_srcsa);
919 1.112 christos aio->aio_srcpolicy = match_addrselectpolicy(&aio->aio_srcsa, ph);
920 1.112 christos aio->aio_matchlen = matchlen(&aio->aio_srcsa, aio->aio_ai->ai_addr);
921 1.112 christos #ifdef INET6
922 1.112 christos if (ai.ai_family == AF_INET6) {
923 1.112 christos struct in6_ifreq ifr6;
924 1.112 christos u_int32_t flags6;
925 1.112 christos
926 1.112 christos memset(&ifr6, 0, sizeof(ifr6));
927 1.112 christos memcpy(&ifr6.ifr_addr, ai.ai_addr, ai.ai_addrlen);
928 1.112 christos if (ioctl(s, SIOCGIFAFLAG_IN6, &ifr6) == 0) {
929 1.112 christos flags6 = ifr6.ifr_ifru.ifru_flags6;
930 1.112 christos if ((flags6 & IN6_IFF_DEPRECATED))
931 1.112 christos aio->aio_srcflag |= AIO_SRCFLAG_DEPRECATED;
932 1.112 christos }
933 1.112 christos }
934 1.112 christos #endif
935 1.112 christos
936 1.112 christos cleanup:
937 1.112 christos close(s);
938 1.112 christos return;
939 1.112 christos }
940 1.112 christos
941 1.112 christos static int
942 1.112 christos matchlen(struct sockaddr *src, struct sockaddr *dst)
943 1.112 christos {
944 1.112 christos int match = 0;
945 1.112 christos u_char *s, *d;
946 1.115 christos u_char *lim;
947 1.115 christos u_int r, addrlen;
948 1.112 christos
949 1.112 christos switch (src->sa_family) {
950 1.112 christos #ifdef INET6
951 1.112 christos case AF_INET6:
952 1.115 christos s = sa6addr(src);
953 1.115 christos d = sa6addr(dst);
954 1.112 christos addrlen = sizeof(struct in6_addr);
955 1.112 christos lim = s + addrlen;
956 1.112 christos break;
957 1.112 christos #endif
958 1.112 christos case AF_INET:
959 1.115 christos s = sa4addr(src);
960 1.115 christos d = sa4addr(dst);
961 1.112 christos addrlen = sizeof(struct in_addr);
962 1.112 christos lim = s + addrlen;
963 1.112 christos break;
964 1.112 christos default:
965 1.115 christos return 0;
966 1.112 christos }
967 1.112 christos
968 1.112 christos while (s < lim)
969 1.112 christos if ((r = (*d++ ^ *s++)) != 0) {
970 1.112 christos while (r < addrlen * 8) {
971 1.112 christos match++;
972 1.112 christos r <<= 1;
973 1.112 christos }
974 1.112 christos break;
975 1.112 christos } else
976 1.112 christos match += 8;
977 1.115 christos return match;
978 1.112 christos }
979 1.112 christos
980 1.112 christos static int
981 1.112 christos comp_dst(const void *arg1, const void *arg2)
982 1.112 christos {
983 1.112 christos const struct ai_order *dst1 = arg1, *dst2 = arg2;
984 1.112 christos
985 1.112 christos /*
986 1.112 christos * Rule 1: Avoid unusable destinations.
987 1.112 christos * XXX: we currently do not consider if an appropriate route exists.
988 1.112 christos */
989 1.112 christos if (dst1->aio_srcsa.sa_family != AF_UNSPEC &&
990 1.112 christos dst2->aio_srcsa.sa_family == AF_UNSPEC) {
991 1.115 christos return -1;
992 1.112 christos }
993 1.112 christos if (dst1->aio_srcsa.sa_family == AF_UNSPEC &&
994 1.112 christos dst2->aio_srcsa.sa_family != AF_UNSPEC) {
995 1.115 christos return 1;
996 1.112 christos }
997 1.112 christos
998 1.112 christos /* Rule 2: Prefer matching scope. */
999 1.112 christos if (dst1->aio_dstscope == dst1->aio_srcscope &&
1000 1.112 christos dst2->aio_dstscope != dst2->aio_srcscope) {
1001 1.115 christos return -1;
1002 1.112 christos }
1003 1.112 christos if (dst1->aio_dstscope != dst1->aio_srcscope &&
1004 1.112 christos dst2->aio_dstscope == dst2->aio_srcscope) {
1005 1.115 christos return 1;
1006 1.112 christos }
1007 1.112 christos
1008 1.112 christos /* Rule 3: Avoid deprecated addresses. */
1009 1.112 christos if (dst1->aio_srcsa.sa_family != AF_UNSPEC &&
1010 1.112 christos dst2->aio_srcsa.sa_family != AF_UNSPEC) {
1011 1.112 christos if (!(dst1->aio_srcflag & AIO_SRCFLAG_DEPRECATED) &&
1012 1.112 christos (dst2->aio_srcflag & AIO_SRCFLAG_DEPRECATED)) {
1013 1.115 christos return -1;
1014 1.112 christos }
1015 1.112 christos if ((dst1->aio_srcflag & AIO_SRCFLAG_DEPRECATED) &&
1016 1.112 christos !(dst2->aio_srcflag & AIO_SRCFLAG_DEPRECATED)) {
1017 1.115 christos return 1;
1018 1.112 christos }
1019 1.112 christos }
1020 1.112 christos
1021 1.112 christos /* Rule 4: Prefer home addresses. */
1022 1.112 christos /* XXX: not implemented yet */
1023 1.112 christos
1024 1.112 christos /* Rule 5: Prefer matching label. */
1025 1.112 christos #ifdef INET6
1026 1.112 christos if (dst1->aio_srcpolicy && dst1->aio_dstpolicy &&
1027 1.112 christos dst1->aio_srcpolicy->pc_policy.label ==
1028 1.112 christos dst1->aio_dstpolicy->pc_policy.label &&
1029 1.112 christos (dst2->aio_srcpolicy == NULL || dst2->aio_dstpolicy == NULL ||
1030 1.112 christos dst2->aio_srcpolicy->pc_policy.label !=
1031 1.112 christos dst2->aio_dstpolicy->pc_policy.label)) {
1032 1.115 christos return -1;
1033 1.112 christos }
1034 1.112 christos if (dst2->aio_srcpolicy && dst2->aio_dstpolicy &&
1035 1.112 christos dst2->aio_srcpolicy->pc_policy.label ==
1036 1.112 christos dst2->aio_dstpolicy->pc_policy.label &&
1037 1.112 christos (dst1->aio_srcpolicy == NULL || dst1->aio_dstpolicy == NULL ||
1038 1.112 christos dst1->aio_srcpolicy->pc_policy.label !=
1039 1.112 christos dst1->aio_dstpolicy->pc_policy.label)) {
1040 1.115 christos return 1;
1041 1.112 christos }
1042 1.112 christos #endif
1043 1.112 christos
1044 1.112 christos /* Rule 6: Prefer higher precedence. */
1045 1.112 christos #ifdef INET6
1046 1.112 christos if (dst1->aio_dstpolicy &&
1047 1.112 christos (dst2->aio_dstpolicy == NULL ||
1048 1.112 christos dst1->aio_dstpolicy->pc_policy.preced >
1049 1.112 christos dst2->aio_dstpolicy->pc_policy.preced)) {
1050 1.115 christos return -1;
1051 1.112 christos }
1052 1.112 christos if (dst2->aio_dstpolicy &&
1053 1.112 christos (dst1->aio_dstpolicy == NULL ||
1054 1.112 christos dst2->aio_dstpolicy->pc_policy.preced >
1055 1.112 christos dst1->aio_dstpolicy->pc_policy.preced)) {
1056 1.115 christos return 1;
1057 1.112 christos }
1058 1.112 christos #endif
1059 1.112 christos
1060 1.112 christos /* Rule 7: Prefer native transport. */
1061 1.112 christos /* XXX: not implemented yet */
1062 1.112 christos
1063 1.112 christos /* Rule 8: Prefer smaller scope. */
1064 1.112 christos if (dst1->aio_dstscope >= 0 &&
1065 1.112 christos dst1->aio_dstscope < dst2->aio_dstscope) {
1066 1.115 christos return -1;
1067 1.112 christos }
1068 1.112 christos if (dst2->aio_dstscope >= 0 &&
1069 1.112 christos dst2->aio_dstscope < dst1->aio_dstscope) {
1070 1.115 christos return 1;
1071 1.112 christos }
1072 1.112 christos
1073 1.112 christos /*
1074 1.112 christos * Rule 9: Use longest matching prefix.
1075 1.112 christos * We compare the match length in a same AF only.
1076 1.112 christos */
1077 1.112 christos if (dst1->aio_ai->ai_addr->sa_family ==
1078 1.112 christos dst2->aio_ai->ai_addr->sa_family &&
1079 1.112 christos dst1->aio_ai->ai_addr->sa_family != AF_INET) {
1080 1.112 christos if (dst1->aio_matchlen > dst2->aio_matchlen) {
1081 1.115 christos return -1;
1082 1.112 christos }
1083 1.112 christos if (dst1->aio_matchlen < dst2->aio_matchlen) {
1084 1.115 christos return 1;
1085 1.112 christos }
1086 1.112 christos }
1087 1.112 christos
1088 1.112 christos /* Rule 10: Otherwise, leave the order unchanged. */
1089 1.115 christos return -1;
1090 1.112 christos }
1091 1.112 christos
1092 1.112 christos /*
1093 1.112 christos * Copy from scope.c.
1094 1.112 christos * XXX: we should standardize the functions and link them as standard
1095 1.112 christos * library.
1096 1.112 christos */
1097 1.112 christos static int
1098 1.112 christos gai_addr2scopetype(struct sockaddr *sa)
1099 1.112 christos {
1100 1.112 christos #ifdef INET6
1101 1.112 christos struct sockaddr_in6 *sa6;
1102 1.112 christos #endif
1103 1.112 christos struct sockaddr_in *sa4;
1104 1.115 christos u_char *p;
1105 1.112 christos
1106 1.112 christos switch(sa->sa_family) {
1107 1.112 christos #ifdef INET6
1108 1.112 christos case AF_INET6:
1109 1.115 christos sa6 = (void *)sa;
1110 1.112 christos if (IN6_IS_ADDR_MULTICAST(&sa6->sin6_addr)) {
1111 1.112 christos /* just use the scope field of the multicast address */
1112 1.115 christos return sa6->sin6_addr.s6_addr[2] & 0x0f;
1113 1.112 christos }
1114 1.112 christos /*
1115 1.112 christos * Unicast addresses: map scope type to corresponding scope
1116 1.112 christos * value defined for multcast addresses.
1117 1.112 christos * XXX: hardcoded scope type values are bad...
1118 1.112 christos */
1119 1.112 christos if (IN6_IS_ADDR_LOOPBACK(&sa6->sin6_addr))
1120 1.115 christos return 1; /* node local scope */
1121 1.112 christos if (IN6_IS_ADDR_LINKLOCAL(&sa6->sin6_addr))
1122 1.115 christos return 2; /* link-local scope */
1123 1.112 christos if (IN6_IS_ADDR_SITELOCAL(&sa6->sin6_addr))
1124 1.115 christos return 5; /* site-local scope */
1125 1.115 christos return 14; /* global scope */
1126 1.112 christos #endif
1127 1.112 christos case AF_INET:
1128 1.112 christos /*
1129 1.112 christos * IPv4 pseudo scoping according to RFC 3484.
1130 1.112 christos */
1131 1.115 christos sa4 = (void *)sa;
1132 1.115 christos p = (u_char *)(void *)&sa4->sin_addr;
1133 1.112 christos /* IPv4 autoconfiguration addresses have link-local scope. */
1134 1.115 christos if (p[0] == 169 && p[1] == 254)
1135 1.115 christos return 2;
1136 1.112 christos /* Private addresses have site-local scope. */
1137 1.115 christos if (p[0] == 10 ||
1138 1.115 christos (p[0] == 172 && (p[1] & 0xf0) == 16) ||
1139 1.115 christos (p[0] == 192 && p[1] == 168))
1140 1.115 christos return 14; /* XXX: It should be 5 unless NAT */
1141 1.112 christos /* Loopback addresses have link-local scope. */
1142 1.115 christos if (p[0] == 127)
1143 1.115 christos return 2;
1144 1.115 christos return 14;
1145 1.112 christos default:
1146 1.112 christos errno = EAFNOSUPPORT; /* is this a good error? */
1147 1.115 christos return -1;
1148 1.112 christos }
1149 1.112 christos }
1150 1.112 christos
1151 1.14 itojun /*
1152 1.14 itojun * FQDN hostname, DNS lookup
1153 1.14 itojun */
1154 1.14 itojun static int
1155 1.70 christos explore_fqdn(const struct addrinfo *pai, const char *hostname,
1156 1.89 christos const char *servname, struct addrinfo **res, struct servent_data *svd)
1157 1.14 itojun {
1158 1.32 itojun struct addrinfo *result;
1159 1.32 itojun struct addrinfo *cur;
1160 1.27 itojun int error = 0;
1161 1.32 itojun static const ns_dtab dtab[] = {
1162 1.32 itojun NS_FILES_CB(_files_getaddrinfo, NULL)
1163 1.32 itojun { NSSRC_DNS, _dns_getaddrinfo, NULL }, /* force -DHESIOD */
1164 1.32 itojun NS_NIS_CB(_yp_getaddrinfo, NULL)
1165 1.87 christos NS_NULL_CB
1166 1.32 itojun };
1167 1.14 itojun
1168 1.51 lukem _DIAGASSERT(pai != NULL);
1169 1.51 lukem /* hostname may be NULL */
1170 1.51 lukem /* servname may be NULL */
1171 1.51 lukem _DIAGASSERT(res != NULL);
1172 1.51 lukem
1173 1.32 itojun result = NULL;
1174 1.14 itojun
1175 1.14 itojun /*
1176 1.14 itojun * if the servname does not match socktype/protocol, ignore it.
1177 1.14 itojun */
1178 1.89 christos if (get_portmatch(pai, servname, svd) != 0)
1179 1.14 itojun return 0;
1180 1.14 itojun
1181 1.32 itojun switch (nsdispatch(&result, dtab, NSDB_HOSTS, "getaddrinfo",
1182 1.103 christos default_dns_files, hostname, pai, servname)) {
1183 1.32 itojun case NS_TRYAGAIN:
1184 1.32 itojun error = EAI_AGAIN;
1185 1.32 itojun goto free;
1186 1.32 itojun case NS_UNAVAIL:
1187 1.14 itojun error = EAI_FAIL;
1188 1.14 itojun goto free;
1189 1.32 itojun case NS_NOTFOUND:
1190 1.118 dholland error = EAI_NODATA;
1191 1.15 itojun goto free;
1192 1.32 itojun case NS_SUCCESS:
1193 1.32 itojun error = 0;
1194 1.32 itojun for (cur = result; cur; cur = cur->ai_next) {
1195 1.103 christos /* Check for already filled port. */
1196 1.103 christos if (*getport(cur))
1197 1.103 christos continue;
1198 1.89 christos GET_PORT(cur, servname, svd);
1199 1.32 itojun /* canonname should be filled already */
1200 1.16 itojun }
1201 1.32 itojun break;
1202 1.16 itojun }
1203 1.15 itojun
1204 1.32 itojun *res = result;
1205 1.14 itojun
1206 1.14 itojun return 0;
1207 1.14 itojun
1208 1.14 itojun free:
1209 1.34 itojun if (result)
1210 1.34 itojun freeaddrinfo(result);
1211 1.14 itojun return error;
1212 1.14 itojun }
1213 1.14 itojun
1214 1.14 itojun /*
1215 1.14 itojun * hostname == NULL.
1216 1.14 itojun * passive socket -> anyaddr (0.0.0.0 or ::)
1217 1.14 itojun * non-passive socket -> localhost (127.0.0.1 or ::1)
1218 1.14 itojun */
1219 1.14 itojun static int
1220 1.70 christos explore_null(const struct addrinfo *pai, const char *servname,
1221 1.89 christos struct addrinfo **res, struct servent_data *svd)
1222 1.14 itojun {
1223 1.14 itojun int s;
1224 1.14 itojun const struct afd *afd;
1225 1.14 itojun struct addrinfo *cur;
1226 1.14 itojun struct addrinfo sentinel;
1227 1.14 itojun int error;
1228 1.14 itojun
1229 1.51 lukem _DIAGASSERT(pai != NULL);
1230 1.51 lukem /* servname may be NULL */
1231 1.51 lukem _DIAGASSERT(res != NULL);
1232 1.51 lukem
1233 1.14 itojun *res = NULL;
1234 1.14 itojun sentinel.ai_next = NULL;
1235 1.14 itojun cur = &sentinel;
1236 1.14 itojun
1237 1.14 itojun /*
1238 1.14 itojun * filter out AFs that are not supported by the kernel
1239 1.14 itojun * XXX errno?
1240 1.14 itojun */
1241 1.14 itojun s = socket(pai->ai_family, SOCK_DGRAM, 0);
1242 1.14 itojun if (s < 0) {
1243 1.14 itojun if (errno != EMFILE)
1244 1.14 itojun return 0;
1245 1.14 itojun } else
1246 1.14 itojun close(s);
1247 1.14 itojun
1248 1.14 itojun /*
1249 1.14 itojun * if the servname does not match socktype/protocol, ignore it.
1250 1.14 itojun */
1251 1.89 christos if (get_portmatch(pai, servname, svd) != 0)
1252 1.14 itojun return 0;
1253 1.14 itojun
1254 1.14 itojun afd = find_afd(pai->ai_family);
1255 1.42 itojun if (afd == NULL)
1256 1.42 itojun return 0;
1257 1.14 itojun
1258 1.14 itojun if (pai->ai_flags & AI_PASSIVE) {
1259 1.14 itojun GET_AI(cur->ai_next, afd, afd->a_addrany);
1260 1.14 itojun /* xxx meaningless?
1261 1.14 itojun * GET_CANONNAME(cur->ai_next, "anyaddr");
1262 1.14 itojun */
1263 1.89 christos GET_PORT(cur->ai_next, servname, svd);
1264 1.14 itojun } else {
1265 1.14 itojun GET_AI(cur->ai_next, afd, afd->a_loopback);
1266 1.14 itojun /* xxx meaningless?
1267 1.14 itojun * GET_CANONNAME(cur->ai_next, "localhost");
1268 1.14 itojun */
1269 1.89 christos GET_PORT(cur->ai_next, servname, svd);
1270 1.14 itojun }
1271 1.14 itojun cur = cur->ai_next;
1272 1.14 itojun
1273 1.14 itojun *res = sentinel.ai_next;
1274 1.14 itojun return 0;
1275 1.14 itojun
1276 1.14 itojun free:
1277 1.14 itojun if (sentinel.ai_next)
1278 1.14 itojun freeaddrinfo(sentinel.ai_next);
1279 1.14 itojun return error;
1280 1.14 itojun }
1281 1.14 itojun
1282 1.14 itojun /*
1283 1.14 itojun * numeric hostname
1284 1.14 itojun */
1285 1.14 itojun static int
1286 1.70 christos explore_numeric(const struct addrinfo *pai, const char *hostname,
1287 1.89 christos const char *servname, struct addrinfo **res, const char *canonname,
1288 1.89 christos struct servent_data *svd)
1289 1.14 itojun {
1290 1.14 itojun const struct afd *afd;
1291 1.14 itojun struct addrinfo *cur;
1292 1.14 itojun struct addrinfo sentinel;
1293 1.14 itojun int error;
1294 1.14 itojun char pton[PTON_MAX];
1295 1.14 itojun
1296 1.51 lukem _DIAGASSERT(pai != NULL);
1297 1.51 lukem /* hostname may be NULL */
1298 1.51 lukem /* servname may be NULL */
1299 1.51 lukem _DIAGASSERT(res != NULL);
1300 1.51 lukem
1301 1.14 itojun *res = NULL;
1302 1.14 itojun sentinel.ai_next = NULL;
1303 1.14 itojun cur = &sentinel;
1304 1.14 itojun
1305 1.14 itojun /*
1306 1.14 itojun * if the servname does not match socktype/protocol, ignore it.
1307 1.14 itojun */
1308 1.89 christos if (get_portmatch(pai, servname, svd) != 0)
1309 1.14 itojun return 0;
1310 1.14 itojun
1311 1.14 itojun afd = find_afd(pai->ai_family);
1312 1.42 itojun if (afd == NULL)
1313 1.42 itojun return 0;
1314 1.14 itojun
1315 1.27 itojun switch (afd->a_af) {
1316 1.27 itojun case AF_INET:
1317 1.113 christos /*
1318 1.113 christos * RFC3493 section 6.1, requires getaddrinfo() to accept
1319 1.113 christos * AF_INET formats that are accepted by inet_addr(); here
1320 1.113 christos * we use the equivalent inet_aton() function so we can
1321 1.113 christos * check for errors. inet_pton() only accepts addresses
1322 1.113 christos * in the dotted quad format and only in base 10, so we
1323 1.113 christos * need to treat AF_INET specially.
1324 1.117 christos *
1325 1.117 christos * We also check for trailing characters and fail if there
1326 1.117 christos * are any. This matches the inet_pton6(), but not the
1327 1.117 christos * inet_pton4() behavior. We choose to make the protocol
1328 1.117 christos * behavior consistent.
1329 1.113 christos */
1330 1.117 christos if (inet_aton(hostname, (void *)pton) == 1 &&
1331 1.117 christos hostname[strspn(hostname, "0123456789.xabcdefXABCDEF")]
1332 1.117 christos == '\0') {
1333 1.27 itojun if (pai->ai_family == afd->a_af ||
1334 1.27 itojun pai->ai_family == PF_UNSPEC /*?*/) {
1335 1.27 itojun GET_AI(cur->ai_next, afd, pton);
1336 1.89 christos GET_PORT(cur->ai_next, servname, svd);
1337 1.65 itojun if ((pai->ai_flags & AI_CANONNAME)) {
1338 1.65 itojun /*
1339 1.65 itojun * Set the numeric address itself as
1340 1.65 itojun * the canonical name, based on a
1341 1.65 itojun * clarification in rfc2553bis-03.
1342 1.65 itojun */
1343 1.65 itojun GET_CANONNAME(cur->ai_next, canonname);
1344 1.65 itojun }
1345 1.27 itojun while (cur && cur->ai_next)
1346 1.27 itojun cur = cur->ai_next;
1347 1.42 itojun } else
1348 1.27 itojun ERR(EAI_FAMILY); /*xxx*/
1349 1.27 itojun }
1350 1.27 itojun break;
1351 1.27 itojun default:
1352 1.27 itojun if (inet_pton(afd->a_af, hostname, pton) == 1) {
1353 1.27 itojun if (pai->ai_family == afd->a_af ||
1354 1.27 itojun pai->ai_family == PF_UNSPEC /*?*/) {
1355 1.27 itojun GET_AI(cur->ai_next, afd, pton);
1356 1.89 christos GET_PORT(cur->ai_next, servname, svd);
1357 1.65 itojun if ((pai->ai_flags & AI_CANONNAME)) {
1358 1.65 itojun /*
1359 1.65 itojun * Set the numeric address itself as
1360 1.65 itojun * the canonical name, based on a
1361 1.65 itojun * clarification in rfc2553bis-03.
1362 1.65 itojun */
1363 1.65 itojun GET_CANONNAME(cur->ai_next, canonname);
1364 1.65 itojun }
1365 1.81 christos while (cur->ai_next)
1366 1.27 itojun cur = cur->ai_next;
1367 1.42 itojun } else
1368 1.27 itojun ERR(EAI_FAMILY); /*xxx*/
1369 1.27 itojun }
1370 1.27 itojun break;
1371 1.1 itojun }
1372 1.1 itojun
1373 1.14 itojun *res = sentinel.ai_next;
1374 1.14 itojun return 0;
1375 1.14 itojun
1376 1.14 itojun free:
1377 1.14 itojun bad:
1378 1.14 itojun if (sentinel.ai_next)
1379 1.14 itojun freeaddrinfo(sentinel.ai_next);
1380 1.14 itojun return error;
1381 1.14 itojun }
1382 1.14 itojun
1383 1.14 itojun /*
1384 1.14 itojun * numeric hostname with scope
1385 1.14 itojun */
1386 1.14 itojun static int
1387 1.70 christos explore_numeric_scope(const struct addrinfo *pai, const char *hostname,
1388 1.89 christos const char *servname, struct addrinfo **res, struct servent_data *svd)
1389 1.14 itojun {
1390 1.29 itojun #if !defined(SCOPE_DELIMITER) || !defined(INET6)
1391 1.90 christos return explore_numeric(pai, hostname, servname, res, hostname, svd);
1392 1.14 itojun #else
1393 1.14 itojun const struct afd *afd;
1394 1.14 itojun struct addrinfo *cur;
1395 1.14 itojun int error;
1396 1.125 christos char *hostname2 = NULL, *addr;
1397 1.125 christos const char *cp, *scope;
1398 1.14 itojun struct sockaddr_in6 *sin6;
1399 1.14 itojun
1400 1.51 lukem _DIAGASSERT(pai != NULL);
1401 1.51 lukem /* hostname may be NULL */
1402 1.51 lukem /* servname may be NULL */
1403 1.51 lukem _DIAGASSERT(res != NULL);
1404 1.51 lukem
1405 1.14 itojun /*
1406 1.14 itojun * if the servname does not match socktype/protocol, ignore it.
1407 1.14 itojun */
1408 1.89 christos if (get_portmatch(pai, servname, svd) != 0)
1409 1.14 itojun return 0;
1410 1.14 itojun
1411 1.14 itojun afd = find_afd(pai->ai_family);
1412 1.42 itojun if (afd == NULL)
1413 1.42 itojun return 0;
1414 1.42 itojun
1415 1.14 itojun if (!afd->a_scoped)
1416 1.89 christos return explore_numeric(pai, hostname, servname, res, hostname,
1417 1.89 christos svd);
1418 1.14 itojun
1419 1.14 itojun cp = strchr(hostname, SCOPE_DELIMITER);
1420 1.14 itojun if (cp == NULL)
1421 1.89 christos return explore_numeric(pai, hostname, servname, res, hostname,
1422 1.89 christos svd);
1423 1.14 itojun
1424 1.30 itojun /*
1425 1.30 itojun * Handle special case of <scoped_address><delimiter><scope id>
1426 1.30 itojun */
1427 1.30 itojun hostname2 = strdup(hostname);
1428 1.30 itojun if (hostname2 == NULL)
1429 1.30 itojun return EAI_MEMORY;
1430 1.30 itojun /* terminate at the delimiter */
1431 1.30 itojun hostname2[cp - hostname] = '\0';
1432 1.30 itojun addr = hostname2;
1433 1.30 itojun scope = cp + 1;
1434 1.1 itojun
1435 1.89 christos error = explore_numeric(pai, addr, servname, res, hostname, svd);
1436 1.1 itojun if (error == 0) {
1437 1.59 itojun u_int32_t scopeid;
1438 1.27 itojun
1439 1.14 itojun for (cur = *res; cur; cur = cur->ai_next) {
1440 1.14 itojun if (cur->ai_family != AF_INET6)
1441 1.14 itojun continue;
1442 1.36 christos sin6 = (struct sockaddr_in6 *)(void *)cur->ai_addr;
1443 1.125 christos if (ip6_str2scopeid(__UNCONST(scope), sin6, &scopeid)
1444 1.125 christos == -1) {
1445 1.27 itojun free(hostname2);
1446 1.115 christos return EAI_NODATA; /* XXX: is return OK? */
1447 1.27 itojun }
1448 1.27 itojun sin6->sin6_scope_id = scopeid;
1449 1.14 itojun }
1450 1.1 itojun }
1451 1.14 itojun
1452 1.14 itojun free(hostname2);
1453 1.14 itojun
1454 1.1 itojun return error;
1455 1.14 itojun #endif
1456 1.1 itojun }
1457 1.1 itojun
1458 1.1 itojun static int
1459 1.70 christos get_canonname(const struct addrinfo *pai, struct addrinfo *ai, const char *str)
1460 1.1 itojun {
1461 1.51 lukem
1462 1.51 lukem _DIAGASSERT(pai != NULL);
1463 1.51 lukem _DIAGASSERT(ai != NULL);
1464 1.51 lukem _DIAGASSERT(str != NULL);
1465 1.51 lukem
1466 1.14 itojun if ((pai->ai_flags & AI_CANONNAME) != 0) {
1467 1.66 itojun ai->ai_canonname = strdup(str);
1468 1.14 itojun if (ai->ai_canonname == NULL)
1469 1.14 itojun return EAI_MEMORY;
1470 1.14 itojun }
1471 1.14 itojun return 0;
1472 1.14 itojun }
1473 1.1 itojun
1474 1.109 christos struct addrinfo *
1475 1.93 tsarna allocaddrinfo(socklen_t addrlen)
1476 1.93 tsarna {
1477 1.93 tsarna struct addrinfo *ai;
1478 1.93 tsarna
1479 1.93 tsarna ai = calloc(sizeof(struct addrinfo) + addrlen, 1);
1480 1.93 tsarna if (ai) {
1481 1.93 tsarna ai->ai_addr = (void *)(ai+1);
1482 1.93 tsarna ai->ai_addrlen = ai->ai_addr->sa_len = addrlen;
1483 1.93 tsarna }
1484 1.93 tsarna
1485 1.93 tsarna return ai;
1486 1.93 tsarna }
1487 1.93 tsarna
1488 1.14 itojun static struct addrinfo *
1489 1.70 christos get_ai(const struct addrinfo *pai, const struct afd *afd, const char *addr)
1490 1.14 itojun {
1491 1.14 itojun char *p;
1492 1.14 itojun struct addrinfo *ai;
1493 1.93 tsarna struct sockaddr *save;
1494 1.12 lukem
1495 1.51 lukem _DIAGASSERT(pai != NULL);
1496 1.51 lukem _DIAGASSERT(afd != NULL);
1497 1.51 lukem _DIAGASSERT(addr != NULL);
1498 1.51 lukem
1499 1.93 tsarna ai = allocaddrinfo((socklen_t)afd->a_socklen);
1500 1.14 itojun if (ai == NULL)
1501 1.14 itojun return NULL;
1502 1.14 itojun
1503 1.102 christos save = ai->ai_addr;
1504 1.14 itojun memcpy(ai, pai, sizeof(struct addrinfo));
1505 1.93 tsarna
1506 1.102 christos /* since we just overwrote all of ai, we have
1507 1.102 christos to restore ai_addr and ai_addrlen */
1508 1.102 christos ai->ai_addr = save;
1509 1.102 christos ai->ai_addrlen = (socklen_t)afd->a_socklen;
1510 1.102 christos
1511 1.14 itojun ai->ai_addr->sa_family = ai->ai_family = afd->a_af;
1512 1.36 christos p = (char *)(void *)(ai->ai_addr);
1513 1.20 mycroft memcpy(p + afd->a_off, addr, (size_t)afd->a_addrlen);
1514 1.14 itojun return ai;
1515 1.14 itojun }
1516 1.1 itojun
1517 1.14 itojun static int
1518 1.89 christos get_portmatch(const struct addrinfo *ai, const char *servname,
1519 1.89 christos struct servent_data *svd)
1520 1.14 itojun {
1521 1.4 itojun
1522 1.51 lukem _DIAGASSERT(ai != NULL);
1523 1.51 lukem /* servname may be NULL */
1524 1.51 lukem
1525 1.89 christos return get_port(ai, servname, 1, svd);
1526 1.1 itojun }
1527 1.1 itojun
1528 1.1 itojun static int
1529 1.89 christos get_port(const struct addrinfo *ai, const char *servname, int matchonly,
1530 1.89 christos struct servent_data *svd)
1531 1.1 itojun {
1532 1.14 itojun const char *proto;
1533 1.14 itojun struct servent *sp;
1534 1.14 itojun int port;
1535 1.14 itojun int allownumeric;
1536 1.12 lukem
1537 1.51 lukem _DIAGASSERT(ai != NULL);
1538 1.51 lukem /* servname may be NULL */
1539 1.51 lukem
1540 1.14 itojun if (servname == NULL)
1541 1.14 itojun return 0;
1542 1.23 itojun switch (ai->ai_family) {
1543 1.23 itojun case AF_INET:
1544 1.27 itojun #ifdef AF_INET6
1545 1.23 itojun case AF_INET6:
1546 1.27 itojun #endif
1547 1.23 itojun break;
1548 1.23 itojun default:
1549 1.14 itojun return 0;
1550 1.23 itojun }
1551 1.1 itojun
1552 1.14 itojun switch (ai->ai_socktype) {
1553 1.14 itojun case SOCK_RAW:
1554 1.14 itojun return EAI_SERVICE;
1555 1.14 itojun case SOCK_DGRAM:
1556 1.14 itojun case SOCK_STREAM:
1557 1.14 itojun allownumeric = 1;
1558 1.14 itojun break;
1559 1.14 itojun case ANY:
1560 1.92 tls /*
1561 1.102 christos * This was 0. It is now 1 so that queries specifying
1562 1.92 tls * a NULL hint, or hint without socktype (but, hopefully,
1563 1.92 tls * with protocol) and numeric address actually work.
1564 1.92 tls */
1565 1.92 tls allownumeric = 1;
1566 1.14 itojun break;
1567 1.14 itojun default:
1568 1.14 itojun return EAI_SOCKTYPE;
1569 1.1 itojun }
1570 1.14 itojun
1571 1.67 itojun port = str2number(servname);
1572 1.67 itojun if (port >= 0) {
1573 1.14 itojun if (!allownumeric)
1574 1.14 itojun return EAI_SERVICE;
1575 1.14 itojun if (port < 0 || port > 65535)
1576 1.14 itojun return EAI_SERVICE;
1577 1.58 itojun port = htons(port);
1578 1.14 itojun } else {
1579 1.84 christos struct servent sv;
1580 1.67 itojun if (ai->ai_flags & AI_NUMERICSERV)
1581 1.67 itojun return EAI_NONAME;
1582 1.67 itojun
1583 1.14 itojun switch (ai->ai_socktype) {
1584 1.14 itojun case SOCK_DGRAM:
1585 1.14 itojun proto = "udp";
1586 1.1 itojun break;
1587 1.14 itojun case SOCK_STREAM:
1588 1.14 itojun proto = "tcp";
1589 1.1 itojun break;
1590 1.1 itojun default:
1591 1.14 itojun proto = NULL;
1592 1.1 itojun break;
1593 1.1 itojun }
1594 1.14 itojun
1595 1.89 christos sp = getservbyname_r(servname, proto, &sv, svd);
1596 1.85 christos if (sp == NULL)
1597 1.14 itojun return EAI_SERVICE;
1598 1.14 itojun port = sp->s_port;
1599 1.1 itojun }
1600 1.1 itojun
1601 1.103 christos if (!matchonly)
1602 1.103 christos *getport(__UNCONST(ai)) = port;
1603 1.14 itojun return 0;
1604 1.14 itojun }
1605 1.14 itojun
1606 1.14 itojun static const struct afd *
1607 1.70 christos find_afd(int af)
1608 1.14 itojun {
1609 1.14 itojun const struct afd *afd;
1610 1.1 itojun
1611 1.14 itojun if (af == PF_UNSPEC)
1612 1.14 itojun return NULL;
1613 1.14 itojun for (afd = afdl; afd->a_af; afd++) {
1614 1.14 itojun if (afd->a_af == af)
1615 1.14 itojun return afd;
1616 1.1 itojun }
1617 1.14 itojun return NULL;
1618 1.1 itojun }
1619 1.27 itojun
1620 1.98 christos /*
1621 1.98 christos * AI_ADDRCONFIG check: Build a mask containing a bit set for each address
1622 1.98 christos * family configured in the system.
1623 1.98 christos *
1624 1.98 christos */
1625 1.98 christos static int
1626 1.98 christos addrconfig(uint64_t *mask)
1627 1.98 christos {
1628 1.98 christos struct ifaddrs *ifaddrs, *ifa;
1629 1.98 christos
1630 1.98 christos if (getifaddrs(&ifaddrs) == -1)
1631 1.98 christos return -1;
1632 1.98 christos
1633 1.99 kardel *mask = 0;
1634 1.98 christos for (ifa = ifaddrs; ifa != NULL; ifa = ifa->ifa_next)
1635 1.98 christos if (ifa->ifa_addr && (ifa->ifa_flags & IFF_UP)) {
1636 1.98 christos _DIAGASSERT(ifa->ifa_addr->sa_family < 64);
1637 1.98 christos *mask |= (uint64_t)1 << ifa->ifa_addr->sa_family;
1638 1.98 christos }
1639 1.98 christos
1640 1.98 christos freeifaddrs(ifaddrs);
1641 1.98 christos return 0;
1642 1.98 christos }
1643 1.98 christos
1644 1.27 itojun #ifdef INET6
1645 1.27 itojun /* convert a string to a scope identifier. XXX: IPv6 specific */
1646 1.60 itojun static int
1647 1.70 christos ip6_str2scopeid(char *scope, struct sockaddr_in6 *sin6, u_int32_t *scopeid)
1648 1.27 itojun {
1649 1.59 itojun u_long lscopeid;
1650 1.51 lukem struct in6_addr *a6;
1651 1.27 itojun char *ep;
1652 1.46 itojun
1653 1.51 lukem _DIAGASSERT(scope != NULL);
1654 1.51 lukem _DIAGASSERT(sin6 != NULL);
1655 1.60 itojun _DIAGASSERT(scopeid != NULL);
1656 1.51 lukem
1657 1.51 lukem a6 = &sin6->sin6_addr;
1658 1.51 lukem
1659 1.46 itojun /* empty scopeid portion is invalid */
1660 1.46 itojun if (*scope == '\0')
1661 1.46 itojun return -1;
1662 1.27 itojun
1663 1.27 itojun if (IN6_IS_ADDR_LINKLOCAL(a6) || IN6_IS_ADDR_MC_LINKLOCAL(a6)) {
1664 1.27 itojun /*
1665 1.95 wiz * We currently assume a one-to-one mapping between links
1666 1.27 itojun * and interfaces, so we simply use interface indices for
1667 1.27 itojun * like-local scopes.
1668 1.27 itojun */
1669 1.60 itojun *scopeid = if_nametoindex(scope);
1670 1.60 itojun if (*scopeid == 0)
1671 1.27 itojun goto trynumeric;
1672 1.60 itojun return 0;
1673 1.27 itojun }
1674 1.27 itojun
1675 1.27 itojun /* still unclear about literal, allow numeric only - placeholder */
1676 1.27 itojun if (IN6_IS_ADDR_SITELOCAL(a6) || IN6_IS_ADDR_MC_SITELOCAL(a6))
1677 1.27 itojun goto trynumeric;
1678 1.27 itojun if (IN6_IS_ADDR_MC_ORGLOCAL(a6))
1679 1.27 itojun goto trynumeric;
1680 1.27 itojun else
1681 1.27 itojun goto trynumeric; /* global */
1682 1.27 itojun
1683 1.27 itojun /* try to convert to a numeric id as a last resort */
1684 1.27 itojun trynumeric:
1685 1.59 itojun errno = 0;
1686 1.59 itojun lscopeid = strtoul(scope, &ep, 10);
1687 1.61 itojun *scopeid = (u_int32_t)(lscopeid & 0xffffffffUL);
1688 1.60 itojun if (errno == 0 && ep && *ep == '\0' && *scopeid == lscopeid)
1689 1.60 itojun return 0;
1690 1.27 itojun else
1691 1.27 itojun return -1;
1692 1.27 itojun }
1693 1.42 itojun #endif
1694 1.32 itojun
1695 1.32 itojun /* code duplicate with gethnamaddr.c */
1696 1.32 itojun
1697 1.32 itojun static const char AskedForGot[] =
1698 1.32 itojun "gethostby*.getanswer: asked for \"%s\", got \"%s\"";
1699 1.32 itojun
1700 1.110 christos #define maybe_ok(res, nm, ok) (((res)->options & RES_NOCHECKNAME) != 0U || \
1701 1.110 christos (ok)(nm) != 0)
1702 1.32 itojun static struct addrinfo *
1703 1.110 christos getanswer(res_state res, const querybuf *answer, int anslen, const char *qname,
1704 1.110 christos int qtype, const struct addrinfo *pai)
1705 1.32 itojun {
1706 1.32 itojun struct addrinfo sentinel, *cur;
1707 1.103 christos struct addrinfo ai, *aip;
1708 1.32 itojun const struct afd *afd;
1709 1.32 itojun char *canonname;
1710 1.32 itojun const HEADER *hp;
1711 1.32 itojun const u_char *cp;
1712 1.32 itojun int n;
1713 1.32 itojun const u_char *eom;
1714 1.56 itojun char *bp, *ep;
1715 1.56 itojun int type, class, ancount, qdcount;
1716 1.32 itojun int haveanswer, had_error;
1717 1.32 itojun char tbuf[MAXDNAME];
1718 1.70 christos int (*name_ok) (const char *);
1719 1.32 itojun char hostbuf[8*1024];
1720 1.103 christos int port, pri, weight;
1721 1.103 christos struct srvinfo *srvlist, *srv, *csrv;
1722 1.32 itojun
1723 1.51 lukem _DIAGASSERT(answer != NULL);
1724 1.51 lukem _DIAGASSERT(qname != NULL);
1725 1.51 lukem _DIAGASSERT(pai != NULL);
1726 1.110 christos _DIAGASSERT(res != NULL);
1727 1.51 lukem
1728 1.32 itojun memset(&sentinel, 0, sizeof(sentinel));
1729 1.32 itojun cur = &sentinel;
1730 1.32 itojun
1731 1.32 itojun canonname = NULL;
1732 1.32 itojun eom = answer->buf + anslen;
1733 1.32 itojun switch (qtype) {
1734 1.32 itojun case T_A:
1735 1.32 itojun case T_AAAA:
1736 1.32 itojun case T_ANY: /*use T_ANY only for T_A/T_AAAA lookup*/
1737 1.32 itojun name_ok = res_hnok;
1738 1.32 itojun break;
1739 1.103 christos case T_SRV:
1740 1.103 christos name_ok = gai_srvok;
1741 1.103 christos break;
1742 1.32 itojun default:
1743 1.70 christos return NULL; /* XXX should be abort(); */
1744 1.32 itojun }
1745 1.32 itojun /*
1746 1.32 itojun * find first satisfactory answer
1747 1.32 itojun */
1748 1.32 itojun hp = &answer->hdr;
1749 1.32 itojun ancount = ntohs(hp->ancount);
1750 1.32 itojun qdcount = ntohs(hp->qdcount);
1751 1.32 itojun bp = hostbuf;
1752 1.56 itojun ep = hostbuf + sizeof hostbuf;
1753 1.32 itojun cp = answer->buf + HFIXEDSZ;
1754 1.32 itojun if (qdcount != 1) {
1755 1.32 itojun h_errno = NO_RECOVERY;
1756 1.115 christos return NULL;
1757 1.32 itojun }
1758 1.97 christos n = dn_expand(answer->buf, eom, cp, bp, (int)(ep - bp));
1759 1.110 christos if ((n < 0) || !maybe_ok(res, bp, name_ok)) {
1760 1.32 itojun h_errno = NO_RECOVERY;
1761 1.115 christos return NULL;
1762 1.32 itojun }
1763 1.32 itojun cp += n + QFIXEDSZ;
1764 1.32 itojun if (qtype == T_A || qtype == T_AAAA || qtype == T_ANY) {
1765 1.32 itojun /* res_send() has already verified that the query name is the
1766 1.32 itojun * same as the one we sent; this just gets the expanded name
1767 1.32 itojun * (i.e., with the succeeding search-domain tacked on).
1768 1.32 itojun */
1769 1.97 christos n = (int)strlen(bp) + 1; /* for the \0 */
1770 1.32 itojun if (n >= MAXHOSTNAMELEN) {
1771 1.32 itojun h_errno = NO_RECOVERY;
1772 1.115 christos return NULL;
1773 1.32 itojun }
1774 1.32 itojun canonname = bp;
1775 1.32 itojun bp += n;
1776 1.32 itojun /* The qname can be abbreviated, but h_name is now absolute. */
1777 1.32 itojun qname = canonname;
1778 1.32 itojun }
1779 1.32 itojun haveanswer = 0;
1780 1.32 itojun had_error = 0;
1781 1.103 christos srvlist = NULL;
1782 1.32 itojun while (ancount-- > 0 && cp < eom && !had_error) {
1783 1.97 christos n = dn_expand(answer->buf, eom, cp, bp, (int)(ep - bp));
1784 1.110 christos if ((n < 0) || !maybe_ok(res, bp, name_ok)) {
1785 1.32 itojun had_error++;
1786 1.32 itojun continue;
1787 1.32 itojun }
1788 1.32 itojun cp += n; /* name */
1789 1.32 itojun type = _getshort(cp);
1790 1.102 christos cp += INT16SZ; /* type */
1791 1.32 itojun class = _getshort(cp);
1792 1.102 christos cp += INT16SZ + INT32SZ; /* class, TTL */
1793 1.32 itojun n = _getshort(cp);
1794 1.32 itojun cp += INT16SZ; /* len */
1795 1.32 itojun if (class != C_IN) {
1796 1.32 itojun /* XXX - debug? syslog? */
1797 1.32 itojun cp += n;
1798 1.32 itojun continue; /* XXX - had_error++ ? */
1799 1.32 itojun }
1800 1.32 itojun if ((qtype == T_A || qtype == T_AAAA || qtype == T_ANY) &&
1801 1.32 itojun type == T_CNAME) {
1802 1.97 christos n = dn_expand(answer->buf, eom, cp, tbuf, (int)sizeof tbuf);
1803 1.110 christos if ((n < 0) || !maybe_ok(res, tbuf, name_ok)) {
1804 1.32 itojun had_error++;
1805 1.32 itojun continue;
1806 1.32 itojun }
1807 1.32 itojun cp += n;
1808 1.32 itojun /* Get canonical name. */
1809 1.97 christos n = (int)strlen(tbuf) + 1; /* for the \0 */
1810 1.56 itojun if (n > ep - bp || n >= MAXHOSTNAMELEN) {
1811 1.32 itojun had_error++;
1812 1.32 itojun continue;
1813 1.32 itojun }
1814 1.66 itojun strlcpy(bp, tbuf, (size_t)(ep - bp));
1815 1.32 itojun canonname = bp;
1816 1.32 itojun bp += n;
1817 1.32 itojun continue;
1818 1.32 itojun }
1819 1.32 itojun if (qtype == T_ANY) {
1820 1.32 itojun if (!(type == T_A || type == T_AAAA)) {
1821 1.32 itojun cp += n;
1822 1.32 itojun continue;
1823 1.32 itojun }
1824 1.32 itojun } else if (type != qtype) {
1825 1.120 is if (type != T_KEY && type != T_SIG && type != T_DNAME) {
1826 1.88 christos struct syslog_data sd = SYSLOG_DATA_INIT;
1827 1.88 christos syslog_r(LOG_NOTICE|LOG_AUTH, &sd,
1828 1.32 itojun "gethostby*.getanswer: asked for \"%s %s %s\", got type \"%s\"",
1829 1.32 itojun qname, p_class(C_IN), p_type(qtype),
1830 1.32 itojun p_type(type));
1831 1.88 christos }
1832 1.32 itojun cp += n;
1833 1.32 itojun continue; /* XXX - had_error++ ? */
1834 1.32 itojun }
1835 1.32 itojun switch (type) {
1836 1.32 itojun case T_A:
1837 1.32 itojun case T_AAAA:
1838 1.32 itojun if (strcasecmp(canonname, bp) != 0) {
1839 1.88 christos struct syslog_data sd = SYSLOG_DATA_INIT;
1840 1.88 christos syslog_r(LOG_NOTICE|LOG_AUTH, &sd,
1841 1.32 itojun AskedForGot, canonname, bp);
1842 1.32 itojun cp += n;
1843 1.32 itojun continue; /* XXX - had_error++ ? */
1844 1.32 itojun }
1845 1.32 itojun if (type == T_A && n != INADDRSZ) {
1846 1.32 itojun cp += n;
1847 1.32 itojun continue;
1848 1.32 itojun }
1849 1.32 itojun if (type == T_AAAA && n != IN6ADDRSZ) {
1850 1.32 itojun cp += n;
1851 1.32 itojun continue;
1852 1.62 itojun }
1853 1.62 itojun if (type == T_AAAA) {
1854 1.62 itojun struct in6_addr in6;
1855 1.62 itojun memcpy(&in6, cp, IN6ADDRSZ);
1856 1.62 itojun if (IN6_IS_ADDR_V4MAPPED(&in6)) {
1857 1.62 itojun cp += n;
1858 1.62 itojun continue;
1859 1.62 itojun }
1860 1.32 itojun }
1861 1.32 itojun if (!haveanswer) {
1862 1.32 itojun int nn;
1863 1.32 itojun
1864 1.32 itojun canonname = bp;
1865 1.97 christos nn = (int)strlen(bp) + 1; /* for the \0 */
1866 1.32 itojun bp += nn;
1867 1.32 itojun }
1868 1.32 itojun
1869 1.32 itojun /* don't overwrite pai */
1870 1.32 itojun ai = *pai;
1871 1.32 itojun ai.ai_family = (type == T_A) ? AF_INET : AF_INET6;
1872 1.32 itojun afd = find_afd(ai.ai_family);
1873 1.32 itojun if (afd == NULL) {
1874 1.32 itojun cp += n;
1875 1.32 itojun continue;
1876 1.32 itojun }
1877 1.36 christos cur->ai_next = get_ai(&ai, afd, (const char *)cp);
1878 1.32 itojun if (cur->ai_next == NULL)
1879 1.32 itojun had_error++;
1880 1.32 itojun while (cur && cur->ai_next)
1881 1.32 itojun cur = cur->ai_next;
1882 1.32 itojun cp += n;
1883 1.32 itojun break;
1884 1.103 christos case T_SRV:
1885 1.103 christos /* Add to SRV list. Insertion sort on priority. */
1886 1.103 christos pri = _getshort(cp);
1887 1.103 christos cp += INT16SZ;
1888 1.103 christos weight = _getshort(cp);
1889 1.103 christos cp += INT16SZ;
1890 1.103 christos port = _getshort(cp);
1891 1.103 christos cp += INT16SZ;
1892 1.103 christos n = dn_expand(answer->buf, eom, cp, tbuf,
1893 1.103 christos (int)sizeof(tbuf));
1894 1.110 christos if ((n < 0) || !maybe_ok(res, tbuf, res_hnok)) {
1895 1.103 christos had_error++;
1896 1.103 christos continue;
1897 1.103 christos }
1898 1.103 christos cp += n;
1899 1.103 christos if (strlen(tbuf) + 1 >= MAXDNAME) {
1900 1.103 christos had_error++;
1901 1.103 christos continue;
1902 1.103 christos }
1903 1.103 christos srv = malloc(sizeof(*srv));
1904 1.103 christos if (!srv) {
1905 1.103 christos had_error++;
1906 1.103 christos continue;
1907 1.103 christos }
1908 1.103 christos strlcpy(srv->name, tbuf, sizeof(srv->name));
1909 1.103 christos srv->pri = pri;
1910 1.103 christos srv->weight = weight;
1911 1.103 christos srv->port = port;
1912 1.103 christos /* Weight 0 is sorted before other weights. */
1913 1.103 christos if (!srvlist
1914 1.103 christos || srv->pri < srvlist->pri
1915 1.103 christos || (srv->pri == srvlist->pri &&
1916 1.103 christos (!srv->weight || srvlist->weight))) {
1917 1.103 christos srv->next = srvlist;
1918 1.103 christos srvlist = srv;
1919 1.103 christos } else {
1920 1.103 christos for (csrv = srvlist;
1921 1.103 christos csrv->next && csrv->next->pri <= srv->pri;
1922 1.103 christos csrv = csrv->next) {
1923 1.103 christos if (csrv->next->pri == srv->pri
1924 1.103 christos && (!srv->weight ||
1925 1.103 christos csrv->next->weight))
1926 1.103 christos break;
1927 1.103 christos }
1928 1.103 christos srv->next = csrv->next;
1929 1.103 christos csrv->next = srv;
1930 1.103 christos }
1931 1.103 christos continue; /* Don't add to haveanswer yet. */
1932 1.32 itojun default:
1933 1.32 itojun abort();
1934 1.32 itojun }
1935 1.32 itojun if (!had_error)
1936 1.32 itojun haveanswer++;
1937 1.32 itojun }
1938 1.103 christos
1939 1.103 christos if (srvlist) {
1940 1.103 christos /*
1941 1.103 christos * Check for explicit rejection.
1942 1.103 christos */
1943 1.103 christos if (!srvlist->next && !srvlist->name[0]) {
1944 1.103 christos free(srvlist);
1945 1.103 christos h_errno = HOST_NOT_FOUND;
1946 1.103 christos return NULL;
1947 1.103 christos }
1948 1.103 christos
1949 1.103 christos while (srvlist) {
1950 1.103 christos struct res_target q, q2;
1951 1.103 christos
1952 1.103 christos srv = srvlist;
1953 1.103 christos srvlist = srvlist->next;
1954 1.103 christos
1955 1.103 christos /*
1956 1.103 christos * Since res_* doesn't give the additional
1957 1.103 christos * section, we always look up.
1958 1.103 christos */
1959 1.103 christos memset(&q, 0, sizeof(q));
1960 1.103 christos memset(&q2, 0, sizeof(q2));
1961 1.103 christos
1962 1.103 christos q.name = srv->name;
1963 1.103 christos q.qclass = C_IN;
1964 1.103 christos q.qtype = T_AAAA;
1965 1.103 christos q.next = &q2;
1966 1.103 christos q2.name = srv->name;
1967 1.103 christos q2.qclass = C_IN;
1968 1.103 christos q2.qtype = T_A;
1969 1.103 christos
1970 1.103 christos aip = _dns_query(&q, pai, res, 0);
1971 1.103 christos
1972 1.103 christos if (aip != NULL) {
1973 1.103 christos cur->ai_next = aip;
1974 1.103 christos while (cur && cur->ai_next) {
1975 1.103 christos cur = cur->ai_next;
1976 1.103 christos *getport(cur) = htons(srv->port);
1977 1.103 christos haveanswer++;
1978 1.103 christos }
1979 1.103 christos }
1980 1.103 christos free(srv);
1981 1.103 christos }
1982 1.103 christos }
1983 1.32 itojun if (haveanswer) {
1984 1.103 christos if (!sentinel.ai_next->ai_canonname)
1985 1.103 christos (void)get_canonname(pai, sentinel.ai_next,
1986 1.103 christos canonname ? canonname : qname);
1987 1.32 itojun h_errno = NETDB_SUCCESS;
1988 1.32 itojun return sentinel.ai_next;
1989 1.32 itojun }
1990 1.32 itojun
1991 1.111 christos /* We could have walked a CNAME chain, */
1992 1.111 christos /* but the ultimate target may not have what we looked for */
1993 1.111 christos h_errno = ntohs(hp->ancount) > 0? NO_DATA : NO_RECOVERY;
1994 1.32 itojun return NULL;
1995 1.32 itojun }
1996 1.32 itojun
1997 1.73 tsarna #define SORTEDADDR(p) (((struct sockaddr_in *)(void *)(p->ai_next->ai_addr))->sin_addr.s_addr)
1998 1.73 tsarna #define SORTMATCH(p, s) ((SORTEDADDR(p) & (s).mask) == (s).addr.s_addr)
1999 1.73 tsarna
2000 1.73 tsarna static void
2001 1.73 tsarna aisort(struct addrinfo *s, res_state res)
2002 1.73 tsarna {
2003 1.73 tsarna struct addrinfo head, *t, *p;
2004 1.73 tsarna int i;
2005 1.73 tsarna
2006 1.73 tsarna head.ai_next = NULL;
2007 1.73 tsarna t = &head;
2008 1.73 tsarna
2009 1.73 tsarna for (i = 0; i < res->nsort; i++) {
2010 1.73 tsarna p = s;
2011 1.73 tsarna while (p->ai_next) {
2012 1.73 tsarna if ((p->ai_next->ai_family != AF_INET)
2013 1.73 tsarna || SORTMATCH(p, res->sort_list[i])) {
2014 1.73 tsarna t->ai_next = p->ai_next;
2015 1.73 tsarna t = t->ai_next;
2016 1.73 tsarna p->ai_next = p->ai_next->ai_next;
2017 1.73 tsarna } else {
2018 1.73 tsarna p = p->ai_next;
2019 1.73 tsarna }
2020 1.73 tsarna }
2021 1.73 tsarna }
2022 1.73 tsarna
2023 1.73 tsarna /* add rest of list and reset s to the new list*/
2024 1.73 tsarna t->ai_next = s->ai_next;
2025 1.73 tsarna s->ai_next = head.ai_next;
2026 1.73 tsarna }
2027 1.73 tsarna
2028 1.103 christos static struct addrinfo *
2029 1.103 christos _dns_query(struct res_target *q, const struct addrinfo *pai,
2030 1.103 christos res_state res, int dosearch)
2031 1.103 christos {
2032 1.103 christos struct res_target *q2 = q->next;
2033 1.103 christos querybuf *buf, *buf2;
2034 1.103 christos struct addrinfo sentinel, *cur, *ai;
2035 1.103 christos
2036 1.103 christos #ifdef DNS_DEBUG
2037 1.103 christos struct res_target *iter;
2038 1.103 christos for (iter = q; iter; iter = iter->next)
2039 1.103 christos printf("Query type %d for %s\n", iter->qtype, iter->name);
2040 1.103 christos #endif
2041 1.103 christos
2042 1.103 christos buf = malloc(sizeof(*buf));
2043 1.103 christos if (buf == NULL) {
2044 1.103 christos h_errno = NETDB_INTERNAL;
2045 1.103 christos return NULL;
2046 1.103 christos }
2047 1.103 christos buf2 = malloc(sizeof(*buf2));
2048 1.103 christos if (buf2 == NULL) {
2049 1.103 christos free(buf);
2050 1.103 christos h_errno = NETDB_INTERNAL;
2051 1.103 christos return NULL;
2052 1.103 christos }
2053 1.103 christos
2054 1.103 christos memset(&sentinel, 0, sizeof(sentinel));
2055 1.103 christos cur = &sentinel;
2056 1.103 christos
2057 1.103 christos q->answer = buf->buf;
2058 1.103 christos q->anslen = sizeof(buf->buf);
2059 1.103 christos if (q2) {
2060 1.103 christos q2->answer = buf2->buf;
2061 1.103 christos q2->anslen = sizeof(buf2->buf);
2062 1.103 christos }
2063 1.103 christos
2064 1.103 christos if (dosearch) {
2065 1.103 christos if (res_searchN(q->name, q, res) < 0)
2066 1.103 christos goto out;
2067 1.103 christos } else {
2068 1.103 christos if (res_queryN(q->name, q, res) < 0)
2069 1.103 christos goto out;
2070 1.103 christos }
2071 1.103 christos
2072 1.110 christos ai = getanswer(res, buf, q->n, q->name, q->qtype, pai);
2073 1.103 christos if (ai) {
2074 1.103 christos cur->ai_next = ai;
2075 1.103 christos while (cur && cur->ai_next)
2076 1.103 christos cur = cur->ai_next;
2077 1.103 christos }
2078 1.103 christos if (q2) {
2079 1.110 christos ai = getanswer(res, buf2, q2->n, q2->name, q2->qtype, pai);
2080 1.103 christos if (ai)
2081 1.103 christos cur->ai_next = ai;
2082 1.103 christos }
2083 1.103 christos free(buf);
2084 1.103 christos free(buf2);
2085 1.103 christos return sentinel.ai_next;
2086 1.103 christos out:
2087 1.103 christos free(buf);
2088 1.103 christos free(buf2);
2089 1.103 christos return NULL;
2090 1.103 christos }
2091 1.103 christos
2092 1.32 itojun /*ARGSUSED*/
2093 1.103 christos static struct addrinfo *
2094 1.103 christos _dns_srv_lookup(const char *name, const char *servname,
2095 1.103 christos const struct addrinfo *pai)
2096 1.32 itojun {
2097 1.103 christos static const char * const srvprotos[] = { "tcp", "udp" };
2098 1.103 christos static const int srvnottype[] = { SOCK_DGRAM, SOCK_STREAM };
2099 1.103 christos static const int nsrvprotos = 2;
2100 1.103 christos struct addrinfo sentinel, *cur, *ai;
2101 1.103 christos struct servent *serv, sv;
2102 1.103 christos struct servent_data svd;
2103 1.103 christos struct res_target q;
2104 1.73 tsarna res_state res;
2105 1.103 christos char *tname;
2106 1.103 christos int i;
2107 1.32 itojun
2108 1.103 christos res = __res_get_state();
2109 1.103 christos if (res == NULL)
2110 1.103 christos return NULL;
2111 1.32 itojun
2112 1.103 christos memset(&svd, 0, sizeof(svd));
2113 1.32 itojun memset(&sentinel, 0, sizeof(sentinel));
2114 1.32 itojun cur = &sentinel;
2115 1.32 itojun
2116 1.103 christos /*
2117 1.103 christos * Iterate over supported SRV protocols.
2118 1.103 christos * (currently UDP and TCP only)
2119 1.103 christos */
2120 1.103 christos for (i = 0; i < nsrvprotos; i++) {
2121 1.103 christos /*
2122 1.103 christos * Check that the caller didn't specify a hint
2123 1.103 christos * which precludes this protocol.
2124 1.103 christos */
2125 1.103 christos if (pai->ai_socktype == srvnottype[i])
2126 1.103 christos continue;
2127 1.103 christos /*
2128 1.103 christos * If the caller specified a port,
2129 1.103 christos * then lookup the database for the
2130 1.103 christos * official service name.
2131 1.103 christos */
2132 1.103 christos serv = getservbyname_r(servname, srvprotos[i], &sv, &svd);
2133 1.103 christos if (serv == NULL)
2134 1.103 christos continue;
2135 1.103 christos
2136 1.103 christos /*
2137 1.103 christos * Construct service DNS name.
2138 1.103 christos */
2139 1.103 christos if (asprintf(&tname, "_%s._%s.%s", serv->s_name, serv->s_proto,
2140 1.103 christos name) < 0)
2141 1.103 christos continue;
2142 1.103 christos
2143 1.103 christos memset(&q, 0, sizeof(q));
2144 1.103 christos q.name = tname;
2145 1.103 christos q.qclass = C_IN;
2146 1.103 christos q.qtype = T_SRV;
2147 1.103 christos
2148 1.103 christos /*
2149 1.103 christos * Do SRV query.
2150 1.103 christos */
2151 1.103 christos ai = _dns_query(&q, pai, res, 1);
2152 1.103 christos if (ai) {
2153 1.103 christos cur->ai_next = ai;
2154 1.103 christos while (cur && cur->ai_next)
2155 1.103 christos cur = cur->ai_next;
2156 1.103 christos }
2157 1.103 christos free(tname);
2158 1.63 itojun }
2159 1.103 christos
2160 1.103 christos if (res->nsort)
2161 1.103 christos aisort(&sentinel, res);
2162 1.103 christos
2163 1.103 christos __res_put_state(res);
2164 1.103 christos
2165 1.103 christos return sentinel.ai_next;
2166 1.103 christos }
2167 1.103 christos
2168 1.103 christos /*ARGSUSED*/
2169 1.103 christos static struct addrinfo *
2170 1.103 christos _dns_host_lookup(const char *name, const struct addrinfo *pai)
2171 1.103 christos {
2172 1.103 christos struct res_target q, q2;
2173 1.103 christos struct addrinfo sentinel, *ai;
2174 1.103 christos res_state res;
2175 1.103 christos
2176 1.103 christos res = __res_get_state();
2177 1.103 christos if (res == NULL)
2178 1.103 christos return NULL;
2179 1.103 christos
2180 1.103 christos memset(&q, 0, sizeof(q2));
2181 1.103 christos memset(&q2, 0, sizeof(q2));
2182 1.63 itojun
2183 1.32 itojun switch (pai->ai_family) {
2184 1.32 itojun case AF_UNSPEC:
2185 1.32 itojun /* prefer IPv6 */
2186 1.52 itojun q.name = name;
2187 1.43 itojun q.qclass = C_IN;
2188 1.43 itojun q.qtype = T_AAAA;
2189 1.32 itojun q.next = &q2;
2190 1.52 itojun q2.name = name;
2191 1.43 itojun q2.qclass = C_IN;
2192 1.43 itojun q2.qtype = T_A;
2193 1.32 itojun break;
2194 1.32 itojun case AF_INET:
2195 1.52 itojun q.name = name;
2196 1.43 itojun q.qclass = C_IN;
2197 1.43 itojun q.qtype = T_A;
2198 1.32 itojun break;
2199 1.32 itojun case AF_INET6:
2200 1.52 itojun q.name = name;
2201 1.43 itojun q.qclass = C_IN;
2202 1.43 itojun q.qtype = T_AAAA;
2203 1.32 itojun break;
2204 1.32 itojun default:
2205 1.105 christos __res_put_state(res);
2206 1.103 christos h_errno = NETDB_INTERNAL;
2207 1.103 christos return NULL;
2208 1.32 itojun }
2209 1.73 tsarna
2210 1.103 christos ai = _dns_query(&q, pai, res, 1);
2211 1.73 tsarna
2212 1.103 christos memset(&sentinel, 0, sizeof(sentinel));
2213 1.103 christos sentinel.ai_next = ai;
2214 1.73 tsarna
2215 1.103 christos if (ai != NULL && res->nsort)
2216 1.73 tsarna aisort(&sentinel, res);
2217 1.73 tsarna
2218 1.73 tsarna __res_put_state(res);
2219 1.73 tsarna
2220 1.103 christos return sentinel.ai_next;
2221 1.103 christos }
2222 1.103 christos
2223 1.103 christos /*ARGSUSED*/
2224 1.103 christos static int
2225 1.103 christos _dns_getaddrinfo(void *rv, void *cb_data, va_list ap)
2226 1.103 christos {
2227 1.103 christos struct addrinfo *ai = NULL;
2228 1.103 christos const char *name, *servname;
2229 1.103 christos const struct addrinfo *pai;
2230 1.103 christos
2231 1.103 christos name = va_arg(ap, char *);
2232 1.103 christos pai = va_arg(ap, const struct addrinfo *);
2233 1.103 christos servname = va_arg(ap, char *);
2234 1.103 christos
2235 1.103 christos /*
2236 1.103 christos * Try doing SRV lookup on service first.
2237 1.103 christos */
2238 1.103 christos if (servname
2239 1.103 christos #ifdef AI_SRV
2240 1.103 christos && (pai->ai_flags & AI_SRV)
2241 1.103 christos #endif
2242 1.103 christos && !(pai->ai_flags & AI_NUMERICSERV)
2243 1.103 christos && str2number(servname) == -1) {
2244 1.103 christos
2245 1.103 christos #ifdef DNS_DEBUG
2246 1.103 christos printf("%s: try SRV lookup\n", __func__);
2247 1.103 christos #endif
2248 1.103 christos ai = _dns_srv_lookup(name, servname, pai);
2249 1.103 christos }
2250 1.103 christos
2251 1.103 christos /*
2252 1.103 christos * Do lookup on name.
2253 1.103 christos */
2254 1.103 christos if (ai == NULL) {
2255 1.103 christos
2256 1.103 christos #ifdef DNS_DEBUG
2257 1.103 christos printf("%s: try HOST lookup\n", __func__);
2258 1.103 christos #endif
2259 1.103 christos ai = _dns_host_lookup(name, pai);
2260 1.103 christos
2261 1.103 christos if (ai == NULL) {
2262 1.103 christos switch (h_errno) {
2263 1.103 christos case HOST_NOT_FOUND:
2264 1.111 christos case NO_DATA: // XXX: Perhaps we could differentiate
2265 1.111 christos // So that we could return EAI_NODATA?
2266 1.103 christos return NS_NOTFOUND;
2267 1.103 christos case TRY_AGAIN:
2268 1.103 christos return NS_TRYAGAIN;
2269 1.103 christos default:
2270 1.103 christos return NS_UNAVAIL;
2271 1.103 christos }
2272 1.103 christos }
2273 1.103 christos }
2274 1.103 christos
2275 1.103 christos *((struct addrinfo **)rv) = ai;
2276 1.32 itojun return NS_SUCCESS;
2277 1.32 itojun }
2278 1.32 itojun
2279 1.32 itojun static void
2280 1.70 christos _sethtent(FILE **hostf)
2281 1.32 itojun {
2282 1.51 lukem
2283 1.70 christos if (!*hostf)
2284 1.96 christos *hostf = fopen(_PATH_HOSTS, "re");
2285 1.32 itojun else
2286 1.70 christos rewind(*hostf);
2287 1.32 itojun }
2288 1.32 itojun
2289 1.32 itojun static void
2290 1.70 christos _endhtent(FILE **hostf)
2291 1.32 itojun {
2292 1.51 lukem
2293 1.70 christos if (*hostf) {
2294 1.70 christos (void) fclose(*hostf);
2295 1.70 christos *hostf = NULL;
2296 1.32 itojun }
2297 1.32 itojun }
2298 1.32 itojun
2299 1.32 itojun static struct addrinfo *
2300 1.70 christos _gethtent(FILE **hostf, const char *name, const struct addrinfo *pai)
2301 1.32 itojun {
2302 1.32 itojun char *p;
2303 1.48 itojun char *cp, *tname, *cname;
2304 1.32 itojun struct addrinfo hints, *res0, *res;
2305 1.32 itojun int error;
2306 1.32 itojun const char *addr;
2307 1.32 itojun char hostbuf[8*1024];
2308 1.32 itojun
2309 1.51 lukem _DIAGASSERT(name != NULL);
2310 1.51 lukem _DIAGASSERT(pai != NULL);
2311 1.51 lukem
2312 1.96 christos if (!*hostf && !(*hostf = fopen(_PATH_HOSTS, "re")))
2313 1.115 christos return NULL;
2314 1.32 itojun again:
2315 1.97 christos if (!(p = fgets(hostbuf, (int)sizeof hostbuf, *hostf)))
2316 1.115 christos return NULL;
2317 1.32 itojun if (*p == '#')
2318 1.32 itojun goto again;
2319 1.32 itojun if (!(cp = strpbrk(p, "#\n")))
2320 1.32 itojun goto again;
2321 1.32 itojun *cp = '\0';
2322 1.32 itojun if (!(cp = strpbrk(p, " \t")))
2323 1.32 itojun goto again;
2324 1.32 itojun *cp++ = '\0';
2325 1.32 itojun addr = p;
2326 1.32 itojun /* if this is not something we're looking for, skip it. */
2327 1.48 itojun cname = NULL;
2328 1.32 itojun while (cp && *cp) {
2329 1.32 itojun if (*cp == ' ' || *cp == '\t') {
2330 1.32 itojun cp++;
2331 1.32 itojun continue;
2332 1.32 itojun }
2333 1.48 itojun if (!cname)
2334 1.48 itojun cname = cp;
2335 1.32 itojun tname = cp;
2336 1.32 itojun if ((cp = strpbrk(cp, " \t")) != NULL)
2337 1.32 itojun *cp++ = '\0';
2338 1.32 itojun if (strcasecmp(name, tname) == 0)
2339 1.32 itojun goto found;
2340 1.32 itojun }
2341 1.32 itojun goto again;
2342 1.32 itojun
2343 1.32 itojun found:
2344 1.32 itojun hints = *pai;
2345 1.32 itojun hints.ai_flags = AI_NUMERICHOST;
2346 1.32 itojun error = getaddrinfo(addr, NULL, &hints, &res0);
2347 1.32 itojun if (error)
2348 1.32 itojun goto again;
2349 1.32 itojun for (res = res0; res; res = res->ai_next) {
2350 1.32 itojun /* cover it up */
2351 1.32 itojun res->ai_flags = pai->ai_flags;
2352 1.32 itojun
2353 1.32 itojun if (pai->ai_flags & AI_CANONNAME) {
2354 1.48 itojun if (get_canonname(pai, res, cname) != 0) {
2355 1.32 itojun freeaddrinfo(res0);
2356 1.32 itojun goto again;
2357 1.32 itojun }
2358 1.32 itojun }
2359 1.32 itojun }
2360 1.32 itojun return res0;
2361 1.32 itojun }
2362 1.32 itojun
2363 1.32 itojun /*ARGSUSED*/
2364 1.32 itojun static int
2365 1.70 christos _files_getaddrinfo(void *rv, void *cb_data, va_list ap)
2366 1.32 itojun {
2367 1.32 itojun const char *name;
2368 1.32 itojun const struct addrinfo *pai;
2369 1.32 itojun struct addrinfo sentinel, *cur;
2370 1.32 itojun struct addrinfo *p;
2371 1.70 christos #ifndef _REENTRANT
2372 1.70 christos static
2373 1.70 christos #endif
2374 1.70 christos FILE *hostf = NULL;
2375 1.32 itojun
2376 1.32 itojun name = va_arg(ap, char *);
2377 1.91 lukem pai = va_arg(ap, const struct addrinfo *);
2378 1.32 itojun
2379 1.32 itojun memset(&sentinel, 0, sizeof(sentinel));
2380 1.32 itojun cur = &sentinel;
2381 1.32 itojun
2382 1.70 christos _sethtent(&hostf);
2383 1.70 christos while ((p = _gethtent(&hostf, name, pai)) != NULL) {
2384 1.32 itojun cur->ai_next = p;
2385 1.32 itojun while (cur && cur->ai_next)
2386 1.32 itojun cur = cur->ai_next;
2387 1.32 itojun }
2388 1.70 christos _endhtent(&hostf);
2389 1.32 itojun
2390 1.32 itojun *((struct addrinfo **)rv) = sentinel.ai_next;
2391 1.32 itojun if (sentinel.ai_next == NULL)
2392 1.32 itojun return NS_NOTFOUND;
2393 1.32 itojun return NS_SUCCESS;
2394 1.32 itojun }
2395 1.32 itojun
2396 1.32 itojun #ifdef YP
2397 1.32 itojun /*ARGSUSED*/
2398 1.32 itojun static struct addrinfo *
2399 1.70 christos _yphostent(char *line, const struct addrinfo *pai)
2400 1.32 itojun {
2401 1.32 itojun struct addrinfo sentinel, *cur;
2402 1.32 itojun struct addrinfo hints, *res, *res0;
2403 1.32 itojun int error;
2404 1.51 lukem char *p;
2405 1.32 itojun const char *addr, *canonname;
2406 1.32 itojun char *nextline;
2407 1.32 itojun char *cp;
2408 1.32 itojun
2409 1.51 lukem _DIAGASSERT(line != NULL);
2410 1.51 lukem _DIAGASSERT(pai != NULL);
2411 1.51 lukem
2412 1.51 lukem p = line;
2413 1.32 itojun addr = canonname = NULL;
2414 1.32 itojun
2415 1.36 christos memset(&sentinel, 0, sizeof(sentinel));
2416 1.36 christos cur = &sentinel;
2417 1.36 christos
2418 1.32 itojun nextline:
2419 1.32 itojun /* terminate line */
2420 1.32 itojun cp = strchr(p, '\n');
2421 1.32 itojun if (cp) {
2422 1.32 itojun *cp++ = '\0';
2423 1.32 itojun nextline = cp;
2424 1.32 itojun } else
2425 1.32 itojun nextline = NULL;
2426 1.32 itojun
2427 1.32 itojun cp = strpbrk(p, " \t");
2428 1.32 itojun if (cp == NULL) {
2429 1.32 itojun if (canonname == NULL)
2430 1.115 christos return NULL;
2431 1.32 itojun else
2432 1.32 itojun goto done;
2433 1.32 itojun }
2434 1.32 itojun *cp++ = '\0';
2435 1.32 itojun
2436 1.32 itojun addr = p;
2437 1.32 itojun
2438 1.32 itojun while (cp && *cp) {
2439 1.32 itojun if (*cp == ' ' || *cp == '\t') {
2440 1.32 itojun cp++;
2441 1.32 itojun continue;
2442 1.32 itojun }
2443 1.32 itojun if (!canonname)
2444 1.32 itojun canonname = cp;
2445 1.32 itojun if ((cp = strpbrk(cp, " \t")) != NULL)
2446 1.32 itojun *cp++ = '\0';
2447 1.32 itojun }
2448 1.32 itojun
2449 1.32 itojun hints = *pai;
2450 1.32 itojun hints.ai_flags = AI_NUMERICHOST;
2451 1.32 itojun error = getaddrinfo(addr, NULL, &hints, &res0);
2452 1.32 itojun if (error == 0) {
2453 1.32 itojun for (res = res0; res; res = res->ai_next) {
2454 1.32 itojun /* cover it up */
2455 1.32 itojun res->ai_flags = pai->ai_flags;
2456 1.32 itojun
2457 1.32 itojun if (pai->ai_flags & AI_CANONNAME)
2458 1.32 itojun (void)get_canonname(pai, res, canonname);
2459 1.32 itojun }
2460 1.37 itojun } else
2461 1.37 itojun res0 = NULL;
2462 1.32 itojun if (res0) {
2463 1.32 itojun cur->ai_next = res0;
2464 1.80 christos while (cur->ai_next)
2465 1.32 itojun cur = cur->ai_next;
2466 1.32 itojun }
2467 1.32 itojun
2468 1.32 itojun if (nextline) {
2469 1.32 itojun p = nextline;
2470 1.32 itojun goto nextline;
2471 1.32 itojun }
2472 1.32 itojun
2473 1.32 itojun done:
2474 1.32 itojun return sentinel.ai_next;
2475 1.32 itojun }
2476 1.32 itojun
2477 1.32 itojun /*ARGSUSED*/
2478 1.32 itojun static int
2479 1.70 christos _yp_getaddrinfo(void *rv, void *cb_data, va_list ap)
2480 1.32 itojun {
2481 1.32 itojun struct addrinfo sentinel, *cur;
2482 1.32 itojun struct addrinfo *ai = NULL;
2483 1.72 christos char *ypbuf;
2484 1.72 christos int ypbuflen, r;
2485 1.32 itojun const char *name;
2486 1.32 itojun const struct addrinfo *pai;
2487 1.72 christos char *ypdomain;
2488 1.72 christos
2489 1.72 christos if (_yp_check(&ypdomain) == 0)
2490 1.72 christos return NS_UNAVAIL;
2491 1.32 itojun
2492 1.32 itojun name = va_arg(ap, char *);
2493 1.32 itojun pai = va_arg(ap, const struct addrinfo *);
2494 1.32 itojun
2495 1.32 itojun memset(&sentinel, 0, sizeof(sentinel));
2496 1.32 itojun cur = &sentinel;
2497 1.32 itojun
2498 1.32 itojun /* hosts.byname is only for IPv4 (Solaris8) */
2499 1.33 itojun if (pai->ai_family == PF_UNSPEC || pai->ai_family == PF_INET) {
2500 1.72 christos r = yp_match(ypdomain, "hosts.byname", name,
2501 1.72 christos (int)strlen(name), &ypbuf, &ypbuflen);
2502 1.33 itojun if (r == 0) {
2503 1.33 itojun struct addrinfo ai4;
2504 1.33 itojun
2505 1.33 itojun ai4 = *pai;
2506 1.33 itojun ai4.ai_family = AF_INET;
2507 1.72 christos ai = _yphostent(ypbuf, &ai4);
2508 1.33 itojun if (ai) {
2509 1.33 itojun cur->ai_next = ai;
2510 1.33 itojun while (cur && cur->ai_next)
2511 1.33 itojun cur = cur->ai_next;
2512 1.33 itojun }
2513 1.32 itojun }
2514 1.72 christos free(ypbuf);
2515 1.32 itojun }
2516 1.32 itojun
2517 1.32 itojun /* ipnodes.byname can hold both IPv4/v6 */
2518 1.72 christos r = yp_match(ypdomain, "ipnodes.byname", name,
2519 1.72 christos (int)strlen(name), &ypbuf, &ypbuflen);
2520 1.32 itojun if (r == 0) {
2521 1.72 christos ai = _yphostent(ypbuf, pai);
2522 1.72 christos if (ai)
2523 1.32 itojun cur->ai_next = ai;
2524 1.72 christos free(ypbuf);
2525 1.32 itojun }
2526 1.32 itojun
2527 1.32 itojun if (sentinel.ai_next == NULL) {
2528 1.32 itojun h_errno = HOST_NOT_FOUND;
2529 1.32 itojun return NS_NOTFOUND;
2530 1.32 itojun }
2531 1.32 itojun *((struct addrinfo **)rv) = sentinel.ai_next;
2532 1.32 itojun return NS_SUCCESS;
2533 1.32 itojun }
2534 1.32 itojun #endif
2535 1.32 itojun
2536 1.32 itojun /* resolver logic */
2537 1.32 itojun
2538 1.32 itojun /*
2539 1.32 itojun * Formulate a normal query, send, and await answer.
2540 1.32 itojun * Returned answer is placed in supplied buffer "answer".
2541 1.32 itojun * Perform preliminary check of answer, returning success only
2542 1.32 itojun * if no error is indicated and the answer count is nonzero.
2543 1.32 itojun * Return the size of the response on success, -1 on error.
2544 1.32 itojun * Error number is left in h_errno.
2545 1.32 itojun *
2546 1.32 itojun * Caller must parse answer and determine whether it answers the question.
2547 1.32 itojun */
2548 1.32 itojun static int
2549 1.70 christos res_queryN(const char *name, /* domain name */ struct res_target *target,
2550 1.116 christos res_state statp)
2551 1.32 itojun {
2552 1.32 itojun u_char buf[MAXPACKET];
2553 1.32 itojun HEADER *hp;
2554 1.32 itojun int n;
2555 1.32 itojun struct res_target *t;
2556 1.32 itojun int rcode;
2557 1.116 christos u_char *rdata;
2558 1.32 itojun int ancount;
2559 1.32 itojun
2560 1.51 lukem _DIAGASSERT(name != NULL);
2561 1.51 lukem /* XXX: target may be NULL??? */
2562 1.51 lukem
2563 1.32 itojun rcode = NOERROR;
2564 1.32 itojun ancount = 0;
2565 1.32 itojun
2566 1.32 itojun for (t = target; t; t = t->next) {
2567 1.32 itojun int class, type;
2568 1.32 itojun u_char *answer;
2569 1.32 itojun int anslen;
2570 1.116 christos u_int oflags;
2571 1.32 itojun
2572 1.32 itojun hp = (HEADER *)(void *)t->answer;
2573 1.116 christos oflags = statp->_flags;
2574 1.116 christos
2575 1.116 christos again:
2576 1.32 itojun hp->rcode = NOERROR; /* default */
2577 1.32 itojun
2578 1.32 itojun /* make it easier... */
2579 1.43 itojun class = t->qclass;
2580 1.43 itojun type = t->qtype;
2581 1.32 itojun answer = t->answer;
2582 1.32 itojun anslen = t->anslen;
2583 1.32 itojun #ifdef DEBUG
2584 1.116 christos if (statp->options & RES_DEBUG)
2585 1.70 christos printf(";; res_nquery(%s, %d, %d)\n", name, class, type);
2586 1.32 itojun #endif
2587 1.32 itojun
2588 1.116 christos n = res_nmkquery(statp, QUERY, name, class, type, NULL, 0, NULL,
2589 1.97 christos buf, (int)sizeof(buf));
2590 1.47 itojun #ifdef RES_USE_EDNS0
2591 1.116 christos if (n > 0 && (statp->_flags & RES_F_EDNS0ERR) == 0 &&
2592 1.116 christos (statp->options & (RES_USE_EDNS0|RES_USE_DNSSEC)) != 0) {
2593 1.116 christos n = res_nopt(statp, n, buf, (int)sizeof(buf), anslen);
2594 1.116 christos rdata = &buf[n];
2595 1.116 christos if (n > 0 && (statp->options & RES_NSID) != 0U) {
2596 1.116 christos n = res_nopt_rdata(statp, n, buf,
2597 1.116 christos (int)sizeof(buf),
2598 1.116 christos rdata, NS_OPT_NSID, 0, NULL);
2599 1.116 christos }
2600 1.116 christos }
2601 1.47 itojun #endif
2602 1.32 itojun if (n <= 0) {
2603 1.32 itojun #ifdef DEBUG
2604 1.116 christos if (statp->options & RES_DEBUG)
2605 1.70 christos printf(";; res_nquery: mkquery failed\n");
2606 1.32 itojun #endif
2607 1.32 itojun h_errno = NO_RECOVERY;
2608 1.70 christos return n;
2609 1.32 itojun }
2610 1.116 christos n = res_nsend(statp, buf, n, answer, anslen);
2611 1.116 christos if (n < 0) {
2612 1.116 christos #ifdef RES_USE_EDNS0
2613 1.116 christos /* if the query choked with EDNS0, retry without EDNS0 */
2614 1.116 christos if ((statp->options & (RES_USE_EDNS0|RES_USE_DNSSEC)) != 0U &&
2615 1.116 christos ((oflags ^ statp->_flags) & RES_F_EDNS0ERR) != 0) {
2616 1.116 christos statp->_flags |= RES_F_EDNS0ERR;
2617 1.116 christos if (statp->options & RES_DEBUG)
2618 1.116 christos printf(";; res_nquery: retry without EDNS0\n");
2619 1.116 christos goto again;
2620 1.116 christos }
2621 1.116 christos #endif
2622 1.35 itojun #if 0
2623 1.32 itojun #ifdef DEBUG
2624 1.116 christos if (statp->options & RES_DEBUG)
2625 1.32 itojun printf(";; res_query: send error\n");
2626 1.32 itojun #endif
2627 1.32 itojun h_errno = TRY_AGAIN;
2628 1.70 christos return n;
2629 1.116 christos #endif
2630 1.32 itojun }
2631 1.32 itojun
2632 1.35 itojun if (n < 0 || hp->rcode != NOERROR || ntohs(hp->ancount) == 0) {
2633 1.32 itojun rcode = hp->rcode; /* record most recent error */
2634 1.32 itojun #ifdef DEBUG
2635 1.116 christos if (statp->options & RES_DEBUG)
2636 1.116 christos printf(";; rcode = (%s), counts = an:%d ns:%d ar:%d\n",
2637 1.116 christos p_rcode(hp->rcode),
2638 1.116 christos ntohs(hp->ancount),
2639 1.116 christos ntohs(hp->nscount),
2640 1.116 christos ntohs(hp->arcount));
2641 1.32 itojun #endif
2642 1.32 itojun continue;
2643 1.32 itojun }
2644 1.32 itojun
2645 1.32 itojun ancount += ntohs(hp->ancount);
2646 1.32 itojun
2647 1.32 itojun t->n = n;
2648 1.32 itojun }
2649 1.32 itojun
2650 1.32 itojun if (ancount == 0) {
2651 1.32 itojun switch (rcode) {
2652 1.32 itojun case NXDOMAIN:
2653 1.32 itojun h_errno = HOST_NOT_FOUND;
2654 1.32 itojun break;
2655 1.32 itojun case SERVFAIL:
2656 1.32 itojun h_errno = TRY_AGAIN;
2657 1.32 itojun break;
2658 1.32 itojun case NOERROR:
2659 1.32 itojun h_errno = NO_DATA;
2660 1.32 itojun break;
2661 1.32 itojun case FORMERR:
2662 1.32 itojun case NOTIMP:
2663 1.32 itojun case REFUSED:
2664 1.32 itojun default:
2665 1.32 itojun h_errno = NO_RECOVERY;
2666 1.32 itojun break;
2667 1.32 itojun }
2668 1.70 christos return -1;
2669 1.32 itojun }
2670 1.70 christos return ancount;
2671 1.32 itojun }
2672 1.32 itojun
2673 1.32 itojun /*
2674 1.32 itojun * Formulate a normal query, send, and retrieve answer in supplied buffer.
2675 1.32 itojun * Return the size of the response on success, -1 on error.
2676 1.32 itojun * If enabled, implement search rules until answer or unrecoverable failure
2677 1.102 christos * is detected. Error code, if any, is left in h_errno.
2678 1.32 itojun */
2679 1.32 itojun static int
2680 1.73 tsarna res_searchN(const char *name, struct res_target *target, res_state res)
2681 1.32 itojun {
2682 1.32 itojun const char *cp, * const *domain;
2683 1.51 lukem HEADER *hp;
2684 1.32 itojun u_int dots;
2685 1.106 christos char buf[MAXHOSTNAMELEN];
2686 1.32 itojun int trailing_dot, ret, saved_herrno;
2687 1.32 itojun int got_nodata = 0, got_servfail = 0, tried_as_is = 0;
2688 1.71 christos
2689 1.51 lukem _DIAGASSERT(name != NULL);
2690 1.51 lukem _DIAGASSERT(target != NULL);
2691 1.51 lukem
2692 1.51 lukem hp = (HEADER *)(void *)target->answer; /*XXX*/
2693 1.51 lukem
2694 1.32 itojun errno = 0;
2695 1.32 itojun h_errno = HOST_NOT_FOUND; /* default, if we never query */
2696 1.32 itojun dots = 0;
2697 1.32 itojun for (cp = name; *cp; cp++)
2698 1.32 itojun dots += (*cp == '.');
2699 1.32 itojun trailing_dot = 0;
2700 1.32 itojun if (cp > name && *--cp == '.')
2701 1.32 itojun trailing_dot++;
2702 1.32 itojun
2703 1.32 itojun /*
2704 1.95 wiz * if there aren't any dots, it could be a user-level alias
2705 1.32 itojun */
2706 1.106 christos if (!dots && (cp = res_hostalias(res, name, buf, sizeof(buf))) != NULL) {
2707 1.70 christos ret = res_queryN(cp, target, res);
2708 1.70 christos return ret;
2709 1.70 christos }
2710 1.32 itojun
2711 1.32 itojun /*
2712 1.32 itojun * If there are dots in the name already, let's just give it a try
2713 1.32 itojun * 'as is'. The threshold can be set with the "ndots" option.
2714 1.32 itojun */
2715 1.32 itojun saved_herrno = -1;
2716 1.70 christos if (dots >= res->ndots) {
2717 1.70 christos ret = res_querydomainN(name, NULL, target, res);
2718 1.73 tsarna if (ret > 0)
2719 1.115 christos return ret;
2720 1.32 itojun saved_herrno = h_errno;
2721 1.32 itojun tried_as_is++;
2722 1.32 itojun }
2723 1.32 itojun
2724 1.32 itojun /*
2725 1.32 itojun * We do at least one level of search if
2726 1.32 itojun * - there is no dot and RES_DEFNAME is set, or
2727 1.32 itojun * - there is at least one dot, there is no trailing dot,
2728 1.32 itojun * and RES_DNSRCH is set.
2729 1.32 itojun */
2730 1.70 christos if ((!dots && (res->options & RES_DEFNAMES)) ||
2731 1.70 christos (dots && !trailing_dot && (res->options & RES_DNSRCH))) {
2732 1.32 itojun int done = 0;
2733 1.32 itojun
2734 1.70 christos for (domain = (const char * const *)res->dnsrch;
2735 1.32 itojun *domain && !done;
2736 1.32 itojun domain++) {
2737 1.32 itojun
2738 1.70 christos ret = res_querydomainN(name, *domain, target, res);
2739 1.73 tsarna if (ret > 0)
2740 1.70 christos return ret;
2741 1.32 itojun
2742 1.32 itojun /*
2743 1.32 itojun * If no server present, give up.
2744 1.32 itojun * If name isn't found in this domain,
2745 1.32 itojun * keep trying higher domains in the search list
2746 1.32 itojun * (if that's enabled).
2747 1.32 itojun * On a NO_DATA error, keep trying, otherwise
2748 1.32 itojun * a wildcard entry of another type could keep us
2749 1.32 itojun * from finding this entry higher in the domain.
2750 1.32 itojun * If we get some other error (negative answer or
2751 1.32 itojun * server failure), then stop searching up,
2752 1.32 itojun * but try the input name below in case it's
2753 1.32 itojun * fully-qualified.
2754 1.32 itojun */
2755 1.32 itojun if (errno == ECONNREFUSED) {
2756 1.32 itojun h_errno = TRY_AGAIN;
2757 1.70 christos return -1;
2758 1.32 itojun }
2759 1.32 itojun
2760 1.32 itojun switch (h_errno) {
2761 1.32 itojun case NO_DATA:
2762 1.32 itojun got_nodata++;
2763 1.32 itojun /* FALLTHROUGH */
2764 1.32 itojun case HOST_NOT_FOUND:
2765 1.32 itojun /* keep trying */
2766 1.32 itojun break;
2767 1.32 itojun case TRY_AGAIN:
2768 1.32 itojun if (hp->rcode == SERVFAIL) {
2769 1.32 itojun /* try next search element, if any */
2770 1.32 itojun got_servfail++;
2771 1.32 itojun break;
2772 1.32 itojun }
2773 1.32 itojun /* FALLTHROUGH */
2774 1.32 itojun default:
2775 1.32 itojun /* anything else implies that we're done */
2776 1.32 itojun done++;
2777 1.32 itojun }
2778 1.32 itojun /*
2779 1.32 itojun * if we got here for some reason other than DNSRCH,
2780 1.32 itojun * we only wanted one iteration of the loop, so stop.
2781 1.32 itojun */
2782 1.70 christos if (!(res->options & RES_DNSRCH))
2783 1.102 christos done++;
2784 1.32 itojun }
2785 1.32 itojun }
2786 1.32 itojun
2787 1.32 itojun /*
2788 1.32 itojun * if we have not already tried the name "as is", do that now.
2789 1.32 itojun * note that we do this regardless of how many dots were in the
2790 1.32 itojun * name or whether it ends with a dot.
2791 1.32 itojun */
2792 1.32 itojun if (!tried_as_is) {
2793 1.70 christos ret = res_querydomainN(name, NULL, target, res);
2794 1.73 tsarna if (ret > 0)
2795 1.70 christos return ret;
2796 1.32 itojun }
2797 1.32 itojun
2798 1.32 itojun /*
2799 1.32 itojun * if we got here, we didn't satisfy the search.
2800 1.32 itojun * if we did an initial full query, return that query's h_errno
2801 1.32 itojun * (note that we wouldn't be here if that query had succeeded).
2802 1.32 itojun * else if we ever got a nodata, send that back as the reason.
2803 1.32 itojun * else send back meaningless h_errno, that being the one from
2804 1.32 itojun * the last DNSRCH we did.
2805 1.32 itojun */
2806 1.32 itojun if (saved_herrno != -1)
2807 1.32 itojun h_errno = saved_herrno;
2808 1.32 itojun else if (got_nodata)
2809 1.32 itojun h_errno = NO_DATA;
2810 1.32 itojun else if (got_servfail)
2811 1.32 itojun h_errno = TRY_AGAIN;
2812 1.70 christos return -1;
2813 1.32 itojun }
2814 1.32 itojun
2815 1.32 itojun /*
2816 1.32 itojun * Perform a call on res_query on the concatenation of name and domain,
2817 1.32 itojun * removing a trailing dot from name if domain is NULL.
2818 1.32 itojun */
2819 1.32 itojun static int
2820 1.73 tsarna res_querydomainN(const char *name, const char *domain,
2821 1.70 christos struct res_target *target, res_state res)
2822 1.32 itojun {
2823 1.32 itojun char nbuf[MAXDNAME];
2824 1.32 itojun const char *longname = nbuf;
2825 1.124 mrg size_t n;
2826 1.51 lukem
2827 1.51 lukem _DIAGASSERT(name != NULL);
2828 1.51 lukem /* XXX: target may be NULL??? */
2829 1.32 itojun
2830 1.32 itojun #ifdef DEBUG
2831 1.70 christos if (res->options & RES_DEBUG)
2832 1.32 itojun printf(";; res_querydomain(%s, %s)\n",
2833 1.32 itojun name, domain?domain:"<Nil>");
2834 1.32 itojun #endif
2835 1.32 itojun if (domain == NULL) {
2836 1.32 itojun /*
2837 1.32 itojun * Check for trailing '.';
2838 1.32 itojun * copy without '.' if present.
2839 1.32 itojun */
2840 1.32 itojun n = strlen(name);
2841 1.63 itojun if (n + 1 > sizeof(nbuf)) {
2842 1.32 itojun h_errno = NO_RECOVERY;
2843 1.70 christos return -1;
2844 1.32 itojun }
2845 1.40 itojun if (n > 0 && name[--n] == '.') {
2846 1.124 mrg snprintf(nbuf, sizeof(nbuf), "%*s", (int)n, name);
2847 1.32 itojun } else
2848 1.32 itojun longname = name;
2849 1.32 itojun } else {
2850 1.124 mrg if ((size_t)snprintf(nbuf, sizeof(nbuf), "%s.%s",
2851 1.124 mrg name, domain) >= sizeof(nbuf)) {
2852 1.32 itojun h_errno = NO_RECOVERY;
2853 1.70 christos return -1;
2854 1.32 itojun }
2855 1.32 itojun }
2856 1.70 christos return res_queryN(longname, target, res);
2857 1.32 itojun }
2858 1.103 christos
2859 1.103 christos #ifdef TEST
2860 1.103 christos int
2861 1.103 christos main(int argc, char *argv[]) {
2862 1.103 christos struct addrinfo *ai, *sai;
2863 1.103 christos int i, e;
2864 1.103 christos char buf[1024];
2865 1.103 christos
2866 1.103 christos for (i = 1; i < argc; i++) {
2867 1.103 christos if ((e = getaddrinfo(argv[i], NULL, NULL, &sai)) != 0)
2868 1.103 christos warnx("%s: %s", argv[i], gai_strerror(e));
2869 1.103 christos for (ai = sai; ai; ai = ai->ai_next) {
2870 1.103 christos sockaddr_snprintf(buf, sizeof(buf), "%a", ai->ai_addr);
2871 1.103 christos printf("flags=0x%x family=%d socktype=%d protocol=%d "
2872 1.103 christos "addrlen=%zu addr=%s canonname=%s next=%p\n",
2873 1.103 christos ai->ai_flags,
2874 1.103 christos ai->ai_family,
2875 1.103 christos ai->ai_socktype,
2876 1.103 christos ai->ai_protocol,
2877 1.103 christos (size_t)ai->ai_addrlen,
2878 1.103 christos buf,
2879 1.103 christos ai->ai_canonname,
2880 1.103 christos ai->ai_next);
2881 1.103 christos }
2882 1.103 christos if (sai)
2883 1.103 christos freeaddrinfo(sai);
2884 1.103 christos }
2885 1.103 christos return 0;
2886 1.103 christos }
2887 1.103 christos #endif
2888