hostip4.c 10 KB

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