2
0

curl_addrinfo.c 16 KB

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