2.5-18.1
[glibc.git] / resolv / nss_dns / dns-host.c
blobcf060be8efd56bab06c30f1d518e28a8c513d3fe
1 /* Copyright (C) 1996-2003, 2004, 2007 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
18 02111-1307 USA. */
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
25 * -
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
31 * are met:
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
51 * SUCH DAMAGE.
52 * -
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
69 * SOFTWARE.
70 * -
71 * --Copyright--
74 #include <ctype.h>
75 #include <errno.h>
76 #include <netdb.h>
77 #include <stdio.h>
78 #include <stdlib.h>
79 #include <stddef.h>
80 #include <string.h>
81 #include <sys/syslog.h>
83 #include "nsswitch.h"
85 /* Get implementation for some internal functions. */
86 #include <resolv/mapv4v6addr.h>
87 #include <resolv/mapv4v6hostent.h>
89 #define RESOLVSORT
91 /* Maximum number of aliases we allow. */
92 #define MAX_NR_ALIASES 48
93 #define MAX_NR_ADDRS 48
95 #if PACKETSZ > 65536
96 # define MAXPACKET PACKETSZ
97 #else
98 # define MAXPACKET 65536
99 #endif
100 /* As per RFC 1034 and 1035 a host name cannot exceed 255 octets in length. */
101 #ifdef MAXHOSTNAMELEN
102 # undef MAXHOSTNAMELEN
103 #endif
104 #define MAXHOSTNAMELEN 256
106 static const char AskedForGot[] = "\
107 gethostby*.getanswer: asked for \"%s\", got \"%s\"";
110 /* We need this time later. */
111 typedef union querybuf
113 HEADER hdr;
114 u_char buf[MAXPACKET];
115 } querybuf;
117 /* These functions are defined in res_comp.c. */
118 #define NS_MAXCDNAME 255 /* maximum compressed domain name */
119 extern int __ns_name_ntop (const u_char *, char *, size_t);
120 extern int __ns_name_unpack (const u_char *, const u_char *,
121 const u_char *, u_char *, size_t);
124 static enum nss_status getanswer_r (const querybuf *answer, int anslen,
125 const char *qname, int qtype,
126 struct hostent *result, char *buffer,
127 size_t buflen, int *errnop, int *h_errnop,
128 int map, int32_t *ttlp, char **canonp);
130 extern enum nss_status _nss_dns_gethostbyname3_r (const char *name, int af,
131 struct hostent *result,
132 char *buffer, size_t buflen,
133 int *errnop, int *h_errnop,
134 int32_t *ttlp,
135 char **canonp);
136 hidden_proto (_nss_dns_gethostbyname3_r)
138 enum nss_status
139 _nss_dns_gethostbyname3_r (const char *name, int af, struct hostent *result,
140 char *buffer, size_t buflen, int *errnop,
141 int *h_errnop, int32_t *ttlp, char **canonp)
143 union
145 querybuf *buf;
146 u_char *ptr;
147 } host_buffer;
148 querybuf *orig_host_buffer;
149 char tmp[NS_MAXDNAME];
150 int size, type, n;
151 const char *cp;
152 int map = 0;
153 int olderr = errno;
154 enum nss_status status;
156 if (__res_maybe_init (&_res, 0) == -1)
157 return NSS_STATUS_UNAVAIL;
159 switch (af) {
160 case AF_INET:
161 size = INADDRSZ;
162 type = T_A;
163 break;
164 case AF_INET6:
165 size = IN6ADDRSZ;
166 type = T_AAAA;
167 break;
168 default:
169 *h_errnop = NO_DATA;
170 *errnop = EAFNOSUPPORT;
171 return NSS_STATUS_UNAVAIL;
174 result->h_addrtype = af;
175 result->h_length = size;
178 * if there aren't any dots, it could be a user-level alias.
179 * this is also done in res_query() since we are not the only
180 * function that looks up host names.
182 if (strchr (name, '.') == NULL
183 && (cp = res_hostalias (&_res, name, tmp, sizeof (tmp))) != NULL)
184 name = cp;
186 host_buffer.buf = orig_host_buffer = (querybuf *) alloca (1024);
188 n = __libc_res_nsearch (&_res, name, C_IN, type, host_buffer.buf->buf,
189 1024, &host_buffer.ptr);
190 if (n < 0)
192 enum nss_status status = (errno == ECONNREFUSED
193 ? NSS_STATUS_UNAVAIL : NSS_STATUS_NOTFOUND);
194 *h_errnop = h_errno;
195 if (h_errno == TRY_AGAIN)
196 *errnop = EAGAIN;
197 else
198 __set_errno (olderr);
200 /* If we are looking for a IPv6 address and mapping is enabled
201 by having the RES_USE_INET6 bit in _res.options set, we try
202 another lookup. */
203 if (af == AF_INET6 && (_res.options & RES_USE_INET6))
204 n = __libc_res_nsearch (&_res, name, C_IN, T_A, host_buffer.buf->buf,
205 host_buffer.buf != orig_host_buffer
206 ? MAXPACKET : 1024, &host_buffer.ptr);
208 if (n < 0)
210 if (host_buffer.buf != orig_host_buffer)
211 free (host_buffer.buf);
212 return status;
215 map = 1;
217 result->h_addrtype = AF_INET;
218 result->h_length = INADDRSZ;;
221 status = getanswer_r (host_buffer.buf, n, name, type, result, buffer, buflen,
222 errnop, h_errnop, map, ttlp, canonp);
223 if (host_buffer.buf != orig_host_buffer)
224 free (host_buffer.buf);
225 return status;
227 hidden_def (_nss_dns_gethostbyname3_r)
230 enum nss_status
231 _nss_dns_gethostbyname2_r (const char *name, int af, struct hostent *result,
232 char *buffer, size_t buflen, int *errnop,
233 int *h_errnop)
235 return _nss_dns_gethostbyname3_r (name, af, result, buffer, buflen, errnop,
236 h_errnop, NULL, NULL);
240 enum nss_status
241 _nss_dns_gethostbyname_r (const char *name, struct hostent *result,
242 char *buffer, size_t buflen, int *errnop,
243 int *h_errnop)
245 enum nss_status status = NSS_STATUS_NOTFOUND;
247 if (_res.options & RES_USE_INET6)
248 status = _nss_dns_gethostbyname3_r (name, AF_INET6, result, buffer,
249 buflen, errnop, h_errnop, NULL, NULL);
250 if (status == NSS_STATUS_NOTFOUND)
251 status = _nss_dns_gethostbyname3_r (name, AF_INET, result, buffer,
252 buflen, errnop, h_errnop, NULL, NULL);
254 return status;
258 enum nss_status
259 _nss_dns_gethostbyaddr_r (const void *addr, socklen_t len, int af,
260 struct hostent *result, char *buffer, size_t buflen,
261 int *errnop, int *h_errnop)
263 static const u_char mapped[] = { 0,0, 0,0, 0,0, 0,0, 0,0, 0xff,0xff };
264 static const u_char tunnelled[] = { 0,0, 0,0, 0,0, 0,0, 0,0, 0,0 };
265 static const u_char v6local[] = { 0,0, 0,1 };
266 const u_char *uaddr = (const u_char *)addr;
267 struct host_data
269 char *aliases[MAX_NR_ALIASES];
270 unsigned char host_addr[16]; /* IPv4 or IPv6 */
271 char *h_addr_ptrs[MAX_NR_ADDRS + 1];
272 char linebuffer[0];
273 } *host_data = (struct host_data *) buffer;
274 union
276 querybuf *buf;
277 u_char *ptr;
278 } host_buffer;
279 querybuf *orig_host_buffer;
280 char qbuf[MAXDNAME+1], *qp = NULL;
281 size_t size;
282 int n, status;
283 int olderr = errno;
285 if (__res_maybe_init (&_res, 0) == -1)
286 return NSS_STATUS_UNAVAIL;
288 if (af == AF_INET6 && len == IN6ADDRSZ
289 && (memcmp (uaddr, mapped, sizeof mapped) == 0
290 || (memcmp (uaddr, tunnelled, sizeof tunnelled) == 0
291 && memcmp (&uaddr[sizeof tunnelled], v6local, sizeof v6local))))
293 /* Unmap. */
294 addr += sizeof mapped;
295 uaddr += sizeof mapped;
296 af = AF_INET;
297 len = INADDRSZ;
300 switch (af)
302 case AF_INET:
303 size = INADDRSZ;
304 break;
305 case AF_INET6:
306 size = IN6ADDRSZ;
307 break;
308 default:
309 *errnop = EAFNOSUPPORT;
310 *h_errnop = NETDB_INTERNAL;
311 return NSS_STATUS_UNAVAIL;
313 if (size > len)
315 *errnop = EAFNOSUPPORT;
316 *h_errnop = NETDB_INTERNAL;
317 return NSS_STATUS_UNAVAIL;
320 host_buffer.buf = orig_host_buffer = (querybuf *) alloca (1024);
322 switch (af)
324 case AF_INET:
325 sprintf (qbuf, "%u.%u.%u.%u.in-addr.arpa", (uaddr[3] & 0xff),
326 (uaddr[2] & 0xff), (uaddr[1] & 0xff), (uaddr[0] & 0xff));
327 break;
328 case AF_INET6:
329 /* Only lookup with the byte string format if the user wants it. */
330 if (__builtin_expect (_res.options & RES_USEBSTRING, 0))
332 qp = stpcpy (qbuf, "\\[x");
333 for (n = 0; n < IN6ADDRSZ; ++n)
334 qp += sprintf (qp, "%02hhx", uaddr[n]);
335 strcpy (qp, "].ip6.arpa");
336 n = __libc_res_nquery (&_res, qbuf, C_IN, T_PTR,
337 host_buffer.buf->buf, 1024, &host_buffer.ptr);
338 if (n >= 0)
339 goto got_it_already;
341 qp = qbuf;
342 for (n = IN6ADDRSZ - 1; n >= 0; n--)
344 static const char nibblechar[16] = "0123456789abcdef";
345 *qp++ = nibblechar[uaddr[n] & 0xf];
346 *qp++ = '.';
347 *qp++ = nibblechar[(uaddr[n] >> 4) & 0xf];
348 *qp++ = '.';
350 strcpy(qp, "ip6.arpa");
351 break;
352 default:
353 /* Cannot happen. */
354 break;
357 n = __libc_res_nquery (&_res, qbuf, C_IN, T_PTR, host_buffer.buf->buf,
358 1024, &host_buffer.ptr);
359 if (n < 0 && af == AF_INET6 && (_res.options & RES_NOIP6DOTINT) == 0)
361 strcpy (qp, "ip6.int");
362 n = __libc_res_nquery (&_res, qbuf, C_IN, T_PTR, host_buffer.buf->buf,
363 host_buffer.buf != orig_host_buffer
364 ? MAXPACKET : 1024, &host_buffer.ptr);
366 if (n < 0)
368 *h_errnop = h_errno;
369 __set_errno (olderr);
370 if (host_buffer.buf != orig_host_buffer)
371 free (host_buffer.buf);
372 return errno == ECONNREFUSED ? NSS_STATUS_UNAVAIL : NSS_STATUS_NOTFOUND;
375 got_it_already:
376 status = getanswer_r (host_buffer.buf, n, qbuf, T_PTR, result, buffer, buflen,
377 errnop, h_errnop, 0 /* XXX */, NULL, NULL);
378 if (host_buffer.buf != orig_host_buffer)
379 free (host_buffer.buf);
380 if (status != NSS_STATUS_SUCCESS)
382 *h_errnop = h_errno;
383 *errnop = errno;
384 return status;
387 #ifdef SUNSECURITY
388 This is not implemented because it is not possible to use the current
389 source from bind in a multi-threaded program.
390 #endif
392 result->h_addrtype = af;
393 result->h_length = len;
394 memcpy (host_data->host_addr, addr, len);
395 host_data->h_addr_ptrs[0] = (char *) host_data->host_addr;
396 host_data->h_addr_ptrs[1] = NULL;
397 #if 0
398 /* XXX I think this is wrong. Why should an IPv4 address be
399 converted to IPv6 if the user explicitly asked for IPv4? */
400 if (af == AF_INET && (_res.options & RES_USE_INET6))
402 map_v4v6_address ((char *) host_data->host_addr,
403 (char *) host_data->host_addr);
404 result->h_addrtype = AF_INET6;
405 result->h_length = IN6ADDRSZ;
407 #endif
408 *h_errnop = NETDB_SUCCESS;
409 return NSS_STATUS_SUCCESS;
412 #ifdef RESOLVSORT
413 static void addrsort (char **ap, int num);
415 static void
416 addrsort (char **ap, int num)
418 int i, j;
419 char **p;
420 short aval[MAX_NR_ADDRS];
421 int needsort = 0;
423 p = ap;
424 if (num > MAX_NR_ADDRS)
425 num = MAX_NR_ADDRS;
426 for (i = 0; i < num; i++, p++)
428 for (j = 0 ; (unsigned)j < _res.nsort; j++)
429 if (_res.sort_list[j].addr.s_addr ==
430 (((struct in_addr *)(*p))->s_addr & _res.sort_list[j].mask))
431 break;
432 aval[i] = j;
433 if (needsort == 0 && i > 0 && j < aval[i-1])
434 needsort = i;
436 if (!needsort)
437 return;
439 while (needsort++ < num)
440 for (j = needsort - 2; j >= 0; j--)
441 if (aval[j] > aval[j+1])
443 char *hp;
445 i = aval[j];
446 aval[j] = aval[j+1];
447 aval[j+1] = i;
449 hp = ap[j];
450 ap[j] = ap[j+1];
451 ap[j+1] = hp;
453 else
454 break;
456 #endif
458 static enum nss_status
459 getanswer_r (const querybuf *answer, int anslen, const char *qname, int qtype,
460 struct hostent *result, char *buffer, size_t buflen,
461 int *errnop, int *h_errnop, int map, int32_t *ttlp, char **canonp)
463 struct host_data
465 char *aliases[MAX_NR_ALIASES];
466 unsigned char host_addr[16]; /* IPv4 or IPv6 */
467 char *h_addr_ptrs[0];
468 } *host_data;
469 int linebuflen;
470 register const HEADER *hp;
471 const u_char *end_of_message, *cp;
472 int n, ancount, qdcount;
473 int haveanswer, had_error;
474 char *bp, **ap, **hap;
475 char tbuf[MAXDNAME];
476 const char *tname;
477 int (*name_ok) (const char *);
478 u_char packtmp[NS_MAXCDNAME];
479 int have_to_map = 0;
480 int32_t ttl = 0;
481 uintptr_t pad = -(uintptr_t) buffer % __alignof__ (struct host_data);
482 buffer += pad;
483 if (__builtin_expect (buflen < sizeof (struct host_data) + pad, 0))
485 /* The buffer is too small. */
486 too_small:
487 *errnop = ERANGE;
488 *h_errnop = NETDB_INTERNAL;
489 return NSS_STATUS_TRYAGAIN;
491 host_data = (struct host_data *) buffer;
492 linebuflen = buflen - sizeof (struct host_data);
493 if (buflen - sizeof (struct host_data) != linebuflen)
494 linebuflen = INT_MAX;
496 tname = qname;
497 result->h_name = NULL;
498 end_of_message = answer->buf + anslen;
499 switch (qtype)
501 case T_A:
502 case T_AAAA:
503 name_ok = res_hnok;
504 break;
505 case T_PTR:
506 name_ok = res_dnok;
507 break;
508 default:
509 *errnop = ENOENT;
510 return NSS_STATUS_UNAVAIL; /* XXX should be abort(); */
514 * find first satisfactory answer
516 hp = &answer->hdr;
517 ancount = ntohs (hp->ancount);
518 qdcount = ntohs (hp->qdcount);
519 cp = answer->buf + HFIXEDSZ;
520 if (__builtin_expect (qdcount, 1) != 1)
522 *h_errnop = NO_RECOVERY;
523 return NSS_STATUS_UNAVAIL;
525 if (sizeof (struct host_data) + (ancount + 1) * sizeof (char *) >= buflen)
526 goto too_small;
527 bp = (char *) &host_data->h_addr_ptrs[ancount + 1];
528 linebuflen -= (ancount + 1) * sizeof (char *);
530 n = __ns_name_unpack (answer->buf, end_of_message, cp,
531 packtmp, sizeof packtmp);
532 if (n != -1 && __ns_name_ntop (packtmp, bp, linebuflen) == -1)
534 if (__builtin_expect (errno, 0) == EMSGSIZE)
535 goto too_small;
537 n = -1;
540 if (n > 0 && bp[0] == '.')
541 bp[0] = '\0';
543 if (n < 0 || (*name_ok) (bp) == 0)
545 *errnop = errno;
546 *h_errnop = NO_RECOVERY;
547 return NSS_STATUS_UNAVAIL;
549 cp += n + QFIXEDSZ;
551 if (qtype == T_A || qtype == T_AAAA)
553 /* res_send() has already verified that the query name is the
554 * same as the one we sent; this just gets the expanded name
555 * (i.e., with the succeeding search-domain tacked on).
557 n = strlen (bp) + 1; /* for the \0 */
558 if (n >= MAXHOSTNAMELEN)
560 *h_errnop = NO_RECOVERY;
561 *errnop = ENOENT;
562 return NSS_STATUS_TRYAGAIN;
564 result->h_name = bp;
565 bp += n;
566 linebuflen -= n;
567 if (linebuflen < 0)
568 goto too_small;
569 /* The qname can be abbreviated, but h_name is now absolute. */
570 qname = result->h_name;
573 ap = host_data->aliases;
574 *ap = NULL;
575 result->h_aliases = host_data->aliases;
576 hap = host_data->h_addr_ptrs;
577 *hap = NULL;
578 result->h_addr_list = host_data->h_addr_ptrs;
579 haveanswer = 0;
580 had_error = 0;
582 while (ancount-- > 0 && cp < end_of_message && had_error == 0)
584 int type, class;
586 n = __ns_name_unpack (answer->buf, end_of_message, cp,
587 packtmp, sizeof packtmp);
588 if (n != -1 && __ns_name_ntop (packtmp, bp, linebuflen) == -1)
590 if (__builtin_expect (errno, 0) == EMSGSIZE)
591 goto too_small;
593 n = -1;
596 if (n < 0 || (*name_ok) (bp) == 0)
598 ++had_error;
599 continue;
601 cp += n; /* name */
602 type = ns_get16 (cp);
603 cp += INT16SZ; /* type */
604 class = ns_get16 (cp);
605 cp += INT16SZ; /* class */
606 ttl = ns_get32 (cp);
607 cp += INT32SZ; /* TTL */
608 n = ns_get16 (cp);
609 cp += INT16SZ; /* len */
610 if (class != C_IN)
612 /* XXX - debug? syslog? */
613 cp += n;
614 continue; /* XXX - had_error++ ? */
617 if ((qtype ==T_A || qtype == T_AAAA) && type == T_CNAME)
619 if (ap >= &host_data->aliases[MAX_NR_ALIASES - 1])
620 continue;
621 n = dn_expand (answer->buf, end_of_message, cp, tbuf, sizeof tbuf);
622 if (n < 0 || (*name_ok) (tbuf) == 0)
624 ++had_error;
625 continue;
627 cp += n;
628 /* Store alias. */
629 *ap++ = bp;
630 n = strlen (bp) + 1; /* For the \0. */
631 if (__builtin_expect (n, 0) >= MAXHOSTNAMELEN)
633 ++had_error;
634 continue;
636 bp += n;
637 linebuflen -= n;
638 /* Get canonical name. */
639 n = strlen (tbuf) + 1; /* For the \0. */
640 if (__builtin_expect (n > linebuflen, 0))
641 goto too_small;
642 if (__builtin_expect (n, 0) >= MAXHOSTNAMELEN)
644 ++had_error;
645 continue;
647 result->h_name = bp;
648 bp = __mempcpy (bp, tbuf, n); /* Cannot overflow. */
649 linebuflen -= n;
650 continue;
653 if (qtype == T_PTR && type == T_CNAME)
655 n = dn_expand (answer->buf, end_of_message, cp, tbuf, sizeof tbuf);
656 if (n < 0 || res_dnok (tbuf) == 0)
658 ++had_error;
659 continue;
661 cp += n;
662 /* Get canonical name. */
663 n = strlen (tbuf) + 1; /* For the \0. */
664 if (__builtin_expect (n > linebuflen, 0))
665 goto too_small;
666 if (__builtin_expect (n, 0) >= MAXHOSTNAMELEN)
668 ++had_error;
669 continue;
671 tname = bp;
672 bp = __mempcpy (bp, tbuf, n); /* Cannot overflow. */
673 linebuflen -= n;
674 continue;
676 if (__builtin_expect (type == T_SIG, 0)
677 || __builtin_expect (type == T_KEY, 0)
678 || __builtin_expect (type == T_NXT, 0))
680 /* We don't support DNSSEC yet. For now, ignore the record
681 and send a low priority message to syslog. */
682 syslog (LOG_DEBUG | LOG_AUTH,
683 "gethostby*.getanswer: asked for \"%s %s %s\", got type \"%s\"",
684 qname, p_class (C_IN), p_type(qtype), p_type (type));
685 cp += n;
686 continue;
689 if (type == T_A && qtype == T_AAAA && map)
690 have_to_map = 1;
691 else if (__builtin_expect (type != qtype, 0))
693 syslog (LOG_NOTICE | LOG_AUTH,
694 "gethostby*.getanswer: asked for \"%s %s %s\", got type \"%s\"",
695 qname, p_class (C_IN), p_type (qtype), p_type (type));
696 cp += n;
697 continue; /* XXX - had_error++ ? */
700 switch (type)
702 case T_PTR:
703 if (__strcasecmp (tname, bp) != 0)
705 syslog (LOG_NOTICE | LOG_AUTH, AskedForGot, qname, bp);
706 cp += n;
707 continue; /* XXX - had_error++ ? */
710 n = __ns_name_unpack (answer->buf, end_of_message, cp,
711 packtmp, sizeof packtmp);
712 if (n != -1 && __ns_name_ntop (packtmp, bp, linebuflen) == -1)
714 if (__builtin_expect (errno, 0) == EMSGSIZE)
715 goto too_small;
717 n = -1;
720 if (n < 0 || res_hnok (bp) == 0)
722 ++had_error;
723 break;
725 #if MULTI_PTRS_ARE_ALIASES
726 cp += n;
727 if (haveanswer == 0)
728 result->h_name = bp;
729 else if (ap < &host_data->aliases[MAXALIASES-1])
730 *ap++ = bp;
731 else
732 n = -1;
733 if (n != -1)
735 n = strlen (bp) + 1; /* for the \0 */
736 if (__builtin_expect (n, 0) >= MAXHOSTNAMELEN)
738 ++had_error;
739 break;
741 bp += n;
742 linebuflen -= n;
744 break;
745 #else
746 result->h_name = bp;
747 if (have_to_map)
749 n = strlen (bp) + 1; /* for the \0 */
750 if (n >= MAXHOSTNAMELEN)
752 ++had_error;
753 break;
755 bp += n;
756 linebuflen -= n;
757 map_v4v6_hostent (result, &bp, &linebuflen);
759 *h_errnop = NETDB_SUCCESS;
760 return NSS_STATUS_SUCCESS;
761 #endif
762 case T_A:
763 case T_AAAA:
764 if (__builtin_expect (strcasecmp (result->h_name, bp), 0) != 0)
766 syslog (LOG_NOTICE | LOG_AUTH, AskedForGot, result->h_name, bp);
767 cp += n;
768 continue; /* XXX - had_error++ ? */
770 if (n != result->h_length)
772 cp += n;
773 continue;
775 if (!haveanswer)
777 register int nn;
779 if (ttlp != NULL && ttl != 0)
780 *ttlp = ttl;
781 if (canonp != NULL)
782 *canonp = bp;
783 result->h_name = bp;
784 nn = strlen (bp) + 1; /* for the \0 */
785 bp += nn;
786 linebuflen -= nn;
789 linebuflen -= sizeof (align) - ((u_long) bp % sizeof (align));
790 bp += sizeof (align) - ((u_long) bp % sizeof (align));
792 if (__builtin_expect (n > linebuflen, 0))
793 goto too_small;
794 bp = __mempcpy (*hap++ = bp, cp, n);
795 cp += n;
796 linebuflen -= n;
797 break;
798 default:
799 abort ();
801 if (had_error == 0)
802 ++haveanswer;
805 if (haveanswer > 0)
807 *ap = NULL;
808 *hap = NULL;
809 #if defined RESOLVSORT
811 * Note: we sort even if host can take only one address
812 * in its return structures - should give it the "best"
813 * address in that case, not some random one
815 if (_res.nsort && haveanswer > 1 && qtype == T_A)
816 addrsort (host_data->h_addr_ptrs, haveanswer);
817 #endif /*RESOLVSORT*/
819 if (result->h_name == NULL)
821 n = strlen (qname) + 1; /* For the \0. */
822 if (n > linebuflen)
823 goto too_small;
824 if (n >= MAXHOSTNAMELEN)
825 goto no_recovery;
826 result->h_name = bp;
827 bp = __mempcpy (bp, qname, n); /* Cannot overflow. */
828 linebuflen -= n;
831 if (have_to_map)
832 map_v4v6_hostent (result, &bp, &linebuflen);
833 *h_errnop = NETDB_SUCCESS;
834 return NSS_STATUS_SUCCESS;
836 no_recovery:
837 *h_errnop = NO_RECOVERY;
838 *errnop = ENOENT;
839 return NSS_STATUS_TRYAGAIN;