hostip4.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2006, 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 NEED_MALLOC_H
  27. #include <malloc.h>
  28. #endif
  29. #ifdef HAVE_SYS_TYPES_H
  30. #include <sys/types.h>
  31. #endif
  32. #ifdef HAVE_SYS_SOCKET_H
  33. #include <sys/socket.h>
  34. #endif
  35. #ifdef HAVE_NETINET_IN_H
  36. #include <netinet/in.h>
  37. #endif
  38. #ifdef HAVE_NETDB_H
  39. #include <netdb.h>
  40. #endif
  41. #ifdef HAVE_ARPA_INET_H
  42. #include <arpa/inet.h>
  43. #endif
  44. #ifdef HAVE_STDLIB_H
  45. #include <stdlib.h> /* required for free() prototypes */
  46. #endif
  47. #ifdef HAVE_UNISTD_H
  48. #include <unistd.h> /* for the close() proto */
  49. #endif
  50. #ifdef VMS
  51. #include <in.h>
  52. #include <inet.h>
  53. #include <stdlib.h>
  54. #endif
  55. #ifdef HAVE_SETJMP_H
  56. #include <setjmp.h>
  57. #endif
  58. #ifdef HAVE_PROCESS_H
  59. #include <process.h>
  60. #endif
  61. #include "urldata.h"
  62. #include "sendf.h"
  63. #include "hostip.h"
  64. #include "hash.h"
  65. #include "share.h"
  66. #include "strerror.h"
  67. #include "url.h"
  68. #include "inet_pton.h"
  69. #define _MPRINTF_REPLACE /* use our functions only */
  70. #include <curl/mprintf.h>
  71. #if defined(HAVE_INET_NTOA_R) && !defined(HAVE_INET_NTOA_R_DECL)
  72. #include "inet_ntoa_r.h"
  73. #endif
  74. #include "memory.h"
  75. /* The last #include file should be: */
  76. #include "memdebug.h"
  77. /***********************************************************************
  78. * Only for plain-ipv4 builds
  79. **********************************************************************/
  80. #ifdef CURLRES_IPV4 /* plain ipv4 code coming up */
  81. /*
  82. * Curl_ipvalid() checks what CURL_IPRESOLVE_* requirements that might've
  83. * been set and returns TRUE if they are OK.
  84. */
  85. bool Curl_ipvalid(struct SessionHandle *data)
  86. {
  87. if(data->set.ip_version == CURL_IPRESOLVE_V6)
  88. /* an ipv6 address was requested and we can't get/use one */
  89. return FALSE;
  90. return TRUE; /* OK, proceed */
  91. }
  92. #ifdef CURLRES_SYNCH /* the functions below are for synchronous resolves */
  93. /*
  94. * Curl_getaddrinfo() - the ipv4 synchronous version.
  95. *
  96. * The original code to this function was from the Dancer source code, written
  97. * by Bjorn Reese, it has since been patched and modified considerably.
  98. *
  99. * gethostbyname_r() is the thread-safe version of the gethostbyname()
  100. * function. When we build for plain IPv4, we attempt to use this
  101. * function. There are _three_ different gethostbyname_r() versions, and we
  102. * detect which one this platform supports in the configure script and set up
  103. * the HAVE_GETHOSTBYNAME_R_3, HAVE_GETHOSTBYNAME_R_5 or
  104. * HAVE_GETHOSTBYNAME_R_6 defines accordingly. Note that HAVE_GETADDRBYNAME
  105. * has the corresponding rules. This is primarily on *nix. Note that some unix
  106. * flavours have thread-safe versions of the plain gethostbyname() etc.
  107. *
  108. */
  109. Curl_addrinfo *Curl_getaddrinfo(struct connectdata *conn,
  110. const char *hostname,
  111. int port,
  112. int *waitp)
  113. {
  114. Curl_addrinfo *ai = NULL;
  115. struct hostent *h = NULL;
  116. in_addr_t in;
  117. struct SessionHandle *data = conn->data;
  118. struct hostent *buf = NULL;
  119. (void)port; /* unused in IPv4 code */
  120. *waitp = 0; /* don't wait, we act synchronously */
  121. if(1 == Curl_inet_pton(AF_INET, hostname, &in))
  122. /* This is a dotted IP address 123.123.123.123-style */
  123. return Curl_ip2addr(in, hostname, port);
  124. #if defined(HAVE_GETHOSTBYNAME_R)
  125. /*
  126. * gethostbyname_r() is the preferred resolve function for many platforms.
  127. * Since there are three different versions of it, the following code is
  128. * somewhat #ifdef-ridden.
  129. */
  130. else {
  131. int h_errnop;
  132. int res=ERANGE;
  133. buf = (struct hostent *)calloc(CURL_HOSTENT_SIZE, 1);
  134. if(!buf)
  135. return NULL; /* major failure */
  136. /*
  137. * The clearing of the buffer is a workaround for a gethostbyname_r bug in
  138. * qnx nto and it is also _required_ for some of these functions on some
  139. * platforms.
  140. */
  141. #ifdef HAVE_GETHOSTBYNAME_R_5
  142. /* Solaris, IRIX and more */
  143. (void)res; /* prevent compiler warning */
  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. res=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= errno; /* 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(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. h = gethostbyname(hostname);
  257. if (!h)
  258. infof(data, "gethostbyname(2) failed for %s\n", hostname);
  259. #endif /*HAVE_GETHOSTBYNAME_R */
  260. }
  261. if(h) {
  262. ai = Curl_he2ai(h, port);
  263. if (buf) /* used a *_r() function */
  264. free(buf);
  265. }
  266. return ai;
  267. }
  268. #endif /* CURLRES_SYNCH */
  269. #endif /* CURLRES_IPV4 */
  270. /*
  271. * Curl_he2ai() translates from a hostent struct to a Curl_addrinfo struct.
  272. * The Curl_addrinfo is meant to work like the addrinfo struct does for IPv6
  273. * stacks, but for all hosts and environments.
  274. *
  275. * Curl_addrinfo defined in "lib/hostip.h"
  276. *
  277. * struct Curl_addrinfo {
  278. * int ai_flags;
  279. * int ai_family;
  280. * int ai_socktype;
  281. * int ai_protocol;
  282. * socklen_t ai_addrlen; * Follow rfc3493 struct addrinfo *
  283. * char *ai_canonname;
  284. * struct sockaddr *ai_addr;
  285. * struct Curl_addrinfo *ai_next;
  286. * };
  287. *
  288. * hostent defined in <netdb.h>
  289. *
  290. * struct hostent {
  291. * char *h_name;
  292. * char **h_aliases;
  293. * int h_addrtype;
  294. * int h_length;
  295. * char **h_addr_list;
  296. * };
  297. *
  298. * for backward compatibility:
  299. *
  300. * #define h_addr h_addr_list[0]
  301. */
  302. Curl_addrinfo *Curl_he2ai(const struct hostent *he, int port)
  303. {
  304. Curl_addrinfo *ai;
  305. Curl_addrinfo *prevai = NULL;
  306. Curl_addrinfo *firstai = NULL;
  307. struct sockaddr_in *addr;
  308. int i;
  309. struct in_addr *curr;
  310. if(!he)
  311. /* no input == no output! */
  312. return NULL;
  313. for(i=0; (curr = (struct in_addr *)he->h_addr_list[i]) != NULL; i++) {
  314. ai = calloc(1, sizeof(Curl_addrinfo) + sizeof(struct sockaddr_in));
  315. if(!ai)
  316. break;
  317. if(!firstai)
  318. /* store the pointer we want to return from this function */
  319. firstai = ai;
  320. if(prevai)
  321. /* make the previous entry point to this */
  322. prevai->ai_next = ai;
  323. ai->ai_family = AF_INET; /* we only support this */
  324. /* we return all names as STREAM, so when using this address for TFTP
  325. the type must be ignored and conn->socktype be used instead! */
  326. ai->ai_socktype = SOCK_STREAM;
  327. ai->ai_addrlen = sizeof(struct sockaddr_in);
  328. /* make the ai_addr point to the address immediately following this struct
  329. and use that area to store the address */
  330. ai->ai_addr = (struct sockaddr *) ((char*)ai + sizeof(Curl_addrinfo));
  331. /* leave the rest of the struct filled with zero */
  332. addr = (struct sockaddr_in *)ai->ai_addr; /* storage area for this info */
  333. memcpy((char *)&(addr->sin_addr), curr, sizeof(struct in_addr));
  334. addr->sin_family = he->h_addrtype;
  335. addr->sin_port = htons((unsigned short)port);
  336. prevai = ai;
  337. }
  338. return firstai;
  339. }