Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 1 | /*************************************************************************** |
| 2 | * _ _ ____ _ |
| 3 | * Project ___| | | | _ \| | |
| 4 | * / __| | | | |_) | | |
| 5 | * | (__| |_| | _ <| |___ |
| 6 | * \___|\___/|_| \_\_____| |
| 7 | * |
| 8 | * Copyright (C) 1998 - 2010, 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 http://curl.haxx.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 | ***************************************************************************/ |
| 22 | |
| 23 | #include "setup.h" |
| 24 | |
| 25 | #include <string.h> |
| 26 | #include <errno.h> |
| 27 | |
| 28 | #ifdef HAVE_SYS_SOCKET_H |
| 29 | #include <sys/socket.h> |
| 30 | #endif |
| 31 | #ifdef HAVE_NETINET_IN_H |
| 32 | #include <netinet/in.h> |
| 33 | #endif |
| 34 | #ifdef HAVE_NETDB_H |
| 35 | #include <netdb.h> |
| 36 | #endif |
| 37 | #ifdef HAVE_ARPA_INET_H |
| 38 | #include <arpa/inet.h> |
| 39 | #endif |
| 40 | #ifdef HAVE_STDLIB_H |
| 41 | #include <stdlib.h> /* required for free() prototypes */ |
| 42 | #endif |
| 43 | #ifdef HAVE_UNISTD_H |
| 44 | #include <unistd.h> /* for the close() proto */ |
| 45 | #endif |
| 46 | #ifdef __VMS |
| 47 | #include <in.h> |
| 48 | #include <inet.h> |
| 49 | #include <stdlib.h> |
| 50 | #endif |
| 51 | |
| 52 | #ifdef HAVE_PROCESS_H |
| 53 | #include <process.h> |
| 54 | #endif |
| 55 | |
| 56 | #include "urldata.h" |
| 57 | #include "sendf.h" |
| 58 | #include "hostip.h" |
| 59 | #include "hash.h" |
| 60 | #include "share.h" |
| 61 | #include "strerror.h" |
| 62 | #include "url.h" |
| 63 | #include "inet_pton.h" |
| 64 | |
| 65 | #define _MPRINTF_REPLACE /* use our functions only */ |
| 66 | #include <curl/mprintf.h> |
| 67 | |
| 68 | #include "curl_memory.h" |
| 69 | /* The last #include file should be: */ |
| 70 | #include "memdebug.h" |
| 71 | |
| 72 | /*********************************************************************** |
| 73 | * Only for plain-ipv4 builds |
| 74 | **********************************************************************/ |
| 75 | #ifdef CURLRES_IPV4 /* plain ipv4 code coming up */ |
| 76 | /* |
| 77 | * Curl_ipvalid() checks what CURL_IPRESOLVE_* requirements that might've |
| 78 | * been set and returns TRUE if they are OK. |
| 79 | */ |
| 80 | bool Curl_ipvalid(struct SessionHandle *data) |
| 81 | { |
| 82 | if(data->set.ip_version == CURL_IPRESOLVE_V6) |
| 83 | /* an ipv6 address was requested and we can't get/use one */ |
| 84 | return FALSE; |
| 85 | |
| 86 | return TRUE; /* OK, proceed */ |
| 87 | } |
| 88 | |
| 89 | #ifdef CURLRES_SYNCH |
| 90 | /* |
| 91 | * Curl_getaddrinfo() - the ipv4 synchronous version. |
| 92 | * |
| 93 | * The original code to this function was from the Dancer source code, written |
| 94 | * by Bjorn Reese, it has since been patched and modified considerably. |
| 95 | * |
| 96 | * gethostbyname_r() is the thread-safe version of the gethostbyname() |
| 97 | * function. When we build for plain IPv4, we attempt to use this |
| 98 | * function. There are _three_ different gethostbyname_r() versions, and we |
| 99 | * detect which one this platform supports in the configure script and set up |
| 100 | * the HAVE_GETHOSTBYNAME_R_3, HAVE_GETHOSTBYNAME_R_5 or |
| 101 | * HAVE_GETHOSTBYNAME_R_6 defines accordingly. Note that HAVE_GETADDRBYNAME |
| 102 | * has the corresponding rules. This is primarily on *nix. Note that some unix |
| 103 | * flavours have thread-safe versions of the plain gethostbyname() etc. |
| 104 | * |
| 105 | */ |
| 106 | Curl_addrinfo *Curl_getaddrinfo(struct connectdata *conn, |
| 107 | const char *hostname, |
| 108 | int port, |
| 109 | int *waitp) |
| 110 | { |
| 111 | Curl_addrinfo *ai = NULL; |
| 112 | |
| 113 | #ifdef CURL_DISABLE_VERBOSE_STRINGS |
| 114 | (void)conn; |
| 115 | #endif |
| 116 | |
| 117 | *waitp = 0; /* synchronous response only */ |
| 118 | |
| 119 | ai = Curl_ipv4_resolve_r(hostname, port); |
| 120 | if(!ai) |
| 121 | infof(conn->data, "Curl_ipv4_resolve_r failed for %s\n", hostname); |
| 122 | |
| 123 | return ai; |
| 124 | } |
| 125 | #endif /* CURLRES_SYNCH */ |
| 126 | #endif /* CURLRES_IPV4 */ |
| 127 | |
| 128 | /* |
| 129 | * Curl_ipv4_resolve_r() - ipv4 threadsafe resolver function. |
| 130 | * |
| 131 | * This is used for both synchronous and asynchronous resolver builds, |
| 132 | * implying that only threadsafe code and function calls may be used. |
| 133 | * |
| 134 | */ |
| 135 | Curl_addrinfo *Curl_ipv4_resolve_r(const char *hostname, |
| 136 | int port) |
| 137 | { |
| 138 | #if !defined(HAVE_GETADDRINFO_THREADSAFE) && defined(HAVE_GETHOSTBYNAME_R_3) |
| 139 | int res; |
| 140 | #endif |
| 141 | Curl_addrinfo *ai = NULL; |
| 142 | struct hostent *h = NULL; |
| 143 | struct in_addr in; |
| 144 | struct hostent *buf = NULL; |
| 145 | |
| 146 | if(Curl_inet_pton(AF_INET, hostname, &in) > 0) |
| 147 | /* This is a dotted IP address 123.123.123.123-style */ |
| 148 | return Curl_ip2addr(AF_INET, &in, hostname, port); |
| 149 | |
| 150 | #if defined(HAVE_GETADDRINFO_THREADSAFE) |
| 151 | else { |
| 152 | struct addrinfo hints; |
| 153 | char sbuf[NI_MAXSERV]; |
| 154 | char *sbufptr = NULL; |
| 155 | |
| 156 | memset(&hints, 0, sizeof(hints)); |
| 157 | hints.ai_family = PF_INET; |
| 158 | hints.ai_socktype = SOCK_STREAM; |
| 159 | if(port) { |
| 160 | snprintf(sbuf, sizeof(sbuf), "%d", port); |
| 161 | sbufptr = sbuf; |
| 162 | } |
Lucas Eckels | 9bd90e6 | 2012-08-06 15:07:02 -0700 | [diff] [blame^] | 163 | |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 164 | (void)Curl_getaddrinfo_ex(hostname, sbufptr, &hints, &ai); |
| 165 | |
| 166 | #elif defined(HAVE_GETHOSTBYNAME_R) |
| 167 | /* |
| 168 | * gethostbyname_r() is the preferred resolve function for many platforms. |
| 169 | * Since there are three different versions of it, the following code is |
| 170 | * somewhat #ifdef-ridden. |
| 171 | */ |
| 172 | else { |
| 173 | int h_errnop; |
| 174 | |
| 175 | buf = calloc(1, CURL_HOSTENT_SIZE); |
| 176 | if(!buf) |
| 177 | return NULL; /* major failure */ |
| 178 | /* |
| 179 | * The clearing of the buffer is a workaround for a gethostbyname_r bug in |
| 180 | * qnx nto and it is also _required_ for some of these functions on some |
| 181 | * platforms. |
| 182 | */ |
| 183 | |
| 184 | #if defined(HAVE_GETHOSTBYNAME_R_5) |
| 185 | /* Solaris, IRIX and more */ |
| 186 | h = gethostbyname_r(hostname, |
| 187 | (struct hostent *)buf, |
| 188 | (char *)buf + sizeof(struct hostent), |
| 189 | CURL_HOSTENT_SIZE - sizeof(struct hostent), |
| 190 | &h_errnop); |
| 191 | |
| 192 | /* If the buffer is too small, it returns NULL and sets errno to |
| 193 | * ERANGE. The errno is thread safe if this is compiled with |
| 194 | * -D_REENTRANT as then the 'errno' variable is a macro defined to get |
| 195 | * used properly for threads. |
| 196 | */ |
| 197 | |
| 198 | if(h) { |
| 199 | ; |
| 200 | } |
| 201 | else |
| 202 | #elif defined(HAVE_GETHOSTBYNAME_R_6) |
| 203 | /* Linux */ |
| 204 | |
| 205 | (void)gethostbyname_r(hostname, |
| 206 | (struct hostent *)buf, |
| 207 | (char *)buf + sizeof(struct hostent), |
| 208 | CURL_HOSTENT_SIZE - sizeof(struct hostent), |
| 209 | &h, /* DIFFERENCE */ |
| 210 | &h_errnop); |
| 211 | /* Redhat 8, using glibc 2.2.93 changed the behavior. Now all of a |
| 212 | * sudden this function returns EAGAIN if the given buffer size is too |
| 213 | * small. Previous versions are known to return ERANGE for the same |
| 214 | * problem. |
| 215 | * |
| 216 | * This wouldn't be such a big problem if older versions wouldn't |
| 217 | * sometimes return EAGAIN on a common failure case. Alas, we can't |
| 218 | * assume that EAGAIN *or* ERANGE means ERANGE for any given version of |
| 219 | * glibc. |
| 220 | * |
| 221 | * For now, we do that and thus we may call the function repeatedly and |
| 222 | * fail for older glibc versions that return EAGAIN, until we run out of |
| 223 | * buffer size (step_size grows beyond CURL_HOSTENT_SIZE). |
| 224 | * |
| 225 | * If anyone has a better fix, please tell us! |
| 226 | * |
| 227 | * ------------------------------------------------------------------- |
| 228 | * |
| 229 | * On October 23rd 2003, Dan C dug up more details on the mysteries of |
| 230 | * gethostbyname_r() in glibc: |
| 231 | * |
| 232 | * In glibc 2.2.5 the interface is different (this has also been |
| 233 | * discovered in glibc 2.1.1-6 as shipped by Redhat 6). What I can't |
| 234 | * explain, is that tests performed on glibc 2.2.4-34 and 2.2.4-32 |
| 235 | * (shipped/upgraded by Redhat 7.2) don't show this behavior! |
| 236 | * |
| 237 | * In this "buggy" version, the return code is -1 on error and 'errno' |
| 238 | * is set to the ERANGE or EAGAIN code. Note that 'errno' is not a |
| 239 | * thread-safe variable. |
| 240 | */ |
| 241 | |
| 242 | if(!h) /* failure */ |
| 243 | #elif defined(HAVE_GETHOSTBYNAME_R_3) |
| 244 | /* AIX, Digital Unix/Tru64, HPUX 10, more? */ |
| 245 | |
| 246 | /* For AIX 4.3 or later, we don't use gethostbyname_r() at all, because of |
| 247 | * the plain fact that it does not return unique full buffers on each |
| 248 | * call, but instead several of the pointers in the hostent structs will |
| 249 | * point to the same actual data! This have the unfortunate down-side that |
| 250 | * our caching system breaks down horribly. Luckily for us though, AIX 4.3 |
| 251 | * and more recent versions have a "completely thread-safe"[*] libc where |
| 252 | * all the data is stored in thread-specific memory areas making calls to |
| 253 | * the plain old gethostbyname() work fine even for multi-threaded |
| 254 | * programs. |
| 255 | * |
| 256 | * This AIX 4.3 or later detection is all made in the configure script. |
| 257 | * |
| 258 | * Troels Walsted Hansen helped us work this out on March 3rd, 2003. |
| 259 | * |
| 260 | * [*] = much later we've found out that it isn't at all "completely |
| 261 | * thread-safe", but at least the gethostbyname() function is. |
| 262 | */ |
| 263 | |
| 264 | if(CURL_HOSTENT_SIZE >= |
| 265 | (sizeof(struct hostent)+sizeof(struct hostent_data))) { |
| 266 | |
| 267 | /* August 22nd, 2000: Albert Chin-A-Young brought an updated version |
| 268 | * that should work! September 20: Richard Prescott worked on the buffer |
| 269 | * size dilemma. |
| 270 | */ |
| 271 | |
| 272 | res = gethostbyname_r(hostname, |
| 273 | (struct hostent *)buf, |
| 274 | (struct hostent_data *)((char *)buf + |
| 275 | sizeof(struct hostent))); |
| 276 | h_errnop = SOCKERRNO; /* we don't deal with this, but set it anyway */ |
| 277 | } |
| 278 | else |
| 279 | res = -1; /* failure, too smallish buffer size */ |
| 280 | |
| 281 | if(!res) { /* success */ |
| 282 | |
| 283 | h = buf; /* result expected in h */ |
| 284 | |
| 285 | /* This is the worst kind of the different gethostbyname_r() interfaces. |
| 286 | * Since we don't know how big buffer this particular lookup required, |
| 287 | * we can't realloc down the huge alloc without doing closer analysis of |
| 288 | * the returned data. Thus, we always use CURL_HOSTENT_SIZE for every |
| 289 | * name lookup. Fixing this would require an extra malloc() and then |
| 290 | * calling Curl_addrinfo_copy() that subsequent realloc()s down the new |
| 291 | * memory area to the actually used amount. |
| 292 | */ |
| 293 | } |
| 294 | else |
| 295 | #endif /* HAVE_...BYNAME_R_5 || HAVE_...BYNAME_R_6 || HAVE_...BYNAME_R_3 */ |
| 296 | { |
| 297 | h = NULL; /* set return code to NULL */ |
| 298 | free(buf); |
| 299 | } |
| 300 | #else /* HAVE_GETADDRINFO_THREADSAFE || HAVE_GETHOSTBYNAME_R */ |
| 301 | /* |
| 302 | * Here is code for platforms that don't have a thread safe |
| 303 | * getaddrinfo() nor gethostbyname_r() function or for which |
| 304 | * gethostbyname() is the preferred one. |
| 305 | */ |
| 306 | else { |
| 307 | h = gethostbyname((void*)hostname); |
| 308 | #endif /* HAVE_GETADDRINFO_THREADSAFE || HAVE_GETHOSTBYNAME_R */ |
| 309 | } |
| 310 | |
| 311 | if(h) { |
| 312 | ai = Curl_he2ai(h, port); |
| 313 | |
| 314 | if(buf) /* used a *_r() function */ |
| 315 | free(buf); |
| 316 | } |
| 317 | |
| 318 | return ai; |
| 319 | } |