1159d09a2SMark Phalan 
2159d09a2SMark Phalan /*
3159d09a2SMark Phalan  * Copyright (C) 2004 by the Massachusetts Institute of Technology,
4159d09a2SMark Phalan  * Cambridge, MA, USA.  All Rights Reserved.
5*55fea89dSDan Cross  *
6*55fea89dSDan Cross  * This software is being provided to you, the LICENSEE, by the
7*55fea89dSDan Cross  * Massachusetts Institute of Technology (M.I.T.) under the following
8*55fea89dSDan Cross  * license.  By obtaining, using and/or copying this software, you agree
9*55fea89dSDan Cross  * that you have read, understood, and will comply with these terms and
10*55fea89dSDan Cross  * conditions:
11*55fea89dSDan Cross  *
12159d09a2SMark Phalan  * Export of this software from the United States of America may
13159d09a2SMark Phalan  * require a specific license from the United States Government.
14159d09a2SMark Phalan  * It is the responsibility of any person or organization contemplating
15159d09a2SMark Phalan  * export to obtain such a license before exporting.
16*55fea89dSDan Cross  *
17*55fea89dSDan Cross  * WITHIN THAT CONSTRAINT, permission to use, copy, modify and distribute
18*55fea89dSDan Cross  * this software and its documentation for any purpose and without fee or
19*55fea89dSDan Cross  * royalty is hereby granted, provided that you agree to comply with the
20*55fea89dSDan Cross  * following copyright notice and statements, including the disclaimer, and
21*55fea89dSDan Cross  * that the same appear on ALL copies of the software and documentation,
22*55fea89dSDan Cross  * including modifications that you make for internal use or for
23159d09a2SMark Phalan  * distribution:
24*55fea89dSDan Cross  *
25*55fea89dSDan Cross  * THIS SOFTWARE IS PROVIDED "AS IS", AND M.I.T. MAKES NO REPRESENTATIONS
26*55fea89dSDan Cross  * OR WARRANTIES, EXPRESS OR IMPLIED.  By way of example, but not
27*55fea89dSDan Cross  * limitation, M.I.T. MAKES NO REPRESENTATIONS OR WARRANTIES OF
28*55fea89dSDan Cross  * MERCHANTABILITY OR FITNESS FOR ANY PARTICULAR PURPOSE OR THAT THE USE OF
29*55fea89dSDan Cross  * THE LICENSED SOFTWARE OR DOCUMENTATION WILL NOT INFRINGE ANY THIRD PARTY
30*55fea89dSDan Cross  * PATENTS, COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS.
31*55fea89dSDan Cross  *
32*55fea89dSDan Cross  * The name of the Massachusetts Institute of Technology or M.I.T. may NOT
33*55fea89dSDan Cross  * be used in advertising or publicity pertaining to distribution of the
34*55fea89dSDan Cross  * software.  Title to copyright in this software and any associated
35*55fea89dSDan Cross  * documentation shall at all times remain with M.I.T., and USER agrees to
36159d09a2SMark Phalan  * preserve same.
37159d09a2SMark Phalan  *
38159d09a2SMark Phalan  * Furthermore if you modify this software you must label
39159d09a2SMark Phalan  * your software as modified software and not distribute it in such a
40*55fea89dSDan Cross  * fashion that it might be confused with the original M.I.T. software.
41159d09a2SMark Phalan  */
42159d09a2SMark Phalan 
43159d09a2SMark Phalan /* Approach overview:
44159d09a2SMark Phalan 
45159d09a2SMark Phalan    If a system version is available but buggy, save handles to it,
46159d09a2SMark Phalan    redefine the names to refer to static functions defined here, and
47159d09a2SMark Phalan    in those functions, call the system versions and fix up the
48159d09a2SMark Phalan    returned data.  Use the native data structures and flag values.
49159d09a2SMark Phalan 
50159d09a2SMark Phalan    If no system version exists, use gethostby* and fake it.  Define
51159d09a2SMark Phalan    the data structures and flag values locally.
52159d09a2SMark Phalan 
53159d09a2SMark Phalan 
54159d09a2SMark Phalan    On Mac OS X, getaddrinfo results aren't cached (though
55159d09a2SMark Phalan    gethostbyname results are), so we need to build a cache here.  Now
56159d09a2SMark Phalan    things are getting really messy.  Because the cache is in use, we
57159d09a2SMark Phalan    use getservbyname, and throw away thread safety.  (Not that the
58159d09a2SMark Phalan    cache is thread safe, but when we get locking support, that'll be
59159d09a2SMark Phalan    dealt with.)  This code needs tearing down and rebuilding, soon.
60159d09a2SMark Phalan 
61159d09a2SMark Phalan 
62159d09a2SMark Phalan    Note that recent Windows developers' code has an interesting hack:
63159d09a2SMark Phalan    When you include the right header files, with the right set of
64159d09a2SMark Phalan    macros indicating system versions, you'll get an inline function
65159d09a2SMark Phalan    that looks for getaddrinfo (or whatever) in the system library, and
66159d09a2SMark Phalan    calls it if it's there.  If it's not there, it fakes it with
67159d09a2SMark Phalan    gethostby* calls.
68159d09a2SMark Phalan 
69159d09a2SMark Phalan    We're taking a simpler approach: A system provides these routines or
70159d09a2SMark Phalan    it does not.
71159d09a2SMark Phalan 
72159d09a2SMark Phalan    Someday, we may want to take into account different versions (say,
73159d09a2SMark Phalan    different revs of GNU libc) where some are broken in one way, and
74159d09a2SMark Phalan    some work or are broken in another way.  Cross that bridge when we
75159d09a2SMark Phalan    come to it.  */
76159d09a2SMark Phalan 
77159d09a2SMark Phalan /* To do, maybe:
78159d09a2SMark Phalan 
79159d09a2SMark Phalan    + For AIX 4.3.3, using the RFC 2133 definition: Implement
80159d09a2SMark Phalan      AI_NUMERICHOST.  It's not defined in the header file.
81159d09a2SMark Phalan 
82159d09a2SMark Phalan      For certain (old?) versions of GNU libc, AI_NUMERICHOST is
83159d09a2SMark Phalan      defined but not implemented.
84159d09a2SMark Phalan 
85159d09a2SMark Phalan    + Use gethostbyname2, inet_aton and other IPv6 or thread-safe
86159d09a2SMark Phalan      functions if available.  But, see
87159d09a2SMark Phalan      http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=135182 for one
88159d09a2SMark Phalan      gethostbyname2 problem on Linux.  And besides, if a platform is
89159d09a2SMark Phalan      supporting IPv6 at all, they really should be doing getaddrinfo
90159d09a2SMark Phalan      by now.
91159d09a2SMark Phalan 
92159d09a2SMark Phalan    + inet_ntop, inet_pton
93159d09a2SMark Phalan 
94159d09a2SMark Phalan    + Conditionally export/import the function definitions, so a
95159d09a2SMark Phalan      library can have a single copy instead of multiple.
96159d09a2SMark Phalan 
97159d09a2SMark Phalan    + Upgrade host requirements to include working implementations of
98159d09a2SMark Phalan      these functions, and throw all this away.  Pleeease?  :-)  */
99159d09a2SMark Phalan 
100159d09a2SMark Phalan #include "port-sockets.h"
101159d09a2SMark Phalan #include "socket-utils.h"
102159d09a2SMark Phalan #include "k5-platform.h"
103159d09a2SMark Phalan #include "k5-thread.h"
104159d09a2SMark Phalan 
105159d09a2SMark Phalan #include "fake-addrinfo.h"
106159d09a2SMark Phalan 
107159d09a2SMark Phalan #if defined (__APPLE__) && defined (__MACH__)
108159d09a2SMark Phalan #define FAI_CACHE
109159d09a2SMark Phalan #endif
110159d09a2SMark Phalan 
111159d09a2SMark Phalan struct face {
112159d09a2SMark Phalan     struct in_addr *addrs4;
113159d09a2SMark Phalan     struct in6_addr *addrs6;
114159d09a2SMark Phalan     unsigned int naddrs4, naddrs6;
115159d09a2SMark Phalan     time_t expiration;
116159d09a2SMark Phalan     char *canonname, *name;
117159d09a2SMark Phalan     struct face *next;
118159d09a2SMark Phalan };
119159d09a2SMark Phalan 
120159d09a2SMark Phalan /* fake addrinfo cache */
121159d09a2SMark Phalan struct fac {
122159d09a2SMark Phalan     k5_mutex_t lock;
123159d09a2SMark Phalan     struct face *data;
124159d09a2SMark Phalan };
125159d09a2SMark Phalan 
126159d09a2SMark Phalan extern struct fac krb5int_fac;
127159d09a2SMark Phalan 
128159d09a2SMark Phalan extern int krb5int_init_fac (void);
129159d09a2SMark Phalan extern void krb5int_fini_fac (void);
130