2 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
30 #include "ircd_addrinfo.h"
31 #include <stddef.h> /* for offsetof() */
32 #include <stdlib.h> /* for free() */
33 #include <string.h> /* for memset() */
34 #include <unistd.h> /* for close() */
35 #include <arpa/inet.h> /* for inet_aton(), inet_pton() */
36 #include <errno.h> /* for errno */
39 #define AI_MASK (AI_NUMERICHOST | AI_PASSIVE)
44 static const char in_addrany[] = { 0, 0, 0, 0 };
45 static const char in_loopback[] = { 127, 0, 0, 1 };
46 static const char in6_addrany[] = {
47 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
49 static const char in6_loopback[] = {
50 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1
53 static const struct afd {
58 const char *a_addrany;
59 const char *a_loopback;
64 {PF_INET6, sizeof(struct in6_addr),
65 sizeof(struct sockaddr_in6),
66 offsetof(struct sockaddr_in6, sin6_addr),
67 in6_addrany, in6_loopback, 1},
70 {PF_INET, sizeof(struct in_addr),
71 sizeof(struct sockaddr_in),
72 offsetof(struct sockaddr_in, sin_addr),
73 in_addrany, in_loopback, 0},
74 {0, 0, 0, 0, NULL, NULL, 0},
81 const char *e_protostr;
83 #define WILD_AF(ex) ((ex)->e_wild & 0x01)
84 #define WILD_SOCKTYPE(ex) ((ex)->e_wild & 0x02)
85 #define WILD_PROTOCOL(ex) ((ex)->e_wild & 0x04)
90 static const struct explore explore[] = {
92 { PF_INET6, SOCK_DGRAM, IPPROTO_UDP, "udp", 0x07 },
93 { PF_INET6, SOCK_STREAM, IPPROTO_TCP, "tcp", 0x07 },
94 { PF_INET6, SOCK_RAW, ANY, NULL, 0x05 },
96 { PF_INET, SOCK_DGRAM, IPPROTO_UDP, "udp", 0x07 },
97 { PF_INET, SOCK_STREAM, IPPROTO_TCP, "tcp", 0x07 },
98 { PF_INET, SOCK_RAW, ANY, NULL, 0x05 },
99 { PF_UNSPEC, SOCK_DGRAM, IPPROTO_UDP, "udp", 0x07 },
100 { PF_UNSPEC, SOCK_STREAM, IPPROTO_TCP, "tcp", 0x07 },
101 { PF_UNSPEC, SOCK_RAW, ANY, NULL, 0x05 },
102 { -1, 0, 0, NULL, 0 },
107 static int str_isnumber(const char *);
108 static int explore_null(const struct addrinfo *,
109 const char *, struct addrinfo **);
110 static int explore_numeric(const struct addrinfo *, const char *,
111 const char *, struct addrinfo **);
112 static struct addrinfo *get_ai(const struct addrinfo *,
113 const struct afd *, const char *);
114 static int get_portmatch(const struct addrinfo *, const char *);
115 static int get_port(struct addrinfo *, const char *, int);
116 static const struct afd *find_afd(int);
118 /* We will need this should we ever want gai_strerror().
119 * Note though that GNU libc doesn't define EAI_BADHINTS. */
120 static char *ai_errlist[] = {
122 "Address family for hostname not supported", /* EAI_ADDRFAMILY */
123 "Temporary failure in name resolution", /* EAI_AGAIN */
124 "Invalid value for ai_flags", /* EAI_BADFLAGS */
125 "Non-recoverable failure in name resolution", /* EAI_FAIL */
126 "ai_family not supported", /* EAI_FAMILY */
127 "Memory allocation failure", /* EAI_MEMORY */
128 "No address associated with hostname", /* EAI_NODATA */
129 "hostname nor servname provided, or not known", /* EAI_NONAME */
130 "servname not supported for ai_socktype", /* EAI_SERVICE */
131 "ai_socktype not supported", /* EAI_SOCKTYPE */
132 "System error returned in errno", /* EAI_SYSTEM */
133 "Invalid value for hints", /* EAI_BADHINTS */
134 "Resolved protocol is unknown", /* EAI_PROTOCOL */
135 "Unknown error", /* EAI_MAX */
138 /* XXX macros that make external reference is BAD. */
140 #define GET_AI(ai, afd, addr) \
142 /* external reference: pai, error, and label free */ \
143 (ai) = get_ai(pai, (afd), (addr)); \
144 if ((ai) == NULL) { \
145 error = EAI_MEMORY; \
148 } while (/*CONSTCOND*/0)
150 #define GET_PORT(ai, serv) \
152 /* external reference: error and label free */ \
153 error = get_port((ai), (serv), 0); \
156 } while (/*CONSTCOND*/0)
160 /* external reference: error, and label bad */ \
164 } while (/*CONSTCOND*/0)
166 #define MATCH_FAMILY(x, y, w) \
167 ((x) == (y) || (/*CONSTCOND*/(w) && ((x) == PF_UNSPEC || (y) == PF_UNSPEC)))
168 #define MATCH(x, y, w) \
169 ((x) == (y) || (/*CONSTCOND*/(w) && ((x) == ANY || (y) == ANY)))
172 /* We will need this should we ever want gai_strerror() */
174 irc_gai_strerror(int ecode)
176 if (ecode < 0 || ecode > EAI_MAX)
178 return ai_errlist[ecode];
183 irc_freeaddrinfo(struct addrinfo *ai)
185 struct addrinfo *next;
189 if (ai->ai_canonname)
190 free(ai->ai_canonname);
191 /* no need to free(ai->ai_addr) */
198 str_isnumber(const char *p)
206 (void)strtoul(p, &ep, 10);
207 if (errno == 0 && ep && *ep == '\0')
214 irc_getaddrinfo(const char *hostname, const char *servname,
215 const struct addrinfo *hints, struct addrinfo **res)
217 struct addrinfo sentinel;
218 struct addrinfo *cur;
222 struct addrinfo *pai;
223 const struct explore *ex;
225 memset(&sentinel, 0, sizeof(sentinel));
229 pai->ai_family = PF_UNSPEC;
230 pai->ai_socktype = ANY;
231 pai->ai_protocol = ANY;
233 pai->ai_canonname = NULL;
237 if (hostname == NULL && servname == NULL)
240 /* error check for hints */
241 if (hints->ai_addrlen || hints->ai_canonname ||
242 hints->ai_addr || hints->ai_next)
245 ERR(EAI_BADHINTS); /* xxx */
251 if (hints->ai_flags & ~AI_MASK)
253 switch (hints->ai_family) {
263 memcpy(pai, hints, sizeof(*pai));
266 * if both socktype/protocol are specified, check if they
267 * are meaningful combination.
269 if (pai->ai_socktype != ANY && pai->ai_protocol != ANY) {
270 for (ex = explore; ex->e_af >= 0; ex++) {
271 if (pai->ai_family != ex->e_af)
273 if (ex->e_socktype == ANY)
275 if (ex->e_protocol == ANY)
277 if (pai->ai_socktype == ex->e_socktype &&
278 pai->ai_protocol != ex->e_protocol) {
280 ERR(EAI_BADHINTS); /* xxx */
291 * check for special cases. (1) numeric servname is disallowed if
292 * socktype/protocol are left unspecified. (2) servname is disallowed
293 * for raw and other inet{,6} sockets.
295 if (MATCH_FAMILY(pai->ai_family, PF_INET, 1)
297 || MATCH_FAMILY(pai->ai_family, PF_INET6, 1)
300 ai0 = *pai; /* backup *pai */
302 if (pai->ai_family == PF_UNSPEC) {
304 pai->ai_family = PF_INET6;
306 pai->ai_family = PF_INET;
309 error = get_portmatch(pai, servname);
318 /* NULL hostname, or numeric hostname */
319 for (ex = explore; ex->e_af >= 0; ex++) {
322 /* PF_UNSPEC entries are prepared for DNS queries only */
323 if (ex->e_af == PF_UNSPEC)
326 if (!MATCH_FAMILY(pai->ai_family, ex->e_af, WILD_AF(ex)))
328 if (!MATCH(pai->ai_socktype, ex->e_socktype, WILD_SOCKTYPE(ex)))
330 if (!MATCH(pai->ai_protocol, ex->e_protocol, WILD_PROTOCOL(ex)))
333 if (pai->ai_family == PF_UNSPEC)
334 pai->ai_family = ex->e_af;
335 if (pai->ai_socktype == ANY && ex->e_socktype != ANY)
336 pai->ai_socktype = ex->e_socktype;
337 if (pai->ai_protocol == ANY && ex->e_protocol != ANY)
338 pai->ai_protocol = ex->e_protocol;
340 if (hostname == NULL)
341 error = explore_null(pai, servname, &cur->ai_next);
343 error = explore_numeric(pai, hostname, servname, &cur->ai_next);
348 while (cur && cur->ai_next)
354 * If numeric representation of AF1 can be interpreted as FQDN
355 * representation of AF2, we need to think again about the code below.
357 if (sentinel.ai_next)
360 if (pai->ai_flags & AI_NUMERICHOST)
362 if (hostname == NULL)
366 if (sentinel.ai_next)
372 if (sentinel.ai_next) {
374 *res = sentinel.ai_next;
381 if (sentinel.ai_next)
382 irc_freeaddrinfo(sentinel.ai_next);
389 * passive socket -> anyaddr (0.0.0.0 or ::)
390 * non-passive socket -> localhost (127.0.0.1 or ::1)
393 explore_null(const struct addrinfo *pai, const char *servname, struct addrinfo **res)
396 const struct afd *afd;
397 struct addrinfo *cur;
398 struct addrinfo sentinel;
402 sentinel.ai_next = NULL;
406 * filter out AFs that are not supported by the kernel
409 s = socket(pai->ai_family, SOCK_DGRAM, 0);
417 * if the servname does not match socktype/protocol, ignore it.
419 if (get_portmatch(pai, servname) != 0)
422 afd = find_afd(pai->ai_family);
426 if (pai->ai_flags & AI_PASSIVE) {
427 GET_AI(cur->ai_next, afd, afd->a_addrany);
428 GET_PORT(cur->ai_next, servname);
430 GET_AI(cur->ai_next, afd, afd->a_loopback);
431 GET_PORT(cur->ai_next, servname);
435 *res = sentinel.ai_next;
439 if (sentinel.ai_next)
440 irc_freeaddrinfo(sentinel.ai_next);
448 explore_numeric(const struct addrinfo *pai, const char *hostname,
449 const char *servname, struct addrinfo **res)
451 const struct afd *afd;
452 struct addrinfo *cur;
453 struct addrinfo sentinel;
458 sentinel.ai_next = NULL;
462 * if the servname does not match socktype/protocol, ignore it.
464 if (get_portmatch(pai, servname) != 0)
467 afd = find_afd(pai->ai_family);
472 #if 1 /*X/Open spec*/
474 if (inet_aton(hostname, (struct in_addr *)pton) == 1) {
475 if (pai->ai_family == afd->a_af ||
476 pai->ai_family == PF_UNSPEC /*?*/) {
477 GET_AI(cur->ai_next, afd, pton);
478 GET_PORT(cur->ai_next, servname);
479 while (cur && cur->ai_next)
482 ERR(EAI_FAMILY); /*xxx*/
487 if (inet_pton(afd->a_af, hostname, pton) == 1) {
488 if (pai->ai_family == afd->a_af ||
489 pai->ai_family == PF_UNSPEC /*?*/) {
490 GET_AI(cur->ai_next, afd, pton);
491 GET_PORT(cur->ai_next, servname);
492 while (cur && cur->ai_next)
495 ERR(EAI_FAMILY); /* XXX */
500 *res = sentinel.ai_next;
505 if (sentinel.ai_next)
506 irc_freeaddrinfo(sentinel.ai_next);
510 static struct addrinfo *
511 get_ai(const struct addrinfo *pai, const struct afd *afd, const char *addr)
516 ai = (struct addrinfo *)malloc(sizeof(struct addrinfo)
521 memcpy(ai, pai, sizeof(struct addrinfo));
522 ai->ai_addr = (struct sockaddr *)(void *)(ai + 1);
523 memset(ai->ai_addr, 0, (size_t)afd->a_socklen);
524 ai->ai_addrlen = afd->a_socklen;
525 ai->ai_addr->sa_family = ai->ai_family = afd->a_af;
526 p = (char *)(void *)(ai->ai_addr);
527 memcpy(p + afd->a_off, addr, (size_t)afd->a_addrlen);
532 get_portmatch(const struct addrinfo *ai, const char *servname)
534 /* get_port does not touch first argument. when matchonly == 1. */
535 /* LINTED const cast */
536 return(get_port((struct addrinfo *)ai, servname, 1));
540 get_port(struct addrinfo *ai, const char *servname, int matchonly)
547 if (servname == NULL)
549 switch (ai->ai_family) {
559 switch (ai->ai_socktype) {
573 if (str_isnumber(servname)) {
576 port = atoi(servname);
577 if (port < 0 || port > 65535)
581 switch (ai->ai_socktype) {
593 if ((sp = getservbyname(servname, proto)) == NULL)
599 switch (ai->ai_family) {
601 ((struct sockaddr_in *)(void *)
602 ai->ai_addr)->sin_port = port;
606 ((struct sockaddr_in6 *)(void *)
607 ai->ai_addr)->sin6_port = port;
616 static const struct afd *
619 const struct afd *afd;
624 for (afd = afdl; afd->a_af; afd++)