1 /* Copyright (C) 1996-2004, 2007, 2008, 2009 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Extended from original form by Ulrich Drepper <drepper@cygnus.com>, 1996.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
20 /* Parts of this file are plain copies of the file `gethtnamadr.c' from
21 the bind package and it has the following copyright. */
24 * ++Copyright++ 1985, 1988, 1993
26 * Copyright (c) 1985, 1988, 1993
27 * The Regents of the University of California. All rights reserved.
29 * Redistribution and use in source and binary forms, with or without
30 * modification, are permitted provided that the following conditions
32 * 1. Redistributions of source code must retain the above copyright
33 * notice, this list of conditions and the following disclaimer.
34 * 2. Redistributions in binary form must reproduce the above copyright
35 * notice, this list of conditions and the following disclaimer in the
36 * documentation and/or other materials provided with the distribution.
37 * 4. Neither the name of the University nor the names of its contributors
38 * may be used to endorse or promote products derived from this software
39 * without specific prior written permission.
41 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
53 * Portions Copyright (c) 1993 by Digital Equipment Corporation.
55 * Permission to use, copy, modify, and distribute this software for any
56 * purpose with or without fee is hereby granted, provided that the above
57 * copyright notice and this permission notice appear in all copies, and that
58 * the name of Digital Equipment Corporation not be used in advertising or
59 * publicity pertaining to distribution of the document or software without
60 * specific, written prior permission.
62 * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL
63 * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES
64 * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL DIGITAL EQUIPMENT
65 * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
66 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
67 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
68 * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
82 #include <sys/syslog.h>
86 /* Get implementation for some internal functions. */
87 #include <resolv/mapv4v6addr.h>
88 #include <resolv/mapv4v6hostent.h>
92 /* Maximum number of aliases we allow. */
93 #define MAX_NR_ALIASES 48
94 #define MAX_NR_ADDRS 48
97 # define MAXPACKET PACKETSZ
99 # define MAXPACKET 65536
101 /* As per RFC 1034 and 1035 a host name cannot exceed 255 octets in length. */
102 #ifdef MAXHOSTNAMELEN
103 # undef MAXHOSTNAMELEN
105 #define MAXHOSTNAMELEN 256
107 static const char AskedForGot
[] = "\
108 gethostby*.getanswer: asked for \"%s\", got \"%s\"";
111 /* We need this time later. */
112 typedef union querybuf
115 u_char buf
[MAXPACKET
];
118 /* These functions are defined in res_comp.c. */
119 #define NS_MAXCDNAME 255 /* maximum compressed domain name */
120 extern int __ns_name_ntop (const u_char
*, char *, size_t);
121 extern int __ns_name_unpack (const u_char
*, const u_char
*,
122 const u_char
*, u_char
*, size_t);
125 static enum nss_status
getanswer_r (const querybuf
*answer
, int anslen
,
126 const char *qname
, int qtype
,
127 struct hostent
*result
, char *buffer
,
128 size_t buflen
, int *errnop
, int *h_errnop
,
129 int map
, int32_t *ttlp
, char **canonp
);
131 static enum nss_status
gaih_getanswer (const querybuf
*answer1
, int anslen1
,
132 const querybuf
*answer2
, int anslen2
,
134 struct gaih_addrtuple
**pat
,
135 char *buffer
, size_t buflen
,
136 int *errnop
, int *h_errnop
,
139 extern enum nss_status
_nss_dns_gethostbyname3_r (const char *name
, int af
,
140 struct hostent
*result
,
141 char *buffer
, size_t buflen
,
142 int *errnop
, int *h_errnop
,
145 hidden_proto (_nss_dns_gethostbyname3_r
)
148 _nss_dns_gethostbyname3_r (const char *name
, int af
, struct hostent
*result
,
149 char *buffer
, size_t buflen
, int *errnop
,
150 int *h_errnop
, int32_t *ttlp
, char **canonp
)
157 querybuf
*orig_host_buffer
;
158 char tmp
[NS_MAXDNAME
];
163 enum nss_status status
;
165 if (__res_maybe_init (&_res
, 0) == -1)
166 return NSS_STATUS_UNAVAIL
;
179 *errnop
= EAFNOSUPPORT
;
180 return NSS_STATUS_UNAVAIL
;
183 result
->h_addrtype
= af
;
184 result
->h_length
= size
;
187 * if there aren't any dots, it could be a user-level alias.
188 * this is also done in res_query() since we are not the only
189 * function that looks up host names.
191 if (strchr (name
, '.') == NULL
192 && (cp
= res_hostalias (&_res
, name
, tmp
, sizeof (tmp
))) != NULL
)
195 host_buffer
.buf
= orig_host_buffer
= (querybuf
*) alloca (1024);
197 n
= __libc_res_nsearch (&_res
, name
, C_IN
, type
, host_buffer
.buf
->buf
,
198 1024, &host_buffer
.ptr
, NULL
, NULL
, NULL
);
203 status
= NSS_STATUS_TRYAGAIN
;
207 status
= (errno
== ECONNREFUSED
208 ? NSS_STATUS_UNAVAIL
: NSS_STATUS_NOTFOUND
);
210 if (h_errno
== TRY_AGAIN
)
213 __set_errno (olderr
);
215 /* If we are looking for a IPv6 address and mapping is enabled
216 by having the RES_USE_INET6 bit in _res.options set, we try
218 if (af
== AF_INET6
&& (_res
.options
& RES_USE_INET6
))
219 n
= __libc_res_nsearch (&_res
, name
, C_IN
, T_A
, host_buffer
.buf
->buf
,
220 host_buffer
.buf
!= orig_host_buffer
221 ? MAXPACKET
: 1024, &host_buffer
.ptr
,
226 if (host_buffer
.buf
!= orig_host_buffer
)
227 free (host_buffer
.buf
);
233 result
->h_addrtype
= AF_INET
;
234 result
->h_length
= INADDRSZ
;
237 status
= getanswer_r (host_buffer
.buf
, n
, name
, type
, result
, buffer
, buflen
,
238 errnop
, h_errnop
, map
, ttlp
, canonp
);
239 if (host_buffer
.buf
!= orig_host_buffer
)
240 free (host_buffer
.buf
);
243 hidden_def (_nss_dns_gethostbyname3_r
)
247 _nss_dns_gethostbyname2_r (const char *name
, int af
, struct hostent
*result
,
248 char *buffer
, size_t buflen
, int *errnop
,
251 return _nss_dns_gethostbyname3_r (name
, af
, result
, buffer
, buflen
, errnop
,
252 h_errnop
, NULL
, NULL
);
257 _nss_dns_gethostbyname_r (const char *name
, struct hostent
*result
,
258 char *buffer
, size_t buflen
, int *errnop
,
261 enum nss_status status
= NSS_STATUS_NOTFOUND
;
263 if (_res
.options
& RES_USE_INET6
)
264 status
= _nss_dns_gethostbyname3_r (name
, AF_INET6
, result
, buffer
,
265 buflen
, errnop
, h_errnop
, NULL
, NULL
);
266 if (status
== NSS_STATUS_NOTFOUND
)
267 status
= _nss_dns_gethostbyname3_r (name
, AF_INET
, result
, buffer
,
268 buflen
, errnop
, h_errnop
, NULL
, NULL
);
275 _nss_dns_gethostbyname4_r (const char *name
, struct gaih_addrtuple
**pat
,
276 char *buffer
, size_t buflen
, int *errnop
,
277 int *herrnop
, int32_t *ttlp
)
279 if (__res_maybe_init (&_res
, 0) == -1)
280 return NSS_STATUS_UNAVAIL
;
283 * if there aren't any dots, it could be a user-level alias.
284 * this is also done in res_query() since we are not the only
285 * function that looks up host names.
287 if (strchr (name
, '.') == NULL
)
289 char *tmp
= alloca (NS_MAXDNAME
);
290 const char *cp
= res_hostalias (&_res
, name
, tmp
, NS_MAXDNAME
);
300 querybuf
*orig_host_buffer
;
301 host_buffer
.buf
= orig_host_buffer
= (querybuf
*) alloca (2048);
302 u_char
*ans2p
= NULL
;
307 enum nss_status status
;
308 int n
= __libc_res_nsearch (&_res
, name
, C_IN
, T_UNSPEC
,
309 host_buffer
.buf
->buf
, 2048, &host_buffer
.ptr
,
310 &ans2p
, &nans2p
, &resplen2
);
315 status
= NSS_STATUS_TRYAGAIN
;
319 status
= (errno
== ECONNREFUSED
320 ? NSS_STATUS_UNAVAIL
: NSS_STATUS_NOTFOUND
);
322 if (h_errno
== TRY_AGAIN
)
325 __set_errno (olderr
);
327 if (host_buffer
.buf
!= orig_host_buffer
)
328 free (host_buffer
.buf
);
333 status
= gaih_getanswer(host_buffer
.buf
, n
, (const querybuf
*) ans2p
,
334 resplen2
, name
, pat
, buffer
, buflen
,
335 errnop
, herrnop
, ttlp
);
337 if (host_buffer
.buf
!= orig_host_buffer
)
338 free (host_buffer
.buf
);
344 extern enum nss_status
_nss_dns_gethostbyaddr2_r (const void *addr
,
345 socklen_t len
, int af
,
346 struct hostent
*result
,
347 char *buffer
, size_t buflen
,
348 int *errnop
, int *h_errnop
,
350 hidden_proto (_nss_dns_gethostbyaddr2_r
)
353 _nss_dns_gethostbyaddr2_r (const void *addr
, socklen_t len
, int af
,
354 struct hostent
*result
, char *buffer
, size_t buflen
,
355 int *errnop
, int *h_errnop
, int32_t *ttlp
)
357 static const u_char mapped
[] = { 0,0, 0,0, 0,0, 0,0, 0,0, 0xff,0xff };
358 static const u_char tunnelled
[] = { 0,0, 0,0, 0,0, 0,0, 0,0, 0,0 };
359 static const u_char v6local
[] = { 0,0, 0,1 };
360 const u_char
*uaddr
= (const u_char
*)addr
;
363 char *aliases
[MAX_NR_ALIASES
];
364 unsigned char host_addr
[16]; /* IPv4 or IPv6 */
365 char *h_addr_ptrs
[MAX_NR_ADDRS
+ 1];
367 } *host_data
= (struct host_data
*) buffer
;
373 querybuf
*orig_host_buffer
;
374 char qbuf
[MAXDNAME
+1], *qp
= NULL
;
379 uintptr_t pad
= -(uintptr_t) buffer
% __alignof__ (struct host_data
);
381 buflen
= buflen
> pad
? buflen
- pad
: 0;
383 if (__builtin_expect (buflen
< sizeof (struct host_data
), 0))
386 *h_errnop
= NETDB_INTERNAL
;
387 return NSS_STATUS_TRYAGAIN
;
390 host_data
= (struct host_data
*) buffer
;
392 if (__res_maybe_init (&_res
, 0) == -1)
393 return NSS_STATUS_UNAVAIL
;
395 if (af
== AF_INET6
&& len
== IN6ADDRSZ
396 && (memcmp (uaddr
, mapped
, sizeof mapped
) == 0
397 || (memcmp (uaddr
, tunnelled
, sizeof tunnelled
) == 0
398 && memcmp (&uaddr
[sizeof tunnelled
], v6local
, sizeof v6local
))))
401 addr
+= sizeof mapped
;
402 uaddr
+= sizeof mapped
;
416 *errnop
= EAFNOSUPPORT
;
417 *h_errnop
= NETDB_INTERNAL
;
418 return NSS_STATUS_UNAVAIL
;
422 *errnop
= EAFNOSUPPORT
;
423 *h_errnop
= NETDB_INTERNAL
;
424 return NSS_STATUS_UNAVAIL
;
427 host_buffer
.buf
= orig_host_buffer
= (querybuf
*) alloca (1024);
432 sprintf (qbuf
, "%u.%u.%u.%u.in-addr.arpa", (uaddr
[3] & 0xff),
433 (uaddr
[2] & 0xff), (uaddr
[1] & 0xff), (uaddr
[0] & 0xff));
436 /* Only lookup with the byte string format if the user wants it. */
437 if (__builtin_expect (_res
.options
& RES_USEBSTRING
, 0))
439 qp
= stpcpy (qbuf
, "\\[x");
440 for (n
= 0; n
< IN6ADDRSZ
; ++n
)
441 qp
+= sprintf (qp
, "%02hhx", uaddr
[n
]);
442 strcpy (qp
, "].ip6.arpa");
443 n
= __libc_res_nquery (&_res
, qbuf
, C_IN
, T_PTR
,
444 host_buffer
.buf
->buf
, 1024, &host_buffer
.ptr
,
450 for (n
= IN6ADDRSZ
- 1; n
>= 0; n
--)
452 static const char nibblechar
[16] = "0123456789abcdef";
453 *qp
++ = nibblechar
[uaddr
[n
] & 0xf];
455 *qp
++ = nibblechar
[(uaddr
[n
] >> 4) & 0xf];
458 strcpy(qp
, "ip6.arpa");
465 n
= __libc_res_nquery (&_res
, qbuf
, C_IN
, T_PTR
, host_buffer
.buf
->buf
,
466 1024, &host_buffer
.ptr
, NULL
, NULL
, NULL
);
467 if (n
< 0 && af
== AF_INET6
&& (_res
.options
& RES_NOIP6DOTINT
) == 0)
469 strcpy (qp
, "ip6.int");
470 n
= __libc_res_nquery (&_res
, qbuf
, C_IN
, T_PTR
, host_buffer
.buf
->buf
,
471 host_buffer
.buf
!= orig_host_buffer
472 ? MAXPACKET
: 1024, &host_buffer
.ptr
,
478 __set_errno (olderr
);
479 if (host_buffer
.buf
!= orig_host_buffer
)
480 free (host_buffer
.buf
);
481 return errno
== ECONNREFUSED
? NSS_STATUS_UNAVAIL
: NSS_STATUS_NOTFOUND
;
485 status
= getanswer_r (host_buffer
.buf
, n
, qbuf
, T_PTR
, result
, buffer
, buflen
,
486 errnop
, h_errnop
, 0 /* XXX */, ttlp
, NULL
);
487 if (host_buffer
.buf
!= orig_host_buffer
)
488 free (host_buffer
.buf
);
489 if (status
!= NSS_STATUS_SUCCESS
)
493 This is
not implemented because it is
not possible to use the current
494 source from bind in a multi
-threaded program
.
497 result
->h_addrtype
= af
;
498 result
->h_length
= len
;
499 memcpy (host_data
->host_addr
, addr
, len
);
500 host_data
->h_addr_ptrs
[0] = (char *) host_data
->host_addr
;
501 host_data
->h_addr_ptrs
[1] = NULL
;
503 /* XXX I think this is wrong. Why should an IPv4 address be
504 converted to IPv6 if the user explicitly asked for IPv4? */
505 if (af
== AF_INET
&& (_res
.options
& RES_USE_INET6
))
507 map_v4v6_address ((char *) host_data
->host_addr
,
508 (char *) host_data
->host_addr
);
509 result
->h_addrtype
= AF_INET6
;
510 result
->h_length
= IN6ADDRSZ
;
513 *h_errnop
= NETDB_SUCCESS
;
514 return NSS_STATUS_SUCCESS
;
516 hidden_def (_nss_dns_gethostbyaddr2_r
)
520 _nss_dns_gethostbyaddr_r (const void *addr
, socklen_t len
, int af
,
521 struct hostent
*result
, char *buffer
, size_t buflen
,
522 int *errnop
, int *h_errnop
)
524 return _nss_dns_gethostbyaddr2_r (addr
, len
, af
, result
, buffer
, buflen
,
525 errnop
, h_errnop
, NULL
);
529 static void addrsort (char **ap
, int num
);
532 addrsort (char **ap
, int num
)
536 short aval
[MAX_NR_ADDRS
];
540 if (num
> MAX_NR_ADDRS
)
542 for (i
= 0; i
< num
; i
++, p
++)
544 for (j
= 0 ; (unsigned)j
< _res
.nsort
; j
++)
545 if (_res
.sort_list
[j
].addr
.s_addr
==
546 (((struct in_addr
*)(*p
))->s_addr
& _res
.sort_list
[j
].mask
))
549 if (needsort
== 0 && i
> 0 && j
< aval
[i
-1])
555 while (needsort
++ < num
)
556 for (j
= needsort
- 2; j
>= 0; j
--)
557 if (aval
[j
] > aval
[j
+1])
574 static enum nss_status
575 getanswer_r (const querybuf
*answer
, int anslen
, const char *qname
, int qtype
,
576 struct hostent
*result
, char *buffer
, size_t buflen
,
577 int *errnop
, int *h_errnop
, int map
, int32_t *ttlp
, char **canonp
)
581 char *aliases
[MAX_NR_ALIASES
];
582 unsigned char host_addr
[16]; /* IPv4 or IPv6 */
583 char *h_addr_ptrs
[0];
586 register const HEADER
*hp
;
587 const u_char
*end_of_message
, *cp
;
588 int n
, ancount
, qdcount
;
589 int haveanswer
, had_error
;
590 char *bp
, **ap
, **hap
;
593 int (*name_ok
) (const char *);
594 u_char packtmp
[NS_MAXCDNAME
];
597 uintptr_t pad
= -(uintptr_t) buffer
% __alignof__ (struct host_data
);
599 if (__builtin_expect (buflen
< sizeof (struct host_data
) + pad
, 0))
601 /* The buffer is too small. */
604 *h_errnop
= NETDB_INTERNAL
;
605 return NSS_STATUS_TRYAGAIN
;
607 host_data
= (struct host_data
*) buffer
;
608 linebuflen
= buflen
- sizeof (struct host_data
);
609 if (buflen
- sizeof (struct host_data
) != linebuflen
)
610 linebuflen
= INT_MAX
;
613 result
->h_name
= NULL
;
614 end_of_message
= answer
->buf
+ anslen
;
626 return NSS_STATUS_UNAVAIL
; /* XXX should be abort(); */
630 * find first satisfactory answer
633 ancount
= ntohs (hp
->ancount
);
634 qdcount
= ntohs (hp
->qdcount
);
635 cp
= answer
->buf
+ HFIXEDSZ
;
636 if (__builtin_expect (qdcount
, 1) != 1)
638 *h_errnop
= NO_RECOVERY
;
639 return NSS_STATUS_UNAVAIL
;
641 if (sizeof (struct host_data
) + (ancount
+ 1) * sizeof (char *) >= buflen
)
643 bp
= (char *) &host_data
->h_addr_ptrs
[ancount
+ 1];
644 linebuflen
-= (ancount
+ 1) * sizeof (char *);
646 n
= __ns_name_unpack (answer
->buf
, end_of_message
, cp
,
647 packtmp
, sizeof packtmp
);
648 if (n
!= -1 && __ns_name_ntop (packtmp
, bp
, linebuflen
) == -1)
650 if (__builtin_expect (errno
, 0) == EMSGSIZE
)
656 if (n
> 0 && bp
[0] == '.')
659 if (__builtin_expect (n
< 0 || ((*name_ok
) (bp
) == 0 && (errno
= EBADMSG
)),
663 *h_errnop
= NO_RECOVERY
;
664 return NSS_STATUS_UNAVAIL
;
668 if (qtype
== T_A
|| qtype
== T_AAAA
)
670 /* res_send() has already verified that the query name is the
671 * same as the one we sent; this just gets the expanded name
672 * (i.e., with the succeeding search-domain tacked on).
674 n
= strlen (bp
) + 1; /* for the \0 */
675 if (n
>= MAXHOSTNAMELEN
)
677 *h_errnop
= NO_RECOVERY
;
679 return NSS_STATUS_TRYAGAIN
;
686 /* The qname can be abbreviated, but h_name is now absolute. */
687 qname
= result
->h_name
;
690 ap
= host_data
->aliases
;
692 result
->h_aliases
= host_data
->aliases
;
693 hap
= host_data
->h_addr_ptrs
;
695 result
->h_addr_list
= host_data
->h_addr_ptrs
;
699 while (ancount
-- > 0 && cp
< end_of_message
&& had_error
== 0)
703 n
= __ns_name_unpack (answer
->buf
, end_of_message
, cp
,
704 packtmp
, sizeof packtmp
);
705 if (n
!= -1 && __ns_name_ntop (packtmp
, bp
, linebuflen
) == -1)
707 if (__builtin_expect (errno
, 0) == EMSGSIZE
)
713 if (__builtin_expect (n
< 0 || (*name_ok
) (bp
) == 0, 0))
720 if (__builtin_expect (cp
+ 10 > end_of_message
, 0))
726 type
= __ns_get16 (cp
);
727 cp
+= INT16SZ
; /* type */
728 class = __ns_get16 (cp
);
729 cp
+= INT16SZ
; /* class */
730 ttl
= __ns_get32 (cp
);
731 cp
+= INT32SZ
; /* TTL */
733 cp
+= INT16SZ
; /* len */
734 if (__builtin_expect (class != C_IN
, 0))
736 /* XXX - debug? syslog? */
738 continue; /* XXX - had_error++ ? */
741 if ((qtype
== T_A
|| qtype
== T_AAAA
) && type
== T_CNAME
)
743 if (ap
>= &host_data
->aliases
[MAX_NR_ALIASES
- 1])
745 n
= dn_expand (answer
->buf
, end_of_message
, cp
, tbuf
, sizeof tbuf
);
746 if (__builtin_expect (n
< 0 || (*name_ok
) (tbuf
) == 0, 0))
754 n
= strlen (bp
) + 1; /* For the \0. */
755 if (__builtin_expect (n
, 0) >= MAXHOSTNAMELEN
)
762 /* Get canonical name. */
763 n
= strlen (tbuf
) + 1; /* For the \0. */
764 if (__builtin_expect (n
> linebuflen
, 0))
766 if (__builtin_expect (n
, 0) >= MAXHOSTNAMELEN
)
772 bp
= __mempcpy (bp
, tbuf
, n
); /* Cannot overflow. */
777 if (qtype
== T_PTR
&& type
== T_CNAME
)
779 n
= dn_expand (answer
->buf
, end_of_message
, cp
, tbuf
, sizeof tbuf
);
780 if (__builtin_expect (n
< 0 || res_dnok (tbuf
) == 0, 0))
786 /* Get canonical name. */
787 n
= strlen (tbuf
) + 1; /* For the \0. */
788 if (__builtin_expect (n
> linebuflen
, 0))
790 if (__builtin_expect (n
, 0) >= MAXHOSTNAMELEN
)
796 bp
= __mempcpy (bp
, tbuf
, n
); /* Cannot overflow. */
800 if (__builtin_expect (type
== T_SIG
, 0)
801 || __builtin_expect (type
== T_KEY
, 0)
802 || __builtin_expect (type
== T_NXT
, 0))
804 /* We don't support DNSSEC yet. For now, ignore the record
805 and send a low priority message to syslog. */
806 syslog (LOG_DEBUG
| LOG_AUTH
,
807 "gethostby*.getanswer: asked for \"%s %s %s\", got type \"%s\"",
808 qname
, p_class (C_IN
), p_type(qtype
), p_type (type
));
813 if (type
== T_A
&& qtype
== T_AAAA
&& map
)
815 else if (__builtin_expect (type
!= qtype
, 0))
817 syslog (LOG_NOTICE
| LOG_AUTH
,
818 "gethostby*.getanswer: asked for \"%s %s %s\", got type \"%s\"",
819 qname
, p_class (C_IN
), p_type (qtype
), p_type (type
));
821 continue; /* XXX - had_error++ ? */
827 if (__builtin_expect (__strcasecmp (tname
, bp
) != 0, 0))
829 syslog (LOG_NOTICE
| LOG_AUTH
, AskedForGot
, qname
, bp
);
831 continue; /* XXX - had_error++ ? */
834 n
= __ns_name_unpack (answer
->buf
, end_of_message
, cp
,
835 packtmp
, sizeof packtmp
);
836 if (n
!= -1 && __ns_name_ntop (packtmp
, bp
, linebuflen
) == -1)
838 if (__builtin_expect (errno
, 0) == EMSGSIZE
)
844 if (__builtin_expect (n
< 0 || res_hnok (bp
) == 0, 0))
849 #if MULTI_PTRS_ARE_ALIASES
853 else if (ap
< &host_data
->aliases
[MAXALIASES
-1])
859 n
= strlen (bp
) + 1; /* for the \0 */
860 if (__builtin_expect (n
, 0) >= MAXHOSTNAMELEN
)
873 n
= strlen (bp
) + 1; /* for the \0 */
874 if (__builtin_expect (n
>= MAXHOSTNAMELEN
, 0))
881 map_v4v6_hostent (result
, &bp
, &linebuflen
);
883 *h_errnop
= NETDB_SUCCESS
;
884 return NSS_STATUS_SUCCESS
;
888 if (__builtin_expect (strcasecmp (result
->h_name
, bp
), 0) != 0)
890 syslog (LOG_NOTICE
| LOG_AUTH
, AskedForGot
, result
->h_name
, bp
);
892 continue; /* XXX - had_error++ ? */
894 if (n
!= result
->h_length
)
903 if (ttlp
!= NULL
&& ttl
!= 0)
908 nn
= strlen (bp
) + 1; /* for the \0 */
913 linebuflen
-= sizeof (align
) - ((u_long
) bp
% sizeof (align
));
914 bp
+= sizeof (align
) - ((u_long
) bp
% sizeof (align
));
916 if (__builtin_expect (n
> linebuflen
, 0))
918 bp
= __mempcpy (*hap
++ = bp
, cp
, n
);
933 #if defined RESOLVSORT
935 * Note: we sort even if host can take only one address
936 * in its return structures - should give it the "best"
937 * address in that case, not some random one
939 if (_res
.nsort
&& haveanswer
> 1 && qtype
== T_A
)
940 addrsort (host_data
->h_addr_ptrs
, haveanswer
);
941 #endif /*RESOLVSORT*/
943 if (result
->h_name
== NULL
)
945 n
= strlen (qname
) + 1; /* For the \0. */
948 if (n
>= MAXHOSTNAMELEN
)
951 bp
= __mempcpy (bp
, qname
, n
); /* Cannot overflow. */
956 map_v4v6_hostent (result
, &bp
, &linebuflen
);
957 *h_errnop
= NETDB_SUCCESS
;
958 return NSS_STATUS_SUCCESS
;
961 *h_errnop
= NO_RECOVERY
;
963 /* Special case here: if the resolver sent a result but it only
964 contains a CNAME while we are looking for a T_A or T_AAAA record,
965 we fail with NOTFOUND instead of TRYAGAIN. */
966 return ((qtype
== T_A
|| qtype
== T_AAAA
) && ap
!= host_data
->aliases
967 ? NSS_STATUS_NOTFOUND
: NSS_STATUS_TRYAGAIN
);
971 static enum nss_status
972 gaih_getanswer_slice (const querybuf
*answer
, int anslen
, const char *qname
,
973 struct gaih_addrtuple
***patp
,
974 char **bufferp
, size_t *buflenp
,
975 int *errnop
, int *h_errnop
, int32_t *ttlp
, int *firstp
)
977 char *buffer
= *bufferp
;
978 size_t buflen
= *buflenp
;
980 struct gaih_addrtuple
**pat
= *patp
;
981 const HEADER
*hp
= &answer
->hdr
;
982 int ancount
= ntohs (hp
->ancount
);
983 int qdcount
= ntohs (hp
->qdcount
);
984 const u_char
*cp
= answer
->buf
+ HFIXEDSZ
;
985 const u_char
*end_of_message
= answer
->buf
+ anslen
;
986 if (__builtin_expect (qdcount
!= 1, 0))
988 *h_errnop
= NO_RECOVERY
;
989 return NSS_STATUS_UNAVAIL
;
992 u_char packtmp
[NS_MAXCDNAME
];
993 int n
= __ns_name_unpack (answer
->buf
, end_of_message
, cp
,
994 packtmp
, sizeof packtmp
);
995 /* We unpack the name to check it for validity. But we do not need
997 if (n
!= -1 && __ns_name_ntop (packtmp
, buffer
, buflen
) == -1)
999 if (__builtin_expect (errno
, 0) == EMSGSIZE
)
1003 *h_errnop
= NETDB_INTERNAL
;
1004 return NSS_STATUS_TRYAGAIN
;
1010 if (__builtin_expect (n
< 0 || (res_hnok (buffer
) == 0
1011 && (errno
= EBADMSG
)), 0))
1014 *h_errnop
= NO_RECOVERY
;
1015 return NSS_STATUS_UNAVAIL
;
1022 char *h_name
= NULL
;
1026 return NSS_STATUS_NOTFOUND
;
1028 while (ancount
-- > 0 && cp
< end_of_message
&& had_error
== 0)
1030 n
= __ns_name_unpack (answer
->buf
, end_of_message
, cp
,
1031 packtmp
, sizeof packtmp
);
1033 (h_namelen
= __ns_name_ntop (packtmp
, buffer
, buflen
)) == -1)
1035 if (__builtin_expect (errno
, 0) == EMSGSIZE
)
1040 if (__builtin_expect (n
< 0 || res_hnok (buffer
) == 0, 0))
1048 buffer
+= h_namelen
;
1049 buflen
-= h_namelen
;
1054 if (__builtin_expect (cp
+ 10 > end_of_message
, 0))
1060 int type
= __ns_get16 (cp
);
1061 cp
+= INT16SZ
; /* type */
1062 int class = __ns_get16 (cp
);
1063 cp
+= INT16SZ
; /* class */
1064 int32_t ttl
= __ns_get32 (cp
);
1065 cp
+= INT32SZ
; /* TTL */
1066 n
= __ns_get16 (cp
);
1067 cp
+= INT16SZ
; /* len */
1075 if (type
== T_CNAME
)
1077 char tbuf
[MAXDNAME
];
1078 n
= dn_expand (answer
->buf
, end_of_message
, cp
, tbuf
, sizeof tbuf
);
1079 if (__builtin_expect (n
< 0 || res_hnok (tbuf
) == 0, 0))
1088 /* Reclaim buffer space. */
1089 if (h_name
+ h_namelen
== buffer
)
1092 buflen
+= h_namelen
;
1095 n
= strlen (tbuf
) + 1;
1096 if (__builtin_expect (n
> buflen
, 0))
1098 if (__builtin_expect (n
>= MAXHOSTNAMELEN
, 0))
1105 buffer
= __mempcpy (buffer
, tbuf
, n
);
1112 // We should not see any types other than those explicitly listed
1113 // below. Some types sent by server seem missing, though. Just
1114 // collect the data for now.
1115 if (__builtin_expect (type
!= T_A
&& type
!= T_AAAA
, 0))
1117 if (__builtin_expect (type
== T_SIG
, 0)
1118 || __builtin_expect (type
== T_KEY
, 0)
1119 || __builtin_expect (type
== T_NXT
, 0)
1120 || __builtin_expect (type
== T_PTR
, 0)
1121 || __builtin_expect (type
== T_DNAME
, 0))
1124 /* We don't support DNSSEC yet. For now, ignore the record
1125 and send a low priority message to syslog.
1127 We also don't expect T_PTR or T_DNAME messages. */
1128 syslog (LOG_DEBUG
| LOG_AUTH
,
1129 "getaddrinfo*.gaih_getanswer: got type \"%s\"",
1134 if (type
!= T_A
&& type
!= T_AAAA
)
1139 uintptr_t pad
= (-(uintptr_t) buffer
1140 % __alignof__ (struct gaih_addrtuple
));
1142 buflen
= buflen
> pad
? buflen
- pad
: 0;
1144 if (__builtin_expect (buflen
< sizeof (struct gaih_addrtuple
),
1148 *pat
= (struct gaih_addrtuple
*) buffer
;
1149 buffer
+= sizeof (struct gaih_addrtuple
);
1150 buflen
-= sizeof (struct gaih_addrtuple
);
1153 (*pat
)->name
= NULL
;
1154 (*pat
)->next
= NULL
;
1158 if (ttl
!= 0 && ttlp
!= NULL
)
1163 (*pat
)->name
= canon
;
1165 /* Reclaim buffer space. */
1166 if (h_name
+ h_namelen
== buffer
)
1169 buflen
+= h_namelen
;
1173 (*pat
)->name
= h_name
;
1178 (*pat
)->family
= type
== T_A
? AF_INET
: AF_INET6
;
1179 if (__builtin_expect ((type
== T_A
&& n
!= INADDRSZ
)
1180 || (type
== T_AAAA
&& n
!= IN6ADDRSZ
), 0))
1185 memcpy ((*pat
)->addr
, cp
, n
);
1187 (*pat
)->scopeid
= 0;
1189 pat
= &((*pat
)->next
);
1200 *h_errnop
= NETDB_SUCCESS
;
1201 return NSS_STATUS_SUCCESS
;
1204 /* Special case here: if the resolver sent a result but it only
1205 contains a CNAME while we are looking for a T_A or T_AAAA record,
1206 we fail with NOTFOUND instead of TRYAGAIN. */
1207 return canon
== NULL
? NSS_STATUS_TRYAGAIN
: NSS_STATUS_NOTFOUND
;
1211 static enum nss_status
1212 gaih_getanswer (const querybuf
*answer1
, int anslen1
, const querybuf
*answer2
,
1213 int anslen2
, const char *qname
,
1214 struct gaih_addrtuple
**pat
, char *buffer
, size_t buflen
,
1215 int *errnop
, int *h_errnop
, int32_t *ttlp
)
1219 enum nss_status status
= NSS_STATUS_NOTFOUND
;
1222 status
= gaih_getanswer_slice(answer1
, anslen1
, qname
,
1223 &pat
, &buffer
, &buflen
,
1224 errnop
, h_errnop
, ttlp
,
1226 if ((status
== NSS_STATUS_SUCCESS
|| status
== NSS_STATUS_NOTFOUND
1227 || (status
== NSS_STATUS_TRYAGAIN
1228 && (errno
!= ERANGE
|| *h_errnop
!= NO_RECOVERY
)))
1229 && answer2
!= NULL
&& anslen2
> 0)
1231 enum nss_status status2
= gaih_getanswer_slice(answer2
, anslen2
, qname
,
1232 &pat
, &buffer
, &buflen
,
1233 errnop
, h_errnop
, ttlp
,
1235 if (status
!= NSS_STATUS_SUCCESS
&& status2
!= NSS_STATUS_NOTFOUND
)