Optimize for kernels which are known to have the vfork syscall.
[glibc/pb-stable.git] / resolv / nss_dns / dns-host.c
blobf362dd746dea0c3606376802227de97d6128618b
1 /* Copyright (C) 1996-2000, 2001, 2002 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);
130 enum nss_status
131 _nss_dns_gethostbyname2_r (const char *name, int af, struct hostent *result,
132 char *buffer, size_t buflen, int *errnop,
133 int *h_errnop)
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.options & RES_INIT) == 0 && __res_ninit (&_res) == -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);
215 if (host_buffer.buf != orig_host_buffer)
216 free (host_buffer.buf);
217 return status;
221 enum nss_status
222 _nss_dns_gethostbyname_r (const char *name, struct hostent *result,
223 char *buffer, size_t buflen, int *errnop,
224 int *h_errnop)
226 enum nss_status status = NSS_STATUS_NOTFOUND;
228 if (_res.options & RES_USE_INET6)
229 status = _nss_dns_gethostbyname2_r (name, AF_INET6, result, buffer,
230 buflen, errnop, h_errnop);
231 if (status == NSS_STATUS_NOTFOUND)
232 status = _nss_dns_gethostbyname2_r (name, AF_INET, result, buffer,
233 buflen, errnop, h_errnop);
235 return status;
239 enum nss_status
240 _nss_dns_gethostbyaddr_r (const void *addr, socklen_t len, int af,
241 struct hostent *result, char *buffer, size_t buflen,
242 int *errnop, int *h_errnop)
244 static const u_char mapped[] = { 0,0, 0,0, 0,0, 0,0, 0,0, 0xff,0xff };
245 static const u_char tunnelled[] = { 0,0, 0,0, 0,0, 0,0, 0,0, 0,0 };
246 static const u_char v6local[] = { 0,0, 0,1 };
247 const u_char *uaddr = (const u_char *)addr;
248 struct host_data
250 char *aliases[MAX_NR_ALIASES];
251 unsigned char host_addr[16]; /* IPv4 or IPv6 */
252 char *h_addr_ptrs[MAX_NR_ADDRS + 1];
253 char linebuffer[0];
254 } *host_data = (struct host_data *) buffer;
255 union
257 querybuf *buf;
258 u_char *ptr;
259 } host_buffer;
260 querybuf *orig_host_buffer;
261 char qbuf[MAXDNAME+1], *qp = NULL;
262 size_t size;
263 int n, status;
264 int olderr = errno;
266 if ((_res.options & RES_INIT) == 0 && __res_ninit (&_res) == -1)
267 return NSS_STATUS_UNAVAIL;
269 if (af == AF_INET6 && len == IN6ADDRSZ
270 && (memcmp (uaddr, mapped, sizeof mapped) == 0
271 || (memcmp (uaddr, tunnelled, sizeof tunnelled) == 0
272 && memcmp (&uaddr[sizeof tunnelled], v6local, sizeof v6local))))
274 /* Unmap. */
275 addr += sizeof mapped;
276 uaddr += sizeof mapped;
277 af = AF_INET;
278 len = INADDRSZ;
281 switch (af)
283 case AF_INET:
284 size = INADDRSZ;
285 break;
286 case AF_INET6:
287 size = IN6ADDRSZ;
288 break;
289 default:
290 *errnop = EAFNOSUPPORT;
291 *h_errnop = NETDB_INTERNAL;
292 return NSS_STATUS_UNAVAIL;
294 if (size > len)
296 *errnop = EAFNOSUPPORT;
297 *h_errnop = NETDB_INTERNAL;
298 return NSS_STATUS_UNAVAIL;
301 switch (af)
303 case AF_INET:
304 sprintf (qbuf, "%u.%u.%u.%u.in-addr.arpa", (uaddr[3] & 0xff),
305 (uaddr[2] & 0xff), (uaddr[1] & 0xff), (uaddr[0] & 0xff));
306 break;
307 case AF_INET6:
308 qp = qbuf;
309 for (n = IN6ADDRSZ - 1; n >= 0; n--)
310 qp += sprintf (qp, "%x.%x.", uaddr[n] & 0xf, (uaddr[n] >> 4) & 0xf);
311 strcpy (qp, "ip6.arpa");
312 break;
313 default:
314 /* Cannot happen. */
315 break;
318 host_buffer.buf = orig_host_buffer = (querybuf *) alloca (1024);
320 n = __libc_res_nquery (&_res, qbuf, C_IN, T_PTR, host_buffer.buf->buf,
321 1024, &host_buffer.ptr);
322 if (n < 0 && af == AF_INET6)
324 strcpy (qp, "ip6.int");
325 n = __libc_res_nquery (&_res, qbuf, C_IN, T_PTR, host_buffer.buf->buf,
326 host_buffer.buf != orig_host_buffer
327 ? MAXPACKET : 1024, &host_buffer.ptr);
329 if (n < 0)
331 *h_errnop = h_errno;
332 __set_errno (olderr);
333 if (host_buffer.buf != orig_host_buffer)
334 free (host_buffer.buf);
335 return errno == ECONNREFUSED ? NSS_STATUS_UNAVAIL : NSS_STATUS_NOTFOUND;
338 status = getanswer_r (host_buffer.buf, n, qbuf, T_PTR, result, buffer, buflen,
339 errnop, h_errnop, 0 /* XXX */);
340 if (host_buffer.buf != orig_host_buffer)
341 free (host_buffer.buf);
342 if (status != NSS_STATUS_SUCCESS)
344 *h_errnop = h_errno;
345 *errnop = errno;
346 return status;
349 #ifdef SUNSECURITY
350 This is not implemented because it is not possible to use the current
351 source from bind in a multi-threaded program.
352 #endif
354 result->h_addrtype = af;
355 result->h_length = len;
356 memcpy (host_data->host_addr, addr, len);
357 host_data->h_addr_ptrs[0] = (char *) host_data->host_addr;
358 host_data->h_addr_ptrs[1] = NULL;
359 #if 0
360 /* XXX I think this is wrong. Why should an IPv4 address be
361 converted to IPv6 if the user explicitly asked for IPv4? */
362 if (af == AF_INET && (_res.options & RES_USE_INET6))
364 map_v4v6_address ((char *) host_data->host_addr,
365 (char *) host_data->host_addr);
366 result->h_addrtype = AF_INET6;
367 result->h_length = IN6ADDRSZ;
369 #endif
370 *h_errnop = NETDB_SUCCESS;
371 return NSS_STATUS_SUCCESS;
374 #ifdef RESOLVSORT
375 static void addrsort (char **ap, int num);
377 static void
378 addrsort (char **ap, int num)
380 int i, j;
381 char **p;
382 short aval[MAX_NR_ADDRS];
383 int needsort = 0;
385 p = ap;
386 if (num > MAX_NR_ADDRS)
387 num = MAX_NR_ADDRS;
388 for (i = 0; i < num; i++, p++)
390 for (j = 0 ; (unsigned)j < _res.nsort; j++)
391 if (_res.sort_list[j].addr.s_addr ==
392 (((struct in_addr *)(*p))->s_addr & _res.sort_list[j].mask))
393 break;
394 aval[i] = j;
395 if (needsort == 0 && i > 0 && j < aval[i-1])
396 needsort = i;
398 if (!needsort)
399 return;
401 while (needsort++ < num)
402 for (j = needsort - 2; j >= 0; j--)
403 if (aval[j] > aval[j+1])
405 char *hp;
407 i = aval[j];
408 aval[j] = aval[j+1];
409 aval[j+1] = i;
411 hp = ap[j];
412 ap[j] = ap[j+1];
413 ap[j+1] = hp;
415 else
416 break;
418 #endif
420 static enum nss_status
421 getanswer_r (const querybuf *answer, int anslen, const char *qname, int qtype,
422 struct hostent *result, char *buffer, size_t buflen,
423 int *errnop, int *h_errnop, int map)
425 struct host_data
427 char *aliases[MAX_NR_ALIASES];
428 unsigned char host_addr[16]; /* IPv4 or IPv6 */
429 char *h_addr_ptrs[0];
430 } *host_data = (struct host_data *) buffer;
431 int linebuflen = buflen - sizeof (struct host_data);
432 register const HEADER *hp;
433 const u_char *end_of_message, *cp;
434 int n, ancount, qdcount;
435 int haveanswer, had_error;
436 char *bp, **ap, **hap;
437 char tbuf[MAXDNAME];
438 const char *tname;
439 int (*name_ok) (const char *);
440 u_char packtmp[NS_MAXCDNAME];
441 int have_to_map = 0;
443 if (__builtin_expect (linebuflen, 0) < 0)
445 /* The buffer is too small. */
446 too_small:
447 *errnop = ERANGE;
448 *h_errnop = NETDB_INTERNAL;
449 return NSS_STATUS_TRYAGAIN;
452 tname = qname;
453 result->h_name = NULL;
454 end_of_message = answer->buf + anslen;
455 switch (qtype)
457 case T_A:
458 case T_AAAA:
459 name_ok = res_hnok;
460 break;
461 case T_PTR:
462 name_ok = res_dnok;
463 break;
464 default:
465 *errnop = ENOENT;
466 return NSS_STATUS_UNAVAIL; /* XXX should be abort(); */
470 * find first satisfactory answer
472 hp = &answer->hdr;
473 ancount = ntohs (hp->ancount);
474 qdcount = ntohs (hp->qdcount);
475 cp = answer->buf + HFIXEDSZ;
476 if (__builtin_expect (qdcount, 1) != 1)
478 *h_errnop = NO_RECOVERY;
479 return NSS_STATUS_UNAVAIL;
481 if (sizeof (struct host_data) + (ancount + 1) * sizeof (char *) >= buflen)
482 goto too_small;
483 bp = (char *) &host_data->h_addr_ptrs[ancount + 1];
484 linebuflen -= (ancount + 1) * sizeof (char *);
486 n = __ns_name_unpack (answer->buf, end_of_message, cp,
487 packtmp, sizeof packtmp);
488 if (n != -1 && __ns_name_ntop (packtmp, bp, linebuflen) == -1)
490 if (__builtin_expect (errno, 0) == EMSGSIZE)
491 goto too_small;
493 n = -1;
496 if (n > 0 && bp[0] == '.')
497 bp[0] = '\0';
499 if (n < 0 || (*name_ok) (bp) == 0)
501 *errnop = errno;
502 *h_errnop = NO_RECOVERY;
503 return NSS_STATUS_UNAVAIL;
505 cp += n + QFIXEDSZ;
507 if (qtype == T_A || qtype == T_AAAA)
509 /* res_send() has already verified that the query name is the
510 * same as the one we sent; this just gets the expanded name
511 * (i.e., with the succeeding search-domain tacked on).
513 n = strlen (bp) + 1; /* for the \0 */
514 if (n >= MAXHOSTNAMELEN)
516 *h_errnop = NO_RECOVERY;
517 *errnop = ENOENT;
518 return NSS_STATUS_TRYAGAIN;
520 result->h_name = bp;
521 bp += n;
522 linebuflen -= n;
523 if (linebuflen < 0)
524 goto too_small;
525 /* The qname can be abbreviated, but h_name is now absolute. */
526 qname = result->h_name;
529 ap = host_data->aliases;
530 *ap = NULL;
531 result->h_aliases = host_data->aliases;
532 hap = host_data->h_addr_ptrs;
533 *hap = NULL;
534 result->h_addr_list = host_data->h_addr_ptrs;
535 haveanswer = 0;
536 had_error = 0;
538 while (ancount-- > 0 && cp < end_of_message && had_error == 0)
540 int type, class;
542 n = __ns_name_unpack (answer->buf, end_of_message, cp,
543 packtmp, sizeof packtmp);
544 if (n != -1 && __ns_name_ntop (packtmp, bp, linebuflen) == -1)
546 if (__builtin_expect (errno, 0) == EMSGSIZE)
547 goto too_small;
549 n = -1;
552 if (n < 0 || (*name_ok) (bp) == 0)
554 ++had_error;
555 continue;
557 cp += n; /* name */
558 type = ns_get16 (cp);
559 cp += INT16SZ; /* type */
560 class = ns_get16 (cp);
561 cp += INT16SZ + INT32SZ; /* class, TTL */
562 n = ns_get16 (cp);
563 cp += INT16SZ; /* len */
564 if (class != C_IN)
566 /* XXX - debug? syslog? */
567 cp += n;
568 continue; /* XXX - had_error++ ? */
571 if ((qtype ==T_A || qtype == T_AAAA) && type == T_CNAME)
573 if (ap >= &host_data->aliases[MAX_NR_ALIASES - 1])
574 continue;
575 n = dn_expand (answer->buf, end_of_message, cp, tbuf, sizeof tbuf);
576 if (n < 0 || (*name_ok) (tbuf) == 0)
578 ++had_error;
579 continue;
581 cp += n;
582 /* Store alias. */
583 *ap++ = bp;
584 n = strlen (bp) + 1; /* For the \0. */
585 if (__builtin_expect (n, 0) >= MAXHOSTNAMELEN)
587 ++had_error;
588 continue;
590 bp += n;
591 linebuflen -= n;
592 /* Get canonical name. */
593 n = strlen (tbuf) + 1; /* For the \0. */
594 if (__builtin_expect (n > linebuflen, 0))
595 goto too_small;
596 if (__builtin_expect (n, 0) >= MAXHOSTNAMELEN)
598 ++had_error;
599 continue;
601 result->h_name = bp;
602 bp = __mempcpy (bp, tbuf, n); /* Cannot overflow. */
603 linebuflen -= n;
604 continue;
607 if (qtype == T_PTR && type == T_CNAME)
609 n = dn_expand (answer->buf, end_of_message, cp, tbuf, sizeof tbuf);
610 if (n < 0 || res_dnok (tbuf) == 0)
612 ++had_error;
613 continue;
615 cp += n;
616 /* Get canonical name. */
617 n = strlen (tbuf) + 1; /* For the \0. */
618 if (__builtin_expect (n > linebuflen, 0))
619 goto too_small;
620 if (__builtin_expect (n, 0) >= MAXHOSTNAMELEN)
622 ++had_error;
623 continue;
625 tname = bp;
626 bp = __mempcpy (bp, tbuf, n); /* Cannot overflow. */
627 linebuflen -= n;
628 continue;
630 if (__builtin_expect (type == T_SIG, 0)
631 || __builtin_expect (type == T_KEY, 0)
632 || __builtin_expect (type == T_NXT, 0))
634 /* We don't support DNSSEC yet. For now, ignore the record
635 and send a low priority message to syslog. */
636 syslog (LOG_DEBUG | LOG_AUTH,
637 "gethostby*.getanswer: asked for \"%s %s %s\", got type \"%s\"",
638 qname, p_class (C_IN), p_type(qtype), p_type (type));
639 cp += n;
640 continue;
643 if (type == T_A && qtype == T_AAAA && map)
644 have_to_map = 1;
645 else if (__builtin_expect (type != qtype, 0))
647 syslog (LOG_NOTICE | LOG_AUTH,
648 "gethostby*.getanswer: asked for \"%s %s %s\", got type \"%s\"",
649 qname, p_class (C_IN), p_type (qtype), p_type (type));
650 cp += n;
651 continue; /* XXX - had_error++ ? */
654 switch (type)
656 case T_PTR:
657 if (__strcasecmp (tname, bp) != 0)
659 syslog (LOG_NOTICE | LOG_AUTH, AskedForGot, qname, bp);
660 cp += n;
661 continue; /* XXX - had_error++ ? */
664 n = __ns_name_unpack (answer->buf, end_of_message, cp,
665 packtmp, sizeof packtmp);
666 if (n != -1 && __ns_name_ntop (packtmp, bp, linebuflen) == -1)
668 if (__builtin_expect (errno, 0) == EMSGSIZE)
669 goto too_small;
671 n = -1;
674 if (n < 0 || res_hnok (bp) == 0)
676 ++had_error;
677 break;
679 #if MULTI_PTRS_ARE_ALIASES
680 cp += n;
681 if (haveanswer == 0)
682 result->h_name = bp;
683 else if (ap < &host_data->aliases[MAXALIASES-1])
684 *ap++ = bp;
685 else
686 n = -1;
687 if (n != -1)
689 n = strlen (bp) + 1; /* for the \0 */
690 if (__builtin_expect (n, 0) >= MAXHOSTNAMELEN)
692 ++had_error;
693 break;
695 bp += n;
696 linebuflen -= n;
698 break;
699 #else
700 result->h_name = bp;
701 if (have_to_map)
703 n = strlen (bp) + 1; /* for the \0 */
704 if (n >= MAXHOSTNAMELEN)
706 ++had_error;
707 break;
709 bp += n;
710 linebuflen -= n;
711 map_v4v6_hostent (result, &bp, &linebuflen);
713 *h_errnop = NETDB_SUCCESS;
714 return NSS_STATUS_SUCCESS;
715 #endif
716 case T_A:
717 case T_AAAA:
718 if (__builtin_expect (strcasecmp (result->h_name, bp), 0) != 0)
720 syslog (LOG_NOTICE | LOG_AUTH, AskedForGot, result->h_name, bp);
721 cp += n;
722 continue; /* XXX - had_error++ ? */
724 if (n != result->h_length)
726 cp += n;
727 continue;
729 if (!haveanswer)
731 register int nn;
733 result->h_name = bp;
734 nn = strlen (bp) + 1; /* for the \0 */
735 bp += nn;
736 linebuflen -= nn;
739 linebuflen -= sizeof (align) - ((u_long) bp % sizeof (align));
740 bp += sizeof (align) - ((u_long) bp % sizeof (align));
742 if (__builtin_expect (n > linebuflen, 0))
743 goto too_small;
744 bp = __mempcpy (*hap++ = bp, cp, n);
745 cp += n;
746 linebuflen -= n;
747 break;
748 default:
749 abort ();
751 if (had_error == 0)
752 ++haveanswer;
755 if (haveanswer > 0)
757 *ap = NULL;
758 *hap = NULL;
759 #if defined RESOLVSORT
761 * Note: we sort even if host can take only one address
762 * in its return structures - should give it the "best"
763 * address in that case, not some random one
765 if (_res.nsort && haveanswer > 1 && qtype == T_A)
766 addrsort (host_data->h_addr_ptrs, haveanswer);
767 #endif /*RESOLVSORT*/
769 if (result->h_name == NULL)
771 n = strlen (qname) + 1; /* For the \0. */
772 if (n > linebuflen)
773 goto too_small;
774 if (n >= MAXHOSTNAMELEN)
775 goto no_recovery;
776 result->h_name = bp;
777 bp = __mempcpy (bp, qname, n); /* Cannot overflow. */
778 linebuflen -= n;
781 if (have_to_map)
782 map_v4v6_hostent (result, &bp, &linebuflen);
783 *h_errnop = NETDB_SUCCESS;
784 return NSS_STATUS_SUCCESS;
786 no_recovery:
787 *h_errnop = NO_RECOVERY;
788 *errnop = ENOENT;
789 return NSS_STATUS_TRYAGAIN;