Path: blob/main/crypto/krb5/src/util/support/cache-addrinfo.h
34889 views
/* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */1/*2* Copyright (C) 2004 by the Massachusetts Institute of Technology,3* Cambridge, MA, USA. All Rights Reserved.4*5* This software is being provided to you, the LICENSEE, by the6* Massachusetts Institute of Technology (M.I.T.) under the following7* license. By obtaining, using and/or copying this software, you agree8* that you have read, understood, and will comply with these terms and9* conditions:10*11* Export of this software from the United States of America may12* require a specific license from the United States Government.13* It is the responsibility of any person or organization contemplating14* export to obtain such a license before exporting.15*16* WITHIN THAT CONSTRAINT, permission to use, copy, modify and distribute17* this software and its documentation for any purpose and without fee or18* royalty is hereby granted, provided that you agree to comply with the19* following copyright notice and statements, including the disclaimer, and20* that the same appear on ALL copies of the software and documentation,21* including modifications that you make for internal use or for22* distribution:23*24* THIS SOFTWARE IS PROVIDED "AS IS", AND M.I.T. MAKES NO REPRESENTATIONS25* OR WARRANTIES, EXPRESS OR IMPLIED. By way of example, but not26* limitation, M.I.T. MAKES NO REPRESENTATIONS OR WARRANTIES OF27* MERCHANTABILITY OR FITNESS FOR ANY PARTICULAR PURPOSE OR THAT THE USE OF28* THE LICENSED SOFTWARE OR DOCUMENTATION WILL NOT INFRINGE ANY THIRD PARTY29* PATENTS, COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS.30*31* The name of the Massachusetts Institute of Technology or M.I.T. may NOT32* be used in advertising or publicity pertaining to distribution of the33* software. Title to copyright in this software and any associated34* documentation shall at all times remain with M.I.T., and USER agrees to35* preserve same.36*37* Furthermore if you modify this software you must label38* your software as modified software and not distribute it in such a39* fashion that it might be confused with the original M.I.T. software.40*/4142/*43* Approach overview:44*45* If a system version is available but buggy, save handles to it,46* redefine the names to refer to static functions defined here, and47* in those functions, call the system versions and fix up the48* returned data. Use the native data structures and flag values.49*50* If no system version exists, use gethostby* and fake it. Define51* the data structures and flag values locally.52*53*54* On macOS, getaddrinfo results aren't cached (though gethostbyname55* results are), so we need to build a cache here. Now things are56* getting really messy. Because the cache is in use, we use57* getservbyname, and throw away thread safety. (Not that the cache58* is thread safe, but when we get locking support, that'll be dealt59* with.) This code needs tearing down and rebuilding, soon.60*61*62* Note that recent Windows developers' code has an interesting hack:63* When you include the right header files, with the right set of64* macros indicating system versions, you'll get an inline function65* that looks for getaddrinfo (or whatever) in the system library, and66* calls it if it's there. If it's not there, it fakes it with67* gethostby* calls.68*69* We're taking a simpler approach: A system provides these routines or70* it does not.71*72* Someday, we may want to take into account different versions (say,73* different revs of GNU libc) where some are broken in one way, and74* some work or are broken in another way. Cross that bridge when we75* come to it.76*/7778/* To do, maybe:79*80* + For AIX 4.3.3, using the RFC 2133 definition: Implement81* AI_NUMERICHOST. It's not defined in the header file.82*83* For certain (old?) versions of GNU libc, AI_NUMERICHOST is84* defined but not implemented.85*86* + Use gethostbyname2, inet_aton and other IPv6 or thread-safe87* functions if available. But, see88* https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=135182 for one89* gethostbyname2 problem on Linux. And besides, if a platform is90* supporting IPv6 at all, they really should be doing getaddrinfo91* by now.92*93* + inet_ntop, inet_pton94*95* + Conditionally export/import the function definitions, so a96* library can have a single copy instead of multiple.97*98* + Upgrade host requirements to include working implementations of99* these functions, and throw all this away. Pleeease? :-)100*/101102#include "k5-platform.h"103#include "k5-thread.h"104#include "port-sockets.h"105#include "socket-utils.h"106#include "fake-addrinfo.h"107108#if defined (__APPLE__) && defined (__MACH__) && 0109#define FAI_CACHE110#endif111112struct face {113struct in_addr *addrs4;114struct in6_addr *addrs6;115unsigned int naddrs4, naddrs6;116time_t expiration;117char *canonname, *name;118struct face *next;119};120121/* fake addrinfo cache */122struct fac {123k5_mutex_t lock;124struct face *data;125};126127extern struct fac krb5int_fac;128129extern int krb5int_init_fac (void);130extern void krb5int_fini_fac (void);131132133