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