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