Update.
[glibc.git] / resolv / nss_dns / dns-host.c
blobf6a2fa65d680888cd0764ae29f34e5ffae23fba0
1 /* Copyright (C) 1996-2003, 2004 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 enum nss_status
131 _nss_dns_gethostbyname3_r (const char *name, int af, struct hostent *result,
132 char *buffer, size_t buflen, int *errnop,
133 int *h_errnop, int32_t *ttlp, char **canonp)
135 union
137 querybuf *buf;
138 u_char *ptr;
139 } host_buffer;
140 querybuf *orig_host_buffer;
141 char tmp[NS_MAXDNAME];
142 int size, type, n;
143 const char *cp;
144 int map = 0;
145 int olderr = errno;
146 enum nss_status status;
148 if (__res_maybe_init (&_res, 0) == -1)
149 return NSS_STATUS_UNAVAIL;
151 switch (af) {
152 case AF_INET:
153 size = INADDRSZ;
154 type = T_A;
155 break;
156 case AF_INET6:
157 size = IN6ADDRSZ;
158 type = T_AAAA;
159 break;
160 default:
161 *h_errnop = NO_DATA;
162 *errnop = EAFNOSUPPORT;
163 return NSS_STATUS_UNAVAIL;
166 result->h_addrtype = af;
167 result->h_length = size;
170 * if there aren't any dots, it could be a user-level alias.
171 * this is also done in res_query() since we are not the only
172 * function that looks up host names.
174 if (strchr (name, '.') == NULL
175 && (cp = res_hostalias (&_res, name, tmp, sizeof (tmp))) != NULL)
176 name = cp;
178 host_buffer.buf = orig_host_buffer = (querybuf *) alloca (1024);
180 n = __libc_res_nsearch (&_res, name, C_IN, type, host_buffer.buf->buf,
181 1024, &host_buffer.ptr);
182 if (n < 0)
184 enum nss_status status = (errno == ECONNREFUSED
185 ? NSS_STATUS_UNAVAIL : NSS_STATUS_NOTFOUND);
186 *h_errnop = h_errno;
187 if (h_errno == TRY_AGAIN)
188 *errnop = EAGAIN;
189 else
190 __set_errno (olderr);
192 /* If we are looking for a IPv6 address and mapping is enabled
193 by having the RES_USE_INET6 bit in _res.options set, we try
194 another lookup. */
195 if (af == AF_INET6 && (_res.options & RES_USE_INET6))
196 n = __libc_res_nsearch (&_res, name, C_IN, T_A, host_buffer.buf->buf,
197 host_buffer.buf != orig_host_buffer
198 ? MAXPACKET : 1024, &host_buffer.ptr);
200 if (n < 0)
202 if (host_buffer.buf != orig_host_buffer)
203 free (host_buffer.buf);
204 return status;
207 map = 1;
209 result->h_addrtype = AF_INET;
210 result->h_length = INADDRSZ;;
213 status = getanswer_r (host_buffer.buf, n, name, type, result, buffer, buflen,
214 errnop, h_errnop, map, ttlp, canonp);
215 if (host_buffer.buf != orig_host_buffer)
216 free (host_buffer.buf);
217 return status;
221 enum nss_status
222 _nss_dns_gethostbyname2_r (const char *name, int af, struct hostent *result,
223 char *buffer, size_t buflen, int *errnop,
224 int *h_errnop)
226 return _nss_dns_gethostbyname3_r (name, af, result, buffer, buflen, errnop,
227 h_errnop, NULL, NULL);
231 enum nss_status
232 _nss_dns_gethostbyname_r (const char *name, struct hostent *result,
233 char *buffer, size_t buflen, int *errnop,
234 int *h_errnop)
236 enum nss_status status = NSS_STATUS_NOTFOUND;
238 if (_res.options & RES_USE_INET6)
239 status = _nss_dns_gethostbyname2_r (name, AF_INET6, result, buffer,
240 buflen, errnop, h_errnop);
241 if (status == NSS_STATUS_NOTFOUND)
242 status = _nss_dns_gethostbyname2_r (name, AF_INET, result, buffer,
243 buflen, errnop, h_errnop);
245 return status;
249 enum nss_status
250 _nss_dns_gethostbyaddr_r (const void *addr, socklen_t len, int af,
251 struct hostent *result, char *buffer, size_t buflen,
252 int *errnop, int *h_errnop)
254 static const u_char mapped[] = { 0,0, 0,0, 0,0, 0,0, 0,0, 0xff,0xff };
255 static const u_char tunnelled[] = { 0,0, 0,0, 0,0, 0,0, 0,0, 0,0 };
256 static const u_char v6local[] = { 0,0, 0,1 };
257 const u_char *uaddr = (const u_char *)addr;
258 struct host_data
260 char *aliases[MAX_NR_ALIASES];
261 unsigned char host_addr[16]; /* IPv4 or IPv6 */
262 char *h_addr_ptrs[MAX_NR_ADDRS + 1];
263 char linebuffer[0];
264 } *host_data = (struct host_data *) buffer;
265 union
267 querybuf *buf;
268 u_char *ptr;
269 } host_buffer;
270 querybuf *orig_host_buffer;
271 char qbuf[MAXDNAME+1], *qp = NULL;
272 size_t size;
273 int n, status;
274 int olderr = errno;
276 if (__res_maybe_init (&_res, 0) == -1)
277 return NSS_STATUS_UNAVAIL;
279 if (af == AF_INET6 && len == IN6ADDRSZ
280 && (memcmp (uaddr, mapped, sizeof mapped) == 0
281 || (memcmp (uaddr, tunnelled, sizeof tunnelled) == 0
282 && memcmp (&uaddr[sizeof tunnelled], v6local, sizeof v6local))))
284 /* Unmap. */
285 addr += sizeof mapped;
286 uaddr += sizeof mapped;
287 af = AF_INET;
288 len = INADDRSZ;
291 switch (af)
293 case AF_INET:
294 size = INADDRSZ;
295 break;
296 case AF_INET6:
297 size = IN6ADDRSZ;
298 break;
299 default:
300 *errnop = EAFNOSUPPORT;
301 *h_errnop = NETDB_INTERNAL;
302 return NSS_STATUS_UNAVAIL;
304 if (size > len)
306 *errnop = EAFNOSUPPORT;
307 *h_errnop = NETDB_INTERNAL;
308 return NSS_STATUS_UNAVAIL;
311 host_buffer.buf = orig_host_buffer = (querybuf *) alloca (1024);
313 switch (af)
315 case AF_INET:
316 sprintf (qbuf, "%u.%u.%u.%u.in-addr.arpa", (uaddr[3] & 0xff),
317 (uaddr[2] & 0xff), (uaddr[1] & 0xff), (uaddr[0] & 0xff));
318 break;
319 case AF_INET6:
320 /* Only lookup with the byte string format if the user wants it. */
321 if (__builtin_expect (_res.options & RES_USEBSTRING, 0))
323 qp = stpcpy (qbuf, "\\[x");
324 for (n = 0; n < IN6ADDRSZ; ++n)
325 qp += sprintf (qp, "%02hhx", uaddr[n]);
326 strcpy (qp, "].ip6.arpa");
327 n = __libc_res_nquery (&_res, qbuf, C_IN, T_PTR,
328 host_buffer.buf->buf, 1024, &host_buffer.ptr);
329 if (n >= 0)
330 goto got_it_already;
332 qp = qbuf;
333 for (n = IN6ADDRSZ - 1; n >= 0; n--)
335 static const char nibblechar[16] = "0123456789abcdef";
336 *qp++ = nibblechar[uaddr[n] & 0xf];
337 *qp++ = '.';
338 *qp++ = nibblechar[(uaddr[n] >> 4) & 0xf];
339 *qp++ = '.';
341 strcpy(qp, "ip6.arpa");
342 break;
343 default:
344 /* Cannot happen. */
345 break;
348 n = __libc_res_nquery (&_res, qbuf, C_IN, T_PTR, host_buffer.buf->buf,
349 1024, &host_buffer.ptr);
350 if (n < 0 && af == AF_INET6 && (_res.options & RES_NOIP6DOTINT) == 0)
352 strcpy (qp, "ip6.int");
353 n = __libc_res_nquery (&_res, qbuf, C_IN, T_PTR, host_buffer.buf->buf,
354 host_buffer.buf != orig_host_buffer
355 ? MAXPACKET : 1024, &host_buffer.ptr);
357 if (n < 0)
359 *h_errnop = h_errno;
360 __set_errno (olderr);
361 if (host_buffer.buf != orig_host_buffer)
362 free (host_buffer.buf);
363 return errno == ECONNREFUSED ? NSS_STATUS_UNAVAIL : NSS_STATUS_NOTFOUND;
366 got_it_already:
367 status = getanswer_r (host_buffer.buf, n, qbuf, T_PTR, result, buffer, buflen,
368 errnop, h_errnop, 0 /* XXX */, NULL, NULL);
369 if (host_buffer.buf != orig_host_buffer)
370 free (host_buffer.buf);
371 if (status != NSS_STATUS_SUCCESS)
373 *h_errnop = h_errno;
374 *errnop = errno;
375 return status;
378 #ifdef SUNSECURITY
379 This is not implemented because it is not possible to use the current
380 source from bind in a multi-threaded program.
381 #endif
383 result->h_addrtype = af;
384 result->h_length = len;
385 memcpy (host_data->host_addr, addr, len);
386 host_data->h_addr_ptrs[0] = (char *) host_data->host_addr;
387 host_data->h_addr_ptrs[1] = NULL;
388 #if 0
389 /* XXX I think this is wrong. Why should an IPv4 address be
390 converted to IPv6 if the user explicitly asked for IPv4? */
391 if (af == AF_INET && (_res.options & RES_USE_INET6))
393 map_v4v6_address ((char *) host_data->host_addr,
394 (char *) host_data->host_addr);
395 result->h_addrtype = AF_INET6;
396 result->h_length = IN6ADDRSZ;
398 #endif
399 *h_errnop = NETDB_SUCCESS;
400 return NSS_STATUS_SUCCESS;
403 #ifdef RESOLVSORT
404 static void addrsort (char **ap, int num);
406 static void
407 addrsort (char **ap, int num)
409 int i, j;
410 char **p;
411 short aval[MAX_NR_ADDRS];
412 int needsort = 0;
414 p = ap;
415 if (num > MAX_NR_ADDRS)
416 num = MAX_NR_ADDRS;
417 for (i = 0; i < num; i++, p++)
419 for (j = 0 ; (unsigned)j < _res.nsort; j++)
420 if (_res.sort_list[j].addr.s_addr ==
421 (((struct in_addr *)(*p))->s_addr & _res.sort_list[j].mask))
422 break;
423 aval[i] = j;
424 if (needsort == 0 && i > 0 && j < aval[i-1])
425 needsort = i;
427 if (!needsort)
428 return;
430 while (needsort++ < num)
431 for (j = needsort - 2; j >= 0; j--)
432 if (aval[j] > aval[j+1])
434 char *hp;
436 i = aval[j];
437 aval[j] = aval[j+1];
438 aval[j+1] = i;
440 hp = ap[j];
441 ap[j] = ap[j+1];
442 ap[j+1] = hp;
444 else
445 break;
447 #endif
449 static enum nss_status
450 getanswer_r (const querybuf *answer, int anslen, const char *qname, int qtype,
451 struct hostent *result, char *buffer, size_t buflen,
452 int *errnop, int *h_errnop, int map, int32_t *ttlp, char **canonp)
454 struct host_data
456 char *aliases[MAX_NR_ALIASES];
457 unsigned char host_addr[16]; /* IPv4 or IPv6 */
458 char *h_addr_ptrs[0];
459 } *host_data = (struct host_data *) buffer;
460 int linebuflen = buflen - sizeof (struct host_data);
461 register const HEADER *hp;
462 const u_char *end_of_message, *cp;
463 int n, ancount, qdcount;
464 int haveanswer, had_error;
465 char *bp, **ap, **hap;
466 char tbuf[MAXDNAME];
467 const char *tname;
468 int (*name_ok) (const char *);
469 u_char packtmp[NS_MAXCDNAME];
470 int have_to_map = 0;
471 int32_t ttl = 0;
473 if (__builtin_expect (linebuflen, 0) < 0)
475 /* The buffer is too small. */
476 too_small:
477 *errnop = ERANGE;
478 *h_errnop = NETDB_INTERNAL;
479 return NSS_STATUS_TRYAGAIN;
482 tname = qname;
483 result->h_name = NULL;
484 end_of_message = answer->buf + anslen;
485 switch (qtype)
487 case T_A:
488 case T_AAAA:
489 name_ok = res_hnok;
490 break;
491 case T_PTR:
492 name_ok = res_dnok;
493 break;
494 default:
495 *errnop = ENOENT;
496 return NSS_STATUS_UNAVAIL; /* XXX should be abort(); */
500 * find first satisfactory answer
502 hp = &answer->hdr;
503 ancount = ntohs (hp->ancount);
504 qdcount = ntohs (hp->qdcount);
505 cp = answer->buf + HFIXEDSZ;
506 if (__builtin_expect (qdcount, 1) != 1)
508 *h_errnop = NO_RECOVERY;
509 return NSS_STATUS_UNAVAIL;
511 if (sizeof (struct host_data) + (ancount + 1) * sizeof (char *) >= buflen)
512 goto too_small;
513 bp = (char *) &host_data->h_addr_ptrs[ancount + 1];
514 linebuflen -= (ancount + 1) * sizeof (char *);
516 n = __ns_name_unpack (answer->buf, end_of_message, cp,
517 packtmp, sizeof packtmp);
518 if (n != -1 && __ns_name_ntop (packtmp, bp, linebuflen) == -1)
520 if (__builtin_expect (errno, 0) == EMSGSIZE)
521 goto too_small;
523 n = -1;
526 if (n > 0 && bp[0] == '.')
527 bp[0] = '\0';
529 if (n < 0 || (*name_ok) (bp) == 0)
531 *errnop = errno;
532 *h_errnop = NO_RECOVERY;
533 return NSS_STATUS_UNAVAIL;
535 cp += n + QFIXEDSZ;
537 if (qtype == T_A || qtype == T_AAAA)
539 /* res_send() has already verified that the query name is the
540 * same as the one we sent; this just gets the expanded name
541 * (i.e., with the succeeding search-domain tacked on).
543 n = strlen (bp) + 1; /* for the \0 */
544 if (n >= MAXHOSTNAMELEN)
546 *h_errnop = NO_RECOVERY;
547 *errnop = ENOENT;
548 return NSS_STATUS_TRYAGAIN;
550 result->h_name = bp;
551 bp += n;
552 linebuflen -= n;
553 if (linebuflen < 0)
554 goto too_small;
555 /* The qname can be abbreviated, but h_name is now absolute. */
556 qname = result->h_name;
559 ap = host_data->aliases;
560 *ap = NULL;
561 result->h_aliases = host_data->aliases;
562 hap = host_data->h_addr_ptrs;
563 *hap = NULL;
564 result->h_addr_list = host_data->h_addr_ptrs;
565 haveanswer = 0;
566 had_error = 0;
568 while (ancount-- > 0 && cp < end_of_message && had_error == 0)
570 int type, class;
572 n = __ns_name_unpack (answer->buf, end_of_message, cp,
573 packtmp, sizeof packtmp);
574 if (n != -1 && __ns_name_ntop (packtmp, bp, linebuflen) == -1)
576 if (__builtin_expect (errno, 0) == EMSGSIZE)
577 goto too_small;
579 n = -1;
582 if (n < 0 || (*name_ok) (bp) == 0)
584 ++had_error;
585 continue;
587 cp += n; /* name */
588 type = ns_get16 (cp);
589 cp += INT16SZ; /* type */
590 class = ns_get16 (cp);
591 cp += INT16SZ; /* class */
592 ttl = ns_get32 (cp);
593 cp += INT32SZ; /* TTL */
594 n = ns_get16 (cp);
595 cp += INT16SZ; /* len */
596 if (class != C_IN)
598 /* XXX - debug? syslog? */
599 cp += n;
600 continue; /* XXX - had_error++ ? */
603 if ((qtype ==T_A || qtype == T_AAAA) && type == T_CNAME)
605 if (ap >= &host_data->aliases[MAX_NR_ALIASES - 1])
606 continue;
607 n = dn_expand (answer->buf, end_of_message, cp, tbuf, sizeof tbuf);
608 if (n < 0 || (*name_ok) (tbuf) == 0)
610 ++had_error;
611 continue;
613 cp += n;
614 /* Store alias. */
615 *ap++ = bp;
616 n = strlen (bp) + 1; /* For the \0. */
617 if (__builtin_expect (n, 0) >= MAXHOSTNAMELEN)
619 ++had_error;
620 continue;
622 bp += n;
623 linebuflen -= n;
624 /* Get canonical name. */
625 n = strlen (tbuf) + 1; /* For the \0. */
626 if (__builtin_expect (n > linebuflen, 0))
627 goto too_small;
628 if (__builtin_expect (n, 0) >= MAXHOSTNAMELEN)
630 ++had_error;
631 continue;
633 result->h_name = bp;
634 bp = __mempcpy (bp, tbuf, n); /* Cannot overflow. */
635 linebuflen -= n;
636 continue;
639 if (qtype == T_PTR && type == T_CNAME)
641 n = dn_expand (answer->buf, end_of_message, cp, tbuf, sizeof tbuf);
642 if (n < 0 || res_dnok (tbuf) == 0)
644 ++had_error;
645 continue;
647 cp += n;
648 /* Get canonical name. */
649 n = strlen (tbuf) + 1; /* For the \0. */
650 if (__builtin_expect (n > linebuflen, 0))
651 goto too_small;
652 if (__builtin_expect (n, 0) >= MAXHOSTNAMELEN)
654 ++had_error;
655 continue;
657 tname = bp;
658 bp = __mempcpy (bp, tbuf, n); /* Cannot overflow. */
659 linebuflen -= n;
660 continue;
662 if (__builtin_expect (type == T_SIG, 0)
663 || __builtin_expect (type == T_KEY, 0)
664 || __builtin_expect (type == T_NXT, 0))
666 /* We don't support DNSSEC yet. For now, ignore the record
667 and send a low priority message to syslog. */
668 syslog (LOG_DEBUG | LOG_AUTH,
669 "gethostby*.getanswer: asked for \"%s %s %s\", got type \"%s\"",
670 qname, p_class (C_IN), p_type(qtype), p_type (type));
671 cp += n;
672 continue;
675 if (type == T_A && qtype == T_AAAA && map)
676 have_to_map = 1;
677 else if (__builtin_expect (type != qtype, 0))
679 syslog (LOG_NOTICE | LOG_AUTH,
680 "gethostby*.getanswer: asked for \"%s %s %s\", got type \"%s\"",
681 qname, p_class (C_IN), p_type (qtype), p_type (type));
682 cp += n;
683 continue; /* XXX - had_error++ ? */
686 switch (type)
688 case T_PTR:
689 if (__strcasecmp (tname, bp) != 0)
691 syslog (LOG_NOTICE | LOG_AUTH, AskedForGot, qname, bp);
692 cp += n;
693 continue; /* XXX - had_error++ ? */
696 n = __ns_name_unpack (answer->buf, end_of_message, cp,
697 packtmp, sizeof packtmp);
698 if (n != -1 && __ns_name_ntop (packtmp, bp, linebuflen) == -1)
700 if (__builtin_expect (errno, 0) == EMSGSIZE)
701 goto too_small;
703 n = -1;
706 if (n < 0 || res_hnok (bp) == 0)
708 ++had_error;
709 break;
711 #if MULTI_PTRS_ARE_ALIASES
712 cp += n;
713 if (haveanswer == 0)
714 result->h_name = bp;
715 else if (ap < &host_data->aliases[MAXALIASES-1])
716 *ap++ = bp;
717 else
718 n = -1;
719 if (n != -1)
721 n = strlen (bp) + 1; /* for the \0 */
722 if (__builtin_expect (n, 0) >= MAXHOSTNAMELEN)
724 ++had_error;
725 break;
727 bp += n;
728 linebuflen -= n;
730 break;
731 #else
732 result->h_name = bp;
733 if (have_to_map)
735 n = strlen (bp) + 1; /* for the \0 */
736 if (n >= MAXHOSTNAMELEN)
738 ++had_error;
739 break;
741 bp += n;
742 linebuflen -= n;
743 map_v4v6_hostent (result, &bp, &linebuflen);
745 *h_errnop = NETDB_SUCCESS;
746 return NSS_STATUS_SUCCESS;
747 #endif
748 case T_A:
749 case T_AAAA:
750 if (__builtin_expect (strcasecmp (result->h_name, bp), 0) != 0)
752 syslog (LOG_NOTICE | LOG_AUTH, AskedForGot, result->h_name, bp);
753 cp += n;
754 continue; /* XXX - had_error++ ? */
756 if (n != result->h_length)
758 cp += n;
759 continue;
761 if (!haveanswer)
763 register int nn;
765 if (ttlp != NULL && ttl != 0)
766 *ttlp = ttl;
767 if (canonp != NULL)
768 *canonp = bp;
769 result->h_name = bp;
770 nn = strlen (bp) + 1; /* for the \0 */
771 bp += nn;
772 linebuflen -= nn;
775 linebuflen -= sizeof (align) - ((u_long) bp % sizeof (align));
776 bp += sizeof (align) - ((u_long) bp % sizeof (align));
778 if (__builtin_expect (n > linebuflen, 0))
779 goto too_small;
780 bp = __mempcpy (*hap++ = bp, cp, n);
781 cp += n;
782 linebuflen -= n;
783 break;
784 default:
785 abort ();
787 if (had_error == 0)
788 ++haveanswer;
791 if (haveanswer > 0)
793 *ap = NULL;
794 *hap = NULL;
795 #if defined RESOLVSORT
797 * Note: we sort even if host can take only one address
798 * in its return structures - should give it the "best"
799 * address in that case, not some random one
801 if (_res.nsort && haveanswer > 1 && qtype == T_A)
802 addrsort (host_data->h_addr_ptrs, haveanswer);
803 #endif /*RESOLVSORT*/
805 if (result->h_name == NULL)
807 n = strlen (qname) + 1; /* For the \0. */
808 if (n > linebuflen)
809 goto too_small;
810 if (n >= MAXHOSTNAMELEN)
811 goto no_recovery;
812 result->h_name = bp;
813 bp = __mempcpy (bp, qname, n); /* Cannot overflow. */
814 linebuflen -= n;
817 if (have_to_map)
818 map_v4v6_hostent (result, &bp, &linebuflen);
819 *h_errnop = NETDB_SUCCESS;
820 return NSS_STATUS_SUCCESS;
822 no_recovery:
823 *h_errnop = NO_RECOVERY;
824 *errnop = ENOENT;
825 return NSS_STATUS_TRYAGAIN;