curl_addrinfo.c 16 KB

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