curl_addrinfo.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2010, 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. ***************************************************************************/
  22. #include "setup.h"
  23. #include <curl/curl.h>
  24. #ifdef HAVE_SYS_SOCKET_H
  25. # include <sys/socket.h>
  26. #endif
  27. #ifdef HAVE_NETINET_IN_H
  28. # include <netinet/in.h>
  29. #endif
  30. #ifdef HAVE_NETDB_H
  31. # include <netdb.h>
  32. #endif
  33. #ifdef HAVE_ARPA_INET_H
  34. # include <arpa/inet.h>
  35. #endif
  36. #ifdef __VMS
  37. # include <in.h>
  38. # include <inet.h>
  39. # include <stdlib.h>
  40. #endif
  41. #if defined(NETWARE) && defined(__NOVELL_LIBC__)
  42. # undef in_addr_t
  43. # define in_addr_t unsigned long
  44. #endif
  45. #include "curl_addrinfo.h"
  46. #include "inet_pton.h"
  47. #define _MPRINTF_REPLACE /* use our functions only */
  48. #include <curl/mprintf.h>
  49. #include "curl_memory.h"
  50. /* The last #include file should be: */
  51. #include "memdebug.h"
  52. /*
  53. * Curl_freeaddrinfo()
  54. *
  55. * This is used to free a linked list of Curl_addrinfo structs along
  56. * with all its associated allocated storage. This function should be
  57. * called once for each successful call to Curl_getaddrinfo_ex() or to
  58. * any function call which actually allocates a Curl_addrinfo struct.
  59. */
  60. #if defined(__INTEL_COMPILER) && (__INTEL_COMPILER == 910) && \
  61. defined(__OPTIMIZE__) && defined(__unix__) && defined(__i386__)
  62. /* workaround icc 9.1 optimizer issue */
  63. # define vqualifier volatile
  64. #else
  65. # define vqualifier
  66. #endif
  67. void
  68. Curl_freeaddrinfo(Curl_addrinfo *cahead)
  69. {
  70. Curl_addrinfo *vqualifier canext;
  71. Curl_addrinfo *ca;
  72. for(ca = cahead; ca != NULL; ca = canext) {
  73. if(ca->ai_addr)
  74. free(ca->ai_addr);
  75. if(ca->ai_canonname)
  76. free(ca->ai_canonname);
  77. canext = ca->ai_next;
  78. free(ca);
  79. }
  80. }
  81. #ifdef HAVE_GETADDRINFO
  82. /*
  83. * Curl_getaddrinfo_ex()
  84. *
  85. * This is a wrapper function around system's getaddrinfo(), with
  86. * the only difference that instead of returning a linked list of
  87. * addrinfo structs this one returns a linked list of Curl_addrinfo
  88. * ones. The memory allocated by this function *MUST* be free'd with
  89. * Curl_freeaddrinfo(). For each successful call to this function
  90. * there must be an associated call later to Curl_freeaddrinfo().
  91. *
  92. * There should be no single call to system's getaddrinfo() in the
  93. * whole library, any such call should be 'routed' through this one.
  94. */
  95. int
  96. Curl_getaddrinfo_ex(const char *nodename,
  97. const char *servname,
  98. const struct addrinfo *hints,
  99. Curl_addrinfo **result)
  100. {
  101. const struct addrinfo *ai;
  102. struct addrinfo *aihead;
  103. Curl_addrinfo *cafirst = NULL;
  104. Curl_addrinfo *calast = NULL;
  105. Curl_addrinfo *ca;
  106. size_t ss_size;
  107. int error;
  108. *result = NULL; /* assume failure */
  109. error = getaddrinfo(nodename, servname, hints, &aihead);
  110. if(error)
  111. return error;
  112. /* traverse the addrinfo list */
  113. for(ai = aihead; ai != NULL; ai = ai->ai_next) {
  114. /* ignore elements with unsupported address family, */
  115. /* settle family-specific sockaddr structure size. */
  116. if(ai->ai_family == AF_INET)
  117. ss_size = sizeof(struct sockaddr_in);
  118. #ifdef ENABLE_IPV6
  119. else if(ai->ai_family == AF_INET6)
  120. ss_size = sizeof(struct sockaddr_in6);
  121. #endif
  122. else
  123. continue;
  124. /* ignore elements without required address info */
  125. if((ai->ai_addr == NULL) || !(ai->ai_addrlen > 0))
  126. continue;
  127. /* ignore elements with bogus address size */
  128. if((size_t)ai->ai_addrlen < ss_size)
  129. continue;
  130. if((ca = malloc(sizeof(Curl_addrinfo))) == NULL) {
  131. error = EAI_MEMORY;
  132. break;
  133. }
  134. /* copy each structure member individually, member ordering, */
  135. /* size, or padding might be different for each platform. */
  136. ca->ai_flags = ai->ai_flags;
  137. ca->ai_family = ai->ai_family;
  138. ca->ai_socktype = ai->ai_socktype;
  139. ca->ai_protocol = ai->ai_protocol;
  140. ca->ai_addrlen = (curl_socklen_t)ss_size;
  141. ca->ai_addr = NULL;
  142. ca->ai_canonname = NULL;
  143. ca->ai_next = NULL;
  144. if((ca->ai_addr = malloc(ss_size)) == NULL) {
  145. error = EAI_MEMORY;
  146. free(ca);
  147. break;
  148. }
  149. memcpy(ca->ai_addr, ai->ai_addr, ss_size);
  150. if(ai->ai_canonname != NULL) {
  151. if((ca->ai_canonname = strdup(ai->ai_canonname)) == NULL) {
  152. error = EAI_MEMORY;
  153. free(ca->ai_addr);
  154. free(ca);
  155. break;
  156. }
  157. }
  158. /* if the return list is empty, this becomes the first element */
  159. if(!cafirst)
  160. cafirst = ca;
  161. /* add this element last in the return list */
  162. if(calast)
  163. calast->ai_next = ca;
  164. calast = ca;
  165. }
  166. /* destroy the addrinfo list */
  167. if(aihead)
  168. freeaddrinfo(aihead);
  169. /* if we failed, also destroy the Curl_addrinfo list */
  170. if(error) {
  171. Curl_freeaddrinfo(cafirst);
  172. cafirst = NULL;
  173. }
  174. else if(!cafirst) {
  175. #ifdef EAI_NONAME
  176. /* rfc3493 conformant */
  177. error = EAI_NONAME;
  178. #else
  179. /* rfc3493 obsoleted */
  180. error = EAI_NODATA;
  181. #endif
  182. #ifdef USE_WINSOCK
  183. SET_SOCKERRNO(error);
  184. #endif
  185. }
  186. *result = cafirst;
  187. /* This is not a CURLcode */
  188. return error;
  189. }
  190. #endif /* HAVE_GETADDRINFO */
  191. /*
  192. * Curl_he2ai()
  193. *
  194. * This function returns a pointer to the first element of a newly allocated
  195. * Curl_addrinfo struct linked list filled with the data of a given hostent.
  196. * Curl_addrinfo is meant to work like the addrinfo struct does for a IPv6
  197. * stack, but usable also for IPv4, all hosts and environments.
  198. *
  199. * The memory allocated by this function *MUST* be free'd later on calling
  200. * Curl_freeaddrinfo(). For each successful call to this function there
  201. * must be an associated call later to Curl_freeaddrinfo().
  202. *
  203. * Curl_addrinfo defined in "lib/curl_addrinfo.h"
  204. *
  205. * struct Curl_addrinfo {
  206. * int ai_flags;
  207. * int ai_family;
  208. * int ai_socktype;
  209. * int ai_protocol;
  210. * curl_socklen_t ai_addrlen; * Follow rfc3493 struct addrinfo *
  211. * char *ai_canonname;
  212. * struct sockaddr *ai_addr;
  213. * struct Curl_addrinfo *ai_next;
  214. * };
  215. * typedef struct Curl_addrinfo Curl_addrinfo;
  216. *
  217. * hostent defined in <netdb.h>
  218. *
  219. * struct hostent {
  220. * char *h_name;
  221. * char **h_aliases;
  222. * int h_addrtype;
  223. * int h_length;
  224. * char **h_addr_list;
  225. * };
  226. *
  227. * for backward compatibility:
  228. *
  229. * #define h_addr h_addr_list[0]
  230. */
  231. Curl_addrinfo *
  232. Curl_he2ai(const struct hostent *he, int port)
  233. {
  234. Curl_addrinfo *ai;
  235. Curl_addrinfo *prevai = NULL;
  236. Curl_addrinfo *firstai = NULL;
  237. struct sockaddr_in *addr;
  238. #ifdef ENABLE_IPV6
  239. struct sockaddr_in6 *addr6;
  240. #endif
  241. CURLcode result = CURLE_OK;
  242. int i;
  243. char *curr;
  244. if(!he)
  245. /* no input == no output! */
  246. return NULL;
  247. DEBUGASSERT((he->h_name != NULL) && (he->h_addr_list != NULL));
  248. for(i=0; (curr = he->h_addr_list[i]) != NULL; i++) {
  249. size_t ss_size;
  250. #ifdef ENABLE_IPV6
  251. if (he->h_addrtype == AF_INET6)
  252. ss_size = sizeof (struct sockaddr_in6);
  253. else
  254. #endif
  255. ss_size = sizeof (struct sockaddr_in);
  256. if((ai = calloc(1, sizeof(Curl_addrinfo))) == NULL) {
  257. result = CURLE_OUT_OF_MEMORY;
  258. break;
  259. }
  260. if((ai->ai_canonname = strdup(he->h_name)) == NULL) {
  261. result = CURLE_OUT_OF_MEMORY;
  262. free(ai);
  263. break;
  264. }
  265. if((ai->ai_addr = calloc(1, ss_size)) == NULL) {
  266. result = CURLE_OUT_OF_MEMORY;
  267. free(ai->ai_canonname);
  268. free(ai);
  269. break;
  270. }
  271. if(!firstai)
  272. /* store the pointer we want to return from this function */
  273. firstai = ai;
  274. if(prevai)
  275. /* make the previous entry point to this */
  276. prevai->ai_next = ai;
  277. ai->ai_family = he->h_addrtype;
  278. /* we return all names as STREAM, so when using this address for TFTP
  279. the type must be ignored and conn->socktype be used instead! */
  280. ai->ai_socktype = SOCK_STREAM;
  281. ai->ai_addrlen = (curl_socklen_t)ss_size;
  282. /* leave the rest of the struct filled with zero */
  283. switch (ai->ai_family) {
  284. case AF_INET:
  285. addr = (void *)ai->ai_addr; /* storage area for this info */
  286. memcpy(&addr->sin_addr, curr, sizeof(struct in_addr));
  287. addr->sin_family = (unsigned short)(he->h_addrtype);
  288. addr->sin_port = htons((unsigned short)port);
  289. break;
  290. #ifdef ENABLE_IPV6
  291. case AF_INET6:
  292. addr6 = (void *)ai->ai_addr; /* storage area for this info */
  293. memcpy(&addr6->sin6_addr, curr, sizeof(struct in6_addr));
  294. addr6->sin6_family = (unsigned short)(he->h_addrtype);
  295. addr6->sin6_port = htons((unsigned short)port);
  296. break;
  297. #endif
  298. }
  299. prevai = ai;
  300. }
  301. if(result != CURLE_OK) {
  302. Curl_freeaddrinfo(firstai);
  303. firstai = NULL;
  304. }
  305. return firstai;
  306. }
  307. struct namebuff {
  308. struct hostent hostentry;
  309. union {
  310. struct in_addr ina4;
  311. #ifdef ENABLE_IPV6
  312. struct in6_addr ina6;
  313. #endif
  314. } addrentry;
  315. char *h_addr_list[2];
  316. };
  317. /*
  318. * Curl_ip2addr()
  319. *
  320. * This function takes an internet address, in binary form, as input parameter
  321. * along with its address family and the string version of the address, and it
  322. * returns a Curl_addrinfo chain filled in correctly with information for the
  323. * given address/host
  324. */
  325. Curl_addrinfo *
  326. Curl_ip2addr(int af, const void *inaddr, const char *hostname, int port)
  327. {
  328. Curl_addrinfo *ai;
  329. #if defined(__VMS) && \
  330. defined(__INITIAL_POINTER_SIZE) && (__INITIAL_POINTER_SIZE == 64)
  331. #pragma pointer_size save
  332. #pragma pointer_size short
  333. #pragma message disable PTRMISMATCH
  334. #endif
  335. struct hostent *h;
  336. struct namebuff *buf;
  337. char *addrentry;
  338. char *hoststr;
  339. size_t addrsize;
  340. DEBUGASSERT(inaddr && hostname);
  341. buf = malloc(sizeof(struct namebuff));
  342. if(!buf)
  343. return NULL;
  344. hoststr = strdup(hostname);
  345. if(!hoststr) {
  346. free(buf);
  347. return NULL;
  348. }
  349. switch(af) {
  350. case AF_INET:
  351. addrsize = sizeof(struct in_addr);
  352. addrentry = (void *)&buf->addrentry.ina4;
  353. memcpy(addrentry, inaddr, sizeof(struct in_addr));
  354. break;
  355. #ifdef ENABLE_IPV6
  356. case AF_INET6:
  357. addrsize = sizeof(struct in6_addr);
  358. addrentry = (void *)&buf->addrentry.ina6;
  359. memcpy(addrentry, inaddr, sizeof(struct in6_addr));
  360. break;
  361. #endif
  362. default:
  363. free(hoststr);
  364. free(buf);
  365. return NULL;
  366. }
  367. h = &buf->hostentry;
  368. h->h_name = hoststr;
  369. h->h_aliases = NULL;
  370. h->h_addrtype = (short)af;
  371. h->h_length = (short)addrsize;
  372. h->h_addr_list = &buf->h_addr_list[0];
  373. h->h_addr_list[0] = addrentry;
  374. h->h_addr_list[1] = NULL; /* terminate list of entries */
  375. #if defined(__VMS) && \
  376. defined(__INITIAL_POINTER_SIZE) && (__INITIAL_POINTER_SIZE == 64)
  377. #pragma pointer_size restore
  378. #pragma message enable PTRMISMATCH
  379. #endif
  380. ai = Curl_he2ai(h, port);
  381. free(hoststr);
  382. free(buf);
  383. return ai;
  384. }
  385. /*
  386. * Given an IPv4 or IPv6 dotted string address, this converts it to a proper
  387. * allocated Curl_addrinfo struct and returns it.
  388. */
  389. Curl_addrinfo *Curl_str2addr(char *address, int port)
  390. {
  391. struct in_addr in;
  392. if(Curl_inet_pton(AF_INET, address, &in) > 0)
  393. /* This is a dotted IP address 123.123.123.123-style */
  394. return Curl_ip2addr(AF_INET, &in, address, port);
  395. #ifdef ENABLE_IPV6
  396. else {
  397. struct in6_addr in6;
  398. if(Curl_inet_pton(AF_INET6, address, &in6) > 0)
  399. /* This is a dotted IPv6 address ::1-style */
  400. return Curl_ip2addr(AF_INET6, &in6, address, port);
  401. }
  402. #endif
  403. return NULL; /* bad input format */
  404. }
  405. #if defined(CURLDEBUG) && defined(HAVE_FREEADDRINFO)
  406. /*
  407. * curl_dofreeaddrinfo()
  408. *
  409. * This is strictly for memory tracing and are using the same style as the
  410. * family otherwise present in memdebug.c. I put these ones here since they
  411. * require a bunch of structs I didn't wanna include in memdebug.c
  412. */
  413. void
  414. curl_dofreeaddrinfo(struct addrinfo *freethis,
  415. int line, const char *source)
  416. {
  417. (freeaddrinfo)(freethis);
  418. curl_memlog("ADDR %s:%d freeaddrinfo(%p)\n",
  419. source, line, (void *)freethis);
  420. }
  421. #endif /* defined(CURLDEBUG) && defined(HAVE_FREEADDRINFO) */
  422. #if defined(CURLDEBUG) && defined(HAVE_GETADDRINFO)
  423. /*
  424. * curl_dogetaddrinfo()
  425. *
  426. * This is strictly for memory tracing and are using the same style as the
  427. * family otherwise present in memdebug.c. I put these ones here since they
  428. * require a bunch of structs I didn't wanna include in memdebug.c
  429. */
  430. int
  431. curl_dogetaddrinfo(const char *hostname,
  432. const char *service,
  433. const struct addrinfo *hints,
  434. struct addrinfo **result,
  435. int line, const char *source)
  436. {
  437. int res=(getaddrinfo)(hostname, service, hints, result);
  438. if(0 == res)
  439. /* success */
  440. curl_memlog("ADDR %s:%d getaddrinfo() = %p\n",
  441. source, line, (void *)*result);
  442. else
  443. curl_memlog("ADDR %s:%d getaddrinfo() failed\n",
  444. source, line);
  445. return res;
  446. }
  447. #endif /* defined(CURLDEBUG) && defined(HAVE_GETADDRINFO) */