curl_addrinfo.c 16 KB

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