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