diff options
Diffstat (limited to 'src/lib/libc/net/gethostnamadr.c')
| -rw-r--r-- | src/lib/libc/net/gethostnamadr.c | 982 |
1 files changed, 738 insertions, 244 deletions
diff --git a/src/lib/libc/net/gethostnamadr.c b/src/lib/libc/net/gethostnamadr.c index ec3f14a900..ee4af36191 100644 --- a/src/lib/libc/net/gethostnamadr.c +++ b/src/lib/libc/net/gethostnamadr.c | |||
| @@ -1,5 +1,3 @@ | |||
| 1 | /* $NetBSD: gethostnamadr.c,v 1.13 1995/05/21 16:21:14 mycroft Exp $ */ | ||
| 2 | |||
| 3 | /*- | 1 | /*- |
| 4 | * Copyright (c) 1985, 1988, 1993 | 2 | * Copyright (c) 1985, 1988, 1993 |
| 5 | * The Regents of the University of California. All rights reserved. | 3 | * The Regents of the University of California. All rights reserved. |
| @@ -12,11 +10,7 @@ | |||
| 12 | * 2. Redistributions in binary form must reproduce the above copyright | 10 | * 2. Redistributions in binary form must reproduce the above copyright |
| 13 | * notice, this list of conditions and the following disclaimer in the | 11 | * notice, this list of conditions and the following disclaimer in the |
| 14 | * documentation and/or other materials provided with the distribution. | 12 | * documentation and/or other materials provided with the distribution. |
| 15 | * 3. All advertising materials mentioning features or use of this software | 13 | * 3. Neither the name of the University nor the names of its contributors |
| 16 | * must display the following acknowledgement: | ||
| 17 | * This product includes software developed by the University of | ||
| 18 | * California, Berkeley and its contributors. | ||
| 19 | * 4. Neither the name of the University nor the names of its contributors | ||
| 20 | * may be used to endorse or promote products derived from this software | 14 | * may be used to endorse or promote products derived from this software |
| 21 | * without specific prior written permission. | 15 | * without specific prior written permission. |
| 22 | * | 16 | * |
| @@ -54,12 +48,7 @@ | |||
| 54 | */ | 48 | */ |
| 55 | 49 | ||
| 56 | #if defined(LIBC_SCCS) && !defined(lint) | 50 | #if defined(LIBC_SCCS) && !defined(lint) |
| 57 | #if 0 | 51 | static const char rcsid[] = "$OpenBSD: gethostnamadr.c,v 1.59 2003/10/06 19:18:09 millert Exp $"; |
| 58 | static char sccsid[] = "@(#)gethostnamadr.c 8.1 (Berkeley) 6/4/93"; | ||
| 59 | static char rcsid[] = "$Id: gethnamaddr.c,v 4.9.1.1 1993/05/02 22:43:03 vixie Rel "; | ||
| 60 | #else | ||
| 61 | static char rcsid[] = "$NetBSD: gethostnamadr.c,v 1.13 1995/05/21 16:21:14 mycroft Exp $"; | ||
| 62 | #endif | ||
| 63 | #endif /* LIBC_SCCS and not lint */ | 52 | #endif /* LIBC_SCCS and not lint */ |
| 64 | 53 | ||
| 65 | #include <sys/param.h> | 54 | #include <sys/param.h> |
| @@ -73,11 +62,17 @@ static char rcsid[] = "$NetBSD: gethostnamadr.c,v 1.13 1995/05/21 16:21:14 mycro | |||
| 73 | #include <ctype.h> | 62 | #include <ctype.h> |
| 74 | #include <errno.h> | 63 | #include <errno.h> |
| 75 | #include <string.h> | 64 | #include <string.h> |
| 65 | #include <syslog.h> | ||
| 66 | #include <stdlib.h> | ||
| 76 | #ifdef YP | 67 | #ifdef YP |
| 77 | #include <rpc/rpc.h> | 68 | #include <rpc/rpc.h> |
| 78 | #include <rpcsvc/yp_prot.h> | 69 | #include <rpcsvc/yp.h> |
| 79 | #include <rpcsvc/ypclnt.h> | 70 | #include <rpcsvc/ypclnt.h> |
| 71 | #include "ypinternal.h" | ||
| 80 | #endif | 72 | #endif |
| 73 | #include "thread_private.h" | ||
| 74 | |||
| 75 | #define MULTI_PTRS_ARE_ALIASES 1 /* XXX - experimental */ | ||
| 81 | 76 | ||
| 82 | #define MAXALIASES 35 | 77 | #define MAXALIASES 35 |
| 83 | #define MAXADDRS 35 | 78 | #define MAXADDRS 35 |
| @@ -91,47 +86,101 @@ static char *__ypdomain; | |||
| 91 | static struct hostent host; | 86 | static struct hostent host; |
| 92 | static char *host_aliases[MAXALIASES]; | 87 | static char *host_aliases[MAXALIASES]; |
| 93 | static char hostbuf[BUFSIZ+1]; | 88 | static char hostbuf[BUFSIZ+1]; |
| 94 | static struct in_addr host_addr; | 89 | static union { |
| 90 | struct in_addr _host_in_addr; | ||
| 91 | u_char _host_addr[16]; /* IPv4 or IPv6 */ | ||
| 92 | } _host_addr_u; | ||
| 93 | #define host_addr _host_addr_u._host_addr | ||
| 95 | static FILE *hostf = NULL; | 94 | static FILE *hostf = NULL; |
| 96 | static int stayopen = 0; | 95 | static int stayopen = 0; |
| 97 | 96 | ||
| 98 | #if PACKETSZ > 1024 | 97 | static void map_v4v6_address(const char *src, char *dst); |
| 99 | #define MAXPACKET PACKETSZ | 98 | static void map_v4v6_hostent(struct hostent *hp, char **bp, char *); |
| 100 | #else | 99 | |
| 101 | #define MAXPACKET 1024 | 100 | #ifdef RESOLVSORT |
| 101 | static void addrsort(char **, int); | ||
| 102 | #endif | 102 | #endif |
| 103 | 103 | ||
| 104 | int _hokchar(const char *); | ||
| 105 | |||
| 106 | static const char AskedForGot[] = | ||
| 107 | "gethostby*.getanswer: asked for \"%s\", got \"%s\""; | ||
| 108 | |||
| 109 | #define MAXPACKET (64*1024) | ||
| 110 | |||
| 104 | typedef union { | 111 | typedef union { |
| 105 | HEADER hdr; | 112 | HEADER hdr; |
| 106 | u_char buf[MAXPACKET]; | 113 | u_char buf[MAXPACKET]; |
| 107 | } querybuf; | 114 | } querybuf; |
| 108 | 115 | ||
| 109 | typedef union { | 116 | typedef union { |
| 110 | int32_t al; | 117 | int32_t al; |
| 111 | char ac; | 118 | char ac; |
| 112 | } align; | 119 | } align; |
| 113 | 120 | ||
| 114 | static int qcomp __P((struct in_addr **, struct in_addr **)); | 121 | static struct hostent *getanswer(const querybuf *, int, const char *, int); |
| 115 | static struct hostent *getanswer __P((querybuf *, int, int)); | ||
| 116 | 122 | ||
| 117 | extern int h_errno; | 123 | extern int h_errno; |
| 118 | 124 | ||
| 125 | int | ||
| 126 | _hokchar(const char *p) | ||
| 127 | { | ||
| 128 | char c; | ||
| 129 | |||
| 130 | /* | ||
| 131 | * Many people do not obey RFC 822 and 1035. The valid | ||
| 132 | * characters are a-z, A-Z, 0-9, '-' and . But the others | ||
| 133 | * tested for below can happen, and we must be more permissive | ||
| 134 | * than the resolver until those idiots clean up their act. | ||
| 135 | * We let '/' through, but not '..' | ||
| 136 | */ | ||
| 137 | while ((c = *p++)) { | ||
| 138 | if (('a' <= c && c <= 'z') || | ||
| 139 | ('A' <= c && c <= 'Z') || | ||
| 140 | ('0' <= c && c <= '9')) | ||
| 141 | continue; | ||
| 142 | if (strchr("-_/", c)) | ||
| 143 | continue; | ||
| 144 | if (c == '.' && *p != '.') | ||
| 145 | continue; | ||
| 146 | return 0; | ||
| 147 | } | ||
| 148 | return 1; | ||
| 149 | } | ||
| 150 | |||
| 119 | static struct hostent * | 151 | static struct hostent * |
| 120 | getanswer(answer, anslen, iquery) | 152 | getanswer(const querybuf *answer, int anslen, const char *qname, int qtype) |
| 121 | querybuf *answer; | ||
| 122 | int anslen; | ||
| 123 | int iquery; | ||
| 124 | { | 153 | { |
| 125 | register HEADER *hp; | 154 | struct __res_state *_resp = _THREAD_PRIVATE(_res, _res, &_res); |
| 126 | register u_char *cp; | 155 | const HEADER *hp; |
| 127 | register int n; | 156 | const u_char *cp, *eom; |
| 128 | u_char *eom; | 157 | char tbuf[MAXDNAME]; |
| 129 | char *bp, **ap; | 158 | char *bp, **ap, **hap, *ep; |
| 130 | int type, class, buflen, ancount, qdcount; | 159 | int type, class, ancount, qdcount, n; |
| 131 | int haveanswer, getclass = C_ANY; | 160 | int haveanswer, had_error, toobig = 0; |
| 132 | char **hap; | 161 | const char *tname; |
| 162 | int (*name_ok)(const char *); | ||
| 133 | 163 | ||
| 164 | tname = qname; | ||
| 165 | host.h_name = NULL; | ||
| 134 | eom = answer->buf + anslen; | 166 | eom = answer->buf + anslen; |
| 167 | switch (qtype) { | ||
| 168 | case T_A: | ||
| 169 | case T_AAAA: | ||
| 170 | #ifdef USE_RESOLV_NAME_OK | ||
| 171 | name_ok = res_hnok; | ||
| 172 | break; | ||
| 173 | #endif | ||
| 174 | case T_PTR: | ||
| 175 | #ifdef USE_RESOLV_NAME_OK | ||
| 176 | name_ok = res_dnok; | ||
| 177 | #else | ||
| 178 | name_ok = _hokchar; | ||
| 179 | #endif | ||
| 180 | break; | ||
| 181 | default: | ||
| 182 | return (NULL); | ||
| 183 | } | ||
| 135 | /* | 184 | /* |
| 136 | * find first satisfactory answer | 185 | * find first satisfactory answer |
| 137 | */ | 186 | */ |
| @@ -139,31 +188,28 @@ getanswer(answer, anslen, iquery) | |||
| 139 | ancount = ntohs(hp->ancount); | 188 | ancount = ntohs(hp->ancount); |
| 140 | qdcount = ntohs(hp->qdcount); | 189 | qdcount = ntohs(hp->qdcount); |
| 141 | bp = hostbuf; | 190 | bp = hostbuf; |
| 142 | buflen = sizeof(hostbuf); | 191 | ep = hostbuf + sizeof hostbuf; |
| 143 | cp = answer->buf + sizeof(HEADER); | 192 | cp = answer->buf + HFIXEDSZ; |
| 144 | if (qdcount) { | 193 | if (qdcount != 1) { |
| 145 | if (iquery) { | 194 | h_errno = NO_RECOVERY; |
| 146 | if ((n = dn_expand((u_char *)answer->buf, | 195 | return (NULL); |
| 147 | (u_char *)eom, (u_char *)cp, (u_char *)bp, | 196 | } |
| 148 | buflen)) < 0) { | 197 | n = dn_expand(answer->buf, eom, cp, bp, ep - bp); |
| 149 | h_errno = NO_RECOVERY; | 198 | if ((n < 0) || !(*name_ok)(bp)) { |
| 150 | return ((struct hostent *) NULL); | 199 | h_errno = NO_RECOVERY; |
| 151 | } | 200 | return (NULL); |
| 152 | cp += n + QFIXEDSZ; | 201 | } |
| 153 | host.h_name = bp; | 202 | cp += n + QFIXEDSZ; |
| 154 | n = strlen(bp) + 1; | 203 | if (qtype == T_A || qtype == T_AAAA) { |
| 155 | bp += n; | 204 | /* res_send() has already verified that the query name is the |
| 156 | buflen -= n; | 205 | * same as the one we sent; this just gets the expanded name |
| 157 | } else | 206 | * (i.e., with the succeeding search-domain tacked on). |
| 158 | cp += __dn_skipname(cp, eom) + QFIXEDSZ; | 207 | */ |
| 159 | while (--qdcount > 0) | 208 | n = strlen(bp) + 1; /* for the \0 */ |
| 160 | cp += __dn_skipname(cp, eom) + QFIXEDSZ; | 209 | host.h_name = bp; |
| 161 | } else if (iquery) { | 210 | bp += n; |
| 162 | if (hp->aa) | 211 | /* The qname can be abbreviated, but h_name is now absolute. */ |
| 163 | h_errno = HOST_NOT_FOUND; | 212 | qname = host.h_name; |
| 164 | else | ||
| 165 | h_errno = TRY_AGAIN; | ||
| 166 | return ((struct hostent *) NULL); | ||
| 167 | } | 213 | } |
| 168 | ap = host_aliases; | 214 | ap = host_aliases; |
| 169 | *ap = NULL; | 215 | *ap = NULL; |
| @@ -172,103 +218,321 @@ getanswer(answer, anslen, iquery) | |||
| 172 | *hap = NULL; | 218 | *hap = NULL; |
| 173 | host.h_addr_list = h_addr_ptrs; | 219 | host.h_addr_list = h_addr_ptrs; |
| 174 | haveanswer = 0; | 220 | haveanswer = 0; |
| 175 | while (--ancount >= 0 && cp < eom) { | 221 | had_error = 0; |
| 176 | if ((n = dn_expand((u_char *)answer->buf, (u_char *)eom, | 222 | while (ancount-- > 0 && cp < eom && !had_error) { |
| 177 | (u_char *)cp, (u_char *)bp, buflen)) < 0) | 223 | n = dn_expand(answer->buf, eom, cp, bp, ep - bp); |
| 224 | if ((n < 0) || !(*name_ok)(bp)) { | ||
| 225 | had_error++; | ||
| 226 | continue; | ||
| 227 | } | ||
| 228 | cp += n; /* name */ | ||
| 229 | if (cp >= eom) | ||
| 178 | break; | 230 | break; |
| 179 | cp += n; | ||
| 180 | type = _getshort(cp); | 231 | type = _getshort(cp); |
| 181 | cp += sizeof(u_int16_t); | 232 | cp += INT16SZ; /* type */ |
| 233 | if (cp >= eom) | ||
| 234 | break; | ||
| 182 | class = _getshort(cp); | 235 | class = _getshort(cp); |
| 183 | cp += sizeof(u_int16_t) + sizeof(u_int32_t); | 236 | cp += INT16SZ + INT32SZ; /* class, TTL */ |
| 237 | if (cp >= eom) | ||
| 238 | break; | ||
| 184 | n = _getshort(cp); | 239 | n = _getshort(cp); |
| 185 | cp += sizeof(u_int16_t); | 240 | cp += INT16SZ; /* len */ |
| 186 | if (type == T_CNAME) { | 241 | if (cp >= eom) |
| 242 | break; | ||
| 243 | if (type == T_SIG) { | ||
| 244 | /* XXX - ignore signatures as we don't use them yet */ | ||
| 187 | cp += n; | 245 | cp += n; |
| 246 | continue; | ||
| 247 | } | ||
| 248 | if (class != C_IN) { | ||
| 249 | /* XXX - debug? syslog? */ | ||
| 250 | cp += n; | ||
| 251 | continue; /* XXX - had_error++ ? */ | ||
| 252 | } | ||
| 253 | if ((qtype == T_A || qtype == T_AAAA) && type == T_CNAME) { | ||
| 188 | if (ap >= &host_aliases[MAXALIASES-1]) | 254 | if (ap >= &host_aliases[MAXALIASES-1]) |
| 189 | continue; | 255 | continue; |
| 256 | n = dn_expand(answer->buf, eom, cp, tbuf, sizeof tbuf); | ||
| 257 | if ((n < 0) || !(*name_ok)(tbuf)) { | ||
| 258 | had_error++; | ||
| 259 | continue; | ||
| 260 | } | ||
| 261 | cp += n; | ||
| 262 | /* Store alias. */ | ||
| 190 | *ap++ = bp; | 263 | *ap++ = bp; |
| 191 | n = strlen(bp) + 1; | 264 | n = strlen(bp) + 1; /* for the \0 */ |
| 192 | bp += n; | 265 | bp += n; |
| 193 | buflen -= n; | 266 | /* Get canonical name. */ |
| 194 | continue; | 267 | n = strlen(tbuf) + 1; /* for the \0 */ |
| 195 | } | 268 | if (n > ep - bp) { |
| 196 | if (iquery && type == T_PTR) { | 269 | had_error++; |
| 197 | if ((n = dn_expand((u_char *)answer->buf, | 270 | continue; |
| 198 | (u_char *)eom, (u_char *)cp, (u_char *)bp, | 271 | } |
| 199 | buflen)) < 0) | 272 | strlcpy(bp, tbuf, ep - bp); |
| 200 | break; | ||
| 201 | cp += n; | ||
| 202 | host.h_name = bp; | 273 | host.h_name = bp; |
| 203 | return(&host); | 274 | bp += n; |
| 275 | continue; | ||
| 204 | } | 276 | } |
| 205 | if (iquery || type != T_A) { | 277 | if (qtype == T_PTR && type == T_CNAME) { |
| 206 | #ifdef DEBUG | 278 | n = dn_expand(answer->buf, eom, cp, tbuf, sizeof tbuf); |
| 207 | if (_res.options & RES_DEBUG) | 279 | #ifdef USE_RESOLV_NAME_OK |
| 208 | printf("unexpected answer type %d, size %d\n", | 280 | if ((n < 0) || !res_hnok(tbuf)) { |
| 209 | type, n); | 281 | #else |
| 282 | if ((n < 0) || !_hokchar(tbuf)) { | ||
| 210 | #endif | 283 | #endif |
| 284 | had_error++; | ||
| 285 | continue; | ||
| 286 | } | ||
| 211 | cp += n; | 287 | cp += n; |
| 288 | /* Get canonical name. */ | ||
| 289 | n = strlen(tbuf) + 1; /* for the \0 */ | ||
| 290 | if (n > ep - bp) { | ||
| 291 | had_error++; | ||
| 292 | continue; | ||
| 293 | } | ||
| 294 | strlcpy(bp, tbuf, ep - bp); | ||
| 295 | tname = bp; | ||
| 296 | bp += n; | ||
| 212 | continue; | 297 | continue; |
| 213 | } | 298 | } |
| 214 | if (haveanswer) { | 299 | if (type != qtype) { |
| 215 | if (n != host.h_length) { | 300 | syslog(LOG_NOTICE|LOG_AUTH, |
| 301 | "gethostby*.getanswer: asked for \"%s %s %s\", got type \"%s\"", | ||
| 302 | qname, p_class(C_IN), p_type(qtype), | ||
| 303 | p_type(type)); | ||
| 304 | cp += n; | ||
| 305 | continue; /* XXX - had_error++ ? */ | ||
| 306 | } | ||
| 307 | switch (type) { | ||
| 308 | case T_PTR: | ||
| 309 | if (strcasecmp(tname, bp) != 0) { | ||
| 310 | syslog(LOG_NOTICE|LOG_AUTH, | ||
| 311 | AskedForGot, qname, bp); | ||
| 216 | cp += n; | 312 | cp += n; |
| 217 | continue; | 313 | continue; /* XXX - had_error++ ? */ |
| 314 | } | ||
| 315 | n = dn_expand(answer->buf, eom, cp, bp, ep - bp); | ||
| 316 | #ifdef USE_RESOLV_NAME_OK | ||
| 317 | if ((n < 0) || !res_hnok(bp)) { | ||
| 318 | #else | ||
| 319 | if ((n < 0) || !_hokchar(bp)) { | ||
| 320 | #endif | ||
| 321 | had_error++; | ||
| 322 | break; | ||
| 323 | } | ||
| 324 | #if MULTI_PTRS_ARE_ALIASES | ||
| 325 | cp += n; | ||
| 326 | if (!haveanswer) | ||
| 327 | host.h_name = bp; | ||
| 328 | else if (ap < &host_aliases[MAXALIASES-1]) | ||
| 329 | *ap++ = bp; | ||
| 330 | else | ||
| 331 | n = -1; | ||
| 332 | if (n != -1) { | ||
| 333 | n = strlen(bp) + 1; /* for the \0 */ | ||
| 334 | bp += n; | ||
| 335 | } | ||
| 336 | break; | ||
| 337 | #else | ||
| 338 | host.h_name = bp; | ||
| 339 | if (_resp->options & RES_USE_INET6) { | ||
| 340 | n = strlen(bp) + 1; /* for the \0 */ | ||
| 341 | bp += n; | ||
| 342 | map_v4v6_hostent(&host, &bp, ep); | ||
| 218 | } | 343 | } |
| 219 | if (class != getclass) { | 344 | h_errno = NETDB_SUCCESS; |
| 345 | return (&host); | ||
| 346 | #endif | ||
| 347 | case T_A: | ||
| 348 | case T_AAAA: | ||
| 349 | if (strcasecmp(host.h_name, bp) != 0) { | ||
| 350 | syslog(LOG_NOTICE|LOG_AUTH, | ||
| 351 | AskedForGot, host.h_name, bp); | ||
| 352 | cp += n; | ||
| 353 | continue; /* XXX - had_error++ ? */ | ||
| 354 | } | ||
| 355 | if (n != host.h_length) { | ||
| 220 | cp += n; | 356 | cp += n; |
| 221 | continue; | 357 | continue; |
| 222 | } | 358 | } |
| 223 | } else { | 359 | if (type == T_AAAA) { |
| 224 | host.h_length = n; | 360 | struct in6_addr in6; |
| 225 | getclass = class; | 361 | memcpy(&in6, cp, IN6ADDRSZ); |
| 226 | host.h_addrtype = (class == C_IN) ? AF_INET : AF_UNSPEC; | 362 | if (IN6_IS_ADDR_V4MAPPED(&in6)) { |
| 227 | if (!iquery) { | 363 | cp += n; |
| 364 | continue; | ||
| 365 | } | ||
| 366 | } | ||
| 367 | if (!haveanswer) { | ||
| 368 | int nn; | ||
| 369 | |||
| 228 | host.h_name = bp; | 370 | host.h_name = bp; |
| 229 | bp += strlen(bp) + 1; | 371 | nn = strlen(bp) + 1; /* for the \0 */ |
| 372 | bp += nn; | ||
| 230 | } | 373 | } |
| 231 | } | ||
| 232 | 374 | ||
| 233 | bp += sizeof(align) - ((u_long)bp % sizeof(align)); | 375 | bp += sizeof(align) - ((u_long)bp % sizeof(align)); |
| 234 | 376 | ||
| 235 | if (bp + n >= &hostbuf[sizeof(hostbuf)]) { | 377 | if (bp + n >= &hostbuf[sizeof hostbuf]) { |
| 236 | #ifdef DEBUG | 378 | #ifdef DEBUG |
| 237 | if (_res.options & RES_DEBUG) | 379 | if (_resp->options & RES_DEBUG) |
| 238 | printf("size (%d) too big\n", n); | 380 | printf("size (%d) too big\n", n); |
| 239 | #endif | 381 | #endif |
| 382 | had_error++; | ||
| 383 | continue; | ||
| 384 | } | ||
| 385 | if (hap >= &h_addr_ptrs[MAXADDRS-1]) { | ||
| 386 | if (!toobig++) | ||
| 387 | #ifdef DEBUG | ||
| 388 | if (_resp->options & RES_DEBUG) | ||
| 389 | printf("Too many addresses (%d)\n", MAXADDRS); | ||
| 390 | #endif | ||
| 391 | cp += n; | ||
| 392 | continue; | ||
| 393 | } | ||
| 394 | bcopy(cp, *hap++ = bp, n); | ||
| 395 | bp += n; | ||
| 396 | cp += n; | ||
| 240 | break; | 397 | break; |
| 241 | } | 398 | } |
| 242 | bcopy(cp, *hap++ = bp, n); | 399 | if (!had_error) |
| 243 | bp +=n; | 400 | haveanswer++; |
| 244 | cp += n; | ||
| 245 | haveanswer++; | ||
| 246 | } | 401 | } |
| 247 | if (haveanswer) { | 402 | if (haveanswer) { |
| 248 | *ap = NULL; | 403 | *ap = NULL; |
| 249 | *hap = NULL; | 404 | *hap = NULL; |
| 250 | if (_res.nsort) { | 405 | # if defined(RESOLVSORT) |
| 251 | qsort(host.h_addr_list, haveanswer, | 406 | /* |
| 252 | sizeof(struct in_addr), | 407 | * Note: we sort even if host can take only one address |
| 253 | (int (*)__P((const void *, const void *)))qcomp); | 408 | * in its return structures - should give it the "best" |
| 409 | * address in that case, not some random one | ||
| 410 | */ | ||
| 411 | if (_resp->nsort && haveanswer > 1 && qtype == T_A) | ||
| 412 | addrsort(h_addr_ptrs, haveanswer); | ||
| 413 | # endif /*RESOLVSORT*/ | ||
| 414 | if (!host.h_name) { | ||
| 415 | n = strlen(qname) + 1; /* for the \0 */ | ||
| 416 | if (n > ep - bp) | ||
| 417 | goto try_again; | ||
| 418 | strlcpy(bp, qname, ep - bp); | ||
| 419 | host.h_name = bp; | ||
| 420 | bp += n; | ||
| 254 | } | 421 | } |
| 422 | if (_resp->options & RES_USE_INET6) | ||
| 423 | map_v4v6_hostent(&host, &bp, ep); | ||
| 424 | h_errno = NETDB_SUCCESS; | ||
| 255 | return (&host); | 425 | return (&host); |
| 256 | } else { | ||
| 257 | h_errno = TRY_AGAIN; | ||
| 258 | return ((struct hostent *) NULL); | ||
| 259 | } | 426 | } |
| 427 | try_again: | ||
| 428 | h_errno = TRY_AGAIN; | ||
| 429 | return (NULL); | ||
| 430 | } | ||
| 431 | |||
| 432 | #ifdef notyet | ||
| 433 | /* | ||
| 434 | * XXX This is an extremely bogus implementation. | ||
| 435 | * | ||
| 436 | * FreeBSD has this interface: | ||
| 437 | * int gethostbyaddr_r(const char *addr, int len, int type, | ||
| 438 | * struct hostent *result, struct hostent_data *buffer) | ||
| 439 | */ | ||
| 440 | |||
| 441 | struct hostent * | ||
| 442 | gethostbyname_r(const char *name, struct hostent *hp, char *buf, int buflen, | ||
| 443 | int *errorp) | ||
| 444 | { | ||
| 445 | struct hostent *res; | ||
| 446 | |||
| 447 | res = gethostbyname(name); | ||
| 448 | *errorp = h_errno; | ||
| 449 | if (res == NULL) | ||
| 450 | return NULL; | ||
| 451 | memcpy(hp, res, sizeof *hp); /* XXX not sufficient */ | ||
| 452 | return hp; | ||
| 453 | } | ||
| 454 | |||
| 455 | /* | ||
| 456 | * XXX This is an extremely bogus implementation. | ||
| 457 | */ | ||
| 458 | struct hostent * | ||
| 459 | gethostbyaddr_r(const char *addr, int len, int af, struct hostent *he, | ||
| 460 | char *buf, int buflen, int *errorp) | ||
| 461 | { | ||
| 462 | struct hostent * res; | ||
| 463 | |||
| 464 | res = gethostbyaddr(addr, len, af); | ||
| 465 | *errorp = h_errno; | ||
| 466 | if (res == NULL) | ||
| 467 | return NULL; | ||
| 468 | memcpy(he, res, sizeof *he); /* XXX not sufficient */ | ||
| 469 | return he; | ||
| 470 | } | ||
| 471 | |||
| 472 | /* XXX RFC2133 expects a gethostbyname2_r() -- unimplemented */ | ||
| 473 | #endif | ||
| 474 | |||
| 475 | struct hostent * | ||
| 476 | gethostbyname(const char *name) | ||
| 477 | { | ||
| 478 | struct __res_state *_resp = _THREAD_PRIVATE(_res, _res, &_res); | ||
| 479 | struct hostent *hp; | ||
| 480 | extern struct hostent *_gethtbyname2(const char *, int); | ||
| 481 | |||
| 482 | if ((_resp->options & RES_INIT) == 0 && res_init() == -1) | ||
| 483 | hp = _gethtbyname2(name, AF_INET); | ||
| 484 | |||
| 485 | else if (_resp->options & RES_USE_INET6) { | ||
| 486 | hp = gethostbyname2(name, AF_INET6); | ||
| 487 | if (hp == NULL) | ||
| 488 | hp = gethostbyname2(name, AF_INET); | ||
| 489 | } | ||
| 490 | else | ||
| 491 | hp = gethostbyname2(name, AF_INET); | ||
| 492 | return hp; | ||
| 260 | } | 493 | } |
| 261 | 494 | ||
| 262 | struct hostent * | 495 | struct hostent * |
| 263 | gethostbyname(name) | 496 | gethostbyname2(const char *name, int af) |
| 264 | const char *name; | ||
| 265 | { | 497 | { |
| 266 | querybuf buf; | 498 | struct __res_state *_resp = _THREAD_PRIVATE(_res, _res, &_res); |
| 267 | register const char *cp; | 499 | querybuf *buf; |
| 268 | int n, i; | 500 | const char *cp; |
| 269 | extern struct hostent *_gethtbyname(), *_yp_gethtbyname(); | 501 | char *bp, *ep; |
| 270 | register struct hostent *hp; | 502 | int n, size, type, i; |
| 503 | struct hostent *hp; | ||
| 271 | char lookups[MAXDNSLUS]; | 504 | char lookups[MAXDNSLUS]; |
| 505 | extern struct hostent *_gethtbyname2(const char *, int); | ||
| 506 | extern struct hostent *_yp_gethtbyname(const char *); | ||
| 507 | |||
| 508 | if ((_resp->options & RES_INIT) == 0 && res_init() == -1) | ||
| 509 | return (_gethtbyname2(name, af)); | ||
| 510 | |||
| 511 | switch (af) { | ||
| 512 | case AF_INET: | ||
| 513 | size = INADDRSZ; | ||
| 514 | type = T_A; | ||
| 515 | break; | ||
| 516 | case AF_INET6: | ||
| 517 | size = IN6ADDRSZ; | ||
| 518 | type = T_AAAA; | ||
| 519 | break; | ||
| 520 | default: | ||
| 521 | h_errno = NETDB_INTERNAL; | ||
| 522 | errno = EAFNOSUPPORT; | ||
| 523 | return (NULL); | ||
| 524 | } | ||
| 525 | |||
| 526 | host.h_addrtype = af; | ||
| 527 | host.h_length = size; | ||
| 528 | |||
| 529 | /* | ||
| 530 | * if there aren't any dots, it could be a user-level alias. | ||
| 531 | * this is also done in res_query() since we are not the only | ||
| 532 | * function that looks up host names. | ||
| 533 | */ | ||
| 534 | if (!strchr(name, '.') && (cp = __hostalias(name))) | ||
| 535 | name = cp; | ||
| 272 | 536 | ||
| 273 | /* | 537 | /* |
| 274 | * disallow names consisting only of digits/dots, unless | 538 | * disallow names consisting only of digits/dots, unless |
| @@ -284,122 +548,234 @@ gethostbyname(name) | |||
| 284 | * Fake up a hostent as if we'd actually | 548 | * Fake up a hostent as if we'd actually |
| 285 | * done a lookup. | 549 | * done a lookup. |
| 286 | */ | 550 | */ |
| 287 | if (!inet_aton(name, &host_addr)) { | 551 | if (inet_pton(af, name, host_addr) <= 0) { |
| 288 | h_errno = HOST_NOT_FOUND; | 552 | h_errno = HOST_NOT_FOUND; |
| 289 | return((struct hostent *) NULL); | 553 | return (NULL); |
| 290 | } | 554 | } |
| 291 | host.h_name = (char *)name; | 555 | strlcpy(hostbuf, name, MAXHOSTNAMELEN); |
| 556 | bp = hostbuf + MAXHOSTNAMELEN; | ||
| 557 | ep = hostbuf + sizeof(hostbuf); | ||
| 558 | host.h_name = hostbuf; | ||
| 292 | host.h_aliases = host_aliases; | 559 | host.h_aliases = host_aliases; |
| 293 | host_aliases[0] = NULL; | 560 | host_aliases[0] = NULL; |
| 294 | host.h_addrtype = AF_INET; | 561 | h_addr_ptrs[0] = (char *)host_addr; |
| 295 | host.h_length = sizeof(u_int32_t); | ||
| 296 | h_addr_ptrs[0] = (char *)&host_addr; | ||
| 297 | h_addr_ptrs[1] = NULL; | 562 | h_addr_ptrs[1] = NULL; |
| 298 | host.h_addr_list = h_addr_ptrs; | 563 | host.h_addr_list = h_addr_ptrs; |
| 564 | if (_resp->options & RES_USE_INET6) | ||
| 565 | map_v4v6_hostent(&host, &bp, ep); | ||
| 566 | h_errno = NETDB_SUCCESS; | ||
| 299 | return (&host); | 567 | return (&host); |
| 300 | } | 568 | } |
| 301 | if (!isdigit(*cp) && *cp != '.') | 569 | if (!isdigit(*cp) && *cp != '.') |
| 302 | break; | 570 | break; |
| 303 | } | 571 | } |
| 572 | if ((isxdigit(name[0]) && strchr(name, ':') != NULL) || | ||
| 573 | name[0] == ':') | ||
| 574 | for (cp = name;; ++cp) { | ||
| 575 | if (!*cp) { | ||
| 576 | if (*--cp == '.') | ||
| 577 | break; | ||
| 578 | /* | ||
| 579 | * All-IPv6-legal, no dot at the end. | ||
| 580 | * Fake up a hostent as if we'd actually | ||
| 581 | * done a lookup. | ||
| 582 | */ | ||
| 583 | if (inet_pton(af, name, host_addr) <= 0) { | ||
| 584 | h_errno = HOST_NOT_FOUND; | ||
| 585 | return (NULL); | ||
| 586 | } | ||
| 587 | strlcpy(hostbuf, name, MAXHOSTNAMELEN); | ||
| 588 | bp = hostbuf + MAXHOSTNAMELEN; | ||
| 589 | ep = hostbuf + sizeof(hostbuf); | ||
| 590 | host.h_name = hostbuf; | ||
| 591 | host.h_aliases = host_aliases; | ||
| 592 | host_aliases[0] = NULL; | ||
| 593 | h_addr_ptrs[0] = (char *)host_addr; | ||
| 594 | h_addr_ptrs[1] = NULL; | ||
| 595 | host.h_addr_list = h_addr_ptrs; | ||
| 596 | h_errno = NETDB_SUCCESS; | ||
| 597 | return (&host); | ||
| 598 | } | ||
| 599 | if (!isxdigit(*cp) && *cp != ':' && *cp != '.') | ||
| 600 | break; | ||
| 601 | } | ||
| 304 | 602 | ||
| 305 | if ((_res.options & RES_INIT) == 0 && res_init() == -1) | 603 | bcopy(_resp->lookups, lookups, sizeof lookups); |
| 306 | return (_gethtbyname(name)); | ||
| 307 | |||
| 308 | bcopy(_res.lookups, lookups, sizeof lookups); | ||
| 309 | if (lookups[0] == '\0') | 604 | if (lookups[0] == '\0') |
| 310 | strncpy(lookups, "bf", sizeof lookups); | 605 | strlcpy(lookups, "bf", sizeof lookups); |
| 311 | 606 | ||
| 312 | hp = (struct hostent *)NULL; | 607 | hp = (struct hostent *)NULL; |
| 313 | for (i = 0; i < MAXDNSLUS && hp == NULL && lookups[i]; i++) { | 608 | for (i = 0; i < MAXDNSLUS && hp == NULL && lookups[i]; i++) { |
| 314 | switch (lookups[i]) { | 609 | switch (lookups[i]) { |
| 315 | #ifdef YP | 610 | #ifdef YP |
| 316 | case 'y': | 611 | case 'y': |
| 317 | hp = _yp_gethtbyname(name); | 612 | /* YP only supports AF_INET. */ |
| 613 | if (af == AF_INET) | ||
| 614 | hp = _yp_gethtbyname(name); | ||
| 318 | break; | 615 | break; |
| 319 | #endif | 616 | #endif |
| 320 | case 'b': | 617 | case 'b': |
| 321 | if ((n = res_search(name, C_IN, T_A, buf.buf, | 618 | buf = malloc(sizeof(*buf)); |
| 322 | sizeof(buf))) < 0) { | 619 | if (buf == NULL) |
| 620 | break; | ||
| 621 | if ((n = res_search(name, C_IN, type, buf->buf, | ||
| 622 | sizeof(buf->buf))) < 0) { | ||
| 623 | free(buf); | ||
| 323 | #ifdef DEBUG | 624 | #ifdef DEBUG |
| 324 | if (_res.options & RES_DEBUG) | 625 | if (_resp->options & RES_DEBUG) |
| 325 | printf("res_search failed\n"); | 626 | printf("res_search failed\n"); |
| 326 | #endif | 627 | #endif |
| 327 | break; | 628 | break; |
| 328 | } | 629 | } |
| 329 | hp = getanswer(&buf, n, 0); | 630 | hp = getanswer(buf, n, name, type); |
| 631 | free(buf); | ||
| 330 | break; | 632 | break; |
| 331 | case 'f': | 633 | case 'f': |
| 332 | hp = _gethtbyname(name); | 634 | hp = _gethtbyname2(name, af); |
| 333 | break; | 635 | break; |
| 334 | } | 636 | } |
| 335 | } | 637 | } |
| 638 | /* XXX h_errno not correct in all cases... */ | ||
| 336 | return (hp); | 639 | return (hp); |
| 337 | } | 640 | } |
| 338 | 641 | ||
| 339 | struct hostent * | 642 | struct hostent * |
| 340 | gethostbyaddr(addr, len, type) | 643 | gethostbyaddr(const char *addr, int len, int af) |
| 341 | const char *addr; | ||
| 342 | int len, type; | ||
| 343 | { | 644 | { |
| 344 | int n, i; | 645 | struct __res_state *_resp = _THREAD_PRIVATE(_res, _res, &_res); |
| 345 | querybuf buf; | 646 | const u_char *uaddr = (const u_char *)addr; |
| 346 | register struct hostent *hp; | 647 | int n, size, i; |
| 347 | char qbuf[MAXDNAME]; | 648 | querybuf *buf; |
| 348 | extern struct hostent *_gethtbyaddr(), *_yp_gethtbyaddr(); | 649 | struct hostent *hp; |
| 650 | char qbuf[MAXDNAME+1], *qp, *ep; | ||
| 349 | char lookups[MAXDNSLUS]; | 651 | char lookups[MAXDNSLUS]; |
| 652 | struct hostent *res; | ||
| 653 | extern struct hostent *_gethtbyaddr(const char *, int, int); | ||
| 654 | extern struct hostent *_yp_gethtbyaddr(const char *); | ||
| 350 | 655 | ||
| 351 | if (type != AF_INET) | 656 | if ((_resp->options & RES_INIT) == 0 && res_init() == -1) { |
| 352 | return ((struct hostent *) NULL); | 657 | res = _gethtbyaddr(addr, len, af); |
| 353 | (void)sprintf(qbuf, "%u.%u.%u.%u.in-addr.arpa", | 658 | return (res); |
| 354 | ((unsigned)addr[3] & 0xff), | 659 | } |
| 355 | ((unsigned)addr[2] & 0xff), | 660 | |
| 356 | ((unsigned)addr[1] & 0xff), | 661 | if (af == AF_INET6 && len == IN6ADDRSZ && |
| 357 | ((unsigned)addr[0] & 0xff)); | 662 | (IN6_IS_ADDR_LINKLOCAL((struct in6_addr *)uaddr) || |
| 358 | 663 | IN6_IS_ADDR_SITELOCAL((struct in6_addr *)uaddr))) { | |
| 359 | if ((_res.options & RES_INIT) == 0 && res_init() == -1) | 664 | h_errno = HOST_NOT_FOUND; |
| 360 | return (_gethtbyaddr(addr, len, type)); | 665 | return (NULL); |
| 361 | 666 | } | |
| 362 | bcopy(_res.lookups, lookups, sizeof lookups); | 667 | if (af == AF_INET6 && len == IN6ADDRSZ && |
| 668 | (IN6_IS_ADDR_V4MAPPED((struct in6_addr *)uaddr) || | ||
| 669 | IN6_IS_ADDR_V4COMPAT((struct in6_addr *)uaddr))) { | ||
| 670 | /* Unmap. */ | ||
| 671 | addr += IN6ADDRSZ - INADDRSZ; | ||
| 672 | uaddr += IN6ADDRSZ - INADDRSZ; | ||
| 673 | af = AF_INET; | ||
| 674 | len = INADDRSZ; | ||
| 675 | } | ||
| 676 | switch (af) { | ||
| 677 | case AF_INET: | ||
| 678 | size = INADDRSZ; | ||
| 679 | break; | ||
| 680 | case AF_INET6: | ||
| 681 | size = IN6ADDRSZ; | ||
| 682 | break; | ||
| 683 | default: | ||
| 684 | errno = EAFNOSUPPORT; | ||
| 685 | h_errno = NETDB_INTERNAL; | ||
| 686 | return (NULL); | ||
| 687 | } | ||
| 688 | if (size != len) { | ||
| 689 | errno = EINVAL; | ||
| 690 | h_errno = NETDB_INTERNAL; | ||
| 691 | return (NULL); | ||
| 692 | } | ||
| 693 | ep = qbuf + sizeof(qbuf); | ||
| 694 | switch (af) { | ||
| 695 | case AF_INET: | ||
| 696 | (void) snprintf(qbuf, sizeof qbuf, "%u.%u.%u.%u.in-addr.arpa", | ||
| 697 | (uaddr[3] & 0xff), (uaddr[2] & 0xff), | ||
| 698 | (uaddr[1] & 0xff), (uaddr[0] & 0xff)); | ||
| 699 | break; | ||
| 700 | case AF_INET6: | ||
| 701 | qp = qbuf; | ||
| 702 | for (n = IN6ADDRSZ - 1; n >= 0; n--) { | ||
| 703 | i = snprintf(qp, ep - qp, "%x.%x.", | ||
| 704 | uaddr[n] & 0xf, (uaddr[n] >> 4) & 0xf); | ||
| 705 | if (i <= 0) { | ||
| 706 | errno = EINVAL; | ||
| 707 | h_errno = NETDB_INTERNAL; | ||
| 708 | return (NULL); | ||
| 709 | } | ||
| 710 | qp += i; | ||
| 711 | } | ||
| 712 | break; | ||
| 713 | } | ||
| 714 | |||
| 715 | bcopy(_resp->lookups, lookups, sizeof lookups); | ||
| 363 | if (lookups[0] == '\0') | 716 | if (lookups[0] == '\0') |
| 364 | strncpy(lookups, "bf", sizeof lookups); | 717 | strlcpy(lookups, "bf", sizeof lookups); |
| 365 | 718 | ||
| 366 | hp = (struct hostent *)NULL; | 719 | hp = (struct hostent *)NULL; |
| 367 | for (i = 0; i < MAXDNSLUS && hp == NULL && lookups[i]; i++) { | 720 | for (i = 0; i < MAXDNSLUS && hp == NULL && lookups[i]; i++) { |
| 368 | switch (lookups[i]) { | 721 | switch (lookups[i]) { |
| 369 | #ifdef YP | 722 | #ifdef YP |
| 370 | case 'y': | 723 | case 'y': |
| 371 | hp = _yp_gethtbyaddr(addr, len, type); | 724 | /* YP only supports AF_INET. */ |
| 725 | if (af == AF_INET) | ||
| 726 | hp = _yp_gethtbyaddr(addr); | ||
| 372 | break; | 727 | break; |
| 373 | #endif | 728 | #endif |
| 374 | case 'b': | 729 | case 'b': |
| 375 | n = res_query(qbuf, C_IN, T_PTR, (char *)&buf, sizeof(buf)); | 730 | if (af == AF_INET6) |
| 731 | strlcpy(qp, "ip6.arpa", ep - qp); | ||
| 732 | buf = malloc(sizeof(*buf)); | ||
| 733 | if (!buf) | ||
| 734 | break; | ||
| 735 | n = res_query(qbuf, C_IN, T_PTR, buf->buf, | ||
| 736 | sizeof(buf->buf)); | ||
| 737 | if (n < 0 && af == AF_INET6) { | ||
| 738 | strlcpy(qp, "ip6.int", ep - qp); | ||
| 739 | n = res_query(qbuf, C_IN, T_PTR, | ||
| 740 | buf->buf, sizeof(buf->buf)); | ||
| 741 | } | ||
| 376 | if (n < 0) { | 742 | if (n < 0) { |
| 743 | free(buf); | ||
| 377 | #ifdef DEBUG | 744 | #ifdef DEBUG |
| 378 | if (_res.options & RES_DEBUG) | 745 | if (_resp->options & RES_DEBUG) |
| 379 | printf("res_query failed\n"); | 746 | printf("res_query failed\n"); |
| 380 | #endif | 747 | #endif |
| 381 | break; | 748 | break; |
| 382 | } | 749 | } |
| 383 | hp = getanswer(&buf, n, 1); | 750 | if (!(hp = getanswer(buf, n, qbuf, T_PTR))) { |
| 384 | if (hp == NULL) | 751 | free(buf); |
| 385 | break; | 752 | break; |
| 386 | hp->h_addrtype = type; | 753 | } |
| 754 | free(buf); | ||
| 755 | hp->h_addrtype = af; | ||
| 387 | hp->h_length = len; | 756 | hp->h_length = len; |
| 388 | h_addr_ptrs[0] = (char *)&host_addr; | 757 | bcopy(addr, host_addr, len); |
| 389 | h_addr_ptrs[1] = (char *)0; | 758 | h_addr_ptrs[0] = (char *)host_addr; |
| 390 | host_addr = *(struct in_addr *)addr; | 759 | h_addr_ptrs[1] = NULL; |
| 760 | if (af == AF_INET && (_resp->options & RES_USE_INET6)) { | ||
| 761 | map_v4v6_address((char*)host_addr, | ||
| 762 | (char*)host_addr); | ||
| 763 | hp->h_addrtype = AF_INET6; | ||
| 764 | hp->h_length = IN6ADDRSZ; | ||
| 765 | } | ||
| 766 | h_errno = NETDB_SUCCESS; | ||
| 391 | break; | 767 | break; |
| 392 | case 'f': | 768 | case 'f': |
| 393 | hp = _gethtbyaddr(addr, len, type); | 769 | hp = _gethtbyaddr(addr, len, af); |
| 394 | break; | 770 | break; |
| 395 | } | 771 | } |
| 396 | } | 772 | } |
| 773 | /* XXX h_errno not correct in all cases... */ | ||
| 397 | return (hp); | 774 | return (hp); |
| 398 | } | 775 | } |
| 399 | 776 | ||
| 400 | void | 777 | void |
| 401 | _sethtent(f) | 778 | _sethtent(int f) |
| 402 | int f; | ||
| 403 | { | 779 | { |
| 404 | if (hostf == NULL) | 780 | if (hostf == NULL) |
| 405 | hostf = fopen(_PATH_HOSTS, "r" ); | 781 | hostf = fopen(_PATH_HOSTS, "r" ); |
| @@ -409,7 +785,7 @@ _sethtent(f) | |||
| 409 | } | 785 | } |
| 410 | 786 | ||
| 411 | void | 787 | void |
| 412 | _endhtent() | 788 | _endhtent(void) |
| 413 | { | 789 | { |
| 414 | if (hostf && !stayopen) { | 790 | if (hostf && !stayopen) { |
| 415 | (void) fclose(hostf); | 791 | (void) fclose(hostf); |
| @@ -418,39 +794,65 @@ _endhtent() | |||
| 418 | } | 794 | } |
| 419 | 795 | ||
| 420 | struct hostent * | 796 | struct hostent * |
| 421 | _gethtent() | 797 | _gethtent(void) |
| 422 | { | 798 | { |
| 423 | char *p; | 799 | struct __res_state *_resp = _THREAD_PRIVATE(_res, _res, &_res); |
| 424 | register char *cp, **q; | 800 | char *p, *cp, **q; |
| 801 | int af; | ||
| 802 | size_t len; | ||
| 425 | 803 | ||
| 426 | if (hostf == NULL && (hostf = fopen(_PATH_HOSTS, "r" )) == NULL) | 804 | if (!hostf && !(hostf = fopen(_PATH_HOSTS, "r" ))) { |
| 805 | h_errno = NETDB_INTERNAL; | ||
| 427 | return (NULL); | 806 | return (NULL); |
| 428 | again: | 807 | } |
| 429 | if ((p = fgets(hostbuf, BUFSIZ, hostf)) == NULL) | 808 | again: |
| 809 | if ((p = fgetln(hostf, &len)) == NULL) { | ||
| 810 | h_errno = HOST_NOT_FOUND; | ||
| 430 | return (NULL); | 811 | return (NULL); |
| 812 | } | ||
| 813 | if (p[len-1] == '\n') | ||
| 814 | len--; | ||
| 815 | if (len >= sizeof(hostbuf) || len == 0) | ||
| 816 | goto again; | ||
| 817 | p = memcpy(hostbuf, p, len); | ||
| 818 | hostbuf[len] = '\0'; | ||
| 431 | if (*p == '#') | 819 | if (*p == '#') |
| 432 | goto again; | 820 | goto again; |
| 433 | cp = strpbrk(p, "#\n"); | 821 | if ((cp = strchr(p, '#'))) |
| 434 | if (cp == NULL) | 822 | *cp = '\0'; |
| 435 | goto again; | 823 | if (!(cp = strpbrk(p, " \t"))) |
| 436 | *cp = '\0'; | ||
| 437 | cp = strpbrk(p, " \t"); | ||
| 438 | if (cp == NULL) | ||
| 439 | goto again; | 824 | goto again; |
| 440 | *cp++ = '\0'; | 825 | *cp++ = '\0'; |
| 441 | /* THIS STUFF IS INTERNET SPECIFIC */ | 826 | if (inet_pton(AF_INET6, p, host_addr) > 0) { |
| 442 | h_addr_ptrs[0] = (char *)&host_addr; | 827 | af = AF_INET6; |
| 828 | len = IN6ADDRSZ; | ||
| 829 | } else if (inet_pton(AF_INET, p, host_addr) > 0) { | ||
| 830 | if (_resp->options & RES_USE_INET6) { | ||
| 831 | map_v4v6_address((char*)host_addr, (char*)host_addr); | ||
| 832 | af = AF_INET6; | ||
| 833 | len = IN6ADDRSZ; | ||
| 834 | } else { | ||
| 835 | af = AF_INET; | ||
| 836 | len = INADDRSZ; | ||
| 837 | } | ||
| 838 | } else { | ||
| 839 | goto again; | ||
| 840 | } | ||
| 841 | /* if this is not something we're looking for, skip it. */ | ||
| 842 | if (host.h_addrtype != af) | ||
| 843 | goto again; | ||
| 844 | if (host.h_length != len) | ||
| 845 | goto again; | ||
| 846 | h_addr_ptrs[0] = (char *)host_addr; | ||
| 443 | h_addr_ptrs[1] = NULL; | 847 | h_addr_ptrs[1] = NULL; |
| 444 | (void) inet_aton(p, &host_addr); | ||
| 445 | host.h_addr_list = h_addr_ptrs; | 848 | host.h_addr_list = h_addr_ptrs; |
| 446 | host.h_length = sizeof(u_int32_t); | 849 | host.h_length = len; |
| 447 | host.h_addrtype = AF_INET; | 850 | host.h_addrtype = af; |
| 448 | while (*cp == ' ' || *cp == '\t') | 851 | while (*cp == ' ' || *cp == '\t') |
| 449 | cp++; | 852 | cp++; |
| 450 | host.h_name = cp; | 853 | host.h_name = cp; |
| 451 | q = host.h_aliases = host_aliases; | 854 | q = host.h_aliases = host_aliases; |
| 452 | cp = strpbrk(cp, " \t"); | 855 | if ((cp = strpbrk(cp, " \t"))) |
| 453 | if (cp != NULL) | ||
| 454 | *cp++ = '\0'; | 856 | *cp++ = '\0'; |
| 455 | while (cp && *cp) { | 857 | while (cp && *cp) { |
| 456 | if (*cp == ' ' || *cp == '\t') { | 858 | if (*cp == ' ' || *cp == '\t') { |
| @@ -459,74 +861,75 @@ again: | |||
| 459 | } | 861 | } |
| 460 | if (q < &host_aliases[MAXALIASES - 1]) | 862 | if (q < &host_aliases[MAXALIASES - 1]) |
| 461 | *q++ = cp; | 863 | *q++ = cp; |
| 462 | cp = strpbrk(cp, " \t"); | 864 | if ((cp = strpbrk(cp, " \t"))) |
| 463 | if (cp != NULL) | ||
| 464 | *cp++ = '\0'; | 865 | *cp++ = '\0'; |
| 465 | } | 866 | } |
| 466 | *q = NULL; | 867 | *q = NULL; |
| 868 | if (_resp->options & RES_USE_INET6) { | ||
| 869 | char *bp = hostbuf; | ||
| 870 | char *ep = hostbuf + sizeof hostbuf; | ||
| 871 | |||
| 872 | map_v4v6_hostent(&host, &bp, ep); | ||
| 873 | } | ||
| 874 | h_errno = NETDB_SUCCESS; | ||
| 467 | return (&host); | 875 | return (&host); |
| 468 | } | 876 | } |
| 469 | 877 | ||
| 470 | struct hostent * | 878 | struct hostent * |
| 471 | _gethtbyname(name) | 879 | _gethtbyname(const char *name) |
| 472 | char *name; | ||
| 473 | { | 880 | { |
| 474 | register struct hostent *p; | 881 | struct __res_state *_resp = _THREAD_PRIVATE(_res, _res, &_res); |
| 475 | register char **cp; | 882 | struct hostent *hp; |
| 883 | extern struct hostent *_gethtbyname2(const char *, int); | ||
| 884 | |||
| 885 | if (_resp->options & RES_USE_INET6) { | ||
| 886 | hp = _gethtbyname2(name, AF_INET6); | ||
| 887 | if (hp) | ||
| 888 | return (hp); | ||
| 889 | } | ||
| 890 | return (_gethtbyname2(name, AF_INET)); | ||
| 891 | } | ||
| 892 | |||
| 893 | struct hostent * | ||
| 894 | _gethtbyname2(const char *name, int af) | ||
| 895 | { | ||
| 896 | struct hostent *p; | ||
| 897 | char **cp; | ||
| 476 | 898 | ||
| 477 | _sethtent(0); | 899 | _sethtent(0); |
| 478 | while (p = _gethtent()) { | 900 | while ((p = _gethtent())) { |
| 901 | if (p->h_addrtype != af) | ||
| 902 | continue; | ||
| 479 | if (strcasecmp(p->h_name, name) == 0) | 903 | if (strcasecmp(p->h_name, name) == 0) |
| 480 | break; | 904 | break; |
| 481 | for (cp = p->h_aliases; *cp != 0; cp++) | 905 | for (cp = p->h_aliases; *cp != 0; cp++) |
| 482 | if (strcasecmp(*cp, name) == 0) | 906 | if (strcasecmp(*cp, name) == 0) |
| 483 | goto found; | 907 | goto found; |
| 484 | } | 908 | } |
| 485 | found: | 909 | found: |
| 486 | _endhtent(); | 910 | _endhtent(); |
| 487 | if (p==NULL) | ||
| 488 | h_errno = HOST_NOT_FOUND; | ||
| 489 | return (p); | 911 | return (p); |
| 490 | } | 912 | } |
| 491 | 913 | ||
| 492 | struct hostent * | 914 | struct hostent * |
| 493 | _gethtbyaddr(addr, len, type) | 915 | _gethtbyaddr(const char *addr, int len, int af) |
| 494 | const char *addr; | ||
| 495 | int len, type; | ||
| 496 | { | 916 | { |
| 497 | register struct hostent *p; | 917 | struct hostent *p; |
| 918 | |||
| 919 | host.h_length = len; | ||
| 920 | host.h_addrtype = af; | ||
| 498 | 921 | ||
| 499 | _sethtent(0); | 922 | _sethtent(0); |
| 500 | while (p = _gethtent()) | 923 | while ((p = _gethtent())) |
| 501 | if (p->h_addrtype == type && !bcmp(p->h_addr, addr, len)) | 924 | if (p->h_addrtype == af && !bcmp(p->h_addr, addr, len)) |
| 502 | break; | 925 | break; |
| 503 | _endhtent(); | 926 | _endhtent(); |
| 504 | if (p==NULL) | ||
| 505 | h_errno = HOST_NOT_FOUND; | ||
| 506 | return (p); | 927 | return (p); |
| 507 | } | 928 | } |
| 508 | 929 | ||
| 509 | static int | ||
| 510 | qcomp(a1, a2) | ||
| 511 | struct in_addr **a1, **a2; | ||
| 512 | { | ||
| 513 | int pos1, pos2; | ||
| 514 | |||
| 515 | for (pos1 = 0; pos1 < _res.nsort; pos1++) | ||
| 516 | if (_res.sort_list[pos1].addr.s_addr == | ||
| 517 | ((*a1)->s_addr & _res.sort_list[pos1].mask)) | ||
| 518 | break; | ||
| 519 | for (pos2 = 0; pos2 < _res.nsort; pos2++) | ||
| 520 | if (_res.sort_list[pos2].addr.s_addr == | ||
| 521 | ((*a2)->s_addr & _res.sort_list[pos2].mask)) | ||
| 522 | break; | ||
| 523 | return pos1 - pos2; | ||
| 524 | } | ||
| 525 | |||
| 526 | #ifdef YP | 930 | #ifdef YP |
| 527 | struct hostent * | 931 | struct hostent * |
| 528 | _yphostent(line) | 932 | _yphostent(char *line) |
| 529 | char *line; | ||
| 530 | { | 933 | { |
| 531 | static struct in_addr host_addrs[MAXADDRS]; | 934 | static struct in_addr host_addrs[MAXADDRS]; |
| 532 | char *p = line; | 935 | char *p = line; |
| @@ -537,21 +940,21 @@ _yphostent(line) | |||
| 537 | 940 | ||
| 538 | host.h_name = NULL; | 941 | host.h_name = NULL; |
| 539 | host.h_addr_list = h_addr_ptrs; | 942 | host.h_addr_list = h_addr_ptrs; |
| 540 | host.h_length = sizeof(u_int32_t); | 943 | host.h_length = INADDRSZ; |
| 541 | host.h_addrtype = AF_INET; | 944 | host.h_addrtype = AF_INET; |
| 542 | hap = h_addr_ptrs; | 945 | hap = h_addr_ptrs; |
| 543 | buf = host_addrs; | 946 | buf = host_addrs; |
| 544 | q = host.h_aliases = host_aliases; | 947 | q = host.h_aliases = host_aliases; |
| 545 | 948 | ||
| 546 | nextline: | 949 | nextline: |
| 950 | /* check for host_addrs overflow */ | ||
| 951 | if (buf >= &host_addrs[sizeof(host_addrs) / sizeof(host_addrs[0])]) | ||
| 952 | goto done; | ||
| 953 | |||
| 547 | more = 0; | 954 | more = 0; |
| 548 | cp = strpbrk(p, " \t"); | 955 | cp = strpbrk(p, " \t"); |
| 549 | if (cp == NULL) { | 956 | if (cp == NULL) |
| 550 | if (host.h_name == NULL) | 957 | goto done; |
| 551 | return (NULL); | ||
| 552 | else | ||
| 553 | goto done; | ||
| 554 | } | ||
| 555 | *cp++ = '\0'; | 958 | *cp++ = '\0'; |
| 556 | 959 | ||
| 557 | *hap++ = (char *)buf; | 960 | *hap++ = (char *)buf; |
| @@ -592,30 +995,28 @@ nextline: | |||
| 592 | *cp++ = '\0'; | 995 | *cp++ = '\0'; |
| 593 | } | 996 | } |
| 594 | done: | 997 | done: |
| 998 | if (host.h_name == NULL) | ||
| 999 | return (NULL); | ||
| 595 | *q = NULL; | 1000 | *q = NULL; |
| 596 | *hap = NULL; | 1001 | *hap = NULL; |
| 597 | return (&host); | 1002 | return (&host); |
| 598 | } | 1003 | } |
| 599 | 1004 | ||
| 600 | struct hostent * | 1005 | struct hostent * |
| 601 | _yp_gethtbyaddr(addr, len, type) | 1006 | _yp_gethtbyaddr(const char *addr) |
| 602 | const char *addr; | ||
| 603 | int len, type; | ||
| 604 | { | 1007 | { |
| 605 | struct hostent *hp = (struct hostent *)NULL; | 1008 | struct hostent *hp = (struct hostent *)NULL; |
| 606 | static char *__ypcurrent; | 1009 | static char *__ypcurrent; |
| 607 | int __ypcurrentlen, r; | 1010 | int __ypcurrentlen, r; |
| 608 | char name[sizeof("xxx.xxx.xxx.xxx") + 1]; | 1011 | char name[sizeof("xxx.xxx.xxx.xxx")]; |
| 609 | 1012 | ||
| 610 | if (!__ypdomain) { | 1013 | if (!__ypdomain) { |
| 611 | if (_yp_check(&__ypdomain) == 0) | 1014 | if (_yp_check(&__ypdomain) == 0) |
| 612 | return (hp); | 1015 | return (hp); |
| 613 | } | 1016 | } |
| 614 | sprintf(name, "%u.%u.%u.%u", | 1017 | snprintf(name, sizeof name, "%u.%u.%u.%u", |
| 615 | ((unsigned)addr[0] & 0xff), | 1018 | ((unsigned)addr[0] & 0xff), ((unsigned)addr[1] & 0xff), |
| 616 | ((unsigned)addr[1] & 0xff), | 1019 | ((unsigned)addr[2] & 0xff), ((unsigned)addr[3] & 0xff)); |
| 617 | ((unsigned)addr[2] & 0xff), | ||
| 618 | ((unsigned)addr[3] & 0xff)); | ||
| 619 | if (__ypcurrent) | 1020 | if (__ypcurrent) |
| 620 | free(__ypcurrent); | 1021 | free(__ypcurrent); |
| 621 | __ypcurrent = NULL; | 1022 | __ypcurrent = NULL; |
| @@ -629,13 +1030,14 @@ _yp_gethtbyaddr(addr, len, type) | |||
| 629 | } | 1030 | } |
| 630 | 1031 | ||
| 631 | struct hostent * | 1032 | struct hostent * |
| 632 | _yp_gethtbyname(name) | 1033 | _yp_gethtbyname(const char *name) |
| 633 | const char *name; | ||
| 634 | { | 1034 | { |
| 635 | struct hostent *hp = (struct hostent *)NULL; | 1035 | struct hostent *hp = (struct hostent *)NULL; |
| 636 | static char *__ypcurrent; | 1036 | static char *__ypcurrent; |
| 637 | int __ypcurrentlen, r; | 1037 | int __ypcurrentlen, r; |
| 638 | 1038 | ||
| 1039 | if (strlen(name) >= MAXHOSTNAMELEN) | ||
| 1040 | return (NULL); | ||
| 639 | if (!__ypdomain) { | 1041 | if (!__ypdomain) { |
| 640 | if (_yp_check(&__ypdomain) == 0) | 1042 | if (_yp_check(&__ypdomain) == 0) |
| 641 | return (hp); | 1043 | return (hp); |
| @@ -645,10 +1047,102 @@ _yp_gethtbyname(name) | |||
| 645 | __ypcurrent = NULL; | 1047 | __ypcurrent = NULL; |
| 646 | r = yp_match(__ypdomain, "hosts.byname", name, | 1048 | r = yp_match(__ypdomain, "hosts.byname", name, |
| 647 | strlen(name), &__ypcurrent, &__ypcurrentlen); | 1049 | strlen(name), &__ypcurrent, &__ypcurrentlen); |
| 648 | if (r==0) | 1050 | if (r == 0) |
| 649 | hp = _yphostent(__ypcurrent); | 1051 | hp = _yphostent(__ypcurrent); |
| 650 | if (hp==NULL) | 1052 | if (hp == NULL) |
| 651 | h_errno = HOST_NOT_FOUND; | 1053 | h_errno = HOST_NOT_FOUND; |
| 652 | return (hp); | 1054 | return (hp); |
| 653 | } | 1055 | } |
| 654 | #endif | 1056 | #endif |
| 1057 | |||
| 1058 | static void | ||
| 1059 | map_v4v6_address(const char *src, char *dst) | ||
| 1060 | { | ||
| 1061 | u_char *p = (u_char *)dst; | ||
| 1062 | char tmp[INADDRSZ]; | ||
| 1063 | int i; | ||
| 1064 | |||
| 1065 | /* Stash a temporary copy so our caller can update in place. */ | ||
| 1066 | bcopy(src, tmp, INADDRSZ); | ||
| 1067 | /* Mark this ipv6 addr as a mapped ipv4. */ | ||
| 1068 | for (i = 0; i < 10; i++) | ||
| 1069 | *p++ = 0x00; | ||
| 1070 | *p++ = 0xff; | ||
| 1071 | *p++ = 0xff; | ||
| 1072 | /* Retrieve the saved copy and we're done. */ | ||
| 1073 | bcopy(tmp, (void*)p, INADDRSZ); | ||
| 1074 | } | ||
| 1075 | |||
| 1076 | static void | ||
| 1077 | map_v4v6_hostent(struct hostent *hp, char **bpp, char *ep) | ||
| 1078 | { | ||
| 1079 | char **ap; | ||
| 1080 | |||
| 1081 | if (hp->h_addrtype != AF_INET || hp->h_length != INADDRSZ) | ||
| 1082 | return; | ||
| 1083 | hp->h_addrtype = AF_INET6; | ||
| 1084 | hp->h_length = IN6ADDRSZ; | ||
| 1085 | for (ap = hp->h_addr_list; *ap; ap++) { | ||
| 1086 | int i = sizeof(align) - ((u_long)*bpp % sizeof(align)); | ||
| 1087 | |||
| 1088 | if (ep - *bpp < (i + IN6ADDRSZ)) { | ||
| 1089 | /* Out of memory. Truncate address list here. XXX */ | ||
| 1090 | *ap = NULL; | ||
| 1091 | return; | ||
| 1092 | } | ||
| 1093 | *bpp += i; | ||
| 1094 | map_v4v6_address(*ap, *bpp); | ||
| 1095 | *ap = *bpp; | ||
| 1096 | *bpp += IN6ADDRSZ; | ||
| 1097 | } | ||
| 1098 | } | ||
| 1099 | |||
| 1100 | struct hostent * | ||
| 1101 | gethostent(void) | ||
| 1102 | { | ||
| 1103 | return (_gethtent()); | ||
| 1104 | } | ||
| 1105 | |||
| 1106 | #ifdef RESOLVSORT | ||
| 1107 | static void | ||
| 1108 | addrsort(char **ap, int num) | ||
| 1109 | { | ||
| 1110 | struct __res_state *_resp = _THREAD_PRIVATE(_res, _res, &_res); | ||
| 1111 | int i, j; | ||
| 1112 | char **p; | ||
| 1113 | short aval[MAXADDRS]; | ||
| 1114 | int needsort = 0; | ||
| 1115 | |||
| 1116 | p = ap; | ||
| 1117 | for (i = 0; i < num; i++, p++) { | ||
| 1118 | for (j = 0 ; (unsigned)j < _resp->nsort; j++) | ||
| 1119 | if (_resp->sort_list[j].addr.s_addr == | ||
| 1120 | (((struct in_addr *)(*p))->s_addr & | ||
| 1121 | _resp->sort_list[j].mask)) | ||
| 1122 | break; | ||
| 1123 | aval[i] = j; | ||
| 1124 | if (needsort == 0 && i > 0 && j < aval[i-1]) | ||
| 1125 | needsort = i; | ||
| 1126 | } | ||
| 1127 | if (!needsort) | ||
| 1128 | return; | ||
| 1129 | |||
| 1130 | while (needsort < num) { | ||
| 1131 | for (j = needsort - 1; j >= 0; j--) { | ||
| 1132 | if (aval[j] > aval[j+1]) { | ||
| 1133 | char *hp; | ||
| 1134 | |||
| 1135 | i = aval[j]; | ||
| 1136 | aval[j] = aval[j+1]; | ||
| 1137 | aval[j+1] = i; | ||
| 1138 | |||
| 1139 | hp = ap[j]; | ||
| 1140 | ap[j] = ap[j+1]; | ||
| 1141 | ap[j+1] = hp; | ||
| 1142 | } else | ||
| 1143 | break; | ||
| 1144 | } | ||
| 1145 | needsort++; | ||
| 1146 | } | ||
| 1147 | } | ||
| 1148 | #endif | ||
