getaddrinfo.c revision 1.18 1 1.18 itojun /* $NetBSD: getaddrinfo.c,v 1.18 2000/01/17 08:34:04 itojun Exp $ */
2 1.6 itojun
3 1.1 itojun /*
4 1.1 itojun * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
5 1.1 itojun * All rights reserved.
6 1.1 itojun *
7 1.1 itojun * Redistribution and use in source and binary forms, with or without
8 1.1 itojun * modification, are permitted provided that the following conditions
9 1.1 itojun * are met:
10 1.1 itojun * 1. Redistributions of source code must retain the above copyright
11 1.1 itojun * notice, this list of conditions and the following disclaimer.
12 1.1 itojun * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 itojun * notice, this list of conditions and the following disclaimer in the
14 1.1 itojun * documentation and/or other materials provided with the distribution.
15 1.1 itojun * 3. Neither the name of the project nor the names of its contributors
16 1.1 itojun * may be used to endorse or promote products derived from this software
17 1.1 itojun * without specific prior written permission.
18 1.1 itojun *
19 1.1 itojun * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
20 1.1 itojun * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 1.1 itojun * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 1.1 itojun * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
23 1.1 itojun * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 1.1 itojun * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 1.1 itojun * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 1.1 itojun * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 1.1 itojun * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 1.1 itojun * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 1.1 itojun * SUCH DAMAGE.
30 1.1 itojun */
31 1.1 itojun
32 1.1 itojun /*
33 1.1 itojun * "#ifdef FAITH" part is local hack for supporting IPv4-v6 translator.
34 1.1 itojun *
35 1.1 itojun * Issues to be discussed:
36 1.1 itojun * - Thread safe-ness must be checked.
37 1.1 itojun * - Return values. There are nonstandard return values defined and used
38 1.14 itojun * in the source code. This is because RFC2553 is silent about which error
39 1.1 itojun * code must be returned for which situation.
40 1.14 itojun * Note:
41 1.14 itojun * - We use getipnodebyname() just for thread-safeness. There's no intent
42 1.14 itojun * to let it do PF_UNSPEC (actually we never pass PF_UNSPEC to
43 1.14 itojun * getipnodebyname().
44 1.14 itojun * - The code filters out AFs that are not supported by the kernel,
45 1.14 itojun * when resolving FQDNs and globbing NULL hostname. Is it the right
46 1.14 itojun * thing to do? What is the relationship with post-RFC2553 AI_ADDRCONFIG
47 1.14 itojun * in ai_flags?
48 1.1 itojun */
49 1.1 itojun
50 1.14 itojun #include <sys/types.h>
51 1.1 itojun #include <sys/param.h>
52 1.1 itojun #include <sys/socket.h>
53 1.14 itojun #include <net/if.h>
54 1.1 itojun #include <netinet/in.h>
55 1.1 itojun #include <arpa/inet.h>
56 1.1 itojun #include <arpa/nameser.h>
57 1.1 itojun #include <netdb.h>
58 1.1 itojun #include <resolv.h>
59 1.14 itojun #include <string.h>
60 1.14 itojun #include <stdlib.h>
61 1.12 lukem #include <stddef.h>
62 1.14 itojun #include <ctype.h>
63 1.1 itojun #include <unistd.h>
64 1.14 itojun #include <stdio.h>
65 1.14 itojun #include <errno.h>
66 1.14 itojun
67 1.1 itojun #define SUCCESS 0
68 1.1 itojun #define ANY 0
69 1.1 itojun #define YES 1
70 1.1 itojun #define NO 0
71 1.1 itojun
72 1.1 itojun #ifdef FAITH
73 1.1 itojun static int translate = NO;
74 1.1 itojun static struct in6_addr faith_prefix = IN6ADDR_ANY_INIT;
75 1.1 itojun #endif
76 1.1 itojun
77 1.1 itojun static const char in_addrany[] = { 0, 0, 0, 0 };
78 1.1 itojun static const char in6_addrany[] = {
79 1.1 itojun 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
80 1.1 itojun };
81 1.1 itojun static const char in_loopback[] = { 127, 0, 0, 1 };
82 1.1 itojun static const char in6_loopback[] = {
83 1.1 itojun 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1
84 1.1 itojun };
85 1.1 itojun
86 1.1 itojun struct sockinet {
87 1.1 itojun u_char si_len;
88 1.1 itojun u_char si_family;
89 1.1 itojun u_short si_port;
90 1.14 itojun u_int32_t si_scope_id;
91 1.1 itojun };
92 1.1 itojun
93 1.14 itojun static const struct afd {
94 1.1 itojun int a_af;
95 1.1 itojun int a_addrlen;
96 1.1 itojun int a_socklen;
97 1.1 itojun int a_off;
98 1.1 itojun const char *a_addrany;
99 1.1 itojun const char *a_loopback;
100 1.14 itojun int a_scoped;
101 1.1 itojun } afdl [] = {
102 1.1 itojun #ifdef INET6
103 1.1 itojun {PF_INET6, sizeof(struct in6_addr),
104 1.1 itojun sizeof(struct sockaddr_in6),
105 1.1 itojun offsetof(struct sockaddr_in6, sin6_addr),
106 1.14 itojun in6_addrany, in6_loopback, 1},
107 1.1 itojun #endif
108 1.1 itojun {PF_INET, sizeof(struct in_addr),
109 1.1 itojun sizeof(struct sockaddr_in),
110 1.1 itojun offsetof(struct sockaddr_in, sin_addr),
111 1.14 itojun in_addrany, in_loopback, 0},
112 1.14 itojun {0, 0, 0, 0, NULL, NULL, 0},
113 1.14 itojun };
114 1.14 itojun
115 1.14 itojun struct explore {
116 1.14 itojun int e_af;
117 1.14 itojun int e_socktype;
118 1.14 itojun int e_protocol;
119 1.14 itojun const char *e_protostr;
120 1.14 itojun int e_wild;
121 1.14 itojun #define WILD_AF(ex) ((ex)->e_wild & 0x01)
122 1.14 itojun #define WILD_SOCKTYPE(ex) ((ex)->e_wild & 0x02)
123 1.14 itojun #define WILD_PROTOCOL(ex) ((ex)->e_wild & 0x04)
124 1.14 itojun };
125 1.14 itojun
126 1.14 itojun static const struct explore explore[] = {
127 1.14 itojun #if 0
128 1.14 itojun { PF_LOCAL, 0, ANY, ANY, NULL, 0x01 },
129 1.14 itojun #endif
130 1.14 itojun #ifdef INET6
131 1.14 itojun { PF_INET6, SOCK_DGRAM, IPPROTO_UDP, "udp", 0x07 },
132 1.14 itojun { PF_INET6, SOCK_STREAM, IPPROTO_TCP, "tcp", 0x07 },
133 1.14 itojun { PF_INET6, SOCK_RAW, ANY, NULL, 0x05 },
134 1.14 itojun #endif
135 1.14 itojun { PF_INET, SOCK_DGRAM, IPPROTO_UDP, "udp", 0x07 },
136 1.14 itojun { PF_INET, SOCK_STREAM, IPPROTO_TCP, "tcp", 0x07 },
137 1.14 itojun { PF_INET, SOCK_RAW, ANY, NULL, 0x05 },
138 1.14 itojun { -1, 0, 0, NULL, 0 },
139 1.1 itojun };
140 1.1 itojun
141 1.1 itojun #ifdef INET6
142 1.1 itojun #define PTON_MAX 16
143 1.1 itojun #else
144 1.1 itojun #define PTON_MAX 4
145 1.1 itojun #endif
146 1.1 itojun
147 1.1 itojun
148 1.1 itojun static int str_isnumber __P((const char *));
149 1.14 itojun static int explore_fqdn __P((const struct addrinfo *, const char *,
150 1.14 itojun const char *, struct addrinfo **));
151 1.14 itojun static int explore_null __P((const struct addrinfo *, const char *,
152 1.14 itojun const char *, struct addrinfo **));
153 1.14 itojun static int explore_numeric __P((const struct addrinfo *, const char *,
154 1.14 itojun const char *, struct addrinfo **));
155 1.14 itojun static int explore_numeric_scope __P((const struct addrinfo *, const char *,
156 1.14 itojun const char *, struct addrinfo **));
157 1.14 itojun static int get_name __P((const char *, const struct afd *, struct addrinfo **,
158 1.14 itojun char *, const struct addrinfo *, const char *));
159 1.14 itojun static int get_canonname __P((const struct addrinfo *,
160 1.14 itojun struct addrinfo *, const char *));
161 1.14 itojun static struct addrinfo *get_ai __P((const struct addrinfo *,
162 1.14 itojun const struct afd *, const char *));
163 1.14 itojun static int get_portmatch __P((const struct addrinfo *, const char *));
164 1.14 itojun static int get_port __P((struct addrinfo *, const char *, int));
165 1.14 itojun static const struct afd *find_afd __P((int));
166 1.14 itojun
167 1.1 itojun static char *ai_errlist[] = {
168 1.7 lukem "Success",
169 1.7 lukem "Address family for hostname not supported", /* EAI_ADDRFAMILY */
170 1.7 lukem "Temporary failure in name resolution", /* EAI_AGAIN */
171 1.7 lukem "Invalid value for ai_flags", /* EAI_BADFLAGS */
172 1.7 lukem "Non-recoverable failure in name resolution", /* EAI_FAIL */
173 1.2 lukem "ai_family not supported", /* EAI_FAMILY */
174 1.7 lukem "Memory allocation failure", /* EAI_MEMORY */
175 1.7 lukem "No address associated with hostname", /* EAI_NODATA */
176 1.14 itojun "hostname nor servname provided, or not known", /* EAI_NONAME */
177 1.2 lukem "servname not supported for ai_socktype", /* EAI_SERVICE */
178 1.2 lukem "ai_socktype not supported", /* EAI_SOCKTYPE */
179 1.7 lukem "System error returned in errno", /* EAI_SYSTEM */
180 1.7 lukem "Invalid value for hints", /* EAI_BADHINTS */
181 1.7 lukem "Resolved protocol is unknown", /* EAI_PROTOCOL */
182 1.7 lukem "Unknown error", /* EAI_MAX */
183 1.1 itojun };
184 1.1 itojun
185 1.14 itojun /* XXX macros that make external reference is BAD. */
186 1.14 itojun
187 1.14 itojun #define GET_AI(ai, afd, addr) \
188 1.14 itojun do { \
189 1.14 itojun /* external reference: pai, error, and label free */ \
190 1.14 itojun (ai) = get_ai(pai, (afd), (addr)); \
191 1.14 itojun if ((ai) == NULL) { \
192 1.14 itojun error = EAI_MEMORY; \
193 1.14 itojun goto free; \
194 1.14 itojun } \
195 1.14 itojun } while (0)
196 1.14 itojun
197 1.14 itojun #define GET_PORT(ai, serv) \
198 1.14 itojun do { \
199 1.14 itojun /* external reference: error and label free */ \
200 1.14 itojun error = get_port((ai), (serv), 0); \
201 1.14 itojun if (error != 0) \
202 1.14 itojun goto free; \
203 1.14 itojun } while (0)
204 1.14 itojun
205 1.1 itojun #define GET_CANONNAME(ai, str) \
206 1.14 itojun do { \
207 1.14 itojun /* external reference: pai, error and label free */ \
208 1.14 itojun error = get_canonname(pai, (ai), (str)); \
209 1.14 itojun if (error != 0) \
210 1.14 itojun goto free; \
211 1.14 itojun } while (0)
212 1.14 itojun
213 1.14 itojun #define ERR(err) \
214 1.14 itojun do { \
215 1.14 itojun /* external reference: error, and label bad */ \
216 1.14 itojun error = (err); \
217 1.14 itojun goto bad; \
218 1.14 itojun } while (0)
219 1.14 itojun
220 1.14 itojun #define MATCH_FAMILY(x, y, w) \
221 1.14 itojun ((x) == (y) || ((w) && ((x) == PF_UNSPEC || (y) == PF_UNSPEC)))
222 1.14 itojun #define MATCH(x, y, w) \
223 1.14 itojun ((x) == (y) || ((w) && ((x) == ANY || (y) == ANY)))
224 1.1 itojun
225 1.1 itojun char *
226 1.1 itojun gai_strerror(ecode)
227 1.1 itojun int ecode;
228 1.1 itojun {
229 1.1 itojun if (ecode < 0 || ecode > EAI_MAX)
230 1.1 itojun ecode = EAI_MAX;
231 1.1 itojun return ai_errlist[ecode];
232 1.1 itojun }
233 1.1 itojun
234 1.1 itojun void
235 1.1 itojun freeaddrinfo(ai)
236 1.1 itojun struct addrinfo *ai;
237 1.1 itojun {
238 1.1 itojun struct addrinfo *next;
239 1.1 itojun
240 1.1 itojun do {
241 1.1 itojun next = ai->ai_next;
242 1.1 itojun if (ai->ai_canonname)
243 1.1 itojun free(ai->ai_canonname);
244 1.1 itojun /* no need to free(ai->ai_addr) */
245 1.1 itojun free(ai);
246 1.1 itojun } while ((ai = next) != NULL);
247 1.1 itojun }
248 1.1 itojun
249 1.1 itojun static int
250 1.1 itojun str_isnumber(p)
251 1.1 itojun const char *p;
252 1.1 itojun {
253 1.1 itojun char *q = (char *)p;
254 1.1 itojun while (*q) {
255 1.1 itojun if (! isdigit(*q))
256 1.1 itojun return NO;
257 1.1 itojun q++;
258 1.1 itojun }
259 1.1 itojun return YES;
260 1.1 itojun }
261 1.1 itojun
262 1.1 itojun int
263 1.1 itojun getaddrinfo(hostname, servname, hints, res)
264 1.1 itojun const char *hostname, *servname;
265 1.1 itojun const struct addrinfo *hints;
266 1.1 itojun struct addrinfo **res;
267 1.1 itojun {
268 1.1 itojun struct addrinfo sentinel;
269 1.1 itojun struct addrinfo *cur;
270 1.14 itojun int error = 0;
271 1.1 itojun struct addrinfo ai;
272 1.14 itojun struct addrinfo ai0;
273 1.1 itojun struct addrinfo *pai;
274 1.14 itojun const struct afd *afd;
275 1.14 itojun const struct explore *ex;
276 1.1 itojun
277 1.1 itojun #ifdef FAITH
278 1.1 itojun static int firsttime = 1;
279 1.1 itojun
280 1.1 itojun if (firsttime) {
281 1.1 itojun /* translator hack */
282 1.14 itojun char *q = getenv("GAI");
283 1.14 itojun if (q && inet_pton(AF_INET6, q, &faith_prefix) == 1)
284 1.14 itojun translate = YES;
285 1.1 itojun firsttime = 0;
286 1.1 itojun }
287 1.1 itojun #endif
288 1.1 itojun
289 1.1 itojun sentinel.ai_next = NULL;
290 1.1 itojun cur = &sentinel;
291 1.1 itojun pai = &ai;
292 1.1 itojun pai->ai_flags = 0;
293 1.1 itojun pai->ai_family = PF_UNSPEC;
294 1.1 itojun pai->ai_socktype = ANY;
295 1.1 itojun pai->ai_protocol = ANY;
296 1.1 itojun pai->ai_addrlen = 0;
297 1.1 itojun pai->ai_canonname = NULL;
298 1.1 itojun pai->ai_addr = NULL;
299 1.1 itojun pai->ai_next = NULL;
300 1.1 itojun
301 1.1 itojun if (hostname == NULL && servname == NULL)
302 1.1 itojun return EAI_NONAME;
303 1.1 itojun if (hints) {
304 1.1 itojun /* error check for hints */
305 1.1 itojun if (hints->ai_addrlen || hints->ai_canonname ||
306 1.1 itojun hints->ai_addr || hints->ai_next)
307 1.1 itojun ERR(EAI_BADHINTS); /* xxx */
308 1.1 itojun if (hints->ai_flags & ~AI_MASK)
309 1.1 itojun ERR(EAI_BADFLAGS);
310 1.1 itojun switch (hints->ai_family) {
311 1.1 itojun case PF_UNSPEC:
312 1.1 itojun case PF_INET:
313 1.1 itojun #ifdef INET6
314 1.1 itojun case PF_INET6:
315 1.1 itojun #endif
316 1.1 itojun break;
317 1.1 itojun default:
318 1.1 itojun ERR(EAI_FAMILY);
319 1.1 itojun }
320 1.1 itojun memcpy(pai, hints, sizeof(*pai));
321 1.14 itojun
322 1.14 itojun /*
323 1.14 itojun * if both socktype/protocol are specified, check if they
324 1.14 itojun * are meaningful combination.
325 1.14 itojun */
326 1.14 itojun if (pai->ai_socktype != ANY && pai->ai_protocol != ANY) {
327 1.14 itojun for (ex = explore; ex->e_af >= 0; ex++) {
328 1.14 itojun if (pai->ai_family != ex->e_af)
329 1.14 itojun continue;
330 1.14 itojun if (ex->e_socktype == ANY)
331 1.14 itojun continue;
332 1.14 itojun if (ex->e_protocol == ANY)
333 1.14 itojun continue;
334 1.14 itojun if (pai->ai_socktype == ex->e_socktype
335 1.14 itojun && pai->ai_protocol != ex->e_protocol) {
336 1.14 itojun ERR(EAI_BADHINTS);
337 1.14 itojun }
338 1.1 itojun }
339 1.1 itojun }
340 1.1 itojun }
341 1.1 itojun
342 1.1 itojun /*
343 1.14 itojun * check for special cases. (1) numeric servname is disallowed if
344 1.14 itojun * socktype/protocol are left unspecified. (2) servname is disallowed
345 1.14 itojun * for raw and other inet{,6} sockets.
346 1.1 itojun */
347 1.14 itojun if (MATCH_FAMILY(pai->ai_family, PF_INET, 1)
348 1.14 itojun || MATCH_FAMILY(pai->ai_family, PF_INET6, 1)) {
349 1.14 itojun ai0 = *pai;
350 1.14 itojun
351 1.14 itojun if (pai->ai_family == PF_UNSPEC)
352 1.14 itojun pai->ai_family = PF_INET6;
353 1.14 itojun error = get_portmatch(pai, servname);
354 1.14 itojun if (error)
355 1.14 itojun ERR(error);
356 1.14 itojun
357 1.14 itojun *pai = ai0;
358 1.14 itojun }
359 1.14 itojun
360 1.14 itojun ai0 = *pai;
361 1.14 itojun
362 1.14 itojun /* NULL hostname, or numeric hostname */
363 1.14 itojun for (ex = explore; ex->e_af >= 0; ex++) {
364 1.14 itojun *pai = ai0;
365 1.14 itojun
366 1.14 itojun if (!MATCH_FAMILY(pai->ai_family, ex->e_af, WILD_AF(ex)))
367 1.14 itojun continue;
368 1.14 itojun if (!MATCH(pai->ai_socktype, ex->e_socktype, WILD_SOCKTYPE(ex)))
369 1.14 itojun continue;
370 1.14 itojun if (!MATCH(pai->ai_protocol, ex->e_protocol, WILD_PROTOCOL(ex)))
371 1.14 itojun continue;
372 1.14 itojun
373 1.14 itojun if (pai->ai_family == PF_UNSPEC)
374 1.14 itojun pai->ai_family = ex->e_af;
375 1.14 itojun if (pai->ai_socktype == ANY && ex->e_socktype != ANY)
376 1.14 itojun pai->ai_socktype = ex->e_socktype;
377 1.14 itojun if (pai->ai_protocol == ANY && ex->e_protocol != ANY)
378 1.14 itojun pai->ai_protocol = ex->e_protocol;
379 1.14 itojun
380 1.14 itojun if (hostname == NULL)
381 1.14 itojun error = explore_null(pai, hostname, servname, &cur->ai_next);
382 1.14 itojun else
383 1.14 itojun error = explore_numeric_scope(pai, hostname, servname, &cur->ai_next);
384 1.14 itojun
385 1.14 itojun if (error)
386 1.14 itojun goto free;
387 1.14 itojun
388 1.14 itojun while (cur && cur->ai_next)
389 1.14 itojun cur = cur->ai_next;
390 1.14 itojun }
391 1.14 itojun
392 1.14 itojun /*
393 1.14 itojun * XXX
394 1.14 itojun * If numreic representation of AF1 can be interpreted as FQDN
395 1.14 itojun * representation of AF2, we need to think again about the code below.
396 1.14 itojun */
397 1.14 itojun if (sentinel.ai_next)
398 1.14 itojun goto good;
399 1.14 itojun
400 1.14 itojun if (pai->ai_flags & AI_NUMERICHOST)
401 1.14 itojun ERR(EAI_NONAME);
402 1.14 itojun if (hostname == NULL)
403 1.14 itojun ERR(EAI_NONAME);
404 1.14 itojun
405 1.14 itojun /*
406 1.14 itojun * hostname as alphabetical name.
407 1.14 itojun * we would like to prefer AF_INET6 than AF_INET, so we'll make a
408 1.14 itojun * outer loop by AFs.
409 1.14 itojun */
410 1.14 itojun for (afd = afdl; afd->a_af; afd++) {
411 1.14 itojun *pai = ai0;
412 1.14 itojun
413 1.14 itojun if (!MATCH_FAMILY(pai->ai_family, afd->a_af, 1))
414 1.14 itojun continue;
415 1.14 itojun
416 1.14 itojun for (ex = explore; ex->e_af >= 0; ex++) {
417 1.14 itojun *pai = ai0;
418 1.14 itojun
419 1.14 itojun if (pai->ai_family == PF_UNSPEC)
420 1.14 itojun pai->ai_family = afd->a_af;
421 1.1 itojun
422 1.14 itojun if (!MATCH_FAMILY(pai->ai_family, ex->e_af, WILD_AF(ex)))
423 1.14 itojun continue;
424 1.14 itojun if (!MATCH(pai->ai_socktype, ex->e_socktype,
425 1.14 itojun WILD_SOCKTYPE(ex))) {
426 1.14 itojun continue;
427 1.1 itojun }
428 1.14 itojun if (!MATCH(pai->ai_protocol, ex->e_protocol,
429 1.14 itojun WILD_PROTOCOL(ex))) {
430 1.14 itojun continue;
431 1.1 itojun }
432 1.14 itojun
433 1.14 itojun if (pai->ai_family == PF_UNSPEC)
434 1.14 itojun pai->ai_family = ex->e_af;
435 1.14 itojun if (pai->ai_socktype == ANY && ex->e_socktype != ANY)
436 1.14 itojun pai->ai_socktype = ex->e_socktype;
437 1.14 itojun if (pai->ai_protocol == ANY && ex->e_protocol != ANY)
438 1.14 itojun pai->ai_protocol = ex->e_protocol;
439 1.14 itojun
440 1.14 itojun error = explore_fqdn(pai, hostname, servname,
441 1.14 itojun &cur->ai_next);
442 1.14 itojun
443 1.14 itojun while (cur && cur->ai_next)
444 1.14 itojun cur = cur->ai_next;
445 1.1 itojun }
446 1.1 itojun }
447 1.14 itojun
448 1.14 itojun /* XXX */
449 1.14 itojun if (sentinel.ai_next)
450 1.14 itojun error = 0;
451 1.14 itojun
452 1.14 itojun if (error)
453 1.14 itojun goto free;
454 1.14 itojun if (error == 0) {
455 1.14 itojun if (sentinel.ai_next) {
456 1.14 itojun good:
457 1.14 itojun *res = sentinel.ai_next;
458 1.14 itojun return SUCCESS;
459 1.14 itojun } else
460 1.14 itojun error = EAI_FAIL;
461 1.14 itojun }
462 1.14 itojun free:
463 1.14 itojun bad:
464 1.14 itojun if (sentinel.ai_next)
465 1.14 itojun freeaddrinfo(sentinel.ai_next);
466 1.14 itojun *res = NULL;
467 1.14 itojun return error;
468 1.14 itojun }
469 1.14 itojun
470 1.14 itojun /*
471 1.14 itojun * FQDN hostname, DNS lookup
472 1.14 itojun */
473 1.14 itojun static int
474 1.14 itojun explore_fqdn(pai, hostname, servname, res)
475 1.14 itojun const struct addrinfo *pai;
476 1.14 itojun const char *hostname;
477 1.14 itojun const char *servname;
478 1.14 itojun struct addrinfo **res;
479 1.14 itojun {
480 1.14 itojun int s;
481 1.14 itojun struct hostent *hp;
482 1.14 itojun int h_error;
483 1.14 itojun int af;
484 1.16 itojun char **aplist = NULL, *apbuf = NULL;
485 1.16 itojun char *ap;
486 1.14 itojun struct addrinfo sentinel, *cur;
487 1.14 itojun int i;
488 1.16 itojun #ifndef USE_GETIPNODEBY
489 1.16 itojun int naddrs;
490 1.16 itojun #endif
491 1.14 itojun const struct afd *afd;
492 1.14 itojun int error;
493 1.14 itojun
494 1.14 itojun *res = NULL;
495 1.14 itojun sentinel.ai_next = NULL;
496 1.14 itojun cur = &sentinel;
497 1.14 itojun
498 1.14 itojun /*
499 1.14 itojun * filter out AFs that are not supported by the kernel
500 1.14 itojun * XXX errno?
501 1.14 itojun */
502 1.14 itojun s = socket(pai->ai_family, SOCK_DGRAM, 0);
503 1.14 itojun if (s < 0) {
504 1.14 itojun if (errno != EMFILE)
505 1.14 itojun return 0;
506 1.14 itojun } else
507 1.14 itojun close(s);
508 1.14 itojun
509 1.14 itojun /*
510 1.14 itojun * if the servname does not match socktype/protocol, ignore it.
511 1.14 itojun */
512 1.14 itojun if (get_portmatch(pai, servname) != 0)
513 1.14 itojun return 0;
514 1.14 itojun
515 1.14 itojun afd = find_afd(pai->ai_family);
516 1.14 itojun
517 1.1 itojun /*
518 1.14 itojun * post-RFC2553: should look at (pai->ai_flags & AI_ADDRCONFIG)
519 1.14 itojun * rather than hardcoding it. we may need to add AI_ADDRCONFIG
520 1.14 itojun * handling code by ourselves in case we don't have getipnodebyname().
521 1.14 itojun */
522 1.14 itojun #ifdef USE_GETIPNODEBY
523 1.14 itojun hp = getipnodebyname(hostname, pai->ai_family, AI_ADDRCONFIG, &h_error);
524 1.14 itojun #else
525 1.14 itojun hp = gethostbyname2(hostname, pai->ai_family);
526 1.14 itojun h_error = h_errno;
527 1.14 itojun #endif
528 1.14 itojun
529 1.14 itojun if (hp == NULL) {
530 1.14 itojun switch (h_error) {
531 1.14 itojun case HOST_NOT_FOUND:
532 1.14 itojun case NO_DATA:
533 1.14 itojun error = EAI_NODATA;
534 1.14 itojun break;
535 1.14 itojun case TRY_AGAIN:
536 1.14 itojun error = EAI_AGAIN;
537 1.14 itojun break;
538 1.14 itojun case NO_RECOVERY:
539 1.14 itojun case NETDB_INTERNAL:
540 1.14 itojun default:
541 1.14 itojun error = EAI_FAIL;
542 1.14 itojun break;
543 1.14 itojun }
544 1.14 itojun } else if ((hp->h_name == NULL) || (hp->h_name[0] == 0)
545 1.14 itojun || (hp->h_addr_list[0] == NULL)) {
546 1.14 itojun #ifdef USE_GETIPNODEBY
547 1.14 itojun freehostent(hp);
548 1.14 itojun #endif
549 1.14 itojun hp = NULL;
550 1.14 itojun error = EAI_FAIL;
551 1.14 itojun }
552 1.14 itojun
553 1.14 itojun if (hp == NULL)
554 1.14 itojun goto free;
555 1.14 itojun
556 1.16 itojun #ifdef USE_GETIPNODEBY
557 1.16 itojun aplist = hp->h_addr_list;
558 1.16 itojun #else
559 1.16 itojun /*
560 1.16 itojun * hp will be overwritten if we use gethostbyname2().
561 1.16 itojun * always deep copy for simplification.
562 1.16 itojun */
563 1.16 itojun for (naddrs = 0; hp->h_addr_list[naddrs] != NULL; naddrs++)
564 1.16 itojun ;
565 1.16 itojun naddrs++;
566 1.16 itojun aplist = (char **)malloc(sizeof(aplist[0]) * naddrs);
567 1.16 itojun apbuf = (char *)malloc(hp->h_length * naddrs);
568 1.16 itojun if (aplist == NULL || apbuf == NULL) {
569 1.15 itojun error = EAI_MEMORY;
570 1.15 itojun goto free;
571 1.15 itojun }
572 1.16 itojun memset(aplist, 0, sizeof(aplist[0]) * naddrs);
573 1.16 itojun for (i = 0; i < naddrs; i++) {
574 1.16 itojun if (hp->h_addr_list[i] == NULL) {
575 1.16 itojun aplist[i] = NULL;
576 1.16 itojun continue;
577 1.16 itojun }
578 1.16 itojun memcpy(&apbuf[i * hp->h_length], hp->h_addr_list[i],
579 1.16 itojun hp->h_length);
580 1.16 itojun aplist[i] = &apbuf[i * hp->h_length];
581 1.16 itojun }
582 1.16 itojun #endif
583 1.15 itojun
584 1.16 itojun for (i = 0; aplist[i] != NULL; i++) {
585 1.14 itojun af = hp->h_addrtype;
586 1.16 itojun ap = aplist[i];
587 1.14 itojun if (af == AF_INET6
588 1.14 itojun && IN6_IS_ADDR_V4MAPPED((struct in6_addr *)ap)) {
589 1.14 itojun af = AF_INET;
590 1.14 itojun ap = ap + sizeof(struct in6_addr)
591 1.14 itojun - sizeof(struct in_addr);
592 1.14 itojun }
593 1.14 itojun
594 1.14 itojun if (af != pai->ai_family)
595 1.14 itojun continue;
596 1.1 itojun
597 1.14 itojun if ((pai->ai_flags & AI_CANONNAME) == 0) {
598 1.14 itojun GET_AI(cur->ai_next, afd, ap);
599 1.14 itojun GET_PORT(cur->ai_next, servname);
600 1.14 itojun } else {
601 1.1 itojun /*
602 1.14 itojun * if AI_CANONNAME and if reverse lookup
603 1.14 itojun * fail, return ai anyway to pacify
604 1.14 itojun * calling application.
605 1.14 itojun *
606 1.14 itojun * XXX getaddrinfo() is a name->address
607 1.14 itojun * translation function, and it looks
608 1.14 itojun * strange that we do addr->name
609 1.14 itojun * translation here.
610 1.1 itojun */
611 1.14 itojun get_name(ap, afd, &cur->ai_next,
612 1.14 itojun ap, pai, servname);
613 1.14 itojun }
614 1.1 itojun
615 1.14 itojun while (cur && cur->ai_next)
616 1.1 itojun cur = cur->ai_next;
617 1.1 itojun }
618 1.14 itojun
619 1.14 itojun *res = sentinel.ai_next;
620 1.14 itojun return 0;
621 1.14 itojun
622 1.14 itojun free:
623 1.14 itojun #ifdef USE_GETIPNODEBY
624 1.14 itojun if (hp)
625 1.14 itojun freehostent(hp);
626 1.14 itojun #endif
627 1.16 itojun if (aplist)
628 1.16 itojun free(aplist);
629 1.16 itojun if (apbuf)
630 1.16 itojun free(apbuf);
631 1.14 itojun if (sentinel.ai_next)
632 1.14 itojun freeaddrinfo(sentinel.ai_next);
633 1.14 itojun return error;
634 1.14 itojun }
635 1.14 itojun
636 1.14 itojun /*
637 1.14 itojun * hostname == NULL.
638 1.14 itojun * passive socket -> anyaddr (0.0.0.0 or ::)
639 1.14 itojun * non-passive socket -> localhost (127.0.0.1 or ::1)
640 1.14 itojun */
641 1.14 itojun static int
642 1.14 itojun explore_null(pai, hostname, servname, res)
643 1.14 itojun const struct addrinfo *pai;
644 1.14 itojun const char *hostname;
645 1.14 itojun const char *servname;
646 1.14 itojun struct addrinfo **res;
647 1.14 itojun {
648 1.14 itojun int s;
649 1.14 itojun const struct afd *afd;
650 1.14 itojun struct addrinfo *cur;
651 1.14 itojun struct addrinfo sentinel;
652 1.14 itojun int error;
653 1.14 itojun
654 1.14 itojun *res = NULL;
655 1.14 itojun sentinel.ai_next = NULL;
656 1.14 itojun cur = &sentinel;
657 1.14 itojun
658 1.14 itojun /*
659 1.14 itojun * filter out AFs that are not supported by the kernel
660 1.14 itojun * XXX errno?
661 1.14 itojun */
662 1.14 itojun s = socket(pai->ai_family, SOCK_DGRAM, 0);
663 1.14 itojun if (s < 0) {
664 1.14 itojun if (errno != EMFILE)
665 1.14 itojun return 0;
666 1.14 itojun } else
667 1.14 itojun close(s);
668 1.14 itojun
669 1.14 itojun /*
670 1.14 itojun * if the servname does not match socktype/protocol, ignore it.
671 1.14 itojun */
672 1.14 itojun if (get_portmatch(pai, servname) != 0)
673 1.14 itojun return 0;
674 1.14 itojun
675 1.14 itojun afd = find_afd(pai->ai_family);
676 1.14 itojun
677 1.14 itojun if (pai->ai_flags & AI_PASSIVE) {
678 1.14 itojun GET_AI(cur->ai_next, afd, afd->a_addrany);
679 1.14 itojun /* xxx meaningless?
680 1.14 itojun * GET_CANONNAME(cur->ai_next, "anyaddr");
681 1.14 itojun */
682 1.14 itojun GET_PORT(cur->ai_next, servname);
683 1.14 itojun } else {
684 1.14 itojun GET_AI(cur->ai_next, afd, afd->a_loopback);
685 1.14 itojun /* xxx meaningless?
686 1.14 itojun * GET_CANONNAME(cur->ai_next, "localhost");
687 1.14 itojun */
688 1.14 itojun GET_PORT(cur->ai_next, servname);
689 1.14 itojun }
690 1.14 itojun cur = cur->ai_next;
691 1.14 itojun
692 1.14 itojun *res = sentinel.ai_next;
693 1.14 itojun return 0;
694 1.14 itojun
695 1.14 itojun free:
696 1.14 itojun if (sentinel.ai_next)
697 1.14 itojun freeaddrinfo(sentinel.ai_next);
698 1.14 itojun return error;
699 1.14 itojun }
700 1.14 itojun
701 1.14 itojun /*
702 1.14 itojun * numeric hostname
703 1.14 itojun */
704 1.14 itojun static int
705 1.14 itojun explore_numeric(pai, hostname, servname, res)
706 1.14 itojun const struct addrinfo *pai;
707 1.14 itojun const char *hostname;
708 1.14 itojun const char *servname;
709 1.14 itojun struct addrinfo **res;
710 1.14 itojun {
711 1.14 itojun const struct afd *afd;
712 1.14 itojun struct addrinfo *cur;
713 1.14 itojun struct addrinfo sentinel;
714 1.14 itojun int error;
715 1.14 itojun char pton[PTON_MAX];
716 1.14 itojun int flags;
717 1.14 itojun
718 1.14 itojun *res = NULL;
719 1.14 itojun sentinel.ai_next = NULL;
720 1.14 itojun cur = &sentinel;
721 1.14 itojun
722 1.14 itojun /*
723 1.14 itojun * if the servname does not match socktype/protocol, ignore it.
724 1.14 itojun */
725 1.14 itojun if (get_portmatch(pai, servname) != 0)
726 1.14 itojun return 0;
727 1.14 itojun
728 1.14 itojun afd = find_afd(pai->ai_family);
729 1.14 itojun flags = pai->ai_flags;
730 1.14 itojun
731 1.14 itojun if (inet_pton(afd->a_af, hostname, pton) == 1) {
732 1.14 itojun u_int32_t v4a;
733 1.14 itojun #ifdef INET6
734 1.14 itojun u_char pfx;
735 1.14 itojun #endif
736 1.14 itojun
737 1.14 itojun switch (afd->a_af) {
738 1.14 itojun case AF_INET:
739 1.14 itojun v4a = (u_int32_t)ntohl(((struct in_addr *)pton)->s_addr);
740 1.14 itojun if (IN_MULTICAST(v4a) || IN_EXPERIMENTAL(v4a))
741 1.14 itojun flags &= ~AI_CANONNAME;
742 1.14 itojun v4a >>= IN_CLASSA_NSHIFT;
743 1.14 itojun if (v4a == 0 || v4a == IN_LOOPBACKNET)
744 1.14 itojun flags &= ~AI_CANONNAME;
745 1.14 itojun break;
746 1.1 itojun #ifdef INET6
747 1.14 itojun case AF_INET6:
748 1.14 itojun pfx = ((struct in6_addr *)pton)->s6_addr[0];
749 1.14 itojun if (pfx == 0 || pfx == 0xfe || pfx == 0xff)
750 1.14 itojun flags &= ~AI_CANONNAME;
751 1.14 itojun break;
752 1.1 itojun #endif
753 1.14 itojun }
754 1.14 itojun
755 1.14 itojun if (pai->ai_family == afd->a_af ||
756 1.14 itojun pai->ai_family == PF_UNSPEC /*?*/) {
757 1.14 itojun if ((flags & AI_CANONNAME) == 0) {
758 1.14 itojun GET_AI(cur->ai_next, afd, pton);
759 1.14 itojun GET_PORT(cur->ai_next, servname);
760 1.14 itojun } else {
761 1.1 itojun /*
762 1.1 itojun * if AI_CANONNAME and if reverse lookup
763 1.1 itojun * fail, return ai anyway to pacify
764 1.1 itojun * calling application.
765 1.1 itojun *
766 1.1 itojun * XXX getaddrinfo() is a name->address
767 1.14 itojun * translation function, and it looks
768 1.14 itojun * strange that we do addr->name
769 1.14 itojun * translation here.
770 1.1 itojun */
771 1.14 itojun get_name(pton, afd, &cur->ai_next,
772 1.14 itojun pton, pai, servname);
773 1.14 itojun }
774 1.14 itojun while (cur && cur->ai_next)
775 1.14 itojun cur = cur->ai_next;
776 1.14 itojun } else
777 1.14 itojun ERR(EAI_FAMILY); /*xxx*/
778 1.1 itojun }
779 1.1 itojun
780 1.14 itojun *res = sentinel.ai_next;
781 1.14 itojun return 0;
782 1.14 itojun
783 1.14 itojun free:
784 1.14 itojun bad:
785 1.14 itojun if (sentinel.ai_next)
786 1.14 itojun freeaddrinfo(sentinel.ai_next);
787 1.14 itojun return error;
788 1.14 itojun }
789 1.14 itojun
790 1.14 itojun /*
791 1.14 itojun * numeric hostname with scope
792 1.14 itojun */
793 1.14 itojun static int
794 1.14 itojun explore_numeric_scope(pai, hostname, servname, res)
795 1.14 itojun const struct addrinfo *pai;
796 1.14 itojun const char *hostname;
797 1.14 itojun const char *servname;
798 1.14 itojun struct addrinfo **res;
799 1.14 itojun {
800 1.14 itojun #ifndef SCOPE_DELIMITER
801 1.14 itojun return explore_numeric(pai, hostname, servname, res);
802 1.14 itojun #else
803 1.14 itojun const struct afd *afd;
804 1.14 itojun struct addrinfo *cur;
805 1.14 itojun int error;
806 1.14 itojun char *cp, *hostname2 = NULL;
807 1.14 itojun int scope;
808 1.14 itojun struct sockaddr_in6 *sin6;
809 1.14 itojun
810 1.14 itojun /*
811 1.14 itojun * if the servname does not match socktype/protocol, ignore it.
812 1.14 itojun */
813 1.14 itojun if (get_portmatch(pai, servname) != 0)
814 1.14 itojun return 0;
815 1.14 itojun
816 1.14 itojun afd = find_afd(pai->ai_family);
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.14 itojun /*
825 1.14 itojun * Handle special case of <scoped_address><delimiter><scope id>
826 1.14 itojun */
827 1.14 itojun hostname2 = strdup(hostname);
828 1.14 itojun if (hostname2 == NULL)
829 1.14 itojun return EAI_MEMORY;
830 1.14 itojun /* terminate at the delimiter */
831 1.14 itojun hostname2[cp - hostname] = '\0';
832 1.14 itojun
833 1.14 itojun cp++;
834 1.14 itojun switch (pai->ai_family) {
835 1.14 itojun #ifdef INET6
836 1.14 itojun case AF_INET6:
837 1.14 itojun scope = if_nametoindex(cp);
838 1.17 itojun if (scope == 0) {
839 1.17 itojun free(hostname2);
840 1.17 itojun return (EAI_NONAME);
841 1.17 itojun }
842 1.14 itojun break;
843 1.14 itojun #endif
844 1.14 itojun }
845 1.1 itojun
846 1.14 itojun error = explore_numeric(pai, hostname2, servname, res);
847 1.1 itojun if (error == 0) {
848 1.14 itojun for (cur = *res; cur; cur = cur->ai_next) {
849 1.14 itojun if (cur->ai_family != AF_INET6)
850 1.14 itojun continue;
851 1.14 itojun sin6 = (struct sockaddr_in6 *)cur->ai_addr;
852 1.14 itojun if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr) ||
853 1.14 itojun IN6_IS_ADDR_MC_LINKLOCAL(&sin6->sin6_addr))
854 1.14 itojun sin6->sin6_scope_id = scope;
855 1.14 itojun }
856 1.1 itojun }
857 1.14 itojun
858 1.14 itojun free(hostname2);
859 1.14 itojun
860 1.1 itojun return error;
861 1.14 itojun #endif
862 1.1 itojun }
863 1.1 itojun
864 1.1 itojun static int
865 1.14 itojun get_name(addr, afd, res, numaddr, pai, servname)
866 1.1 itojun const char *addr;
867 1.14 itojun const struct afd *afd;
868 1.1 itojun struct addrinfo **res;
869 1.1 itojun char *numaddr;
870 1.14 itojun const struct addrinfo *pai;
871 1.14 itojun const char *servname;
872 1.1 itojun {
873 1.15 itojun struct hostent *hp = NULL;
874 1.15 itojun struct addrinfo *cur = NULL;
875 1.1 itojun int error = 0;
876 1.15 itojun char *ap = NULL, *cn = NULL;
877 1.1 itojun #ifdef USE_GETIPNODEBY
878 1.1 itojun int h_error;
879 1.12 lukem
880 1.1 itojun hp = getipnodebyaddr(addr, afd->a_addrlen, afd->a_af, &h_error);
881 1.1 itojun #else
882 1.1 itojun hp = gethostbyaddr(addr, afd->a_addrlen, afd->a_af);
883 1.1 itojun #endif
884 1.1 itojun if (hp && hp->h_name && hp->h_name[0] && hp->h_addr_list[0]) {
885 1.15 itojun #ifdef USE_GETIPNODEBY
886 1.14 itojun GET_AI(cur, afd, hp->h_addr_list[0]);
887 1.14 itojun GET_PORT(cur, servname);
888 1.1 itojun GET_CANONNAME(cur, hp->h_name);
889 1.15 itojun #else
890 1.15 itojun /* hp will be damaged if we use gethostbyaddr() */
891 1.15 itojun if ((ap = (char *)malloc(hp->h_length)) == NULL) {
892 1.15 itojun error = EAI_MEMORY;
893 1.15 itojun goto free;
894 1.15 itojun }
895 1.15 itojun memcpy(ap, hp->h_addr_list[0], hp->h_length);
896 1.15 itojun if ((cn = strdup(hp->h_name)) == NULL) {
897 1.15 itojun error = EAI_MEMORY;
898 1.15 itojun goto free;
899 1.15 itojun }
900 1.15 itojun
901 1.15 itojun GET_AI(cur, afd, ap);
902 1.15 itojun GET_PORT(cur, servname);
903 1.15 itojun GET_CANONNAME(cur, cn);
904 1.15 itojun free(ap); ap = NULL;
905 1.15 itojun free(cn); cn = NULL;
906 1.15 itojun #endif
907 1.14 itojun } else {
908 1.14 itojun GET_AI(cur, afd, numaddr);
909 1.14 itojun GET_PORT(cur, servname);
910 1.14 itojun }
911 1.1 itojun
912 1.1 itojun #ifdef USE_GETIPNODEBY
913 1.1 itojun if (hp)
914 1.1 itojun freehostent(hp);
915 1.1 itojun #endif
916 1.1 itojun *res = cur;
917 1.1 itojun return SUCCESS;
918 1.1 itojun free:
919 1.1 itojun if (cur)
920 1.1 itojun freeaddrinfo(cur);
921 1.15 itojun if (ap)
922 1.15 itojun free(ap);
923 1.15 itojun if (cn)
924 1.15 itojun free(cn);
925 1.1 itojun #ifdef USE_GETIPNODEBY
926 1.1 itojun if (hp)
927 1.1 itojun freehostent(hp);
928 1.1 itojun #endif
929 1.1 itojun *res = NULL;
930 1.1 itojun return error;
931 1.1 itojun }
932 1.1 itojun
933 1.1 itojun static int
934 1.14 itojun get_canonname(pai, ai, str)
935 1.14 itojun const struct addrinfo *pai;
936 1.14 itojun struct addrinfo *ai;
937 1.14 itojun const char *str;
938 1.1 itojun {
939 1.14 itojun if ((pai->ai_flags & AI_CANONNAME) != 0) {
940 1.14 itojun ai->ai_canonname = (char *)malloc(strlen(str) + 1);
941 1.14 itojun if (ai->ai_canonname == NULL)
942 1.14 itojun return EAI_MEMORY;
943 1.14 itojun strcpy(ai->ai_canonname, str);
944 1.14 itojun }
945 1.14 itojun return 0;
946 1.14 itojun }
947 1.1 itojun
948 1.14 itojun static struct addrinfo *
949 1.14 itojun get_ai(pai, afd, addr)
950 1.14 itojun const struct addrinfo *pai;
951 1.14 itojun const struct afd *afd;
952 1.14 itojun const char *addr;
953 1.14 itojun {
954 1.14 itojun char *p;
955 1.14 itojun struct addrinfo *ai;
956 1.12 lukem
957 1.14 itojun ai = (struct addrinfo *)malloc(sizeof(struct addrinfo)
958 1.14 itojun + (afd->a_socklen));
959 1.14 itojun if (ai == NULL)
960 1.14 itojun return NULL;
961 1.14 itojun
962 1.14 itojun memcpy(ai, pai, sizeof(struct addrinfo));
963 1.14 itojun ai->ai_addr = (struct sockaddr *)(ai + 1);
964 1.14 itojun memset(ai->ai_addr, 0, afd->a_socklen);
965 1.14 itojun ai->ai_addr->sa_len = afd->a_socklen;
966 1.14 itojun ai->ai_addrlen = afd->a_socklen;
967 1.14 itojun ai->ai_addr->sa_family = ai->ai_family = afd->a_af;
968 1.14 itojun p = (char *)(ai->ai_addr);
969 1.14 itojun memcpy(p + afd->a_off, addr, afd->a_addrlen);
970 1.14 itojun return ai;
971 1.14 itojun }
972 1.1 itojun
973 1.14 itojun static int
974 1.14 itojun get_portmatch(ai, servname)
975 1.14 itojun const struct addrinfo *ai;
976 1.14 itojun const char *servname;
977 1.14 itojun {
978 1.4 itojun
979 1.14 itojun /* get_port does not touch first argument. when matchonly == 1. */
980 1.14 itojun return get_port((struct addrinfo *)ai, servname, 1);
981 1.1 itojun }
982 1.1 itojun
983 1.1 itojun static int
984 1.14 itojun get_port(ai, servname, matchonly)
985 1.14 itojun struct addrinfo *ai;
986 1.14 itojun const char *servname;
987 1.14 itojun int matchonly;
988 1.1 itojun {
989 1.14 itojun const char *proto;
990 1.14 itojun struct servent *sp;
991 1.14 itojun int port;
992 1.14 itojun int allownumeric;
993 1.12 lukem
994 1.14 itojun if (servname == NULL)
995 1.14 itojun return 0;
996 1.14 itojun if (ai->ai_family != AF_INET && ai->ai_family != AF_INET6)
997 1.14 itojun return 0;
998 1.1 itojun
999 1.14 itojun switch (ai->ai_socktype) {
1000 1.14 itojun case SOCK_RAW:
1001 1.14 itojun return EAI_SERVICE;
1002 1.14 itojun case SOCK_DGRAM:
1003 1.14 itojun case SOCK_STREAM:
1004 1.14 itojun allownumeric = 1;
1005 1.14 itojun break;
1006 1.14 itojun case ANY:
1007 1.14 itojun allownumeric = 0;
1008 1.14 itojun break;
1009 1.14 itojun default:
1010 1.14 itojun return EAI_SOCKTYPE;
1011 1.1 itojun }
1012 1.14 itojun
1013 1.14 itojun if (str_isnumber(servname)) {
1014 1.14 itojun if (!allownumeric)
1015 1.14 itojun return EAI_SERVICE;
1016 1.14 itojun port = htons(atoi(servname));
1017 1.14 itojun if (port < 0 || port > 65535)
1018 1.14 itojun return EAI_SERVICE;
1019 1.14 itojun } else {
1020 1.14 itojun switch (ai->ai_socktype) {
1021 1.14 itojun case SOCK_DGRAM:
1022 1.14 itojun proto = "udp";
1023 1.1 itojun break;
1024 1.14 itojun case SOCK_STREAM:
1025 1.14 itojun proto = "tcp";
1026 1.1 itojun break;
1027 1.1 itojun default:
1028 1.14 itojun proto = NULL;
1029 1.1 itojun break;
1030 1.1 itojun }
1031 1.14 itojun
1032 1.14 itojun if ((sp = getservbyname(servname, proto)) == NULL)
1033 1.14 itojun return EAI_SERVICE;
1034 1.14 itojun port = sp->s_port;
1035 1.1 itojun }
1036 1.1 itojun
1037 1.14 itojun if (!matchonly) {
1038 1.14 itojun switch (ai->ai_family) {
1039 1.1 itojun case AF_INET:
1040 1.14 itojun ((struct sockaddr_in *)ai->ai_addr)->sin_port = port;
1041 1.1 itojun break;
1042 1.1 itojun #ifdef INET6
1043 1.14 itojun case AF_INET6:
1044 1.14 itojun ((struct sockaddr_in6 *)ai->ai_addr)->sin6_port = port;
1045 1.1 itojun break;
1046 1.1 itojun #endif
1047 1.1 itojun }
1048 1.14 itojun }
1049 1.14 itojun
1050 1.14 itojun return 0;
1051 1.14 itojun }
1052 1.14 itojun
1053 1.14 itojun static const struct afd *
1054 1.14 itojun find_afd(af)
1055 1.14 itojun int af;
1056 1.14 itojun {
1057 1.14 itojun const struct afd *afd;
1058 1.1 itojun
1059 1.14 itojun if (af == PF_UNSPEC)
1060 1.14 itojun return NULL;
1061 1.14 itojun for (afd = afdl; afd->a_af; afd++) {
1062 1.14 itojun if (afd->a_af == af)
1063 1.14 itojun return afd;
1064 1.1 itojun }
1065 1.14 itojun return NULL;
1066 1.1 itojun }
1067