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 strnlen __strnlen
137 #define strncat __strncat
138 #define strstr __strstr
139 #define random __random
140 #define getservbyport __getservbyport
141 #define getdomainname __getdomainname
142 #define uname __uname
143 #define inet_addr __inet_addr
144 #define inet_aton __inet_aton
145 #define inet_pton __inet_pton
146 #define inet_ntop __inet_ntop
147 #define connect __connect
148 #define select __select
151 #define snprintf __snprintf
153 #define __FORCE_GLIBC
154 #include <features.h>
159 #include <sys/socket.h>
160 #include <sys/types.h>
161 #include <sys/time.h>
162 #include <netinet/in.h>
163 #include <arpa/inet.h>
169 #include <arpa/nameser.h>
170 #include <sys/utsname.h>
173 extern int __gethostbyname_r (__const char *__restrict __name,
174 struct hostent *__restrict __result_buf,
175 char *__restrict __buf, size_t __buflen,
176 struct hostent **__restrict __result,
177 int *__restrict __h_errnop) attribute_hidden;
179 extern struct hostent *__gethostbyaddr (__const void *__addr, __socklen_t __len,
180 int __type) attribute_hidden;
181 #define MAX_RECURSE 5
182 #define REPLY_TIMEOUT 10
183 #define MAX_RETRIES 3
184 #define MAX_SERVERS 3
187 #define MAX_ALIASES 5
189 /* 1:ip + 1:full + MAX_ALIASES:aliases + 1:NULL */
190 #define ALIAS_DIM (2 + MAX_ALIASES + 1)
196 #define DPRINTF(X,args...) fprintf(stderr, X, ##args)
198 #define DPRINTF(X,args...)
202 /* Global stuff (stuff needing to be locked to be thread safe)... */
203 extern int __nameservers;
204 extern char * __nameserver[MAX_SERVERS];
205 extern int __searchdomains;
206 extern char * __searchdomain[MAX_SEARCH];
209 #ifdef __UCLIBC_HAS_THREADS__
210 # include <pthread.h>
211 extern pthread_mutex_t __resolv_lock;
213 #define BIGLOCK __pthread_mutex_lock(&__resolv_lock)
214 #define BIGUNLOCK __pthread_mutex_unlock(&__resolv_lock)
219 struct resolv_header {
221 int qr,opcode,aa,tc,rd,ra,rcode;
228 struct resolv_question {
234 struct resolv_answer {
240 unsigned char * rdata;
247 enum etc_hosts_action {
248 GET_HOSTS_BYNAME = 0,
253 /* function prototypes */
254 extern int __get_hosts_byname_r(const char * name, int type,
255 struct hostent * result_buf,
256 char * buf, size_t buflen,
257 struct hostent ** result,
258 int * h_errnop) attribute_hidden;
259 extern int __get_hosts_byaddr_r(const char * addr, int len, int type,
260 struct hostent * result_buf,
261 char * buf, size_t buflen,
262 struct hostent ** result,
263 int * h_errnop) attribute_hidden;
264 extern void __open_etc_hosts(FILE **fp) attribute_hidden;
265 extern int __read_etc_hosts_r(FILE *fp, const char * name, int type,
266 enum etc_hosts_action action,
267 struct hostent * result_buf,
268 char * buf, size_t buflen,
269 struct hostent ** result,
270 int * h_errnop) attribute_hidden;
271 extern int __dns_lookup(const char * name, int type, int nscount,
272 char ** nsip, unsigned char ** outpacket, struct resolv_answer * a) attribute_hidden;
274 extern int __encode_dotted(const char * dotted, unsigned char * dest, int maxlen) attribute_hidden;
275 extern int __decode_dotted(const unsigned char * message, int offset,
276 char * dest, int maxlen) attribute_hidden;
277 extern int __length_dotted(const unsigned char * message, int offset) attribute_hidden;
278 extern int __encode_header(struct resolv_header * h, unsigned char * dest, int maxlen) attribute_hidden;
279 extern int __decode_header(unsigned char * data, struct resolv_header * h) attribute_hidden;
280 extern int __encode_question(struct resolv_question * q,
281 unsigned char * dest, int maxlen) attribute_hidden;
282 extern int __decode_question(unsigned char * message, int offset,
283 struct resolv_question * q);
284 extern int __encode_answer(struct resolv_answer * a,
285 unsigned char * dest, int maxlen) attribute_hidden;
286 extern int __decode_answer(unsigned char * message, int offset,
287 struct resolv_answer * a) attribute_hidden;
288 extern int __length_question(unsigned char * message, int offset) attribute_hidden;
289 extern int __open_nameservers(void) attribute_hidden;
290 extern void __close_nameservers(void) attribute_hidden;
291 extern int __dn_expand(const u_char *, const u_char *, const u_char *,
293 extern int __ns_name_uncompress_internal(const u_char *, const u_char *,
294 const u_char *, char *, size_t) attribute_hidden;
295 extern int __ns_name_ntop_internal(const u_char *, char *, size_t) attribute_hidden;
296 extern int __ns_name_unpack_internal(const u_char *, const u_char *, const u_char *,
297 u_char *, size_t) attribute_hidden;
300 extern int __gethostent_r (struct hostent *__restrict __result_buf,
301 char *__restrict __buf, size_t __buflen,
302 struct hostent **__restrict __result,
303 int *__restrict __h_errnop) attribute_hidden;
306 int attribute_hidden __encode_header(struct resolv_header *h, unsigned char *dest, int maxlen)
308 if (maxlen < HFIXEDSZ)
311 dest[0] = (h->id & 0xff00) >> 8;
312 dest[1] = (h->id & 0x00ff) >> 0;
313 dest[2] = (h->qr ? 0x80 : 0) |
314 ((h->opcode & 0x0f) << 3) |
318 dest[3] = (h->ra ? 0x80 : 0) | (h->rcode & 0x0f);
319 dest[4] = (h->qdcount & 0xff00) >> 8;
320 dest[5] = (h->qdcount & 0x00ff) >> 0;
321 dest[6] = (h->ancount & 0xff00) >> 8;
322 dest[7] = (h->ancount & 0x00ff) >> 0;
323 dest[8] = (h->nscount & 0xff00) >> 8;
324 dest[9] = (h->nscount & 0x00ff) >> 0;
325 dest[10] = (h->arcount & 0xff00) >> 8;
326 dest[11] = (h->arcount & 0x00ff) >> 0;
333 int attribute_hidden __decode_header(unsigned char *data, struct resolv_header *h)
335 h->id = (data[0] << 8) | data[1];
336 h->qr = (data[2] & 0x80) ? 1 : 0;
337 h->opcode = (data[2] >> 3) & 0x0f;
338 h->aa = (data[2] & 0x04) ? 1 : 0;
339 h->tc = (data[2] & 0x02) ? 1 : 0;
340 h->rd = (data[2] & 0x01) ? 1 : 0;
341 h->ra = (data[3] & 0x80) ? 1 : 0;
342 h->rcode = data[3] & 0x0f;
343 h->qdcount = (data[4] << 8) | data[5];
344 h->ancount = (data[6] << 8) | data[7];
345 h->nscount = (data[8] << 8) | data[9];
346 h->arcount = (data[10] << 8) | data[11];
353 /* Encode a dotted string into nameserver transport-level encoding.
354 This routine is fairly dumb, and doesn't attempt to compress
357 int attribute_hidden __encode_dotted(const char *dotted, unsigned char *dest, int maxlen)
361 while (dotted && *dotted) {
362 char *c = __strchr(dotted, '.');
363 int l = c ? c - dotted : __strlen(dotted);
365 if (l >= (maxlen - used - 1))
369 __memcpy(dest + used, dotted, l);
388 /* Decode a dotted string from nameserver transport-level encoding.
389 This routine understands compressed data. */
391 int attribute_hidden __decode_dotted(const unsigned char *data, int offset,
392 char *dest, int maxlen)
402 while ((l=data[offset++])) {
405 if ((l & 0xc0) == (0xc0)) {
408 /* compressed item, redirect */
409 offset = ((l & 0x3f) << 8) | data[offset];
414 if ((used + l + 1) >= maxlen)
417 __memcpy(dest + used, data + offset, l);
423 if (data[offset] != 0)
429 /* The null byte must be counted too */
434 DPRINTF("Total decode len = %d\n", total);
442 int attribute_hidden __length_dotted(const unsigned char *data, int offset)
444 int orig_offset = offset;
450 while ((l = data[offset++])) {
452 if ((l & 0xc0) == (0xc0)) {
460 return offset - orig_offset;
465 int attribute_hidden __encode_question(struct resolv_question *q,
466 unsigned char *dest, int maxlen)
470 i = __encode_dotted(q->dotted, dest, maxlen);
480 dest[0] = (q->qtype & 0xff00) >> 8;
481 dest[1] = (q->qtype & 0x00ff) >> 0;
482 dest[2] = (q->qclass & 0xff00) >> 8;
483 dest[3] = (q->qclass & 0x00ff) >> 0;
490 int __decode_question(unsigned char *message, int offset,
491 struct resolv_question *q)
496 i = __decode_dotted(message, offset, temp, sizeof(temp));
502 q->dotted = __strdup(temp);
503 q->qtype = (message[offset + 0] << 8) | message[offset + 1];
504 q->qclass = (message[offset + 2] << 8) | message[offset + 3];
511 int attribute_hidden __length_question(unsigned char *message, int offset)
515 i = __length_dotted(message, offset);
524 int attribute_hidden __encode_answer(struct resolv_answer *a, unsigned char *dest, int maxlen)
528 i = __encode_dotted(a->dotted, dest, maxlen);
535 if (maxlen < (RRFIXEDSZ+a->rdlength))
538 *dest++ = (a->atype & 0xff00) >> 8;
539 *dest++ = (a->atype & 0x00ff) >> 0;
540 *dest++ = (a->aclass & 0xff00) >> 8;
541 *dest++ = (a->aclass & 0x00ff) >> 0;
542 *dest++ = (a->ttl & 0xff000000) >> 24;
543 *dest++ = (a->ttl & 0x00ff0000) >> 16;
544 *dest++ = (a->ttl & 0x0000ff00) >> 8;
545 *dest++ = (a->ttl & 0x000000ff) >> 0;
546 *dest++ = (a->rdlength & 0xff00) >> 8;
547 *dest++ = (a->rdlength & 0x00ff) >> 0;
548 __memcpy(dest, a->rdata, a->rdlength);
550 return i + RRFIXEDSZ + a->rdlength;
555 int attribute_hidden __decode_answer(unsigned char *message, int offset,
556 struct resolv_answer *a)
561 i = __decode_dotted(message, offset, temp, sizeof(temp));
565 message += offset + i;
567 a->dotted = __strdup(temp);
568 a->atype = (message[0] << 8) | message[1];
570 a->aclass = (message[0] << 8) | message[1];
572 a->ttl = (message[0] << 24) |
573 (message[1] << 16) | (message[2] << 8) | (message[3] << 0);
575 a->rdlength = (message[0] << 8) | message[1];
578 a->rdoffset = offset + i + RRFIXEDSZ;
580 DPRINTF("i=%d,rdlength=%d\n", i, a->rdlength);
582 return i + RRFIXEDSZ + a->rdlength;
587 int __encode_packet(struct resolv_header *h,
588 struct resolv_question **q,
589 struct resolv_answer **an,
590 struct resolv_answer **ns,
591 struct resolv_answer **ar,
592 unsigned char *dest, int maxlen)
597 i = __encode_header(h, dest, maxlen);
605 for (j = 0; j < h->qdcount; j++) {
606 i = __encode_question(q[j], dest, maxlen);
614 for (j = 0; j < h->ancount; j++) {
615 i = __encode_answer(an[j], dest, maxlen);
622 for (j = 0; j < h->nscount; j++) {
623 i = __encode_answer(ns[j], dest, maxlen);
630 for (j = 0; j < h->arcount; j++) {
631 i = __encode_answer(ar[j], dest, maxlen);
644 int __decode_packet(unsigned char *data, struct resolv_header *h)
646 return __decode_header(data, h);
651 int __form_query(int id, const char *name, int type, unsigned char *packet,
654 struct resolv_header h;
655 struct resolv_question q;
658 __memset(&h, 0, sizeof(h));
662 q.dotted = (char *) name;
664 q.qclass = C_IN; /* CLASS_IN */
666 i = __encode_header(&h, packet, maxlen);
670 j = __encode_question(&q, packet + i, maxlen - i);
678 #if defined(L_dnslookup) || defined(L_gethostent)
679 #ifdef __UCLIBC_HAS_THREADS__
680 # include <pthread.h>
681 static pthread_mutex_t mylock = PTHREAD_MUTEX_INITIALIZER;
683 #define LOCK __pthread_mutex_lock(&mylock)
684 #define UNLOCK __pthread_mutex_unlock(&mylock)
689 /* Just for the record, having to lock __dns_lookup() just for these two globals
690 * is pretty lame. I think these two variables can probably be de-global-ized,
691 * which should eliminate the need for doing locking here... Needs a closer
693 static int ns=0, id=1;
695 int attribute_hidden __dns_lookup(const char *name, int type, int nscount, char **nsip,
696 unsigned char **outpacket, struct resolv_answer *a)
698 int i, j, len, fd, pos, rc;
701 struct resolv_header h;
702 struct resolv_question q;
703 struct resolv_answer ma;
704 int first_answer = 1;
706 unsigned char * packet = malloc(PACKETSZ);
707 char *dns, *lookup = malloc(MAXDNAME);
709 struct sockaddr_in sa;
710 int local_ns = -1, local_id = -1;
711 #ifdef __UCLIBC_HAS_IPV6__
713 struct sockaddr_in6 sa6;
718 if (!packet || !lookup || !nscount)
721 DPRINTF("Looking up type %d answer for '%s'\n", type, name);
723 /* Mess with globals while under lock */
725 local_ns = ns % nscount;
729 while (retries < MAX_RETRIES) {
733 __memset(packet, 0, PACKETSZ);
735 __memset(&h, 0, sizeof(h));
740 dns = nsip[local_ns];
745 DPRINTF("encoding header\n", h.rd);
747 i = __encode_header(&h, packet, PACKETSZ);
751 __strncpy(lookup,name,MAXDNAME);
754 if (variant < __searchdomains) {
755 strncat(lookup,".", MAXDNAME);
756 strncat(lookup,__searchdomain[variant], MAXDNAME);
760 DPRINTF("lookup name: %s\n", lookup);
761 q.dotted = (char *)lookup;
763 q.qclass = C_IN; /* CLASS_IN */
765 j = __encode_question(&q, packet+i, PACKETSZ-i);
771 DPRINTF("On try %d, sending query to port %d of machine %s\n",
772 retries+1, NAMESERVER_PORT, dns);
774 #ifdef __UCLIBC_HAS_IPV6__
775 v6 = inet_pton(AF_INET6, dns, &sa6.sin6_addr) > 0;
776 fd = __socket(v6 ? AF_INET6 : AF_INET, SOCK_DGRAM, IPPROTO_UDP);
778 fd = __socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
785 /* Connect to the UDP socket so that asyncronous errors are returned */
786 #ifdef __UCLIBC_HAS_IPV6__
788 sa6.sin6_family = AF_INET6;
789 sa6.sin6_port = htons(NAMESERVER_PORT);
790 /* sa6.sin6_addr is already here */
791 rc = connect(fd, (struct sockaddr *) &sa6, sizeof(sa6));
794 sa.sin_family = AF_INET;
795 sa.sin_port = htons(NAMESERVER_PORT);
796 sa.sin_addr.s_addr = inet_addr(dns);
797 rc = connect(fd, (struct sockaddr *) &sa, sizeof(sa));
798 #ifdef __UCLIBC_HAS_IPV6__
802 if (errno == ENETUNREACH) {
803 /* routing error, presume not transient */
811 DPRINTF("Transmitting packet of length %d, id=%d, qr=%d\n",
814 send(fd, packet, len, 0);
818 tv.tv_sec = REPLY_TIMEOUT;
820 if (select(fd + 1, &fds, NULL, NULL, &tv) <= 0) {
821 DPRINTF("Timeout\n");
823 /* timed out, so retry send and receive,
824 * to next nameserver on queue */
828 len = recv(fd, packet, 512, 0);
829 if (len < HFIXEDSZ) {
834 __decode_header(packet, &h);
836 DPRINTF("id = %d, qr = %d\n", h.id, h.qr);
838 if ((h.id != local_id) || (!h.qr)) {
844 DPRINTF("Got response %s\n", "(i think)!");
845 DPRINTF("qrcount=%d,ancount=%d,nscount=%d,arcount=%d\n",
846 h.qdcount, h.ancount, h.nscount, h.arcount);
847 DPRINTF("opcode=%d,aa=%d,tc=%d,rd=%d,ra=%d,rcode=%d\n",
848 h.opcode, h.aa, h.tc, h.rd, h.ra, h.rcode);
850 if ((h.rcode) || (h.ancount < 1)) {
851 /* negative result, not present */
857 for (j = 0; j < h.qdcount; j++) {
858 DPRINTF("Skipping question %d at %d\n", j, pos);
859 i = __length_question(packet, pos);
860 DPRINTF("Length of question %d is %d\n", j, i);
865 DPRINTF("Decoding answer at pos %d\n", pos);
868 for (j=0;j<h.ancount;j++,pos += i)
870 i = __decode_answer(packet, pos, &ma);
873 DPRINTF("failed decode %d\n", i);
880 ma.buflen = a->buflen;
881 ma.add_count = a->add_count;
882 __memcpy(a, &ma, sizeof(ma));
883 if (a->atype != T_SIG && (0 == a->buf || (type != T_A && type != T_AAAA)))
887 if (a->atype != type)
892 a->add_count = h.ancount - j - 1;
893 if ((a->rdlength + sizeof(struct in_addr*)) * a->add_count > a->buflen)
903 if (ma.atype != type)
907 if (a->rdlength != ma.rdlength)
910 DPRINTF("Answer address len(%u) differs from original(%u)\n",
911 ma.rdlength, a->rdlength);
914 __memcpy(a->buf + (a->add_count * ma.rdlength), ma.rdata, ma.rdlength);
919 DPRINTF("Answer name = |%s|\n", a->dotted);
920 DPRINTF("Answer type = |%d|\n", a->atype);
930 /* Mess with globals while under lock */
936 return (len); /* success! */
939 /* if there are other nameservers, give them a go,
940 otherwise return with error */
943 local_ns = (local_ns + 1) % nscount;
951 /* if there are searchdomains, try them or fallback as passed */
955 sdomains=__searchdomains;
958 if (variant < sdomains - 1) {
962 /* next server, first search */
963 local_ns = (local_ns + 1) % nscount;
979 h_errno = NETDB_INTERNAL;
980 /* Mess with globals while under lock */
981 if (local_ns != -1) {
991 #ifdef L_opennameservers
994 char * __nameserver[MAX_SERVERS];
996 char * __searchdomain[MAX_SEARCH];
997 #ifdef __UCLIBC_HAS_THREADS__
998 # include <pthread.h>
999 pthread_mutex_t __resolv_lock = PTHREAD_MUTEX_INITIALIZER;
1003 * we currently read formats not quite the same as that on normal
1004 * unix systems, we can have a list of nameservers after the keyword.
1007 int attribute_hidden __open_nameservers()
1011 #define RESOLV_ARGS 5
1012 char szBuffer[128], *p, *argv[RESOLV_ARGS];
1016 if (__nameservers > 0) {
1021 if ((fp = fopen("/etc/resolv.conf", "r")) ||
1022 (fp = fopen("/etc/config/resolv.conf", "r")))
1025 while (fgets(szBuffer, sizeof(szBuffer), fp) != NULL) {
1027 for (p = szBuffer; *p && isspace(*p); p++)
1028 /* skip white space */;
1029 if (*p == '\0' || *p == '\n' || *p == '#') /* skip comments etc */
1032 while (*p && argc < RESOLV_ARGS) {
1034 while (*p && !isspace(*p) && *p != '\n')
1036 while (*p && (isspace(*p) || *p == '\n')) /* remove spaces */
1040 if (__strcmp(argv[0], "nameserver") == 0) {
1041 for (i = 1; i < argc && __nameservers < MAX_SERVERS; i++) {
1042 __nameserver[__nameservers++] = __strdup(argv[i]);
1043 DPRINTF("adding nameserver %s\n", argv[i]);
1047 /* domain and search are mutually exclusive, the last one wins */
1048 if (__strcmp(argv[0],"domain")==0 || __strcmp(argv[0],"search")==0) {
1049 while (__searchdomains > 0) {
1050 free(__searchdomain[--__searchdomains]);
1051 __searchdomain[__searchdomains] = NULL;
1053 for (i=1; i < argc && __searchdomains < MAX_SEARCH; i++) {
1054 __searchdomain[__searchdomains++] = __strdup(argv[i]);
1055 DPRINTF("adding search %s\n", argv[i]);
1060 DPRINTF("nameservers = %d\n", __nameservers);
1064 DPRINTF("failed to open %s\n", "resolv.conf");
1065 h_errno = NO_RECOVERY;
1072 #ifdef L_closenameservers
1074 void attribute_hidden __close_nameservers(void)
1077 while (__nameservers > 0) {
1078 free(__nameserver[--__nameservers]);
1079 __nameserver[__nameservers] = NULL;
1081 while (__searchdomains > 0) {
1082 free(__searchdomain[--__searchdomains]);
1083 __searchdomain[__searchdomains] = NULL;
1089 #ifdef L_gethostbyname
1091 struct hostent attribute_hidden *__gethostbyname(const char *name)
1093 static struct hostent h;
1094 static char buf[sizeof(struct in_addr) +
1095 sizeof(struct in_addr *)*2 +
1096 sizeof(char *)*(ALIAS_DIM) + 384/*namebuffer*/ + 32/* margin */];
1099 __gethostbyname_r(name, &h, buf, sizeof(buf), &hp, &h_errno);
1103 strong_alias(__gethostbyname,gethostbyname)
1106 #ifdef L_gethostbyname2
1108 #ifndef __UCLIBC_HAS_IPV6__
1109 extern struct hostent *__gethostbyname (__const char *__name) attribute_hidden;
1111 extern int __gethostbyname2_r (__const char *__restrict __name, int __af,
1112 struct hostent *__restrict __result_buf,
1113 char *__restrict __buf, size_t __buflen,
1114 struct hostent **__restrict __result,
1115 int *__restrict __h_errnop) attribute_hidden;
1118 struct hostent *gethostbyname2(const char *name, int family)
1120 #ifndef __UCLIBC_HAS_IPV6__
1121 return family == AF_INET ? __gethostbyname(name) : (struct hostent*)0;
1122 #else /* __UCLIBC_HAS_IPV6__ */
1123 static struct hostent h;
1124 static char buf[sizeof(struct in6_addr) +
1125 sizeof(struct in6_addr *)*2 +
1126 sizeof(char *)*(ALIAS_DIM) + 384/*namebuffer*/ + 32/* margin */];
1129 __gethostbyname2_r(name, family, &h, buf, sizeof(buf), &hp, &h_errno);
1132 #endif /* __UCLIBC_HAS_IPV6__ */
1139 struct __res_state _res;
1143 struct __res_state *rp = &(_res);
1145 __close_nameservers();
1146 __open_nameservers();
1147 rp->retrans = RES_TIMEOUT;
1149 rp->options = RES_INIT;
1150 rp->id = (u_int) random();
1151 rp->nsaddr.sin_addr.s_addr = INADDR_ANY;
1152 rp->nsaddr.sin_family = AF_INET;
1153 rp->nsaddr.sin_port = htons(NAMESERVER_PORT);
1155 /** rp->pfcode = 0; **/
1157 /** rp->_flags = 0; **/
1158 /** rp->qhook = NULL; **/
1159 /** rp->rhook = NULL; **/
1160 /** rp->_u._ext.nsinit = 0; **/
1163 if(__searchdomains) {
1165 for(i=0; i<__searchdomains; i++) {
1166 rp->dnsrch[i] = __searchdomain[i];
1173 for(i=0; i<__nameservers; i++) {
1174 if (inet_aton(__nameserver[i], &a)) {
1175 rp->nsaddr_list[i].sin_addr = a;
1176 rp->nsaddr_list[i].sin_family = AF_INET;
1177 rp->nsaddr_list[i].sin_port = htons(NAMESERVER_PORT);
1181 rp->nscount = __nameservers;
1187 void res_close( void )
1198 #define MIN(x, y) ((x) < (y) ? (x) : (y))
1201 int res_query(const char *dname, int class, int type,
1202 unsigned char *answer, int anslen)
1205 unsigned char * packet = 0;
1206 struct resolv_answer a;
1207 int __nameserversXX;
1208 char ** __nameserverXX;
1210 __open_nameservers();
1211 if (!dname || class != 1 /* CLASS_IN */) {
1212 h_errno = NO_RECOVERY;
1216 __memset((char *) &a, '\0', sizeof(a));
1219 __nameserversXX=__nameservers;
1220 __nameserverXX=__nameserver;
1222 i = __dns_lookup(dname, type, __nameserversXX, __nameserverXX, &packet, &a);
1225 h_errno = TRY_AGAIN;
1231 if (a.atype == type) { /* CNAME*/
1232 int len = MIN(anslen, i);
1233 __memcpy(answer, packet, len);
1244 * Formulate a normal query, send, and retrieve answer in supplied buffer.
1245 * Return the size of the response on success, -1 on error.
1246 * If enabled, implement search rules until answer or unrecoverable failure
1247 * is detected. Error code, if any, is left in h_errno.
1249 int res_search(name, class, type, answer, anslen)
1250 const char *name; /* domain name */
1251 int class, type; /* class and type of query */
1252 u_char *answer; /* buffer to put answer */
1253 int anslen; /* size of answer */
1255 const char *cp, * const *domain;
1256 HEADER *hp = (HEADER *)(void *)answer;
1258 int trailing_dot, ret, saved_herrno;
1259 int got_nodata = 0, got_servfail = 0, tried_as_is = 0;
1261 if ((!name || !answer) || ((_res.options & RES_INIT) == 0 && res_init() == -1)) {
1262 h_errno = NETDB_INTERNAL;
1267 h_errno = HOST_NOT_FOUND; /* default, if we never query */
1269 for (cp = name; *cp; cp++)
1270 dots += (*cp == '.');
1272 if (cp > name && *--cp == '.')
1276 * If there are dots in the name already, let's just give it a try
1277 * 'as is'. The threshold can be set with the "ndots" option.
1280 if (dots >= _res.ndots) {
1281 ret = res_querydomain(name, NULL, class, type, answer, anslen);
1284 saved_herrno = h_errno;
1289 * We do at least one level of search if
1290 * - there is no dot and RES_DEFNAME is set, or
1291 * - there is at least one dot, there is no trailing dot,
1292 * and RES_DNSRCH is set.
1294 if ((!dots && (_res.options & RES_DEFNAMES)) ||
1295 (dots && !trailing_dot && (_res.options & RES_DNSRCH))) {
1298 for (domain = (const char * const *)_res.dnsrch;
1302 ret = res_querydomain(name, *domain, class, type,
1308 * If no server present, give up.
1309 * If name isn't found in this domain,
1310 * keep trying higher domains in the search list
1311 * (if that's enabled).
1312 * On a NO_DATA error, keep trying, otherwise
1313 * a wildcard entry of another type could keep us
1314 * from finding this entry higher in the domain.
1315 * If we get some other error (negative answer or
1316 * server failure), then stop searching up,
1317 * but try the input name below in case it's
1320 if (errno == ECONNREFUSED) {
1321 h_errno = TRY_AGAIN;
1329 case HOST_NOT_FOUND:
1333 if (hp->rcode == SERVFAIL) {
1334 /* try next search element, if any */
1340 /* anything else implies that we're done */
1344 * if we got here for some reason other than DNSRCH,
1345 * we only wanted one iteration of the loop, so stop.
1347 if (!(_res.options & RES_DNSRCH))
1353 * if we have not already tried the name "as is", do that now.
1354 * note that we do this regardless of how many dots were in the
1355 * name or whether it ends with a dot.
1358 ret = res_querydomain(name, NULL, class, type, answer, anslen);
1364 * if we got here, we didn't satisfy the search.
1365 * if we did an initial full query, return that query's h_errno
1366 * (note that we wouldn't be here if that query had succeeded).
1367 * else if we ever got a nodata, send that back as the reason.
1368 * else send back meaningless h_errno, that being the one from
1369 * the last DNSRCH we did.
1371 if (saved_herrno != -1)
1372 h_errno = saved_herrno;
1373 else if (got_nodata)
1375 else if (got_servfail)
1376 h_errno = TRY_AGAIN;
1381 * Perform a call on res_query on the concatenation of name and domain,
1382 * removing a trailing dot from name if domain is NULL.
1384 int res_querydomain(name, domain, class, type, answer, anslen)
1385 const char *name, *domain;
1386 int class, type; /* class and type of query */
1387 u_char *answer; /* buffer to put answer */
1388 int anslen; /* size of answer */
1390 char nbuf[MAXDNAME];
1391 const char *longname = nbuf;
1394 if ((!name || !answer) || ((_res.options & RES_INIT) == 0 && res_init() == -1)) {
1395 h_errno = NETDB_INTERNAL;
1400 if (_res.options & RES_DEBUG)
1401 __printf(";; res_querydomain(%s, %s, %d, %d)\n",
1402 name, domain?domain:"<Nil>", class, type);
1404 if (domain == NULL) {
1406 * Check for trailing '.';
1407 * copy without '.' if present.
1410 if (n + 1 > sizeof(nbuf)) {
1411 h_errno = NO_RECOVERY;
1414 if (n > 0 && name[--n] == '.') {
1415 __strncpy(nbuf, name, n);
1421 d = __strlen(domain);
1422 if (n + 1 + d + 1 > sizeof(nbuf)) {
1423 h_errno = NO_RECOVERY;
1426 snprintf(nbuf, sizeof(nbuf), "%s.%s", name, domain);
1428 return (res_query(longname, class, type, answer, anslen));
1437 #ifdef L_gethostbyaddr
1438 extern int __gethostbyaddr_r (__const void *__restrict __addr, __socklen_t __len,
1440 struct hostent *__restrict __result_buf,
1441 char *__restrict __buf, size_t __buflen,
1442 struct hostent **__restrict __result,
1443 int *__restrict __h_errnop) attribute_hidden;
1445 struct hostent attribute_hidden *__gethostbyaddr (const void *addr, socklen_t len, int type)
1447 static struct hostent h;
1449 #ifndef __UCLIBC_HAS_IPV6__
1450 sizeof(struct in_addr) + sizeof(struct in_addr *)*2 +
1452 sizeof(struct in6_addr) + sizeof(struct in6_addr *)*2 +
1453 #endif /* __UCLIBC_HAS_IPV6__ */
1454 sizeof(char *)*(ALIAS_DIM) + 384/*namebuffer*/ + 32/* margin */];
1457 __gethostbyaddr_r(addr, len, type, &h, buf, sizeof(buf), &hp, &h_errno);
1461 strong_alias(__gethostbyaddr,gethostbyaddr)
1465 #ifdef L_read_etc_hosts_r
1467 void attribute_hidden __open_etc_hosts(FILE **fp)
1469 if ((*fp = fopen("/etc/hosts", "r")) == NULL) {
1470 *fp = fopen("/etc/config/hosts", "r");
1475 int attribute_hidden __read_etc_hosts_r(FILE * fp, const char * name, int type,
1476 enum etc_hosts_action action,
1477 struct hostent * result_buf,
1478 char * buf, size_t buflen,
1479 struct hostent ** result,
1482 struct in_addr *in=NULL;
1483 struct in_addr **addr_list=NULL;
1484 #ifdef __UCLIBC_HAS_IPV6__
1485 struct in6_addr *in6=NULL;
1486 struct in6_addr **addr_list6=NULL;
1487 #endif /* __UCLIBC_HAS_IPV6__ */
1489 int aliases, i, ret=HOST_NOT_FOUND;
1491 if (buflen < sizeof(char *)*(ALIAS_DIM))
1494 buf+=sizeof(char **)*(ALIAS_DIM);
1495 buflen-=sizeof(char **)*(ALIAS_DIM);
1497 if (action!=GETHOSTENT) {
1498 #ifdef __UCLIBC_HAS_IPV6__
1501 #endif /* __UCLIBC_HAS_IPV6__ */
1502 *h_errnop=NETDB_INTERNAL;
1503 if (buflen < sizeof(*in))
1505 in=(struct in_addr*)buf;
1507 buflen-=sizeof(*in);
1509 if (buflen < sizeof(*addr_list)*2)
1511 addr_list=(struct in_addr **)buf;
1512 buf+=sizeof(*addr_list)*2;
1513 buflen-=sizeof(*addr_list)*2;
1515 #ifdef __UCLIBC_HAS_IPV6__
1516 if (len < sizeof(*in6))
1518 in6=(struct in6_addr*)p;
1522 if (len < sizeof(*addr_list6)*2)
1524 addr_list6=(struct in6_addr**)p;
1525 p+=sizeof(*addr_list6)*2;
1526 len-=sizeof(*addr_list6)*2;
1532 #endif /* __UCLIBC_HAS_IPV6__ */
1537 __open_etc_hosts(&fp);
1544 *h_errnop=HOST_NOT_FOUND;
1545 while (fgets(buf, buflen, fp)) {
1546 if ((cp = __strchr(buf, '#')))
1548 DPRINTF("Looking at: %s\n", buf);
1553 while (*cp && isspace(*cp))
1557 if (aliases < (2+MAX_ALIASES))
1558 alias[aliases++] = cp;
1559 while (*cp && !isspace(*cp))
1565 continue; /* syntax error really */
1567 if (action==GETHOSTENT) {
1568 /* Return whatever the next entry happens to be. */
1570 } else if (action==GET_HOSTS_BYADDR) {
1571 if (__strcmp(name, alias[0]) != 0)
1574 /* GET_HOSTS_BYNAME */
1575 for (i = 1; i < aliases; i++)
1576 if (__strcasecmp(name, alias[i]) == 0)
1582 if (type == AF_INET && inet_pton(AF_INET, alias[0], in) > 0) {
1583 DPRINTF("Found INET\n");
1586 result_buf->h_name = alias[1];
1587 result_buf->h_addrtype = AF_INET;
1588 result_buf->h_length = sizeof(*in);
1589 result_buf->h_addr_list = (char**) addr_list;
1590 result_buf->h_aliases = alias + 2;
1593 #ifdef __UCLIBC_HAS_IPV6__
1594 } else if (type == AF_INET6 && inet_pton(AF_INET6, alias[0], in6) > 0) {
1595 DPRINTF("Found INET6\n");
1596 addr_list6[0] = in6;
1598 result_buf->h_name = alias[1];
1599 result_buf->h_addrtype = AF_INET6;
1600 result_buf->h_length = sizeof(*in6);
1601 result_buf->h_addr_list = (char**) addr_list6;
1602 result_buf->h_aliases = alias + 2;
1605 #endif /* __UCLIBC_HAS_IPV6__ */
1609 break; /* bad ip address */
1612 if (action!=GETHOSTENT) {
1617 if (action!=GETHOSTENT) {
1627 static int __stay_open;
1628 static FILE * __gethostent_fp;
1630 void endhostent (void)
1634 if (__gethostent_fp) {
1635 fclose(__gethostent_fp);
1640 void sethostent (int stay_open)
1643 __stay_open = stay_open;
1647 int attribute_hidden __gethostent_r(struct hostent *result_buf, char *buf, size_t buflen,
1648 struct hostent **result, int *h_errnop)
1653 if (__gethostent_fp == NULL) {
1654 __open_etc_hosts(&__gethostent_fp);
1655 if (__gethostent_fp == NULL) {
1662 ret = __read_etc_hosts_r(__gethostent_fp, NULL, AF_INET, GETHOSTENT,
1663 result_buf, buf, buflen, result, h_errnop);
1664 if (__stay_open==0) {
1665 fclose(__gethostent_fp);
1670 strong_alias(__gethostent_r,gethostent_r)
1672 struct hostent *gethostent (void)
1674 static struct hostent h;
1676 #ifndef __UCLIBC_HAS_IPV6__
1677 sizeof(struct in_addr) + sizeof(struct in_addr *)*2 +
1679 sizeof(struct in6_addr) + sizeof(struct in6_addr *)*2 +
1680 #endif /* __UCLIBC_HAS_IPV6__ */
1681 sizeof(char *)*(ALIAS_DIM) +
1682 80/*namebuffer*/ + 2/* margin */];
1683 struct hostent *host;
1686 __gethostent_r(&h, buf, sizeof(buf), &host, &h_errno);
1692 #ifdef L_get_hosts_byname_r
1694 int attribute_hidden __get_hosts_byname_r(const char * name, int type,
1695 struct hostent * result_buf,
1696 char * buf, size_t buflen,
1697 struct hostent ** result,
1700 return(__read_etc_hosts_r(NULL, name, type, GET_HOSTS_BYNAME,
1701 result_buf, buf, buflen, result, h_errnop));
1705 #ifdef L_get_hosts_byaddr_r
1707 int attribute_hidden __get_hosts_byaddr_r(const char * addr, int len, int type,
1708 struct hostent * result_buf,
1709 char * buf, size_t buflen,
1710 struct hostent ** result,
1713 #ifndef __UCLIBC_HAS_IPV6__
1714 char ipaddr[INET_ADDRSTRLEN];
1716 char ipaddr[INET6_ADDRSTRLEN];
1717 #endif /* __UCLIBC_HAS_IPV6__ */
1721 if (len != sizeof(struct in_addr))
1724 #ifdef __UCLIBC_HAS_IPV6__
1726 if (len != sizeof(struct in6_addr))
1729 #endif /* __UCLIBC_HAS_IPV6__ */
1734 inet_ntop(type, addr, ipaddr, sizeof(ipaddr));
1736 return(__read_etc_hosts_r(NULL, ipaddr, type, GET_HOSTS_BYADDR,
1737 result_buf, buf, buflen, result, h_errnop));
1741 #ifdef L_getnameinfo
1744 # define min(x,y) (((x) > (y)) ? (y) : (x))
1747 int attribute_hidden __getnameinfo (const struct sockaddr *sa, socklen_t addrlen, char *host,
1748 socklen_t hostlen, char *serv, socklen_t servlen,
1753 struct hostent *h = NULL;
1756 if (flags & ~(NI_NUMERICHOST|NI_NUMERICSERV|NI_NOFQDN|NI_NAMEREQD|NI_DGRAM))
1757 return EAI_BADFLAGS;
1759 if (sa == NULL || addrlen < sizeof (sa_family_t))
1762 switch (sa->sa_family) {
1766 if (addrlen < sizeof (struct sockaddr_in))
1769 #ifdef __UCLIBC_HAS_IPV6__
1771 if (addrlen < sizeof (struct sockaddr_in6))
1774 #endif /* __UCLIBC_HAS_IPV6__ */
1779 if (host != NULL && hostlen > 0)
1780 switch (sa->sa_family) {
1782 #ifdef __UCLIBC_HAS_IPV6__
1784 #endif /* __UCLIBC_HAS_IPV6__ */
1785 if (!(flags & NI_NUMERICHOST)) {
1786 #ifdef __UCLIBC_HAS_IPV6__
1787 if (sa->sa_family == AF_INET6)
1788 h = __gethostbyaddr ((const void *)
1789 &(((const struct sockaddr_in6 *) sa)->sin6_addr),
1790 sizeof(struct in6_addr), AF_INET6);
1792 #endif /* __UCLIBC_HAS_IPV6__ */
1793 h = __gethostbyaddr ((const void *) &(((const struct sockaddr_in *)sa)->sin_addr),
1794 sizeof(struct in_addr), AF_INET);
1798 if ((flags & NI_NOFQDN)
1799 && (getdomainname (domain, sizeof(domain)) == 0)
1800 && (c = strstr (h->h_name, domain))
1801 && (c != h->h_name) && (*(--c) == '.')) {
1802 __strncpy (host, h->h_name,
1803 min(hostlen, (size_t) (c - h->h_name)));
1804 host[min(hostlen - 1, (size_t) (c - h->h_name))] = '\0';
1807 __strncpy (host, h->h_name, hostlen);
1814 if (flags & NI_NAMEREQD) {
1819 #ifdef __UCLIBC_HAS_IPV6__
1820 if (sa->sa_family == AF_INET6) {
1821 const struct sockaddr_in6 *sin6p;
1823 sin6p = (const struct sockaddr_in6 *) sa;
1825 c = inet_ntop (AF_INET6,
1826 (const void *) &sin6p->sin6_addr, host, hostlen);
1828 /* Does scope id need to be supported? */
1830 scopeid = sin6p->sin6_scope_id;
1832 /* Buffer is >= IFNAMSIZ+1. */
1833 char scopebuf[IFNAMSIZ + 1];
1835 int ni_numericscope = 0;
1836 size_t real_hostlen = __strnlen (host, hostlen);
1837 size_t scopelen = 0;
1839 scopebuf[0] = SCOPE_DELIMITER;
1841 scopeptr = &scopebuf[1];
1843 if (IN6_IS_ADDR_LINKLOCAL (&sin6p->sin6_addr)
1844 || IN6_IS_ADDR_MC_LINKLOCAL (&sin6p->sin6_addr)) {
1845 if (if_indextoname (scopeid, scopeptr) == NULL)
1848 scopelen = __strlen (scopebuf);
1853 if (ni_numericscope)
1854 scopelen = 1 + snprintf (scopeptr,
1860 if (real_hostlen + scopelen + 1 > hostlen)
1862 __memcpy (host + real_hostlen, scopebuf, scopelen + 1);
1866 #endif /* __UCLIBC_HAS_IPV6__ */
1867 c = inet_ntop (AF_INET, (const void *)
1868 &(((const struct sockaddr_in *) sa)->sin_addr),
1881 if (!(flags & NI_NUMERICHOST)) {
1882 struct utsname utsname;
1884 if (!uname (&utsname)) {
1885 __strncpy (host, utsname.nodename, hostlen);
1890 if (flags & NI_NAMEREQD) {
1895 __strncpy (host, "localhost", hostlen);
1902 if (serv && (servlen > 0)) {
1903 switch (sa->sa_family) {
1905 #ifdef __UCLIBC_HAS_IPV6__
1907 #endif /* __UCLIBC_HAS_IPV6__ */
1908 if (!(flags & NI_NUMERICSERV)) {
1910 s = getservbyport (((const struct sockaddr_in *) sa)->sin_port,
1911 ((flags & NI_DGRAM) ? "udp" : "tcp"));
1913 __strncpy (serv, s->s_name, servlen);
1917 snprintf (serv, servlen, "%d",
1918 ntohs (((const struct sockaddr_in *) sa)->sin_port));
1922 __strncpy (serv, ((const struct sockaddr_un *) sa)->sun_path, servlen);
1926 if (host && (hostlen > 0))
1927 host[hostlen-1] = 0;
1928 if (serv && (servlen > 0))
1929 serv[servlen-1] = 0;
1933 strong_alias(__getnameinfo,getnameinfo)
1937 #ifdef L_gethostbyname_r
1939 int attribute_hidden __gethostbyname_r(const char * name,
1940 struct hostent * result_buf,
1941 char * buf, size_t buflen,
1942 struct hostent ** result,
1946 struct in_addr **addr_list;
1948 unsigned char *packet;
1949 struct resolv_answer a;
1951 int __nameserversXX;
1952 char ** __nameserverXX;
1954 __open_nameservers();
1959 /* do /etc/hosts first */
1961 int old_errno = errno; /* Save the old errno and reset errno */
1962 __set_errno(0); /* to check for missing /etc/hosts. */
1964 if ((i=__get_hosts_byname_r(name, AF_INET, result_buf,
1965 buf, buflen, result, h_errnop))==0)
1967 switch (*h_errnop) {
1968 case HOST_NOT_FOUND:
1971 case NETDB_INTERNAL:
1972 if (errno == ENOENT) {
1975 /* else fall through */
1979 __set_errno(old_errno);
1982 DPRINTF("Nothing found in /etc/hosts\n");
1984 *h_errnop = NETDB_INTERNAL;
1985 if (buflen < sizeof(*in))
1987 in=(struct in_addr*)buf;
1989 buflen-=sizeof(*in);
1991 if (buflen < sizeof(*addr_list)*2)
1993 addr_list=(struct in_addr**)buf;
1994 buf+=sizeof(*addr_list)*2;
1995 buflen-=sizeof(*addr_list)*2;
2000 if (buflen < sizeof(char *)*(ALIAS_DIM))
2003 buf+=sizeof(char **)*(ALIAS_DIM);
2004 buflen-=sizeof(char **)*(ALIAS_DIM);
2008 __strncpy(buf, name, buflen);
2013 /* First check if this is already an address */
2014 if (inet_aton(name, in)) {
2015 result_buf->h_name = buf;
2016 result_buf->h_addrtype = AF_INET;
2017 result_buf->h_length = sizeof(*in);
2018 result_buf->h_addr_list = (char **) addr_list;
2019 result_buf->h_aliases = alias;
2021 *h_errnop = NETDB_SUCCESS;
2022 return NETDB_SUCCESS;
2028 __nameserversXX=__nameservers;
2029 __nameserverXX=__nameserver;
2034 i = __dns_lookup(name, T_A, __nameserversXX, __nameserverXX, &packet, &a);
2037 *h_errnop = HOST_NOT_FOUND;
2038 DPRINTF("__dns_lookup\n");
2042 if ((a.rdlength + sizeof(struct in_addr*)) * a.add_count + 256 > buflen)
2046 *h_errnop = NETDB_INTERNAL;
2047 DPRINTF("buffer too small for all addresses\n");
2050 else if(a.add_count > 0)
2052 __memmove(buf - sizeof(struct in_addr*)*2, buf, a.add_count * a.rdlength);
2053 addr_list = (struct in_addr**)(buf + a.add_count * a.rdlength);
2055 for (i = a.add_count-1; i>=0; --i)
2056 addr_list[i+1] = (struct in_addr*)(buf - sizeof(struct in_addr*)*2 + a.rdlength * i);
2057 addr_list[a.add_count + 1] = 0;
2058 buflen -= (((char*)&(addr_list[a.add_count + 2])) - buf);
2059 buf = (char*)&addr_list[a.add_count + 2];
2062 __strncpy(buf, a.dotted, buflen);
2065 if (a.atype == T_A) { /* ADDRESS */
2066 __memcpy(in, a.rdata, sizeof(*in));
2067 result_buf->h_name = buf;
2068 result_buf->h_addrtype = AF_INET;
2069 result_buf->h_length = sizeof(*in);
2070 result_buf->h_addr_list = (char **) addr_list;
2071 #ifdef __UCLIBC_MJN3_ONLY__
2072 #warning TODO -- generate the full list
2074 result_buf->h_aliases = alias; /* TODO: generate the full list */
2079 *h_errnop=HOST_NOT_FOUND;
2085 *h_errnop = NETDB_SUCCESS;
2086 return NETDB_SUCCESS;
2088 strong_alias(__gethostbyname_r,gethostbyname_r)
2091 #ifdef L_gethostbyname2_r
2093 int attribute_hidden __gethostbyname2_r(const char *name, int family,
2094 struct hostent * result_buf,
2095 char * buf, size_t buflen,
2096 struct hostent ** result,
2099 #ifndef __UCLIBC_HAS_IPV6__
2100 return family == (AF_INET)? __gethostbyname_r(name, result_buf,
2101 buf, buflen, result, h_errnop) : HOST_NOT_FOUND;
2102 #else /* __UCLIBC_HAS_IPV6__ */
2103 struct in6_addr *in;
2104 struct in6_addr **addr_list;
2105 unsigned char *packet;
2106 struct resolv_answer a;
2109 int __nameserversXX;
2110 char ** __nameserverXX;
2112 if (family == AF_INET)
2113 return __gethostbyname_r(name, result_buf, buf, buflen, result, h_errnop);
2115 if (family != AF_INET6)
2118 __open_nameservers();
2123 /* do /etc/hosts first */
2125 int old_errno = errno; /* Save the old errno and reset errno */
2126 __set_errno(0); /* to check for missing /etc/hosts. */
2128 if ((i=__get_hosts_byname_r(name, AF_INET, result_buf,
2129 buf, buflen, result, h_errnop))==0)
2131 switch (*h_errnop) {
2132 case HOST_NOT_FOUND:
2135 case NETDB_INTERNAL:
2136 if (errno == ENOENT) {
2139 /* else fall through */
2143 __set_errno(old_errno);
2146 DPRINTF("Nothing found in /etc/hosts\n");
2148 *h_errnop = NETDB_INTERNAL;
2149 if (buflen < sizeof(*in))
2151 in=(struct in6_addr*)buf;
2153 buflen-=sizeof(*in);
2155 if (buflen < sizeof(*addr_list)*2)
2157 addr_list=(struct in6_addr**)buf;
2158 buf+=sizeof(*addr_list)*2;
2159 buflen-=sizeof(*addr_list)*2;
2166 __strncpy(buf, name, buflen);
2168 /* First check if this is already an address */
2169 if (inet_pton(AF_INET6, name, in)) {
2170 result_buf->h_name = buf;
2171 result_buf->h_addrtype = AF_INET6;
2172 result_buf->h_length = sizeof(*in);
2173 result_buf->h_addr_list = (char **) addr_list;
2175 *h_errnop = NETDB_SUCCESS;
2176 return NETDB_SUCCESS;
2179 __memset((char *) &a, '\0', sizeof(a));
2183 __nameserversXX=__nameservers;
2184 __nameserverXX=__nameserver;
2187 i = __dns_lookup(buf, T_AAAA, __nameserversXX, __nameserverXX, &packet, &a);
2190 *h_errnop = HOST_NOT_FOUND;
2194 __strncpy(buf, a.dotted, buflen);
2197 if (a.atype == T_CNAME) { /* CNAME */
2198 DPRINTF("Got a CNAME in gethostbyname()\n");
2199 i = __decode_dotted(packet, a.rdoffset, buf, buflen);
2203 *h_errnop = NO_RECOVERY;
2206 if (++nest > MAX_RECURSE) {
2207 *h_errnop = NO_RECOVERY;
2211 } else if (a.atype == T_AAAA) { /* ADDRESS */
2212 __memcpy(in, a.rdata, sizeof(*in));
2213 result_buf->h_name = buf;
2214 result_buf->h_addrtype = AF_INET6;
2215 result_buf->h_length = sizeof(*in);
2216 result_buf->h_addr_list = (char **) addr_list;
2221 *h_errnop=HOST_NOT_FOUND;
2227 *h_errnop = NETDB_SUCCESS;
2228 return NETDB_SUCCESS;
2229 #endif /* __UCLIBC_HAS_IPV6__ */
2231 strong_alias(__gethostbyname2_r,gethostbyname2_r)
2234 #ifdef L_gethostbyaddr_r
2235 int attribute_hidden __gethostbyaddr_r (const void *addr, socklen_t len, int type,
2236 struct hostent * result_buf,
2237 char * buf, size_t buflen,
2238 struct hostent ** result,
2243 struct in_addr **addr_list;
2244 #ifdef __UCLIBC_HAS_IPV6__
2247 struct in6_addr *in6;
2248 struct in6_addr **addr_list6;
2249 #endif /* __UCLIBC_HAS_IPV6__ */
2250 unsigned char *packet;
2251 struct resolv_answer a;
2254 int __nameserversXX;
2255 char ** __nameserverXX;
2261 __memset((char *) &a, '\0', sizeof(a));
2265 if (len != sizeof(struct in_addr))
2268 #ifdef __UCLIBC_HAS_IPV6__
2270 if (len != sizeof(struct in6_addr))
2273 #endif /* __UCLIBC_HAS_IPV6__ */
2278 /* do /etc/hosts first */
2279 if ((i=__get_hosts_byaddr_r(addr, len, type, result_buf,
2280 buf, buflen, result, h_errnop))==0)
2282 switch (*h_errnop) {
2283 case HOST_NOT_FOUND:
2290 __open_nameservers();
2292 #ifdef __UCLIBC_HAS_IPV6__
2295 #endif /* __UCLIBC_HAS_IPV6__ */
2297 *h_errnop = NETDB_INTERNAL;
2298 if (buflen < sizeof(*in))
2300 in=(struct in_addr*)buf;
2302 buflen-=sizeof(*in);
2304 if (buflen < sizeof(*addr_list)*2)
2306 addr_list=(struct in_addr**)buf;
2307 buf+=sizeof(*addr_list)*2;
2308 buflen-=sizeof(*addr_list)*2;
2310 #ifdef __UCLIBC_HAS_IPV6__
2311 if (plen < sizeof(*in6))
2313 in6=(struct in6_addr*)qp;
2317 if (plen < sizeof(*addr_list6)*2)
2319 addr_list6=(struct in6_addr**)qp;
2320 qp+=sizeof(*addr_list6)*2;
2321 plen-=sizeof(*addr_list6)*2;
2323 if (plen < buflen) {
2327 #endif /* __UCLIBC_HAS_IPV6__ */
2332 if(type == AF_INET) {
2333 unsigned char *tmp_addr = (unsigned char *)addr;
2335 __memcpy(&in->s_addr, addr, len);
2339 __sprintf(buf, "%u.%u.%u.%u.in-addr.arpa",
2340 tmp_addr[3], tmp_addr[2], tmp_addr[1], tmp_addr[0]);
2341 #ifdef __UCLIBC_HAS_IPV6__
2343 __memcpy(in6->s6_addr, addr, len);
2345 addr_list6[0] = in6;
2348 for (i = len - 1; i >= 0; i--) {
2349 qp += __sprintf(qp, "%x.%x.", in6->s6_addr[i] & 0xf,
2350 (in6->s6_addr[i] >> 4) & 0xf);
2352 __strcpy(qp, "ip6.int");
2353 #endif /* __UCLIBC_HAS_IPV6__ */
2361 __nameserversXX=__nameservers;
2362 __nameserverXX=__nameserver;
2364 i = __dns_lookup(buf, T_PTR, __nameserversXX, __nameserverXX, &packet, &a);
2367 *h_errnop = HOST_NOT_FOUND;
2371 __strncpy(buf, a.dotted, buflen);
2374 if (a.atype == T_CNAME) { /* CNAME */
2375 DPRINTF("Got a CNAME in gethostbyaddr()\n");
2376 i = __decode_dotted(packet, a.rdoffset, buf, buflen);
2380 *h_errnop = NO_RECOVERY;
2383 if (++nest > MAX_RECURSE) {
2384 *h_errnop = NO_RECOVERY;
2388 } else if (a.atype == T_PTR) { /* ADDRESS */
2389 i = __decode_dotted(packet, a.rdoffset, buf, buflen);
2392 result_buf->h_name = buf;
2393 result_buf->h_addrtype = type;
2395 if(type == AF_INET) {
2396 result_buf->h_length = sizeof(*in);
2397 #ifdef __UCLIBC_HAS_IPV6__
2399 result_buf->h_length = sizeof(*in6);
2400 #endif /* __UCLIBC_HAS_IPV6__ */
2403 result_buf->h_addr_list = (char **) addr_list;
2407 *h_errnop = NO_ADDRESS;
2413 *h_errnop = NETDB_SUCCESS;
2414 return NETDB_SUCCESS;
2416 strong_alias(__gethostbyaddr_r,gethostbyaddr_r)
2421 * Expand compressed domain name 'comp_dn' to full domain name.
2422 * 'msg' is a pointer to the begining of the message,
2423 * 'eomorig' points to the first location after the message,
2424 * 'exp_dn' is a pointer to a buffer of size 'length' for the result.
2425 * Return size of compressed name or -1 if there was an error.
2427 int __dn_expand(const u_char *msg, const u_char *eom, const u_char *src,
2428 char *dst, int dstsiz)
2430 int n = __ns_name_uncompress_internal(msg, eom, src, dst, (size_t)dstsiz);
2432 if (n > 0 && dst[0] == '.')
2436 #endif /* L_res_comp */
2441 * Thinking in noninternationalized USASCII (per the DNS spec),
2442 * is this character visible and not a space when printed ?
2446 static int printable(int ch)
2448 return (ch > 0x20 && ch < 0x7f);
2453 * Thinking in noninternationalized USASCII (per the DNS spec),
2454 * is this characted special ("in need of quoting") ?
2458 static int special(int ch)
2461 case 0x22: /* '"' */
2462 case 0x2E: /* '.' */
2463 case 0x3B: /* ';' */
2464 case 0x5C: /* '\\' */
2465 /* Special modifiers in zone files. */
2466 case 0x40: /* '@' */
2467 case 0x24: /* '$' */
2475 * ns_name_uncompress(msg, eom, src, dst, dstsiz)
2476 * Expand compressed domain name to presentation format.
2478 * Number of bytes read out of `src', or -1 (with errno set).
2480 * Root domain returns as "." not "".
2482 int attribute_hidden __ns_name_uncompress_internal(const u_char *msg, const u_char *eom,
2483 const u_char *src, char *dst, size_t dstsiz)
2485 u_char tmp[NS_MAXCDNAME];
2488 if ((n = __ns_name_unpack_internal(msg, eom, src, tmp, sizeof tmp)) == -1)
2490 if (__ns_name_ntop_internal(tmp, dst, dstsiz) == -1)
2494 strong_alias(__ns_name_uncompress_internal,__ns_name_uncompress)
2498 * ns_name_ntop(src, dst, dstsiz)
2499 * Convert an encoded domain name to printable ascii as per RFC1035.
2501 * Number of bytes written to buffer, or -1 (with errno set)
2503 * The root is returned as "."
2504 * All other domains are returned in non absolute form
2506 int attribute_hidden __ns_name_ntop_internal(const u_char *src, char *dst, size_t dstsiz) {
2511 const char digits[] = "0123456789";
2517 while ((n = *cp++) != 0) {
2518 if ((n & NS_CMPRSFLGS) != 0) {
2519 /* Some kind of compression pointer. */
2520 __set_errno (EMSGSIZE);
2525 __set_errno (EMSGSIZE);
2530 if (dn + n >= eom) {
2531 __set_errno (EMSGSIZE);
2534 for ((void)NULL; n > 0; n--) {
2537 if (dn + 1 >= eom) {
2538 __set_errno (EMSGSIZE);
2543 } else if (!printable(c)) {
2544 if (dn + 3 >= eom) {
2545 __set_errno (EMSGSIZE);
2549 *dn++ = digits[c / 100];
2550 *dn++ = digits[(c % 100) / 10];
2551 *dn++ = digits[c % 10];
2554 __set_errno (EMSGSIZE);
2563 __set_errno (EMSGSIZE);
2569 __set_errno (EMSGSIZE);
2575 strong_alias(__ns_name_ntop_internal,__ns_name_ntop)
2578 * ns_name_unpack(msg, eom, src, dst, dstsiz)
2579 * Unpack a domain name from a message, source may be compressed.
2581 * -1 if it fails, or consumed octets if it succeeds.
2583 int attribute_hidden __ns_name_unpack_internal(const u_char *msg, const u_char *eom, const u_char *src,
2584 u_char *dst, size_t dstsiz)
2586 const u_char *srcp, *dstlim;
2588 int n, len, checked;
2594 dstlim = dst + dstsiz;
2595 if (srcp < msg || srcp >= eom) {
2596 __set_errno (EMSGSIZE);
2599 /* Fetch next label in domain name. */
2600 while ((n = *srcp++) != 0) {
2601 /* Check for indirection. */
2602 switch (n & NS_CMPRSFLGS) {
2605 if (dstp + n + 1 >= dstlim || srcp + n >= eom) {
2606 __set_errno (EMSGSIZE);
2611 __memcpy(dstp, srcp, n);
2618 __set_errno (EMSGSIZE);
2622 len = srcp - src + 1;
2623 srcp = msg + (((n & 0x3f) << 8) | (*srcp & 0xff));
2624 if (srcp < msg || srcp >= eom) { /* Out of range. */
2625 __set_errno (EMSGSIZE);
2630 * Check for loops in the compressed name;
2631 * if we've looked at the whole message,
2632 * there must be a loop.
2634 if (checked >= eom - msg) {
2635 __set_errno (EMSGSIZE);
2641 __set_errno (EMSGSIZE);
2642 return (-1); /* flag error */
2650 strong_alias(__ns_name_unpack_internal,__ns_name_unpack)
2651 #endif /* L_ns_name */