curl_addrinfo.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2017, 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 https://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 HAVE_SYS_UN_H
  34. # include <sys/un.h>
  35. #endif
  36. #ifdef __VMS
  37. # include <in.h>
  38. # include <inet.h>
  39. #endif
  40. #if defined(NETWARE) && defined(__NOVELL_LIBC__)
  41. # undef in_addr_t
  42. # define in_addr_t unsigned long
  43. #endif
  44. #include <stddef.h>
  45. #include "curl_addrinfo.h"
  46. #include "inet_pton.h"
  47. #include "warnless.h"
  48. /* The last 3 #include files should be in this order */
  49. #include "curl_printf.h"
  50. #include "curl_memory.h"
  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. free(ca->ai_addr);
  74. free(ca->ai_canonname);
  75. canext = ca->ai_next;
  76. free(ca);
  77. }
  78. }
  79. #ifdef HAVE_GETADDRINFO
  80. /*
  81. * Curl_getaddrinfo_ex()
  82. *
  83. * This is a wrapper function around system's getaddrinfo(), with
  84. * the only difference that instead of returning a linked list of
  85. * addrinfo structs this one returns a linked list of Curl_addrinfo
  86. * ones. The memory allocated by this function *MUST* be free'd with
  87. * Curl_freeaddrinfo(). For each successful call to this function
  88. * there must be an associated call later to Curl_freeaddrinfo().
  89. *
  90. * There should be no single call to system's getaddrinfo() in the
  91. * whole library, any such call should be 'routed' through this one.
  92. */
  93. int
  94. Curl_getaddrinfo_ex(const char *nodename,
  95. const char *servname,
  96. const struct addrinfo *hints,
  97. Curl_addrinfo **result)
  98. {
  99. const struct addrinfo *ai;
  100. struct addrinfo *aihead;
  101. Curl_addrinfo *cafirst = NULL;
  102. Curl_addrinfo *calast = NULL;
  103. Curl_addrinfo *ca;
  104. size_t ss_size;
  105. int error;
  106. *result = NULL; /* assume failure */
  107. error = getaddrinfo(nodename, servname, hints, &aihead);
  108. if(error)
  109. return error;
  110. /* traverse the addrinfo list */
  111. for(ai = aihead; ai != NULL; ai = ai->ai_next) {
  112. /* ignore elements with unsupported address family, */
  113. /* settle family-specific sockaddr structure size. */
  114. if(ai->ai_family == AF_INET)
  115. ss_size = sizeof(struct sockaddr_in);
  116. #ifdef ENABLE_IPV6
  117. else if(ai->ai_family == AF_INET6)
  118. ss_size = sizeof(struct sockaddr_in6);
  119. #endif
  120. else
  121. continue;
  122. /* ignore elements without required address info */
  123. if((ai->ai_addr == NULL) || !(ai->ai_addrlen > 0))
  124. continue;
  125. /* ignore elements with bogus address size */
  126. if((size_t)ai->ai_addrlen < ss_size)
  127. continue;
  128. ca = malloc(sizeof(Curl_addrinfo));
  129. if(!ca) {
  130. error = EAI_MEMORY;
  131. break;
  132. }
  133. /* copy each structure member individually, member ordering, */
  134. /* size, or padding might be different for each platform. */
  135. ca->ai_flags = ai->ai_flags;
  136. ca->ai_family = ai->ai_family;
  137. ca->ai_socktype = ai->ai_socktype;
  138. ca->ai_protocol = ai->ai_protocol;
  139. ca->ai_addrlen = (curl_socklen_t)ss_size;
  140. ca->ai_addr = NULL;
  141. ca->ai_canonname = NULL;
  142. ca->ai_next = NULL;
  143. ca->ai_addr = malloc(ss_size);
  144. if(!ca->ai_addr) {
  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. ca->ai_canonname = strdup(ai->ai_canonname);
  152. if(!ca->ai_canonname) {
  153. error = EAI_MEMORY;
  154. free(ca->ai_addr);
  155. free(ca);
  156. break;
  157. }
  158. }
  159. /* if the return list is empty, this becomes the first element */
  160. if(!cafirst)
  161. cafirst = ca;
  162. /* add this element last in the return list */
  163. if(calast)
  164. calast->ai_next = ca;
  165. calast = ca;
  166. }
  167. /* destroy the addrinfo list */
  168. if(aihead)
  169. freeaddrinfo(aihead);
  170. /* if we failed, also destroy the Curl_addrinfo list */
  171. if(error) {
  172. Curl_freeaddrinfo(cafirst);
  173. cafirst = NULL;
  174. }
  175. else if(!cafirst) {
  176. #ifdef EAI_NONAME
  177. /* rfc3493 conformant */
  178. error = EAI_NONAME;
  179. #else
  180. /* rfc3493 obsoleted */
  181. error = EAI_NODATA;
  182. #endif
  183. #ifdef USE_WINSOCK
  184. SET_SOCKERRNO(error);
  185. #endif
  186. }
  187. *result = cafirst;
  188. /* This is not a CURLcode */
  189. return error;
  190. }
  191. #endif /* HAVE_GETADDRINFO */
  192. /*
  193. * Curl_he2ai()
  194. *
  195. * This function returns a pointer to the first element of a newly allocated
  196. * Curl_addrinfo struct linked list filled with the data of a given hostent.
  197. * Curl_addrinfo is meant to work like the addrinfo struct does for a IPv6
  198. * stack, but usable also for IPv4, all hosts and environments.
  199. *
  200. * The memory allocated by this function *MUST* be free'd later on calling
  201. * Curl_freeaddrinfo(). For each successful call to this function there
  202. * must be an associated call later to Curl_freeaddrinfo().
  203. *
  204. * Curl_addrinfo defined in "lib/curl_addrinfo.h"
  205. *
  206. * struct Curl_addrinfo {
  207. * int ai_flags;
  208. * int ai_family;
  209. * int ai_socktype;
  210. * int ai_protocol;
  211. * curl_socklen_t ai_addrlen; * Follow rfc3493 struct addrinfo *
  212. * char *ai_canonname;
  213. * struct sockaddr *ai_addr;
  214. * struct Curl_addrinfo *ai_next;
  215. * };
  216. * typedef struct Curl_addrinfo Curl_addrinfo;
  217. *
  218. * hostent defined in <netdb.h>
  219. *
  220. * struct hostent {
  221. * char *h_name;
  222. * char **h_aliases;
  223. * int h_addrtype;
  224. * int h_length;
  225. * char **h_addr_list;
  226. * };
  227. *
  228. * for backward compatibility:
  229. *
  230. * #define h_addr h_addr_list[0]
  231. */
  232. Curl_addrinfo *
  233. Curl_he2ai(const struct hostent *he, int port)
  234. {
  235. Curl_addrinfo *ai;
  236. Curl_addrinfo *prevai = NULL;
  237. Curl_addrinfo *firstai = NULL;
  238. struct sockaddr_in *addr;
  239. #ifdef ENABLE_IPV6
  240. struct sockaddr_in6 *addr6;
  241. #endif
  242. CURLcode result = CURLE_OK;
  243. int i;
  244. char *curr;
  245. if(!he)
  246. /* no input == no output! */
  247. return NULL;
  248. DEBUGASSERT((he->h_name != NULL) && (he->h_addr_list != NULL));
  249. for(i=0; (curr = he->h_addr_list[i]) != NULL; i++) {
  250. size_t ss_size;
  251. #ifdef ENABLE_IPV6
  252. if(he->h_addrtype == AF_INET6)
  253. ss_size = sizeof(struct sockaddr_in6);
  254. else
  255. #endif
  256. ss_size = sizeof(struct sockaddr_in);
  257. ai = calloc(1, sizeof(Curl_addrinfo));
  258. if(!ai) {
  259. result = CURLE_OUT_OF_MEMORY;
  260. break;
  261. }
  262. ai->ai_canonname = strdup(he->h_name);
  263. if(!ai->ai_canonname) {
  264. result = CURLE_OUT_OF_MEMORY;
  265. free(ai);
  266. break;
  267. }
  268. ai->ai_addr = calloc(1, ss_size);
  269. if(!ai->ai_addr) {
  270. result = CURLE_OUT_OF_MEMORY;
  271. free(ai->ai_canonname);
  272. free(ai);
  273. break;
  274. }
  275. if(!firstai)
  276. /* store the pointer we want to return from this function */
  277. firstai = ai;
  278. if(prevai)
  279. /* make the previous entry point to this */
  280. prevai->ai_next = ai;
  281. ai->ai_family = he->h_addrtype;
  282. /* we return all names as STREAM, so when using this address for TFTP
  283. the type must be ignored and conn->socktype be used instead! */
  284. ai->ai_socktype = SOCK_STREAM;
  285. ai->ai_addrlen = (curl_socklen_t)ss_size;
  286. /* leave the rest of the struct filled with zero */
  287. switch(ai->ai_family) {
  288. case AF_INET:
  289. addr = (void *)ai->ai_addr; /* storage area for this info */
  290. memcpy(&addr->sin_addr, curr, sizeof(struct in_addr));
  291. addr->sin_family = (unsigned short)(he->h_addrtype);
  292. addr->sin_port = htons((unsigned short)port);
  293. break;
  294. #ifdef ENABLE_IPV6
  295. case AF_INET6:
  296. addr6 = (void *)ai->ai_addr; /* storage area for this info */
  297. memcpy(&addr6->sin6_addr, curr, sizeof(struct in6_addr));
  298. addr6->sin6_family = (unsigned short)(he->h_addrtype);
  299. addr6->sin6_port = htons((unsigned short)port);
  300. break;
  301. #endif
  302. }
  303. prevai = ai;
  304. }
  305. if(result) {
  306. Curl_freeaddrinfo(firstai);
  307. firstai = NULL;
  308. }
  309. return firstai;
  310. }
  311. struct namebuff {
  312. struct hostent hostentry;
  313. union {
  314. struct in_addr ina4;
  315. #ifdef ENABLE_IPV6
  316. struct in6_addr ina6;
  317. #endif
  318. } addrentry;
  319. char *h_addr_list[2];
  320. };
  321. /*
  322. * Curl_ip2addr()
  323. *
  324. * This function takes an internet address, in binary form, as input parameter
  325. * along with its address family and the string version of the address, and it
  326. * returns a Curl_addrinfo chain filled in correctly with information for the
  327. * given address/host
  328. */
  329. Curl_addrinfo *
  330. Curl_ip2addr(int af, const void *inaddr, const char *hostname, int port)
  331. {
  332. Curl_addrinfo *ai;
  333. #if defined(__VMS) && \
  334. defined(__INITIAL_POINTER_SIZE) && (__INITIAL_POINTER_SIZE == 64)
  335. #pragma pointer_size save
  336. #pragma pointer_size short
  337. #pragma message disable PTRMISMATCH
  338. #endif
  339. struct hostent *h;
  340. struct namebuff *buf;
  341. char *addrentry;
  342. char *hoststr;
  343. size_t addrsize;
  344. DEBUGASSERT(inaddr && hostname);
  345. buf = malloc(sizeof(struct namebuff));
  346. if(!buf)
  347. return NULL;
  348. hoststr = strdup(hostname);
  349. if(!hoststr) {
  350. free(buf);
  351. return NULL;
  352. }
  353. switch(af) {
  354. case AF_INET:
  355. addrsize = sizeof(struct in_addr);
  356. addrentry = (void *)&buf->addrentry.ina4;
  357. memcpy(addrentry, inaddr, sizeof(struct in_addr));
  358. break;
  359. #ifdef ENABLE_IPV6
  360. case AF_INET6:
  361. addrsize = sizeof(struct in6_addr);
  362. addrentry = (void *)&buf->addrentry.ina6;
  363. memcpy(addrentry, inaddr, sizeof(struct in6_addr));
  364. break;
  365. #endif
  366. default:
  367. free(hoststr);
  368. free(buf);
  369. return NULL;
  370. }
  371. h = &buf->hostentry;
  372. h->h_name = hoststr;
  373. h->h_aliases = NULL;
  374. h->h_addrtype = (short)af;
  375. h->h_length = (short)addrsize;
  376. h->h_addr_list = &buf->h_addr_list[0];
  377. h->h_addr_list[0] = addrentry;
  378. h->h_addr_list[1] = NULL; /* terminate list of entries */
  379. #if defined(__VMS) && \
  380. defined(__INITIAL_POINTER_SIZE) && (__INITIAL_POINTER_SIZE == 64)
  381. #pragma pointer_size restore
  382. #pragma message enable PTRMISMATCH
  383. #endif
  384. ai = Curl_he2ai(h, port);
  385. free(hoststr);
  386. free(buf);
  387. return ai;
  388. }
  389. /*
  390. * Given an IPv4 or IPv6 dotted string address, this converts it to a proper
  391. * allocated Curl_addrinfo struct and returns it.
  392. */
  393. Curl_addrinfo *Curl_str2addr(char *address, int port)
  394. {
  395. struct in_addr in;
  396. if(Curl_inet_pton(AF_INET, address, &in) > 0)
  397. /* This is a dotted IP address 123.123.123.123-style */
  398. return Curl_ip2addr(AF_INET, &in, address, port);
  399. #ifdef ENABLE_IPV6
  400. {
  401. struct in6_addr in6;
  402. if(Curl_inet_pton(AF_INET6, address, &in6) > 0)
  403. /* This is a dotted IPv6 address ::1-style */
  404. return Curl_ip2addr(AF_INET6, &in6, address, port);
  405. }
  406. #endif
  407. return NULL; /* bad input format */
  408. }
  409. #ifdef USE_UNIX_SOCKETS
  410. /**
  411. * Given a path to a Unix domain socket, return a newly allocated Curl_addrinfo
  412. * struct initialized with this path.
  413. * Set '*longpath' to TRUE if the error is a too long path.
  414. */
  415. Curl_addrinfo *Curl_unix2addr(const char *path, bool *longpath, bool abstract)
  416. {
  417. Curl_addrinfo *ai;
  418. struct sockaddr_un *sa_un;
  419. size_t path_len;
  420. *longpath = FALSE;
  421. ai = calloc(1, sizeof(Curl_addrinfo));
  422. if(!ai)
  423. return NULL;
  424. ai->ai_addr = calloc(1, sizeof(struct sockaddr_un));
  425. if(!ai->ai_addr) {
  426. free(ai);
  427. return NULL;
  428. }
  429. sa_un = (void *) ai->ai_addr;
  430. sa_un->sun_family = AF_UNIX;
  431. /* sun_path must be able to store the NUL-terminated path */
  432. path_len = strlen(path) + 1;
  433. if(path_len > sizeof(sa_un->sun_path)) {
  434. free(ai->ai_addr);
  435. free(ai);
  436. *longpath = TRUE;
  437. return NULL;
  438. }
  439. ai->ai_family = AF_UNIX;
  440. ai->ai_socktype = SOCK_STREAM; /* assume reliable transport for HTTP */
  441. ai->ai_addrlen = (curl_socklen_t)
  442. ((offsetof(struct sockaddr_un, sun_path) + path_len) & 0x7FFFFFFF);
  443. /* Abstract Unix domain socket have NULL prefix instead of suffix */
  444. if(abstract)
  445. memcpy(sa_un->sun_path + 1, path, path_len - 1);
  446. else
  447. memcpy(sa_un->sun_path, path, path_len); /* copy NUL byte */
  448. return ai;
  449. }
  450. #endif
  451. #if defined(CURLDEBUG) && defined(HAVE_FREEADDRINFO)
  452. /*
  453. * curl_dofreeaddrinfo()
  454. *
  455. * This is strictly for memory tracing and are using the same style as the
  456. * family otherwise present in memdebug.c. I put these ones here since they
  457. * require a bunch of structs I didn't want to include in memdebug.c
  458. */
  459. void
  460. curl_dofreeaddrinfo(struct addrinfo *freethis,
  461. int line, const char *source)
  462. {
  463. #ifdef USE_LWIPSOCK
  464. lwip_freeaddrinfo(freethis);
  465. #else
  466. (freeaddrinfo)(freethis);
  467. #endif
  468. curl_memlog("ADDR %s:%d freeaddrinfo(%p)\n",
  469. source, line, (void *)freethis);
  470. }
  471. #endif /* defined(CURLDEBUG) && defined(HAVE_FREEADDRINFO) */
  472. #if defined(CURLDEBUG) && defined(HAVE_GETADDRINFO)
  473. /*
  474. * curl_dogetaddrinfo()
  475. *
  476. * This is strictly for memory tracing and are using the same style as the
  477. * family otherwise present in memdebug.c. I put these ones here since they
  478. * require a bunch of structs I didn't want to include in memdebug.c
  479. */
  480. int
  481. curl_dogetaddrinfo(const char *hostname,
  482. const char *service,
  483. const struct addrinfo *hints,
  484. struct addrinfo **result,
  485. int line, const char *source)
  486. {
  487. #ifdef USE_LWIPSOCK
  488. int res=lwip_getaddrinfo(hostname, service, hints, result);
  489. #else
  490. int res=(getaddrinfo)(hostname, service, hints, result);
  491. #endif
  492. if(0 == res)
  493. /* success */
  494. curl_memlog("ADDR %s:%d getaddrinfo() = %p\n",
  495. source, line, (void *)*result);
  496. else
  497. curl_memlog("ADDR %s:%d getaddrinfo() failed\n",
  498. source, line);
  499. return res;
  500. }
  501. #endif /* defined(CURLDEBUG) && defined(HAVE_GETADDRINFO) */
  502. #if defined(HAVE_GETADDRINFO) && defined(USE_RESOLVE_ON_IPS)
  503. /*
  504. * Work-arounds the sin6_port is always zero bug on iOS 9.3.2 and Mac OS X
  505. * 10.11.5.
  506. */
  507. void Curl_addrinfo_set_port(Curl_addrinfo *addrinfo, int port)
  508. {
  509. Curl_addrinfo *ca;
  510. struct sockaddr_in *addr;
  511. #ifdef ENABLE_IPV6
  512. struct sockaddr_in6 *addr6;
  513. #endif
  514. for(ca = addrinfo; ca != NULL; ca = ca->ai_next) {
  515. switch(ca->ai_family) {
  516. case AF_INET:
  517. addr = (void *)ca->ai_addr; /* storage area for this info */
  518. addr->sin_port = htons((unsigned short)port);
  519. break;
  520. #ifdef ENABLE_IPV6
  521. case AF_INET6:
  522. addr6 = (void *)ca->ai_addr; /* storage area for this info */
  523. addr6->sin6_port = htons((unsigned short)port);
  524. break;
  525. #endif
  526. }
  527. }
  528. }
  529. #endif