curl_addrinfo.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2009, 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 <curl/curl.h>
  25. #ifdef HAVE_SYS_SOCKET_H
  26. # include <sys/socket.h>
  27. #endif
  28. #ifdef HAVE_NETINET_IN_H
  29. # include <netinet/in.h>
  30. #endif
  31. #ifdef HAVE_NETDB_H
  32. # include <netdb.h>
  33. #endif
  34. #ifdef HAVE_ARPA_INET_H
  35. # include <arpa/inet.h>
  36. #endif
  37. #ifdef VMS
  38. # include <in.h>
  39. # include <inet.h>
  40. # include <stdlib.h>
  41. #endif
  42. #if defined(NETWARE) && defined(__NOVELL_LIBC__)
  43. # undef in_addr_t
  44. # define in_addr_t unsigned long
  45. #endif
  46. #include "curl_addrinfo.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 *ainext;
  102. const struct addrinfo *ai;
  103. struct addrinfo *aihead;
  104. Curl_addrinfo *cafirst = NULL;
  105. Curl_addrinfo *calast = NULL;
  106. Curl_addrinfo *ca;
  107. int error;
  108. *result = NULL; /* assume failure */
  109. error = getaddrinfo(nodename, servname, hints, &aihead);
  110. if(error)
  111. return error;
  112. for(ai = aihead; ai != NULL; ai = ainext) {
  113. if((ca = malloc(sizeof(Curl_addrinfo))) == NULL) {
  114. error = EAI_MEMORY;
  115. break;
  116. }
  117. /* copy each structure member individually, member ordering, */
  118. /* size, or padding might be different for each structure. */
  119. ca->ai_flags = ai->ai_flags;
  120. ca->ai_family = ai->ai_family;
  121. ca->ai_socktype = ai->ai_socktype;
  122. ca->ai_protocol = ai->ai_protocol;
  123. ca->ai_addrlen = 0;
  124. ca->ai_addr = NULL;
  125. ca->ai_canonname = NULL;
  126. ca->ai_next = NULL;
  127. if((ai->ai_addrlen > 0) && (ai->ai_addr != NULL)) {
  128. ca->ai_addrlen = ai->ai_addrlen;
  129. if((ca->ai_addr = malloc(ca->ai_addrlen)) == NULL) {
  130. error = EAI_MEMORY;
  131. free(ca);
  132. break;
  133. }
  134. memcpy(ca->ai_addr, ai->ai_addr, ca->ai_addrlen);
  135. }
  136. if(ai->ai_canonname != NULL) {
  137. if((ca->ai_canonname = strdup(ai->ai_canonname)) == NULL) {
  138. error = EAI_MEMORY;
  139. if(ca->ai_addr)
  140. free(ca->ai_addr);
  141. free(ca);
  142. break;
  143. }
  144. }
  145. /* if the return list is empty, this becomes the first element */
  146. if(!cafirst)
  147. cafirst = ca;
  148. /* add this element last in the return list */
  149. if(calast)
  150. calast->ai_next = ca;
  151. calast = ca;
  152. /* fetch next element fom the addrinfo list */
  153. ainext = ai->ai_next;
  154. }
  155. /* destroy the addrinfo list */
  156. if(aihead)
  157. freeaddrinfo(aihead);
  158. /* if we failed, also destroy the Curl_addrinfo list */
  159. if(error) {
  160. Curl_freeaddrinfo(cafirst);
  161. cafirst = NULL;
  162. }
  163. *result = cafirst;
  164. /* This is not a CURLcode */
  165. return error;
  166. }
  167. #endif /* HAVE_GETADDRINFO */
  168. /*
  169. * Curl_he2ai()
  170. *
  171. * This function returns a pointer to the first element of a newly allocated
  172. * Curl_addrinfo struct linked list filled with the data of a given hostent.
  173. * Curl_addrinfo is meant to work like the addrinfo struct does for a IPv6
  174. * stack, but usable also for IPv4, all hosts and environments.
  175. *
  176. * The memory allocated by this function *MUST* be free'd later on calling
  177. * Curl_freeaddrinfo(). For each successful call to this function there
  178. * must be an associated call later to Curl_freeaddrinfo().
  179. *
  180. * Curl_addrinfo defined in "lib/curl_addrinfo.h"
  181. *
  182. * struct Curl_addrinfo {
  183. * int ai_flags;
  184. * int ai_family;
  185. * int ai_socktype;
  186. * int ai_protocol;
  187. * curl_socklen_t ai_addrlen; * Follow rfc3493 struct addrinfo *
  188. * char *ai_canonname;
  189. * struct sockaddr *ai_addr;
  190. * struct Curl_addrinfo *ai_next;
  191. * };
  192. * typedef struct Curl_addrinfo Curl_addrinfo;
  193. *
  194. * hostent defined in <netdb.h>
  195. *
  196. * struct hostent {
  197. * char *h_name;
  198. * char **h_aliases;
  199. * int h_addrtype;
  200. * int h_length;
  201. * char **h_addr_list;
  202. * };
  203. *
  204. * for backward compatibility:
  205. *
  206. * #define h_addr h_addr_list[0]
  207. */
  208. Curl_addrinfo *
  209. Curl_he2ai(const struct hostent *he, int port)
  210. {
  211. Curl_addrinfo *ai;
  212. Curl_addrinfo *prevai = NULL;
  213. Curl_addrinfo *firstai = NULL;
  214. struct sockaddr_in *addr;
  215. #ifdef ENABLE_IPV6
  216. struct sockaddr_in6 *addr6;
  217. #endif
  218. CURLcode result = CURLE_OK;
  219. int i;
  220. char *curr;
  221. if(!he)
  222. /* no input == no output! */
  223. return NULL;
  224. DEBUGASSERT((he->h_name != NULL) && (he->h_addr_list != NULL));
  225. for(i=0; (curr = he->h_addr_list[i]) != NULL; i++) {
  226. size_t ss_size;
  227. #ifdef ENABLE_IPV6
  228. if (he->h_addrtype == AF_INET6)
  229. ss_size = sizeof (struct sockaddr_in6);
  230. else
  231. #endif
  232. ss_size = sizeof (struct sockaddr_in);
  233. if((ai = calloc(1, sizeof(Curl_addrinfo))) == NULL) {
  234. result = CURLE_OUT_OF_MEMORY;
  235. break;
  236. }
  237. if((ai->ai_canonname = strdup(he->h_name)) == NULL) {
  238. result = CURLE_OUT_OF_MEMORY;
  239. free(ai);
  240. break;
  241. }
  242. if((ai->ai_addr = calloc(1, ss_size)) == NULL) {
  243. result = CURLE_OUT_OF_MEMORY;
  244. free(ai->ai_canonname);
  245. free(ai);
  246. break;
  247. }
  248. if(!firstai)
  249. /* store the pointer we want to return from this function */
  250. firstai = ai;
  251. if(prevai)
  252. /* make the previous entry point to this */
  253. prevai->ai_next = ai;
  254. ai->ai_family = he->h_addrtype;
  255. /* we return all names as STREAM, so when using this address for TFTP
  256. the type must be ignored and conn->socktype be used instead! */
  257. ai->ai_socktype = SOCK_STREAM;
  258. ai->ai_addrlen = (int)ss_size;
  259. /* leave the rest of the struct filled with zero */
  260. switch (ai->ai_family) {
  261. case AF_INET:
  262. addr = (void *)ai->ai_addr; /* storage area for this info */
  263. memcpy(&addr->sin_addr, curr, sizeof(struct in_addr));
  264. addr->sin_family = (unsigned short)(he->h_addrtype);
  265. addr->sin_port = htons((unsigned short)port);
  266. break;
  267. #ifdef ENABLE_IPV6
  268. case AF_INET6:
  269. addr6 = (void *)ai->ai_addr; /* storage area for this info */
  270. memcpy(&addr6->sin6_addr, curr, sizeof(struct in6_addr));
  271. addr6->sin6_family = (unsigned short)(he->h_addrtype);
  272. addr6->sin6_port = htons((unsigned short)port);
  273. break;
  274. #endif
  275. }
  276. prevai = ai;
  277. }
  278. if(result != CURLE_OK) {
  279. Curl_freeaddrinfo(firstai);
  280. firstai = NULL;
  281. }
  282. return firstai;
  283. }
  284. struct namebuff {
  285. struct hostent hostentry;
  286. union {
  287. struct in_addr ina4;
  288. #ifdef ENABLE_IPV6
  289. struct in6_addr ina6;
  290. #endif
  291. } addrentry;
  292. char *h_addr_list[2];
  293. };
  294. /*
  295. * Curl_ip2addr()
  296. *
  297. * This function takes an internet address, in binary form, as input parameter
  298. * along with its address family and the string version of the address, and it
  299. * returns a Curl_addrinfo chain filled in correctly with information for the
  300. * given address/host
  301. */
  302. Curl_addrinfo *
  303. Curl_ip2addr(int af, const void *inaddr, const char *hostname, int port)
  304. {
  305. Curl_addrinfo *ai;
  306. #if defined(VMS) && \
  307. defined(__INITIAL_POINTER_SIZE) && (__INITIAL_POINTER_SIZE == 64)
  308. #pragma pointer_size save
  309. #pragma pointer_size short
  310. #pragma message disable PTRMISMATCH
  311. #endif
  312. struct hostent *h;
  313. struct namebuff *buf;
  314. char *addrentry;
  315. char *hoststr;
  316. size_t addrsize;
  317. DEBUGASSERT(inaddr && hostname);
  318. buf = malloc(sizeof(struct namebuff));
  319. if(!buf)
  320. return NULL;
  321. hoststr = strdup(hostname);
  322. if(!hoststr) {
  323. free(buf);
  324. return NULL;
  325. }
  326. switch(af) {
  327. case AF_INET:
  328. addrsize = sizeof(struct in_addr);
  329. addrentry = (void *)&buf->addrentry.ina4;
  330. memcpy(addrentry, inaddr, sizeof(struct in_addr));
  331. break;
  332. #ifdef ENABLE_IPV6
  333. case AF_INET6:
  334. addrsize = sizeof(struct in6_addr);
  335. addrentry = (void *)&buf->addrentry.ina6;
  336. memcpy(addrentry, inaddr, sizeof(struct in6_addr));
  337. break;
  338. #endif
  339. default:
  340. free(hoststr);
  341. free(buf);
  342. return NULL;
  343. }
  344. h = &buf->hostentry;
  345. h->h_name = hoststr;
  346. h->h_aliases = NULL;
  347. h->h_addrtype = (short)af;
  348. h->h_length = (short)addrsize;
  349. h->h_addr_list = &buf->h_addr_list[0];
  350. h->h_addr_list[0] = addrentry;
  351. h->h_addr_list[1] = NULL; /* terminate list of entries */
  352. #if defined(VMS) && \
  353. defined(__INITIAL_POINTER_SIZE) && (__INITIAL_POINTER_SIZE == 64)
  354. #pragma pointer_size restore
  355. #pragma message enable PTRMISMATCH
  356. #endif
  357. ai = Curl_he2ai(h, port);
  358. free(hoststr);
  359. free(buf);
  360. return ai;
  361. }
  362. #if defined(CURLDEBUG) && defined(HAVE_FREEADDRINFO)
  363. /*
  364. * curl_dofreeaddrinfo()
  365. *
  366. * This is strictly for memory tracing and are using the same style as the
  367. * family otherwise present in memdebug.c. I put these ones here since they
  368. * require a bunch of structs I didn't wanna include in memdebug.c
  369. */
  370. void
  371. curl_dofreeaddrinfo(struct addrinfo *freethis,
  372. int line, const char *source)
  373. {
  374. (freeaddrinfo)(freethis);
  375. if(logfile)
  376. fprintf(logfile, "ADDR %s:%d freeaddrinfo(%p)\n",
  377. source, line, (void *)freethis);
  378. }
  379. #endif /* defined(CURLDEBUG) && defined(HAVE_FREEADDRINFO) */
  380. #if defined(CURLDEBUG) && defined(HAVE_GETADDRINFO)
  381. /*
  382. * curl_dogetaddrinfo()
  383. *
  384. * This is strictly for memory tracing and are using the same style as the
  385. * family otherwise present in memdebug.c. I put these ones here since they
  386. * require a bunch of structs I didn't wanna include in memdebug.c
  387. */
  388. int
  389. curl_dogetaddrinfo(const char *hostname,
  390. const char *service,
  391. const struct addrinfo *hints,
  392. struct addrinfo **result,
  393. int line, const char *source)
  394. {
  395. int res=(getaddrinfo)(hostname, service, hints, result);
  396. if(0 == res) {
  397. /* success */
  398. if(logfile)
  399. fprintf(logfile, "ADDR %s:%d getaddrinfo() = %p\n",
  400. source, line, (void *)*result);
  401. }
  402. else {
  403. if(logfile)
  404. fprintf(logfile, "ADDR %s:%d getaddrinfo() failed\n",
  405. source, line);
  406. }
  407. return res;
  408. }
  409. #endif /* defined(CURLDEBUG) && defined(HAVE_GETADDRINFO) */