hostip4.c 11 KB

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