1 /* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */
3 * Copyright (C) 2001,2002,2003,2004,2005,2006 by the Massachusetts Institute of Technology,
4 * Cambridge, MA, USA. All Rights Reserved.
6 * This software is being provided to you, the LICENSEE, by the
7 * Massachusetts Institute of Technology (M.I.T.) under the following
8 * license. By obtaining, using and/or copying this software, you agree
9 * that you have read, understood, and will comply with these terms and
12 * Export of this software from the United States of America may
13 * require a specific license from the United States Government.
14 * It is the responsibility of any person or organization contemplating
15 * export to obtain such a license before exporting.
17 * WITHIN THAT CONSTRAINT, permission to use, copy, modify and distribute
18 * this software and its documentation for any purpose and without fee or
19 * royalty is hereby granted, provided that you agree to comply with the
20 * following copyright notice and statements, including the disclaimer, and
21 * that the same appear on ALL copies of the software and documentation,
22 * including modifications that you make for internal use or for
25 * THIS SOFTWARE IS PROVIDED "AS IS", AND M.I.T. MAKES NO REPRESENTATIONS
26 * OR WARRANTIES, EXPRESS OR IMPLIED. By way of example, but not
27 * limitation, M.I.T. MAKES NO REPRESENTATIONS OR WARRANTIES OF
28 * MERCHANTABILITY OR FITNESS FOR ANY PARTICULAR PURPOSE OR THAT THE USE OF
29 * THE LICENSED SOFTWARE OR DOCUMENTATION WILL NOT INFRINGE ANY THIRD PARTY
30 * PATENTS, COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS.
32 * The name of the Massachusetts Institute of Technology or M.I.T. may NOT
33 * be used in advertising or publicity pertaining to distribution of the
34 * software. Title to copyright in this software and any associated
35 * documentation shall at all times remain with M.I.T., and USER agrees to
38 * Furthermore if you modify this software you must label
39 * your software as modified software and not distribute it in such a
40 * fashion that it might be confused with the original M.I.T. software.
46 * If a system version is available but buggy, save handles to it,
47 * redefine the names to refer to static functions defined here, and
48 * in those functions, call the system versions and fix up the
49 * returned data. Use the native data structures and flag values.
51 * If no system version exists, use gethostby* and fake it. Define
52 * the data structures and flag values locally.
55 * On macOS, getaddrinfo results aren't cached (though
56 * gethostbyname results are), so we need to build a cache here. Now
57 * things are getting really messy. Because the cache is in use, we
58 * use getservbyname, and throw away thread safety. (Not that the
59 * cache is thread safe, but when we get locking support, that'll be
60 * dealt with.) This code needs tearing down and rebuilding, soon.
63 * Note that recent Windows developers' code has an interesting hack:
64 * When you include the right header files, with the right set of
65 * macros indicating system versions, you'll get an inline function
66 * that looks for getaddrinfo (or whatever) in the system library, and
67 * calls it if it's there. If it's not there, it fakes it with
70 * We're taking a simpler approach: A system provides these routines or
73 * Someday, we may want to take into account different versions (say,
74 * different revs of GNU libc) where some are broken in one way, and
75 * some work or are broken in another way. Cross that bridge when we
82 * + For AIX 4.3.3, using the RFC 2133 definition: Implement
83 * AI_NUMERICHOST. It's not defined in the header file.
85 * For certain (old?) versions of GNU libc, AI_NUMERICHOST is
86 * defined but not implemented.
88 * + Use gethostbyname2, inet_aton and other IPv6 or thread-safe
89 * functions if available. But, see
90 * http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=135182 for one
91 * gethostbyname2 problem on Linux. And besides, if a platform is
92 * supporting IPv6 at all, they really should be doing getaddrinfo
95 * + inet_ntop, inet_pton
97 * + Conditionally export/import the function definitions, so a
98 * library can have a single copy instead of multiple.
100 * + Upgrade host requirements to include working implementations of
101 * these functions, and throw all this away. Pleeease? :-)
104 #include "k5-platform.h"
105 #include "k5-thread.h"
106 #include "port-sockets.h"
107 #include "socket-utils.h"
108 #include "supp-int.h"
110 #define IMPLEMENT_FAKE_GETADDRINFO
111 #include "fake-addrinfo.h"
116 getaddrinfo (/*@in@*/ /*@null@*/ const char *,
117 /*@in@*/ /*@null@*/ const char *,
118 /*@in@*/ /*@null@*/ const struct addrinfo *,
119 /*@out@*/ struct addrinfo **)
122 freeaddrinfo (/*@only@*/ /*@out@*/ struct addrinfo *)
125 getnameinfo (const struct sockaddr *addr, socklen_t addrsz,
126 /*@out@*/ /*@null@*/ char *h, socklen_t hsz,
127 /*@out@*/ /*@null@*/ char *s, socklen_t ssz,
129 /*@requires (maxSet(h)+1) >= hsz /\ (maxSet(s)+1) >= ssz @*/
130 /* too hard: maxRead(addr) >= (addrsz-1) */
131 /*@modifies *h, *s@*/;
132 extern /*@dependent@*/ char *gai_strerror (int code) /*@*/;
137 #include "cache-addrinfo.h"
139 #if (defined (__linux__) && defined(HAVE_GETADDRINFO)) || defined (_AIX)
140 /* See comments below. */
141 # define WRAP_GETADDRINFO
144 #if defined (__linux__) && defined(HAVE_GETADDRINFO)
145 /* Define COPY_FIRST_CANONNAME for glibc 2.3 and prior. */
146 #include <features.h>
147 # ifdef __GLIBC_PREREQ
148 # if ! __GLIBC_PREREQ(2, 4)
149 # define COPY_FIRST_CANONNAME
152 # define COPY_FIRST_CANONNAME
157 # define NUMERIC_SERVICE_BROKEN
158 # define COPY_FIRST_CANONNAME
162 #ifdef COPY_FIRST_CANONNAME
166 #ifdef NUMERIC_SERVICE_BROKEN
167 # include <ctype.h> /* isdigit */
168 # include <stdlib.h> /* strtoul */
172 /* Do we actually have *any* systems we care about that don't provide
173 either getaddrinfo or one of these two flavors of
175 #if !defined(HAVE_GETHOSTBYNAME_R) || defined(THREADSAFE_GETHOSTBYNAME)
176 typedef struct hostent *GET_HOST_TMP;
177 #define GET_HOST_BY_NAME(NAME, HP, ERR, TMP) \
178 { TMP = gethostbyname (NAME); (ERR) = h_errno; (HP) = TMP; }
179 #define GET_HOST_BY_ADDR(ADDR, ADDRLEN, FAMILY, HP, ERR, TMP) \
180 { TMP = gethostbyaddr ((ADDR), (ADDRLEN), (FAMILY)); (ERR) = h_errno; (HP) = TMP; }
182 #ifdef _AIX /* XXX should have a feature test! */
185 struct hostent_data data;
187 #define GET_HOST_BY_NAME(NAME, HP, ERR, TMP) \
189 (HP) = (gethostbyname_r((NAME), &TMP.ent, &TMP.data) \
195 #define GET_HOST_BY_ADDR(ADDR, ADDRLEN, FAMILY, HP, ERR) \
197 struct hostent my_h_ent; \
198 struct hostent_data my_h_ent_data; \
199 (HP) = (gethostbyaddr_r((ADDR), (ADDRLEN), (FAMILY), &my_h_ent, \
207 #ifdef GETHOSTBYNAME_R_RETURNS_INT
212 #define GET_HOST_BY_NAME(NAME, HP, ERR, TMP) \
214 struct hostent *my_hp = NULL; \
215 int my_h_err, my_ret; \
216 my_ret = gethostbyname_r((NAME), &TMP.ent, \
217 TMP.buf, sizeof (TMP.buf), &my_hp, \
219 (HP) = (((my_ret != 0) || (my_hp != &TMP.ent)) \
224 #define GET_HOST_BY_ADDR(ADDR, ADDRLEN, FAMILY, HP, ERR, TMP) \
226 struct hostent *my_hp; \
227 int my_h_err, my_ret; \
228 my_ret = gethostbyaddr_r((ADDR), (ADDRLEN), (FAMILY), &TMP.ent, \
229 TMP.buf, sizeof (TMP.buf), &my_hp, \
231 (HP) = (((my_ret != 0) || (my_hp != &TMP.ent)) \
241 #define GET_HOST_BY_NAME(NAME, HP, ERR, TMP) \
244 (HP) = gethostbyname_r((NAME), &TMP.ent, \
245 TMP.buf, sizeof (TMP.buf), &my_h_err); \
248 #define GET_HOST_BY_ADDR(ADDR, ADDRLEN, FAMILY, HP, ERR, TMP) \
251 (HP) = gethostbyaddr_r((ADDR), (ADDRLEN), (FAMILY), &TMP.ent, \
252 TMP.buf, sizeof (TMP.buf), &my_h_err); \
255 #endif /* returns int? */
259 /* Now do the same for getservby* functions. */
260 #ifndef HAVE_GETSERVBYNAME_R
261 typedef struct servent *GET_SERV_TMP;
262 #define GET_SERV_BY_NAME(NAME, PROTO, SP, ERR, TMP) \
263 (TMP = getservbyname (NAME, PROTO), (SP) = TMP, (ERR) = (SP) ? 0 : -1)
264 #define GET_SERV_BY_PORT(PORT, PROTO, SP, ERR, TMP) \
265 (TMP = getservbyport (PORT, PROTO), (SP) = TMP, (ERR) = (SP) ? 0 : -1)
267 #ifdef GETSERVBYNAME_R_RETURNS_INT
272 #define GET_SERV_BY_NAME(NAME, PROTO, SP, ERR, TMP) \
274 struct servent *my_sp; \
276 (SP) = (getservbyname_r((NAME), (PROTO), &TMP.ent, \
277 TMP.buf, sizeof (TMP.buf), &my_sp, \
283 #define GET_SERV_BY_PORT(PORT, PROTO, SP, ERR, TMP) \
285 struct servent *my_sp; \
287 (SP) = (getservbyport_r((PORT), (PROTO), &TMP.ent, \
288 TMP.buf, sizeof (TMP.buf), &my_sp, \
295 /* returns ptr -- IRIX? */
300 #define GET_SERV_BY_NAME(NAME, PROTO, SP, ERR, TMP) \
302 (SP) = getservbyname_r((NAME), (PROTO), &TMP.ent, \
303 TMP.buf, sizeof (TMP.buf)); \
304 (ERR) = (SP) == NULL; \
307 #define GET_SERV_BY_PORT(PORT, PROTO, SP, ERR, TMP) \
309 struct servent *my_sp; \
310 my_sp = getservbyport_r((PORT), (PROTO), &TMP.ent, \
311 TMP.buf, sizeof (TMP.buf)); \
313 (ERR) = my_sp == 0; \
314 (ERR) = (ERR); /* avoid "unused" warning */ \
319 #if defined(WRAP_GETADDRINFO) || defined(FAI_CACHE)
321 system_getaddrinfo (const char *name, const char *serv,
322 const struct addrinfo *hint,
323 struct addrinfo **res)
325 return getaddrinfo(name, serv, hint, res);
329 system_freeaddrinfo (struct addrinfo *ai)
336 #if !defined (HAVE_GETADDRINFO) || defined(WRAP_GETADDRINFO) || defined(FAI_CACHE)
339 #define getaddrinfo my_fake_getaddrinfo
341 #define freeaddrinfo my_fake_freeaddrinfo
345 #if !defined (HAVE_GETADDRINFO)
348 #define gai_strerror my_fake_gai_strerror
350 #endif /* ! HAVE_GETADDRINFO */
352 #if (!defined (HAVE_GETADDRINFO) || defined (WRAP_GETADDRINFO)) && defined(DEBUG_ADDRINFO)
353 /* Some debug routines. */
355 static const char *protoname (int p, char *buf, size_t bufsize) {
356 #define X(N) if (p == IPPROTO_ ## N) return #N
374 snprintf(buf, bufsize, " %-2d", p);
378 static const char *socktypename (int t, char *buf, size_t bufsize) {
380 case SOCK_DGRAM: return "DGRAM";
381 case SOCK_STREAM: return "STREAM";
382 case SOCK_RAW: return "RAW";
383 case SOCK_RDM: return "RDM";
384 case SOCK_SEQPACKET: return "SEQPACKET";
386 snprintf(buf, bufsize, " %-2d", t);
390 static const char *familyname (int f, char *buf, size_t bufsize) {
393 snprintf(buf, bufsize, "AF %d", f);
395 case AF_INET: return "AF_INET";
396 case AF_INET6: return "AF_INET6";
398 case AF_UNIX: return "AF_UNIX";
403 static void debug_dump_getaddrinfo_args (const char *name, const char *serv,
404 const struct addrinfo *hint)
408 "getaddrinfo(hostname %s, service %s,\n"
410 name ? name : "(null)", serv ? serv : "(null)");
414 #define Z(FLAG) if (hint->ai_flags & AI_##FLAG) fprintf(stderr, "%s%s", sep, #FLAG), sep = "|"
417 #ifdef AI_NUMERICHOST
421 fprintf(stderr, "no-flags");
423 fprintf(stderr, " %s", familyname(hint->ai_family, buf,
425 if (hint->ai_socktype)
426 fprintf(stderr, " SOCK_%s", socktypename(hint->ai_socktype, buf,
428 if (hint->ai_protocol)
429 fprintf(stderr, " IPPROTO_%s", protoname(hint->ai_protocol, buf,
432 fprintf(stderr, "(null)");
433 fprintf(stderr, " }):\n");
436 static void debug_dump_error (int err)
438 fprintf(stderr, "error %d: %s\n", err, gai_strerror(err));
441 static void debug_dump_addrinfos (const struct addrinfo *ai)
445 fprintf(stderr, "addrinfos returned:\n");
447 fprintf(stderr, "%p...", ai);
448 fprintf(stderr, " socktype=%s", socktypename(ai->ai_socktype, buf,
450 fprintf(stderr, " ai_family=%s", familyname(ai->ai_family, buf,
452 if (ai->ai_family != ai->ai_addr->sa_family)
453 fprintf(stderr, " sa_family=%s",
454 familyname(ai->ai_addr->sa_family, buf, sizeof(buf)));
455 fprintf(stderr, "\n");
459 fprintf(stderr, "end addrinfos returned (%d)\n");
464 #if !defined (HAVE_GETADDRINFO) || defined (WRAP_GETADDRINFO)
467 int getaddrinfo (const char *name, const char *serv,
468 const struct addrinfo *hint, struct addrinfo **result);
471 void freeaddrinfo (struct addrinfo *ai);
475 #if !defined (HAVE_GETADDRINFO)
477 #define HAVE_FAKE_GETADDRINFO /* was not originally HAVE_GETADDRINFO */
478 #define HAVE_GETADDRINFO
479 #define NEED_FAKE_GETNAMEINFO
480 #undef HAVE_GETNAMEINFO
481 #define HAVE_GETNAMEINFO 1
484 #define getnameinfo my_fake_getnameinfo
487 char *gai_strerror (int code);
491 #if !defined (HAVE_GETADDRINFO)
493 int getnameinfo (const struct sockaddr *addr, socklen_t len,
494 char *host, socklen_t hostlen,
495 char *service, socklen_t servicelen,
499 /* Fudge things on older gai implementations. */
500 /* AIX 4.3.3 is based on RFC 2133; no AI_NUMERICHOST. */
501 #ifndef AI_NUMERICHOST
502 # define AI_NUMERICHOST 0
504 /* Partial RFC 2553 implementations may not have AI_ADDRCONFIG and
505 friends, which RFC 3493 says are now part of the getaddrinfo
506 interface, and we'll want to use. */
507 #ifndef AI_ADDRCONFIG
508 # define AI_ADDRCONFIG 0
511 # define AI_V4MAPPED 0
517 # define AI_DEFAULT (AI_ADDRCONFIG|AI_V4MAPPED)
520 #if defined(HAVE_FAKE_GETADDRINFO) || defined(FAI_CACHE)
521 #define NEED_FAKE_GETADDRINFO
524 #if defined(NEED_FAKE_GETADDRINFO) || defined(WRAP_GETADDRINFO)
528 #ifdef NEED_FAKE_GETADDRINFO
529 #include <string.h> /* for strspn */
531 static inline int translate_h_errno (int h);
533 static inline int fai_add_entry (struct addrinfo **result, void *addr,
534 int port, const struct addrinfo *template)
536 struct addrinfo *n = malloc (sizeof (struct addrinfo));
539 if (template->ai_family != AF_INET
540 && template->ai_family != AF_INET6
544 if (template->ai_family == AF_INET) {
545 struct sockaddr_in *sin4;
546 sin4 = malloc (sizeof (struct sockaddr_in));
549 memset (sin4, 0, sizeof (struct sockaddr_in)); /* for sin_zero */
550 n->ai_addr = (struct sockaddr *) sin4;
551 sin4->sin_family = AF_INET;
552 sin4->sin_addr = *(struct in_addr *)addr;
553 sin4->sin_port = port;
555 if (template->ai_family == AF_INET6) {
556 struct sockaddr_in6 *sin6;
557 sin6 = malloc (sizeof (struct sockaddr_in6));
560 memset (sin6, 0, sizeof (struct sockaddr_in6)); /* for sin_zero */
561 n->ai_addr = (struct sockaddr *) sin6;
562 sin6->sin6_family = AF_INET6;
563 sin6->sin6_addr = *(struct in6_addr *)addr;
564 sin6->sin6_port = port;
566 n->ai_next = *result;
572 /* fake addrinfo cache entries */
573 #define CACHE_ENTRY_LIFETIME 15 /* seconds */
575 static void plant_face (const char *name, struct face *entry)
577 entry->name = strdup(name);
578 if (entry->name == NULL)
579 /* @@ Wastes memory. */
581 k5_mutex_assert_locked(&krb5int_fac.lock);
582 entry->next = krb5int_fac.data;
583 entry->expiration = time(0) + CACHE_ENTRY_LIFETIME;
584 krb5int_fac.data = entry;
585 #ifdef DEBUG_ADDRINFO
586 printf("added cache entry '%s' at %p: %d ipv4, %d ipv6; expire %d\n",
587 name, entry, entry->naddrs4, entry->naddrs6, entry->expiration);
591 static int find_face (const char *name, struct face **entry)
593 struct face *fp, **fpp;
594 time_t now = time(0);
596 /* First, scan for expired entries and free them.
597 (Future improvement: Integrate these two loops.) */
598 #ifdef DEBUG_ADDRINFO
599 printf("scanning cache at %d for '%s'...\n", now, name);
601 k5_mutex_assert_locked(&krb5int_fac.lock);
602 for (fpp = &krb5int_fac.data; *fpp; ) {
604 #ifdef DEBUG_ADDRINFO
605 printf(" checking expiration time of @%p: %d\n",
608 if (fp->expiration < now) {
609 #ifdef DEBUG_ADDRINFO
610 printf("\texpiring cache entry\n");
618 /* Stay at this point in the list, and check again. */
624 for (fp = krb5int_fac.data; fp; fp = fp->next) {
625 #ifdef DEBUG_ADDRINFO
626 printf(" comparing entry @%p\n", fp);
628 if (!strcasecmp(fp->name, name)) {
629 #ifdef DEBUG_ADDRINFO
630 printf("\tMATCH!\n");
642 static int krb5int_lock_fac(void), krb5int_unlock_fac(void);
645 static inline int fai_add_hosts_by_name (const char *name,
646 struct addrinfo *template,
647 int portnum, int flags,
648 struct addrinfo **result)
655 err = krb5int_lock_fac();
660 if (!find_face(name, &ce)) {
661 struct addrinfo myhints = { 0 }, *ai, *ai2;
664 #ifdef DEBUG_ADDRINFO
665 printf("looking up new data for '%s'...\n", name);
667 myhints.ai_socktype = SOCK_STREAM;
668 myhints.ai_flags = AI_CANONNAME;
669 /* Don't set ai_family -- we want to cache all address types,
670 because the next lookup may not use the same constraints as
671 the current one. We *could* cache them separately, so that
672 we never have to look up an IPv6 address if we are always
673 asked for IPv4 only, but let's deal with that later, if we
675 /* Try NULL for the service for now.
677 It would be nice to use the requested service name, and not
678 have to patch things up, but then we'd be doing multiple
679 queries for the same host when we get different services.
680 We were using "telnet" for a little more confidence that
681 getaddrinfo would heed the hints to only give us stream
682 socket types (with no socket type and null service name, we
683 might get stream *and* dgram *and* raw, for each address,
684 or only raw). The RFC 3493 description of ai_socktype
685 sometimes associates it with the specified service,
688 But on macOS (10.3, 10.4) they've "extended" getaddrinfo
689 to make SRV RR queries. (Please, somebody, show me
690 something in the specs that actually supports this? RFC
691 3493 says nothing about it, but it does say getaddrinfo is
692 the new way to look up hostnames. RFC 2782 says SRV
693 records should *not* be used unless the application
694 protocol spec says to do so. The Telnet spec does not say
695 to do it.) And then they complain when our code
696 "unexpectedly" seems to use this "extension" in cases where
697 they don't want it to be used.
699 Fortunately, it appears that if we specify ai_socktype as
700 SOCK_STREAM and use a null service name, we only get one
701 copy of each address on all the platforms I've tried,
702 although it may not have ai_socktype filled in properly.
703 So, we'll fudge it with that for now. */
704 aierr = system_getaddrinfo(name, NULL, &myhints, &ai);
706 krb5int_unlock_fac();
709 ce = malloc(sizeof(struct face));
710 memset(ce, 0, sizeof(*ce));
711 ce->expiration = time(0) + 30;
712 for (ai2 = ai; ai2; ai2 = ai2->ai_next) {
713 #ifdef DEBUG_ADDRINFO
714 printf(" found an address in family %d...\n", ai2->ai_family);
716 switch (ai2->ai_family) {
727 ce->addrs4 = calloc(ce->naddrs4, sizeof(*ce->addrs4));
728 if (ce->addrs4 == NULL && ce->naddrs4 != 0) {
729 krb5int_unlock_fac();
730 system_freeaddrinfo(ai);
733 ce->addrs6 = calloc(ce->naddrs6, sizeof(*ce->addrs6));
734 if (ce->addrs6 == NULL && ce->naddrs6 != 0) {
735 krb5int_unlock_fac();
737 system_freeaddrinfo(ai);
740 for (ai2 = ai, i4 = i6 = 0; ai2; ai2 = ai2->ai_next) {
741 switch (ai2->ai_family) {
743 ce->addrs4[i4++] = ((struct sockaddr_in *)ai2->ai_addr)->sin_addr;
746 ce->addrs6[i6++] = ((struct sockaddr_in6 *)ai2->ai_addr)->sin6_addr;
752 ce->canonname = ai->ai_canonname ? strdup(ai->ai_canonname) : 0;
753 system_freeaddrinfo(ai);
754 plant_face(name, ce);
756 template->ai_family = AF_INET6;
757 template->ai_addrlen = sizeof(struct sockaddr_in6);
758 for (i = 0; i < ce->naddrs6; i++) {
759 r = fai_add_entry (result, &ce->addrs6[i], portnum, template);
761 krb5int_unlock_fac();
765 template->ai_family = AF_INET;
766 template->ai_addrlen = sizeof(struct sockaddr_in);
767 for (i = 0; i < ce->naddrs4; i++) {
768 r = fai_add_entry (result, &ce->addrs4[i], portnum, template);
770 krb5int_unlock_fac();
774 if (*result && (flags & AI_CANONNAME))
775 (*result)->ai_canonname = (ce->canonname
776 ? strdup(ce->canonname)
778 krb5int_unlock_fac();
788 GET_HOST_BY_NAME (name, hp, herr, htmp);
790 return translate_h_errno (herr);
791 for (i = 0; hp->h_addr_list[i]; i++) {
792 r = fai_add_entry (result, hp->h_addr_list[i], portnum, template);
796 if (*result && (flags & AI_CANONNAME))
797 (*result)->ai_canonname = strdup (hp->h_name);
804 fake_freeaddrinfo (struct addrinfo *ai)
806 struct addrinfo *next;
809 if (ai->ai_canonname)
810 free (ai->ai_canonname);
819 fake_getaddrinfo (const char *name, const char *serv,
820 const struct addrinfo *hint, struct addrinfo **result)
822 struct addrinfo *res = 0;
824 int port = 0, socktype;
826 struct addrinfo template;
828 #ifdef DEBUG_ADDRINFO
829 debug_dump_getaddrinfo_args(name, serv, hint);
833 if (hint->ai_family != 0 && hint->ai_family != AF_INET)
835 socktype = hint->ai_socktype;
836 flags = hint->ai_flags;
843 size_t numlen = strspn (serv, "0123456789");
844 if (serv[numlen] == '\0') {
846 unsigned long p = strtoul (serv, 0, 10);
847 if (p == 0 || p > 65535)
852 int try_dgram_too = 0, s_err;
857 socktype = SOCK_STREAM;
860 GET_SERV_BY_NAME(serv, socktype == SOCK_STREAM ? "tcp" : "udp",
864 socktype = SOCK_DGRAM;
865 goto try_service_lookup;
874 name = (flags & AI_PASSIVE) ? "0.0.0.0" : "127.0.0.1";
875 flags |= AI_NUMERICHOST;
878 template.ai_family = AF_INET;
879 template.ai_addrlen = sizeof (struct sockaddr_in);
880 template.ai_socktype = socktype;
881 template.ai_protocol = 0;
882 template.ai_flags = 0;
883 template.ai_canonname = 0;
884 template.ai_next = 0;
885 template.ai_addr = 0;
887 /* If NUMERICHOST is set, parse a numeric address.
888 If it's not set, don't accept such names. */
889 if (flags & AI_NUMERICHOST) {
890 struct in_addr addr4;
891 addr4.s_addr = inet_addr (name);
892 if (addr4.s_addr == 0xffffffff || addr4.s_addr == -1)
893 /* 255.255.255.255 or parse error, both bad */
895 ret = fai_add_entry (&res, &addr4, port, &template);
897 ret = fai_add_hosts_by_name (name, &template, port, flags,
901 if (ret && ret != NO_ADDRESS) {
902 fake_freeaddrinfo (res);
911 #ifdef NEED_FAKE_GETNAMEINFO
913 fake_getnameinfo (const struct sockaddr *sa, socklen_t len,
914 char *host, socklen_t hostlen,
915 char *service, socklen_t servicelen,
919 const struct sockaddr_in *sinp;
923 if (sa->sa_family != AF_INET) {
926 sinp = (const struct sockaddr_in *) sa;
929 if (hostlen < 0 || hlen != hostlen) {
934 if (servicelen < 0 || slen != servicelen) {
940 if (flags & NI_NUMERICHOST) {
941 #if (defined(__GNUC__) && defined(__mips__)) || 1 /* thread safety always */
942 /* The inet_ntoa call, passing a struct, fails on IRIX 6.5
943 using gcc 2.95; we get back "0.0.0.0". Since this in a
944 configuration still important at Athena, here's the
945 workaround, which also happens to be thread-safe.... */
946 const unsigned char *uc;
949 uc = (const unsigned char *) &sinp->sin_addr;
950 snprintf(tmpbuf, sizeof(tmpbuf), "%d.%d.%d.%d",
951 uc[0], uc[1], uc[2], uc[3]);
952 strncpy(host, tmpbuf, hlen);
956 p = inet_ntoa (sinp->sin_addr);
957 strncpy (host, p, hlen);
963 GET_HOST_BY_ADDR((const char *) &sinp->sin_addr,
964 sizeof (struct in_addr),
965 sa->sa_family, hp, herr, htmp);
967 if (herr == NO_ADDRESS && !(flags & NI_NAMEREQD)) /* ??? */
969 return translate_h_errno (herr);
971 /* According to the Open Group spec, getnameinfo can
972 silently truncate, but must still return a
973 null-terminated string. */
974 strncpy (host, hp->h_name, hlen);
980 if (flags & NI_NUMERICSERV) {
984 port = ntohs (sinp->sin_port);
985 if (port < 0 || port > 65535)
987 snprintf (numbuf, sizeof(numbuf), "%d", port);
988 strncpy (service, numbuf, slen);
993 GET_SERV_BY_PORT(sinp->sin_port,
994 (flags & NI_DGRAM) ? "udp" : "tcp",
997 goto numeric_service;
998 strncpy (service, sp->s_name, slen);
1000 service[servicelen-1] = 0;
1007 #if defined(HAVE_FAKE_GETADDRINFO) || defined(NEED_FAKE_GETNAMEINFO)
1010 char *gai_strerror (int code)
1013 case EAI_ADDRFAMILY: return "address family for nodename not supported";
1014 case EAI_AGAIN: return "temporary failure in name resolution";
1015 case EAI_BADFLAGS: return "bad flags to getaddrinfo/getnameinfo";
1016 case EAI_FAIL: return "non-recoverable failure in name resolution";
1017 case EAI_FAMILY: return "ai_family not supported";
1018 case EAI_MEMORY: return "out of memory";
1019 case EAI_NODATA: return "no address associated with hostname";
1020 case EAI_NONAME: return "name does not exist";
1021 case EAI_SERVICE: return "service name not supported for specified socket type";
1022 case EAI_SOCKTYPE: return "ai_socktype not supported";
1023 case EAI_SYSTEM: return strerror (errno);
1024 default: return "bogus getaddrinfo error?";
1029 static inline int translate_h_errno (int h)
1034 #ifdef NETDB_INTERNAL
1035 case NETDB_INTERNAL:
1036 if (errno == ENOMEM)
1040 case HOST_NOT_FOUND:
1047 #if NO_DATA != NO_ADDRESS
1056 #if defined(HAVE_FAKE_GETADDRINFO) || defined(FAI_CACHE)
1058 int getaddrinfo (const char *name, const char *serv,
1059 const struct addrinfo *hint, struct addrinfo **result)
1061 return fake_getaddrinfo(name, serv, hint, result);
1065 void freeaddrinfo (struct addrinfo *ai)
1067 fake_freeaddrinfo(ai);
1070 #ifdef NEED_FAKE_GETNAMEINFO
1072 int getnameinfo (const struct sockaddr *sa, socklen_t len,
1073 char *host, socklen_t hostlen,
1074 char *service, socklen_t servicelen,
1077 return fake_getnameinfo(sa, len, host, hostlen, service, servicelen,
1080 #endif /* NEED_FAKE_GETNAMEINFO */
1081 #endif /* HAVE_FAKE_GETADDRINFO */
1082 #endif /* NEED_FAKE_GETADDRINFO */
1085 #ifdef WRAP_GETADDRINFO
1089 getaddrinfo (const char *name, const char *serv, const struct addrinfo *hint,
1090 struct addrinfo **result)
1093 #if defined(_AIX) || defined(COPY_FIRST_CANONNAME)
1094 struct addrinfo *ai;
1096 #ifdef NUMERIC_SERVICE_BROKEN
1097 int service_is_numeric = 0;
1098 int service_port = 0;
1099 int socket_type = 0;
1102 #ifdef DEBUG_ADDRINFO
1103 debug_dump_getaddrinfo_args(name, serv, hint);
1106 #ifdef NUMERIC_SERVICE_BROKEN
1107 /* AIX 4.3.3 is broken. (Or perhaps out of date?)
1109 If a numeric service is provided, and it doesn't correspond to
1110 a known service name for tcp or udp (as appropriate), an error
1111 code (for "host not found") is returned. If the port maps to a
1112 known service for both udp and tcp, all is well. */
1113 if (serv && serv[0] && isdigit(serv[0])) {
1114 unsigned long lport;
1116 lport = strtoul(serv, &end, 10);
1119 return EAI_SOCKTYPE;
1120 service_is_numeric = 1;
1121 service_port = lport;
1122 #ifdef AI_NUMERICSERV
1123 if (hint && hint->ai_flags & AI_NUMERICSERV)
1127 serv = "discard"; /* defined for both udp and tcp */
1129 socket_type = hint->ai_socktype;
1134 aierr = system_getaddrinfo (name, serv, hint, result);
1135 if (aierr || *result == 0) {
1136 #ifdef DEBUG_ADDRINFO
1137 debug_dump_error(aierr);
1142 /* Linux libc version 6 prior to 2.3.4 is broken.
1144 RFC 2553 says that when AI_CANONNAME is set, the ai_canonname
1145 flag of the first returned structure has the canonical name of
1146 the host. Instead, GNU libc sets ai_canonname in each returned
1147 structure to the name that the corresponding address maps to,
1148 if any, or a printable numeric form.
1150 RFC 2553 bis and the new Open Group spec say that field will be
1151 the canonical name if it can be determined, otherwise, the
1152 provided hostname or a copy of it.
1154 IMNSHO, "canonical name" means CNAME processing and not PTR
1155 processing, but I can see arguing it. Using the numeric form
1156 when that's not the form provided is just wrong. So, let's fix
1159 The glibc 2.2.5 sources indicate that the canonical name is
1160 *not* allocated separately, it's just some extra storage tacked
1161 on the end of the addrinfo structure. So, let's try this
1162 approach: If getaddrinfo sets ai_canonname, we'll replace the
1163 *first* one with allocated storage, and free up that pointer in
1164 freeaddrinfo if it's set; the other ai_canonname fields will be
1165 left untouched. And we'll just pray that the application code
1166 won't mess around with the list structure; if we start doing
1167 that, we'll have to start replacing and freeing all of the
1168 ai_canonname fields.
1170 Ref: http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=133668 .
1172 Since it's dependent on the target hostname, it's hard to check
1173 for at configure time. The bug was fixed in glibc 2.3.4.
1174 After the fix, the ai_canonname field is allocated, so our
1175 workaround leaks memory. We disable the workaround for glibc
1176 >= 2.4, but there is no easy way to test for glibc patch
1177 versions, so we still leak memory under glibc 2.3.4 through
1180 Some Windows documentation says that even when AI_CANONNAME is
1181 set, the returned ai_canonname field can be null. The NetBSD
1182 1.5 implementation also does this, if the input hostname is a
1183 numeric host address string. That case isn't handled well at
1186 Libc version 5 didn't have getaddrinfo at all. */
1188 #ifdef COPY_FIRST_CANONNAME
1190 * This code must *always* return an error, return a null
1191 * ai_canonname, or return an ai_canonname allocated here using
1192 * malloc, so that freeaddrinfo can always free a non-null
1193 * ai_canonname. Note that it really doesn't matter if the
1194 * AI_CANONNAME flag was set.
1197 if (ai->ai_canonname) {
1199 const char *name2 = 0;
1204 * Current versions of GET_HOST_BY_NAME will fail if the
1205 * target hostname has IPv6 addresses only. Make sure it
1206 * fails fairly cleanly.
1208 GET_HOST_BY_NAME (name, hp, herr, htmp);
1211 * This case probably means it's an IPv6-only name. If
1212 * ai_canonname is a numeric address, get rid of it.
1214 if (ai->ai_canonname && strchr(ai->ai_canonname, ':'))
1215 ai->ai_canonname = 0;
1216 name2 = ai->ai_canonname ? ai->ai_canonname : name;
1219 * Sometimes gethostbyname will be directed to /etc/hosts
1220 * first, and sometimes that file will have entries with
1221 * the unqualified name first. So take the first entry
1222 * that looks like it could be a FQDN. Starting with h_name
1223 * and then all the aliases.
1225 for (i = 0, name2 = hp->h_name; name2; i++) {
1226 if (strchr(name2, '.') != 0)
1228 name2 = hp->h_aliases[i];
1234 ai->ai_canonname = strdup(name2);
1235 if (name2 != 0 && ai->ai_canonname == 0) {
1236 system_freeaddrinfo(ai);
1238 #ifdef DEBUG_ADDRINFO
1239 debug_dump_error(EAI_MEMORY);
1243 /* Zap the remaining ai_canonname fields glibc fills in, in
1244 case the application messes around with the list
1246 while ((ai = ai->ai_next) != NULL)
1247 ai->ai_canonname = 0;
1251 #ifdef NUMERIC_SERVICE_BROKEN
1252 if (service_port != 0) {
1253 for (ai = *result; ai; ai = ai->ai_next) {
1254 if (socket_type != 0 && ai->ai_socktype == 0)
1255 /* Is this check actually needed? */
1256 ai->ai_socktype = socket_type;
1257 sa_setport(ai->ai_addr, service_port);
1263 for (ai = *result; ai; ai = ai->ai_next) {
1264 /* AIX 4.3.3 libc is broken. It doesn't set the family or len
1265 fields of the sockaddr structures. Usually, sa_family is
1266 zero, but I've seen it set to 1 in some cases also (maybe
1267 just leftover from previous contents of the memory
1268 block?). So, always override what libc returned. */
1269 ai->ai_addr->sa_family = ai->ai_family;
1273 /* Not dealt with currently:
1275 - Some versions of GNU libc can lose some IPv4 addresses in
1276 certain cases when multiple IPv4 and IPv6 addresses are
1279 #ifdef DEBUG_ADDRINFO
1280 debug_dump_addrinfos(*result);
1287 void freeaddrinfo (struct addrinfo *ai)
1289 #ifdef COPY_FIRST_CANONNAME
1291 free(ai->ai_canonname);
1292 ai->ai_canonname = 0;
1293 system_freeaddrinfo(ai);
1296 system_freeaddrinfo(ai);
1299 #endif /* WRAP_GETADDRINFO */
1302 static int krb5int_lock_fac (void)
1305 err = krb5int_call_thread_support_init();
1308 return k5_mutex_lock(&krb5int_fac.lock);
1311 static int krb5int_unlock_fac (void)
1313 return k5_mutex_unlock(&krb5int_fac.lock);
1317 /* Some systems don't define in6addr_any. */
1318 const struct in6_addr krb5int_in6addr_any = IN6ADDR_ANY_INIT;
1320 int krb5int_getaddrinfo (const char *node, const char *service,
1321 const struct addrinfo *hints,
1322 struct addrinfo **aip)
1324 return getaddrinfo(node, service, hints, aip);
1327 void krb5int_freeaddrinfo (struct addrinfo *ai)
1332 const char *krb5int_gai_strerror(int err)
1334 return gai_strerror(err);
1337 int krb5int_getnameinfo (const struct sockaddr *sa, socklen_t salen,
1338 char *hbuf, size_t hbuflen,
1339 char *sbuf, size_t sbuflen,
1342 return getnameinfo(sa, salen, hbuf, hbuflen, sbuf, sbuflen, flags);