curl_addrinfo.c 16 KB

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