hostip4.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2005, 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. #if defined(WIN32) && !defined(__GNUC__) || defined(__MINGW32__)
  27. #include <malloc.h>
  28. #else
  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. #endif
  56. #ifdef HAVE_SETJMP_H
  57. #include <setjmp.h>
  58. #endif
  59. #ifdef WIN32
  60. #include <process.h>
  61. #endif
  62. #if (defined(NETWARE) && defined(__NOVELL_LIBC__))
  63. #undef in_addr_t
  64. #define in_addr_t unsigned long
  65. #endif
  66. #include "urldata.h"
  67. #include "sendf.h"
  68. #include "hostip.h"
  69. #include "hash.h"
  70. #include "share.h"
  71. #include "strerror.h"
  72. #include "url.h"
  73. #include "inet_pton.h"
  74. #define _MPRINTF_REPLACE /* use our functions only */
  75. #include <curl/mprintf.h>
  76. #if defined(HAVE_INET_NTOA_R) && !defined(HAVE_INET_NTOA_R_DECL)
  77. #include "inet_ntoa_r.h"
  78. #endif
  79. #include "memory.h"
  80. /* The last #include file should be: */
  81. #include "memdebug.h"
  82. /***********************************************************************
  83. * Only for plain-ipv4 builds
  84. **********************************************************************/
  85. #ifdef CURLRES_IPV4 /* plain ipv4 code coming up */
  86. /*
  87. * This is a function for freeing name information in a protocol independent
  88. * way.
  89. */
  90. void Curl_freeaddrinfo(Curl_addrinfo *ai)
  91. {
  92. Curl_addrinfo *next;
  93. /* walk over the list and free all entries */
  94. while(ai) {
  95. next = ai->ai_next;
  96. free(ai);
  97. ai = next;
  98. }
  99. }
  100. /*
  101. * Curl_ipvalid() checks what CURL_IPRESOLVE_* requirements that might've
  102. * been set and returns TRUE if they are OK.
  103. */
  104. bool Curl_ipvalid(struct SessionHandle *data)
  105. {
  106. if(data->set.ip_version == CURL_IPRESOLVE_V6)
  107. /* an ipv6 address was requested and we can't get/use one */
  108. return FALSE;
  109. return TRUE; /* OK, proceed */
  110. }
  111. struct namebuf {
  112. struct hostent hostentry;
  113. char *h_addr_list[2];
  114. struct in_addr addrentry;
  115. char h_name[16]; /* 123.123.123.123 = 15 letters is maximum */
  116. };
  117. /*
  118. * Curl_ip2addr() takes a 32bit ipv4 internet address as input parameter
  119. * together with a pointer to the string version of the address, and it
  120. * returns a Curl_addrinfo chain filled in correctly with information for this
  121. * address/host.
  122. *
  123. * The input parameters ARE NOT checked for validity but they are expected
  124. * to have been checked already when this is called.
  125. */
  126. Curl_addrinfo *Curl_ip2addr(in_addr_t num, char *hostname, int port)
  127. {
  128. Curl_addrinfo *ai;
  129. struct hostent *h;
  130. struct in_addr *addrentry;
  131. struct namebuf buffer;
  132. struct namebuf *buf = &buffer;
  133. h = &buf->hostentry;
  134. h->h_addr_list = &buf->h_addr_list[0];
  135. addrentry = &buf->addrentry;
  136. addrentry->s_addr = num;
  137. h->h_addr_list[0] = (char*)addrentry;
  138. h->h_addr_list[1] = NULL;
  139. h->h_addrtype = AF_INET;
  140. h->h_length = sizeof(*addrentry);
  141. h->h_name = &buf->h_name[0];
  142. h->h_aliases = NULL;
  143. /* Now store the dotted version of the address */
  144. snprintf((char *)h->h_name, 16, "%s", hostname);
  145. ai = Curl_he2ai(h, port);
  146. return ai;
  147. }
  148. #ifdef CURLRES_SYNCH /* the functions below are for synchronous resolves */
  149. /*
  150. * Curl_getaddrinfo() - the ipv4 synchronous version.
  151. *
  152. * The original code to this function was from the Dancer source code, written
  153. * by Bjorn Reese, it has since been patched and modified considerably.
  154. *
  155. * gethostbyname_r() is the thread-safe version of the gethostbyname()
  156. * function. When we build for plain IPv4, we attempt to use this
  157. * function. There are _three_ different gethostbyname_r() versions, and we
  158. * detect which one this platform supports in the configure script and set up
  159. * the HAVE_GETHOSTBYNAME_R_3, HAVE_GETHOSTBYNAME_R_5 or
  160. * HAVE_GETHOSTBYNAME_R_6 defines accordingly. Note that HAVE_GETADDRBYNAME
  161. * has the corresponding rules. This is primarily on *nix. Note that some unix
  162. * flavours have thread-safe versions of the plain gethostbyname() etc.
  163. *
  164. */
  165. Curl_addrinfo *Curl_getaddrinfo(struct connectdata *conn,
  166. char *hostname,
  167. int port,
  168. int *waitp)
  169. {
  170. Curl_addrinfo *ai = NULL;
  171. struct hostent *h = NULL;
  172. in_addr_t in;
  173. struct SessionHandle *data = conn->data;
  174. struct hostent *buf = NULL;
  175. (void)port; /* unused in IPv4 code */
  176. *waitp = 0; /* don't wait, we act synchronously */
  177. if(1 == Curl_inet_pton(AF_INET, hostname, &in))
  178. /* This is a dotted IP address 123.123.123.123-style */
  179. return Curl_ip2addr(in, hostname, port);
  180. #if defined(HAVE_GETHOSTBYNAME_R)
  181. /*
  182. * gethostbyname_r() is the preferred resolve function for many platforms.
  183. * Since there are three different versions of it, the following code is
  184. * somewhat #ifdef-ridden.
  185. */
  186. else {
  187. int h_errnop;
  188. int res=ERANGE;
  189. buf = (struct hostent *)calloc(CURL_HOSTENT_SIZE, 1);
  190. if(!buf)
  191. return NULL; /* major failure */
  192. /*
  193. * The clearing of the buffer is a workaround for a gethostbyname_r bug in
  194. * qnx nto and it is also _required_ for some of these functions on some
  195. * platforms.
  196. */
  197. #ifdef HAVE_GETHOSTBYNAME_R_5
  198. /* Solaris, IRIX and more */
  199. (void)res; /* prevent compiler warning */
  200. h = gethostbyname_r(hostname,
  201. (struct hostent *)buf,
  202. (char *)buf + sizeof(struct hostent),
  203. CURL_HOSTENT_SIZE - sizeof(struct hostent),
  204. &h_errnop);
  205. /* If the buffer is too small, it returns NULL and sets errno to
  206. * ERANGE. The errno is thread safe if this is compiled with
  207. * -D_REENTRANT as then the 'errno' variable is a macro defined to get
  208. * used properly for threads.
  209. */
  210. if(h) {
  211. ;
  212. }
  213. else
  214. #endif /* HAVE_GETHOSTBYNAME_R_5 */
  215. #ifdef HAVE_GETHOSTBYNAME_R_6
  216. /* Linux */
  217. res=gethostbyname_r(hostname,
  218. (struct hostent *)buf,
  219. (char *)buf + sizeof(struct hostent),
  220. CURL_HOSTENT_SIZE - sizeof(struct hostent),
  221. &h, /* DIFFERENCE */
  222. &h_errnop);
  223. /* Redhat 8, using glibc 2.2.93 changed the behavior. Now all of a
  224. * sudden this function returns EAGAIN if the given buffer size is too
  225. * small. Previous versions are known to return ERANGE for the same
  226. * problem.
  227. *
  228. * This wouldn't be such a big problem if older versions wouldn't
  229. * sometimes return EAGAIN on a common failure case. Alas, we can't
  230. * assume that EAGAIN *or* ERANGE means ERANGE for any given version of
  231. * glibc.
  232. *
  233. * For now, we do that and thus we may call the function repeatedly and
  234. * fail for older glibc versions that return EAGAIN, until we run out of
  235. * buffer size (step_size grows beyond CURL_HOSTENT_SIZE).
  236. *
  237. * If anyone has a better fix, please tell us!
  238. *
  239. * -------------------------------------------------------------------
  240. *
  241. * On October 23rd 2003, Dan C dug up more details on the mysteries of
  242. * gethostbyname_r() in glibc:
  243. *
  244. * In glibc 2.2.5 the interface is different (this has also been
  245. * discovered in glibc 2.1.1-6 as shipped by Redhat 6). What I can't
  246. * explain, is that tests performed on glibc 2.2.4-34 and 2.2.4-32
  247. * (shipped/upgraded by Redhat 7.2) don't show this behavior!
  248. *
  249. * In this "buggy" version, the return code is -1 on error and 'errno'
  250. * is set to the ERANGE or EAGAIN code. Note that 'errno' is not a
  251. * thread-safe variable.
  252. */
  253. if(!h) /* failure */
  254. #endif/* HAVE_GETHOSTBYNAME_R_6 */
  255. #ifdef HAVE_GETHOSTBYNAME_R_3
  256. /* AIX, Digital Unix/Tru64, HPUX 10, more? */
  257. /* For AIX 4.3 or later, we don't use gethostbyname_r() at all, because of
  258. * the plain fact that it does not return unique full buffers on each
  259. * call, but instead several of the pointers in the hostent structs will
  260. * point to the same actual data! This have the unfortunate down-side that
  261. * our caching system breaks down horribly. Luckily for us though, AIX 4.3
  262. * and more recent versions have a "completely thread-safe"[*] libc where
  263. * all the data is stored in thread-specific memory areas making calls to
  264. * the plain old gethostbyname() work fine even for multi-threaded
  265. * programs.
  266. *
  267. * This AIX 4.3 or later detection is all made in the configure script.
  268. *
  269. * Troels Walsted Hansen helped us work this out on March 3rd, 2003.
  270. *
  271. * [*] = much later we've found out that it isn't at all "completely
  272. * thread-safe", but at least the gethostbyname() function is.
  273. */
  274. if(CURL_HOSTENT_SIZE >=
  275. (sizeof(struct hostent)+sizeof(struct hostent_data))) {
  276. /* August 22nd, 2000: Albert Chin-A-Young brought an updated version
  277. * that should work! September 20: Richard Prescott worked on the buffer
  278. * size dilemma.
  279. */
  280. res = gethostbyname_r(hostname,
  281. (struct hostent *)buf,
  282. (struct hostent_data *)((char *)buf +
  283. sizeof(struct hostent)));
  284. h_errnop= errno; /* we don't deal with this, but set it anyway */
  285. }
  286. else
  287. res = -1; /* failure, too smallish buffer size */
  288. if(!res) { /* success */
  289. h = buf; /* result expected in h */
  290. /* This is the worst kind of the different gethostbyname_r() interfaces.
  291. * Since we don't know how big buffer this particular lookup required,
  292. * we can't realloc down the huge alloc without doing closer analysis of
  293. * the returned data. Thus, we always use CURL_HOSTENT_SIZE for every
  294. * name lookup. Fixing this would require an extra malloc() and then
  295. * calling Curl_addrinfo_copy() that subsequent realloc()s down the new
  296. * memory area to the actually used amount.
  297. */
  298. }
  299. else
  300. #endif /* HAVE_GETHOSTBYNAME_R_3 */
  301. {
  302. infof(data, "gethostbyname_r(2) failed for %s\n", hostname);
  303. h = NULL; /* set return code to NULL */
  304. free(buf);
  305. }
  306. #else /* HAVE_GETHOSTBYNAME_R */
  307. /*
  308. * Here is code for platforms that don't have gethostbyname_r() or for
  309. * which the gethostbyname() is the preferred() function.
  310. */
  311. else {
  312. h = gethostbyname(hostname);
  313. if (!h)
  314. infof(data, "gethostbyname(2) failed for %s\n", hostname);
  315. #endif /*HAVE_GETHOSTBYNAME_R */
  316. }
  317. if(h) {
  318. ai = Curl_he2ai(h, port);
  319. if (buf) /* used a *_r() function */
  320. free(buf);
  321. }
  322. return ai;
  323. }
  324. #endif /* CURLRES_SYNCH */
  325. /*
  326. * Curl_he2ai() translates from a hostent struct to a Curl_addrinfo struct.
  327. * The Curl_addrinfo is meant to work like the addrinfo struct does for IPv6
  328. * stacks, but for all hosts and environments.
  329. struct Curl_addrinfo {
  330. int ai_flags;
  331. int ai_family;
  332. int ai_socktype;
  333. int ai_protocol;
  334. size_t ai_addrlen;
  335. struct sockaddr *ai_addr;
  336. char *ai_canonname;
  337. struct addrinfo *ai_next;
  338. };
  339. struct hostent {
  340. char *h_name; * official name of host *
  341. char **h_aliases; * alias list *
  342. int h_addrtype; * host address type *
  343. int h_length; * length of address *
  344. char **h_addr_list; * list of addresses *
  345. }
  346. #define h_addr h_addr_list[0] * for backward compatibility *
  347. */
  348. Curl_addrinfo *Curl_he2ai(struct hostent *he, int port)
  349. {
  350. Curl_addrinfo *ai;
  351. Curl_addrinfo *prevai = NULL;
  352. Curl_addrinfo *firstai = NULL;
  353. struct sockaddr_in *addr;
  354. int i;
  355. struct in_addr *curr;
  356. if(!he)
  357. /* no input == no output! */
  358. return NULL;
  359. for(i=0; (curr = (struct in_addr *)he->h_addr_list[i]); i++) {
  360. ai = calloc(1, sizeof(Curl_addrinfo) + sizeof(struct sockaddr_in));
  361. if(!ai)
  362. break;
  363. if(!firstai)
  364. /* store the pointer we want to return from this function */
  365. firstai = ai;
  366. if(prevai)
  367. /* make the previous entry point to this */
  368. prevai->ai_next = ai;
  369. ai->ai_family = AF_INET; /* we only support this */
  370. ai->ai_socktype = SOCK_STREAM; /* we only support this */
  371. ai->ai_addrlen = sizeof(struct sockaddr_in);
  372. /* make the ai_addr point to the address immediately following this struct
  373. and use that area to store the address */
  374. ai->ai_addr = (struct sockaddr *) ((char*)ai + sizeof(Curl_addrinfo));
  375. /* leave the rest of the struct filled with zero */
  376. addr = (struct sockaddr_in *)ai->ai_addr; /* storage area for this info */
  377. memcpy((char *)&(addr->sin_addr), curr, sizeof(struct in_addr));
  378. addr->sin_family = he->h_addrtype;
  379. addr->sin_port = htons((unsigned short)port);
  380. prevai = ai;
  381. }
  382. return firstai;
  383. }
  384. #endif /* CURLRES_IPV4 */