hostip4.c 9.9 KB

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