]> git.ipfire.org Git - thirdparty/glibc.git/blame - resolv/compat-gethnamaddr.c
iconv, localedef: avoid floating point rounding differences [BZ #24372]
[thirdparty/glibc.git] / resolv / compat-gethnamaddr.c
CommitLineData
28f540f4
RM
1/*
2 * ++Copyright++ 1985, 1988, 1993
3 * -
4 * Copyright (c) 1985, 1988, 1993
5 * The Regents of the University of California. All rights reserved.
fa0bc87c 6 *
28f540f4
RM
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
28f540f4
RM
15 * 4. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
fa0bc87c 18 *
28f540f4
RM
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 * -
31 * Portions Copyright (c) 1993 by Digital Equipment Corporation.
fa0bc87c 32 *
28f540f4
RM
33 * Permission to use, copy, modify, and distribute this software for any
34 * purpose with or without fee is hereby granted, provided that the above
35 * copyright notice and this permission notice appear in all copies, and that
36 * the name of Digital Equipment Corporation not be used in advertising or
37 * publicity pertaining to distribution of the document or software without
38 * specific, written prior permission.
fa0bc87c 39 *
28f540f4
RM
40 * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL
41 * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES
42 * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL DIGITAL EQUIPMENT
43 * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
44 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
45 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
46 * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
47 * SOFTWARE.
48 * -
49 * --Copyright--
50 */
51
e1e041c4
SP
52/* XXX This file is not used by any of the resolver functions implemented by
53 glibc (i.e. get*info and gethostby*). It cannot be removed however because
54 it exports symbols in the libresolv ABI. The file is not maintained any
55 more, nor are these functions. */
56
e7eceec0
FW
57#include <shlib-compat.h>
58#if SHLIB_COMPAT (libresolv, GLIBC_2_0, GLIBC_2_25)
59
60# include <sys/types.h>
61# include <sys/param.h>
62# include <sys/socket.h>
63# include <netinet/in.h>
64# include <arpa/inet.h>
65# include <arpa/nameser.h>
66
67# include <stdio.h>
68# include <netdb.h>
b76e0659 69# include <resolv/resolv-internal.h>
352f4ff9 70# include <resolv/resolv_context.h>
e7eceec0
FW
71# include <ctype.h>
72# include <errno.h>
73# include <stdlib.h>
74# include <string.h>
75
76# define MAXALIASES 35
77# define MAXADDRS 35
28f540f4 78
28f540f4
RM
79static char *h_addr_ptrs[MAXADDRS + 1];
80
81static struct hostent host;
82static char *host_aliases[MAXALIASES];
5f0e6fc7 83static char hostbuf[8*1024];
fa0bc87c 84static u_char host_addr[16]; /* IPv4 or IPv6 */
28f540f4
RM
85static FILE *hostf = NULL;
86static int stayopen = 0;
87
352f4ff9
FW
88static struct hostent *res_gethostbyname2_context (struct resolv_context *,
89 const char *name, int af);
90
79937577
UD
91static void map_v4v6_address (const char *src, char *dst) __THROW;
92static void map_v4v6_hostent (struct hostent *hp, char **bp, int *len) __THROW;
fa0bc87c 93
79937577 94extern void addrsort (char **, int) __THROW;
28f540f4 95
e7eceec0
FW
96# if PACKETSZ > 65536
97# define MAXPACKET PACKETSZ
98# else
99# define MAXPACKET 65536
100# endif
28f540f4 101
1522c368 102/* As per RFC 1034 and 1035 a host name cannot exceed 255 octets in length. */
e7eceec0
FW
103# ifdef MAXHOSTNAMELEN
104# undef MAXHOSTNAMELEN
105# endif
106# define MAXHOSTNAMELEN 256
1522c368 107
28f540f4
RM
108typedef union {
109 HEADER hdr;
110 u_char buf[MAXPACKET];
111} querybuf;
112
113typedef union {
114 int32_t al;
115 char ac;
116} align;
117
e7eceec0 118# ifndef h_errno
28f540f4 119extern int h_errno;
e7eceec0 120# endif
28f540f4 121
e7eceec0 122# define BOUNDED_INCR(x) \
66715f83
UD
123 do { \
124 cp += x; \
125 if (cp > eom) { \
126 __set_h_errno (NO_RECOVERY); \
127 return (NULL); \
128 } \
129 } while (0)
130
e7eceec0 131# define BOUNDS_CHECK(ptr, count) \
66715f83
UD
132 do { \
133 if ((ptr) + (count) > eom) { \
134 __set_h_errno (NO_RECOVERY); \
135 return (NULL); \
136 } \
137 } while (0)
138
139
28f540f4 140static struct hostent *
db0a00d3 141getanswer (const querybuf *answer, int anslen, const char *qname, int qtype)
28f540f4 142{
2e09a79a
JM
143 const HEADER *hp;
144 const u_char *cp;
145 int n;
66715f83 146 const u_char *eom, *erdata;
28f540f4
RM
147 char *bp, **ap, **hap;
148 int type, class, buflen, ancount, qdcount;
149 int haveanswer, had_error;
845dcb57 150 char tbuf[MAXDNAME];
3cf595e5 151 const char *tname;
0cf12a6a 152 int (*name_ok) (const char *);
28f540f4 153
3cf595e5 154 tname = qname;
28f540f4
RM
155 host.h_name = NULL;
156 eom = answer->buf + anslen;
55707265
RM
157 switch (qtype) {
158 case T_A:
fa0bc87c 159 case T_AAAA:
55707265
RM
160 name_ok = res_hnok;
161 break;
162 case T_PTR:
fa0bc87c 163 name_ok = res_dnok;
55707265
RM
164 break;
165 default:
fa0bc87c 166 return (NULL); /* XXX should be abort(); */
55707265 167 }
28f540f4
RM
168 /*
169 * find first satisfactory answer
170 */
171 hp = &answer->hdr;
172 ancount = ntohs(hp->ancount);
173 qdcount = ntohs(hp->qdcount);
174 bp = hostbuf;
175 buflen = sizeof hostbuf;
66715f83
UD
176 cp = answer->buf;
177 BOUNDED_INCR(HFIXEDSZ);
28f540f4 178 if (qdcount != 1) {
a68b0d31 179 __set_h_errno (NO_RECOVERY);
28f540f4
RM
180 return (NULL);
181 }
55707265
RM
182 n = dn_expand(answer->buf, eom, cp, bp, buflen);
183 if ((n < 0) || !(*name_ok)(bp)) {
a68b0d31 184 __set_h_errno (NO_RECOVERY);
28f540f4
RM
185 return (NULL);
186 }
66715f83 187 BOUNDED_INCR(n + QFIXEDSZ);
fa0bc87c 188 if (qtype == T_A || qtype == T_AAAA) {
28f540f4
RM
189 /* res_send() has already verified that the query name is the
190 * same as the one we sent; this just gets the expanded name
191 * (i.e., with the succeeding search-domain tacked on).
192 */
193 n = strlen(bp) + 1; /* for the \0 */
76b87c03
UD
194 if (n >= MAXHOSTNAMELEN) {
195 __set_h_errno (NO_RECOVERY);
196 return (NULL);
197 }
28f540f4
RM
198 host.h_name = bp;
199 bp += n;
200 buflen -= n;
201 /* The qname can be abbreviated, but h_name is now absolute. */
202 qname = host.h_name;
203 }
204 ap = host_aliases;
205 *ap = NULL;
206 host.h_aliases = host_aliases;
207 hap = h_addr_ptrs;
208 *hap = NULL;
28f540f4 209 host.h_addr_list = h_addr_ptrs;
28f540f4
RM
210 haveanswer = 0;
211 had_error = 0;
212 while (ancount-- > 0 && cp < eom && !had_error) {
213 n = dn_expand(answer->buf, eom, cp, bp, buflen);
55707265 214 if ((n < 0) || !(*name_ok)(bp)) {
28f540f4
RM
215 had_error++;
216 continue;
217 }
218 cp += n; /* name */
66715f83 219 BOUNDS_CHECK(cp, 3 * INT16SZ + INT32SZ);
b43b13ac 220 type = ns_get16(cp);
16b66062 221 cp += INT16SZ; /* type */
b43b13ac 222 class = ns_get16(cp);
16b66062 223 cp += INT16SZ + INT32SZ; /* class, TTL */
b43b13ac 224 n = ns_get16(cp);
28f540f4 225 cp += INT16SZ; /* len */
66715f83
UD
226 BOUNDS_CHECK(cp, n);
227 erdata = cp + n;
fa0bc87c 228 if (class != C_IN) {
28f540f4
RM
229 /* XXX - debug? syslog? */
230 cp += n;
231 continue; /* XXX - had_error++ ? */
232 }
fa0bc87c 233 if ((qtype == T_A || qtype == T_AAAA) && type == T_CNAME) {
28f540f4
RM
234 if (ap >= &host_aliases[MAXALIASES-1])
235 continue;
236 n = dn_expand(answer->buf, eom, cp, tbuf, sizeof tbuf);
55707265 237 if ((n < 0) || !(*name_ok)(tbuf)) {
28f540f4
RM
238 had_error++;
239 continue;
240 }
241 cp += n;
66715f83
UD
242 if (cp != erdata) {
243 __set_h_errno (NO_RECOVERY);
244 return (NULL);
245 }
28f540f4
RM
246 /* Store alias. */
247 *ap++ = bp;
248 n = strlen(bp) + 1; /* for the \0 */
76b87c03
UD
249 if (n >= MAXHOSTNAMELEN) {
250 had_error++;
251 continue;
252 }
28f540f4
RM
253 bp += n;
254 buflen -= n;
255 /* Get canonical name. */
256 n = strlen(tbuf) + 1; /* for the \0 */
76b87c03 257 if (n > buflen || n >= MAXHOSTNAMELEN) {
28f540f4
RM
258 had_error++;
259 continue;
260 }
261 strcpy(bp, tbuf);
262 host.h_name = bp;
263 bp += n;
264 buflen -= n;
265 continue;
266 }
3cf595e5
RM
267 if (qtype == T_PTR && type == T_CNAME) {
268 n = dn_expand(answer->buf, eom, cp, tbuf, sizeof tbuf);
da2d1bc5 269 if (n < 0 || !res_dnok(tbuf)) {
3cf595e5
RM
270 had_error++;
271 continue;
272 }
273 cp += n;
66715f83
UD
274 if (cp != erdata) {
275 __set_h_errno (NO_RECOVERY);
276 return (NULL);
277 }
3cf595e5
RM
278 /* Get canonical name. */
279 n = strlen(tbuf) + 1; /* for the \0 */
76b87c03 280 if (n > buflen || n >= MAXHOSTNAMELEN) {
3cf595e5
RM
281 had_error++;
282 continue;
283 }
284 strcpy(bp, tbuf);
285 tname = bp;
286 bp += n;
287 buflen -= n;
288 continue;
289 }
28f540f4 290 if (type != qtype) {
3e3002ff
SP
291 /* Log a low priority message if we get an unexpected
292 * record, but skip it if we are using DNSSEC since it
293 * uses many different types in responses that do not
294 * match QTYPE.
295 */
28f540f4
RM
296 cp += n;
297 continue; /* XXX - had_error++ ? */
298 }
299 switch (type) {
300 case T_PTR:
3cf595e5 301 if (strcasecmp(tname, bp) != 0) {
28f540f4
RM
302 cp += n;
303 continue; /* XXX - had_error++ ? */
304 }
305 n = dn_expand(answer->buf, eom, cp, bp, buflen);
55707265 306 if ((n < 0) || !res_hnok(bp)) {
28f540f4
RM
307 had_error++;
308 break;
309 }
28f540f4 310 cp += n;
66715f83
UD
311 if (cp != erdata) {
312 __set_h_errno (NO_RECOVERY);
313 return (NULL);
314 }
28f540f4
RM
315 if (!haveanswer)
316 host.h_name = bp;
317 else if (ap < &host_aliases[MAXALIASES-1])
318 *ap++ = bp;
319 else
320 n = -1;
321 if (n != -1) {
322 n = strlen(bp) + 1; /* for the \0 */
76b87c03
UD
323 if (n >= MAXHOSTNAMELEN) {
324 had_error++;
325 break;
326 }
28f540f4
RM
327 bp += n;
328 buflen -= n;
329 }
330 break;
28f540f4 331 case T_A:
fa0bc87c 332 case T_AAAA:
28f540f4 333 if (strcasecmp(host.h_name, bp) != 0) {
28f540f4
RM
334 cp += n;
335 continue; /* XXX - had_error++ ? */
336 }
1f64ac13
UD
337 if (n != host.h_length) {
338 cp += n;
339 continue;
340 }
341 if (!haveanswer) {
2e09a79a 342 int nn;
28f540f4 343
28f540f4
RM
344 host.h_name = bp;
345 nn = strlen(bp) + 1; /* for the \0 */
346 bp += nn;
347 buflen -= nn;
348 }
349
31a13ab3
UD
350 /* XXX: when incrementing bp, we have to decrement
351 * buflen by the same amount --okir */
352 buflen -= sizeof(align) - ((u_long)bp % sizeof(align));
353
28f540f4
RM
354 bp += sizeof(align) - ((u_long)bp % sizeof(align));
355
356 if (bp + n >= &hostbuf[sizeof hostbuf]) {
28f540f4
RM
357 had_error++;
358 continue;
359 }
360 if (hap >= &h_addr_ptrs[MAXADDRS-1]) {
28f540f4
RM
361 cp += n;
362 continue;
363 }
9596d0dd 364 memmove(*hap++ = bp, cp, n);
28f540f4 365 bp += n;
df21c858 366 buflen -= n;
28f540f4 367 cp += n;
66715f83
UD
368 if (cp != erdata) {
369 __set_h_errno (NO_RECOVERY);
370 return (NULL);
371 }
28f540f4
RM
372 break;
373 default:
fa0bc87c
RM
374 abort();
375 }
28f540f4
RM
376 if (!had_error)
377 haveanswer++;
fa0bc87c 378 }
28f540f4
RM
379 if (haveanswer) {
380 *ap = NULL;
381 *hap = NULL;
28f540f4
RM
382 /*
383 * Note: we sort even if host can take only one address
384 * in its return structures - should give it the "best"
385 * address in that case, not some random one
386 */
fa0bc87c 387 if (_res.nsort && haveanswer > 1 && qtype == T_A)
28f540f4 388 addrsort(h_addr_ptrs, haveanswer);
28f540f4
RM
389 if (!host.h_name) {
390 n = strlen(qname) + 1; /* for the \0 */
76b87c03
UD
391 if (n > buflen || n >= MAXHOSTNAMELEN)
392 goto no_recovery;
28f540f4
RM
393 strcpy(bp, qname);
394 host.h_name = bp;
fa0bc87c
RM
395 bp += n;
396 buflen -= n;
28f540f4 397 }
b76e0659 398 if (res_use_inet6 ())
fa0bc87c 399 map_v4v6_hostent(&host, &bp, &buflen);
a68b0d31 400 __set_h_errno (NETDB_SUCCESS);
28f540f4 401 return (&host);
28f540f4 402 }
76b87c03
UD
403 no_recovery:
404 __set_h_errno (NO_RECOVERY);
fa0bc87c 405 return (NULL);
28f540f4
RM
406}
407
e7eceec0
FW
408extern struct hostent *res_gethostbyname2(const char *name, int af);
409libresolv_hidden_proto (res_gethostbyname2)
6f9d8e68 410
28f540f4 411struct hostent *
e7eceec0 412res_gethostbyname (const char *name)
3cf595e5 413{
352f4ff9
FW
414 struct resolv_context *ctx = __resolv_context_get ();
415 if (ctx == NULL)
416 {
417 __set_h_errno (NETDB_INTERNAL);
418 return NULL;
419 }
420
421 if (res_use_inet6 ())
422 {
423 struct hostent *hp = res_gethostbyname2_context (ctx, name, AF_INET6);
424 if (hp != NULL)
425 {
426 __resolv_context_put (ctx);
427 return hp;
3cf595e5 428 }
352f4ff9
FW
429 }
430 struct hostent *hp = res_gethostbyname2_context (ctx, name, AF_INET);
431 __resolv_context_put (ctx);
432 return hp;
3cf595e5 433}
e7eceec0 434compat_symbol (libresolv, res_gethostbyname, res_gethostbyname, GLIBC_2_0);
3cf595e5 435
352f4ff9
FW
436static struct hostent *
437res_gethostbyname2_context (struct resolv_context *ctx,
438 const char *name, int af)
28f540f4 439{
16b66062
AJ
440 union
441 {
442 querybuf *buf;
443 u_char *ptr;
444 } buf;
445 querybuf *origbuf;
2e09a79a 446 const char *cp;
fa0bc87c 447 char *bp;
0420d888 448 int n, size, type, len;
6166815d 449 struct hostent *ret;
28f540f4 450
fa0bc87c
RM
451 switch (af) {
452 case AF_INET:
453 size = INADDRSZ;
454 type = T_A;
455 break;
456 case AF_INET6:
457 size = IN6ADDRSZ;
458 type = T_AAAA;
459 break;
460 default:
a68b0d31 461 __set_h_errno (NETDB_INTERNAL);
ba1ffaa1 462 __set_errno (EAFNOSUPPORT);
fa0bc87c
RM
463 return (NULL);
464 }
465
466 host.h_addrtype = af;
467 host.h_length = size;
468
28f540f4
RM
469 /*
470 * if there aren't any dots, it could be a user-level alias.
471 * this is also done in res_query() since we are not the only
472 * function that looks up host names.
473 */
352f4ff9
FW
474 char abuf[MAXDNAME];
475 if (strchr (name, '.') != NULL
476 && (cp = __res_context_hostalias (ctx, name, abuf, sizeof (abuf))))
477 name = cp;
28f540f4
RM
478
479 /*
480 * disallow names consisting only of digits/dots, unless
481 * they end in a dot.
482 */
483 if (isdigit(name[0]))
484 for (cp = name;; ++cp) {
485 if (!*cp) {
486 if (*--cp == '.')
487 break;
488 /*
489 * All-numeric, no dot at the end.
490 * Fake up a hostent as if we'd actually
491 * done a lookup.
492 */
613a76ff 493 if (inet_pton(af, name, host_addr) <= 0) {
a68b0d31 494 __set_h_errno (HOST_NOT_FOUND);
28f540f4
RM
495 return (NULL);
496 }
3d61b63c
RM
497 strncpy(hostbuf, name, MAXDNAME);
498 hostbuf[MAXDNAME] = '\0';
fa0bc87c
RM
499 bp = hostbuf + MAXDNAME;
500 len = sizeof hostbuf - MAXDNAME;
3d61b63c 501 host.h_name = hostbuf;
28f540f4
RM
502 host.h_aliases = host_aliases;
503 host_aliases[0] = NULL;
613a76ff 504 h_addr_ptrs[0] = (char *)host_addr;
28f540f4 505 h_addr_ptrs[1] = NULL;
28f540f4 506 host.h_addr_list = h_addr_ptrs;
b76e0659 507 if (res_use_inet6 ())
fa0bc87c 508 map_v4v6_hostent(&host, &bp, &len);
a68b0d31 509 __set_h_errno (NETDB_SUCCESS);
28f540f4
RM
510 return (&host);
511 }
fa0bc87c 512 if (!isdigit(*cp) && *cp != '.')
28f540f4 513 break;
312be3f9 514 }
76b87c03
UD
515 if ((isxdigit(name[0]) && strchr(name, ':') != NULL) ||
516 name[0] == ':')
845dcb57
UD
517 for (cp = name;; ++cp) {
518 if (!*cp) {
519 if (*--cp == '.')
520 break;
521 /*
522 * All-IPv6-legal, no dot at the end.
523 * Fake up a hostent as if we'd actually
524 * done a lookup.
525 */
526 if (inet_pton(af, name, host_addr) <= 0) {
a68b0d31 527 __set_h_errno (HOST_NOT_FOUND);
845dcb57
UD
528 return (NULL);
529 }
530 strncpy(hostbuf, name, MAXDNAME);
531 hostbuf[MAXDNAME] = '\0';
532 bp = hostbuf + MAXDNAME;
533 len = sizeof hostbuf - MAXDNAME;
534 host.h_name = hostbuf;
535 host.h_aliases = host_aliases;
536 host_aliases[0] = NULL;
537 h_addr_ptrs[0] = (char *)host_addr;
538 h_addr_ptrs[1] = NULL;
539 host.h_addr_list = h_addr_ptrs;
a68b0d31 540 __set_h_errno (NETDB_SUCCESS);
845dcb57
UD
541 return (&host);
542 }
543 if (!isxdigit(*cp) && *cp != ':' && *cp != '.')
544 break;
28f540f4
RM
545 }
546
16b66062 547 buf.buf = origbuf = (querybuf *) alloca (1024);
6166815d 548
352f4ff9
FW
549 if ((n = __res_context_search
550 (ctx, name, C_IN, type, buf.buf->buf, 1024,
551 &buf.ptr, NULL, NULL, NULL, NULL)) < 0) {
16b66062
AJ
552 if (buf.buf != origbuf)
553 free (buf.buf);
5f0e6fc7
RM
554 if (errno == ECONNREFUSED)
555 return (_gethtbyname2(name, af));
556 return (NULL);
28f540f4 557 }
16b66062
AJ
558 ret = getanswer(buf.buf, n, name, type);
559 if (buf.buf != origbuf)
560 free (buf.buf);
6166815d 561 return ret;
28f540f4 562}
352f4ff9
FW
563
564struct hostent *
565res_gethostbyname2 (const char *name, int af)
566{
567 struct resolv_context *ctx = __resolv_context_get ();
568 if (ctx == NULL)
569 {
570 __set_h_errno (NETDB_INTERNAL);
571 return NULL;
572 }
573 struct hostent *hp = res_gethostbyname2_context (ctx, name, AF_INET);
574 __resolv_context_put (ctx);
575 return hp;
576}
e7eceec0
FW
577libresolv_hidden_def (res_gethostbyname2)
578compat_symbol (libresolv, res_gethostbyname2, res_gethostbyname2, GLIBC_2_0);
28f540f4 579
352f4ff9
FW
580static struct hostent *
581res_gethostbyaddr_context (struct resolv_context *ctx,
582 const void *addr, socklen_t len, int af)
28f540f4 583{
fa0bc87c
RM
584 const u_char *uaddr = (const u_char *)addr;
585 static const u_char mapped[] = { 0,0, 0,0, 0,0, 0,0, 0,0, 0xff,0xff };
586 static const u_char tunnelled[] = { 0,0, 0,0, 0,0, 0,0, 0,0, 0,0 };
0420d888 587 int n;
160bb409 588 socklen_t size;
16b66062
AJ
589 union
590 {
591 querybuf *buf;
592 u_char *ptr;
593 } buf;
594 querybuf *orig_buf;
2e09a79a 595 struct hostent *hp;
3db04c6f 596 char qbuf[MAXDNAME+1], *qp = NULL;
fa0bc87c 597
fa0bc87c 598 if (af == AF_INET6 && len == IN6ADDRSZ &&
1c6e6f23
RM
599 (!memcmp(uaddr, mapped, sizeof mapped) ||
600 !memcmp(uaddr, tunnelled, sizeof tunnelled))) {
fa0bc87c
RM
601 /* Unmap. */
602 addr += sizeof mapped;
603 uaddr += sizeof mapped;
604 af = AF_INET;
605 len = INADDRSZ;
606 }
607 switch (af) {
608 case AF_INET:
609 size = INADDRSZ;
610 break;
611 case AF_INET6:
612 size = IN6ADDRSZ;
613 break;
614 default:
a68b0d31
UD
615 __set_errno (EAFNOSUPPORT);
616 __set_h_errno (NETDB_INTERNAL);
28f540f4
RM
617 return (NULL);
618 }
fa0bc87c 619 if (size != len) {
a68b0d31
UD
620 __set_errno (EINVAL);
621 __set_h_errno (NETDB_INTERNAL);
28f540f4
RM
622 return (NULL);
623 }
5f0e6fc7
RM
624 switch (af) {
625 case AF_INET:
626 (void) sprintf(qbuf, "%u.%u.%u.%u.in-addr.arpa",
627 (uaddr[3] & 0xff),
628 (uaddr[2] & 0xff),
629 (uaddr[1] & 0xff),
630 (uaddr[0] & 0xff));
631 break;
632 case AF_INET6:
633 qp = qbuf;
634 for (n = IN6ADDRSZ - 1; n >= 0; n--) {
e01eef67
FW
635 qp += sprintf(qp, "%x.%x.",
636 uaddr[n] & 0xf,
637 (uaddr[n] >> 4) & 0xf);
fa0bc87c 638 }
ca6c7389 639 strcpy(qp, "ip6.arpa");
5f0e6fc7
RM
640 break;
641 default:
642 abort();
643 }
6166815d 644
16b66062 645 buf.buf = orig_buf = (querybuf *) alloca (1024);
6166815d 646
352f4ff9
FW
647 n = __res_context_query (ctx, qbuf, C_IN, T_PTR, buf.buf->buf, 1024,
648 &buf.ptr, NULL, NULL, NULL, NULL);
5f0e6fc7 649 if (n < 0) {
16b66062
AJ
650 if (buf.buf != orig_buf)
651 free (buf.buf);
5f0e6fc7
RM
652 if (errno == ECONNREFUSED)
653 return (_gethtbyaddr(addr, len, af));
654 return (NULL);
28f540f4 655 }
16b66062
AJ
656 hp = getanswer(buf.buf, n, qbuf, T_PTR);
657 if (buf.buf != orig_buf)
658 free (buf.buf);
6166815d 659 if (!hp)
5f0e6fc7 660 return (NULL); /* h_errno was set by getanswer() */
5f0e6fc7
RM
661 hp->h_addrtype = af;
662 hp->h_length = len;
9596d0dd 663 memmove(host_addr, addr, len);
5f0e6fc7
RM
664 h_addr_ptrs[0] = (char *)host_addr;
665 h_addr_ptrs[1] = NULL;
b76e0659 666 if (af == AF_INET && res_use_inet6 ()) {
5f0e6fc7
RM
667 map_v4v6_address((char*)host_addr, (char*)host_addr);
668 hp->h_addrtype = AF_INET6;
669 hp->h_length = IN6ADDRSZ;
670 }
a68b0d31 671 __set_h_errno (NETDB_SUCCESS);
5f0e6fc7 672 return (hp);
28f540f4 673}
352f4ff9
FW
674
675struct hostent *
676res_gethostbyaddr (const void *addr, socklen_t len, int af)
677{
678 struct resolv_context *ctx = __resolv_context_get ();
679 if (ctx == NULL)
680 {
681 __set_h_errno (NETDB_INTERNAL);
682 return NULL;
683 }
684 struct hostent *hp = res_gethostbyaddr_context (ctx, addr, len, af);
685 __resolv_context_put (ctx);
686 return hp;
687}
e7eceec0 688compat_symbol (libresolv, res_gethostbyaddr, res_gethostbyaddr, GLIBC_2_0);
28f540f4
RM
689
690void
9d46370c 691_sethtent (int f)
28f540f4
RM
692{
693 if (!hostf)
312be3f9 694 hostf = fopen(_PATH_HOSTS, "rce" );
28f540f4
RM
695 else
696 rewind(hostf);
697 stayopen = f;
698}
6f9d8e68 699libresolv_hidden_def (_sethtent)
e7eceec0 700compat_symbol (libresolv, _sethtent, _sethtent, GLIBC_2_0);
28f540f4 701
e7eceec0 702static void
60d2f8f3 703_endhtent (void)
28f540f4
RM
704{
705 if (hostf && !stayopen) {
706 (void) fclose(hostf);
707 hostf = NULL;
708 }
709}
710
711struct hostent *
60d2f8f3 712_gethtent (void)
28f540f4
RM
713{
714 char *p;
2e09a79a 715 char *cp, **q;
fa0bc87c 716 int af, len;
28f540f4 717
312be3f9 718 if (!hostf && !(hostf = fopen(_PATH_HOSTS, "rce" ))) {
a68b0d31 719 __set_h_errno (NETDB_INTERNAL);
28f540f4
RM
720 return (NULL);
721 }
fa0bc87c 722 again:
28f540f4 723 if (!(p = fgets(hostbuf, sizeof hostbuf, hostf))) {
a68b0d31 724 __set_h_errno (HOST_NOT_FOUND);
28f540f4
RM
725 return (NULL);
726 }
727 if (*p == '#')
728 goto again;
729 if (!(cp = strpbrk(p, "#\n")))
730 goto again;
731 *cp = '\0';
732 if (!(cp = strpbrk(p, " \t")))
733 goto again;
734 *cp++ = '\0';
76b87c03 735 if (inet_pton(AF_INET6, p, host_addr) > 0) {
fa0bc87c
RM
736 af = AF_INET6;
737 len = IN6ADDRSZ;
613a76ff 738 } else if (inet_pton(AF_INET, p, host_addr) > 0) {
b76e0659 739 if (res_use_inet6 ()) {
613a76ff 740 map_v4v6_address((char*)host_addr, (char*)host_addr);
fa0bc87c
RM
741 af = AF_INET6;
742 len = IN6ADDRSZ;
743 } else {
744 af = AF_INET;
745 len = INADDRSZ;
746 }
747 } else {
28f540f4 748 goto again;
fa0bc87c 749 }
613a76ff 750 h_addr_ptrs[0] = (char *)host_addr;
28f540f4 751 h_addr_ptrs[1] = NULL;
28f540f4 752 host.h_addr_list = h_addr_ptrs;
fa0bc87c
RM
753 host.h_length = len;
754 host.h_addrtype = af;
28f540f4
RM
755 while (*cp == ' ' || *cp == '\t')
756 cp++;
757 host.h_name = cp;
758 q = host.h_aliases = host_aliases;
6796bc80 759 if ((cp = strpbrk(cp, " \t")))
28f540f4
RM
760 *cp++ = '\0';
761 while (cp && *cp) {
762 if (*cp == ' ' || *cp == '\t') {
763 cp++;
764 continue;
765 }
766 if (q < &host_aliases[MAXALIASES - 1])
767 *q++ = cp;
6796bc80 768 if ((cp = strpbrk(cp, " \t")))
28f540f4
RM
769 *cp++ = '\0';
770 }
771 *q = NULL;
a68b0d31 772 __set_h_errno (NETDB_SUCCESS);
28f540f4
RM
773 return (&host);
774}
6f9d8e68 775libresolv_hidden_def (_gethtent)
e7eceec0 776compat_symbol (libresolv, _gethtent, _gethtent, GLIBC_2_0);
28f540f4
RM
777
778struct hostent *
9d46370c 779_gethtbyname (const char *name)
fa0bc87c 780{
fa0bc87c
RM
781 struct hostent *hp;
782
b76e0659 783 if (res_use_inet6 ()) {
fa0bc87c
RM
784 hp = _gethtbyname2(name, AF_INET6);
785 if (hp)
786 return (hp);
787 }
788 return (_gethtbyname2(name, AF_INET));
789}
e7eceec0 790compat_symbol (libresolv, _gethtbyname, _gethtbyname, GLIBC_2_0);
fa0bc87c
RM
791
792struct hostent *
9d46370c 793_gethtbyname2 (const char *name, int af)
28f540f4 794{
2e09a79a
JM
795 struct hostent *p;
796 char **cp;
fa0bc87c 797
28f540f4 798 _sethtent(0);
6796bc80 799 while ((p = _gethtent())) {
5f0e6fc7
RM
800 if (p->h_addrtype != af)
801 continue;
802 if (strcasecmp(p->h_name, name) == 0)
803 break;
804 for (cp = p->h_aliases; *cp != 0; cp++)
805 if (strcasecmp(*cp, name) == 0)
806 goto found;
28f540f4 807 }
fa0bc87c 808 found:
28f540f4
RM
809 _endhtent();
810 return (p);
811}
6f9d8e68 812libresolv_hidden_def (_gethtbyname2)
e7eceec0 813compat_symbol (libresolv, _gethtbyname2, _gethtbyname2, GLIBC_2_0);
28f540f4
RM
814
815struct hostent *
9d46370c 816_gethtbyaddr (const char *addr, size_t len, int af)
28f540f4 817{
2e09a79a 818 struct hostent *p;
28f540f4
RM
819
820 _sethtent(0);
6796bc80 821 while ((p = _gethtent()))
1c6e6f23 822 if (p->h_addrtype == af && !memcmp(p->h_addr, addr, len))
28f540f4
RM
823 break;
824 _endhtent();
825 return (p);
826}
6f9d8e68 827libresolv_hidden_def (_gethtbyaddr)
e7eceec0 828compat_symbol (libresolv, _gethtbyaddr, _gethtbyaddr, GLIBC_2_0);
28f540f4 829
fa0bc87c 830static void
9d46370c 831map_v4v6_address (const char *src, char *dst)
fa0bc87c
RM
832{
833 u_char *p = (u_char *)dst;
834 char tmp[INADDRSZ];
835 int i;
836
837 /* Stash a temporary copy so our caller can update in place. */
9596d0dd 838 memcpy(tmp, src, INADDRSZ);
fa0bc87c
RM
839 /* Mark this ipv6 addr as a mapped ipv4. */
840 for (i = 0; i < 10; i++)
841 *p++ = 0x00;
842 *p++ = 0xff;
843 *p++ = 0xff;
844 /* Retrieve the saved copy and we're done. */
9596d0dd 845 memcpy((void*)p, tmp, INADDRSZ);
fa0bc87c
RM
846}
847
848static void
9d46370c 849map_v4v6_hostent (struct hostent *hp, char **bpp, int *lenp)
fa0bc87c
RM
850{
851 char **ap;
852
853 if (hp->h_addrtype != AF_INET || hp->h_length != INADDRSZ)
854 return;
855 hp->h_addrtype = AF_INET6;
856 hp->h_length = IN6ADDRSZ;
857 for (ap = hp->h_addr_list; *ap; ap++) {
858 int i = sizeof(align) - ((u_long)*bpp % sizeof(align));
859
860 if (*lenp < (i + IN6ADDRSZ)) {
861 /* Out of memory. Truncate address list here. XXX */
862 *ap = NULL;
863 return;
864 }
865 *bpp += i;
866 *lenp -= i;
867 map_v4v6_address(*ap, *bpp);
868 *ap = *bpp;
869 *bpp += IN6ADDRSZ;
870 *lenp -= IN6ADDRSZ;
871 }
872}
873
03e4219e 874extern void
9d46370c 875addrsort (char **ap, int num)
28f540f4
RM
876{
877 int i, j;
878 char **p;
879 short aval[MAXADDRS];
880 int needsort = 0;
881
882 p = ap;
883 for (i = 0; i < num; i++, p++) {
884 for (j = 0 ; (unsigned)j < _res.nsort; j++)
fa0bc87c 885 if (_res.sort_list[j].addr.s_addr ==
28f540f4
RM
886 (((struct in_addr *)(*p))->s_addr & _res.sort_list[j].mask))
887 break;
888 aval[i] = j;
889 if (needsort == 0 && i > 0 && j < aval[i-1])
890 needsort = i;
891 }
892 if (!needsort)
893 return;
894
895 while (needsort < num) {
896 for (j = needsort - 1; j >= 0; j--) {
897 if (aval[j] > aval[j+1]) {
898 char *hp;
899
900 i = aval[j];
901 aval[j] = aval[j+1];
902 aval[j+1] = i;
903
904 hp = ap[j];
905 ap[j] = ap[j+1];
906 ap[j+1] = hp;
907
908 } else
909 break;
910 }
911 needsort++;
912 }
913}
e7eceec0
FW
914
915#endif /* SHLIB_COMPAT (libc, GLIBC_2_0, GLIBC_2_25) */