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