1 /*
2 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of the project nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 */
29
30 #include <ratbox_lib.h>
31 #include "getaddrinfo.h"
32
33 /* $Id: getaddrinfo.c 26142 2008-10-07 18:34:35Z androsyn $ */
34
35 static const char in_addrany[] = { 0, 0, 0, 0 };
36 static const char in_loopback[] = { 127, 0, 0, 1 };
37 static const char in6_addrany[] = {
38 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
39 };
40 static const char in6_loopback[] = {
41 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1
42 };
43
44 static const struct afd {
45 int a_af;
46 int a_addrlen;
47 int a_socklen;
48 int a_off;
49 const char *a_addrany;
50 const char *a_loopback;
51 int a_scoped;
52 } afdl [] = {
53 #define N_INET6 0
54 #ifdef IPV6
55 {PF_INET6, sizeof(struct in6_addr),
56 sizeof(struct sockaddr_in6),
57 offsetof(struct sockaddr_in6, sin6_addr),
58 in6_addrany, in6_loopback, 1},
59 #endif
60 #define N_INET 1
61 {PF_INET, sizeof(struct in_addr),
62 sizeof(struct sockaddr_in),
63 offsetof(struct sockaddr_in, sin_addr),
64 in_addrany, in_loopback, 0},
65 {0, 0, 0, 0, NULL, NULL, 0},
66 };
67
68 struct explore {
69 int e_af;
70 int e_socktype;
71 int e_protocol;
72 const char *e_protostr;
73 int e_wild;
74 #define WILD_AF(ex) ((ex)->e_wild & 0x01)
75 #define WILD_SOCKTYPE(ex) ((ex)->e_wild & 0x02)
76 #define WILD_PROTOCOL(ex) ((ex)->e_wild & 0x04)
77 };
78
79 static const struct explore explore[] = {
80 #ifdef IPV6
81 { PF_INET6, SOCK_DGRAM, IPPROTO_UDP, "udp", 0x07 },
82 { PF_INET6, SOCK_STREAM, IPPROTO_TCP, "tcp", 0x07 },
83 { PF_INET6, SOCK_RAW, ANY, NULL, 0x05 },
84 #endif
85 { PF_INET, SOCK_DGRAM, IPPROTO_UDP, "udp", 0x07 },
86 { PF_INET, SOCK_STREAM, IPPROTO_TCP, "tcp", 0x07 },
87 { PF_INET, SOCK_RAW, ANY, NULL, 0x05 },
88 { PF_UNSPEC, SOCK_DGRAM, IPPROTO_UDP, "udp", 0x07 },
89 { PF_UNSPEC, SOCK_STREAM, IPPROTO_TCP, "tcp", 0x07 },
90 { PF_UNSPEC, SOCK_RAW, ANY, NULL, 0x05 },
91 { -1, 0, 0, NULL, 0 },
92 };
93
94 #define PTON_MAX 16
95
96 static int str_isnumber(const char *);
97 static int explore_null(const struct addrinfo *,
98 const char *, struct addrinfo **);
99 static int explore_numeric(const struct addrinfo *, const char *,
100 const char *, struct addrinfo **);
101 static struct addrinfo *get_ai(const struct addrinfo *,
102 const struct afd *, const char *);
103 static int get_portmatch(const struct addrinfo *, const char *);
104 static int get_port(struct addrinfo *, const char *, int);
105 static const struct afd *find_afd(int);
106 #if 0
107 /* We will need this should we ever want gai_strerror() */
108 static char *ai_errlist[] = {
109 "Success",
110 "Address family for hostname not supported", /* EAI_ADDRFAMILY */
111 "Temporary failure in name resolution", /* EAI_AGAIN */
112 "Invalid value for ai_flags", /* EAI_BADFLAGS */
113 "Non-recoverable failure in name resolution", /* EAI_FAIL */
114 "ai_family not supported", /* EAI_FAMILY */
115 "Memory allocation failure", /* EAI_MEMORY */
116 "No address associated with hostname", /* EAI_NODATA */
117 "hostname nor servname provided, or not known", /* EAI_NONAME */
118 "servname not supported for ai_socktype", /* EAI_SERVICE */
119 "ai_socktype not supported", /* EAI_SOCKTYPE */
120 "System error returned in errno", /* EAI_SYSTEM */
121 "Invalid value for hints", /* EAI_BADHINTS */
122 "Resolved protocol is unknown", /* EAI_PROTOCOL */
123 "Unknown error", /* EAI_MAX */
124 };
125 #endif
126 /* XXX macros that make external reference is BAD. */
127
128 #define GET_AI(ai, afd, addr) \
129 do { \
130 /* external reference: pai, error, and label free */ \
131 (ai) = get_ai(pai, (afd), (addr)); \
132 if ((ai) == NULL) { \
133 error = EAI_MEMORY; \
134 goto free; \
135 } \
136 } while (/*CONSTCOND*/0)
137
138 #define GET_PORT(ai, serv) \
139 do { \
140 /* external reference: error and label free */ \
141 error = get_port((ai), (serv), 0); \
142 if (error != 0) \
143 goto free; \
144 } while (/*CONSTCOND*/0)
145
146 #define ERR(err) \
147 do { \
148 /* external reference: error, and label bad */ \
149 error = (err); \
150 goto bad; \
151 /*NOTREACHED*/ \
152 } while (/*CONSTCOND*/0)
153
154 #define MATCH_FAMILY(x, y, w) \
155 ((x) == (y) || (/*CONSTCOND*/(w) && ((x) == PF_UNSPEC || (y) == PF_UNSPEC)))
156 #define MATCH(x, y, w) \
157 ((x) == (y) || (/*CONSTCOND*/(w) && ((x) == ANY || (y) == ANY)))
158
159 #if 0
160 /* We will need this should we ever want gai_strerror() */
161 char *
162 gai_strerror(int ecode)
163 {
164 if (ecode < 0 || ecode > EAI_MAX)
165 ecode = EAI_MAX;
166 return ai_errlist[ecode];
167 }
168 #endif
169
170 void
freeaddrinfo(struct addrinfo * ai)171 freeaddrinfo(struct addrinfo *ai)
172 {
173 struct addrinfo *next;
174
175 do {
176 next = ai->ai_next;
177 if (ai->ai_canonname)
178 rb_free(ai->ai_canonname);
179 /* no need to free(ai->ai_addr) */
180 rb_free(ai);
181 ai = next;
182 } while (ai);
183 }
184
185 static int
str_isnumber(const char * p)186 str_isnumber(const char *p)
187 {
188 char *ep;
189
190 if (*p == '\0')
191 return NO;
192 ep = NULL;
193 errno = 0;
194 (void)strtoul(p, &ep, 10);
195 if (errno == 0 && ep && *ep == '\0')
196 return YES;
197 else
198 return NO;
199 }
200
201 int
getaddrinfo(const char * hostname,const char * servname,const struct addrinfo * hints,struct addrinfo ** res)202 getaddrinfo(const char *hostname, const char *servname,
203 const struct addrinfo *hints, struct addrinfo **res)
204 {
205 struct addrinfo sentinel;
206 struct addrinfo *cur;
207 int error = 0;
208 struct addrinfo ai;
209 struct addrinfo ai0;
210 struct addrinfo *pai;
211 const struct explore *ex;
212
213 memset(&sentinel, 0, sizeof(sentinel));
214 cur = &sentinel;
215 pai = &ai;
216 pai->ai_flags = 0;
217 pai->ai_family = PF_UNSPEC;
218 pai->ai_socktype = ANY;
219 pai->ai_protocol = ANY;
220 pai->ai_addrlen = 0;
221 pai->ai_canonname = NULL;
222 pai->ai_addr = NULL;
223 pai->ai_next = NULL;
224
225 if (hostname == NULL && servname == NULL)
226 return EAI_NONAME;
227 if (hints) {
228 /* error check for hints */
229 if (hints->ai_addrlen || hints->ai_canonname ||
230 hints->ai_addr || hints->ai_next)
231 ERR(EAI_BADHINTS); /* xxx */
232 if (hints->ai_flags & ~AI_MASK)
233 ERR(EAI_BADFLAGS);
234 switch (hints->ai_family) {
235 case PF_UNSPEC:
236 case PF_INET:
237 #ifdef IPV6
238 case PF_INET6:
239 #endif
240 break;
241 default:
242 ERR(EAI_FAMILY);
243 }
244 memcpy(pai, hints, sizeof(*pai));
245
246 /*
247 * if both socktype/protocol are specified, check if they
248 * are meaningful combination.
249 */
250 if (pai->ai_socktype != ANY && pai->ai_protocol != ANY) {
251 for (ex = explore; ex->e_af >= 0; ex++) {
252 if (pai->ai_family != ex->e_af)
253 continue;
254 if (ex->e_socktype == ANY)
255 continue;
256 if (ex->e_protocol == ANY)
257 continue;
258 if (pai->ai_socktype == ex->e_socktype &&
259 pai->ai_protocol != ex->e_protocol) {
260 ERR(EAI_BADHINTS);
261 }
262 }
263 }
264 }
265
266 /*
267 * check for special cases. (1) numeric servname is disallowed if
268 * socktype/protocol are left unspecified. (2) servname is disallowed
269 * for raw and other inet{,6} sockets.
270 */
271 if (MATCH_FAMILY(pai->ai_family, PF_INET, 1)
272 #ifdef IPV6
273 || MATCH_FAMILY(pai->ai_family, PF_INET6, 1)
274 #endif
275 ) {
276 ai0 = *pai; /* backup *pai */
277
278 if (pai->ai_family == PF_UNSPEC) {
279 #ifdef IPV6
280 pai->ai_family = PF_INET6;
281 #else
282 pai->ai_family = PF_INET;
283 #endif
284 }
285 error = get_portmatch(pai, servname);
286 if (error)
287 ERR(error);
288
289 *pai = ai0;
290 }
291
292 ai0 = *pai;
293
294 /* NULL hostname, or numeric hostname */
295 for (ex = explore; ex->e_af >= 0; ex++) {
296 *pai = ai0;
297
298 /* PF_UNSPEC entries are prepared for DNS queries only */
299 if (ex->e_af == PF_UNSPEC)
300 continue;
301
302 if (!MATCH_FAMILY(pai->ai_family, ex->e_af, WILD_AF(ex)))
303 continue;
304 if (!MATCH(pai->ai_socktype, ex->e_socktype, WILD_SOCKTYPE(ex)))
305 continue;
306 if (!MATCH(pai->ai_protocol, ex->e_protocol, WILD_PROTOCOL(ex)))
307 continue;
308
309 if (pai->ai_family == PF_UNSPEC)
310 pai->ai_family = ex->e_af;
311 if (pai->ai_socktype == ANY && ex->e_socktype != ANY)
312 pai->ai_socktype = ex->e_socktype;
313 if (pai->ai_protocol == ANY && ex->e_protocol != ANY)
314 pai->ai_protocol = ex->e_protocol;
315
316 if (hostname == NULL)
317 error = explore_null(pai, servname, &cur->ai_next);
318 else
319 error = explore_numeric(pai, hostname, servname, &cur->ai_next);
320
321 if (error)
322 goto free;
323
324 while (cur && cur->ai_next)
325 cur = cur->ai_next;
326 }
327
328 /*
329 * XXX
330 * If numreic representation of AF1 can be interpreted as FQDN
331 * representation of AF2, we need to think again about the code below.
332 */
333 if (sentinel.ai_next)
334 goto good;
335
336 if (pai->ai_flags & AI_NUMERICHOST)
337 ERR(EAI_NONAME);
338 if (hostname == NULL)
339 ERR(EAI_NODATA);
340
341 /* XXX */
342 if (sentinel.ai_next)
343 error = 0;
344
345 if (error)
346 goto free;
347 if (error == 0) {
348 if (sentinel.ai_next) {
349 good:
350 *res = sentinel.ai_next;
351 return SUCCESS;
352 } else
353 error = EAI_FAIL;
354 }
355 free:
356 bad:
357 if (sentinel.ai_next)
358 freeaddrinfo(sentinel.ai_next);
359 *res = NULL;
360 return error;
361 }
362
363 /*
364 * hostname == NULL.
365 * passive socket -> anyaddr (0.0.0.0 or ::)
366 * non-passive socket -> localhost (127.0.0.1 or ::1)
367 */
368 static int
explore_null(const struct addrinfo * pai,const char * servname,struct addrinfo ** res)369 explore_null(const struct addrinfo *pai, const char *servname, struct addrinfo **res)
370 {
371 int s;
372 const struct afd *afd;
373 struct addrinfo *cur;
374 struct addrinfo sentinel;
375 int error;
376
377 *res = NULL;
378 sentinel.ai_next = NULL;
379 cur = &sentinel;
380
381 /*
382 * filter out AFs that are not supported by the kernel
383 * XXX errno?
384 */
385 s = socket(pai->ai_family, SOCK_DGRAM, 0);
386 if (s < 0) {
387 #ifdef _WIN32
388 errno = WSAGetLastError();
389 #endif
390 if (errno != EMFILE)
391 return 0;
392 } else
393 #ifdef _WIN32
394 closesocket(s);
395 #else
396 close(s);
397 #endif
398
399 /*
400 * if the servname does not match socktype/protocol, ignore it.
401 */
402 if (get_portmatch(pai, servname) != 0)
403 return 0;
404
405 afd = find_afd(pai->ai_family);
406 if (afd == NULL)
407 return 0;
408
409 if (pai->ai_flags & AI_PASSIVE) {
410 GET_AI(cur->ai_next, afd, afd->a_addrany);
411 GET_PORT(cur->ai_next, servname);
412 } else {
413 GET_AI(cur->ai_next, afd, afd->a_loopback);
414 GET_PORT(cur->ai_next, servname);
415 }
416 cur = cur->ai_next;
417
418 *res = sentinel.ai_next;
419 return 0;
420
421 free:
422 if (sentinel.ai_next)
423 freeaddrinfo(sentinel.ai_next);
424 return error;
425 }
426
427 /*
428 * numeric hostname
429 */
430 static int
explore_numeric(const struct addrinfo * pai,const char * hostname,const char * servname,struct addrinfo ** res)431 explore_numeric(const struct addrinfo *pai, const char *hostname,
432 const char *servname, struct addrinfo **res)
433 {
434 const struct afd *afd;
435 struct addrinfo *cur;
436 struct addrinfo sentinel;
437 int error;
438 char pton[PTON_MAX];
439
440 *res = NULL;
441 sentinel.ai_next = NULL;
442 cur = &sentinel;
443
444 /*
445 * if the servname does not match socktype/protocol, ignore it.
446 */
447 if (get_portmatch(pai, servname) != 0)
448 return 0;
449
450 afd = find_afd(pai->ai_family);
451 if (afd == NULL)
452 return 0;
453
454 switch (afd->a_af) {
455 #if 0 /*X/Open spec*/
456 case AF_INET:
457 if (rb_inet_pton
458 if (inet_aton(hostname, (struct in_addr *)pton) == 1) {
459 if (pai->ai_family == afd->a_af ||
460 pai->ai_family == PF_UNSPEC /*?*/) {
461 GET_AI(cur->ai_next, afd, pton);
462 GET_PORT(cur->ai_next, servname);
463 while (cur && cur->ai_next)
464 cur = cur->ai_next;
465 } else
466 ERR(EAI_FAMILY); /*xxx*/
467 }
468 break;
469 #endif
470 default:
471 if (rb_inet_pton(afd->a_af, hostname, pton) == 1) {
472 if (pai->ai_family == afd->a_af ||
473 pai->ai_family == PF_UNSPEC /*?*/) {
474 GET_AI(cur->ai_next, afd, pton);
475 GET_PORT(cur->ai_next, servname);
476 while (cur && cur->ai_next)
477 cur = cur->ai_next;
478 } else
479 ERR(EAI_FAMILY); /* XXX */
480 }
481 break;
482 }
483
484 *res = sentinel.ai_next;
485 return 0;
486
487 free:
488 bad:
489 if (sentinel.ai_next)
490 freeaddrinfo(sentinel.ai_next);
491 return error;
492 }
493
494 static struct addrinfo *
get_ai(const struct addrinfo * pai,const struct afd * afd,const char * addr)495 get_ai(const struct addrinfo *pai, const struct afd *afd, const char *addr)
496 {
497 char *p;
498 struct addrinfo *ai;
499
500 ai = (struct addrinfo *)rb_malloc(sizeof(struct addrinfo)
501 + (afd->a_socklen));
502 if (ai == NULL)
503 return NULL;
504
505 memcpy(ai, pai, sizeof(struct addrinfo));
506 ai->ai_addr = (struct sockaddr *)(void *)(ai + 1);
507 memset(ai->ai_addr, 0, (size_t)afd->a_socklen);
508 ai->ai_addrlen = afd->a_socklen;
509 ai->ai_addr->sa_family = ai->ai_family = afd->a_af;
510 p = (char *)(void *)(ai->ai_addr);
511 memcpy(p + afd->a_off, addr, (size_t)afd->a_addrlen);
512 return ai;
513 }
514
515 static int
get_portmatch(const struct addrinfo * ai,const char * servname)516 get_portmatch(const struct addrinfo *ai, const char *servname)
517 {
518 struct addrinfo xai;
519 memcpy(&xai, ai, sizeof(struct addrinfo));
520 return(get_port(&xai, servname, 1));
521 }
522
523 static int
get_port(struct addrinfo * ai,const char * servname,int matchonly)524 get_port(struct addrinfo *ai, const char *servname, int matchonly)
525 {
526 const char *proto;
527 struct servent *sp;
528 int port;
529 int allownumeric;
530
531 if (servname == NULL)
532 return 0;
533 switch (ai->ai_family) {
534 case AF_INET:
535 #ifdef AF_INET6
536 case AF_INET6:
537 #endif
538 break;
539 default:
540 return 0;
541 }
542
543 switch (ai->ai_socktype) {
544 case SOCK_RAW:
545 return EAI_SERVICE;
546 case SOCK_DGRAM:
547 case SOCK_STREAM:
548 allownumeric = 1;
549 break;
550 case ANY:
551 allownumeric = 0;
552 break;
553 default:
554 return EAI_SOCKTYPE;
555 }
556
557 if (str_isnumber(servname)) {
558 if (!allownumeric)
559 return EAI_SERVICE;
560 port = atoi(servname);
561 if (port < 0 || port > 65535)
562 return EAI_SERVICE;
563 port = htons(port);
564 } else {
565 switch (ai->ai_socktype) {
566 case SOCK_DGRAM:
567 proto = "udp";
568 break;
569 case SOCK_STREAM:
570 proto = "tcp";
571 break;
572 default:
573 proto = NULL;
574 break;
575 }
576
577 if ((sp = getservbyname(servname, proto)) == NULL)
578 return EAI_SERVICE;
579 port = sp->s_port;
580 }
581
582 if (!matchonly) {
583 switch (ai->ai_family) {
584 case AF_INET:
585 ((struct sockaddr_in *)(void *)
586 ai->ai_addr)->sin_port = port;
587 break;
588 #ifdef IPV6
589 case AF_INET6:
590 ((struct sockaddr_in6 *)(void *)
591 ai->ai_addr)->sin6_port = port;
592 break;
593 #endif
594 }
595 }
596
597 return 0;
598 }
599
600 static const struct afd *
find_afd(int af)601 find_afd(int af)
602 {
603 const struct afd *afd;
604
605 if (af == PF_UNSPEC)
606 return(NULL);
607
608 for (afd = afdl; afd->a_af; afd++)
609 {
610 if (afd->a_af == af)
611 return(afd);
612 }
613
614 return(NULL);
615 }
616