xref: /freebsd/crypto/krb5/src/util/support/cache-addrinfo.h (revision 7f2fe78b9dd5f51c821d771b63d2e096f6fd49e9)
1 /* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2 /*
3  * Copyright (C) 2004 by the Massachusetts Institute of Technology,
4  * Cambridge, MA, USA.  All Rights Reserved.
5  *
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
10  * conditions:
11  *
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.
16  *
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
23  * distribution:
24  *
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.
31  *
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
36  * preserve same.
37  *
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.
41  */
42 
43 /*
44  * Approach overview:
45  *
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.
50  *
51  * If no system version exists, use gethostby* and fake it.  Define
52  * the data structures and flag values locally.
53  *
54  *
55  * On macOS, getaddrinfo results aren't cached (though gethostbyname
56  * results are), so we need to build a cache here.  Now things are
57  * getting really messy.  Because the cache is in use, we use
58  * getservbyname, and throw away thread safety.  (Not that the cache
59  * is thread safe, but when we get locking support, that'll be dealt
60  * with.)  This code needs tearing down and rebuilding, soon.
61  *
62  *
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
68  * gethostby* calls.
69  *
70  * We're taking a simpler approach: A system provides these routines or
71  * it does not.
72  *
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
76  * come to it.
77  */
78 
79 /* To do, maybe:
80  *
81  * + For AIX 4.3.3, using the RFC 2133 definition: Implement
82  *   AI_NUMERICHOST.  It's not defined in the header file.
83  *
84  *   For certain (old?) versions of GNU libc, AI_NUMERICHOST is
85  *   defined but not implemented.
86  *
87  * + Use gethostbyname2, inet_aton and other IPv6 or thread-safe
88  *   functions if available.  But, see
89  *   https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=135182 for one
90  *   gethostbyname2 problem on Linux.  And besides, if a platform is
91  *   supporting IPv6 at all, they really should be doing getaddrinfo
92  *   by now.
93  *
94  * + inet_ntop, inet_pton
95  *
96  * + Conditionally export/import the function definitions, so a
97  *   library can have a single copy instead of multiple.
98  *
99  * + Upgrade host requirements to include working implementations of
100  *   these functions, and throw all this away.  Pleeease?  :-)
101  */
102 
103 #include "k5-platform.h"
104 #include "k5-thread.h"
105 #include "port-sockets.h"
106 #include "socket-utils.h"
107 #include "fake-addrinfo.h"
108 
109 #if defined (__APPLE__) && defined (__MACH__) && 0
110 #define FAI_CACHE
111 #endif
112 
113 struct face {
114     struct in_addr *addrs4;
115     struct in6_addr *addrs6;
116     unsigned int naddrs4, naddrs6;
117     time_t expiration;
118     char *canonname, *name;
119     struct face *next;
120 };
121 
122 /* fake addrinfo cache */
123 struct fac {
124     k5_mutex_t lock;
125     struct face *data;
126 };
127 
128 extern struct fac krb5int_fac;
129 
130 extern int krb5int_init_fac (void);
131 extern void krb5int_fini_fac (void);
132