hostip.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2002, 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 <winsock.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 VMS
  49. #include <in.h>
  50. #include <inet.h>
  51. #include <stdlib.h>
  52. #endif
  53. #endif
  54. #ifdef HAVE_SETJMP_H
  55. #include <setjmp.h>
  56. #endif
  57. #include "urldata.h"
  58. #include "sendf.h"
  59. #include "hostip.h"
  60. #include "hash.h"
  61. #define _MPRINTF_REPLACE /* use our functions only */
  62. #include <curl/mprintf.h>
  63. #if defined(HAVE_INET_NTOA_R) && !defined(HAVE_INET_NTOA_R_DECL)
  64. #include "inet_ntoa_r.h"
  65. #endif
  66. /* The last #include file should be: */
  67. #ifdef MALLOCDEBUG
  68. #include "memdebug.h"
  69. #endif
  70. static curl_hash hostname_cache;
  71. static int host_cache_initialized;
  72. static Curl_addrinfo *my_getaddrinfo(struct SessionHandle *data,
  73. char *hostname,
  74. int port,
  75. char **bufp);
  76. void Curl_global_host_cache_init(void)
  77. {
  78. if (!host_cache_initialized) {
  79. Curl_hash_init(&hostname_cache, 7, Curl_freednsinfo);
  80. host_cache_initialized = 1;
  81. }
  82. }
  83. curl_hash *Curl_global_host_cache_get(void)
  84. {
  85. return &hostname_cache;
  86. }
  87. void Curl_global_host_cache_dtor(void)
  88. {
  89. if (host_cache_initialized) {
  90. Curl_hash_clean(&hostname_cache);
  91. host_cache_initialized = 0;
  92. }
  93. }
  94. /* count the number of characters that an integer takes up */
  95. static int _num_chars(int i)
  96. {
  97. int chars = 0;
  98. /* While the number divided by 10 is greater than one,
  99. * re-divide the number by 10, and increment the number of
  100. * characters by 1.
  101. *
  102. * this relies on the fact that for every multiple of 10,
  103. * a new digit is added onto every number
  104. */
  105. do {
  106. chars++;
  107. i = (int) i / 10;
  108. } while (i >= 1);
  109. return chars;
  110. }
  111. /* Create a hostcache id */
  112. static char *
  113. create_hostcache_id(char *server, int port, ssize_t *entry_len)
  114. {
  115. char *id = NULL;
  116. /* Get the length of the new entry id */
  117. *entry_len = *entry_len + /* Hostname length */
  118. 1 + /* The ':' seperator */
  119. _num_chars(port); /* The number of characters the port will take up */
  120. /* Allocate the new entry id */
  121. id = malloc(*entry_len + 1);
  122. if (!id) {
  123. return NULL;
  124. }
  125. /* Create the new entry */
  126. /* If sprintf() doesn't return the entry length, that signals failure */
  127. if (sprintf(id, "%s:%d", server, port) != *entry_len) {
  128. /* Free the allocated id, set length to zero and return NULL */
  129. *entry_len = 0;
  130. free(id);
  131. return NULL;
  132. }
  133. return id;
  134. }
  135. struct hostcache_prune_data {
  136. int cache_timeout;
  137. int now;
  138. };
  139. static int
  140. hostcache_timestamp_remove(void *datap, void *hc)
  141. {
  142. struct hostcache_prune_data *data =
  143. (struct hostcache_prune_data *) datap;
  144. struct Curl_dns_entry *c = (struct Curl_dns_entry *) hc;
  145. if ((data->now - c->timestamp < data->cache_timeout) ||
  146. c->inuse) {
  147. /* please don't remove */
  148. return 0;
  149. }
  150. /* fine, remove */
  151. return 1;
  152. }
  153. static void
  154. hostcache_prune(curl_hash *hostcache, int cache_timeout, int now)
  155. {
  156. struct hostcache_prune_data user;
  157. user.cache_timeout = cache_timeout;
  158. user.now = now;
  159. Curl_hash_clean_with_criterium(hostcache,
  160. (void *) &user,
  161. hostcache_timestamp_remove);
  162. }
  163. #if defined(MALLOCDEBUG) && defined(AGGRESIVE_TEST)
  164. /* Called from Curl_done() to check that there's no DNS cache entry with
  165. a non-zero counter left. */
  166. void Curl_scan_cache_used(void *user, void *ptr)
  167. {
  168. struct Curl_dns_entry *e = ptr;
  169. (void)user; /* prevent compiler warning */
  170. if(e->inuse) {
  171. fprintf(stderr, "*** WARNING: locked DNS cache entry detected: %s\n",
  172. e->entry_id);
  173. /* perform a segmentation fault to draw attention */
  174. *(void **)0 = 0;
  175. }
  176. }
  177. #endif
  178. /* Macro to save redundant free'ing of entry_id */
  179. #define HOSTCACHE_RETURN(dns) \
  180. { \
  181. free(entry_id); \
  182. return dns; \
  183. }
  184. #ifdef HAVE_SIGSETJMP
  185. /* Beware this is a global and unique instance */
  186. sigjmp_buf curl_jmpenv;
  187. #endif
  188. struct Curl_dns_entry *Curl_resolv(struct SessionHandle *data,
  189. char *hostname,
  190. int port)
  191. {
  192. char *entry_id = NULL;
  193. struct Curl_dns_entry *dns = NULL;
  194. ssize_t entry_len;
  195. time_t now;
  196. char *bufp;
  197. #ifdef HAVE_SIGSETJMP
  198. /* this allows us to time-out from the name resolver, as the timeout
  199. will generate a signal and we will siglongjmp() from that here */
  200. if(!data->set.no_signal && sigsetjmp(curl_jmpenv, 1)) {
  201. /* this is coming from a siglongjmp() */
  202. failf(data, "name lookup time-outed");
  203. return NULL;
  204. }
  205. #endif
  206. /* Create an entry id, based upon the hostname and port */
  207. entry_len = (int)strlen(hostname);
  208. entry_id = create_hostcache_id(hostname, port, &entry_len);
  209. /* If we can't create the entry id, fail */
  210. if (!entry_id)
  211. return NULL;
  212. /* See if its already in our dns cache */
  213. dns = Curl_hash_pick(data->hostcache, entry_id, entry_len+1);
  214. if (!dns) {
  215. Curl_addrinfo *addr = my_getaddrinfo(data, hostname, port, &bufp);
  216. if (!addr) {
  217. HOSTCACHE_RETURN(NULL);
  218. }
  219. /* Create a new cache entry */
  220. dns = (struct Curl_dns_entry *) malloc(sizeof(struct Curl_dns_entry));
  221. if (!dns) {
  222. Curl_freeaddrinfo(addr);
  223. HOSTCACHE_RETURN(NULL);
  224. }
  225. dns->inuse = 0;
  226. dns->addr = addr;
  227. /* Save it in our host cache */
  228. Curl_hash_add(data->hostcache, entry_id, entry_len+1, (const void *) dns);
  229. }
  230. time(&now);
  231. dns->timestamp = now;
  232. dns->inuse++; /* mark entry as in-use */
  233. #ifdef MALLOCDEBUG
  234. dns->entry_id = entry_id;
  235. #endif
  236. /* Remove outdated and unused entries from the hostcache */
  237. hostcache_prune(data->hostcache,
  238. data->set.dns_cache_timeout,
  239. (int)now);
  240. HOSTCACHE_RETURN(dns);
  241. }
  242. /*
  243. * This is a wrapper function for freeing name information in a protocol
  244. * independent way. This takes care of using the appropriate underlaying
  245. * function.
  246. */
  247. void Curl_freeaddrinfo(Curl_addrinfo *p)
  248. {
  249. #ifdef ENABLE_IPV6
  250. freeaddrinfo(p);
  251. #else
  252. free(p);
  253. #endif
  254. }
  255. /*
  256. * Free a cache dns entry.
  257. */
  258. void Curl_freednsinfo(void *freethis)
  259. {
  260. struct Curl_dns_entry *p = (struct Curl_dns_entry *) freethis;
  261. Curl_freeaddrinfo(p->addr);
  262. free(p);
  263. }
  264. /* --- resolve name or IP-number --- */
  265. #ifdef ENABLE_IPV6
  266. #ifdef MALLOCDEBUG
  267. /* These two are strictly for memory tracing and are using the same
  268. * style as the family otherwise present in memdebug.c. I put these ones
  269. * here since they require a bunch of struct types I didn't wanna include
  270. * in memdebug.c
  271. */
  272. int curl_getaddrinfo(char *hostname, char *service,
  273. struct addrinfo *hints,
  274. struct addrinfo **result,
  275. int line, const char *source)
  276. {
  277. int res=(getaddrinfo)(hostname, service, hints, result);
  278. if(0 == res) {
  279. /* success */
  280. if(logfile)
  281. fprintf(logfile, "ADDR %s:%d getaddrinfo() = %p\n",
  282. source, line, (void *)*result);
  283. }
  284. else {
  285. if(logfile)
  286. fprintf(logfile, "ADDR %s:%d getaddrinfo() failed\n",
  287. source, line);
  288. }
  289. return res;
  290. }
  291. void curl_freeaddrinfo(struct addrinfo *freethis,
  292. int line, const char *source)
  293. {
  294. (freeaddrinfo)(freethis);
  295. if(logfile)
  296. fprintf(logfile, "ADDR %s:%d freeaddrinfo(%p)\n",
  297. source, line, (void *)freethis);
  298. }
  299. #endif
  300. /*
  301. * Return name information about the given hostname and port number. If
  302. * successful, the 'addrinfo' is returned and the forth argument will point to
  303. * memory we need to free after use. That meory *MUST* be freed with
  304. * Curl_freeaddrinfo(), nothing else.
  305. */
  306. static Curl_addrinfo *my_getaddrinfo(struct SessionHandle *data,
  307. char *hostname,
  308. int port,
  309. char **bufp)
  310. {
  311. struct addrinfo hints, *res;
  312. int error;
  313. char sbuf[NI_MAXSERV];
  314. int s, pf = PF_UNSPEC;
  315. /* see if we have an IPv6 stack */
  316. s = socket(PF_INET6, SOCK_DGRAM, 0);
  317. if (s < 0)
  318. /* Some non-IPv6 stacks have been found to make very slow name resolves
  319. * when PF_UNSPEC is used, so thus we switch to a mere PF_INET lookup if
  320. * the stack seems to be a non-ipv6 one. */
  321. pf = PF_INET;
  322. else
  323. /* This seems to be an IPv6-capable stack, use PF_UNSPEC for the widest
  324. * possible checks. And close the socket again.
  325. */
  326. sclose(s);
  327. memset(&hints, 0, sizeof(hints));
  328. hints.ai_family = pf;
  329. hints.ai_socktype = SOCK_STREAM;
  330. hints.ai_flags = AI_CANONNAME;
  331. snprintf(sbuf, sizeof(sbuf), "%d", port);
  332. error = getaddrinfo(hostname, sbuf, &hints, &res);
  333. if (error) {
  334. infof(data, "getaddrinfo(3) failed for %s:%d\n", hostname, port);
  335. return NULL;
  336. }
  337. *bufp=(char *)res; /* make it point to the result struct */
  338. return res;
  339. }
  340. #else /* following code is IPv4-only */
  341. #ifndef HAVE_GETHOSTBYNAME_R
  342. static void hostcache_fixoffset(struct hostent *h, int offset);
  343. /**
  344. * Performs a "deep" copy of a hostent into a buffer (returns a pointer to the
  345. * copy). Make absolutely sure the destination buffer is big enough!
  346. *
  347. * Keith McGuigan
  348. * 10/3/2001 */
  349. static struct hostent* pack_hostent(char** buf, struct hostent* orig)
  350. {
  351. char *bufptr;
  352. struct hostent *newbuf;
  353. struct hostent* copy;
  354. int i;
  355. char *str;
  356. size_t len;
  357. bufptr = *buf;
  358. memcpy(&copy, &bufptr, sizeof(struct hostent*));
  359. bufptr += sizeof(struct hostent);
  360. copy->h_name = bufptr;
  361. len = strlen(orig->h_name) + 1;
  362. strncpy(bufptr, orig->h_name, len);
  363. bufptr += len;
  364. /* we align on even 64bit boundaries for safety */
  365. #define MEMALIGN(x) ((x)+(8-(((unsigned long)(x))&0x7)))
  366. /* This must be aligned properly to work on many CPU architectures! */
  367. bufptr = MEMALIGN(bufptr);
  368. memcpy(&copy->h_aliases, &bufptr, sizeof(char**));
  369. /* Figure out how many aliases there are */
  370. for (i = 0; orig->h_aliases[i] != NULL; ++i);
  371. /* Reserve room for the array */
  372. bufptr += (i + 1) * sizeof(char*);
  373. /* Clone all known aliases */
  374. for(i = 0; (str = orig->h_aliases[i]); i++) {
  375. len = strlen(str) + 1;
  376. strncpy(bufptr, str, len);
  377. copy->h_aliases[i] = bufptr;
  378. bufptr += len;
  379. }
  380. /* Terminate the alias list with a NULL */
  381. copy->h_aliases[i] = NULL;
  382. copy->h_addrtype = orig->h_addrtype;
  383. copy->h_length = orig->h_length;
  384. /* align it for (at least) 32bit accesses */
  385. bufptr = MEMALIGN(bufptr);
  386. memcpy(&copy->h_addr_list, &bufptr, sizeof(char**));
  387. /* Figure out how many addresses there are */
  388. for (i = 0; orig->h_addr_list[i] != NULL; ++i);
  389. /* Reserve room for the array */
  390. bufptr += (i + 1) * sizeof(char*);
  391. i = 0;
  392. len = orig->h_length;
  393. str = orig->h_addr_list[i];
  394. while (str != NULL) {
  395. memcpy(bufptr, str, len);
  396. copy->h_addr_list[i] = bufptr;
  397. bufptr += len;
  398. str = orig->h_addr_list[++i];
  399. }
  400. copy->h_addr_list[i] = NULL;
  401. /* now, shrink the allocated buffer to the size we actually need, which
  402. most often is only a fraction of the original alloc */
  403. newbuf=(struct hostent *)realloc(*buf, (int)(bufptr-*buf));
  404. /* if the alloc moved, we need to adjust things again */
  405. if((char*)newbuf != *buf)
  406. hostcache_fixoffset((struct hostent*)newbuf, (int)((char*)newbuf-*buf));
  407. /* setup the return */
  408. *buf = (char*)newbuf;
  409. copy = (struct hostent*)newbuf;
  410. return copy;
  411. }
  412. #endif
  413. static char *MakeIP(unsigned long num,char *addr, int addr_len)
  414. {
  415. #if defined(HAVE_INET_NTOA) || defined(HAVE_INET_NTOA_R)
  416. struct in_addr in;
  417. in.s_addr = htonl(num);
  418. #if defined(HAVE_INET_NTOA_R)
  419. inet_ntoa_r(in,addr,addr_len);
  420. #else
  421. strncpy(addr,inet_ntoa(in),addr_len);
  422. #endif
  423. #else
  424. unsigned char *paddr;
  425. num = htonl(num); /* htonl() added to avoid endian probs */
  426. paddr = (unsigned char *)&num;
  427. sprintf(addr, "%u.%u.%u.%u", paddr[0], paddr[1], paddr[2], paddr[3]);
  428. #endif
  429. return (addr);
  430. }
  431. #ifndef INADDR_NONE
  432. #define INADDR_NONE (in_addr_t) ~0
  433. #endif
  434. static void hostcache_fixoffset(struct hostent *h, int offset)
  435. {
  436. int i=0;
  437. h->h_name=(char *)((long)h->h_name+offset);
  438. h->h_aliases=(char **)((long)h->h_aliases+offset);
  439. while(h->h_aliases[i]) {
  440. h->h_aliases[i]=(char *)((long)h->h_aliases[i]+offset);
  441. i++;
  442. }
  443. h->h_addr_list=(char **)((long)h->h_addr_list+offset);
  444. i=0;
  445. while(h->h_addr_list[i]) {
  446. h->h_addr_list[i]=(char *)((long)h->h_addr_list[i]+offset);
  447. i++;
  448. }
  449. }
  450. /* The original code to this function was once stolen from the Dancer source
  451. code, written by Bjorn Reese, it has since been patched and modified
  452. considerably. */
  453. static Curl_addrinfo *my_getaddrinfo(struct SessionHandle *data,
  454. char *hostname,
  455. int port,
  456. char **bufp)
  457. {
  458. struct hostent *h = NULL;
  459. in_addr_t in;
  460. int ret; /* this variable is unused on several platforms but used on some */
  461. #define CURL_NAMELOOKUP_SIZE 9000
  462. /* Allocate enough memory to hold the full name information structs and
  463. * everything. OSF1 is known to require at least 8872 bytes. The buffer
  464. * required for storing all possible aliases and IP numbers is according to
  465. * Stevens' Unix Network Programming 2nd editor, p. 304: 8192 bytes! */
  466. port=0; /* unused in IPv4 code */
  467. ret = 0; /* to prevent the compiler warning */
  468. (void)ret;
  469. if ( (in=inet_addr(hostname)) != INADDR_NONE ) {
  470. struct in_addr *addrentry;
  471. struct namebuf {
  472. struct hostent hostentry;
  473. char *h_addr_list[2];
  474. struct in_addr addrentry;
  475. char h_name[128];
  476. } *buf = (struct namebuf *)malloc(sizeof(struct namebuf));
  477. if(!buf)
  478. return NULL; /* major failure */
  479. *bufp = (char *)buf;
  480. h = &buf->hostentry;
  481. h->h_addr_list = &buf->h_addr_list[0];
  482. addrentry = &buf->addrentry;
  483. addrentry->s_addr = in;
  484. h->h_addr_list[0] = (char*)addrentry;
  485. h->h_addr_list[1] = NULL;
  486. h->h_addrtype = AF_INET;
  487. h->h_length = sizeof(*addrentry);
  488. h->h_name = &buf->h_name[0];
  489. MakeIP(ntohl(in), (char*)h->h_name, sizeof(buf->h_name));
  490. }
  491. #if defined(HAVE_GETHOSTBYNAME_R)
  492. else {
  493. int h_errnop;
  494. int res=ERANGE;
  495. int step_size=200;
  496. int *buf = (int *)malloc(CURL_NAMELOOKUP_SIZE);
  497. if(!buf)
  498. return NULL; /* major failure */
  499. *bufp=(char *)buf;
  500. /* Workaround for gethostbyname_r bug in qnx nto. It is also _required_
  501. for some of these functions. */
  502. memset(buf, 0, CURL_NAMELOOKUP_SIZE);
  503. #ifdef HAVE_GETHOSTBYNAME_R_5
  504. /* Solaris, IRIX and more */
  505. (void)res; /* prevent compiler warning */
  506. while(!h) {
  507. h = gethostbyname_r(hostname,
  508. (struct hostent *)buf,
  509. (char *)buf + sizeof(struct hostent),
  510. step_size - sizeof(struct hostent),
  511. &h_errnop);
  512. /* If the buffer is too small, it returns NULL and sets errno to
  513. ERANGE. The errno is thread safe if this is compiled with
  514. -D_REENTRANT as then the 'errno' variable is a macro defined to
  515. get used properly for threads. */
  516. if(h || (errno != ERANGE))
  517. break;
  518. step_size+=200;
  519. }
  520. #ifdef MALLOCDEBUG
  521. infof(data, "gethostbyname_r() uses %d bytes\n", step_size);
  522. #endif
  523. if(h) {
  524. int offset;
  525. h=(struct hostent *)realloc(buf, step_size);
  526. offset=(long)h-(long)buf;
  527. hostcache_fixoffset(h, offset);
  528. buf=(int *)h;
  529. *bufp=(char *)buf;
  530. }
  531. else
  532. #endif
  533. #ifdef HAVE_GETHOSTBYNAME_R_6
  534. /* Linux */
  535. do {
  536. res=gethostbyname_r(hostname,
  537. (struct hostent *)buf,
  538. (char *)buf + sizeof(struct hostent),
  539. step_size - sizeof(struct hostent),
  540. &h, /* DIFFERENCE */
  541. &h_errnop);
  542. /* Redhat 8, using glibc 2.2.93 changed the behavior. Now all of a
  543. sudden this function returns EAGAIN if the given buffer size is too
  544. small. Previous versions are known to return ERANGE for the same
  545. problem.
  546. This wouldn't be such a big problem if older versions wouldn't
  547. sometimes return EAGAIN on a common failure case. Alas, we can't
  548. assume that EAGAIN *or* ERANGE means ERANGE for any given version of
  549. glibc.
  550. For now, we do that and thus we may call the function repeatedly and
  551. fail for older glibc versions that return EAGAIN, until we run out
  552. of buffer size (step_size grows beyond CURL_NAMELOOKUP_SIZE).
  553. If anyone has a better fix, please tell us!
  554. */
  555. if((ERANGE == res) || (EAGAIN == res)) {
  556. step_size+=200;
  557. continue;
  558. }
  559. break;
  560. } while(step_size <= CURL_NAMELOOKUP_SIZE);
  561. if(!h) /* failure */
  562. res=1;
  563. #ifdef MALLOCDEBUG
  564. infof(data, "gethostbyname_r() uses %d bytes\n", step_size);
  565. #endif
  566. if(!res) {
  567. int offset;
  568. h=(struct hostent *)realloc(buf, step_size);
  569. offset=(long)h-(long)buf;
  570. hostcache_fixoffset(h, offset);
  571. buf=(int *)h;
  572. *bufp=(char *)buf;
  573. }
  574. else
  575. #endif
  576. #ifdef HAVE_GETHOSTBYNAME_R_3
  577. /* AIX, Digital Unix, HPUX 10, more? */
  578. if(CURL_NAMELOOKUP_SIZE >=
  579. (sizeof(struct hostent)+sizeof(struct hostent_data)))
  580. /* August 22nd, 2000: Albert Chin-A-Young brought an updated version
  581. * that should work! September 20: Richard Prescott worked on the buffer
  582. * size dilemma. */
  583. ret = gethostbyname_r(hostname,
  584. (struct hostent *)buf,
  585. (struct hostent_data *)((char *)buf + sizeof(struct hostent)));
  586. else
  587. ret = -1; /* failure, too smallish buffer size */
  588. /* result expected in h */
  589. h = (struct hostent*)buf;
  590. h_errnop= errno; /* we don't deal with this, but set it anyway */
  591. if(ret)
  592. #endif
  593. {
  594. infof(data, "gethostbyname_r(2) failed for %s\n", hostname);
  595. h = NULL; /* set return code to NULL */
  596. free(buf);
  597. *bufp=NULL;
  598. }
  599. #else
  600. else {
  601. if ((h = gethostbyname(hostname)) == NULL ) {
  602. infof(data, "gethostbyname(2) failed for %s\n", hostname);
  603. *bufp=NULL;
  604. }
  605. else
  606. {
  607. char *buf=(char *)malloc(CURL_NAMELOOKUP_SIZE);
  608. /* we make a copy of the hostent right now, right here, as the
  609. static one we got a pointer to might get removed when we don't
  610. want/expect that */
  611. h = pack_hostent(&buf, h);
  612. *bufp=(char *)buf;
  613. }
  614. #endif
  615. }
  616. return (h);
  617. }
  618. #endif /* end of IPv4-specific code */
  619. /*
  620. * local variables:
  621. * eval: (load-file "../curl-mode.el")
  622. * end:
  623. * vim600: fdm=marker
  624. * vim: et sw=2 ts=2 sts=2 tw=78
  625. */