Path: blob/main/crypto/krb5/src/include/fake-addrinfo.h
34878 views
/* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */1/*2* Copyright (C) 2001,2002,2003,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/* Approach overview:4344If a system version is available but buggy, save handles to it (via45inline functions in a support library), redefine the names to refer46to library functions, and in those functions, call the system47versions and fix up the returned data. Use the native data48structures and flag values.4950If no system version exists, use gethostby* and fake it. Define51the data structures and flag values locally.525354On macOS, getaddrinfo results aren't cached (though55gethostbyname results are), so we need to build a cache here. Now56things are getting really messy. Because the cache is in use, we57use getservbyname, and throw away thread safety. (Not that the58cache is thread safe, but when we get locking support, that'll be59dealt with.) This code needs tearing down and rebuilding, soon.606162Note that recent Windows developers' code has an interesting hack:63When you include the right header files, with the right set of64macros indicating system versions, you'll get an inline function65that looks for getaddrinfo (or whatever) in the system library, and66calls it if it's there. If it's not there, it fakes it with67gethostby* calls.6869We're taking a simpler approach: A system provides these routines or70it does not.7172Someday, we may want to take into account different versions (say,73different revs of GNU libc) where some are broken in one way, and74some work or are broken in another way. Cross that bridge when we75come to it. */7677/* To do, maybe:7879+ For AIX 4.3.3, using the RFC 2133 definition: Implement80AI_NUMERICHOST. It's not defined in the header file.8182For certain (old?) versions of GNU libc, AI_NUMERICHOST is83defined but not implemented.8485+ Use gethostbyname2, inet_aton and other IPv6 or thread-safe86functions if available. But, see87https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=135182 for one88gethostbyname2 problem on Linux. And besides, if a platform is89supporting IPv6 at all, they really should be doing getaddrinfo90by now.9192+ inet_ntop, inet_pton9394+ Conditionally export/import the function definitions, so a95library can have a single copy instead of multiple.9697+ Upgrade host requirements to include working implementations of98these functions, and throw all this away. Pleeease? :-) */99100#ifndef FAI_DEFINED101#define FAI_DEFINED102#include "port-sockets.h"103#include "socket-utils.h"104105#if !defined (HAVE_GETADDRINFO)106107#undef addrinfo108#define addrinfo my_fake_addrinfo109110struct addrinfo {111int ai_family; /* PF_foo */112int ai_socktype; /* SOCK_foo */113int ai_protocol; /* 0, IPPROTO_foo */114int ai_flags; /* AI_PASSIVE etc */115size_t ai_addrlen; /* real length of socket address */116char *ai_canonname; /* canonical name of host */117struct sockaddr *ai_addr; /* pointer to variable-size address */118struct addrinfo *ai_next; /* next in linked list */119};120121#undef AI_PASSIVE122#define AI_PASSIVE 0x01123#undef AI_CANONNAME124#define AI_CANONNAME 0x02125#undef AI_NUMERICHOST126#define AI_NUMERICHOST 0x04127/* RFC 2553 says these are part of the interface for getipnodebyname,128not for getaddrinfo. RFC 3493 says they're part of the interface129for getaddrinfo, and getipnodeby* are deprecated. Our fake130getaddrinfo implementation here does IPv4 only anyways. */131#undef AI_V4MAPPED132#define AI_V4MAPPED 0133#undef AI_ADDRCONFIG134#define AI_ADDRCONFIG 0135#undef AI_ALL136#define AI_ALL 0137#undef AI_DEFAULT138#define AI_DEFAULT (AI_V4MAPPED|AI_ADDRCONFIG)139140#ifndef NI_MAXHOST141#define NI_MAXHOST 1025142#endif143#ifndef NI_MAXSERV144#define NI_MAXSERV 32145#endif146147#undef NI_NUMERICHOST148#define NI_NUMERICHOST 0x01149#undef NI_NUMERICSERV150#define NI_NUMERICSERV 0x02151#undef NI_NAMEREQD152#define NI_NAMEREQD 0x04153#undef NI_DGRAM154#define NI_DGRAM 0x08155#undef NI_NOFQDN156#define NI_NOFQDN 0x10157158159#undef EAI_ADDRFAMILY160#define EAI_ADDRFAMILY 1161#undef EAI_AGAIN162#define EAI_AGAIN 2163#undef EAI_BADFLAGS164#define EAI_BADFLAGS 3165#undef EAI_FAIL166#define EAI_FAIL 4167#undef EAI_FAMILY168#define EAI_FAMILY 5169#undef EAI_MEMORY170#define EAI_MEMORY 6171#undef EAI_NODATA172#define EAI_NODATA 7173#undef EAI_NONAME174#define EAI_NONAME 8175#undef EAI_SERVICE176#define EAI_SERVICE 9177#undef EAI_SOCKTYPE178#define EAI_SOCKTYPE 10179#undef EAI_SYSTEM180#define EAI_SYSTEM 11181182#endif /* ! HAVE_GETADDRINFO */183184/* Fudge things on older gai implementations. */185/* AIX 4.3.3 is based on RFC 2133; no AI_NUMERICHOST. */186#ifndef AI_NUMERICHOST187# define AI_NUMERICHOST 0188#endif189/* Partial RFC 2553 implementations may not have AI_ADDRCONFIG and190friends, which RFC 3493 says are now part of the getaddrinfo191interface, and we'll want to use. */192#ifndef AI_ADDRCONFIG193# define AI_ADDRCONFIG 0194#endif195#ifndef AI_V4MAPPED196# define AI_V4MAPPED 0197#endif198#ifndef AI_ALL199# define AI_ALL 0200#endif201#ifndef AI_DEFAULT202# define AI_DEFAULT (AI_ADDRCONFIG|AI_V4MAPPED)203#endif204205#if defined(NEED_INSIXADDR_ANY)206/* If compiling with IPv6 support and C library does not define in6addr_any */207extern const struct in6_addr krb5int_in6addr_any;208#undef in6addr_any209#define in6addr_any krb5int_in6addr_any210#endif211212/* Call out to stuff defined in libkrb5support. */213extern int krb5int_getaddrinfo (const char *node, const char *service,214const struct addrinfo *hints,215struct addrinfo **aip);216extern void krb5int_freeaddrinfo (struct addrinfo *ai);217extern const char *krb5int_gai_strerror(int err);218extern int krb5int_getnameinfo (const struct sockaddr *sa, socklen_t salen,219char *hbuf, size_t hbuflen,220char *sbuf, size_t sbuflen,221int flags);222#ifndef IMPLEMENT_FAKE_GETADDRINFO223#undef getaddrinfo224#define getaddrinfo krb5int_getaddrinfo225#undef freeaddrinfo226#define freeaddrinfo krb5int_freeaddrinfo227#undef gai_strerror228#define gai_strerror krb5int_gai_strerror229#undef getnameinfo230#define getnameinfo krb5int_getnameinfo231#endif232233#endif /* FAI_DEFINED */234235236