hostip6.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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. /***********************************************************************
  26. * Only for IPv6-enabled builds
  27. **********************************************************************/
  28. #ifdef CURLRES_IPV6
  29. #ifdef HAVE_NETINET_IN_H
  30. #include <netinet/in.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 __VMS
  39. #include <in.h>
  40. #include <inet.h>
  41. #endif
  42. #include "urldata.h"
  43. #include "cfilters.h"
  44. #include "sendf.h"
  45. #include "hostip.h"
  46. #include "hash.h"
  47. #include "share.h"
  48. #include "url.h"
  49. #include "curlx/inet_pton.h"
  50. #include "connect.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. #if defined(CURLRES_SYNCH)
  56. #ifdef DEBUG_ADDRINFO
  57. static void dump_addrinfo(const struct Curl_addrinfo *ai)
  58. {
  59. printf("dump_addrinfo:\n");
  60. for(; ai; ai = ai->ai_next) {
  61. char buf[INET6_ADDRSTRLEN];
  62. printf(" fam %2d, CNAME %s, ",
  63. ai->ai_family, ai->ai_canonname ? ai->ai_canonname : "<none>");
  64. Curl_printable_address(ai, buf, sizeof(buf));
  65. printf("%s\n", buf);
  66. }
  67. }
  68. #else
  69. #define dump_addrinfo(x) Curl_nop_stmt
  70. #endif
  71. /*
  72. * Curl_sync_getaddrinfo() when built IPv6-enabled (non-threading and
  73. * non-ares version).
  74. *
  75. * Returns name information about the given hostname and port number. If
  76. * successful, the 'addrinfo' is returned and the fourth argument will point
  77. * to memory we need to free after use. That memory *MUST* be freed with
  78. * Curl_freeaddrinfo(), nothing else.
  79. */
  80. struct Curl_addrinfo *Curl_sync_getaddrinfo(struct Curl_easy *data,
  81. const char *hostname,
  82. int port,
  83. int ip_version)
  84. {
  85. struct addrinfo hints;
  86. struct Curl_addrinfo *res;
  87. int error;
  88. char sbuf[12];
  89. char *sbufptr = NULL;
  90. #ifndef USE_RESOLVE_ON_IPS
  91. char addrbuf[128];
  92. #endif
  93. int pf = PF_INET;
  94. if((ip_version != CURL_IPRESOLVE_V4) && Curl_ipv6works(data))
  95. /* The stack seems to be IPv6-enabled */
  96. pf = PF_UNSPEC;
  97. memset(&hints, 0, sizeof(hints));
  98. hints.ai_family = pf;
  99. hints.ai_socktype =
  100. (Curl_conn_get_transport(data, data->conn) == TRNSPRT_TCP) ?
  101. SOCK_STREAM : SOCK_DGRAM;
  102. #ifndef USE_RESOLVE_ON_IPS
  103. /*
  104. * The AI_NUMERICHOST must not be set to get synthesized IPv6 address from
  105. * an IPv4 address on iOS and macOS.
  106. */
  107. if((1 == curlx_inet_pton(AF_INET, hostname, addrbuf)) ||
  108. (1 == curlx_inet_pton(AF_INET6, hostname, addrbuf))) {
  109. /* the given address is numerical only, prevent a reverse lookup */
  110. hints.ai_flags = AI_NUMERICHOST;
  111. }
  112. #endif
  113. if(port) {
  114. msnprintf(sbuf, sizeof(sbuf), "%d", port);
  115. sbufptr = sbuf;
  116. }
  117. error = Curl_getaddrinfo_ex(hostname, sbufptr, &hints, &res);
  118. if(error) {
  119. infof(data, "getaddrinfo(3) failed for %s:%d", hostname, port);
  120. return NULL;
  121. }
  122. if(port) {
  123. Curl_addrinfo_set_port(res, port);
  124. }
  125. dump_addrinfo(res);
  126. return res;
  127. }
  128. #endif /* CURLRES_SYNCH */
  129. #endif /* CURLRES_IPV6 */