hostip4.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2008, 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 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 NEED_MALLOC_H
  27. #include <malloc.h>
  28. #endif
  29. #ifdef HAVE_SYS_SOCKET_H
  30. #include <sys/socket.h>
  31. #endif
  32. #ifdef HAVE_NETINET_IN_H
  33. #include <netinet/in.h>
  34. #endif
  35. #ifdef HAVE_NETDB_H
  36. #include <netdb.h>
  37. #endif
  38. #ifdef HAVE_ARPA_INET_H
  39. #include <arpa/inet.h>
  40. #endif
  41. #ifdef HAVE_STDLIB_H
  42. #include <stdlib.h> /* required for free() prototypes */
  43. #endif
  44. #ifdef HAVE_UNISTD_H
  45. #include <unistd.h> /* for the close() proto */
  46. #endif
  47. #ifdef VMS
  48. #include <in.h>
  49. #include <inet.h>
  50. #include <stdlib.h>
  51. #endif
  52. #ifdef HAVE_SETJMP_H
  53. #include <setjmp.h>
  54. #endif
  55. #ifdef HAVE_PROCESS_H
  56. #include <process.h>
  57. #endif
  58. #include "urldata.h"
  59. #include "sendf.h"
  60. #include "hostip.h"
  61. #include "hash.h"
  62. #include "share.h"
  63. #include "strerror.h"
  64. #include "url.h"
  65. #include "inet_pton.h"
  66. #define _MPRINTF_REPLACE /* use our functions only */
  67. #include <curl/mprintf.h>
  68. #if defined(HAVE_INET_NTOA_R) && !defined(HAVE_INET_NTOA_R_DECL)
  69. #include "inet_ntoa_r.h"
  70. #endif
  71. #include "memory.h"
  72. /* The last #include file should be: */
  73. #include "memdebug.h"
  74. /***********************************************************************
  75. * Only for plain-ipv4 builds
  76. **********************************************************************/
  77. #ifdef CURLRES_IPV4 /* plain ipv4 code coming up */
  78. /*
  79. * Curl_ipvalid() checks what CURL_IPRESOLVE_* requirements that might've
  80. * been set and returns TRUE if they are OK.
  81. */
  82. bool Curl_ipvalid(struct SessionHandle *data)
  83. {
  84. if(data->set.ip_version == CURL_IPRESOLVE_V6)
  85. /* an ipv6 address was requested and we can't get/use one */
  86. return FALSE;
  87. return TRUE; /* OK, proceed */
  88. }
  89. #ifdef CURLRES_SYNCH /* the functions below are for synchronous resolves */
  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. #if defined(HAVE_GETHOSTBYNAME_R_3)
  112. int res;
  113. #endif
  114. Curl_addrinfo *ai = NULL;
  115. struct hostent *h = NULL;
  116. in_addr_t in;
  117. struct hostent *buf = NULL;
  118. #ifdef CURL_DISABLE_VERBOSE_STRINGS
  119. (void)conn;
  120. #endif
  121. (void)port; /* unused in IPv4 code */
  122. *waitp = 0; /* don't wait, we act synchronously */
  123. if(1 == Curl_inet_pton(AF_INET, hostname, &in))
  124. /* This is a dotted IP address 123.123.123.123-style */
  125. return Curl_ip2addr(in, hostname, port);
  126. #if defined(HAVE_GETHOSTBYNAME_R)
  127. /*
  128. * gethostbyname_r() is the preferred resolve function for many platforms.
  129. * Since there are three different versions of it, the following code is
  130. * somewhat #ifdef-ridden.
  131. */
  132. else {
  133. int h_errnop;
  134. buf = (struct hostent *)calloc(CURL_HOSTENT_SIZE, 1);
  135. if(!buf)
  136. return NULL; /* major failure */
  137. /*
  138. * The clearing of the buffer is a workaround for a gethostbyname_r bug in
  139. * qnx nto and it is also _required_ for some of these functions on some
  140. * platforms.
  141. */
  142. #ifdef HAVE_GETHOSTBYNAME_R_5
  143. /* Solaris, IRIX and more */
  144. h = gethostbyname_r(hostname,
  145. (struct hostent *)buf,
  146. (char *)buf + sizeof(struct hostent),
  147. CURL_HOSTENT_SIZE - sizeof(struct hostent),
  148. &h_errnop);
  149. /* If the buffer is too small, it returns NULL and sets errno to
  150. * ERANGE. The errno is thread safe if this is compiled with
  151. * -D_REENTRANT as then the 'errno' variable is a macro defined to get
  152. * used properly for threads.
  153. */
  154. if(h) {
  155. ;
  156. }
  157. else
  158. #endif /* HAVE_GETHOSTBYNAME_R_5 */
  159. #ifdef 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. #endif/* HAVE_GETHOSTBYNAME_R_6 */
  199. #ifdef HAVE_GETHOSTBYNAME_R_3
  200. /* AIX, Digital Unix/Tru64, HPUX 10, more? */
  201. /* For AIX 4.3 or later, we don't use gethostbyname_r() at all, because of
  202. * the plain fact that it does not return unique full buffers on each
  203. * call, but instead several of the pointers in the hostent structs will
  204. * point to the same actual data! This have the unfortunate down-side that
  205. * our caching system breaks down horribly. Luckily for us though, AIX 4.3
  206. * and more recent versions have a "completely thread-safe"[*] libc where
  207. * all the data is stored in thread-specific memory areas making calls to
  208. * the plain old gethostbyname() work fine even for multi-threaded
  209. * programs.
  210. *
  211. * This AIX 4.3 or later detection is all made in the configure script.
  212. *
  213. * Troels Walsted Hansen helped us work this out on March 3rd, 2003.
  214. *
  215. * [*] = much later we've found out that it isn't at all "completely
  216. * thread-safe", but at least the gethostbyname() function is.
  217. */
  218. if(CURL_HOSTENT_SIZE >=
  219. (sizeof(struct hostent)+sizeof(struct hostent_data))) {
  220. /* August 22nd, 2000: Albert Chin-A-Young brought an updated version
  221. * that should work! September 20: Richard Prescott worked on the buffer
  222. * size dilemma.
  223. */
  224. res = gethostbyname_r(hostname,
  225. (struct hostent *)buf,
  226. (struct hostent_data *)((char *)buf +
  227. sizeof(struct hostent)));
  228. h_errnop = SOCKERRNO; /* we don't deal with this, but set it anyway */
  229. }
  230. else
  231. res = -1; /* failure, too smallish buffer size */
  232. if(!res) { /* success */
  233. h = buf; /* result expected in h */
  234. /* This is the worst kind of the different gethostbyname_r() interfaces.
  235. * Since we don't know how big buffer this particular lookup required,
  236. * we can't realloc down the huge alloc without doing closer analysis of
  237. * the returned data. Thus, we always use CURL_HOSTENT_SIZE for every
  238. * name lookup. Fixing this would require an extra malloc() and then
  239. * calling Curl_addrinfo_copy() that subsequent realloc()s down the new
  240. * memory area to the actually used amount.
  241. */
  242. }
  243. else
  244. #endif /* HAVE_GETHOSTBYNAME_R_3 */
  245. {
  246. infof(conn->data, "gethostbyname_r(2) failed for %s\n", hostname);
  247. h = NULL; /* set return code to NULL */
  248. free(buf);
  249. }
  250. #else /* HAVE_GETHOSTBYNAME_R */
  251. /*
  252. * Here is code for platforms that don't have gethostbyname_r() or for
  253. * which the gethostbyname() is the preferred() function.
  254. */
  255. else {
  256. #if (defined(NETWARE) && !defined(__NOVELL_LIBC__))
  257. h = gethostbyname((char*)hostname);
  258. #else
  259. h = gethostbyname(hostname);
  260. #endif
  261. if(!h)
  262. infof(conn->data, "gethostbyname(2) failed for %s\n", hostname);
  263. #endif /*HAVE_GETHOSTBYNAME_R */
  264. }
  265. if(h) {
  266. ai = Curl_he2ai(h, port);
  267. if(buf) /* used a *_r() function */
  268. free(buf);
  269. }
  270. return ai;
  271. }
  272. #endif /* CURLRES_SYNCH */
  273. #endif /* CURLRES_IPV4 */