curl_addrinfo.c 13 KB

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