hostip4.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2004, 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. #define _REENTRANT
  27. #if defined(WIN32) && !defined(__GNUC__) || defined(__MINGW32__)
  28. #include <malloc.h>
  29. #else
  30. #ifdef HAVE_SYS_TYPES_H
  31. #include <sys/types.h>
  32. #endif
  33. #ifdef HAVE_SYS_SOCKET_H
  34. #include <sys/socket.h>
  35. #endif
  36. #ifdef HAVE_NETINET_IN_H
  37. #include <netinet/in.h>
  38. #endif
  39. #ifdef HAVE_NETDB_H
  40. #include <netdb.h>
  41. #endif
  42. #ifdef HAVE_ARPA_INET_H
  43. #include <arpa/inet.h>
  44. #endif
  45. #ifdef HAVE_STDLIB_H
  46. #include <stdlib.h> /* required for free() prototypes */
  47. #endif
  48. #ifdef HAVE_UNISTD_H
  49. #include <unistd.h> /* for the close() proto */
  50. #endif
  51. #ifdef VMS
  52. #include <in.h>
  53. #include <inet.h>
  54. #include <stdlib.h>
  55. #endif
  56. #endif
  57. #ifdef HAVE_SETJMP_H
  58. #include <setjmp.h>
  59. #endif
  60. #ifdef WIN32
  61. #include <process.h>
  62. #endif
  63. #if (defined(NETWARE) && defined(__NOVELL_LIBC__))
  64. #undef in_addr_t
  65. #define in_addr_t unsigned long
  66. #endif
  67. #include "urldata.h"
  68. #include "sendf.h"
  69. #include "hostip.h"
  70. #include "hash.h"
  71. #include "share.h"
  72. #include "strerror.h"
  73. #include "url.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 "curl_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 once stolen from the Dancer source
  153. * code, written by Bjorn Reese, it has since been patched and modified
  154. * considerably.
  155. *
  156. * gethostbyname_r() is the thread-safe version of the gethostbyname()
  157. * function. When we build for plain IPv4, we attempt to use this
  158. * function. There are _three_ different gethostbyname_r() versions, and we
  159. * detect which one this platform supports in the configure script and set up
  160. * the HAVE_GETHOSTBYNAME_R_3, HAVE_GETHOSTBYNAME_R_5 or
  161. * HAVE_GETHOSTBYNAME_R_6 defines accordingly. Note that HAVE_GETADDRBYNAME
  162. * has the corresponding rules. This is primarily on *nix. Note that some unix
  163. * flavours have thread-safe versions of the plain gethostbyname() etc.
  164. *
  165. */
  166. Curl_addrinfo *Curl_getaddrinfo(struct connectdata *conn,
  167. char *hostname,
  168. int port,
  169. int *waitp)
  170. {
  171. Curl_addrinfo *ai = NULL;
  172. struct hostent *h = NULL;
  173. in_addr_t in;
  174. struct SessionHandle *data = conn->data;
  175. struct hostent *buf = NULL;
  176. (void)port; /* unused in IPv4 code */
  177. *waitp = 0; /* don't wait, we act synchronously */
  178. in=inet_addr(hostname);
  179. if (in != CURL_INADDR_NONE) {
  180. /* This is a dotted IP address 123.123.123.123-style */
  181. return Curl_ip2addr(in, hostname, port);
  182. }
  183. #if defined(HAVE_GETHOSTBYNAME_R)
  184. /*
  185. * gethostbyname_r() is the preferred resolve function for many platforms.
  186. * Since there are three different versions of it, the following code is
  187. * somewhat #ifdef-ridden.
  188. */
  189. else {
  190. int h_errnop;
  191. int res=ERANGE;
  192. buf = (struct hostent *)calloc(CURL_HOSTENT_SIZE, 1);
  193. if(!buf)
  194. return NULL; /* major failure */
  195. /*
  196. * The clearing of the buffer is a workaround for a gethostbyname_r bug in
  197. * qnx nto and it is also _required_ for some of these functions on some
  198. * platforms.
  199. */
  200. #ifdef HAVE_GETHOSTBYNAME_R_5
  201. /* Solaris, IRIX and more */
  202. (void)res; /* prevent compiler warning */
  203. h = gethostbyname_r(hostname,
  204. (struct hostent *)buf,
  205. (char *)buf + sizeof(struct hostent),
  206. CURL_HOSTENT_SIZE - sizeof(struct hostent),
  207. &h_errnop);
  208. /* If the buffer is too small, it returns NULL and sets errno to
  209. * ERANGE. The errno is thread safe if this is compiled with
  210. * -D_REENTRANT as then the 'errno' variable is a macro defined to get
  211. * used properly for threads.
  212. */
  213. if(h) {
  214. ;
  215. }
  216. else
  217. #endif /* HAVE_GETHOSTBYNAME_R_5 */
  218. #ifdef HAVE_GETHOSTBYNAME_R_6
  219. /* Linux */
  220. res=gethostbyname_r(hostname,
  221. (struct hostent *)buf,
  222. (char *)buf + sizeof(struct hostent),
  223. CURL_HOSTENT_SIZE - sizeof(struct hostent),
  224. &h, /* DIFFERENCE */
  225. &h_errnop);
  226. /* Redhat 8, using glibc 2.2.93 changed the behavior. Now all of a
  227. * sudden this function returns EAGAIN if the given buffer size is too
  228. * small. Previous versions are known to return ERANGE for the same
  229. * problem.
  230. *
  231. * This wouldn't be such a big problem if older versions wouldn't
  232. * sometimes return EAGAIN on a common failure case. Alas, we can't
  233. * assume that EAGAIN *or* ERANGE means ERANGE for any given version of
  234. * glibc.
  235. *
  236. * For now, we do that and thus we may call the function repeatedly and
  237. * fail for older glibc versions that return EAGAIN, until we run out of
  238. * buffer size (step_size grows beyond CURL_HOSTENT_SIZE).
  239. *
  240. * If anyone has a better fix, please tell us!
  241. *
  242. * -------------------------------------------------------------------
  243. *
  244. * On October 23rd 2003, Dan C dug up more details on the mysteries of
  245. * gethostbyname_r() in glibc:
  246. *
  247. * In glibc 2.2.5 the interface is different (this has also been
  248. * discovered in glibc 2.1.1-6 as shipped by Redhat 6). What I can't
  249. * explain, is that tests performed on glibc 2.2.4-34 and 2.2.4-32
  250. * (shipped/upgraded by Redhat 7.2) don't show this behavior!
  251. *
  252. * In this "buggy" version, the return code is -1 on error and 'errno'
  253. * is set to the ERANGE or EAGAIN code. Note that 'errno' is not a
  254. * thread-safe variable.
  255. */
  256. if(!h) /* failure */
  257. #endif/* HAVE_GETHOSTBYNAME_R_6 */
  258. #ifdef HAVE_GETHOSTBYNAME_R_3
  259. /* AIX, Digital Unix/Tru64, HPUX 10, more? */
  260. /* For AIX 4.3 or later, we don't use gethostbyname_r() at all, because of
  261. * the plain fact that it does not return unique full buffers on each
  262. * call, but instead several of the pointers in the hostent structs will
  263. * point to the same actual data! This have the unfortunate down-side that
  264. * our caching system breaks down horribly. Luckily for us though, AIX 4.3
  265. * and more recent versions have a "completely thread-safe"[*] libc where
  266. * all the data is stored in thread-specific memory areas making calls to
  267. * the plain old gethostbyname() work fine even for multi-threaded
  268. * programs.
  269. *
  270. * This AIX 4.3 or later detection is all made in the configure script.
  271. *
  272. * Troels Walsted Hansen helped us work this out on March 3rd, 2003.
  273. *
  274. * [*] = much later we've found out that it isn't at all "completely
  275. * thread-safe", but at least the gethostbyname() function is.
  276. */
  277. if(CURL_HOSTENT_SIZE >=
  278. (sizeof(struct hostent)+sizeof(struct hostent_data))) {
  279. /* August 22nd, 2000: Albert Chin-A-Young brought an updated version
  280. * that should work! September 20: Richard Prescott worked on the buffer
  281. * size dilemma.
  282. */
  283. res = gethostbyname_r(hostname,
  284. (struct hostent *)buf,
  285. (struct hostent_data *)((char *)buf +
  286. sizeof(struct hostent)));
  287. h_errnop= errno; /* we don't deal with this, but set it anyway */
  288. }
  289. else
  290. res = -1; /* failure, too smallish buffer size */
  291. if(!res) { /* success */
  292. h = buf; /* result expected in h */
  293. /* This is the worst kind of the different gethostbyname_r() interfaces.
  294. * Since we don't know how big buffer this particular lookup required,
  295. * we can't realloc down the huge alloc without doing closer analysis of
  296. * the returned data. Thus, we always use CURL_HOSTENT_SIZE for every
  297. * name lookup. Fixing this would require an extra malloc() and then
  298. * calling Curl_addrinfo_copy() that subsequent realloc()s down the new
  299. * memory area to the actually used amount.
  300. */
  301. }
  302. else
  303. #endif /* HAVE_GETHOSTBYNAME_R_3 */
  304. {
  305. infof(data, "gethostbyname_r(2) failed for %s\n", hostname);
  306. h = NULL; /* set return code to NULL */
  307. free(buf);
  308. }
  309. #else /* HAVE_GETHOSTBYNAME_R */
  310. /*
  311. * Here is code for platforms that don't have gethostbyname_r() or for
  312. * which the gethostbyname() is the preferred() function.
  313. */
  314. else {
  315. h = gethostbyname(hostname);
  316. if (!h)
  317. infof(data, "gethostbyname(2) failed for %s\n", hostname);
  318. #endif /*HAVE_GETHOSTBYNAME_R */
  319. }
  320. if(h) {
  321. ai = Curl_he2ai(h, port);
  322. if (buf) /* used a *_r() function */
  323. free(buf);
  324. }
  325. return ai;
  326. }
  327. #endif /* CURLRES_SYNCH */
  328. /*
  329. * Curl_he2ai() translates from a hostent struct to a Curl_addrinfo struct.
  330. * The Curl_addrinfo is meant to work like the addrinfo struct does for IPv6
  331. * stacks, but for all hosts and environments.
  332. struct Curl_addrinfo {
  333. int ai_flags;
  334. int ai_family;
  335. int ai_socktype;
  336. int ai_protocol;
  337. size_t ai_addrlen;
  338. struct sockaddr *ai_addr;
  339. char *ai_canonname;
  340. struct addrinfo *ai_next;
  341. };
  342. struct hostent {
  343. char *h_name; * official name of host *
  344. char **h_aliases; * alias list *
  345. int h_addrtype; * host address type *
  346. int h_length; * length of address *
  347. char **h_addr_list; * list of addresses *
  348. }
  349. #define h_addr h_addr_list[0] * for backward compatibility *
  350. */
  351. Curl_addrinfo *Curl_he2ai(struct hostent *he, int port)
  352. {
  353. Curl_addrinfo *ai;
  354. Curl_addrinfo *prevai = NULL;
  355. Curl_addrinfo *firstai = NULL;
  356. int i;
  357. union {
  358. struct in_addr *addr;
  359. char* list;
  360. } curr;
  361. union {
  362. struct sockaddr_in* addr_in;
  363. struct sockaddr* addr;
  364. } address;
  365. if(!he)
  366. /* no input == no output! */
  367. return NULL;
  368. for(i=0; (curr.list = he->h_addr_list[i]); i++) {
  369. ai = calloc(1, sizeof(Curl_addrinfo) + sizeof(struct sockaddr_in));
  370. if(!ai)
  371. break;
  372. if(!firstai)
  373. /* store the pointer we want to return from this function */
  374. firstai = ai;
  375. if(prevai)
  376. /* make the previous entry point to this */
  377. prevai->ai_next = ai;
  378. ai->ai_family = AF_INET; /* we only support this */
  379. ai->ai_socktype = SOCK_STREAM; /* we only support this */
  380. ai->ai_addrlen = sizeof(struct sockaddr_in);
  381. /* make the ai_addr point to the address immediately following this struct
  382. and use that area to store the address */
  383. ai->ai_addr = (struct sockaddr *) (ai + 1);
  384. /* leave the rest of the struct filled with zero */
  385. address.addr = ai->ai_addr; /* storage area for this info */
  386. memcpy((char *)&(address.addr_in->sin_addr), curr.addr, sizeof(struct in_addr));
  387. address.addr_in->sin_family = he->h_addrtype;
  388. address.addr_in->sin_port = htons((unsigned short)port);
  389. prevai = ai;
  390. }
  391. return firstai;
  392. }
  393. #endif /* CURLRES_IPV4 */