From 21b23db6f1226e9cbcc89703369f8a48de5eabd5 Mon Sep 17 00:00:00 2001 From: Ken Raeburn Date: Mon, 15 Nov 2004 22:16:02 +0000 Subject: [PATCH] Make static linking not require -lsocket, -lnsl, etc. Don't duplicate macro definitions. Header files and comments still need some cleanup. * cache-addrinfo.h, init-addrinfo.c: New files, split out from fake-addrinfo.c. * fake-addrinfo.c: Include cache-addrinfo.h. (FAI_CACHE, struct face, struct fac): Moved to cache-addrinfo.h. (krb5int_fac, krb5int_init_fac, krb5int_fini_fac): Moved to init-addrinfo.c. (addrinfo, struct addrinfo): Don't define. (AI_* and NI_* and EAI_* macros): Don't define. * threads.c: Include cache-addrinfo.h. (krb5int_init_fac, krb5int_fini_fac): Don't declare. * Makefile.in (SRCS, STLIBOBJS, LIBOBJS): Updated. ticket: 2761 status: open git-svn-id: svn://anonsvn.mit.edu/krb5/trunk@16867 dc483132-0cff-0310-8789-dd5450dbe970 --- src/util/support/ChangeLog | 14 ++++ src/util/support/Makefile.in | 3 + src/util/support/cache-addrinfo.h | 128 ++++++++++++++++++++++++++++++ src/util/support/fake-addrinfo.c | 105 +----------------------- src/util/support/init-addrinfo.c | 70 ++++++++++++++++ src/util/support/threads.c | 3 +- 6 files changed, 217 insertions(+), 106 deletions(-) create mode 100644 src/util/support/cache-addrinfo.h create mode 100644 src/util/support/init-addrinfo.c diff --git a/src/util/support/ChangeLog b/src/util/support/ChangeLog index 9698d0b3f..9732a6778 100644 --- a/src/util/support/ChangeLog +++ b/src/util/support/ChangeLog @@ -1,3 +1,17 @@ +2004-11-15 Ken Raeburn + + * cache-addrinfo.h, init-addrinfo.c: New files, split out from + fake-addrinfo.c. + * fake-addrinfo.c: Include cache-addrinfo.h. + (FAI_CACHE, struct face, struct fac): Moved to cache-addrinfo.h. + (krb5int_fac, krb5int_init_fac, krb5int_fini_fac): Moved to + init-addrinfo.c. + (addrinfo, struct addrinfo): Don't define. + (AI_* and NI_* and EAI_* macros): Don't define. + * threads.c: Include cache-addrinfo.h. + (krb5int_init_fac, krb5int_fini_fac): Don't declare. + * Makefile.in (SRCS, STLIBOBJS, LIBOBJS): Updated. + 2004-11-03 Ken Raeburn * fake-addrinfo.c: Import most of the contents of diff --git a/src/util/support/Makefile.in b/src/util/support/Makefile.in index 034bade41..2f366daa0 100644 --- a/src/util/support/Makefile.in +++ b/src/util/support/Makefile.in @@ -24,10 +24,12 @@ LIBFINIFUNC=krb5int_thread_support_fini STLIBOBJS= \ threads.o \ + init-addrinfo.o \ fake-addrinfo.o LIBOBJS= \ $(OUTPRE)threads.$(OBJEXT) \ + $(OUTPRE)init-addrinfo.$(OBJEXT) \ $(OUTPRE)fake-addrinfo.$(OBJEXT) STOBJLISTS=OBJS.ST @@ -38,6 +40,7 @@ LOCALINCLUDES=-I. -I$(srcdir) SRCS=\ $(srcdir)/threads.c \ + $(srcdir)/init-addrinfo.c \ $(srcdir)/fake-addrinfo.c SHLIB_EXPDEPS = SHLIB_EXPLIBS= $(LIBS) diff --git a/src/util/support/cache-addrinfo.h b/src/util/support/cache-addrinfo.h new file mode 100644 index 000000000..807c652f8 --- /dev/null +++ b/src/util/support/cache-addrinfo.h @@ -0,0 +1,128 @@ +/* + * Copyright (C) 2004 by the Massachusetts Institute of Technology, + * Cambridge, MA, USA. All Rights Reserved. + * + * This software is being provided to you, the LICENSEE, by the + * Massachusetts Institute of Technology (M.I.T.) under the following + * license. By obtaining, using and/or copying this software, you agree + * that you have read, understood, and will comply with these terms and + * conditions: + * + * Export of this software from the United States of America may + * require a specific license from the United States Government. + * It is the responsibility of any person or organization contemplating + * export to obtain such a license before exporting. + * + * WITHIN THAT CONSTRAINT, permission to use, copy, modify and distribute + * this software and its documentation for any purpose and without fee or + * royalty is hereby granted, provided that you agree to comply with the + * following copyright notice and statements, including the disclaimer, and + * that the same appear on ALL copies of the software and documentation, + * including modifications that you make for internal use or for + * distribution: + * + * THIS SOFTWARE IS PROVIDED "AS IS", AND M.I.T. MAKES NO REPRESENTATIONS + * OR WARRANTIES, EXPRESS OR IMPLIED. By way of example, but not + * limitation, M.I.T. MAKES NO REPRESENTATIONS OR WARRANTIES OF + * MERCHANTABILITY OR FITNESS FOR ANY PARTICULAR PURPOSE OR THAT THE USE OF + * THE LICENSED SOFTWARE OR DOCUMENTATION WILL NOT INFRINGE ANY THIRD PARTY + * PATENTS, COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS. + * + * The name of the Massachusetts Institute of Technology or M.I.T. may NOT + * be used in advertising or publicity pertaining to distribution of the + * software. Title to copyright in this software and any associated + * documentation shall at all times remain with M.I.T., and USER agrees to + * preserve same. + * + * Furthermore if you modify this software you must label + * your software as modified software and not distribute it in such a + * fashion that it might be confused with the original M.I.T. software. + */ + +/* Approach overview: + + If a system version is available but buggy, save handles to it, + redefine the names to refer to static functions defined here, and + in those functions, call the system versions and fix up the + returned data. Use the native data structures and flag values. + + If no system version exists, use gethostby* and fake it. Define + the data structures and flag values locally. + + + On Mac OS X, getaddrinfo results aren't cached (though + gethostbyname results are), so we need to build a cache here. Now + things are getting really messy. Because the cache is in use, we + use getservbyname, and throw away thread safety. (Not that the + cache is thread safe, but when we get locking support, that'll be + dealt with.) This code needs tearing down and rebuilding, soon. + + + Note that recent Windows developers' code has an interesting hack: + When you include the right header files, with the right set of + macros indicating system versions, you'll get an inline function + that looks for getaddrinfo (or whatever) in the system library, and + calls it if it's there. If it's not there, it fakes it with + gethostby* calls. + + We're taking a simpler approach: A system provides these routines or + it does not. + + Someday, we may want to take into account different versions (say, + different revs of GNU libc) where some are broken in one way, and + some work or are broken in another way. Cross that bridge when we + come to it. */ + +/* To do, maybe: + + + For AIX 4.3.3, using the RFC 2133 definition: Implement + AI_NUMERICHOST. It's not defined in the header file. + + For certain (old?) versions of GNU libc, AI_NUMERICHOST is + defined but not implemented. + + + Use gethostbyname2, inet_aton and other IPv6 or thread-safe + functions if available. But, see + http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=135182 for one + gethostbyname2 problem on Linux. And besides, if a platform is + supporting IPv6 at all, they really should be doing getaddrinfo + by now. + + + inet_ntop, inet_pton + + + Conditionally export/import the function definitions, so a + library can have a single copy instead of multiple. + + + Upgrade host requirements to include working implementations of + these functions, and throw all this away. Pleeease? :-) */ + +#include "port-sockets.h" +#include "socket-utils.h" +#include "k5-platform.h" +#include "k5-thread.h" + +#include "fake-addrinfo.h" + +#if defined (__APPLE__) && defined (__MACH__) +#define FAI_CACHE +#endif + +struct face { + struct in_addr *addrs4; + struct in6_addr *addrs6; + unsigned int naddrs4, naddrs6; + time_t expiration; + char *canonname, *name; + struct face *next; +}; + +/* fake addrinfo cache */ +struct fac { + k5_mutex_t lock; + struct face *data; +}; + +extern struct fac krb5int_fac; + +extern int krb5int_init_fac (void); +extern void krb5int_fini_fac (void); diff --git a/src/util/support/fake-addrinfo.c b/src/util/support/fake-addrinfo.c index b0ac00104..8ccbe3e78 100644 --- a/src/util/support/fake-addrinfo.c +++ b/src/util/support/fake-addrinfo.c @@ -131,9 +131,7 @@ extern /*@dependent@*/ char *gai_strerror (int code) /*@*/; #endif -#if defined (__APPLE__) && defined (__MACH__) -#define FAI_CACHE -#endif +#include "cache-addrinfo.h" #if (defined (__linux__) && defined(HAVE_GETADDRINFO)) || defined (_AIX) /* See comments below. */ @@ -343,80 +341,6 @@ system_getnameinfo (const struct sockaddr *sa, socklen_t salen, #undef gai_strerror #define gai_strerror my_fake_gai_strerror -#undef addrinfo -#define addrinfo my_fake_addrinfo - -struct addrinfo { - int ai_family; /* PF_foo */ - int ai_socktype; /* SOCK_foo */ - int ai_protocol; /* 0, IPPROTO_foo */ - int ai_flags; /* AI_PASSIVE etc */ - size_t ai_addrlen; /* real length of socket address */ - char *ai_canonname; /* canonical name of host */ - struct sockaddr *ai_addr; /* pointer to variable-size address */ - struct addrinfo *ai_next; /* next in linked list */ -}; - -#undef AI_PASSIVE -#define AI_PASSIVE 0x01 -#undef AI_CANONNAME -#define AI_CANONNAME 0x02 -#undef AI_NUMERICHOST -#define AI_NUMERICHOST 0x04 -/* RFC 2553 says these are part of the interface for getipnodebyname, - not for getaddrinfo. RFC 3493 says they're part of the interface - for getaddrinfo, and getipnodeby* are deprecated. Our fake - getaddrinfo implementation here does IPv4 only anyways. */ -#undef AI_V4MAPPED -#define AI_V4MAPPED 0 -#undef AI_ADDRCONFIG -#define AI_ADDRCONFIG 0 -#undef AI_ALL -#define AI_ALL 0 -#undef AI_DEFAULT -#define AI_DEFAULT (AI_V4MAPPED|AI_ADDRCONFIG) - -#ifndef NI_MAXHOST -#define NI_MAXHOST 1025 -#endif -#ifndef NI_MAXSERV -#define NI_MAXSERV 32 -#endif - -#undef NI_NUMERICHOST -#define NI_NUMERICHOST 0x01 -#undef NI_NUMERICSERV -#define NI_NUMERICSERV 0x02 -#undef NI_NAMEREQD -#define NI_NAMEREQD 0x04 -#undef NI_DGRAM -#define NI_DGRAM 0x08 -#undef NI_NOFQDN -#define NI_NOFQDN 0x10 - - -#undef EAI_ADDRFAMILY -#define EAI_ADDRFAMILY 1 -#undef EAI_AGAIN -#define EAI_AGAIN 2 -#undef EAI_BADFLAGS -#define EAI_BADFLAGS 3 -#undef EAI_FAIL -#define EAI_FAIL 4 -#undef EAI_FAMILY -#define EAI_FAMILY 5 -#undef EAI_MEMORY -#define EAI_MEMORY 6 -#undef EAI_NODATA -#define EAI_NODATA 7 -#undef EAI_NONAME -#define EAI_NONAME 8 -#undef EAI_SERVICE -#define EAI_SERVICE 9 -#undef EAI_SOCKTYPE -#define EAI_SOCKTYPE 10 -#undef EAI_SYSTEM -#define EAI_SYSTEM 11 #endif /* ! HAVE_GETADDRINFO */ @@ -587,23 +511,6 @@ int getnameinfo (const struct sockaddr *addr, socklen_t len, #include #endif -struct face { - struct in_addr *addrs4; - struct in6_addr *addrs6; - unsigned int naddrs4, naddrs6; - time_t expiration; - char *canonname, *name; - struct face *next; -}; - -/* fake addrinfo cache */ -struct fac { - k5_mutex_t lock; - struct face *data; -}; - -static struct fac krb5int_fac = { K5_MUTEX_PARTIAL_INITIALIZER, 0 }; - #ifdef NEED_FAKE_GETADDRINFO #include /* for strspn */ @@ -1354,16 +1261,6 @@ void freeaddrinfo (struct addrinfo *ai) } #endif /* WRAP_GETADDRINFO */ -int krb5int_init_fac (void) -{ - return k5_mutex_finish_init(&krb5int_fac.lock); -} - -void krb5int_fini_fac (void) -{ - k5_mutex_destroy(&krb5int_fac.lock); -} - extern int krb5int_call_thread_support_init(void); static int krb5int_lock_fac (void) { diff --git a/src/util/support/init-addrinfo.c b/src/util/support/init-addrinfo.c new file mode 100644 index 000000000..4c94dc743 --- /dev/null +++ b/src/util/support/init-addrinfo.c @@ -0,0 +1,70 @@ +/* + * Copyright (C) 2004 by the Massachusetts Institute of Technology, + * Cambridge, MA, USA. All Rights Reserved. + * + * This software is being provided to you, the LICENSEE, by the + * Massachusetts Institute of Technology (M.I.T.) under the following + * license. By obtaining, using and/or copying this software, you agree + * that you have read, understood, and will comply with these terms and + * conditions: + * + * Export of this software from the United States of America may + * require a specific license from the United States Government. + * It is the responsibility of any person or organization contemplating + * export to obtain such a license before exporting. + * + * WITHIN THAT CONSTRAINT, permission to use, copy, modify and distribute + * this software and its documentation for any purpose and without fee or + * royalty is hereby granted, provided that you agree to comply with the + * following copyright notice and statements, including the disclaimer, and + * that the same appear on ALL copies of the software and documentation, + * including modifications that you make for internal use or for + * distribution: + * + * THIS SOFTWARE IS PROVIDED "AS IS", AND M.I.T. MAKES NO REPRESENTATIONS + * OR WARRANTIES, EXPRESS OR IMPLIED. By way of example, but not + * limitation, M.I.T. MAKES NO REPRESENTATIONS OR WARRANTIES OF + * MERCHANTABILITY OR FITNESS FOR ANY PARTICULAR PURPOSE OR THAT THE USE OF + * THE LICENSED SOFTWARE OR DOCUMENTATION WILL NOT INFRINGE ANY THIRD PARTY + * PATENTS, COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS. + * + * The name of the Massachusetts Institute of Technology or M.I.T. may NOT + * be used in advertising or publicity pertaining to distribution of the + * software. Title to copyright in this software and any associated + * documentation shall at all times remain with M.I.T., and USER agrees to + * preserve same. + * + * Furthermore if you modify this software you must label + * your software as modified software and not distribute it in such a + * fashion that it might be confused with the original M.I.T. software. + */ + +/* Stuff that needs initialization for fake-addrinfo.c. + + Separated out, so that static linking against this library doesn't + require pulling in socket/nsl/whatever libraries for code not using + getaddrinfo. */ + +#include "port-sockets.h" +#include "socket-utils.h" +#include "k5-platform.h" +#include "k5-thread.h" + +#include /* for sprintf */ +#include + +#define IMPLEMENT_FAKE_GETADDRINFO +#include "fake-addrinfo.h" +#include "cache-addrinfo.h" + +struct fac krb5int_fac = { K5_MUTEX_PARTIAL_INITIALIZER, 0 }; + +int krb5int_init_fac (void) +{ + return k5_mutex_finish_init(&krb5int_fac.lock); +} + +void krb5int_fini_fac (void) +{ + k5_mutex_destroy(&krb5int_fac.lock); +} diff --git a/src/util/support/threads.c b/src/util/support/threads.c index eb27f1cf9..7b061fdd7 100644 --- a/src/util/support/threads.c +++ b/src/util/support/threads.c @@ -322,8 +322,7 @@ int krb5int_call_thread_support_init (void) return CALL_INIT_FUNCTION(krb5int_thread_support_init); } -extern int krb5int_init_fac(void); -extern void krb5int_fini_fac(void); +#include "cache-addrinfo.h" int krb5int_thread_support_init (void) { -- 2.26.2