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 <andersee@debian.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
133 #define __FORCE_GLIBC
134 #include <features.h>
139 #include <sys/socket.h>
140 #include <sys/types.h>
141 #include <sys/time.h>
142 #include <netinet/in.h>
143 #include <arpa/inet.h>
149 #include <arpa/nameser.h>
150 #include <sys/utsname.h>
153 #define MAX_RECURSE 5
154 #define REPLY_TIMEOUT 10
155 #define MAX_RETRIES 3
156 #define MAX_SERVERS 3
159 #define MAX_ALIASES 5
161 /* 1:ip + 1:full + MAX_ALIASES:aliases + 1:NULL */
162 #define ALIAS_DIM (2 + MAX_ALIASES + 1)
168 #define DPRINTF(X,args...) fprintf(stderr, X, ##args)
170 #define DPRINTF(X,args...)
174 /* Global stuff (stuff needing to be locked to be thread safe)... */
175 extern int __nameservers;
176 extern char * __nameserver[MAX_SERVERS];
177 extern int __searchdomains;
178 extern char * __searchdomain[MAX_SEARCH];
180 #ifdef __UCLIBC_HAS_THREADS__
182 extern pthread_mutex_t __resolv_lock;
183 # define BIGLOCK __pthread_mutex_lock(&__resolv_lock)
184 # define BIGUNLOCK __pthread_mutex_unlock(&__resolv_lock);
193 struct resolv_header {
195 int qr,opcode,aa,tc,rd,ra,rcode;
202 struct resolv_question {
208 struct resolv_answer {
214 unsigned char * rdata;
218 enum etc_hosts_action {
219 GET_HOSTS_BYNAME = 0,
224 /* function prototypes */
225 extern int __get_hosts_byname_r(const char * name, int type,
226 struct hostent * result_buf,
227 char * buf, size_t buflen,
228 struct hostent ** result,
230 extern int __get_hosts_byaddr_r(const char * addr, int len, int type,
231 struct hostent * result_buf,
232 char * buf, size_t buflen,
233 struct hostent ** result,
235 extern void __open_etc_hosts(FILE **fp);
236 extern int __read_etc_hosts_r(FILE *fp, const char * name, int type,
237 enum etc_hosts_action action,
238 struct hostent * result_buf,
239 char * buf, size_t buflen,
240 struct hostent ** result,
242 extern int __dns_lookup(const char * name, int type, int nscount,
243 char ** nsip, unsigned char ** outpacket, struct resolv_answer * a);
245 extern int __encode_dotted(const char * dotted, unsigned char * dest, int maxlen);
246 extern int __decode_dotted(const unsigned char * message, int offset,
247 char * dest, int maxlen);
248 extern int __length_dotted(const unsigned char * message, int offset);
249 extern int __encode_header(struct resolv_header * h, unsigned char * dest, int maxlen);
250 extern int __decode_header(unsigned char * data, struct resolv_header * h);
251 extern int __encode_question(struct resolv_question * q,
252 unsigned char * dest, int maxlen);
253 extern int __decode_question(unsigned char * message, int offset,
254 struct resolv_question * q);
255 extern int __encode_answer(struct resolv_answer * a,
256 unsigned char * dest, int maxlen);
257 extern int __decode_answer(unsigned char * message, int offset,
258 struct resolv_answer * a);
259 extern int __length_question(unsigned char * message, int offset);
260 extern int __open_nameservers(void);
261 extern void __close_nameservers(void);
262 extern int __dn_expand(const u_char *, const u_char *, const u_char *,
264 extern int __ns_name_uncompress(const u_char *, const u_char *,
265 const u_char *, char *, size_t);
266 extern int __ns_name_ntop(const u_char *, char *, size_t);
267 extern int __ns_name_unpack(const u_char *, const u_char *, const u_char *,
272 int __encode_header(struct resolv_header *h, unsigned char *dest, int maxlen)
274 if (maxlen < HFIXEDSZ)
277 dest[0] = (h->id & 0xff00) >> 8;
278 dest[1] = (h->id & 0x00ff) >> 0;
279 dest[2] = (h->qr ? 0x80 : 0) |
280 ((h->opcode & 0x0f) << 3) |
284 dest[3] = (h->ra ? 0x80 : 0) | (h->rcode & 0x0f);
285 dest[4] = (h->qdcount & 0xff00) >> 8;
286 dest[5] = (h->qdcount & 0x00ff) >> 0;
287 dest[6] = (h->ancount & 0xff00) >> 8;
288 dest[7] = (h->ancount & 0x00ff) >> 0;
289 dest[8] = (h->nscount & 0xff00) >> 8;
290 dest[9] = (h->nscount & 0x00ff) >> 0;
291 dest[10] = (h->arcount & 0xff00) >> 8;
292 dest[11] = (h->arcount & 0x00ff) >> 0;
299 int __decode_header(unsigned char *data, struct resolv_header *h)
301 h->id = (data[0] << 8) | data[1];
302 h->qr = (data[2] & 0x80) ? 1 : 0;
303 h->opcode = (data[2] >> 3) & 0x0f;
304 h->aa = (data[2] & 0x04) ? 1 : 0;
305 h->tc = (data[2] & 0x02) ? 1 : 0;
306 h->rd = (data[2] & 0x01) ? 1 : 0;
307 h->ra = (data[3] & 0x80) ? 1 : 0;
308 h->rcode = data[3] & 0x0f;
309 h->qdcount = (data[4] << 8) | data[5];
310 h->ancount = (data[6] << 8) | data[7];
311 h->nscount = (data[8] << 8) | data[9];
312 h->arcount = (data[10] << 8) | data[11];
319 /* Encode a dotted string into nameserver transport-level encoding.
320 This routine is fairly dumb, and doesn't attempt to compress
323 int __encode_dotted(const char *dotted, unsigned char *dest, int maxlen)
327 while (dotted && *dotted) {
328 char *c = strchr(dotted, '.');
329 int l = c ? c - dotted : strlen(dotted);
331 if (l >= (maxlen - used - 1))
335 memcpy(dest + used, dotted, l);
354 /* Decode a dotted string from nameserver transport-level encoding.
355 This routine understands compressed data. */
357 int __decode_dotted(const unsigned char *data, int offset,
358 char *dest, int maxlen)
368 while ((l=data[offset++])) {
371 if ((l & 0xc0) == (0xc0)) {
374 /* compressed item, redirect */
375 offset = ((l & 0x3f) << 8) | data[offset];
380 if ((used + l + 1) >= maxlen)
383 memcpy(dest + used, data + offset, l);
389 if (data[offset] != 0)
395 /* The null byte must be counted too */
400 DPRINTF("Total decode len = %d\n", total);
408 int __length_dotted(const unsigned char *data, int offset)
410 int orig_offset = offset;
416 while ((l = data[offset++])) {
418 if ((l & 0xc0) == (0xc0)) {
426 return offset - orig_offset;
431 int __encode_question(struct resolv_question *q,
432 unsigned char *dest, int maxlen)
436 i = __encode_dotted(q->dotted, dest, maxlen);
446 dest[0] = (q->qtype & 0xff00) >> 8;
447 dest[1] = (q->qtype & 0x00ff) >> 0;
448 dest[2] = (q->qclass & 0xff00) >> 8;
449 dest[3] = (q->qclass & 0x00ff) >> 0;
456 int __decode_question(unsigned char *message, int offset,
457 struct resolv_question *q)
462 i = __decode_dotted(message, offset, temp, sizeof(temp));
468 q->dotted = strdup(temp);
469 q->qtype = (message[offset + 0] << 8) | message[offset + 1];
470 q->qclass = (message[offset + 2] << 8) | message[offset + 3];
477 int __length_question(unsigned char *message, int offset)
481 i = __length_dotted(message, offset);
490 int __encode_answer(struct resolv_answer *a, unsigned char *dest, int maxlen)
494 i = __encode_dotted(a->dotted, dest, maxlen);
501 if (maxlen < (RRFIXEDSZ+a->rdlength))
504 *dest++ = (a->atype & 0xff00) >> 8;
505 *dest++ = (a->atype & 0x00ff) >> 0;
506 *dest++ = (a->aclass & 0xff00) >> 8;
507 *dest++ = (a->aclass & 0x00ff) >> 0;
508 *dest++ = (a->ttl & 0xff000000) >> 24;
509 *dest++ = (a->ttl & 0x00ff0000) >> 16;
510 *dest++ = (a->ttl & 0x0000ff00) >> 8;
511 *dest++ = (a->ttl & 0x000000ff) >> 0;
512 *dest++ = (a->rdlength & 0xff00) >> 8;
513 *dest++ = (a->rdlength & 0x00ff) >> 0;
514 memcpy(dest, a->rdata, a->rdlength);
516 return i + RRFIXEDSZ + a->rdlength;
521 int __decode_answer(unsigned char *message, int offset,
522 struct resolv_answer *a)
527 i = __decode_dotted(message, offset, temp, sizeof(temp));
531 message += offset + i;
533 a->dotted = strdup(temp);
534 a->atype = (message[0] << 8) | message[1];
536 a->aclass = (message[0] << 8) | message[1];
538 a->ttl = (message[0] << 24) |
539 (message[1] << 16) | (message[2] << 8) | (message[3] << 0);
541 a->rdlength = (message[0] << 8) | message[1];
544 a->rdoffset = offset + i + RRFIXEDSZ;
546 DPRINTF("i=%d,rdlength=%d\n", i, a->rdlength);
548 return i + RRFIXEDSZ + a->rdlength;
553 int __encode_packet(struct resolv_header *h,
554 struct resolv_question **q,
555 struct resolv_answer **an,
556 struct resolv_answer **ns,
557 struct resolv_answer **ar,
558 unsigned char *dest, int maxlen)
563 i = __encode_header(h, dest, maxlen);
571 for (j = 0; j < h->qdcount; j++) {
572 i = __encode_question(q[j], dest, maxlen);
580 for (j = 0; j < h->ancount; j++) {
581 i = __encode_answer(an[j], dest, maxlen);
588 for (j = 0; j < h->nscount; j++) {
589 i = __encode_answer(ns[j], dest, maxlen);
596 for (j = 0; j < h->arcount; j++) {
597 i = __encode_answer(ar[j], dest, maxlen);
610 int __decode_packet(unsigned char *data, struct resolv_header *h)
612 return __decode_header(data, h);
617 int __form_query(int id, const char *name, int type, unsigned char *packet,
620 struct resolv_header h;
621 struct resolv_question q;
624 memset(&h, 0, sizeof(h));
628 q.dotted = (char *) name;
630 q.qclass = C_IN; /* CLASS_IN */
632 i = __encode_header(&h, packet, maxlen);
636 j = __encode_question(&q, packet + i, maxlen - i);
646 #ifdef __UCLIBC_HAS_THREADS__
647 static pthread_mutex_t mylock = PTHREAD_MUTEX_INITIALIZER;
648 # define LOCK __pthread_mutex_lock(&mylock)
649 # define UNLOCK __pthread_mutex_unlock(&mylock);
655 /* Just for the record, having to lock __dns_lookup() just for these two globals
656 * is pretty lame. I think these two variables can probably be de-global-ized,
657 * which should eliminate the need for doing locking here... Needs a closer
659 static int ns=0, id=1;
661 int __dns_lookup(const char *name, int type, int nscount, char **nsip,
662 unsigned char **outpacket, struct resolv_answer *a)
664 int i, j, len, fd, pos, rc;
667 struct resolv_header h;
668 struct resolv_question q;
670 unsigned char * packet = malloc(PACKETSZ);
671 char *dns, *lookup = malloc(MAXDNAME);
673 struct sockaddr_in sa;
674 #ifdef __UCLIBC_HAS_IPV6__
676 struct sockaddr_in6 sa6;
681 if (!packet || !lookup || !nscount)
684 DPRINTF("Looking up type %d answer for '%s'\n", type, name);
690 while (retries < MAX_RETRIES) {
694 memset(packet, 0, PACKETSZ);
696 memset(&h, 0, sizeof(h));
698 /* Mess with globals while under lock */
709 DPRINTF("encoding header\n", h.rd);
711 i = __encode_header(&h, packet, PACKETSZ);
715 strncpy(lookup,name,MAXDNAME);
717 if (variant < __searchdomains && strchr(lookup, '.') == NULL)
719 strncat(lookup,".", MAXDNAME);
720 strncat(lookup,__searchdomain[variant], MAXDNAME);
723 DPRINTF("lookup name: %s\n", lookup);
724 q.dotted = (char *)lookup;
726 q.qclass = C_IN; /* CLASS_IN */
728 j = __encode_question(&q, packet+i, PACKETSZ-i);
734 DPRINTF("On try %d, sending query to port %d of machine %s\n",
735 retries+1, NAMESERVER_PORT, dns);
737 #ifdef __UCLIBC_HAS_IPV6__
738 v6 = inet_pton(AF_INET6, dns, &sa6.sin6_addr) > 0;
739 fd = socket(v6 ? AF_INET6 : AF_INET, SOCK_DGRAM, IPPROTO_UDP);
741 fd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
748 /* Connect to the UDP socket so that asyncronous errors are returned */
749 #ifdef __UCLIBC_HAS_IPV6__
751 sa6.sin6_family = AF_INET6;
752 sa6.sin6_port = htons(NAMESERVER_PORT);
753 /* sa6.sin6_addr is already here */
754 rc = connect(fd, (struct sockaddr *) &sa6, sizeof(sa6));
757 sa.sin_family = AF_INET;
758 sa.sin_port = htons(NAMESERVER_PORT);
759 sa.sin_addr.s_addr = inet_addr(dns);
760 rc = connect(fd, (struct sockaddr *) &sa, sizeof(sa));
761 #ifdef __UCLIBC_HAS_IPV6__
765 if (errno == ENETUNREACH) {
766 /* routing error, presume not transient */
774 DPRINTF("Transmitting packet of length %d, id=%d, qr=%d\n",
777 send(fd, packet, len, 0);
781 tv.tv_sec = REPLY_TIMEOUT;
783 if (select(fd + 1, &fds, NULL, NULL, &tv) <= 0) {
784 DPRINTF("Timeout\n");
786 /* timed out, so retry send and receive,
787 * to next nameserver on queue */
791 len = recv(fd, packet, 512, 0);
792 if (len < HFIXEDSZ) {
797 __decode_header(packet, &h);
799 DPRINTF("id = %d, qr = %d\n", h.id, h.qr);
802 if ((h.id != id) || (!h.qr)) {
810 DPRINTF("Got response %s\n", "(i think)!");
811 DPRINTF("qrcount=%d,ancount=%d,nscount=%d,arcount=%d\n",
812 h.qdcount, h.ancount, h.nscount, h.arcount);
813 DPRINTF("opcode=%d,aa=%d,tc=%d,rd=%d,ra=%d,rcode=%d\n",
814 h.opcode, h.aa, h.tc, h.rd, h.ra, h.rcode);
816 if ((h.rcode) || (h.ancount < 1)) {
817 /* negative result, not present */
823 for (j = 0; j < h.qdcount; j++) {
824 DPRINTF("Skipping question %d at %d\n", j, pos);
825 i = __length_question(packet, pos);
826 DPRINTF("Length of question %d is %d\n", j, i);
831 DPRINTF("Decoding answer at pos %d\n", pos);
833 for (j=0;j<h.ancount;j++)
835 i = __decode_answer(packet, pos, a);
838 DPRINTF("failed decode %d\n", i);
841 /* For all but T_SIG, accept first answer */
842 if (a->atype != T_SIG)
845 DPRINTF("skipping T_SIG %d\n", i);
850 DPRINTF("Answer name = |%s|\n", a->dotted);
851 DPRINTF("Answer type = |%d|\n", a->atype);
860 return (len); /* success! */
863 /* if there are other nameservers, give them a go,
864 otherwise return with error */
868 ns = (ns + 1) % nscount;
877 /* if there are searchdomains, try them or fallback as passed */
881 sdomains=__searchdomains;
884 if (variant < ((sdomains - 1) && strchr(lookup, '.') == NULL)) {
888 /* next server, first search */
890 ns = (ns + 1) % nscount;
907 h_errno = NETDB_INTERNAL;
912 #ifdef L_opennameservers
915 char * __nameserver[MAX_SERVERS];
917 char * __searchdomain[MAX_SEARCH];
918 #ifdef __UCLIBC_HAS_THREADS__
919 pthread_mutex_t __resolv_lock = PTHREAD_MUTEX_INITIALIZER;
923 * we currently read formats not quite the same as that on normal
924 * unix systems, we can have a list of nameservers after the keyword.
927 int __open_nameservers()
931 #define RESOLV_ARGS 5
932 char szBuffer[128], *p, *argv[RESOLV_ARGS];
936 if (__nameservers > 0) {
941 if ((fp = fopen("/etc/resolv.conf", "r")) ||
942 (fp = fopen("/etc/config/resolv.conf", "r")))
945 while (fgets(szBuffer, sizeof(szBuffer), fp) != NULL) {
947 for (p = szBuffer; *p && isspace(*p); p++)
948 /* skip white space */;
949 if (*p == '\0' || *p == '\n' || *p == '#') /* skip comments etc */
952 while (*p && argc < RESOLV_ARGS) {
954 while (*p && !isspace(*p) && *p != '\n')
956 while (*p && (isspace(*p) || *p == '\n')) /* remove spaces */
960 if (strcmp(argv[0], "nameserver") == 0) {
961 for (i = 1; i < argc && __nameservers < MAX_SERVERS; i++) {
962 __nameserver[__nameservers++] = strdup(argv[i]);
963 DPRINTF("adding nameserver %s\n", argv[i]);
967 /* domain and search are mutually exclusive, the last one wins */
968 if (strcmp(argv[0],"domain")==0 || strcmp(argv[0],"search")==0) {
969 while (__searchdomains > 0) {
970 free(__searchdomain[--__searchdomains]);
971 __searchdomain[__searchdomains] = NULL;
973 for (i=1; i < argc && __searchdomains < MAX_SEARCH; i++) {
974 __searchdomain[__searchdomains++] = strdup(argv[i]);
975 DPRINTF("adding search %s\n", argv[i]);
980 DPRINTF("nameservers = %d\n", __nameservers);
984 DPRINTF("failed to open %s\n", "resolv.conf");
985 h_errno = NO_RECOVERY;
992 #ifdef L_closenameservers
994 void __close_nameservers(void)
997 while (__nameservers > 0) {
998 free(__nameserver[--__nameservers]);
999 __nameserver[__nameservers] = NULL;
1001 while (__searchdomains > 0) {
1002 free(__searchdomain[--__searchdomains]);
1003 __searchdomain[__searchdomains] = NULL;
1009 #ifdef L_gethostbyname
1011 struct hostent *gethostbyname(const char *name)
1013 static struct hostent h;
1014 static char buf[sizeof(struct in_addr) +
1015 sizeof(struct in_addr *)*2 +
1016 sizeof(char *)*(ALIAS_DIM) + 256/*namebuffer*/ + 32/* margin */];
1019 gethostbyname_r(name, &h, buf, sizeof(buf), &hp, &h_errno);
1025 #ifdef L_gethostbyname2
1027 struct hostent *gethostbyname2(const char *name, int family)
1029 #ifndef __UCLIBC_HAS_IPV6__
1030 return family == AF_INET ? gethostbyname(name) : (struct hostent*)0;
1031 #else /* __UCLIBC_HAS_IPV6__ */
1032 static struct hostent h;
1033 static char buf[sizeof(struct in6_addr) +
1034 sizeof(struct in6_addr *)*2 +
1035 sizeof(char *)*(ALIAS_DIM) + 256/*namebuffer*/ + 32/* margin */];
1038 gethostbyname2_r(name, family, &h, buf, sizeof(buf), &hp, &h_errno);
1041 #endif /* __UCLIBC_HAS_IPV6__ */
1045 #ifdef L_getnetbyname
1047 struct netent * getnetbyname(const char * name)
1055 struct __res_state _res;
1059 struct __res_state *rp = &(_res);
1061 __close_nameservers();
1062 if (__open_nameservers()) {
1065 rp->retrans = RES_TIMEOUT;
1067 rp->options = RES_INIT;
1068 rp->id = (u_int) random();
1069 rp->nsaddr.sin_addr.s_addr = INADDR_ANY;
1070 rp->nsaddr.sin_family = AF_INET;
1071 rp->nsaddr.sin_port = htons(NAMESERVER_PORT);
1073 /** rp->pfcode = 0; **/
1075 /** rp->_flags = 0; **/
1076 /** rp->qhook = NULL; **/
1077 /** rp->rhook = NULL; **/
1078 /** rp->_u._ext.nsinit = 0; **/
1081 if(__searchdomains) {
1083 for(i=0; i<__searchdomains; i++) {
1084 rp->dnsrch[i] = __searchdomain[i];
1091 for(i=0; i<__nameservers; i++) {
1092 if (inet_aton(__nameserver[i], &a)) {
1093 rp->nsaddr_list[i].sin_addr = a;
1094 rp->nsaddr_list[i].sin_family = AF_INET;
1095 rp->nsaddr_list[i].sin_port = htons(NAMESERVER_PORT);
1099 rp->nscount = __nameservers;
1105 void res_close( void )
1116 #define MIN(x, y) ((x) < (y) ? (x) : (y))
1119 int res_query(const char *dname, int class, int type,
1120 unsigned char *answer, int anslen)
1123 unsigned char * packet = 0;
1124 struct resolv_answer a;
1125 int __nameserversXX;
1126 char ** __nameserverXX;
1128 if (__open_nameservers() || !dname || class != 1 /* CLASS_IN */) {
1129 h_errno = NO_RECOVERY;
1133 memset((char *) &a, '\0', sizeof(a));
1136 __nameserversXX=__nameservers;
1137 __nameserverXX=__nameserver;
1139 i = __dns_lookup(dname, type, __nameserversXX, __nameserverXX, &packet, &a);
1142 h_errno = TRY_AGAIN;
1148 if (a.atype == type) { /* CNAME*/
1149 int len = MIN(anslen, i);
1150 memcpy(answer, packet, len);
1161 * Formulate a normal query, send, and retrieve answer in supplied buffer.
1162 * Return the size of the response on success, -1 on error.
1163 * If enabled, implement search rules until answer or unrecoverable failure
1164 * is detected. Error code, if any, is left in h_errno.
1166 int res_search(name, class, type, answer, anslen)
1167 const char *name; /* domain name */
1168 int class, type; /* class and type of query */
1169 u_char *answer; /* buffer to put answer */
1170 int anslen; /* size of answer */
1172 const char *cp, * const *domain;
1173 HEADER *hp = (HEADER *)(void *)answer;
1175 int trailing_dot, ret, saved_herrno;
1176 int got_nodata = 0, got_servfail = 0, tried_as_is = 0;
1178 if ((!name || !answer) || ((_res.options & RES_INIT) == 0 && res_init() == -1)) {
1179 h_errno = NETDB_INTERNAL;
1184 h_errno = HOST_NOT_FOUND; /* default, if we never query */
1186 for (cp = name; *cp; cp++)
1187 dots += (*cp == '.');
1189 if (cp > name && *--cp == '.')
1193 * If there are dots in the name already, let's just give it a try
1194 * 'as is'. The threshold can be set with the "ndots" option.
1197 if (dots >= _res.ndots) {
1198 ret = res_querydomain(name, NULL, class, type, answer, anslen);
1201 saved_herrno = h_errno;
1206 * We do at least one level of search if
1207 * - there is no dot and RES_DEFNAME is set, or
1208 * - there is at least one dot, there is no trailing dot,
1209 * and RES_DNSRCH is set.
1211 if ((!dots && (_res.options & RES_DEFNAMES)) ||
1212 (dots && !trailing_dot && (_res.options & RES_DNSRCH))) {
1215 for (domain = (const char * const *)_res.dnsrch;
1219 ret = res_querydomain(name, *domain, class, type,
1225 * If no server present, give up.
1226 * If name isn't found in this domain,
1227 * keep trying higher domains in the search list
1228 * (if that's enabled).
1229 * On a NO_DATA error, keep trying, otherwise
1230 * a wildcard entry of another type could keep us
1231 * from finding this entry higher in the domain.
1232 * If we get some other error (negative answer or
1233 * server failure), then stop searching up,
1234 * but try the input name below in case it's
1237 if (errno == ECONNREFUSED) {
1238 h_errno = TRY_AGAIN;
1246 case HOST_NOT_FOUND:
1250 if (hp->rcode == SERVFAIL) {
1251 /* try next search element, if any */
1257 /* anything else implies that we're done */
1261 * if we got here for some reason other than DNSRCH,
1262 * we only wanted one iteration of the loop, so stop.
1264 if (!(_res.options & RES_DNSRCH))
1270 * if we have not already tried the name "as is", do that now.
1271 * note that we do this regardless of how many dots were in the
1272 * name or whether it ends with a dot.
1275 ret = res_querydomain(name, NULL, class, type, answer, anslen);
1281 * if we got here, we didn't satisfy the search.
1282 * if we did an initial full query, return that query's h_errno
1283 * (note that we wouldn't be here if that query had succeeded).
1284 * else if we ever got a nodata, send that back as the reason.
1285 * else send back meaningless h_errno, that being the one from
1286 * the last DNSRCH we did.
1288 if (saved_herrno != -1)
1289 h_errno = saved_herrno;
1290 else if (got_nodata)
1292 else if (got_servfail)
1293 h_errno = TRY_AGAIN;
1298 * Perform a call on res_query on the concatenation of name and domain,
1299 * removing a trailing dot from name if domain is NULL.
1301 int res_querydomain(name, domain, class, type, answer, anslen)
1302 const char *name, *domain;
1303 int class, type; /* class and type of query */
1304 u_char *answer; /* buffer to put answer */
1305 int anslen; /* size of answer */
1307 char nbuf[MAXDNAME];
1308 const char *longname = nbuf;
1311 if ((!name || !answer) || ((_res.options & RES_INIT) == 0 && res_init() == -1)) {
1312 h_errno = NETDB_INTERNAL;
1317 if (_res.options & RES_DEBUG)
1318 printf(";; res_querydomain(%s, %s, %d, %d)\n",
1319 name, domain?domain:"<Nil>", class, type);
1321 if (domain == NULL) {
1323 * Check for trailing '.';
1324 * copy without '.' if present.
1327 if (n + 1 > sizeof(nbuf)) {
1328 h_errno = NO_RECOVERY;
1331 if (n > 0 && name[--n] == '.') {
1332 strncpy(nbuf, name, n);
1339 if (n + 1 + d + 1 > sizeof(nbuf)) {
1340 h_errno = NO_RECOVERY;
1343 snprintf(nbuf, sizeof(nbuf), "%s.%s", name, domain);
1345 return (res_query(longname, class, type, answer, anslen));
1354 #ifdef L_gethostbyaddr
1355 struct hostent *gethostbyaddr (const void *addr, socklen_t len, int type)
1357 static struct hostent h;
1359 #ifndef __UCLIBC_HAS_IPV6__
1360 sizeof(struct in_addr) + sizeof(struct in_addr *)*2 +
1362 sizeof(struct in6_addr) + sizeof(struct in6_addr *)*2 +
1363 #endif /* __UCLIBC_HAS_IPV6__ */
1364 sizeof(char *)*(ALIAS_DIM) + 256/*namebuffer*/ + 32/* margin */];
1367 gethostbyaddr_r(addr, len, type, &h, buf, sizeof(buf), &hp, &h_errno);
1374 #ifdef L_read_etc_hosts_r
1376 void __open_etc_hosts(FILE **fp)
1378 if ((*fp = fopen("/etc/hosts", "r")) == NULL) {
1379 *fp = fopen("/etc/config/hosts", "r");
1384 int __read_etc_hosts_r(FILE * fp, const char * name, int type,
1385 enum etc_hosts_action action,
1386 struct hostent * result_buf,
1387 char * buf, size_t buflen,
1388 struct hostent ** result,
1391 struct in_addr *in=NULL;
1392 struct in_addr **addr_list=NULL;
1393 #ifdef __UCLIBC_HAS_IPV6__
1394 struct in6_addr *in6=NULL;
1395 struct in6_addr **addr_list6=NULL;
1396 #endif /* __UCLIBC_HAS_IPV6__ */
1400 int ret=HOST_NOT_FOUND;
1402 if (buflen < sizeof(char *)*(ALIAS_DIM))
1405 buf+=sizeof(char **)*(ALIAS_DIM);
1406 buflen-=sizeof(char **)*(ALIAS_DIM);
1408 if (action!=GETHOSTENT) {
1409 #ifdef __UCLIBC_HAS_IPV6__
1412 #endif /* __UCLIBC_HAS_IPV6__ */
1413 *h_errnop=NETDB_INTERNAL;
1414 if (buflen < sizeof(*in))
1416 in=(struct in_addr*)buf;
1418 buflen-=sizeof(*in);
1420 if (buflen < sizeof(*addr_list)*2)
1422 addr_list=(struct in_addr **)buf;
1423 buf+=sizeof(*addr_list)*2;
1424 buflen-=sizeof(*addr_list)*2;
1426 #ifdef __UCLIBC_HAS_IPV6__
1427 if (len < sizeof(*in6))
1429 in6=(struct in6_addr*)p;
1433 if (len < sizeof(*addr_list6)*2)
1435 addr_list6=(struct in6_addr**)p;
1436 p+=sizeof(*addr_list6)*2;
1437 len-=sizeof(*addr_list6)*2;
1443 #endif /* __UCLIBC_HAS_IPV6__ */
1448 __open_etc_hosts(&fp);
1455 *h_errnop=HOST_NOT_FOUND;
1456 while (fgets(buf, buflen, fp)) {
1457 if ((cp = strchr(buf, '#')))
1459 DPRINTF("Looking at: %s\n", buf);
1464 while (*cp && isspace(*cp))
1468 if (aliases < (2+MAX_ALIASES))
1469 alias[aliases++] = cp;
1470 while (*cp && !isspace(*cp))
1476 continue; /* syntax error really */
1478 if (action==GETHOSTENT) {
1479 /* Return whatever the next entry happens to be. */
1481 } else if (action==GET_HOSTS_BYADDR) {
1482 if (strcmp(name, alias[0]) != 0)
1485 /* GET_HOSTS_BYNAME */
1486 for (i = 1; i < aliases; i++)
1487 if (strcasecmp(name, alias[i]) == 0)
1493 if (type == AF_INET && inet_pton(AF_INET, alias[0], in) > 0) {
1494 DPRINTF("Found INET\n");
1497 result_buf->h_name = alias[1];
1498 result_buf->h_addrtype = AF_INET;
1499 result_buf->h_length = sizeof(*in);
1500 result_buf->h_addr_list = (char**) addr_list;
1501 result_buf->h_aliases = alias + 2;
1504 #ifdef __UCLIBC_HAS_IPV6__
1505 } else if (type == AF_INET6 && inet_pton(AF_INET6, alias[0], in6) > 0) {
1506 DPRINTF("Found INET6\n");
1507 addr_list6[0] = in6;
1509 result_buf->h_name = alias[1];
1510 result_buf->h_addrtype = AF_INET6;
1511 result_buf->h_length = sizeof(*in6);
1512 result_buf->h_addr_list = (char**) addr_list6;
1513 result_buf->h_aliases = alias + 2;
1516 #endif /* __UCLIBC_HAS_IPV6__ */
1520 break; /* bad ip address */
1523 if (action!=GETHOSTENT) {
1528 if (action!=GETHOSTENT) {
1538 #ifdef __UCLIBC_HAS_THREADS__
1539 static pthread_mutex_t mylock = PTHREAD_MUTEX_INITIALIZER;
1540 # define LOCK __pthread_mutex_lock(&mylock)
1541 # define UNLOCK __pthread_mutex_unlock(&mylock);
1547 static int __stay_open;
1548 static FILE * __gethostent_fp;
1550 void endhostent (void)
1554 if (__gethostent_fp) {
1555 fclose(__gethostent_fp);
1560 void sethostent (int stay_open)
1563 __stay_open = stay_open;
1568 struct hostent *gethostent (void)
1570 static struct hostent h;
1572 #ifndef __UCLIBC_HAS_IPV6__
1573 sizeof(struct in_addr) + sizeof(struct in_addr *)*2 +
1575 sizeof(struct in6_addr) + sizeof(struct in6_addr *)*2 +
1576 #endif /* __UCLIBC_HAS_IPV6__ */
1577 sizeof(char *)*(ALIAS_DIM) +
1578 80/*namebuffer*/ + 2/* margin */];
1579 struct hostent *host;
1582 if (__gethostent_fp == NULL) {
1583 __open_etc_hosts(&__gethostent_fp);
1584 if (__gethostent_fp == NULL) {
1586 return((struct hostent *)NULL);
1590 __read_etc_hosts_r(__gethostent_fp, NULL, AF_INET, GETHOSTENT,
1591 &h, buf, sizeof(buf), &host, &h_errno);
1592 if (__stay_open==0) {
1593 fclose(__gethostent_fp);
1600 #ifdef L_get_hosts_byname_r
1602 int __get_hosts_byname_r(const char * name, int type,
1603 struct hostent * result_buf,
1604 char * buf, size_t buflen,
1605 struct hostent ** result,
1608 return(__read_etc_hosts_r(NULL, name, type, GET_HOSTS_BYNAME, result_buf, buf, buflen, result, h_errnop));
1612 #ifdef L_get_hosts_byaddr_r
1614 int __get_hosts_byaddr_r(const char * addr, int len, int type,
1615 struct hostent * result_buf,
1616 char * buf, size_t buflen,
1617 struct hostent ** result,
1620 #ifndef __UCLIBC_HAS_IPV6__
1621 char ipaddr[INET_ADDRSTRLEN];
1623 char ipaddr[INET6_ADDRSTRLEN];
1624 #endif /* __UCLIBC_HAS_IPV6__ */
1628 if (len != sizeof(struct in_addr))
1631 #ifdef __UCLIBC_HAS_IPV6__
1633 if (len != sizeof(struct in6_addr))
1636 #endif /* __UCLIBC_HAS_IPV6__ */
1641 inet_ntop(type, addr, ipaddr, sizeof(ipaddr));
1643 return(__read_etc_hosts_r(NULL, ipaddr, type, GET_HOSTS_BYADDR,
1644 result_buf, buf, buflen, result, h_errnop));
1648 #ifdef L_getnameinfo
1651 # define min(x,y) (((x) > (y)) ? (y) : (x))
1654 int getnameinfo (const struct sockaddr *sa, socklen_t addrlen, char *host,
1655 socklen_t hostlen, char *serv, socklen_t servlen,
1660 struct hostent *h = NULL;
1663 if (flags & ~(NI_NUMERICHOST|NI_NUMERICSERV|NI_NOFQDN|NI_NAMEREQD|NI_DGRAM))
1664 return EAI_BADFLAGS;
1666 if (sa == NULL || addrlen < sizeof (sa_family_t))
1669 switch (sa->sa_family) {
1673 if (addrlen < sizeof (struct sockaddr_in))
1676 #ifdef __UCLIBC_HAS_IPV6__
1678 if (addrlen < sizeof (struct sockaddr_in6))
1681 #endif /* __UCLIBC_HAS_IPV6__ */
1686 if (host != NULL && hostlen > 0)
1687 switch (sa->sa_family) {
1689 #ifdef __UCLIBC_HAS_IPV6__
1691 #endif /* __UCLIBC_HAS_IPV6__ */
1692 if (!(flags & NI_NUMERICHOST)) {
1693 #ifdef __UCLIBC_HAS_IPV6__
1694 if (sa->sa_family == AF_INET6)
1695 h = gethostbyaddr ((const void *) &(((const struct sockaddr_in6 *) sa)->sin6_addr),
1696 sizeof(struct in6_addr), AF_INET6);
1698 #endif /* __UCLIBC_HAS_IPV6__ */
1699 h = gethostbyaddr ((const void *) &(((const struct sockaddr_in *)sa)->sin_addr),
1700 sizeof(struct in_addr), AF_INET);
1704 if ((flags & NI_NOFQDN)
1705 && (getdomainname (domain, sizeof(domain)) == 0)
1706 && (c = strstr (h->h_name, domain))
1707 && (c != h->h_name) && (*(--c) == '.')) {
1708 strncpy (host, h->h_name,
1709 min(hostlen, (size_t) (c - h->h_name)));
1710 host[min(hostlen - 1, (size_t) (c - h->h_name))] = '\0';
1713 strncpy (host, h->h_name, hostlen);
1720 if (flags & NI_NAMEREQD) {
1725 #ifdef __UCLIBC_HAS_IPV6__
1726 if (sa->sa_family == AF_INET6) {
1727 const struct sockaddr_in6 *sin6p;
1729 sin6p = (const struct sockaddr_in6 *) sa;
1731 c = inet_ntop (AF_INET6,
1732 (const void *) &sin6p->sin6_addr, host, hostlen);
1734 /* Does scope id need to be supported? */
1736 scopeid = sin6p->sin6_scope_id;
1738 /* Buffer is >= IFNAMSIZ+1. */
1739 char scopebuf[IFNAMSIZ + 1];
1741 int ni_numericscope = 0;
1742 size_t real_hostlen = __strnlen (host, hostlen);
1743 size_t scopelen = 0;
1745 scopebuf[0] = SCOPE_DELIMITER;
1747 scopeptr = &scopebuf[1];
1749 if (IN6_IS_ADDR_LINKLOCAL (&sin6p->sin6_addr)
1750 || IN6_IS_ADDR_MC_LINKLOCAL (&sin6p->sin6_addr)) {
1751 if (if_indextoname (scopeid, scopeptr) == NULL)
1754 scopelen = strlen (scopebuf);
1759 if (ni_numericscope)
1760 scopelen = 1 + snprintf (scopeptr,
1766 if (real_hostlen + scopelen + 1 > hostlen)
1768 memcpy (host + real_hostlen, scopebuf, scopelen + 1);
1772 #endif /* __UCLIBC_HAS_IPV6__ */
1773 c = inet_ntop (AF_INET,
1774 (const void *) &(((const struct sockaddr_in *) sa)->sin_addr),
1787 if (!(flags & NI_NUMERICHOST)) {
1788 struct utsname utsname;
1790 if (!uname (&utsname)) {
1791 strncpy (host, utsname.nodename, hostlen);
1796 if (flags & NI_NAMEREQD) {
1801 strncpy (host, "localhost", hostlen);
1808 if (serv && (servlen > 0)) {
1809 switch (sa->sa_family) {
1811 #ifdef __UCLIBC_HAS_IPV6__
1813 #endif /* __UCLIBC_HAS_IPV6__ */
1814 if (!(flags & NI_NUMERICSERV)) {
1816 s = getservbyport (((const struct sockaddr_in *) sa)->sin_port,
1817 ((flags & NI_DGRAM) ? "udp" : "tcp"));
1819 strncpy (serv, s->s_name, servlen);
1823 snprintf (serv, servlen, "%d",
1824 ntohs (((const struct sockaddr_in *) sa)->sin_port));
1828 strncpy (serv, ((const struct sockaddr_un *) sa)->sun_path, servlen);
1832 if (host && (hostlen > 0))
1833 host[hostlen-1] = 0;
1834 if (serv && (servlen > 0))
1835 serv[servlen-1] = 0;
1842 #ifdef L_gethostbyname_r
1844 int gethostbyname_r(const char * name,
1845 struct hostent * result_buf,
1846 char * buf, size_t buflen,
1847 struct hostent ** result,
1851 struct in_addr **addr_list;
1853 unsigned char *packet;
1854 struct resolv_answer a;
1857 int __nameserversXX;
1858 char ** __nameserverXX;
1860 if (__open_nameservers()) {
1868 /* do /etc/hosts first */
1870 int old_errno = errno; /* Save the old errno and reset errno */
1871 __set_errno(0); /* to check for missing /etc/hosts. */
1873 if ((i=__get_hosts_byname_r(name, AF_INET, result_buf,
1874 buf, buflen, result, h_errnop))==0)
1876 switch (*h_errnop) {
1877 case HOST_NOT_FOUND:
1880 case NETDB_INTERNAL:
1881 if (errno == ENOENT) {
1884 /* else fall through */
1888 __set_errno(old_errno);
1891 DPRINTF("Nothing found in /etc/hosts\n");
1893 *h_errnop = NETDB_INTERNAL;
1894 if (buflen < sizeof(*in))
1896 in=(struct in_addr*)buf;
1898 buflen-=sizeof(*in);
1900 if (buflen < sizeof(*addr_list)*2)
1902 addr_list=(struct in_addr**)buf;
1903 buf+=sizeof(*addr_list)*2;
1904 buflen-=sizeof(*addr_list)*2;
1909 if (buflen < sizeof(char *)*(ALIAS_DIM))
1912 buf+=sizeof(char **)*(ALIAS_DIM);
1913 buflen-=sizeof(char **)*(ALIAS_DIM);
1917 strncpy(buf, name, buflen);
1922 /* First check if this is already an address */
1923 if (inet_aton(name, in)) {
1924 result_buf->h_name = buf;
1925 result_buf->h_addrtype = AF_INET;
1926 result_buf->h_length = sizeof(*in);
1927 result_buf->h_addr_list = (char **) addr_list;
1928 result_buf->h_aliases = alias;
1930 *h_errnop = NETDB_SUCCESS;
1931 return NETDB_SUCCESS;
1937 __nameserversXX=__nameservers;
1938 __nameserverXX=__nameserver;
1940 i = __dns_lookup(buf, T_A, __nameserversXX, __nameserverXX, &packet, &a);
1943 *h_errnop = HOST_NOT_FOUND;
1944 DPRINTF("__dns_lookup\n");
1948 strncpy(buf, a.dotted, buflen);
1951 if (a.atype == T_CNAME) { /* CNAME */
1952 DPRINTF("Got a CNAME in gethostbyname()\n");
1953 i = __decode_dotted(packet, a.rdoffset, buf, buflen);
1957 *h_errnop = NO_RECOVERY;
1958 DPRINTF("__decode_dotted\n");
1961 if (++nest > MAX_RECURSE) {
1962 *h_errnop = NO_RECOVERY;
1963 DPRINTF("recursion\n");
1967 } else if (a.atype == T_A) { /* ADDRESS */
1968 memcpy(in, a.rdata, sizeof(*in));
1969 result_buf->h_name = buf;
1970 result_buf->h_addrtype = AF_INET;
1971 result_buf->h_length = sizeof(*in);
1972 result_buf->h_addr_list = (char **) addr_list;
1973 #ifdef __UCLIBC_MJN3_ONLY__
1974 #warning TODO -- generate the full list
1976 result_buf->h_aliases = alias; /* TODO: generate the full list */
1981 *h_errnop=HOST_NOT_FOUND;
1987 *h_errnop = NETDB_SUCCESS;
1988 return NETDB_SUCCESS;
1992 #ifdef L_gethostbyname2_r
1994 int gethostbyname2_r(const char *name, int family,
1995 struct hostent * result_buf,
1996 char * buf, size_t buflen,
1997 struct hostent ** result,
2000 #ifndef __UCLIBC_HAS_IPV6__
2001 return family == AF_INET ? gethostbyname_r(name, result_buf, buf, buflen, result, h_errnop) : HOST_NOT_FOUND;
2002 #else /* __UCLIBC_HAS_IPV6__ */
2003 struct in6_addr *in;
2004 struct in6_addr **addr_list;
2005 unsigned char *packet;
2006 struct resolv_answer a;
2009 int __nameserversXX;
2010 char ** __nameserverXX;
2012 if (family == AF_INET)
2013 return gethostbyname_r(name, result_buf, buf, buflen, result, h_errnop);
2015 if (family != AF_INET6)
2018 if (__open_nameservers()) {
2026 /* do /etc/hosts first */
2028 int old_errno = errno; /* Save the old errno and reset errno */
2029 __set_errno(0); /* to check for missing /etc/hosts. */
2031 if ((i=__get_hosts_byname_r(name, AF_INET, result_buf,
2032 buf, buflen, result, h_errnop))==0)
2034 switch (*h_errnop) {
2035 case HOST_NOT_FOUND:
2038 case NETDB_INTERNAL:
2039 if (errno == ENOENT) {
2042 /* else fall through */
2046 __set_errno(old_errno);
2049 DPRINTF("Nothing found in /etc/hosts\n");
2051 *h_errnop = NETDB_INTERNAL;
2052 if (buflen < sizeof(*in))
2054 in=(struct in6_addr*)buf;
2056 buflen-=sizeof(*in);
2058 if (buflen < sizeof(*addr_list)*2)
2060 addr_list=(struct in6_addr**)buf;
2061 buf+=sizeof(*addr_list)*2;
2062 buflen-=sizeof(*addr_list)*2;
2069 strncpy(buf, name, buflen);
2071 /* First check if this is already an address */
2072 if (inet_pton(AF_INET6, name, in)) {
2073 result_buf->h_name = buf;
2074 result_buf->h_addrtype = AF_INET6;
2075 result_buf->h_length = sizeof(*in);
2076 result_buf->h_addr_list = (char **) addr_list;
2078 *h_errnop = NETDB_SUCCESS;
2079 return NETDB_SUCCESS;
2084 __nameserversXX=__nameservers;
2085 __nameserverXX=__nameserver;
2088 i = __dns_lookup(buf, T_AAAA, __nameserversXX, __nameserverXX, &packet, &a);
2091 *h_errnop = HOST_NOT_FOUND;
2095 strncpy(buf, a.dotted, buflen);
2098 if (a.atype == T_CNAME) { /* CNAME */
2099 DPRINTF("Got a CNAME in gethostbyname()\n");
2100 i = __decode_dotted(packet, a.rdoffset, buf, buflen);
2104 *h_errnop = NO_RECOVERY;
2107 if (++nest > MAX_RECURSE) {
2108 *h_errnop = NO_RECOVERY;
2112 } else if (a.atype == T_AAAA) { /* ADDRESS */
2113 memcpy(in, a.rdata, sizeof(*in));
2114 result_buf->h_name = buf;
2115 result_buf->h_addrtype = AF_INET6;
2116 result_buf->h_length = sizeof(*in);
2117 result_buf->h_addr_list = (char **) addr_list;
2122 *h_errnop=HOST_NOT_FOUND;
2128 *h_errnop = NETDB_SUCCESS;
2129 return NETDB_SUCCESS;
2130 #endif /* __UCLIBC_HAS_IPV6__ */
2134 #ifdef L_gethostbyaddr_r
2135 int gethostbyaddr_r (const void *addr, socklen_t len, int type,
2136 struct hostent * result_buf,
2137 char * buf, size_t buflen,
2138 struct hostent ** result,
2143 struct in_addr **addr_list;
2144 #ifdef __UCLIBC_HAS_IPV6__
2147 struct in6_addr *in6;
2148 struct in6_addr **addr_list6;
2149 #endif /* __UCLIBC_HAS_IPV6__ */
2150 unsigned char *packet;
2151 struct resolv_answer a;
2154 int __nameserversXX;
2155 char ** __nameserverXX;
2163 if (len != sizeof(struct in_addr))
2166 #ifdef __UCLIBC_HAS_IPV6__
2168 if (len != sizeof(struct in6_addr))
2171 #endif /* __UCLIBC_HAS_IPV6__ */
2176 /* do /etc/hosts first */
2177 if ((i=__get_hosts_byaddr_r(addr, len, type, result_buf,
2178 buf, buflen, result, h_errnop))==0)
2180 switch (*h_errnop) {
2181 case HOST_NOT_FOUND:
2188 if (__open_nameservers()) {
2192 #ifdef __UCLIBC_HAS_IPV6__
2195 #endif /* __UCLIBC_HAS_IPV6__ */
2197 *h_errnop = NETDB_INTERNAL;
2198 if (buflen < sizeof(*in))
2200 in=(struct in_addr*)buf;
2202 buflen-=sizeof(*in);
2204 if (buflen < sizeof(*addr_list)*2)
2206 addr_list=(struct in_addr**)buf;
2207 buf+=sizeof(*addr_list)*2;
2208 buflen-=sizeof(*addr_list)*2;
2210 #ifdef __UCLIBC_HAS_IPV6__
2211 if (plen < sizeof(*in6))
2213 in6=(struct in6_addr*)qp;
2217 if (plen < sizeof(*addr_list6)*2)
2219 addr_list6=(struct in6_addr**)qp;
2220 qp+=sizeof(*addr_list6)*2;
2221 plen-=sizeof(*addr_list6)*2;
2223 if (plen < buflen) {
2227 #endif /* __UCLIBC_HAS_IPV6__ */
2232 if(type == AF_INET) {
2233 unsigned char *tmp_addr = (unsigned char *)addr;
2235 memcpy(&in->s_addr, addr, len);
2239 sprintf(buf, "%u.%u.%u.%u.in-addr.arpa",
2240 tmp_addr[3], tmp_addr[2], tmp_addr[1], tmp_addr[0]);
2241 #ifdef __UCLIBC_HAS_IPV6__
2243 memcpy(in6->s6_addr, addr, len);
2245 addr_list6[0] = in6;
2248 for (i = len - 1; i >= 0; i--) {
2249 qp += sprintf(qp, "%x.%x.", in6->s6_addr[i] & 0xf,
2250 (in6->s6_addr[i] >> 4) & 0xf);
2252 strcpy(qp, "ip6.int");
2253 #endif /* __UCLIBC_HAS_IPV6__ */
2261 __nameserversXX=__nameservers;
2262 __nameserverXX=__nameserver;
2264 i = __dns_lookup(buf, T_PTR, __nameserversXX, __nameserverXX, &packet, &a);
2267 *h_errnop = HOST_NOT_FOUND;
2271 strncpy(buf, a.dotted, buflen);
2274 if (a.atype == T_CNAME) { /* CNAME */
2275 DPRINTF("Got a CNAME in gethostbyaddr()\n");
2276 i = __decode_dotted(packet, a.rdoffset, buf, buflen);
2280 *h_errnop = NO_RECOVERY;
2283 if (++nest > MAX_RECURSE) {
2284 *h_errnop = NO_RECOVERY;
2288 } else if (a.atype == T_PTR) { /* ADDRESS */
2289 i = __decode_dotted(packet, a.rdoffset, buf, buflen);
2292 result_buf->h_name = buf;
2293 result_buf->h_addrtype = type;
2295 if(type == AF_INET) {
2296 result_buf->h_length = sizeof(*in);
2297 #ifdef __UCLIBC_HAS_IPV6__
2299 result_buf->h_length = sizeof(*in6);
2300 #endif /* __UCLIBC_HAS_IPV6__ */
2303 result_buf->h_addr_list = (char **) addr_list;
2307 *h_errnop = NO_ADDRESS;
2313 *h_errnop = NETDB_SUCCESS;
2314 return NETDB_SUCCESS;
2320 * Expand compressed domain name 'comp_dn' to full domain name.
2321 * 'msg' is a pointer to the begining of the message,
2322 * 'eomorig' points to the first location after the message,
2323 * 'exp_dn' is a pointer to a buffer of size 'length' for the result.
2324 * Return size of compressed name or -1 if there was an error.
2326 int __dn_expand(const u_char *msg, const u_char *eom, const u_char *src,
2327 char *dst, int dstsiz)
2329 int n = ns_name_uncompress(msg, eom, src, dst, (size_t)dstsiz);
2331 if (n > 0 && dst[0] == '.')
2335 #endif /* L_res_comp */
2340 * Thinking in noninternationalized USASCII (per the DNS spec),
2341 * is this character visible and not a space when printed ?
2345 static int printable(int ch)
2347 return (ch > 0x20 && ch < 0x7f);
2352 * Thinking in noninternationalized USASCII (per the DNS spec),
2353 * is this characted special ("in need of quoting") ?
2357 static int special(int ch)
2360 case 0x22: /* '"' */
2361 case 0x2E: /* '.' */
2362 case 0x3B: /* ';' */
2363 case 0x5C: /* '\\' */
2364 /* Special modifiers in zone files. */
2365 case 0x40: /* '@' */
2366 case 0x24: /* '$' */
2374 * ns_name_uncompress(msg, eom, src, dst, dstsiz)
2375 * Expand compressed domain name to presentation format.
2377 * Number of bytes read out of `src', or -1 (with errno set).
2379 * Root domain returns as "." not "".
2381 int __ns_name_uncompress(const u_char *msg, const u_char *eom,
2382 const u_char *src, char *dst, size_t dstsiz)
2384 u_char tmp[NS_MAXCDNAME];
2387 if ((n = ns_name_unpack(msg, eom, src, tmp, sizeof tmp)) == -1)
2389 if (ns_name_ntop(tmp, dst, dstsiz) == -1)
2396 * ns_name_ntop(src, dst, dstsiz)
2397 * Convert an encoded domain name to printable ascii as per RFC1035.
2399 * Number of bytes written to buffer, or -1 (with errno set)
2401 * The root is returned as "."
2402 * All other domains are returned in non absolute form
2404 int __ns_name_ntop(const u_char *src, char *dst, size_t dstsiz) {
2409 const char digits[] = "0123456789";
2415 while ((n = *cp++) != 0) {
2416 if ((n & NS_CMPRSFLGS) != 0) {
2417 /* Some kind of compression pointer. */
2418 __set_errno (EMSGSIZE);
2423 __set_errno (EMSGSIZE);
2428 if (dn + n >= eom) {
2429 __set_errno (EMSGSIZE);
2432 for ((void)NULL; n > 0; n--) {
2435 if (dn + 1 >= eom) {
2436 __set_errno (EMSGSIZE);
2441 } else if (!printable(c)) {
2442 if (dn + 3 >= eom) {
2443 __set_errno (EMSGSIZE);
2447 *dn++ = digits[c / 100];
2448 *dn++ = digits[(c % 100) / 10];
2449 *dn++ = digits[c % 10];
2452 __set_errno (EMSGSIZE);
2461 __set_errno (EMSGSIZE);
2467 __set_errno (EMSGSIZE);
2475 * ns_name_unpack(msg, eom, src, dst, dstsiz)
2476 * Unpack a domain name from a message, source may be compressed.
2478 * -1 if it fails, or consumed octets if it succeeds.
2480 int __ns_name_unpack(const u_char *msg, const u_char *eom, const u_char *src,
2481 u_char *dst, size_t dstsiz)
2483 const u_char *srcp, *dstlim;
2485 int n, len, checked;
2491 dstlim = dst + dstsiz;
2492 if (srcp < msg || srcp >= eom) {
2493 __set_errno (EMSGSIZE);
2496 /* Fetch next label in domain name. */
2497 while ((n = *srcp++) != 0) {
2498 /* Check for indirection. */
2499 switch (n & NS_CMPRSFLGS) {
2502 if (dstp + n + 1 >= dstlim || srcp + n >= eom) {
2503 __set_errno (EMSGSIZE);
2508 memcpy(dstp, srcp, n);
2515 __set_errno (EMSGSIZE);
2519 len = srcp - src + 1;
2520 srcp = msg + (((n & 0x3f) << 8) | (*srcp & 0xff));
2521 if (srcp < msg || srcp >= eom) { /* Out of range. */
2522 __set_errno (EMSGSIZE);
2527 * Check for loops in the compressed name;
2528 * if we've looked at the whole message,
2529 * there must be a loop.
2531 if (checked >= eom - msg) {
2532 __set_errno (EMSGSIZE);
2538 __set_errno (EMSGSIZE);
2539 return (-1); /* flag error */
2547 #endif /* L_ns_name */