1*7934SMark.Phalan@Sun.COM 
2*7934SMark.Phalan@Sun.COM /*
3*7934SMark.Phalan@Sun.COM  * Copyright (C) 2004 by the Massachusetts Institute of Technology,
4*7934SMark.Phalan@Sun.COM  * Cambridge, MA, USA.  All Rights Reserved.
5*7934SMark.Phalan@Sun.COM  *
6*7934SMark.Phalan@Sun.COM  * This software is being provided to you, the LICENSEE, by the
7*7934SMark.Phalan@Sun.COM  * Massachusetts Institute of Technology (M.I.T.) under the following
8*7934SMark.Phalan@Sun.COM  * license.  By obtaining, using and/or copying this software, you agree
9*7934SMark.Phalan@Sun.COM  * that you have read, understood, and will comply with these terms and
10*7934SMark.Phalan@Sun.COM  * conditions:
11*7934SMark.Phalan@Sun.COM  *
12*7934SMark.Phalan@Sun.COM  * Export of this software from the United States of America may
13*7934SMark.Phalan@Sun.COM  * require a specific license from the United States Government.
14*7934SMark.Phalan@Sun.COM  * It is the responsibility of any person or organization contemplating
15*7934SMark.Phalan@Sun.COM  * export to obtain such a license before exporting.
16*7934SMark.Phalan@Sun.COM  *
17*7934SMark.Phalan@Sun.COM  * WITHIN THAT CONSTRAINT, permission to use, copy, modify and distribute
18*7934SMark.Phalan@Sun.COM  * this software and its documentation for any purpose and without fee or
19*7934SMark.Phalan@Sun.COM  * royalty is hereby granted, provided that you agree to comply with the
20*7934SMark.Phalan@Sun.COM  * following copyright notice and statements, including the disclaimer, and
21*7934SMark.Phalan@Sun.COM  * that the same appear on ALL copies of the software and documentation,
22*7934SMark.Phalan@Sun.COM  * including modifications that you make for internal use or for
23*7934SMark.Phalan@Sun.COM  * distribution:
24*7934SMark.Phalan@Sun.COM  *
25*7934SMark.Phalan@Sun.COM  * THIS SOFTWARE IS PROVIDED "AS IS", AND M.I.T. MAKES NO REPRESENTATIONS
26*7934SMark.Phalan@Sun.COM  * OR WARRANTIES, EXPRESS OR IMPLIED.  By way of example, but not
27*7934SMark.Phalan@Sun.COM  * limitation, M.I.T. MAKES NO REPRESENTATIONS OR WARRANTIES OF
28*7934SMark.Phalan@Sun.COM  * MERCHANTABILITY OR FITNESS FOR ANY PARTICULAR PURPOSE OR THAT THE USE OF
29*7934SMark.Phalan@Sun.COM  * THE LICENSED SOFTWARE OR DOCUMENTATION WILL NOT INFRINGE ANY THIRD PARTY
30*7934SMark.Phalan@Sun.COM  * PATENTS, COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS.
31*7934SMark.Phalan@Sun.COM  *
32*7934SMark.Phalan@Sun.COM  * The name of the Massachusetts Institute of Technology or M.I.T. may NOT
33*7934SMark.Phalan@Sun.COM  * be used in advertising or publicity pertaining to distribution of the
34*7934SMark.Phalan@Sun.COM  * software.  Title to copyright in this software and any associated
35*7934SMark.Phalan@Sun.COM  * documentation shall at all times remain with M.I.T., and USER agrees to
36*7934SMark.Phalan@Sun.COM  * preserve same.
37*7934SMark.Phalan@Sun.COM  *
38*7934SMark.Phalan@Sun.COM  * Furthermore if you modify this software you must label
39*7934SMark.Phalan@Sun.COM  * your software as modified software and not distribute it in such a
40*7934SMark.Phalan@Sun.COM  * fashion that it might be confused with the original M.I.T. software.
41*7934SMark.Phalan@Sun.COM  */
42*7934SMark.Phalan@Sun.COM 
43*7934SMark.Phalan@Sun.COM /* Approach overview:
44*7934SMark.Phalan@Sun.COM 
45*7934SMark.Phalan@Sun.COM    If a system version is available but buggy, save handles to it,
46*7934SMark.Phalan@Sun.COM    redefine the names to refer to static functions defined here, and
47*7934SMark.Phalan@Sun.COM    in those functions, call the system versions and fix up the
48*7934SMark.Phalan@Sun.COM    returned data.  Use the native data structures and flag values.
49*7934SMark.Phalan@Sun.COM 
50*7934SMark.Phalan@Sun.COM    If no system version exists, use gethostby* and fake it.  Define
51*7934SMark.Phalan@Sun.COM    the data structures and flag values locally.
52*7934SMark.Phalan@Sun.COM 
53*7934SMark.Phalan@Sun.COM 
54*7934SMark.Phalan@Sun.COM    On Mac OS X, getaddrinfo results aren't cached (though
55*7934SMark.Phalan@Sun.COM    gethostbyname results are), so we need to build a cache here.  Now
56*7934SMark.Phalan@Sun.COM    things are getting really messy.  Because the cache is in use, we
57*7934SMark.Phalan@Sun.COM    use getservbyname, and throw away thread safety.  (Not that the
58*7934SMark.Phalan@Sun.COM    cache is thread safe, but when we get locking support, that'll be
59*7934SMark.Phalan@Sun.COM    dealt with.)  This code needs tearing down and rebuilding, soon.
60*7934SMark.Phalan@Sun.COM 
61*7934SMark.Phalan@Sun.COM 
62*7934SMark.Phalan@Sun.COM    Note that recent Windows developers' code has an interesting hack:
63*7934SMark.Phalan@Sun.COM    When you include the right header files, with the right set of
64*7934SMark.Phalan@Sun.COM    macros indicating system versions, you'll get an inline function
65*7934SMark.Phalan@Sun.COM    that looks for getaddrinfo (or whatever) in the system library, and
66*7934SMark.Phalan@Sun.COM    calls it if it's there.  If it's not there, it fakes it with
67*7934SMark.Phalan@Sun.COM    gethostby* calls.
68*7934SMark.Phalan@Sun.COM 
69*7934SMark.Phalan@Sun.COM    We're taking a simpler approach: A system provides these routines or
70*7934SMark.Phalan@Sun.COM    it does not.
71*7934SMark.Phalan@Sun.COM 
72*7934SMark.Phalan@Sun.COM    Someday, we may want to take into account different versions (say,
73*7934SMark.Phalan@Sun.COM    different revs of GNU libc) where some are broken in one way, and
74*7934SMark.Phalan@Sun.COM    some work or are broken in another way.  Cross that bridge when we
75*7934SMark.Phalan@Sun.COM    come to it.  */
76*7934SMark.Phalan@Sun.COM 
77*7934SMark.Phalan@Sun.COM /* To do, maybe:
78*7934SMark.Phalan@Sun.COM 
79*7934SMark.Phalan@Sun.COM    + For AIX 4.3.3, using the RFC 2133 definition: Implement
80*7934SMark.Phalan@Sun.COM      AI_NUMERICHOST.  It's not defined in the header file.
81*7934SMark.Phalan@Sun.COM 
82*7934SMark.Phalan@Sun.COM      For certain (old?) versions of GNU libc, AI_NUMERICHOST is
83*7934SMark.Phalan@Sun.COM      defined but not implemented.
84*7934SMark.Phalan@Sun.COM 
85*7934SMark.Phalan@Sun.COM    + Use gethostbyname2, inet_aton and other IPv6 or thread-safe
86*7934SMark.Phalan@Sun.COM      functions if available.  But, see
87*7934SMark.Phalan@Sun.COM      http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=135182 for one
88*7934SMark.Phalan@Sun.COM      gethostbyname2 problem on Linux.  And besides, if a platform is
89*7934SMark.Phalan@Sun.COM      supporting IPv6 at all, they really should be doing getaddrinfo
90*7934SMark.Phalan@Sun.COM      by now.
91*7934SMark.Phalan@Sun.COM 
92*7934SMark.Phalan@Sun.COM    + inet_ntop, inet_pton
93*7934SMark.Phalan@Sun.COM 
94*7934SMark.Phalan@Sun.COM    + Conditionally export/import the function definitions, so a
95*7934SMark.Phalan@Sun.COM      library can have a single copy instead of multiple.
96*7934SMark.Phalan@Sun.COM 
97*7934SMark.Phalan@Sun.COM    + Upgrade host requirements to include working implementations of
98*7934SMark.Phalan@Sun.COM      these functions, and throw all this away.  Pleeease?  :-)  */
99*7934SMark.Phalan@Sun.COM 
100*7934SMark.Phalan@Sun.COM #include "port-sockets.h"
101*7934SMark.Phalan@Sun.COM #include "socket-utils.h"
102*7934SMark.Phalan@Sun.COM #include "k5-platform.h"
103*7934SMark.Phalan@Sun.COM #include "k5-thread.h"
104*7934SMark.Phalan@Sun.COM 
105*7934SMark.Phalan@Sun.COM #include "fake-addrinfo.h"
106*7934SMark.Phalan@Sun.COM 
107*7934SMark.Phalan@Sun.COM #if defined (__APPLE__) && defined (__MACH__)
108*7934SMark.Phalan@Sun.COM #define FAI_CACHE
109*7934SMark.Phalan@Sun.COM #endif
110*7934SMark.Phalan@Sun.COM 
111*7934SMark.Phalan@Sun.COM struct face {
112*7934SMark.Phalan@Sun.COM     struct in_addr *addrs4;
113*7934SMark.Phalan@Sun.COM     struct in6_addr *addrs6;
114*7934SMark.Phalan@Sun.COM     unsigned int naddrs4, naddrs6;
115*7934SMark.Phalan@Sun.COM     time_t expiration;
116*7934SMark.Phalan@Sun.COM     char *canonname, *name;
117*7934SMark.Phalan@Sun.COM     struct face *next;
118*7934SMark.Phalan@Sun.COM };
119*7934SMark.Phalan@Sun.COM 
120*7934SMark.Phalan@Sun.COM /* fake addrinfo cache */
121*7934SMark.Phalan@Sun.COM struct fac {
122*7934SMark.Phalan@Sun.COM     k5_mutex_t lock;
123*7934SMark.Phalan@Sun.COM     struct face *data;
124*7934SMark.Phalan@Sun.COM };
125*7934SMark.Phalan@Sun.COM 
126*7934SMark.Phalan@Sun.COM extern struct fac krb5int_fac;
127*7934SMark.Phalan@Sun.COM 
128*7934SMark.Phalan@Sun.COM extern int krb5int_init_fac (void);
129*7934SMark.Phalan@Sun.COM extern void krb5int_fini_fac (void);
130