hostip4.c (10175B)
1 /*************************************************************************** 2 * _ _ ____ _ 3 * Project ___| | | | _ \| | 4 * / __| | | | |_) | | 5 * | (__| |_| | _ <| |___ 6 * \___|\___/|_| \_\_____| 7 * 8 * Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al. 9 * 10 * This software is licensed as described in the file COPYING, which 11 * you should have received as part of this distribution. The terms 12 * are also available at https://curl.se/docs/copyright.html. 13 * 14 * You may opt to use, copy, modify, merge, publish, distribute and/or sell 15 * copies of the Software, and permit persons to whom the Software is 16 * furnished to do so, under the terms of the COPYING file. 17 * 18 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY 19 * KIND, either express or implied. 20 * 21 * SPDX-License-Identifier: curl 22 * 23 ***************************************************************************/ 24 25 #include "curl_setup.h" 26 27 /*********************************************************************** 28 * Only for plain IPv4 builds 29 **********************************************************************/ 30 #ifdef CURLRES_IPV4 /* plain IPv4 code coming up */ 31 32 #ifdef HAVE_NETINET_IN_H 33 #include <netinet/in.h> 34 #endif 35 #ifdef HAVE_NETDB_H 36 #include <netdb.h> 37 #endif 38 #ifdef HAVE_ARPA_INET_H 39 #include <arpa/inet.h> 40 #endif 41 #ifdef __VMS 42 #include <in.h> 43 #include <inet.h> 44 #endif 45 46 #include "urldata.h" 47 #include "sendf.h" 48 #include "hostip.h" 49 #include "hash.h" 50 #include "share.h" 51 #include "url.h" 52 /* The last 3 #include files should be in this order */ 53 #include "curl_printf.h" 54 #include "curl_memory.h" 55 #include "memdebug.h" 56 57 58 #ifdef CURLRES_SYNCH 59 60 /* 61 * Curl_sync_getaddrinfo() - the IPv4 synchronous version. 62 * 63 * The original code to this function was from the Dancer source code, written 64 * by Bjorn Reese, it has since been patched and modified considerably. 65 * 66 * gethostbyname_r() is the thread-safe version of the gethostbyname() 67 * function. When we build for plain IPv4, we attempt to use this 68 * function. There are _three_ different gethostbyname_r() versions, and we 69 * detect which one this platform supports in the configure script and set up 70 * the HAVE_GETHOSTBYNAME_R_3, HAVE_GETHOSTBYNAME_R_5 or 71 * HAVE_GETHOSTBYNAME_R_6 defines accordingly. Note that HAVE_GETADDRBYNAME 72 * has the corresponding rules. This is primarily on *nix. Note that some Unix 73 * flavours have thread-safe versions of the plain gethostbyname() etc. 74 * 75 */ 76 struct Curl_addrinfo *Curl_sync_getaddrinfo(struct Curl_easy *data, 77 const char *hostname, 78 int port, 79 int ip_version) 80 { 81 struct Curl_addrinfo *ai = NULL; 82 83 (void)ip_version; 84 #ifdef CURL_DISABLE_VERBOSE_STRINGS 85 (void)data; 86 #endif 87 88 ai = Curl_ipv4_resolve_r(hostname, port); 89 if(!ai) 90 infof(data, "Curl_ipv4_resolve_r failed for %s", hostname); 91 92 return ai; 93 } 94 #endif /* CURLRES_SYNCH */ 95 #endif /* CURLRES_IPV4 */ 96 97 #if defined(CURLRES_IPV4) && \ 98 !defined(CURLRES_ARES) && !defined(CURLRES_AMIGA) 99 100 /* 101 * Curl_ipv4_resolve_r() - ipv4 threadsafe resolver function. 102 * 103 * This is used for both synchronous and asynchronous resolver builds, 104 * implying that only threadsafe code and function calls may be used. 105 * 106 */ 107 struct Curl_addrinfo *Curl_ipv4_resolve_r(const char *hostname, 108 int port) 109 { 110 #if !(defined(HAVE_GETADDRINFO) && defined(HAVE_GETADDRINFO_THREADSAFE)) && \ 111 defined(HAVE_GETHOSTBYNAME_R_3) 112 int res; 113 #endif 114 struct Curl_addrinfo *ai = NULL; 115 #if !(defined(HAVE_GETADDRINFO) && defined(HAVE_GETADDRINFO_THREADSAFE)) 116 struct hostent *h = NULL; 117 struct hostent *buf = NULL; 118 #endif 119 120 #if defined(HAVE_GETADDRINFO) && defined(HAVE_GETADDRINFO_THREADSAFE) 121 struct addrinfo hints; 122 char sbuf[12]; 123 char *sbufptr = NULL; 124 125 memset(&hints, 0, sizeof(hints)); 126 hints.ai_family = PF_INET; 127 hints.ai_socktype = SOCK_STREAM; 128 if(port) { 129 msnprintf(sbuf, sizeof(sbuf), "%d", port); 130 sbufptr = sbuf; 131 } 132 133 (void)Curl_getaddrinfo_ex(hostname, sbufptr, &hints, &ai); 134 135 #elif defined(HAVE_GETHOSTBYNAME_R) 136 /* 137 * gethostbyname_r() is the preferred resolve function for many platforms. 138 * Since there are three different versions of it, the following code is 139 * somewhat #ifdef-ridden. 140 */ 141 int h_errnop; 142 143 buf = calloc(1, CURL_HOSTENT_SIZE); 144 if(!buf) 145 return NULL; /* major failure */ 146 /* 147 * The clearing of the buffer is a workaround for a gethostbyname_r bug in 148 * qnx nto and it is also _required_ for some of these functions on some 149 * platforms. 150 */ 151 152 #if defined(HAVE_GETHOSTBYNAME_R_5) 153 /* Solaris, IRIX and more */ 154 h = gethostbyname_r(hostname, 155 (struct hostent *)buf, 156 (char *)buf + sizeof(struct hostent), 157 CURL_HOSTENT_SIZE - sizeof(struct hostent), 158 &h_errnop); 159 160 /* If the buffer is too small, it returns NULL and sets errno to 161 * ERANGE. The errno is thread safe if this is compiled with 162 * -D_REENTRANT as then the 'errno' variable is a macro defined to get 163 * used properly for threads. 164 */ 165 166 if(h) { 167 ; 168 } 169 else 170 #elif defined(HAVE_GETHOSTBYNAME_R_6) 171 /* Linux */ 172 173 (void)gethostbyname_r(hostname, 174 (struct hostent *)buf, 175 (char *)buf + sizeof(struct hostent), 176 CURL_HOSTENT_SIZE - sizeof(struct hostent), 177 &h, /* DIFFERENCE */ 178 &h_errnop); 179 /* Redhat 8, using glibc 2.2.93 changed the behavior. Now all of a 180 * sudden this function returns EAGAIN if the given buffer size is too 181 * small. Previous versions are known to return ERANGE for the same 182 * problem. 183 * 184 * This would not be such a big problem if older versions would not 185 * sometimes return EAGAIN on a common failure case. Alas, we cannot 186 * assume that EAGAIN *or* ERANGE means ERANGE for any given version of 187 * glibc. 188 * 189 * For now, we do that and thus we may call the function repeatedly and 190 * fail for older glibc versions that return EAGAIN, until we run out of 191 * buffer size (step_size grows beyond CURL_HOSTENT_SIZE). 192 * 193 * If anyone has a better fix, please tell us! 194 * 195 * ------------------------------------------------------------------- 196 * 197 * On October 23rd 2003, Dan C dug up more details on the mysteries of 198 * gethostbyname_r() in glibc: 199 * 200 * In glibc 2.2.5 the interface is different (this has also been 201 * discovered in glibc 2.1.1-6 as shipped by Redhat 6). What I cannot 202 * explain, is that tests performed on glibc 2.2.4-34 and 2.2.4-32 203 * (shipped/upgraded by Redhat 7.2) do not show this behavior! 204 * 205 * In this "buggy" version, the return code is -1 on error and 'errno' 206 * is set to the ERANGE or EAGAIN code. Note that 'errno' is not a 207 * thread-safe variable. 208 */ 209 210 if(!h) /* failure */ 211 #elif defined(HAVE_GETHOSTBYNAME_R_3) 212 /* AIX, Digital UNIX/Tru64, HP-UX 10, more? */ 213 214 /* For AIX 4.3 or later, we do not use gethostbyname_r() at all, because of 215 * the plain fact that it does not return unique full buffers on each 216 * call, but instead several of the pointers in the hostent structs will 217 * point to the same actual data! This have the unfortunate down-side that 218 * our caching system breaks down horribly. Luckily for us though, AIX 4.3 219 * and more recent versions have a "completely thread-safe"[*] libc where 220 * all the data is stored in thread-specific memory areas making calls to 221 * the plain old gethostbyname() work fine even for multi-threaded 222 * programs. 223 * 224 * This AIX 4.3 or later detection is all made in the configure script. 225 * 226 * Troels Walsted Hansen helped us work this out on March 3rd, 2003. 227 * 228 * [*] = much later we have found out that it is not at all "completely 229 * thread-safe", but at least the gethostbyname() function is. 230 */ 231 232 if(CURL_HOSTENT_SIZE >= 233 (sizeof(struct hostent) + sizeof(struct hostent_data))) { 234 235 /* August 22nd, 2000: Albert Chin-A-Young brought an updated version 236 * that should work! September 20: Richard Prescott worked on the buffer 237 * size dilemma. 238 */ 239 240 res = gethostbyname_r(hostname, 241 (struct hostent *)buf, 242 (struct hostent_data *)((char *)buf + 243 sizeof(struct hostent))); 244 h_errnop = SOCKERRNO; /* we do not deal with this, but set it anyway */ 245 } 246 else 247 res = -1; /* failure, too smallish buffer size */ 248 249 if(!res) { /* success */ 250 251 h = buf; /* result expected in h */ 252 253 /* This is the worst kind of the different gethostbyname_r() interfaces. 254 * Since we do not know how big buffer this particular lookup required, 255 * we cannot realloc down the huge alloc without doing closer analysis of 256 * the returned data. Thus, we always use CURL_HOSTENT_SIZE for every 257 * name lookup. Fixing this would require an extra malloc() and then 258 * calling Curl_addrinfo_copy() that subsequent realloc()s down the new 259 * memory area to the actually used amount. 260 */ 261 } 262 else 263 #endif /* HAVE_...BYNAME_R_5 || HAVE_...BYNAME_R_6 || HAVE_...BYNAME_R_3 */ 264 { 265 h = NULL; /* set return code to NULL */ 266 free(buf); 267 } 268 #else /* (HAVE_GETADDRINFO && HAVE_GETADDRINFO_THREADSAFE) || 269 HAVE_GETHOSTBYNAME_R */ 270 /* 271 * Here is code for platforms that do not have a thread safe 272 * getaddrinfo() nor gethostbyname_r() function or for which 273 * gethostbyname() is the preferred one. 274 */ 275 h = gethostbyname(CURL_UNCONST(hostname)); 276 #endif /* (HAVE_GETADDRINFO && HAVE_GETADDRINFO_THREADSAFE) || 277 HAVE_GETHOSTBYNAME_R */ 278 279 #if !(defined(HAVE_GETADDRINFO) && defined(HAVE_GETADDRINFO_THREADSAFE)) 280 if(h) { 281 ai = Curl_he2ai(h, port); 282 283 if(buf) /* used a *_r() function */ 284 free(buf); 285 } 286 #endif 287 288 return ai; 289 } 290 #endif /* defined(CURLRES_IPV4) && !defined(CURLRES_ARES) && 291 !defined(CURLRES_AMIGA) */