hostip4.c 9.9 KB

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