summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorcmetz <>1999-06-23 22:38:52 +0000
committercmetz <>1999-06-23 22:38:52 +0000
commitdb859794a581f4638e6accbed6235a18bc8f74b3 (patch)
tree85b56cd6123a97a0a71c1f443b406754eca70df1 /src
parentbb8e4a1e2b3fa93fc2def309cb510f1734df4d77 (diff)
downloadopenbsd-db859794a581f4638e6accbed6235a18bc8f74b3.tar.gz
openbsd-db859794a581f4638e6accbed6235a18bc8f74b3.tar.bz2
openbsd-db859794a581f4638e6accbed6235a18bc8f74b3.zip
Removed portability ifdefs.
Diffstat (limited to 'src')
-rw-r--r--src/lib/libc/net/gai_strerror.c45
-rw-r--r--src/lib/libc/net/getaddrinfo.c576
-rw-r--r--src/lib/libc/net/getnameinfo.c345
3 files changed, 8 insertions, 958 deletions
diff --git a/src/lib/libc/net/gai_strerror.c b/src/lib/libc/net/gai_strerror.c
index a758b7449b..37e0b55e8b 100644
--- a/src/lib/libc/net/gai_strerror.c
+++ b/src/lib/libc/net/gai_strerror.c
@@ -30,33 +30,6 @@
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE. 31 * SUCH DAMAGE.
32 * 32 *
33 * Redistribution and use in source and binary forms, with or without
34 * modification, are permitted provided that the following conditions
35 * are met:
36 * 1. Redistributions of source code must retain the above copyright
37 * notice, this list of conditions and the following disclaimer.
38 * 2. Redistributions in binary form must reproduce the above copyright
39 * notice, this list of conditions and the following disclaimer in the
40 * documentation and/or other materials provided with the distribution.
41 * 3. All advertising materials mentioning features or use of this software
42 * must display the following acknowledgement:
43 * This product includes software developed by Craig Metz and
44 * by other contributors.
45 * 4. Neither the name of the author nor the names of contributors
46 * may be used to endorse or promote products derived from this software
47 * without specific prior written permission.
48 *
49 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
50 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
51 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
52 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
53 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
54 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
55 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
56 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
57 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
58 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
59 * SUCH DAMAGE.
60 */ 33 */
61 34
62/* gai_strerror() v1.38 */ 35/* gai_strerror() v1.38 */
@@ -65,12 +38,6 @@
65#include <netdb.h> 38#include <netdb.h>
66#include <errno.h> 39#include <errno.h>
67 40
68#define THREAD_SAFE 0
69
70#if !THREAD_SAFE
71static char buffer[256];
72#endif /* !THREAD_SAFE */
73
74char *gai_strerror(int errnum) 41char *gai_strerror(int errnum)
75{ 42{
76 switch(errnum) { 43 switch(errnum) {
@@ -97,20 +64,8 @@ char *gai_strerror(int errnum)
97 case EAI_MEMORY: 64 case EAI_MEMORY:
98 return "memory allocation failure"; 65 return "memory allocation failure";
99 case EAI_SYSTEM: 66 case EAI_SYSTEM:
100#if THREAD_SAFE
101 return "system error"; 67 return "system error";
102#else /* THREAD_SAFE */
103 snprintf(buffer, sizeof(buffer)-1, "system error: %s(%d)", strerror(errno), errno);
104 buffer[sizeof(buffer)-1] = 0;
105 return buffer;
106#endif /* THREAD_SAFE */
107 default: 68 default:
108#if THREAD_SAFE
109 return "unknown/invalid error"; 69 return "unknown/invalid error";
110#else /* THREAD_SAFE */
111 snprintf(buffer, sizeof(buffer)-1, "unknown/invalid error %d", errnum);
112 buffer[sizeof(buffer)-1] = 0;
113 return buffer;
114#endif /* THREAD_SAFE */
115 }; 70 };
116}; 71};
diff --git a/src/lib/libc/net/getaddrinfo.c b/src/lib/libc/net/getaddrinfo.c
index a572dbcde0..0e33aa1368 100644
--- a/src/lib/libc/net/getaddrinfo.c
+++ b/src/lib/libc/net/getaddrinfo.c
@@ -30,34 +30,6 @@
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE. 31 * SUCH DAMAGE.
32 * 32 *
33 * Redistribution and use in source and binary forms, with or without
34 * modification, are permitted provided that the following conditions
35 * are met:
36 * 1. Redistributions of source code must retain the above copyright
37 * notice, this list of conditions and the following disclaimer.
38 * 2. Redistributions in binary form must reproduce the above copyright
39 * notice, this list of conditions and the following disclaimer in the
40 * documentation and/or other materials provided with the distribution.
41 * 3. All advertising materials mentioning features or use of this software
42 * must display the following acknowledgement:
43 * This product includes software developed by Craig Metz and
44 * by other contributors.
45 * 4. Neither the name of the author nor the names of contributors
46 * may be used to endorse or promote products derived from this software
47 * without specific prior written permission.
48 *
49 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
50 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
51 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
52 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
53 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
54 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
55 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
56 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
57 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
58 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
59 * SUCH DAMAGE.
60 *
61 */ 33 */
62 34
63/* getaddrinfo() v1.38 */ 35/* getaddrinfo() v1.38 */
@@ -69,74 +41,17 @@
69 behavior and proposed changes). 41 behavior and proposed changes).
70*/ 42*/
71 43
72/*
73 Someone merged an earlier version of this code into the GNU libc and
74 added support for getservbyname_r and gethostbyname2_r. The support for
75 those functions in this version of the code was written using that work
76 as a reference. I may have improved on it, or I may have broken it.
77*/
78
79/* To do what POSIX says, even when it's broken, define: */
80/* #define BROKEN_LIKE_POSIX 1 */
81/* Note: real apps will break if you define this, while nothing other than a
82 conformance test suite should have a problem with it undefined. */
83
84/* If your C runtime library provides the POSIX p1003.1g D6.6 bit types
85 of the form u?int(16|32)_t, define: */
86/* #define HAVE_POSIX1G_TYPES 1 */
87/* Note: this implementation tries to guess what the correct values are for
88 your compiler+processor combination but might not always get it right. */
89
90/* To enable debugging support (REQUIRES NRL support library), define: */
91/* #define DEBUG 1 */
92
93#if FOR_GNULIBC
94#define HAVE_POSIX1G_TYPES 1
95#define INET6 1
96#define LOCAL 1
97#define NETDB 1
98#undef RESOLVER
99#undef HOSTTABLE
100#undef DEBUG
101#define HAVE_GETSERVBYNAME_R 1
102#define HAVE_GETHOSTBYNAME2_R 1
103#define getservbyname_r __getservbyname_r
104#define gethostbyname2_r __gethostbyname2_r
105#endif /* FOR_GNULIBC */
106
107#ifdef __OpenBSD__
108#define HAVE_POSIX1G_TYPES 1
109#define INET6 1
110#define LOCAL 1
111#define NETDB 1
112#define SALEN 1
113#undef RESOLVER
114#undef HOSTTABLE
115#undef DEBUG
116#undef HAVE_GETSERVBYNAME_R
117#undef HAVE_GETHOSTBYNAME2_R
118#endif /* __OpenBSD__ */
119
120#include <sys/types.h> 44#include <sys/types.h>
121#include <stdlib.h> 45#include <stdlib.h>
122#include <unistd.h> 46#include <unistd.h>
123#include <sys/socket.h> 47#include <sys/socket.h>
124#include <string.h> 48#include <string.h>
125#if LOCAL
126#include <stdio.h> 49#include <stdio.h>
127#include <sys/utsname.h> 50#include <sys/utsname.h>
128#include <sys/un.h> 51#include <sys/un.h>
129#endif /* LOCAL */
130#include <netinet/in.h> 52#include <netinet/in.h>
131#include <netdb.h> 53#include <netdb.h>
132#include <errno.h> 54#include <errno.h>
133#if RESOLVER
134#include <arpa/nameser.h>
135#include <resolv.h>
136#endif /* RESOLVER */
137#if DEBUG
138#include <syslog.h>
139#endif /* DEBUG */
140 55
141#ifndef AF_LOCAL 56#ifndef AF_LOCAL
142#define AF_LOCAL AF_UNIX 57#define AF_LOCAL AF_UNIX
@@ -148,47 +63,6 @@
148#define UNIX_PATH_MAX 108 63#define UNIX_PATH_MAX 108
149#endif /* UNIX_PATH_MAX */ 64#endif /* UNIX_PATH_MAX */
150 65
151#if !HAVE_POSIX1G_TYPES
152#if (~0UL) == 0xffffffff
153#define uint8_t unsigned char
154#define int16_t short
155#define uint16_t unsigned short
156#define int32_t long
157#define uint32_t unsigned long
158#else /* (~0UL) == 0xffffffff */
159#if (~0UL) == 0xffffffffffffffff
160#define uint8_t unsigned char
161#define int16_t short
162#define uint16_t unsigned short
163#define int32_t int
164#define uint32_t unsigned int
165#else /* (~0UL) == 0xffffffffffffffff */
166#error Neither 32 bit nor 64 bit word size detected.
167#error You need to define the bit types manually.
168#endif /* (~0UL) == 0xffffffffffffffff */
169#endif /* (~0UL) == 0xffffffff */
170#endif /* !HAVE_POSIX1G_TYPES */
171
172#if defined(INET6) && !defined(AF_INET6)
173#error Without a definition of AF_INET6, this system cannot support IPv6
174#error addresses.
175#endif /* defined(INET6) && !defined(AF_INET6) */
176
177#if INET6
178#ifndef T_AAAA
179#define T_AAAA 28
180#endif /* T_AAAA */
181#endif /* INET6 */
182
183#if DEBUG
184#if RESOLVER
185#define DEBUG_MESSAGES (_res.options & RES_DEBUG)
186#else /* RESOLVER */
187int __getaddrinfo_debug = 0;
188#define DEBUG_MESSAGES (__getaddrinfo_debug)
189#endif /* RESOLVER */
190#endif /* DEBUG */
191
192#define GAIH_OKIFUNSPEC 0x0100 66#define GAIH_OKIFUNSPEC 0x0100
193#define GAIH_EAI ~(GAIH_OKIFUNSPEC) 67#define GAIH_EAI ~(GAIH_OKIFUNSPEC)
194 68
@@ -224,147 +98,21 @@ struct gaih_typeproto {
224 char *name; 98 char *name;
225}; 99};
226 100
227#if DEBUG
228#define RETURN_ERROR(x) do { \ 101#define RETURN_ERROR(x) do { \
229 if (DEBUG_MESSAGES) \
230 fprintf(stderr, "%s:%d: returning %s\n", __FILE__, __LINE__, #x); \
231 rval = (x); \ 102 rval = (x); \
232 goto ret; \ 103 goto ret; \
233 } while(0) 104 } while(0)
234#else /* DEBUG */
235#define RETURN_ERROR(x) do { \
236 rval = (x); \
237 goto ret; \
238 } while(0)
239#endif /* DEBUG */
240 105
241#if HOSTTABLE 106static int netdb_lookup_addr(const char *name, int af, const struct addrinfo *req, struct gaih_addrtuple **pat)
242static int hosttable_lookup_addr(const char *name, const struct addrinfo *req, struct gaih_addrtuple **pat)
243{
244 FILE *f;
245 char buffer[1024];
246 char *c, *c2;
247 int rval = 1;
248 char *prevcname = NULL;
249 struct gaih_addrtuple at;
250
251 if (!(f = fopen("/etc/hosts", "r")))
252 RETURN_ERROR(-EAI_SYSTEM);
253
254 while(fgets(buffer, sizeof(buffer), f)) {
255 if (c = strchr(buffer, '#'))
256 *c = 0;
257
258 c = buffer;
259 while(*c && !isspace(*c)) c++;
260 if (!*c)
261 continue;
262
263 *(c++) = 0;
264
265 while(*c && isspace(*c)) c++;
266 if (!*c)
267 continue;
268
269 if (!(c2 = strstr(c, name)))
270 continue;
271
272 if (*(c2 - 1) && !isspace(*(c2 - 1)))
273 continue;
274
275 c2 += strlen(name);
276 if (*c2 && !isspace(*c2))
277 continue;
278
279 c2 = c;
280 while(*c2 && !isspace(*c2)) c2++;
281 if (!*c2)
282 continue;
283 *c2 = 0;
284
285 memset(&at, 0, sizeof(struct gaih_addrtuple));
286
287 if (!req->ai_family || (req->ai_family == AF_INET))
288 if (inet_pton(AF_INET, buffer, (char *)&at.addr) > 0) {
289 at.family = AF_INET;
290 goto build;
291 };
292
293#if INET6
294 if (!req->ai_family || (req->ai_family == AF_INET6))
295 if (inet_pton(AF_INET6, buffer, (char *)&at.addr) > 0) {
296 at.family = AF_INET6;
297 goto build;
298 };
299#endif /* INET6 */
300
301 continue;
302
303build:
304 if (!(*pat = malloc(sizeof(struct gaih_addrtuple))))
305 RETURN_ERROR(-EAI_MEMORY);
306
307 memcpy(*pat, &at, sizeof(struct gaih_addrtuple));
308
309 if (req->ai_flags & AI_CANONNAME)
310 if (prevcname && !strcmp(prevcname, c))
311 (*pat)->cname = prevcname;
312 else
313 prevcname = (*pat)->cname = strdup(c);
314
315 pat = &((*pat)->next);
316
317 rval = 0;
318 };
319
320ret:
321 if (f)
322 fclose(f);
323 return rval;
324};
325#endif /* HOSTTABLE */
326
327#if NETDB
328int netdb_lookup_addr(const char *name, int af, const struct addrinfo *req, struct gaih_addrtuple **pat)
329{ 107{
330 int rval, herrno, i; 108 int rval, herrno, i;
331 char *prevcname = NULL; 109 char *prevcname = NULL;
332 struct hostent *h; 110 struct hostent *h;
333 111
334#if HAVE_GETHOSTBYNAME2_R
335 void *buf;
336 int buflen = 1024;
337 struct hostent th;
338 int herrno, j;
339
340 do {
341 if (!(buf = malloc(buflen)))
342 RETURN_ERROR(-EAI_MEMORY);
343
344 if (!gethostbyname2_r(name, af, &th, buf, buflen, &h, &herrno))
345 break;
346
347 free(buf);
348 buf = NULL;
349
350 if ((herrno == NETDB_INTERNAL) && (errno == ERANGE)) {
351 if (buflen >= 65536)
352 RETURN_ERROR(-EAI_MEMORY);
353
354 buflen = buflen << 1;
355 continue;
356 };
357 } while(0);
358#else /* HAVE_GETHOSTBYNAME2_R */
359 h = gethostbyname2(name, af); 112 h = gethostbyname2(name, af);
360 herrno = h_errno; 113 herrno = h_errno;
361#endif /* HAVE_GETHOSTBYNAME2_R */
362 114
363 if (!h) { 115 if (!h) {
364#if DEBUG
365 if (DEBUG_MESSAGES)
366 fprintf(stderr, "getaddrinfo: gethostbyname2 failed, h_errno=%d\n", herrno);
367#endif /* DEBUG */
368 switch(herrno) { 116 switch(herrno) {
369 case NETDB_INTERNAL: 117 case NETDB_INTERNAL:
370 RETURN_ERROR(-EAI_SYSTEM); 118 RETURN_ERROR(-EAI_SYSTEM);
@@ -394,11 +142,9 @@ int netdb_lookup_addr(const char *name, int af, const struct addrinfo *req, stru
394 case AF_INET: 142 case AF_INET:
395 memcpy((*pat)->addr, h->h_addr_list[i], sizeof(struct in_addr)); 143 memcpy((*pat)->addr, h->h_addr_list[i], sizeof(struct in_addr));
396 break; 144 break;
397#if INET6
398 case AF_INET6: 145 case AF_INET6:
399 memcpy((*pat)->addr, h->h_addr_list[i], sizeof(struct in6_addr)); 146 memcpy((*pat)->addr, h->h_addr_list[i], sizeof(struct in6_addr));
400 break; 147 break;
401#endif /* INET6 */
402 default: 148 default:
403 RETURN_ERROR(-EAI_FAIL); 149 RETURN_ERROR(-EAI_FAIL);
404 }; 150 };
@@ -416,140 +162,9 @@ int netdb_lookup_addr(const char *name, int af, const struct addrinfo *req, stru
416 rval = 0; 162 rval = 0;
417 163
418ret: 164ret:
419#if HAVE_GETHOSTBYNAME2_R
420 free(buf);
421#endif /* HAVE_GETHOSTBYNAME2_R */
422 return rval; 165 return rval;
423}; 166};
424#endif /* NETDB */
425
426#if RESOLVER
427#define RRHEADER_SZ 10
428 167
429int resolver_lookup_addr(const char *name, int type, const struct addrinfo *req, struct gaih_addrtuple **pat)
430{
431 int rval;
432 char answer[PACKETSZ];
433 int answerlen;
434 char dn[MAXDNAME];
435 char *prevcname = NULL;
436 void *p, *ep;
437 int answers, i, j;
438 uint16_t rtype, rclass;
439
440 if ((answerlen = res_search(name, C_IN, type, answer, sizeof(answer))) < 0) {
441#if DEBUG
442 if (DEBUG_MESSAGES)
443 fprintf(stderr, "getaddrinfo: res_search failed, h_errno=%d\n", h_errno);
444#endif /* DEBUG */
445 switch(h_errno) {
446 case NETDB_INTERNAL:
447 RETURN_ERROR(-EAI_SYSTEM);
448 case HOST_NOT_FOUND:
449 RETURN_ERROR(1);
450 case TRY_AGAIN:
451 RETURN_ERROR(-EAI_AGAIN);
452 case NO_RECOVERY:
453 RETURN_ERROR(-EAI_FAIL);
454 case NO_DATA:
455 RETURN_ERROR(1);
456 default:
457 RETURN_ERROR(-EAI_FAIL);
458 };
459 };
460
461 p = answer;
462 ep = answer + answerlen;
463
464 if (answerlen < RRHEADER_SZ)
465 RETURN_ERROR(-EAI_FAIL);
466
467 {
468 HEADER *h = (HEADER *)p;
469 if (!h->qr || (h->opcode != QUERY) || (h->qdcount != htons(1)) ||
470 !h->ancount)
471 RETURN_ERROR(-EAI_FAIL);
472 answers = ntohs(h->ancount);
473 };
474 p += sizeof(HEADER);
475
476 if ((i = dn_expand(answer, ep, p, dn, sizeof(dn))) < 0)
477 RETURN_ERROR(-EAI_FAIL);
478 p += i;
479
480 if (p + 2*sizeof(uint16_t) >= ep)
481 RETURN_ERROR(-EAI_FAIL);
482
483 GETSHORT(rtype, p);
484 GETSHORT(rclass, p);
485
486 if ((rtype != type) || (rclass != C_IN))
487 RETURN_ERROR(-EAI_FAIL);
488
489 while(answers--) {
490 if ((i = dn_expand(answer, ep, p, dn, sizeof(dn))) < 0)
491 RETURN_ERROR(-EAI_FAIL);
492 p += i;
493
494 if (p + RRHEADER_SZ >= ep)
495 RETURN_ERROR(-EAI_FAIL);
496
497 GETSHORT(rtype, p);
498 GETSHORT(rclass, p);
499 p += sizeof(uint32_t);
500 if (rclass != C_IN)
501 RETURN_ERROR(-EAI_FAIL);
502 GETSHORT(rclass, p);
503 i = rclass;
504
505 if (p + i > ep)
506 RETURN_ERROR(-EAI_FAIL);
507
508 if (rtype == type) {
509 while(*pat)
510 pat = &((*pat)->next);
511
512 if (!(*pat = malloc(sizeof(struct gaih_addrtuple))))
513 RETURN_ERROR(-EAI_MEMORY);
514
515 memset(*pat, 0, sizeof(struct gaih_addrtuple));
516
517 switch(type) {
518 case T_A:
519 if (i != sizeof(struct in_addr))
520 RETURN_ERROR(-EAI_FAIL);
521 (*pat)->family = AF_INET;
522 break;
523#if INET6
524 case T_AAAA:
525 if (i != sizeof(struct in6_addr))
526 RETURN_ERROR(-EAI_FAIL);
527 (*pat)->family = AF_INET6;
528 break;
529#endif /* INET6 */
530 default:
531 RETURN_ERROR(-EAI_FAIL);
532 };
533
534 memcpy((*pat)->addr, p, i);
535
536 if (req->ai_flags & AI_CANONNAME)
537 if (prevcname && !strcmp(prevcname, dn))
538 (*pat)->cname = prevcname;
539 else
540 prevcname = (*pat)->cname = strdup(dn);
541 };
542 p += i;
543 };
544
545 rval = 0;
546
547ret:
548 return rval;
549};
550#endif /* RESOLVER */
551
552#if LOCAL
553static int gaih_local(const char *name, const struct gaih_service *service, 168static int gaih_local(const char *name, const struct gaih_service *service,
554 const struct addrinfo *req, struct addrinfo **pai) 169 const struct addrinfo *req, struct addrinfo **pai)
555{ 170{
@@ -574,9 +189,7 @@ static int gaih_local(const char *name, const struct gaih_service *service,
574 (*pai)->ai_protocol = req->ai_protocol; 189 (*pai)->ai_protocol = req->ai_protocol;
575 (*pai)->ai_addrlen = sizeof(struct sockaddr_un); 190 (*pai)->ai_addrlen = sizeof(struct sockaddr_un);
576 (*pai)->ai_addr = (void *)(*pai) + sizeof(struct addrinfo); 191 (*pai)->ai_addr = (void *)(*pai) + sizeof(struct addrinfo);
577#if SALEN
578 ((struct sockaddr_un *)(*pai)->ai_addr)->sun_len = sizeof(struct sockaddr_un); 192 ((struct sockaddr_un *)(*pai)->ai_addr)->sun_len = sizeof(struct sockaddr_un);
579#endif /* SALEN */
580 ((struct sockaddr_un *)(*pai)->ai_addr)->sun_family = AF_LOCAL; 193 ((struct sockaddr_un *)(*pai)->ai_addr)->sun_family = AF_LOCAL;
581 memset(((struct sockaddr_un *)(*pai)->ai_addr)->sun_path, 0, UNIX_PATH_MAX); 194 memset(((struct sockaddr_un *)(*pai)->ai_addr)->sun_path, 0, UNIX_PATH_MAX);
582 if (service) { 195 if (service) {
@@ -610,7 +223,6 @@ static int gaih_local(const char *name, const struct gaih_service *service,
610ret: 223ret:
611 return rval; 224 return rval;
612}; 225};
613#endif /* LOCAL */
614 226
615static struct gaih_typeproto gaih_inet_typeproto[] = { 227static struct gaih_typeproto gaih_inet_typeproto[] = {
616 { 0, 0, NULL }, 228 { 0, 0, NULL },
@@ -623,34 +235,9 @@ static int gaih_inet_serv(char *servicename, struct gaih_typeproto *tp, struct g
623{ 235{
624 int rval; 236 int rval;
625 struct servent *s; 237 struct servent *s;
626#if HAVE_GETSERVBYNAME_R
627 int i;
628 void *buf;
629 int buflen = 1024;
630 struct servent ts;
631 238
632 do {
633 if (!(buf = malloc(buflen)))
634 RETURN_ERROR(-EAI_MEMORY);
635
636 if (!getservbyname_r(servicename, tp->name, &ts, buf, buflen, &s))
637 break;
638
639 free(buf);
640 buf = NULL;
641
642 if (errno != ERANGE)
643 RETURN_ERROR(GAIH_OKIFUNSPEC | -EAI_SERVICE);
644
645 if (buflen >= 65536)
646 RETURN_ERROR(-EAI_MEMORY);
647
648 buflen = buflen << 1;
649 } while(1);
650#else /* HAVE_GETSERVBYNAME_R */
651 if (!(s = getservbyname(servicename, tp->name))) 239 if (!(s = getservbyname(servicename, tp->name)))
652 RETURN_ERROR(GAIH_OKIFUNSPEC | -EAI_SERVICE); 240 RETURN_ERROR(GAIH_OKIFUNSPEC | -EAI_SERVICE);
653#endif /* HAVE_GETSERVBYNAME_R */
654 241
655 if (!(*st = malloc(sizeof(struct gaih_servtuple)))) 242 if (!(*st = malloc(sizeof(struct gaih_servtuple))))
656 RETURN_ERROR(-EAI_MEMORY); 243 RETURN_ERROR(-EAI_MEMORY);
@@ -663,10 +250,6 @@ static int gaih_inet_serv(char *servicename, struct gaih_typeproto *tp, struct g
663 rval = 0; 250 rval = 0;
664 251
665ret: 252ret:
666#if HAVE_GETSERVBYNAME_R
667 if (buf)
668 free(buf);
669#endif /* HAVE_GETSERVBYNAME_R */
670 return rval; 253 return rval;
671} 254}
672 255
@@ -727,7 +310,6 @@ static int gaih_inet(const char *name, const struct gaih_service *service,
727 310
728 memset(at, 0, sizeof(struct gaih_addrtuple)); 311 memset(at, 0, sizeof(struct gaih_addrtuple));
729 312
730#if INET6
731 if (req->ai_family) 313 if (req->ai_family)
732 at->family = req->ai_family; 314 at->family = req->ai_family;
733 else { 315 else {
@@ -739,9 +321,6 @@ static int gaih_inet(const char *name, const struct gaih_service *service,
739 memset(at->next, 0, sizeof(struct gaih_addrtuple)); 321 memset(at->next, 0, sizeof(struct gaih_addrtuple));
740 at->next->family = AF_INET; 322 at->next->family = AF_INET;
741 }; 323 };
742#else /* INET6 */
743 at->family = AF_INET;
744#endif /* INET6 */
745 324
746 goto build; 325 goto build;
747 }; 326 };
@@ -760,7 +339,6 @@ static int gaih_inet(const char *name, const struct gaih_service *service,
760 }; 339 };
761 }; 340 };
762 341
763#if INET6
764 if (!req->ai_family || (req->ai_family == AF_INET6)) { 342 if (!req->ai_family || (req->ai_family == AF_INET6)) {
765 struct in6_addr in6_addr; 343 struct in6_addr in6_addr;
766 if (inet_pton(AF_INET6, name, &in6_addr) > 0) { 344 if (inet_pton(AF_INET6, name, &in6_addr) > 0) {
@@ -774,56 +352,17 @@ static int gaih_inet(const char *name, const struct gaih_service *service,
774 goto build; 352 goto build;
775 }; 353 };
776 }; 354 };
777#endif /* INET6 */
778 355
779 if (!(req->ai_flags & AI_NUMERICHOST)) { 356 if (!(req->ai_flags & AI_NUMERICHOST)) {
780#if NETDB
781#if INET6
782 if (!req->ai_family || (req->ai_family == AF_INET6)) 357 if (!req->ai_family || (req->ai_family == AF_INET6))
783 if ((rval = netdb_lookup_addr(name, AF_INET6, req, &at)) < 0) 358 if ((rval = netdb_lookup_addr(name, AF_INET6, req, &at)) < 0)
784 goto ret; 359 goto ret;
785#endif /* INET6 */
786 if (!req->ai_family || (req->ai_family == AF_INET)) 360 if (!req->ai_family || (req->ai_family == AF_INET))
787 if ((rval = netdb_lookup_addr(name, AF_INET, req, &at)) < 0) 361 if ((rval = netdb_lookup_addr(name, AF_INET, req, &at)) < 0)
788 goto ret; 362 goto ret;
789 363
790 if (!rval) 364 if (!rval)
791 goto build; 365 goto build;
792#else /* NETDB */
793#if HOSTTABLE
794 if ((rval = hosttable_lookup_addr(name, req, &at)) < 0)
795 goto ret;
796
797 if (!rval)
798 goto build;
799#endif /* HOSTTABLE */
800
801#if RESOLVER
802#if INET6
803 {
804 int rval2;
805
806 if (!req->ai_family || (req->ai_family == AF_INET6))
807 if ((rval2 = resolver_lookup_addr(name, T_AAAA, req, &at)) < 0) {
808 rval = rval2;
809 goto ret;
810 };
811#endif /* INET6 */
812
813 if (!req->ai_family || (req->ai_family == AF_INET))
814 if ((rval = resolver_lookup_addr(name, T_A, req, &at)) < 0)
815 goto ret;
816
817#if INET6
818 if (!rval || !rval2)
819 goto build;
820 };
821#else /* INET6 */
822 if (!rval)
823 goto build;
824#endif /* INET6 */
825#endif /* RESOLVER */
826#endif /* NETDB */
827 }; 366 };
828 367
829 if (!at) 368 if (!at)
@@ -848,11 +387,9 @@ build:
848 } else 387 } else
849 j = 0; 388 j = 0;
850 389
851#if INET6
852 if (at2->family == AF_INET6) 390 if (at2->family == AF_INET6)
853 i = sizeof(struct sockaddr_in6); 391 i = sizeof(struct sockaddr_in6);
854 else 392 else
855#endif /* INET6 */
856 i = sizeof(struct sockaddr_in); 393 i = sizeof(struct sockaddr_in);
857 394
858 st2 = st; 395 st2 = st;
@@ -868,17 +405,13 @@ build:
868 (*pai)->ai_protocol = st2->protocol; 405 (*pai)->ai_protocol = st2->protocol;
869 (*pai)->ai_addrlen = i; 406 (*pai)->ai_addrlen = i;
870 (*pai)->ai_addr = (void *)(*pai) + sizeof(struct addrinfo); 407 (*pai)->ai_addr = (void *)(*pai) + sizeof(struct addrinfo);
871#if SALEN
872 ((struct sockaddr_in *)(*pai)->ai_addr)->sin_len = i; 408 ((struct sockaddr_in *)(*pai)->ai_addr)->sin_len = i;
873#endif /* SALEN */
874 ((struct sockaddr_in *)(*pai)->ai_addr)->sin_family = at2->family; 409 ((struct sockaddr_in *)(*pai)->ai_addr)->sin_family = at2->family;
875 ((struct sockaddr_in *)(*pai)->ai_addr)->sin_port = st2->port; 410 ((struct sockaddr_in *)(*pai)->ai_addr)->sin_port = st2->port;
876 411
877#if INET6
878 if (at2->family == AF_INET6) 412 if (at2->family == AF_INET6)
879 memcpy(&((struct sockaddr_in6 *)(*pai)->ai_addr)->sin6_addr, at2->addr, sizeof(struct in6_addr)); 413 memcpy(&((struct sockaddr_in6 *)(*pai)->ai_addr)->sin6_addr, at2->addr, sizeof(struct in6_addr));
880 else 414 else
881#endif /* INET6 */
882 memcpy(&((struct sockaddr_in *)(*pai)->ai_addr)->sin_addr, at2->addr, sizeof(struct in_addr)); 415 memcpy(&((struct sockaddr_in *)(*pai)->ai_addr)->sin_addr, at2->addr, sizeof(struct in_addr));
883 416
884 if (j) { 417 if (j) {
@@ -933,86 +466,12 @@ struct gaih {
933}; 466};
934 467
935static struct gaih gaih[] = { 468static struct gaih gaih[] = {
936#if INET6
937 { PF_INET6, "inet6", gaih_inet }, 469 { PF_INET6, "inet6", gaih_inet },
938#endif /* INET6 */
939 { PF_INET, "inet", gaih_inet }, 470 { PF_INET, "inet", gaih_inet },
940#if LOCAL
941 { PF_LOCAL, "local", gaih_local }, 471 { PF_LOCAL, "local", gaih_local },
942#endif /* LOCAL */
943 { -1, NULL, NULL } 472 { -1, NULL, NULL }
944}; 473};
945 474
946#if DEBUG
947static void dump_addrinfo(const struct addrinfo *ai, int follownext)
948{
949 char *c;
950
951loop:
952 fprintf(stderr, "addrinfo at ");
953 if (!ai) {
954 fprintf(stderr, "NULL\n");
955 return;
956 };
957 fprintf(stderr, "%08x:\n", (unsigned int)ai);
958 fprintf(stderr, " flags=%x(", ai->ai_flags);
959 c = "";
960 if (ai->ai_flags & AI_PASSIVE) {
961 fprintf(stderr, "passive");
962 c = " ";
963 };
964 if (ai->ai_flags & AI_CANONNAME) {
965 fprintf(stderr, "%scanonname", c);
966 c = " ";
967 };
968 if (ai->ai_flags & AI_NUMERICHOST) {
969 fprintf(stderr, "%snumerichost", c);
970 c = " ";
971 };
972 if (ai->ai_flags & AI_EXT) {
973 fprintf(stderr, "%sext", c);
974 };
975 fprintf(stderr, ")\n");
976 fprintf(stderr, " family=%x(%s)\n", ai->ai_family, nrl_afnumtoname(ai->ai_family));
977 fprintf(stderr, " socktype=%x(%s)\n", ai->ai_socktype, nrl_socktypenumtoname(ai->ai_socktype));
978 fprintf(stderr, " protocol=%x\n", ai->ai_protocol);
979 fprintf(stderr, " addrlen=%x\n", ai->ai_addrlen);
980 fprintf(stderr, " addr=%08x", (unsigned int)ai->ai_addr);
981 if (ai->ai_addr) {
982 fprintf(stderr, ":\n");
983#if SALEN
984 fprintf(stderr, " len=%x\n", ai->ai_addr->sa_len);
985#endif /* SALEN */
986 fprintf(stderr, " family=%x(%s)\n", ai->ai_addr->sa_family, nrl_afnumtoname(ai->ai_addr->sa_family));
987 fprintf(stderr, " data=");
988
989#if SALEN
990 if (ai->ai_addrlen != ai->ai_addr->sa_len) {
991 fprintf(stderr, " (addrlen != len, skipping)");
992 } else
993#endif /* SALEN */
994 {
995 uint8_t *p;
996 int i;
997
998 p = (uint8_t *)ai->ai_addr->sa_data;
999 i = ai->ai_addrlen - ((void *)ai->ai_addr->sa_data - (void *)ai->ai_addr);
1000 while (i-- > 0)
1001 fprintf(stderr, "%02x", *(p++));
1002 };
1003 };
1004 fprintf(stderr, "\n canonname=%08x", (unsigned int)ai->ai_canonname);
1005 if (ai->ai_canonname)
1006 fprintf(stderr, "(%s)", ai->ai_canonname);
1007 fprintf(stderr, "\n next=%08x\n", (unsigned int)ai->ai_next);
1008
1009 if (follownext && ai->ai_next) {
1010 ai = ai->ai_next;
1011 goto loop;
1012 };
1013};
1014#endif /* DEBUG */
1015
1016int getaddrinfo(const char *name, const char *service, 475int getaddrinfo(const char *name, const char *service,
1017 const struct addrinfo *req, struct addrinfo **pai) 476 const struct addrinfo *req, struct addrinfo **pai)
1018{ 477{
@@ -1022,47 +481,24 @@ int getaddrinfo(const char *name, const char *service,
1022 struct gaih *g = gaih, *pg = NULL; 481 struct gaih *g = gaih, *pg = NULL;
1023 struct gaih_service gaih_service, *pservice; 482 struct gaih_service gaih_service, *pservice;
1024 483
1025#if DEBUG
1026 if (DEBUG_MESSAGES) {
1027 fprintf(stderr, "getaddrinfo(name=%s, service=%s, req=%p, pai=%p)\n req: ", name ? name : "NULL", service ? service : "NULL", req, pai);
1028
1029 dump_addrinfo(req, 0);
1030 };
1031#endif /* DEBUG */
1032
1033 if (name && (name[0] == '*') && !name[1]) 484 if (name && (name[0] == '*') && !name[1])
1034 name = NULL; 485 name = NULL;
1035 486
1036 if (service && (service[0] == '*') && !service[1]) 487 if (service && (service[0] == '*') && !service[1])
1037 service = NULL; 488 service = NULL;
1038 489
1039#if BROKEN_LIKE_POSIX
1040 if (!name && !service && !(req.ai_flags & AI_EXT))
1041 RETURN_ERROR(EAI_NONAME);
1042#endif /* BROKEN_LIKE_POSIX */
1043
1044 if (!req) 490 if (!req)
1045 req = &nullreq; 491 req = &nullreq;
1046 492
1047 if (req->ai_flags & ~(AI_CANONNAME | AI_PASSIVE | AI_NUMERICHOST | AI_EXT)) 493 if (req->ai_flags & ~(AI_CANONNAME | AI_PASSIVE | AI_NUMERICHOST | AI_EXT))
1048 RETURN_ERROR(EAI_BADFLAGS); 494 RETURN_ERROR(EAI_BADFLAGS);
1049 495
1050#if BROKEN_LIKE_POSIX
1051 if ((req->ai_flags & AI_CANONNAME) && !name && !(req.ai_flags & AI_EXT))
1052 RETURN_ERROR(EAI_BADFLAGS);
1053#endif /* BROKEN_LIKE_POSIX */
1054
1055 if (service && *service) { 496 if (service && *service) {
1056 char *c; 497 char *c;
1057 gaih_service.num = strtoul(gaih_service.name = (void *)service, &c, 10); 498 gaih_service.num = strtoul(gaih_service.name = (void *)service, &c, 10);
1058 if (*c) { 499 if (*c) {
1059 gaih_service.num = -1; 500 gaih_service.num = -1;
1060 } 501 }
1061#if BROKEN_LIKE_POSIX
1062 else
1063 if (!req->ai_socktype && !(req.ai_flags & AI_EXT))
1064 RETURN_ERROR(EAI_SERVICE);
1065#endif /* BROKEN_LIKE_POSIX */
1066 502
1067 pservice = &gaih_service; 503 pservice = &gaih_service;
1068 } else 504 } else
@@ -1100,12 +536,6 @@ int getaddrinfo(const char *name, const char *service,
1100 536
1101 if (p) { 537 if (p) {
1102 *pai = p; 538 *pai = p;
1103#if DEBUG
1104 if (DEBUG_MESSAGES) {
1105 fprintf(stderr, "getaddrinfo: Success. *pai:\n");
1106 dump_addrinfo(p, 1);
1107 };
1108#endif /* DEBUG */
1109 rval = 0; 539 rval = 0;
1110 goto ret; 540 goto ret;
1111 } 541 }
@@ -1118,10 +548,6 @@ int getaddrinfo(const char *name, const char *service,
1118 RETURN_ERROR(EAI_NONAME); 548 RETURN_ERROR(EAI_NONAME);
1119 549
1120ret: 550ret:
1121#if DEBUG
1122 if (DEBUG_MESSAGES)
1123 fprintf(stderr, "getaddrinfo=%d\n", rval);
1124#endif /* DEBUG */
1125 return rval; 551 return rval;
1126} 552}
1127 553
diff --git a/src/lib/libc/net/getnameinfo.c b/src/lib/libc/net/getnameinfo.c
index 15aa67d464..95b27a7006 100644
--- a/src/lib/libc/net/getnameinfo.c
+++ b/src/lib/libc/net/getnameinfo.c
@@ -30,98 +30,19 @@
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE. 31 * SUCH DAMAGE.
32 * 32 *
33 * Redistribution and use in source and binary forms, with or without
34 * modification, are permitted provided that the following conditions
35 * are met:
36 * 1. Redistributions of source code must retain the above copyright
37 * notice, this list of conditions and the following disclaimer.
38 * 2. Redistributions in binary form must reproduce the above copyright
39 * notice, this list of conditions and the following disclaimer in the
40 * documentation and/or other materials provided with the distribution.
41 * 3. All advertising materials mentioning features or use of this software
42 * must display the following acknowledgement:
43 * This product includes software developed by Craig Metz and
44 * by other contributors.
45 * 4. Neither the name of the author nor the names of contributors
46 * may be used to endorse or promote products derived from this software
47 * without specific prior written permission.
48 *
49 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
50 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
51 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
52 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
53 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
54 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
55 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
56 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
57 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
58 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
59 * SUCH DAMAGE.
60 *
61 * Redistribution and use in source and binary forms, with or without
62 * modification, are permitted provided that the following conditions
63 * are met:
64 * 1. Redistributions of source code must retain the above copyright
65 * notice, this list of conditions and the following disclaimer.
66 * 2. Redistributions in binary form must reproduce the above copyright
67 * notice, this list of conditions and the following disclaimer in the
68 * documentation and/or other materials provided with the distribution.
69 * 3. All advertising materials mentioning features or use of this software
70 * must display the following acknowledgement:
71 * This product includes software developed by Craig Metz and
72 * by other contributors.
73 * 4. Neither the name of the author nor the names of contributors
74 * may be used to endorse or promote products derived from this software
75 * without specific prior written permission.
76 *
77 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
78 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
79 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
80 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
81 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
82 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
83 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
84 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
85 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
86 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
87 * SUCH DAMAGE.
88 */ 33 */
89 34
90/* getnameinfo() v1.38 */ 35/* getnameinfo() v1.38 */
91 36
92/* To enable debugging support (REQUIRES NRL support library), define: */
93/* #define DEBUG 1 */
94
95#ifdef __OpenBSD__
96#define HAVE_POSIX1G_TYPES 1
97#define INET6 1
98#define LOCAL 1
99#define NETDB 1
100#define SALEN 1
101#undef RESOLVER
102#undef HOSTTABLE
103#undef DEBUG
104#undef HAVE_GETSERVBYNAME_R
105#undef HAVE_GETHOSTBYNAME2_R
106#endif /* __OpenBSD__ */
107
108#include <sys/types.h> 37#include <sys/types.h>
109#include <sys/socket.h> 38#include <sys/socket.h>
110 39
111#include <netinet/in.h> 40#include <netinet/in.h>
112#if LOCAL
113#include <sys/un.h> 41#include <sys/un.h>
114#include <sys/utsname.h> 42#include <sys/utsname.h>
115#endif /* LOCAL */
116#include <netdb.h> 43#include <netdb.h>
117#include <errno.h> 44#include <errno.h>
118#include <string.h> 45#include <string.h>
119#if RESOLVER
120#include <arpa/nameser.h>
121#include <resolv.h>
122#endif /* RESOLVER */
123
124#include "support.h"
125 46
126#ifndef AF_LOCAL 47#ifndef AF_LOCAL
127#define AF_LOCAL AF_UNIX 48#define AF_LOCAL AF_UNIX
@@ -131,214 +52,11 @@
131#define min(x,y) (((x) > (y)) ? (y) : (x)) 52#define min(x,y) (((x) > (y)) ? (y) : (x))
132#endif /* min */ 53#endif /* min */
133 54
134#if DEBUG
135#if RESOLVER
136#define DEBUG_MESSAGES (_res.options & RES_DEBUG)
137#else /* RESOLVER */
138int __getnameinfo_debug = 0;
139#define DEBUG_MESSAGES (__getnameinfo_debug)
140#endif /* RESOLVER */
141#endif /* DEBUG */
142
143#if DEBUG
144#define RETURN_ERROR(x) do { \ 55#define RETURN_ERROR(x) do { \
145 if (DEBUG_MESSAGES) \
146 fprintf(stderr, "%s:%d: returning %s\n", __FILE__, __LINE__, #x); \
147 rval = (x); \ 56 rval = (x); \
148 goto ret; \ 57 goto ret; \
149 } while(0) 58 } while(0)
150#else /* DEBUG */
151#define RETURN_ERROR(x) do { \
152 rval = (x); \
153 goto ret; \
154 } while(0)
155#endif /* DEBUG */
156
157#if HOSTTABLE
158static int hosttable_lookup_name(int family, void *addr, char *name, int namelen, int flags)
159{
160 int rval;
161 FILE *f;
162 char buffer[1024];
163 char addrbuf[16];
164 char *c, *c2;
165 int i;
166 char *prevcname = NULL;
167
168 if (!(f = fopen("/etc/hosts", "r")))
169 RETURN_ERROR(EAI_SYSTEM);
170
171 while(fgets(buffer, sizeof(buffer), f)) {
172 if (c = strchr(buffer, '#'))
173 *c = 0;
174
175 c = buffer;
176 while(*c && !isspace(*c)) c++;
177 if (!*c)
178 continue;
179
180 *(c++) = 0;
181
182 if (family == AF_INET)
183 if (inet_pton(AF_INET, buffer, addrbuf) > 0)
184 if (!memcmp(addrbuf, addr, sizeof(struct in_addr)))
185 goto build;
186
187#if INET6
188 if (family == AF_INET6)
189 if (inet_pton(AF_INET6, buffer, addrbuf) > 0)
190 if (!memcmp(addrbuf, addr, sizeof(struct in6_addr)))
191 goto build;
192#endif /* INET6 */
193
194 continue;
195
196build:
197 while(*c && isspace(*c)) c++;
198 if (!*c)
199 continue;
200 59
201 c2 = c;
202 while(*c2 && !isspace(*c2)) c2++;
203 if (!*c2)
204 continue;
205 *c2 = 0;
206
207 if ((flags & NI_NOFQDN) && (_res.options & RES_INIT) && _res.defdname[0] && (c2 = strstr(c + 1, _res.defdname)) && (*(--c2) == '.')) {
208 *c2 = 0;
209 i = min(c2 - c, namelen) - 1;
210 strncpy(name, c, i);
211 } else
212 strncpy(name, c, namelen - 1);
213
214 rval = 0;
215 goto ret;
216 };
217
218 RETURN_ERROR(1);
219
220ret:
221 fclose(f);
222 return rval;
223};
224#endif /* HOSTTABLE */
225
226#if RESOLVER
227#if INET6
228static char hextab[] = { '0', '1', '2', '3', '4', '5', '6', '7',
229 '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
230#endif /* INET6 */
231
232struct rrheader {
233 int16_t type;
234 int16_t class;
235 u_int32_t ttl;
236 int16_t size;
237};
238#define RRHEADER_SZ 10
239
240int resolver_lookup_name(const char *ptrname, char *name, int namelen, int flags)
241{
242 int rval;
243 char answer[PACKETSZ];
244 int answerlen;
245 char dn[MAXDNAME];
246 char *prevcname = NULL;
247 void *p, *ep;
248 int answers, i;
249 uint16_t rtype, rclass;
250
251 if ((answerlen = res_search(ptrname, C_IN, T_PTR, answer, sizeof(answer))) < 0) {
252 switch(h_errno) {
253 case NETDB_INTERNAL:
254 RETURN_ERROR(EAI_SYSTEM);
255 case HOST_NOT_FOUND:
256 RETURN_ERROR(1);
257 case TRY_AGAIN:
258 RETURN_ERROR(EAI_AGAIN);
259 case NO_RECOVERY:
260 RETURN_ERROR(EAI_FAIL);
261 case NO_DATA:
262 RETURN_ERROR(1);
263 default:
264 RETURN_ERROR(EAI_FAIL);
265 };
266 };
267
268 p = answer;
269 ep = answer + answerlen;
270
271 if (answerlen < sizeof(HEADER))
272 RETURN_ERROR(EAI_FAIL);
273
274 {
275 HEADER *h = (HEADER *)p;
276 if (!h->qr || (h->opcode != QUERY) || (h->qdcount != htons(1)) || !h->ancount)
277 RETURN_ERROR(EAI_FAIL);
278
279 answers = ntohs(h->ancount);
280 };
281 p += sizeof(HEADER);
282
283 if ((i = dn_expand(answer, ep, p, dn, sizeof(dn))) < 0)
284 RETURN_ERROR(EAI_FAIL);
285
286 p += i;
287
288 if (p + 2*sizeof(u_int16_t) >= ep)
289 RETURN_ERROR(EAI_FAIL);
290
291 GETSHORT(rtype, p);
292 GETSHORT(rclass, p);
293
294 if ((rtype != T_PTR) || (rclass != C_IN))
295 RETURN_ERROR(EAI_FAIL);
296
297 while(answers--) {
298 if ((i = dn_expand(answer, ep, p, dn, sizeof(dn))) < 0)
299 RETURN_ERROR(EAI_FAIL);
300
301 p += i;
302
303 if (p + RRHEADER_SZ >= ep)
304 RETURN_ERROR(EAI_FAIL);
305
306 GETSHORT(rtype, p);
307 GETSHORT(rclass, p);
308 p += sizeof(uint32_t);
309 if (rclass != C_IN)
310 RETURN_ERROR(EAI_FAIL);
311 GETSHORT(rclass, p);
312 i = rclass;
313
314 if (p + i > ep)
315 RETURN_ERROR(EAI_FAIL);
316
317 if (rtype == T_PTR) {
318 if (dn_expand(answer, ep, p, dn, sizeof(dn)) != i)
319 RETURN_ERROR(EAI_FAIL);
320
321 {
322 char *c2;
323
324 if ((flags & NI_NOFQDN) && (_res.options & RES_INIT) && _res.defdname[0] && (c2 = strstr(dn + 1, _res.defdname)) && (*(--c2) == '.')) {
325 *c2 = 0;
326 strncpy(name, dn, min(c2 - dn, namelen) - 1);
327 } else
328 strncpy(name, dn, namelen - 1);
329 };
330 };
331 p += i;
332 };
333
334 rval = 0;
335
336ret:
337 return rval;
338};
339#endif /* RESOLVER */
340
341#if NETDB
342static int netdb_lookup_name(int family, void *addr, int addrlen, char *name, 60static int netdb_lookup_name(int family, void *addr, int addrlen, char *name,
343 int namelen) 61 int namelen)
344{ 62{
@@ -379,7 +97,6 @@ static int netdb_lookup_name(int family, void *addr, int addrlen, char *name,
379ret: 97ret:
380 return rval; 98 return rval;
381} 99}
382#endif /* NETDB */
383 100
384int getnameinfo(const struct sockaddr *sa, size_t addrlen, char *host, size_t hostlen, char *serv, size_t servlen, int flags) 101int getnameinfo(const struct sockaddr *sa, size_t addrlen, char *host, size_t hostlen, char *serv, size_t servlen, int flags)
385{ 102{
@@ -391,7 +108,6 @@ int getnameinfo(const struct sockaddr *sa, size_t addrlen, char *host, size_t ho
391 108
392 if (host && (hostlen > 0)) 109 if (host && (hostlen > 0))
393 switch(sa->sa_family) { 110 switch(sa->sa_family) {
394#if INET6
395 case AF_INET6: 111 case AF_INET6:
396 if (IN6_IS_ADDR_UNSPECIFIED(&((struct sockaddr_in6 *)sa)->sin6_addr)) { 112 if (IN6_IS_ADDR_UNSPECIFIED(&((struct sockaddr_in6 *)sa)->sin6_addr)) {
397 if (flags & NI_NUMERICHOST) 113 if (flags & NI_NUMERICHOST)
@@ -404,9 +120,7 @@ int getnameinfo(const struct sockaddr *sa, size_t addrlen, char *host, size_t ho
404 if (IN6_IS_ADDR_V4MAPPED(&((struct sockaddr_in6 *)sa)->sin6_addr)) { 120 if (IN6_IS_ADDR_V4MAPPED(&((struct sockaddr_in6 *)sa)->sin6_addr)) {
405 struct sockaddr_in sin; 121 struct sockaddr_in sin;
406 memset(&sin, 0, sizeof(struct sockaddr_in)); 122 memset(&sin, 0, sizeof(struct sockaddr_in));
407#if SALEN
408 sin.sin_len = sizeof(struct sockaddr_in); 123 sin.sin_len = sizeof(struct sockaddr_in);
409#endif /* SALEN */
410 sin.sin_family = AF_INET; 124 sin.sin_family = AF_INET;
411 sin.sin_port = ((struct sockaddr_in6 *)sa)->sin6_port; 125 sin.sin_port = ((struct sockaddr_in6 *)sa)->sin6_port;
412 sin.sin_addr.s_addr = ((u_int32_t *)&((struct sockaddr_in6 *)sa)->sin6_addr)[3]; 126 sin.sin_addr.s_addr = ((u_int32_t *)&((struct sockaddr_in6 *)sa)->sin6_addr)[3];
@@ -420,37 +134,13 @@ int getnameinfo(const struct sockaddr *sa, size_t addrlen, char *host, size_t ho
420 if (flags & NI_NUMERICHOST) 134 if (flags & NI_NUMERICHOST)
421 goto inet6_noname; 135 goto inet6_noname;
422 136
423#if HOSTTABLE 137 if ((rval = netdb_lookup_name(AF_INET6,
424 if ((rval = hosttable_lookup_name(AF_INET6, &((struct sockaddr_in6 *)sa)->sin6_addr, host, hostlen, flags)) < 0) 138 &((struct sockaddr_in6 *)sa)->sin6_addr, sizeof(struct in6_addr),
139 host, hostlen, flags)) < 0)
425 goto ret; 140 goto ret;
426 141
427 if (!rval) 142 if (!rval)
428 break; 143 break;
429#endif /* HOSTTABLE */
430#if RESOLVER
431 {
432 char ptrname[sizeof("0.1.2.3.4.5.6.7.8.9.a.b.c.d.e.f.0.1.2.3.4.5.6.7.8.9.a.b.c.d.e.f.ip6.int.")];
433 {
434 int i;
435 char *c = ptrname;
436 u_int8_t *p = (u_int8_t *)&((struct sockaddr_in6 *)sa)->sin6_addr + sizeof(struct in6_addr) - 1;
437
438 for (i = sizeof(struct in6_addr) / sizeof(u_int8_t); i > 0; i--, p--) {
439 *(c++) = hextab[*p & 0x0f];
440 *(c++) = '.';
441 *(c++) = hextab[(*p & 0xf0) >> 4];
442 *(c++) = '.';
443 };
444 strcpy(c, "ip6.int.");
445 };
446
447 if ((rval = resolver_lookup_name(ptrname, host, hostlen, flags)) < 0)
448 goto ret;
449
450 if (!rval)
451 break;
452 };
453#endif /* RESOLVER */
454 144
455inet6_noname: 145inet6_noname:
456 if (flags & NI_NAMEREQD) 146 if (flags & NI_NAMEREQD)
@@ -460,7 +150,6 @@ inet6_noname:
460 RETURN_ERROR(EAI_NONAME); 150 RETURN_ERROR(EAI_NONAME);
461 151
462 break; 152 break;
463#endif /* INET6 */
464 case AF_INET: 153 case AF_INET:
465 if (flags & NI_NUMERICHOST) 154 if (flags & NI_NUMERICHOST)
466 goto inet_noname; 155 goto inet_noname;
@@ -470,27 +159,13 @@ inet6_noname:
470 break; 159 break;
471 }; 160 };
472 161
473#if HOSTTABLE 162 if ((rval = netdb_lookup_name(AF_INET,
474 if ((rval = hosttable_lookup_name(AF_INET, &((struct sockaddr_in *)sa)->sin_addr, host, hostlen, flags)) < 0) 163 &((struct sockaddr_in *)sa)->sin_addr, sizeof(struct in_addr),
164 host, hostlen, flags)) < 0)
475 goto ret; 165 goto ret;
476 166
477 if (!rval) 167 if (!rval)
478 break; 168 break;
479#endif /* HOSTTABLE */
480#if RESOLVER
481 {
482 char ptrname[30];
483 u_int8_t *p = (u_int8_t *)&((struct sockaddr_in *)sa)->sin_addr;
484 sprintf(ptrname, "%d.%d.%d.%d.in-addr.arpa.", p[3], p[2], p[1], p[0]);
485
486 if ((rval = resolver_lookup_name(ptrname, host, hostlen, flags)) < 0)
487 goto ret;
488
489 if (!rval)
490 break;
491 };
492#endif /* RESOLVER */
493
494inet_noname: 169inet_noname:
495 if (flags & NI_NAMEREQD) 170 if (flags & NI_NAMEREQD)
496 RETURN_ERROR(EAI_NONAME); 171 RETURN_ERROR(EAI_NONAME);
@@ -499,7 +174,6 @@ inet_noname:
499 RETURN_ERROR(EAI_NONAME); 174 RETURN_ERROR(EAI_NONAME);
500 175
501 break; 176 break;
502#if LOCAL
503 case AF_LOCAL: 177 case AF_LOCAL:
504 if (!(flags & NI_NUMERICHOST)) { 178 if (!(flags & NI_NUMERICHOST)) {
505 struct utsname utsname; 179 struct utsname utsname;
@@ -515,7 +189,6 @@ inet_noname:
515 189
516 strncpy(host, "localhost", hostlen - 1); 190 strncpy(host, "localhost", hostlen - 1);
517 break; 191 break;
518#endif /* LOCAL */
519 default: 192 default:
520 RETURN_ERROR(EAI_FAMILY); 193 RETURN_ERROR(EAI_FAMILY);
521 }; 194 };
@@ -523,9 +196,7 @@ inet_noname:
523 if (serv && (servlen > 0)) 196 if (serv && (servlen > 0))
524 switch(sa->sa_family) { 197 switch(sa->sa_family) {
525 case AF_INET: 198 case AF_INET:
526#if INET6
527 case AF_INET6: 199 case AF_INET6:
528#endif /* INET6 */
529 if (!(flags & NI_NUMERICSERV)) { 200 if (!(flags & NI_NUMERICSERV)) {
530 struct servent *s; 201 struct servent *s;
531 if (s = getservbyport(((struct sockaddr_in *)sa)->sin_port, (flags & NI_DGRAM) ? "udp" : "tcp")) { 202 if (s = getservbyport(((struct sockaddr_in *)sa)->sin_port, (flags & NI_DGRAM) ? "udp" : "tcp")) {
@@ -539,11 +210,9 @@ inet_noname:
539 }; 210 };
540 snprintf(serv, servlen - 1, "%d", ntohs(((struct sockaddr_in *)sa)->sin_port)); 211 snprintf(serv, servlen - 1, "%d", ntohs(((struct sockaddr_in *)sa)->sin_port));
541 break; 212 break;
542#if LOCAL
543 case AF_LOCAL: 213 case AF_LOCAL:
544 strncpy(serv, ((struct sockaddr_un *)sa)->sun_path, servlen - 1); 214 strncpy(serv, ((struct sockaddr_un *)sa)->sun_path, servlen - 1);
545 break; 215 break;
546#endif /* LOCAL */
547 }; 216 };
548 217
549 if (host && (hostlen > 0)) 218 if (host && (hostlen > 0))