1 /* resolv.c: DNS Resolver
3 * Copyright (C) 1998 Kenneth Albanowski <kjahds@kjahds.com>,
4 * The Silver Hammer Group, Ltd.
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
13 * Portions Copyright (c) 1985, 1993
14 * The Regents of the University of California. All rights reserved.
16 * Redistribution and use in source and binary forms, with or without
17 * modification, are permitted provided that the following conditions
19 * 1. Redistributions of source code must retain the above copyright
20 * notice, this list of conditions and the following disclaimer.
21 * 2. Redistributions in binary form must reproduce the above copyright
22 * notice, this list of conditions and the following disclaimer in the
23 * documentation and/or other materials provided with the distribution.
24 * 4. Neither the name of the University nor the names of its contributors
25 * may be used to endorse or promote products derived from this software
26 * without specific prior written permission.
28 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
29 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
32 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
36 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
37 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
42 * Portions Copyright (c) 1993 by Digital Equipment Corporation.
44 * Permission to use, copy, modify, and distribute this software for any
45 * purpose with or without fee is hereby granted, provided that the above
46 * copyright notice and this permission notice appear in all copies, and that
47 * the name of Digital Equipment Corporation not be used in advertising or
48 * publicity pertaining to distribution of the document or software without
49 * specific, written prior permission.
51 * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL
52 * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES
53 * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL DIGITAL EQUIPMENT
54 * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
55 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
56 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
57 * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
62 * Portions Copyright (c) 1996-1999 by Internet Software Consortium.
64 * Permission to use, copy, modify, and distribute this software for any
65 * purpose with or without fee is hereby granted, provided that the above
66 * copyright notice and this permission notice appear in all copies.
68 * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
69 * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
70 * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
71 * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
72 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
73 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
74 * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
80 * 5-Oct-2000 W. Greathouse wgreathouse@smva.com
81 * Fix memory leak and memory corruption.
82 * -- Every name resolution resulted in
83 * a new parse of resolv.conf and new
84 * copy of nameservers allocated by
86 * -- Every name resolution resulted in
87 * a new read of resolv.conf without
88 * resetting index from prior read...
89 * resulting in exceeding array bounds.
91 * Limit nameservers read from resolv.conf
93 * Add "search" domains from resolv.conf
95 * Some systems will return a security
96 * signature along with query answer for
97 * dynamic DNS entries.
98 * -- skip/ignore this answer
100 * Include arpa/nameser.h for defines.
104 * 20-Jun-2001 Michal Moskal <malekith@pld.org.pl>
105 * partial IPv6 support (i.e. gethostbyname2() and resolve_address2()
106 * functions added), IPv6 nameservers are also supported.
108 * 6-Oct-2001 Jari Korva <jari.korva@iki.fi>
109 * more IPv6 support (IPv6 support for gethostbyaddr();
110 * address family parameter and improved IPv6 support for get_hosts_byname
111 * and read_etc_hosts; getnameinfo() port from glibc; defined
112 * defined ip6addr_any and in6addr_loopback)
114 * 2-Feb-2002 Erik Andersen <andersen@codepoet.org>
115 * Added gethostent(), sethostent(), and endhostent()
117 * 17-Aug-2002 Manuel Novoa III <mjn3@codepoet.org>
118 * Fixed __read_etc_hosts_r to return alias list, and modified buffer
119 * allocation accordingly. See MAX_ALIASES and ALIAS_DIM below.
120 * This fixes the segfault in the Python 2.2.1 socket test.
122 * 04-Jan-2003 Jay Kulpinski <jskulpin@berkshire.rr.com>
123 * Fixed __decode_dotted to count the terminating null character
126 * 02-Oct-2003 Tony J. White <tjw@tjw.org>
127 * Lifted dn_expand() and dependent ns_name_uncompress(), ns_name_unpack(),
128 * and ns_name_ntop() from glibc 2.3.2 for compatibility with ipsec-tools
131 * 7-Sep-2004 Erik Andersen <andersen@codepoet.org>
132 * Added gethostent_r()
136 #define __FORCE_GLIBC
137 #include <features.h>
143 #include <sys/poll.h>
144 #include <sys/socket.h>
145 #include <sys/types.h>
146 #include <sys/time.h>
147 #include <netinet/in.h>
148 #include <arpa/inet.h>
156 #include <arpa/nameser.h>
157 #include <sys/utsname.h>
159 #include <bits/uClibc_mutex.h>
161 /* poll() is not supported in kernel <= 2.0, therefore if __NR_poll is
162 * not available, we assume an old Linux kernel is in use and we will
163 * use select() instead. */
164 #include <sys/syscall.h>
169 __UCLIBC_MUTEX_EXTERN(__resolv_lock);
171 /* Experimentally off - libc_hidden_proto(memcpy) */
172 /* Experimentally off - libc_hidden_proto(memset) */
173 /* Experimentally off - libc_hidden_proto(memmove) */
174 /* Experimentally off - libc_hidden_proto(strchr) */
175 /* Experimentally off - libc_hidden_proto(strcmp) */
176 /* Experimentally off - libc_hidden_proto(strcpy) */
177 /* Experimentally off - libc_hidden_proto(strdup) */
178 /* Experimentally off - libc_hidden_proto(strlen) */
179 /* Experimentally off - libc_hidden_proto(strncat) */
180 /* Experimentally off - libc_hidden_proto(strncpy) */
181 /* libc_hidden_proto(strnlen) */
182 /* Experimentally off - libc_hidden_proto(strstr) */
183 /* Experimentally off - libc_hidden_proto(strcasecmp) */
184 libc_hidden_proto(socket)
185 libc_hidden_proto(close)
186 libc_hidden_proto(fopen)
187 libc_hidden_proto(fclose)
188 libc_hidden_proto(random)
189 libc_hidden_proto(getservbyport)
190 libc_hidden_proto(getdomainname)
191 libc_hidden_proto(uname)
192 libc_hidden_proto(inet_addr)
193 libc_hidden_proto(inet_aton)
194 libc_hidden_proto(inet_pton)
195 libc_hidden_proto(inet_ntop)
196 libc_hidden_proto(connect)
197 libc_hidden_proto(poll)
198 libc_hidden_proto(select)
199 libc_hidden_proto(recv)
200 libc_hidden_proto(send)
201 libc_hidden_proto(printf)
202 libc_hidden_proto(sprintf)
203 libc_hidden_proto(snprintf)
204 libc_hidden_proto(fgets)
205 libc_hidden_proto(gethostbyname)
206 libc_hidden_proto(gethostbyname_r)
207 libc_hidden_proto(gethostbyname2_r)
208 libc_hidden_proto(gethostbyaddr)
209 libc_hidden_proto(gethostbyaddr_r)
210 libc_hidden_proto(ns_name_uncompress)
211 libc_hidden_proto(ns_name_unpack)
212 libc_hidden_proto(ns_name_ntop)
213 libc_hidden_proto(res_init)
214 libc_hidden_proto(res_query)
215 libc_hidden_proto(res_querydomain)
216 libc_hidden_proto(gethostent_r)
217 libc_hidden_proto(fprintf)
218 libc_hidden_proto(__h_errno_location)
219 #ifdef __UCLIBC_HAS_XLOCALE__
220 libc_hidden_proto(__ctype_b_loc)
221 #elif __UCLIBC_HAS_CTYPE_TABLES__
222 libc_hidden_proto(__ctype_b)
224 libc_hidden_proto(__uc_malloc)
228 #define MAX_RECURSE 5
229 #define REPLY_TIMEOUT 10
230 #define MAX_RETRIES 3
231 #define MAX_SERVERS 3
234 #define MAX_ALIASES 5
236 /* 1:ip + 1:full + MAX_ALIASES:aliases + 1:NULL */
237 #define ALIAS_DIM (2 + MAX_ALIASES + 1)
243 #define DPRINTF(X,args...) fprintf(stderr, X, ##args)
245 #define DPRINTF(X,args...)
248 /* Make sure the incoming char * buffer is aligned enough to handle our random
249 * structures. This define is the same as we use for malloc alignment (which
250 * has same requirements). The offset is the number of bytes we need to adjust
251 * in order to attain desired alignment.
253 #define ALIGN_ATTR __alignof__(double __attribute_aligned__ (sizeof(size_t)))
254 #define ALIGN_BUFFER_OFFSET(buf) ((ALIGN_ATTR - ((size_t)buf % ALIGN_ATTR)) % ALIGN_ATTR)
257 /* Global stuff (stuff needing to be locked to be thread safe)... */
258 extern int __nameservers attribute_hidden;
259 extern char * __nameserver[MAX_SERVERS] attribute_hidden;
260 extern int __searchdomains attribute_hidden;
261 extern char * __searchdomain[MAX_SEARCH] attribute_hidden;
268 struct resolv_header {
270 int qr,opcode,aa,tc,rd,ra,rcode;
277 struct resolv_question {
283 struct resolv_answer {
289 const unsigned char * rdata;
296 enum etc_hosts_action {
297 GET_HOSTS_BYNAME = 0,
302 /* function prototypes */
303 extern int __get_hosts_byname_r(const char * name, int type,
304 struct hostent * result_buf,
305 char * buf, size_t buflen,
306 struct hostent ** result,
307 int * h_errnop) attribute_hidden;
308 extern int __get_hosts_byaddr_r(const char * addr, int len, int type,
309 struct hostent * result_buf,
310 char * buf, size_t buflen,
311 struct hostent ** result,
312 int * h_errnop) attribute_hidden;
313 extern void __open_etc_hosts(FILE **fp) attribute_hidden;
314 extern int __read_etc_hosts_r(FILE *fp, const char * name, int type,
315 enum etc_hosts_action action,
316 struct hostent * result_buf,
317 char * buf, size_t buflen,
318 struct hostent ** result,
319 int * h_errnop) attribute_hidden;
320 extern int __dns_lookup(const char * name, int type, int nscount,
321 char ** nsip, unsigned char ** outpacket, struct resolv_answer * a) attribute_hidden;
323 extern int __encode_dotted(const char * dotted, unsigned char * dest, int maxlen) attribute_hidden;
324 extern int __decode_dotted(const unsigned char * const message, int offset,
325 char * dest, int maxlen) attribute_hidden;
326 extern int __length_dotted(const unsigned char * const message, int offset) attribute_hidden;
327 extern int __encode_header(struct resolv_header * h, unsigned char * dest, int maxlen) attribute_hidden;
328 extern int __decode_header(unsigned char * data, struct resolv_header * h) attribute_hidden;
329 extern int __encode_question(const struct resolv_question * const q,
330 unsigned char * dest, int maxlen) attribute_hidden;
331 extern int __decode_question(const unsigned char * const message, int offset,
332 struct resolv_question * q) attribute_hidden;
333 extern int __encode_answer(struct resolv_answer * a,
334 unsigned char * dest, int maxlen) attribute_hidden;
335 extern int __decode_answer(const unsigned char * message, int offset,
336 struct resolv_answer * a) attribute_hidden;
337 extern int __length_question(const unsigned char * const message, int offset) attribute_hidden;
338 extern void __open_nameservers(void) attribute_hidden;
339 extern void __close_nameservers(void) attribute_hidden;
340 extern int __dn_expand(const u_char *, const u_char *, const u_char *,
344 int attribute_hidden __encode_header(struct resolv_header *h, unsigned char *dest, int maxlen)
346 if (maxlen < HFIXEDSZ)
349 dest[0] = (h->id & 0xff00) >> 8;
350 dest[1] = (h->id & 0x00ff) >> 0;
351 dest[2] = (h->qr ? 0x80 : 0) |
352 ((h->opcode & 0x0f) << 3) |
356 dest[3] = (h->ra ? 0x80 : 0) | (h->rcode & 0x0f);
357 dest[4] = (h->qdcount & 0xff00) >> 8;
358 dest[5] = (h->qdcount & 0x00ff) >> 0;
359 dest[6] = (h->ancount & 0xff00) >> 8;
360 dest[7] = (h->ancount & 0x00ff) >> 0;
361 dest[8] = (h->nscount & 0xff00) >> 8;
362 dest[9] = (h->nscount & 0x00ff) >> 0;
363 dest[10] = (h->arcount & 0xff00) >> 8;
364 dest[11] = (h->arcount & 0x00ff) >> 0;
371 int attribute_hidden __decode_header(unsigned char *data, struct resolv_header *h)
373 h->id = (data[0] << 8) | data[1];
374 h->qr = (data[2] & 0x80) ? 1 : 0;
375 h->opcode = (data[2] >> 3) & 0x0f;
376 h->aa = (data[2] & 0x04) ? 1 : 0;
377 h->tc = (data[2] & 0x02) ? 1 : 0;
378 h->rd = (data[2] & 0x01) ? 1 : 0;
379 h->ra = (data[3] & 0x80) ? 1 : 0;
380 h->rcode = data[3] & 0x0f;
381 h->qdcount = (data[4] << 8) | data[5];
382 h->ancount = (data[6] << 8) | data[7];
383 h->nscount = (data[8] << 8) | data[9];
384 h->arcount = (data[10] << 8) | data[11];
391 /* Encode a dotted string into nameserver transport-level encoding.
392 This routine is fairly dumb, and doesn't attempt to compress
395 int attribute_hidden __encode_dotted(const char *dotted, unsigned char *dest, int maxlen)
399 while (dotted && *dotted) {
400 char *c = strchr(dotted, '.');
401 int l = c ? c - dotted : strlen(dotted);
403 if (l >= (maxlen - used - 1))
407 memcpy(dest + used, dotted, l);
426 /* Decode a dotted string from nameserver transport-level encoding.
427 This routine understands compressed data. */
429 int attribute_hidden __decode_dotted(const unsigned char * const data, int offset,
430 char *dest, int maxlen)
440 while ((l = data[offset++])) {
443 if ((l & 0xc0) == (0xc0)) {
446 /* compressed item, redirect */
447 offset = ((l & 0x3f) << 8) | data[offset];
452 if ((used + l + 1) >= maxlen)
455 memcpy(dest + used, data + offset, l);
461 if (data[offset] != 0)
467 /* The null byte must be counted too */
471 DPRINTF("Total decode len = %d\n", total);
478 int attribute_hidden __length_dotted(const unsigned char * const data, int offset)
480 int orig_offset = offset;
486 while ((l = data[offset++])) {
488 if ((l & 0xc0) == (0xc0)) {
496 return offset - orig_offset;
501 int attribute_hidden __encode_question(const struct resolv_question * const q,
502 unsigned char *dest, int maxlen)
506 i = __encode_dotted(q->dotted, dest, maxlen);
516 dest[0] = (q->qtype & 0xff00) >> 8;
517 dest[1] = (q->qtype & 0x00ff) >> 0;
518 dest[2] = (q->qclass & 0xff00) >> 8;
519 dest[3] = (q->qclass & 0x00ff) >> 0;
526 int attribute_hidden __decode_question(const unsigned char * const message, int offset,
527 struct resolv_question *q)
532 i = __decode_dotted(message, offset, temp, sizeof(temp));
538 q->dotted = strdup(temp);
539 q->qtype = (message[offset + 0] << 8) | message[offset + 1];
540 q->qclass = (message[offset + 2] << 8) | message[offset + 3];
547 int attribute_hidden __length_question(const unsigned char * const message, int offset)
551 i = __length_dotted(message, offset);
560 int attribute_hidden __encode_answer(struct resolv_answer *a, unsigned char *dest, int maxlen)
564 i = __encode_dotted(a->dotted, dest, maxlen);
571 if (maxlen < (RRFIXEDSZ+a->rdlength))
574 *dest++ = (a->atype & 0xff00) >> 8;
575 *dest++ = (a->atype & 0x00ff) >> 0;
576 *dest++ = (a->aclass & 0xff00) >> 8;
577 *dest++ = (a->aclass & 0x00ff) >> 0;
578 *dest++ = (a->ttl & 0xff000000) >> 24;
579 *dest++ = (a->ttl & 0x00ff0000) >> 16;
580 *dest++ = (a->ttl & 0x0000ff00) >> 8;
581 *dest++ = (a->ttl & 0x000000ff) >> 0;
582 *dest++ = (a->rdlength & 0xff00) >> 8;
583 *dest++ = (a->rdlength & 0x00ff) >> 0;
584 memcpy(dest, a->rdata, a->rdlength);
586 return i + RRFIXEDSZ + a->rdlength;
591 int attribute_hidden __decode_answer(const unsigned char *message, int offset,
592 struct resolv_answer *a)
597 i = __decode_dotted(message, offset, temp, sizeof(temp));
601 message += offset + i;
603 a->dotted = strdup(temp);
604 a->atype = (message[0] << 8) | message[1];
606 a->aclass = (message[0] << 8) | message[1];
608 a->ttl = (message[0] << 24) |
609 (message[1] << 16) | (message[2] << 8) | (message[3] << 0);
611 a->rdlength = (message[0] << 8) | message[1];
614 a->rdoffset = offset + i + RRFIXEDSZ;
616 DPRINTF("i=%d,rdlength=%d\n", i, a->rdlength);
618 return i + RRFIXEDSZ + a->rdlength;
623 int __encode_packet(struct resolv_header *h,
624 struct resolv_question **q,
625 struct resolv_answer **an,
626 struct resolv_answer **ns,
627 struct resolv_answer **ar,
628 unsigned char *dest, int maxlen) attribute_hidden;
629 int __encode_packet(struct resolv_header *h,
630 struct resolv_question **q,
631 struct resolv_answer **an,
632 struct resolv_answer **ns,
633 struct resolv_answer **ar,
634 unsigned char *dest, int maxlen)
639 i = __encode_header(h, dest, maxlen);
647 for (j = 0; j < h->qdcount; j++) {
648 i = __encode_question(q[j], dest, maxlen);
656 for (j = 0; j < h->ancount; j++) {
657 i = __encode_answer(an[j], dest, maxlen);
664 for (j = 0; j < h->nscount; j++) {
665 i = __encode_answer(ns[j], dest, maxlen);
672 for (j = 0; j < h->arcount; j++) {
673 i = __encode_answer(ar[j], dest, maxlen);
686 int __decode_packet(unsigned char *data, struct resolv_header *h) attribute_hidden;
687 int __decode_packet(unsigned char *data, struct resolv_header *h)
689 return __decode_header(data, h);
694 int __form_query(int id, const char *name, int type, unsigned char *packet, int maxlen);
695 int __form_query(int id, const char *name, int type, unsigned char *packet,
698 struct resolv_header h;
699 struct resolv_question q;
702 memset(&h, 0, sizeof(h));
706 q.dotted = (char *) name;
708 q.qclass = C_IN; /* CLASS_IN */
710 i = __encode_header(&h, packet, maxlen);
714 j = __encode_question(&q, packet + i, maxlen - i);
723 __UCLIBC_MUTEX_STATIC(mylock, PTHREAD_MUTEX_INITIALIZER);
725 /* Just for the record, having to lock __dns_lookup() just for these two globals
726 * is pretty lame. I think these two variables can probably be de-global-ized,
727 * which should eliminate the need for doing locking here... Needs a closer
729 static int ns = 0, id = 1;
731 int attribute_hidden __dns_lookup(const char *name, int type, int nscount, char **nsip,
732 unsigned char **outpacket, struct resolv_answer *a)
734 int i, j, len, fd, pos, rc;
741 struct resolv_header h;
742 struct resolv_question q;
743 struct resolv_answer ma;
744 bool first_answer = 1;
745 unsigned retries = 0;
746 unsigned char * packet = malloc(PACKETSZ);
747 char *dns, *lookup = malloc(MAXDNAME);
749 struct sockaddr_in sa;
750 int local_ns = -1, local_id = -1;
751 #ifdef __UCLIBC_HAS_IPV6__
753 struct sockaddr_in6 sa6;
758 if (!packet || !lookup || !nscount)
761 DPRINTF("Looking up type %d answer for '%s'\n", type, name);
763 /* Mess with globals while under lock */
764 __UCLIBC_MUTEX_LOCK(mylock);
765 local_ns = ns % nscount;
767 __UCLIBC_MUTEX_UNLOCK(mylock);
769 while (retries < MAX_RETRIES) {
773 memset(packet, 0, PACKETSZ);
775 memset(&h, 0, sizeof(h));
780 __UCLIBC_MUTEX_LOCK(__resolv_lock);
781 /* this is really __nameserver[] which is a global that
782 needs to hold __resolv_lock before access!! */
783 dns = nsip[local_ns];
784 __UCLIBC_MUTEX_UNLOCK(__resolv_lock);
789 DPRINTF("encoding header\n", h.rd);
791 i = __encode_header(&h, packet, PACKETSZ);
795 strncpy(lookup,name,MAXDNAME);
797 __UCLIBC_MUTEX_LOCK(__resolv_lock);
798 if (variant < __searchdomains) {
799 strncat(lookup,".", MAXDNAME);
800 strncat(lookup,__searchdomain[variant], MAXDNAME);
802 __UCLIBC_MUTEX_UNLOCK(__resolv_lock);
804 DPRINTF("lookup name: %s\n", lookup);
805 q.dotted = (char *)lookup;
807 q.qclass = C_IN; /* CLASS_IN */
809 j = __encode_question(&q, packet+i, PACKETSZ-i);
815 DPRINTF("On try %d, sending query to port %d of machine %s\n",
816 retries+1, NAMESERVER_PORT, dns);
818 #ifdef __UCLIBC_HAS_IPV6__
819 __UCLIBC_MUTEX_LOCK(__resolv_lock);
820 /* 'dns' is really __nameserver[] which is a global that
821 needs to hold __resolv_lock before access!! */
822 v6 = inet_pton(AF_INET6, dns, &sa6.sin6_addr) > 0;
823 __UCLIBC_MUTEX_UNLOCK(__resolv_lock);
824 fd = socket(v6 ? AF_INET6 : AF_INET, SOCK_DGRAM, IPPROTO_UDP);
826 fd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
833 /* Connect to the UDP socket so that asyncronous errors are returned */
834 #ifdef __UCLIBC_HAS_IPV6__
836 sa6.sin6_family = AF_INET6;
837 sa6.sin6_port = htons(NAMESERVER_PORT);
838 /* sa6.sin6_addr is already here */
839 rc = connect(fd, (struct sockaddr *) &sa6, sizeof(sa6));
842 sa.sin_family = AF_INET;
843 sa.sin_port = htons(NAMESERVER_PORT);
844 __UCLIBC_MUTEX_LOCK(__resolv_lock);
845 /* 'dns' is really __nameserver[] which is a global that
846 needs to hold __resolv_lock before access!! */
847 sa.sin_addr.s_addr = inet_addr(dns);
848 __UCLIBC_MUTEX_UNLOCK(__resolv_lock);
849 rc = connect(fd, (struct sockaddr *) &sa, sizeof(sa));
850 #ifdef __UCLIBC_HAS_IPV6__
854 if (errno == ENETUNREACH) {
855 /* routing error, presume not transient */
864 DPRINTF("Transmitting packet of length %d, id=%d, qr=%d\n",
867 send(fd, packet, len, 0);
872 tv.tv_sec = REPLY_TIMEOUT;
874 if (select(fd + 1, &fds, NULL, NULL, &tv) <= 0) {
875 DPRINTF("Timeout\n");
877 /* timed out, so retry send and receive,
878 * to next nameserver on queue */
884 if (poll(&fds, 1, REPLY_TIMEOUT * 1000) <= 0) {
885 DPRINTF("Timeout\n");
887 /* timed out, so retry send and receive,
888 * to next nameserver on queue */
893 len = recv(fd, packet, 512, 0);
894 if (len < HFIXEDSZ) {
899 __decode_header(packet, &h);
901 DPRINTF("id = %d, qr = %d\n", h.id, h.qr);
903 if ((h.id != local_id) || (!h.qr)) {
908 DPRINTF("Got response %s\n", "(i think)!");
909 DPRINTF("qrcount=%d,ancount=%d,nscount=%d,arcount=%d\n",
910 h.qdcount, h.ancount, h.nscount, h.arcount);
911 DPRINTF("opcode=%d,aa=%d,tc=%d,rd=%d,ra=%d,rcode=%d\n",
912 h.opcode, h.aa, h.tc, h.rd, h.ra, h.rcode);
914 if ((h.rcode) || (h.ancount < 1)) {
915 /* negative result, not present */
921 for (j = 0; j < h.qdcount; j++) {
922 DPRINTF("Skipping question %d at %d\n", j, pos);
923 i = __length_question(packet, pos);
924 DPRINTF("Length of question %d is %d\n", j, i);
929 DPRINTF("Decoding answer at pos %d\n", pos);
932 for (j = 0; j < h.ancount; j++, pos += i) {
933 i = __decode_answer(packet, pos, &ma);
936 DPRINTF("failed decode %d\n", i);
942 ma.buflen = a->buflen;
943 ma.add_count = a->add_count;
944 memcpy(a, &ma, sizeof(ma));
945 if (a->atype != T_SIG && (0 == a->buf || (type != T_A && type != T_AAAA)))
947 if (a->atype != type) {
951 a->add_count = h.ancount - j - 1;
952 if ((a->rdlength + sizeof(struct in_addr*)) * a->add_count > a->buflen)
958 if (ma.atype != type)
960 if (a->rdlength != ma.rdlength) {
962 DPRINTF("Answer address len(%u) differs from original(%u)\n",
963 ma.rdlength, a->rdlength);
966 memcpy(a->buf + (a->add_count * ma.rdlength), ma.rdata, ma.rdlength);
971 DPRINTF("Answer name = |%s|\n", a->dotted);
972 DPRINTF("Answer type = |%d|\n", a->atype);
982 /* Mess with globals while under lock */
983 __UCLIBC_MUTEX_LOCK(mylock);
986 __UCLIBC_MUTEX_UNLOCK(mylock);
988 return (len); /* success! */
991 /* if there are other nameservers, give them a go,
992 otherwise return with error */
995 local_ns = (local_ns + 1) % nscount;
1003 /* if there are searchdomains, try them or fallback as passed */
1006 __UCLIBC_MUTEX_LOCK(__resolv_lock);
1007 sdomains = __searchdomains;
1008 __UCLIBC_MUTEX_UNLOCK(__resolv_lock);
1010 if (variant < sdomains - 1) {
1014 /* next server, first search */
1015 local_ns = (local_ns + 1) % nscount;
1029 h_errno = NETDB_INTERNAL;
1030 /* Mess with globals while under lock */
1031 if (local_ns != -1) {
1032 __UCLIBC_MUTEX_LOCK(mylock);
1035 __UCLIBC_MUTEX_UNLOCK(mylock);
1041 #ifdef L_opennameservers
1043 /* We use __resolv_lock to guard access to the
1044 * '__nameservers' and __searchdomains globals */
1046 char * __nameserver[MAX_SERVERS];
1047 int __searchdomains;
1048 char * __searchdomain[MAX_SEARCH];
1050 __UCLIBC_MUTEX_INIT(__resolv_lock, PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP);
1053 * we currently read formats not quite the same as that on normal
1054 * unix systems, we can have a list of nameservers after the keyword.
1057 void attribute_hidden __open_nameservers(void)
1061 #define RESOLV_ARGS 5
1062 char szBuffer[128], *p, *argv[RESOLV_ARGS];
1066 __UCLIBC_MUTEX_LOCK(__resolv_lock);
1067 if (__nameservers > 0)
1070 if ((fp = fopen("/etc/resolv.conf", "r")) ||
1071 (fp = fopen("/etc/config/resolv.conf", "r")))
1074 while (fgets(szBuffer, sizeof(szBuffer), fp) != NULL) {
1076 for (p = szBuffer; *p && isspace(*p); p++)
1077 /* skip white space */;
1078 if (*p == '\0' || *p == '\n' || *p == '#') /* skip comments etc */
1081 while (*p && argc < RESOLV_ARGS) {
1083 while (*p && !isspace(*p) && *p != '\n')
1085 while (*p && (isspace(*p) || *p == '\n')) /* remove spaces */
1089 if (strcmp(argv[0], "nameserver") == 0) {
1090 for (i = 1; i < argc && __nameservers < MAX_SERVERS; i++) {
1091 __nameserver[__nameservers++] = strdup(argv[i]);
1092 DPRINTF("adding nameserver %s\n", argv[i]);
1096 /* domain and search are mutually exclusive, the last one wins */
1097 if (strcmp(argv[0],"domain") == 0 || strcmp(argv[0],"search") == 0) {
1098 while (__searchdomains > 0) {
1099 free(__searchdomain[--__searchdomains]);
1100 __searchdomain[__searchdomains] = NULL;
1102 for (i = 1; i < argc && __searchdomains < MAX_SEARCH; i++) {
1103 __searchdomain[__searchdomains++] = strdup(argv[i]);
1104 DPRINTF("adding search %s\n", argv[i]);
1109 DPRINTF("nameservers = %d\n", __nameservers);
1112 DPRINTF("failed to open %s\n", "resolv.conf");
1113 h_errno = NO_RECOVERY;
1118 __UCLIBC_MUTEX_UNLOCK(__resolv_lock);
1124 #ifdef L_closenameservers
1126 void attribute_hidden __close_nameservers(void)
1128 __UCLIBC_MUTEX_LOCK(__resolv_lock);
1129 while (__nameservers > 0) {
1130 free(__nameserver[--__nameservers]);
1131 __nameserver[__nameservers] = NULL;
1133 while (__searchdomains > 0) {
1134 free(__searchdomain[--__searchdomains]);
1135 __searchdomain[__searchdomains] = NULL;
1137 __UCLIBC_MUTEX_UNLOCK(__resolv_lock);
1141 #ifdef L_gethostbyname
1143 struct hostent *gethostbyname(const char *name)
1147 char buf[sizeof(struct in_addr) +
1148 sizeof(struct in_addr *)*2 +
1149 sizeof(char *)*(ALIAS_DIM) + 384/*namebuffer*/ + 32/* margin */];
1154 sp = __uc_malloc(sizeof(*sp));
1155 gethostbyname_r(name, &sp->h, sp->buf, sizeof(sp->buf), &hp, &h_errno);
1159 libc_hidden_def(gethostbyname)
1162 #ifdef L_gethostbyname2
1164 struct hostent *gethostbyname2(const char *name, int family)
1166 #ifndef __UCLIBC_HAS_IPV6__
1167 return family == AF_INET ? gethostbyname(name) : (struct hostent*)0;
1168 #else /* __UCLIBC_HAS_IPV6__ */
1171 char buf[sizeof(struct in6_addr) +
1172 sizeof(struct in6_addr *)*2 +
1173 sizeof(char *)*(ALIAS_DIM) + 384/*namebuffer*/ + 32/* margin */];
1178 sp = __uc_malloc(sizeof(*sp));
1179 gethostbyname2_r(name, family, &sp->h, sp->buf, sizeof(sp->buf), &hp, &h_errno);
1182 #endif /* __UCLIBC_HAS_IPV6__ */
1189 /* We use __resolv_lock to guard access to global '_res' */
1190 struct __res_state _res;
1194 struct __res_state *rp = &(_res);
1196 __UCLIBC_MUTEX_LOCK(__resolv_lock); /* must be a recursive lock! */
1197 __close_nameservers();
1198 __open_nameservers();
1199 rp->retrans = RES_TIMEOUT;
1201 rp->options = RES_INIT;
1202 rp->id = (u_int) random();
1203 rp->nsaddr.sin_addr.s_addr = INADDR_ANY;
1204 rp->nsaddr.sin_family = AF_INET;
1205 rp->nsaddr.sin_port = htons(NAMESERVER_PORT);
1207 /** rp->pfcode = 0; **/
1209 /** rp->_flags = 0; **/
1210 /** rp->qhook = NULL; **/
1211 /** rp->rhook = NULL; **/
1212 /** rp->_u._ext.nsinit = 0; **/
1214 if (__searchdomains) {
1216 for (i = 0; i < __searchdomains; i++)
1217 rp->dnsrch[i] = __searchdomain[i];
1220 if (__nameservers) {
1223 for (i = 0; i < __nameservers; i++) {
1224 if (inet_aton(__nameserver[i], &a)) {
1225 rp->nsaddr_list[i].sin_addr = a;
1226 rp->nsaddr_list[i].sin_family = AF_INET;
1227 rp->nsaddr_list[i].sin_port = htons(NAMESERVER_PORT);
1231 rp->nscount = __nameservers;
1232 __UCLIBC_MUTEX_UNLOCK(__resolv_lock);
1236 libc_hidden_def(res_init)
1238 #ifdef __UCLIBC_HAS_BSD_RES_CLOSE__
1239 void res_close( void )
1241 __close_nameservers();
1242 memset(&_res, 0, sizeof(_res));
1252 #define MIN(x, y) ((x) < (y) ? (x) : (y))
1255 int res_query(const char *dname, int class, int type,
1256 unsigned char *answer, int anslen)
1259 unsigned char * packet = 0;
1260 struct resolv_answer a;
1261 int __nameserversXX;
1262 char ** __nameserverXX;
1264 __open_nameservers();
1265 if (!dname || class != 1 /* CLASS_IN */) {
1266 h_errno = NO_RECOVERY;
1270 memset((char *) &a, '\0', sizeof(a));
1272 __UCLIBC_MUTEX_LOCK(__resolv_lock);
1273 __nameserversXX = __nameservers;
1274 __nameserverXX = __nameserver;
1275 __UCLIBC_MUTEX_UNLOCK(__resolv_lock);
1276 i = __dns_lookup(dname, type, __nameserversXX, __nameserverXX, &packet, &a);
1279 h_errno = TRY_AGAIN;
1285 if (a.atype == type) { /* CNAME*/
1286 int len = MIN(anslen, i);
1287 memcpy(answer, packet, len);
1294 libc_hidden_def(res_query)
1297 * Formulate a normal query, send, and retrieve answer in supplied buffer.
1298 * Return the size of the response on success, -1 on error.
1299 * If enabled, implement search rules until answer or unrecoverable failure
1300 * is detected. Error code, if any, is left in h_errno.
1302 #define __TRAILING_DOT (1<<0)
1303 #define __GOT_NODATA (1<<1)
1304 #define __GOT_SERVFAIL (1<<2)
1305 #define __TRIED_AS_IS (1<<3)
1306 int res_search(const char *name, int class, int type, u_char *answer,
1309 const char *cp, * const *domain;
1310 HEADER *hp = (HEADER *)(void *)answer;
1312 unsigned _state = 0;
1313 int ret, saved_herrno;
1314 u_long _res_options;
1315 unsigned _res_ndots;
1318 __UCLIBC_MUTEX_LOCK(__resolv_lock);
1319 _res_options = _res.options;
1320 __UCLIBC_MUTEX_UNLOCK(__resolv_lock);
1321 if ((!name || !answer) || ((_res_options & RES_INIT) == 0 && res_init() == -1)) {
1322 h_errno = NETDB_INTERNAL;
1327 h_errno = HOST_NOT_FOUND; /* default, if we never query */
1329 for (cp = name; *cp; cp++)
1330 dots += (*cp == '.');
1332 if (cp > name && *--cp == '.')
1333 _state |= __TRAILING_DOT;
1336 * If there are dots in the name already, let's just give it a try
1337 * 'as is'. The threshold can be set with the "ndots" option.
1340 __UCLIBC_MUTEX_LOCK(__resolv_lock);
1341 _res_ndots = _res.ndots;
1342 __UCLIBC_MUTEX_UNLOCK(__resolv_lock);
1343 if (dots >= _res_ndots) {
1344 ret = res_querydomain(name, NULL, class, type, answer, anslen);
1347 saved_herrno = h_errno;
1348 _state |= __TRIED_AS_IS;
1352 * We do at least one level of search if
1353 * - there is no dot and RES_DEFNAME is set, or
1354 * - there is at least one dot, there is no trailing dot,
1355 * and RES_DNSRCH is set.
1357 __UCLIBC_MUTEX_LOCK(__resolv_lock);
1358 _res_options = _res.options;
1359 _res_dnsrch = _res.dnsrch;
1360 __UCLIBC_MUTEX_UNLOCK(__resolv_lock);
1361 if ((!dots && (_res_options & RES_DEFNAMES)) ||
1362 (dots && !(_state & __TRAILING_DOT) && (_res_options & RES_DNSRCH))) {
1365 for (domain = (const char * const *)_res_dnsrch;
1369 ret = res_querydomain(name, *domain, class, type,
1375 * If no server present, give up.
1376 * If name isn't found in this domain,
1377 * keep trying higher domains in the search list
1378 * (if that's enabled).
1379 * On a NO_DATA error, keep trying, otherwise
1380 * a wildcard entry of another type could keep us
1381 * from finding this entry higher in the domain.
1382 * If we get some other error (negative answer or
1383 * server failure), then stop searching up,
1384 * but try the input name below in case it's
1387 if (errno == ECONNREFUSED) {
1388 h_errno = TRY_AGAIN;
1394 _state |= __GOT_NODATA;
1396 case HOST_NOT_FOUND:
1400 if (hp->rcode == SERVFAIL) {
1401 /* try next search element, if any */
1402 _state |= __GOT_SERVFAIL;
1407 /* anything else implies that we're done */
1411 * if we got here for some reason other than DNSRCH,
1412 * we only wanted one iteration of the loop, so stop.
1414 __UCLIBC_MUTEX_LOCK(__resolv_lock);
1415 _res_options = _res.options;
1416 __UCLIBC_MUTEX_UNLOCK(__resolv_lock);
1417 if (!(_res_options & RES_DNSRCH))
1423 * if we have not already tried the name "as is", do that now.
1424 * note that we do this regardless of how many dots were in the
1425 * name or whether it ends with a dot.
1427 if (!(_state & __TRIED_AS_IS)) {
1428 ret = res_querydomain(name, NULL, class, type, answer, anslen);
1434 * if we got here, we didn't satisfy the search.
1435 * if we did an initial full query, return that query's h_errno
1436 * (note that we wouldn't be here if that query had succeeded).
1437 * else if we ever got a nodata, send that back as the reason.
1438 * else send back meaningless h_errno, that being the one from
1439 * the last DNSRCH we did.
1441 if (saved_herrno != -1)
1442 h_errno = saved_herrno;
1443 else if (_state & __GOT_NODATA)
1445 else if (_state & __GOT_SERVFAIL)
1446 h_errno = TRY_AGAIN;
1449 #undef __TRAILING_DOT
1451 #undef __GOT_SERVFAIL
1452 #undef __TRIED_AS_IS
1454 * Perform a call on res_query on the concatenation of name and domain,
1455 * removing a trailing dot from name if domain is NULL.
1457 int res_querydomain(const char *name, const char *domain, int class, int type,
1458 u_char * answer, int anslen)
1460 char nbuf[MAXDNAME];
1461 const char *longname = nbuf;
1463 u_long _res_options;
1465 __UCLIBC_MUTEX_LOCK(__resolv_lock);
1466 _res_options = _res.options;
1467 __UCLIBC_MUTEX_UNLOCK(__resolv_lock);
1468 if ((!name || !answer) || ((_res_options & RES_INIT) == 0 && res_init() == -1)) {
1469 h_errno = NETDB_INTERNAL;
1474 __UCLIBC_MUTEX_LOCK(__resolv_lock);
1475 _res_options = _res.options;
1476 __UCLIBC_MUTEX_UNLOCK(__resolv_lock);
1477 if (_res_options & RES_DEBUG)
1478 printf(";; res_querydomain(%s, %s, %d, %d)\n",
1479 name, (domain ? domain : "<Nil>"), class, type);
1481 if (domain == NULL) {
1483 * Check for trailing '.';
1484 * copy without '.' if present.
1487 if (n + 1 > sizeof(nbuf)) {
1488 h_errno = NO_RECOVERY;
1491 if (n > 0 && name[--n] == '.') {
1492 strncpy(nbuf, name, n);
1499 if (n + 1 + d + 1 > sizeof(nbuf)) {
1500 h_errno = NO_RECOVERY;
1503 snprintf(nbuf, sizeof(nbuf), "%s.%s", name, domain);
1505 return (res_query(longname, class, type, answer, anslen));
1507 libc_hidden_def(res_querydomain)
1515 #ifdef L_gethostbyaddr
1516 struct hostent *gethostbyaddr (const void *addr, socklen_t len, int type)
1521 #ifndef __UCLIBC_HAS_IPV6__
1522 sizeof(struct in_addr) + sizeof(struct in_addr *)*2 +
1524 sizeof(struct in6_addr) + sizeof(struct in6_addr *)*2 +
1525 #endif /* __UCLIBC_HAS_IPV6__ */
1526 sizeof(char *)*(ALIAS_DIM) + 384/*namebuffer*/ + 32/* margin */];
1531 sp = __uc_malloc(sizeof(*sp));
1532 gethostbyaddr_r(addr, len, type, &sp->h, sp->buf, sizeof(sp->buf), &hp, &h_errno);
1536 libc_hidden_def(gethostbyaddr)
1540 #ifdef L_read_etc_hosts_r
1542 void attribute_hidden __open_etc_hosts(FILE **fp)
1544 if ((*fp = fopen("/etc/hosts", "r")) == NULL) {
1545 *fp = fopen("/etc/config/hosts", "r");
1550 int attribute_hidden __read_etc_hosts_r(FILE * fp, const char * name, int type,
1551 enum etc_hosts_action action,
1552 struct hostent * result_buf,
1553 char * buf, size_t buflen,
1554 struct hostent ** result,
1557 struct in_addr *in = NULL;
1558 struct in_addr **addr_list = NULL;
1559 #ifdef __UCLIBC_HAS_IPV6__
1560 struct in6_addr *in6 = NULL;
1561 struct in6_addr **addr_list6 =NULL;
1562 #endif /* __UCLIBC_HAS_IPV6__ */
1564 int aliases, i, ret = HOST_NOT_FOUND;
1566 /* make sure user char * is aligned */
1567 i = ALIGN_BUFFER_OFFSET(buf);
1575 if (buflen < sizeof(char *)*(ALIAS_DIM))
1577 alias = (char **)buf;
1578 buf += sizeof(char **)*(ALIAS_DIM);
1579 buflen -= sizeof(char **)*(ALIAS_DIM);
1581 if (action != GETHOSTENT) {
1582 #ifdef __UCLIBC_HAS_IPV6__
1584 size_t len = buflen;
1585 #endif /* __UCLIBC_HAS_IPV6__ */
1586 *h_errnop = NETDB_INTERNAL;
1587 if (buflen < sizeof(*in))
1589 in = (struct in_addr*)buf;
1591 buflen -= sizeof(*in);
1593 if (buflen < sizeof(*addr_list)*2)
1595 addr_list = (struct in_addr **)buf;
1596 buf += sizeof(*addr_list)*2;
1597 buflen -= sizeof(*addr_list)*2;
1599 #ifdef __UCLIBC_HAS_IPV6__
1600 if (len < sizeof(*in6))
1602 in6 = (struct in6_addr*)p;
1604 len -= sizeof(*in6);
1606 if (len < sizeof(*addr_list6)*2)
1608 addr_list6 = (struct in6_addr**)p;
1609 p += sizeof(*addr_list6)*2;
1610 len -= sizeof(*addr_list6)*2;
1616 #endif /* __UCLIBC_HAS_IPV6__ */
1621 __open_etc_hosts(&fp);
1628 *h_errnop = HOST_NOT_FOUND;
1629 while (fgets(buf, buflen, fp)) {
1630 if ((cp = strchr(buf, '#')))
1632 DPRINTF("Looking at: %s\n", buf);
1637 while (*cp && isspace(*cp))
1641 if (aliases < (2+MAX_ALIASES))
1642 alias[aliases++] = cp;
1643 while (*cp && !isspace(*cp))
1649 continue; /* syntax error really */
1651 if (action == GETHOSTENT) {
1652 /* Return whatever the next entry happens to be. */
1654 } else if (action == GET_HOSTS_BYADDR) {
1655 if (strcmp(name, alias[0]) != 0)
1658 /* GET_HOSTS_BYNAME */
1659 for (i = 1; i < aliases; i++)
1660 if (strcasecmp(name, alias[i]) == 0)
1666 if (type == AF_INET && inet_pton(AF_INET, alias[0], in) > 0) {
1667 DPRINTF("Found INET\n");
1670 result_buf->h_name = alias[1];
1671 result_buf->h_addrtype = AF_INET;
1672 result_buf->h_length = sizeof(*in);
1673 result_buf->h_addr_list = (char**) addr_list;
1674 result_buf->h_aliases = alias + 2;
1675 *result = result_buf;
1676 ret = NETDB_SUCCESS;
1677 #ifdef __UCLIBC_HAS_IPV6__
1678 } else if (type == AF_INET6 && inet_pton(AF_INET6, alias[0], in6) > 0) {
1679 DPRINTF("Found INET6\n");
1680 addr_list6[0] = in6;
1682 result_buf->h_name = alias[1];
1683 result_buf->h_addrtype = AF_INET6;
1684 result_buf->h_length = sizeof(*in6);
1685 result_buf->h_addr_list = (char**) addr_list6;
1686 result_buf->h_aliases = alias + 2;
1687 *result = result_buf;
1688 ret = NETDB_SUCCESS;
1689 #endif /* __UCLIBC_HAS_IPV6__ */
1691 /* continue parsing in the hope the user has multiple
1692 * host types listed in the database like so:
1695 * If looking for an IPv6 addr, don't bail when we got the IPv4
1697 DPRINTF("Error: Found host but diff network type\n");
1702 if (action != GETHOSTENT)
1706 if (action != GETHOSTENT)
1714 __UCLIBC_MUTEX_STATIC(mylock, PTHREAD_MUTEX_INITIALIZER);
1716 static int __stay_open;
1717 static FILE * __gethostent_fp;
1719 void endhostent (void)
1721 __UCLIBC_MUTEX_LOCK(mylock);
1723 if (__gethostent_fp)
1724 fclose(__gethostent_fp);
1725 __UCLIBC_MUTEX_UNLOCK(mylock);
1728 void sethostent (int stay_open)
1730 __UCLIBC_MUTEX_LOCK(mylock);
1731 __stay_open = stay_open;
1732 __UCLIBC_MUTEX_UNLOCK(mylock);
1735 int gethostent_r(struct hostent *result_buf, char *buf, size_t buflen,
1736 struct hostent **result, int *h_errnop)
1740 __UCLIBC_MUTEX_LOCK(mylock);
1741 if (__gethostent_fp == NULL) {
1742 __open_etc_hosts(&__gethostent_fp);
1743 if (__gethostent_fp == NULL) {
1750 ret = __read_etc_hosts_r(__gethostent_fp, NULL, AF_INET, GETHOSTENT,
1751 result_buf, buf, buflen, result, h_errnop);
1752 if (__stay_open == 0)
1753 fclose(__gethostent_fp);
1755 __UCLIBC_MUTEX_UNLOCK(mylock);
1758 libc_hidden_def(gethostent_r)
1760 struct hostent *gethostent (void)
1765 #ifndef __UCLIBC_HAS_IPV6__
1766 sizeof(struct in_addr) + sizeof(struct in_addr *)*2 +
1768 sizeof(struct in6_addr) + sizeof(struct in6_addr *)*2 +
1769 #endif /* __UCLIBC_HAS_IPV6__ */
1770 sizeof(char *)*(ALIAS_DIM) +
1771 80/*namebuffer*/ + 2/* margin */];
1773 struct hostent *host;
1776 sp = __uc_malloc(sizeof(*sp));
1777 __UCLIBC_MUTEX_LOCK(mylock);
1778 gethostent_r(&sp->h, sp->buf, sizeof(sp->buf), &host, &h_errno);
1779 __UCLIBC_MUTEX_UNLOCK(mylock);
1784 #ifdef L_get_hosts_byname_r
1786 int attribute_hidden __get_hosts_byname_r(const char * name, int type,
1787 struct hostent * result_buf,
1788 char * buf, size_t buflen,
1789 struct hostent ** result,
1792 return __read_etc_hosts_r(NULL, name, type, GET_HOSTS_BYNAME,
1793 result_buf, buf, buflen, result, h_errnop);
1797 #ifdef L_get_hosts_byaddr_r
1799 int attribute_hidden __get_hosts_byaddr_r(const char * addr, int len, int type,
1800 struct hostent * result_buf,
1801 char * buf, size_t buflen,
1802 struct hostent ** result,
1805 #ifndef __UCLIBC_HAS_IPV6__
1806 char ipaddr[INET_ADDRSTRLEN];
1808 char ipaddr[INET6_ADDRSTRLEN];
1809 #endif /* __UCLIBC_HAS_IPV6__ */
1813 if (len != sizeof(struct in_addr))
1816 #ifdef __UCLIBC_HAS_IPV6__
1818 if (len != sizeof(struct in6_addr))
1821 #endif /* __UCLIBC_HAS_IPV6__ */
1826 inet_ntop(type, addr, ipaddr, sizeof(ipaddr));
1828 return(__read_etc_hosts_r(NULL, ipaddr, type, GET_HOSTS_BYADDR,
1829 result_buf, buf, buflen, result, h_errnop));
1833 #ifdef L_getnameinfo
1836 # define min(x,y) (((x) > (y)) ? (y) : (x))
1839 libc_hidden_proto(getnameinfo)
1840 int getnameinfo (const struct sockaddr *sa, socklen_t addrlen, char *host,
1841 socklen_t hostlen, char *serv, socklen_t servlen,
1846 struct hostent *h = NULL;
1849 if (flags & ~(NI_NUMERICHOST|NI_NUMERICSERV|NI_NOFQDN|NI_NAMEREQD|NI_DGRAM))
1850 return EAI_BADFLAGS;
1852 if (sa == NULL || addrlen < sizeof (sa_family_t))
1856 if (ok == AF_LOCAL) /* valid */;
1857 else if (ok == AF_INET) {
1858 if (addrlen < sizeof (struct sockaddr_in))
1860 #ifdef __UCLIBC_HAS_IPV6__
1861 } else if (ok == AF_INET6) {
1862 if (addrlen < sizeof (struct sockaddr_in6))
1864 #endif /* __UCLIBC_HAS_IPV6__ */
1870 if (host != NULL && hostlen > 0)
1871 switch (sa->sa_family) {
1873 #ifdef __UCLIBC_HAS_IPV6__
1875 #endif /* __UCLIBC_HAS_IPV6__ */
1876 if (!(flags & NI_NUMERICHOST)) {
1877 #ifdef __UCLIBC_HAS_IPV6__
1878 if (sa->sa_family == AF_INET6)
1879 h = gethostbyaddr ((const void *)
1880 &(((const struct sockaddr_in6 *) sa)->sin6_addr),
1881 sizeof(struct in6_addr), AF_INET6);
1883 #endif /* __UCLIBC_HAS_IPV6__ */
1884 h = gethostbyaddr ((const void *) &(((const struct sockaddr_in *)sa)->sin_addr),
1885 sizeof(struct in_addr), AF_INET);
1889 if ((flags & NI_NOFQDN)
1890 && (getdomainname (domain, sizeof(domain)) == 0)
1891 && (c = strstr (h->h_name, domain))
1892 && (c != h->h_name) && (*(--c) == '.')) {
1893 strncpy (host, h->h_name,
1894 min(hostlen, (size_t) (c - h->h_name)));
1895 host[min(hostlen - 1, (size_t) (c - h->h_name))] = '\0';
1898 strncpy (host, h->h_name, hostlen);
1905 if (flags & NI_NAMEREQD) {
1910 #ifdef __UCLIBC_HAS_IPV6__
1911 if (sa->sa_family == AF_INET6) {
1912 const struct sockaddr_in6 *sin6p;
1914 sin6p = (const struct sockaddr_in6 *) sa;
1916 c = inet_ntop (AF_INET6,
1917 (const void *) &sin6p->sin6_addr, host, hostlen);
1919 /* Does scope id need to be supported? */
1921 scopeid = sin6p->sin6_scope_id;
1923 /* Buffer is >= IFNAMSIZ+1. */
1924 char scopebuf[IFNAMSIZ + 1];
1926 int ni_numericscope = 0;
1927 size_t real_hostlen = strnlen (host, hostlen);
1928 size_t scopelen = 0;
1930 scopebuf[0] = SCOPE_DELIMITER;
1932 scopeptr = &scopebuf[1];
1934 if (IN6_IS_ADDR_LINKLOCAL (&sin6p->sin6_addr)
1935 || IN6_IS_ADDR_MC_LINKLOCAL (&sin6p->sin6_addr)) {
1936 if (if_indextoname (scopeid, scopeptr) == NULL)
1939 scopelen = strlen (scopebuf);
1944 if (ni_numericscope)
1945 scopelen = 1 + snprintf (scopeptr,
1951 if (real_hostlen + scopelen + 1 > hostlen)
1953 memcpy (host + real_hostlen, scopebuf, scopelen + 1);
1957 #endif /* __UCLIBC_HAS_IPV6__ */
1958 c = inet_ntop (AF_INET, (const void *)
1959 &(((const struct sockaddr_in *) sa)->sin_addr),
1972 if (!(flags & NI_NUMERICHOST)) {
1973 struct utsname utsname;
1975 if (!uname (&utsname)) {
1976 strncpy (host, utsname.nodename, hostlen);
1981 if (flags & NI_NAMEREQD) {
1986 strncpy (host, "localhost", hostlen);
1989 /*Already checked above default:
1994 if (serv && (servlen > 0)) {
1995 if (sa->sa_family == AF_LOCAL) {
1996 strncpy (serv, ((const struct sockaddr_un *) sa)->sun_path, servlen);
1997 } else { /* AF_INET || AF_INET6 */
1998 if (!(flags & NI_NUMERICSERV)) {
2000 s = getservbyport (((const struct sockaddr_in *) sa)->sin_port,
2001 ((flags & NI_DGRAM) ? "udp" : "tcp"));
2003 strncpy (serv, s->s_name, servlen);
2007 snprintf (serv, servlen, "%d",
2008 ntohs (((const struct sockaddr_in *) sa)->sin_port));
2012 if (host && (hostlen > 0))
2013 host[hostlen-1] = 0;
2014 if (serv && (servlen > 0))
2015 serv[servlen-1] = 0;
2019 libc_hidden_def(getnameinfo)
2023 #ifdef L_gethostbyname_r
2025 int gethostbyname_r(const char * name,
2026 struct hostent * result_buf,
2027 char * buf, size_t buflen,
2028 struct hostent ** result,
2032 struct in_addr **addr_list;
2034 unsigned char *packet;
2035 struct resolv_answer a;
2037 int __nameserversXX;
2038 char ** __nameserverXX;
2040 __open_nameservers();
2045 /* do /etc/hosts first */
2047 int old_errno = errno; /* Save the old errno and reset errno */
2048 __set_errno(0); /* to check for missing /etc/hosts. */
2050 if ((i = __get_hosts_byname_r(name, AF_INET, result_buf,
2051 buf, buflen, result, h_errnop)) == 0)
2053 switch (*h_errnop) {
2054 case HOST_NOT_FOUND:
2057 case NETDB_INTERNAL:
2058 if (errno == ENOENT) {
2061 /* else fall through */
2065 __set_errno(old_errno);
2068 DPRINTF("Nothing found in /etc/hosts\n");
2070 /* make sure user char * is aligned */
2071 i = ALIGN_BUFFER_OFFSET(buf);
2079 *h_errnop = NETDB_INTERNAL;
2080 if (buflen < sizeof(*in))
2082 in = (struct in_addr*)buf;
2084 buflen -= sizeof(*in);
2086 if (buflen < sizeof(*addr_list)*2)
2088 addr_list = (struct in_addr**)buf;
2089 buf += sizeof(*addr_list)*2;
2090 buflen -= sizeof(*addr_list)*2;
2095 if (buflen < sizeof(char *)*(ALIAS_DIM))
2097 alias = (char **)buf;
2098 buf += sizeof(char **)*(ALIAS_DIM);
2099 buflen -= sizeof(char **)*(ALIAS_DIM);
2103 strncpy(buf, name, buflen);
2108 /* First check if this is already an address */
2109 if (inet_aton(name, in)) {
2110 result_buf->h_name = buf;
2111 result_buf->h_addrtype = AF_INET;
2112 result_buf->h_length = sizeof(*in);
2113 result_buf->h_addr_list = (char **) addr_list;
2114 result_buf->h_aliases = alias;
2115 *result = result_buf;
2116 *h_errnop = NETDB_SUCCESS;
2117 return NETDB_SUCCESS;
2122 __UCLIBC_MUTEX_LOCK(__resolv_lock);
2123 __nameserversXX = __nameservers;
2124 __nameserverXX = __nameserver;
2125 __UCLIBC_MUTEX_UNLOCK(__resolv_lock);
2129 i = __dns_lookup(name, T_A, __nameserversXX, __nameserverXX, &packet, &a);
2132 *h_errnop = HOST_NOT_FOUND;
2133 DPRINTF("__dns_lookup\n");
2137 if ((a.rdlength + sizeof(struct in_addr*)) * a.add_count + 256 > buflen) {
2140 *h_errnop = NETDB_INTERNAL;
2141 DPRINTF("buffer too small for all addresses\n");
2143 } else if (a.add_count > 0) {
2144 memmove(buf - sizeof(struct in_addr*)*2, buf, a.add_count * a.rdlength);
2145 addr_list = (struct in_addr**)(buf + a.add_count * a.rdlength);
2147 for (i = a.add_count - 1; i >= 0; --i)
2148 addr_list[i+1] = (struct in_addr*)(buf - sizeof(struct in_addr*)*2 + a.rdlength * i);
2149 addr_list[a.add_count + 1] = 0;
2150 buflen -= (((char*)&(addr_list[a.add_count + 2])) - buf);
2151 buf = (char*)&addr_list[a.add_count + 2];
2154 strncpy(buf, a.dotted, buflen);
2157 if (a.atype == T_A) { /* ADDRESS */
2158 memcpy(in, a.rdata, sizeof(*in));
2159 result_buf->h_name = buf;
2160 result_buf->h_addrtype = AF_INET;
2161 result_buf->h_length = sizeof(*in);
2162 result_buf->h_addr_list = (char **) addr_list;
2163 #ifdef __UCLIBC_MJN3_ONLY__
2164 #warning TODO -- generate the full list
2166 result_buf->h_aliases = alias; /* TODO: generate the full list */
2171 *h_errnop = HOST_NOT_FOUND;
2176 *result = result_buf;
2177 *h_errnop = NETDB_SUCCESS;
2178 return NETDB_SUCCESS;
2180 libc_hidden_def(gethostbyname_r)
2183 #ifdef L_gethostbyname2_r
2185 int gethostbyname2_r(const char *name, int family,
2186 struct hostent * result_buf,
2187 char * buf, size_t buflen,
2188 struct hostent ** result,
2191 #ifndef __UCLIBC_HAS_IPV6__
2192 return family == (AF_INET)? gethostbyname_r(name, result_buf,
2193 buf, buflen, result, h_errnop) : HOST_NOT_FOUND;
2194 #else /* __UCLIBC_HAS_IPV6__ */
2195 struct in6_addr *in;
2196 struct in6_addr **addr_list;
2197 unsigned char *packet;
2198 struct resolv_answer a;
2201 int __nameserversXX;
2202 char ** __nameserverXX;
2204 if (family == AF_INET)
2205 return gethostbyname_r(name, result_buf, buf, buflen, result, h_errnop);
2207 if (family != AF_INET6)
2210 __open_nameservers();
2215 /* do /etc/hosts first */
2217 int old_errno = errno; /* Save the old errno and reset errno */
2218 __set_errno(0); /* to check for missing /etc/hosts. */
2220 if ((i = __get_hosts_byname_r(name, family, result_buf,
2221 buf, buflen, result, h_errnop)) == 0)
2223 switch (*h_errnop) {
2224 case HOST_NOT_FOUND:
2227 case NETDB_INTERNAL:
2228 if (errno == ENOENT) {
2231 /* else fall through */
2235 __set_errno(old_errno);
2238 DPRINTF("Nothing found in /etc/hosts\n");
2240 *h_errnop = NETDB_INTERNAL;
2241 if (buflen < sizeof(*in))
2243 in = (struct in6_addr*)buf;
2245 buflen -= sizeof(*in);
2247 if (buflen < sizeof(*addr_list)*2)
2249 addr_list = (struct in6_addr**)buf;
2250 buf += sizeof(*addr_list)*2;
2251 buflen -= sizeof(*addr_list)*2;
2258 strncpy(buf, name, buflen);
2260 /* First check if this is already an address */
2261 if (inet_pton(AF_INET6, name, in)) {
2262 result_buf->h_name = buf;
2263 result_buf->h_addrtype = AF_INET6;
2264 result_buf->h_length = sizeof(*in);
2265 result_buf->h_addr_list = (char **) addr_list;
2266 *result = result_buf;
2267 *h_errnop = NETDB_SUCCESS;
2268 return NETDB_SUCCESS;
2271 memset((char *) &a, '\0', sizeof(a));
2274 __UCLIBC_MUTEX_LOCK(__resolv_lock);
2275 __nameserversXX = __nameservers;
2276 __nameserverXX = __nameserver;
2277 __UCLIBC_MUTEX_UNLOCK(__resolv_lock);
2279 i = __dns_lookup(buf, T_AAAA, __nameserversXX, __nameserverXX, &packet, &a);
2282 *h_errnop = HOST_NOT_FOUND;
2286 strncpy(buf, a.dotted, buflen);
2289 if (a.atype == T_CNAME) { /* CNAME */
2290 DPRINTF("Got a CNAME in gethostbyname()\n");
2291 i = __decode_dotted(packet, a.rdoffset, buf, buflen);
2295 *h_errnop = NO_RECOVERY;
2298 if (++nest > MAX_RECURSE) {
2299 *h_errnop = NO_RECOVERY;
2303 } else if (a.atype == T_AAAA) { /* ADDRESS */
2304 memcpy(in, a.rdata, sizeof(*in));
2305 result_buf->h_name = buf;
2306 result_buf->h_addrtype = AF_INET6;
2307 result_buf->h_length = sizeof(*in);
2308 result_buf->h_addr_list = (char **) addr_list;
2313 *h_errnop = HOST_NOT_FOUND;
2318 *result = result_buf;
2319 *h_errnop = NETDB_SUCCESS;
2320 return NETDB_SUCCESS;
2321 #endif /* __UCLIBC_HAS_IPV6__ */
2323 libc_hidden_def(gethostbyname2_r)
2326 #ifdef L_gethostbyaddr_r
2327 int gethostbyaddr_r (const void *addr, socklen_t len, int type,
2328 struct hostent * result_buf,
2329 char * buf, size_t buflen,
2330 struct hostent ** result,
2335 struct in_addr **addr_list;
2336 #ifdef __UCLIBC_HAS_IPV6__
2339 struct in6_addr *in6;
2340 struct in6_addr **addr_list6;
2341 #endif /* __UCLIBC_HAS_IPV6__ */
2343 unsigned char *packet;
2344 struct resolv_answer a;
2347 int __nameserversXX;
2348 char ** __nameserverXX;
2354 memset((char *) &a, '\0', sizeof(a));
2358 if (len != sizeof(struct in_addr))
2361 #ifdef __UCLIBC_HAS_IPV6__
2363 if (len != sizeof(struct in6_addr))
2366 #endif /* __UCLIBC_HAS_IPV6__ */
2371 /* do /etc/hosts first */
2372 if ((i = __get_hosts_byaddr_r(addr, len, type, result_buf,
2373 buf, buflen, result, h_errnop)) == 0)
2375 switch (*h_errnop) {
2376 case HOST_NOT_FOUND:
2383 __open_nameservers();
2385 #ifdef __UCLIBC_HAS_IPV6__
2388 #endif /* __UCLIBC_HAS_IPV6__ */
2390 *h_errnop = NETDB_INTERNAL;
2391 if (buflen < sizeof(*in))
2393 in = (struct in_addr*)buf;
2395 buflen -= sizeof(*in);
2397 if (buflen < sizeof(*addr_list)*2)
2399 addr_list = (struct in_addr**)buf;
2400 buf += sizeof(*addr_list)*2;
2401 buflen -= sizeof(*addr_list)*2;
2403 if (buflen < sizeof(char *)*(ALIAS_DIM))
2405 alias = (char **)buf;
2406 buf += sizeof(*alias)*(ALIAS_DIM);
2407 buflen -= sizeof(*alias)*(ALIAS_DIM);
2409 #ifdef __UCLIBC_HAS_IPV6__
2410 if (plen < sizeof(*in6))
2412 in6 = (struct in6_addr*)qp;
2414 plen -= sizeof(*in6);
2416 if (plen < sizeof(*addr_list6)*2)
2418 addr_list6 = (struct in6_addr**)qp;
2419 qp += sizeof(*addr_list6)*2;
2420 plen -= sizeof(*addr_list6)*2;
2422 if (plen < buflen) {
2426 #endif /* __UCLIBC_HAS_IPV6__ */
2431 if (type == AF_INET) {
2432 unsigned char *tmp_addr = (unsigned char *)addr;
2434 memcpy(&in->s_addr, addr, len);
2438 sprintf(buf, "%u.%u.%u.%u.in-addr.arpa",
2439 tmp_addr[3], tmp_addr[2], tmp_addr[1], tmp_addr[0]);
2440 #ifdef __UCLIBC_HAS_IPV6__
2442 memcpy(in6->s6_addr, addr, len);
2444 addr_list6[0] = in6;
2447 for (i = len - 1; i >= 0; i--) {
2448 qp += sprintf(qp, "%x.%x.", in6->s6_addr[i] & 0xf,
2449 (in6->s6_addr[i] >> 4) & 0xf);
2451 strcpy(qp, "ip6.int");
2452 #endif /* __UCLIBC_HAS_IPV6__ */
2462 __UCLIBC_MUTEX_LOCK(__resolv_lock);
2463 __nameserversXX = __nameservers;
2464 __nameserverXX = __nameserver;
2465 __UCLIBC_MUTEX_UNLOCK(__resolv_lock);
2466 i = __dns_lookup(buf, T_PTR, __nameserversXX, __nameserverXX, &packet, &a);
2469 *h_errnop = HOST_NOT_FOUND;
2473 strncpy(buf, a.dotted, buflen);
2476 if (a.atype == T_CNAME) { /* CNAME */
2477 DPRINTF("Got a CNAME in gethostbyaddr()\n");
2478 i = __decode_dotted(packet, a.rdoffset, buf, buflen);
2482 *h_errnop = NO_RECOVERY;
2485 if (++nest > MAX_RECURSE) {
2486 *h_errnop = NO_RECOVERY;
2490 } else if (a.atype == T_PTR) { /* ADDRESS */
2491 i = __decode_dotted(packet, a.rdoffset, buf, buflen);
2494 result_buf->h_name = buf;
2495 result_buf->h_addrtype = type;
2497 if (type == AF_INET) {
2498 result_buf->h_length = sizeof(*in);
2499 #ifdef __UCLIBC_HAS_IPV6__
2501 result_buf->h_length = sizeof(*in6);
2502 #endif /* __UCLIBC_HAS_IPV6__ */
2505 result_buf->h_addr_list = (char **) addr_list;
2506 result_buf->h_aliases = alias;
2510 *h_errnop = NO_ADDRESS;
2515 *result = result_buf;
2516 *h_errnop = NETDB_SUCCESS;
2517 return NETDB_SUCCESS;
2519 libc_hidden_def(gethostbyaddr_r)
2524 * Expand compressed domain name 'comp_dn' to full domain name.
2525 * 'msg' is a pointer to the begining of the message,
2526 * 'eomorig' points to the first location after the message,
2527 * 'exp_dn' is a pointer to a buffer of size 'length' for the result.
2528 * Return size of compressed name or -1 if there was an error.
2530 int __dn_expand(const u_char *msg, const u_char *eom, const u_char *src,
2531 char *dst, int dstsiz)
2533 int n = ns_name_uncompress(msg, eom, src, dst, (size_t)dstsiz);
2535 if (n > 0 && dst[0] == '.')
2539 #endif /* L_res_comp */
2544 * Thinking in noninternationalized USASCII (per the DNS spec),
2545 * is this character visible and not a space when printed ?
2549 static int printable(int ch)
2551 return (ch > 0x20 && ch < 0x7f);
2556 * Thinking in noninternationalized USASCII (per the DNS spec),
2557 * is this characted special ("in need of quoting") ?
2561 static int special(int ch)
2564 case 0x22: /* '"' */
2565 case 0x2E: /* '.' */
2566 case 0x3B: /* ';' */
2567 case 0x5C: /* '\\' */
2568 /* Special modifiers in zone files. */
2569 case 0x40: /* '@' */
2570 case 0x24: /* '$' */
2578 * ns_name_uncompress(msg, eom, src, dst, dstsiz)
2579 * Expand compressed domain name to presentation format.
2581 * Number of bytes read out of `src', or -1 (with errno set).
2583 * Root domain returns as "." not "".
2585 int ns_name_uncompress(const u_char *msg, const u_char *eom,
2586 const u_char *src, char *dst, size_t dstsiz)
2588 u_char tmp[NS_MAXCDNAME];
2591 if ((n = ns_name_unpack(msg, eom, src, tmp, sizeof tmp)) == -1)
2593 if (ns_name_ntop(tmp, dst, dstsiz) == -1)
2597 libc_hidden_def(ns_name_uncompress)
2601 * ns_name_ntop(src, dst, dstsiz)
2602 * Convert an encoded domain name to printable ascii as per RFC1035.
2604 * Number of bytes written to buffer, or -1 (with errno set)
2606 * The root is returned as "."
2607 * All other domains are returned in non absolute form
2609 int ns_name_ntop(const u_char *src, char *dst, size_t dstsiz) {
2614 const char digits[] = "0123456789";
2620 while ((n = *cp++) != 0) {
2621 if ((n & NS_CMPRSFLGS) != 0) {
2622 /* Some kind of compression pointer. */
2623 __set_errno (EMSGSIZE);
2628 __set_errno (EMSGSIZE);
2633 if (dn + n >= eom) {
2634 __set_errno (EMSGSIZE);
2637 for ((void)NULL; n > 0; n--) {
2640 if (dn + 1 >= eom) {
2641 __set_errno (EMSGSIZE);
2646 } else if (!printable(c)) {
2647 if (dn + 3 >= eom) {
2648 __set_errno (EMSGSIZE);
2652 *dn++ = digits[c / 100];
2653 *dn++ = digits[(c % 100) / 10];
2654 *dn++ = digits[c % 10];
2657 __set_errno (EMSGSIZE);
2666 __set_errno (EMSGSIZE);
2672 __set_errno (EMSGSIZE);
2678 libc_hidden_def(ns_name_ntop)
2681 * ns_name_unpack(msg, eom, src, dst, dstsiz)
2682 * Unpack a domain name from a message, source may be compressed.
2684 * -1 if it fails, or consumed octets if it succeeds.
2686 int ns_name_unpack(const u_char *msg, const u_char *eom, const u_char *src,
2687 u_char *dst, size_t dstsiz)
2689 const u_char *srcp, *dstlim;
2691 int n, len, checked;
2697 dstlim = dst + dstsiz;
2698 if (srcp < msg || srcp >= eom) {
2699 __set_errno (EMSGSIZE);
2702 /* Fetch next label in domain name. */
2703 while ((n = *srcp++) != 0) {
2704 /* Check for indirection. */
2705 switch (n & NS_CMPRSFLGS) {
2708 if (dstp + n + 1 >= dstlim || srcp + n >= eom) {
2709 __set_errno (EMSGSIZE);
2714 memcpy(dstp, srcp, n);
2721 __set_errno (EMSGSIZE);
2725 len = srcp - src + 1;
2726 srcp = msg + (((n & 0x3f) << 8) | (*srcp & 0xff));
2727 if (srcp < msg || srcp >= eom) { /* Out of range. */
2728 __set_errno (EMSGSIZE);
2733 * Check for loops in the compressed name;
2734 * if we've looked at the whole message,
2735 * there must be a loop.
2737 if (checked >= eom - msg) {
2738 __set_errno (EMSGSIZE);
2744 __set_errno (EMSGSIZE);
2745 return (-1); /* flag error */
2753 libc_hidden_def(ns_name_unpack)
2754 #endif /* L_ns_name */