17c478bd9Sstevel@tonic-gate /*
2505d05c7Sgtb  * Copyright (C) 2001,2002,2003,2004 by the Massachusetts Institute of Technology,
37c478bd9Sstevel@tonic-gate  * Cambridge, MA, USA.  All Rights Reserved.
4*55fea89dSDan Cross  *
5*55fea89dSDan Cross  * This software is being provided to you, the LICENSEE, by the
6*55fea89dSDan Cross  * Massachusetts Institute of Technology (M.I.T.) under the following
7*55fea89dSDan Cross  * license.  By obtaining, using and/or copying this software, you agree
8*55fea89dSDan Cross  * that you have read, understood, and will comply with these terms and
9*55fea89dSDan Cross  * conditions:
10*55fea89dSDan Cross  *
117c478bd9Sstevel@tonic-gate  * Export of this software from the United States of America may
127c478bd9Sstevel@tonic-gate  * require a specific license from the United States Government.
137c478bd9Sstevel@tonic-gate  * It is the responsibility of any person or organization contemplating
147c478bd9Sstevel@tonic-gate  * export to obtain such a license before exporting.
15*55fea89dSDan Cross  *
16*55fea89dSDan Cross  * WITHIN THAT CONSTRAINT, permission to use, copy, modify and distribute
17*55fea89dSDan Cross  * this software and its documentation for any purpose and without fee or
18*55fea89dSDan Cross  * royalty is hereby granted, provided that you agree to comply with the
19*55fea89dSDan Cross  * following copyright notice and statements, including the disclaimer, and
20*55fea89dSDan Cross  * that the same appear on ALL copies of the software and documentation,
21*55fea89dSDan Cross  * including modifications that you make for internal use or for
227c478bd9Sstevel@tonic-gate  * distribution:
23*55fea89dSDan Cross  *
24*55fea89dSDan Cross  * THIS SOFTWARE IS PROVIDED "AS IS", AND M.I.T. MAKES NO REPRESENTATIONS
25*55fea89dSDan Cross  * OR WARRANTIES, EXPRESS OR IMPLIED.  By way of example, but not
26*55fea89dSDan Cross  * limitation, M.I.T. MAKES NO REPRESENTATIONS OR WARRANTIES OF
27*55fea89dSDan Cross  * MERCHANTABILITY OR FITNESS FOR ANY PARTICULAR PURPOSE OR THAT THE USE OF
28*55fea89dSDan Cross  * THE LICENSED SOFTWARE OR DOCUMENTATION WILL NOT INFRINGE ANY THIRD PARTY
29*55fea89dSDan Cross  * PATENTS, COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS.
30*55fea89dSDan Cross  *
31*55fea89dSDan Cross  * The name of the Massachusetts Institute of Technology or M.I.T. may NOT
32*55fea89dSDan Cross  * be used in advertising or publicity pertaining to distribution of the
33*55fea89dSDan Cross  * software.  Title to copyright in this software and any associated
34*55fea89dSDan Cross  * documentation shall at all times remain with M.I.T., and USER agrees to
357c478bd9Sstevel@tonic-gate  * preserve same.
367c478bd9Sstevel@tonic-gate  *
377c478bd9Sstevel@tonic-gate  * Furthermore if you modify this software you must label
387c478bd9Sstevel@tonic-gate  * your software as modified software and not distribute it in such a
39*55fea89dSDan Cross  * fashion that it might be confused with the original M.I.T. software.
407c478bd9Sstevel@tonic-gate  */
417c478bd9Sstevel@tonic-gate 
427c478bd9Sstevel@tonic-gate /* Approach overview:
437c478bd9Sstevel@tonic-gate 
44505d05c7Sgtb    If a system version is available but buggy, save handles to it (via
45159d09a2SMark Phalan    inline functions in a support library), redefine the names to refer
46159d09a2SMark Phalan    to library functions, and in those functions, call the system
47159d09a2SMark Phalan    versions and fix up the returned data.  Use the native data
48159d09a2SMark Phalan    structures and flag values.
497c478bd9Sstevel@tonic-gate 
507c478bd9Sstevel@tonic-gate    If no system version exists, use gethostby* and fake it.  Define
517c478bd9Sstevel@tonic-gate    the data structures and flag values locally.
527c478bd9Sstevel@tonic-gate 
537c478bd9Sstevel@tonic-gate 
54505d05c7Sgtb    On Mac OS X, getaddrinfo results aren't cached (though
55505d05c7Sgtb    gethostbyname results are), so we need to build a cache here.  Now
56505d05c7Sgtb    things are getting really messy.  Because the cache is in use, we
57505d05c7Sgtb    use getservbyname, and throw away thread safety.  (Not that the
58505d05c7Sgtb    cache is thread safe, but when we get locking support, that'll be
59505d05c7Sgtb    dealt with.)  This code needs tearing down and rebuilding, soon.
60505d05c7Sgtb 
61505d05c7Sgtb 
627c478bd9Sstevel@tonic-gate    Note that recent Windows developers' code has an interesting hack:
637c478bd9Sstevel@tonic-gate    When you include the right header files, with the right set of
647c478bd9Sstevel@tonic-gate    macros indicating system versions, you'll get an inline function
657c478bd9Sstevel@tonic-gate    that looks for getaddrinfo (or whatever) in the system library, and
667c478bd9Sstevel@tonic-gate    calls it if it's there.  If it's not there, it fakes it with
677c478bd9Sstevel@tonic-gate    gethostby* calls.
687c478bd9Sstevel@tonic-gate 
697c478bd9Sstevel@tonic-gate    We're taking a simpler approach: A system provides these routines or
707c478bd9Sstevel@tonic-gate    it does not.
717c478bd9Sstevel@tonic-gate 
727c478bd9Sstevel@tonic-gate    Someday, we may want to take into account different versions (say,
737c478bd9Sstevel@tonic-gate    different revs of GNU libc) where some are broken in one way, and
747c478bd9Sstevel@tonic-gate    some work or are broken in another way.  Cross that bridge when we
757c478bd9Sstevel@tonic-gate    come to it.  */
767c478bd9Sstevel@tonic-gate 
777c478bd9Sstevel@tonic-gate /* To do, maybe:
787c478bd9Sstevel@tonic-gate 
797c478bd9Sstevel@tonic-gate    + For AIX 4.3.3, using the RFC 2133 definition: Implement
807c478bd9Sstevel@tonic-gate      AI_NUMERICHOST.  It's not defined in the header file.
817c478bd9Sstevel@tonic-gate 
827c478bd9Sstevel@tonic-gate      For certain (old?) versions of GNU libc, AI_NUMERICHOST is
837c478bd9Sstevel@tonic-gate      defined but not implemented.
847c478bd9Sstevel@tonic-gate 
857c478bd9Sstevel@tonic-gate    + Use gethostbyname2, inet_aton and other IPv6 or thread-safe
867c478bd9Sstevel@tonic-gate      functions if available.  But, see
877c478bd9Sstevel@tonic-gate      http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=135182 for one
887c478bd9Sstevel@tonic-gate      gethostbyname2 problem on Linux.  And besides, if a platform is
897c478bd9Sstevel@tonic-gate      supporting IPv6 at all, they really should be doing getaddrinfo
907c478bd9Sstevel@tonic-gate      by now.
917c478bd9Sstevel@tonic-gate 
927c478bd9Sstevel@tonic-gate    + inet_ntop, inet_pton
937c478bd9Sstevel@tonic-gate 
94505d05c7Sgtb    + Conditionally export/import the function definitions, so a
95505d05c7Sgtb      library can have a single copy instead of multiple.
96505d05c7Sgtb 
977c478bd9Sstevel@tonic-gate    + Upgrade host requirements to include working implementations of
987c478bd9Sstevel@tonic-gate      these functions, and throw all this away.  Pleeease?  :-)  */
997c478bd9Sstevel@tonic-gate 
1007c478bd9Sstevel@tonic-gate #ifndef FAI_DEFINED
1017c478bd9Sstevel@tonic-gate #define FAI_DEFINED
1027c478bd9Sstevel@tonic-gate #include "port-sockets.h"
1037c478bd9Sstevel@tonic-gate #include "socket-utils.h"
1047c478bd9Sstevel@tonic-gate 
1057c478bd9Sstevel@tonic-gate #if !defined (HAVE_GETADDRINFO)
1067c478bd9Sstevel@tonic-gate 
1077c478bd9Sstevel@tonic-gate #undef  addrinfo
1087c478bd9Sstevel@tonic-gate #define addrinfo	my_fake_addrinfo
1097c478bd9Sstevel@tonic-gate 
1107c478bd9Sstevel@tonic-gate struct addrinfo {
1117c478bd9Sstevel@tonic-gate     int ai_family;		/* PF_foo */
1127c478bd9Sstevel@tonic-gate     int ai_socktype;		/* SOCK_foo */
1137c478bd9Sstevel@tonic-gate     int ai_protocol;		/* 0, IPPROTO_foo */
1147c478bd9Sstevel@tonic-gate     int ai_flags;		/* AI_PASSIVE etc */
1157c478bd9Sstevel@tonic-gate     size_t ai_addrlen;		/* real length of socket address */
1167c478bd9Sstevel@tonic-gate     char *ai_canonname;		/* canonical name of host */
1177c478bd9Sstevel@tonic-gate     struct sockaddr *ai_addr;	/* pointer to variable-size address */
1187c478bd9Sstevel@tonic-gate     struct addrinfo *ai_next;	/* next in linked list */
1197c478bd9Sstevel@tonic-gate };
1207c478bd9Sstevel@tonic-gate 
1217c478bd9Sstevel@tonic-gate #undef	AI_PASSIVE
1227c478bd9Sstevel@tonic-gate #define	AI_PASSIVE	0x01
1237c478bd9Sstevel@tonic-gate #undef	AI_CANONNAME
1247c478bd9Sstevel@tonic-gate #define	AI_CANONNAME	0x02
1257c478bd9Sstevel@tonic-gate #undef	AI_NUMERICHOST
1267c478bd9Sstevel@tonic-gate #define	AI_NUMERICHOST	0x04
127505d05c7Sgtb /* RFC 2553 says these are part of the interface for getipnodebyname,
128505d05c7Sgtb    not for getaddrinfo.  RFC 3493 says they're part of the interface
129505d05c7Sgtb    for getaddrinfo, and getipnodeby* are deprecated.  Our fake
130505d05c7Sgtb    getaddrinfo implementation here does IPv4 only anyways.  */
1317c478bd9Sstevel@tonic-gate #undef	AI_V4MAPPED
132505d05c7Sgtb #define	AI_V4MAPPED	0
1337c478bd9Sstevel@tonic-gate #undef	AI_ADDRCONFIG
134505d05c7Sgtb #define	AI_ADDRCONFIG	0
1357c478bd9Sstevel@tonic-gate #undef	AI_ALL
136505d05c7Sgtb #define	AI_ALL		0
1377c478bd9Sstevel@tonic-gate #undef	AI_DEFAULT
138505d05c7Sgtb #define	AI_DEFAULT	(AI_V4MAPPED|AI_ADDRCONFIG)
1397c478bd9Sstevel@tonic-gate 
1407c478bd9Sstevel@tonic-gate #ifndef NI_MAXHOST
1417c478bd9Sstevel@tonic-gate #define NI_MAXHOST 1025
1427c478bd9Sstevel@tonic-gate #endif
1437c478bd9Sstevel@tonic-gate #ifndef NI_MAXSERV
1447c478bd9Sstevel@tonic-gate #define NI_MAXSERV 32
1457c478bd9Sstevel@tonic-gate #endif
1467c478bd9Sstevel@tonic-gate 
1477c478bd9Sstevel@tonic-gate #undef	NI_NUMERICHOST
1487c478bd9Sstevel@tonic-gate #define NI_NUMERICHOST	0x01
1497c478bd9Sstevel@tonic-gate #undef	NI_NUMERICSERV
1507c478bd9Sstevel@tonic-gate #define NI_NUMERICSERV	0x02
1517c478bd9Sstevel@tonic-gate #undef	NI_NAMEREQD
1527c478bd9Sstevel@tonic-gate #define NI_NAMEREQD	0x04
1537c478bd9Sstevel@tonic-gate #undef	NI_DGRAM
1547c478bd9Sstevel@tonic-gate #define NI_DGRAM	0x08
1557c478bd9Sstevel@tonic-gate #undef	NI_NOFQDN
1567c478bd9Sstevel@tonic-gate #define NI_NOFQDN	0x10
1577c478bd9Sstevel@tonic-gate 
1587c478bd9Sstevel@tonic-gate 
1597c478bd9Sstevel@tonic-gate #undef  EAI_ADDRFAMILY
1607c478bd9Sstevel@tonic-gate #define EAI_ADDRFAMILY	1
1617c478bd9Sstevel@tonic-gate #undef  EAI_AGAIN
1627c478bd9Sstevel@tonic-gate #define EAI_AGAIN	2
1637c478bd9Sstevel@tonic-gate #undef  EAI_BADFLAGS
1647c478bd9Sstevel@tonic-gate #define EAI_BADFLAGS	3
1657c478bd9Sstevel@tonic-gate #undef  EAI_FAIL
1667c478bd9Sstevel@tonic-gate #define EAI_FAIL	4
1677c478bd9Sstevel@tonic-gate #undef  EAI_FAMILY
1687c478bd9Sstevel@tonic-gate #define EAI_FAMILY	5
1697c478bd9Sstevel@tonic-gate #undef  EAI_MEMORY
1707c478bd9Sstevel@tonic-gate #define EAI_MEMORY	6
1717c478bd9Sstevel@tonic-gate #undef  EAI_NODATA
1727c478bd9Sstevel@tonic-gate #define EAI_NODATA	7
1737c478bd9Sstevel@tonic-gate #undef  EAI_NONAME
1747c478bd9Sstevel@tonic-gate #define EAI_NONAME	8
1757c478bd9Sstevel@tonic-gate #undef  EAI_SERVICE
1767c478bd9Sstevel@tonic-gate #define EAI_SERVICE	9
1777c478bd9Sstevel@tonic-gate #undef  EAI_SOCKTYPE
1787c478bd9Sstevel@tonic-gate #define EAI_SOCKTYPE	10
1797c478bd9Sstevel@tonic-gate #undef  EAI_SYSTEM
1807c478bd9Sstevel@tonic-gate #define EAI_SYSTEM	11
1817c478bd9Sstevel@tonic-gate 
1827c478bd9Sstevel@tonic-gate #endif /* ! HAVE_GETADDRINFO */
1837c478bd9Sstevel@tonic-gate 
1847c478bd9Sstevel@tonic-gate /* Fudge things on older gai implementations.  */
1857c478bd9Sstevel@tonic-gate /* AIX 4.3.3 is based on RFC 2133; no AI_NUMERICHOST.  */
1867c478bd9Sstevel@tonic-gate #ifndef AI_NUMERICHOST
1877c478bd9Sstevel@tonic-gate # define AI_NUMERICHOST 0
1887c478bd9Sstevel@tonic-gate #endif
189505d05c7Sgtb /* Partial RFC 2553 implementations may not have AI_ADDRCONFIG and
190505d05c7Sgtb    friends, which RFC 3493 says are now part of the getaddrinfo
191505d05c7Sgtb    interface, and we'll want to use.  */
192505d05c7Sgtb #ifndef AI_ADDRCONFIG
193505d05c7Sgtb # define AI_ADDRCONFIG 0
1947c478bd9Sstevel@tonic-gate #endif
195505d05c7Sgtb #ifndef AI_V4MAPPED
196505d05c7Sgtb # define AI_V4MAPPED 0
197505d05c7Sgtb #endif
198505d05c7Sgtb #ifndef AI_ALL
199505d05c7Sgtb # define AI_ALL 0
200505d05c7Sgtb #endif
201505d05c7Sgtb #ifndef AI_DEFAULT
202505d05c7Sgtb # define AI_DEFAULT (AI_ADDRCONFIG|AI_V4MAPPED)
2037c478bd9Sstevel@tonic-gate #endif
2047c478bd9Sstevel@tonic-gate 
205*55fea89dSDan Cross #if defined(KRB5_USE_INET6) && defined(NEED_INSIXADDR_ANY)
2067c478bd9Sstevel@tonic-gate /* If compiling with IPv6 support and C library does not define in6addr_any */
207159d09a2SMark Phalan extern const struct in6_addr krb5int_in6addr_any;
208505d05c7Sgtb #undef in6addr_any
209505d05c7Sgtb #define in6addr_any krb5int_in6addr_any
2107c478bd9Sstevel@tonic-gate #endif
2117c478bd9Sstevel@tonic-gate 
212159d09a2SMark Phalan /* Call out to stuff defined in libkrb5support.  */
213159d09a2SMark Phalan extern int krb5int_getaddrinfo (const char *node, const char *service,
214159d09a2SMark Phalan 				const struct addrinfo *hints,
215159d09a2SMark Phalan 				struct addrinfo **aip);
216159d09a2SMark Phalan extern void krb5int_freeaddrinfo (struct addrinfo *ai);
217159d09a2SMark Phalan extern const char *krb5int_gai_strerror(int err);
218159d09a2SMark Phalan extern int krb5int_getnameinfo (const struct sockaddr *sa, socklen_t salen,
219159d09a2SMark Phalan 				char *hbuf, size_t hbuflen,
220159d09a2SMark Phalan 				char *sbuf, size_t sbuflen,
221159d09a2SMark Phalan 				int flags);
222159d09a2SMark Phalan #ifndef IMPLEMENT_FAKE_GETADDRINFO
223159d09a2SMark Phalan #undef	getaddrinfo
224159d09a2SMark Phalan #define getaddrinfo krb5int_getaddrinfo
225159d09a2SMark Phalan #undef  freeaddrinfo
226159d09a2SMark Phalan #define freeaddrinfo krb5int_freeaddrinfo
227159d09a2SMark Phalan #undef  gai_strerror
228159d09a2SMark Phalan #define gai_strerror krb5int_gai_strerror
229159d09a2SMark Phalan #undef  getnameinfo
230159d09a2SMark Phalan #define getnameinfo krb5int_getnameinfo
2317c478bd9Sstevel@tonic-gate #endif
2327c478bd9Sstevel@tonic-gate 
2337c478bd9Sstevel@tonic-gate #endif /* FAI_DEFINED */
234