hostip6.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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. /***********************************************************************
  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 "sendf.h"
  44. #include "hostip.h"
  45. #include "hash.h"
  46. #include "share.h"
  47. #include "url.h"
  48. #include "inet_pton.h"
  49. #include "connect.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_ipvalid() checks what CURL_IPRESOLVE_* requirements that might've
  56. * been set and returns TRUE if they are OK.
  57. */
  58. bool Curl_ipvalid(struct Curl_easy *data, struct connectdata *conn)
  59. {
  60. if(conn->ip_version == CURL_IPRESOLVE_V6)
  61. return Curl_ipv6works(data);
  62. return TRUE;
  63. }
  64. #if defined(CURLRES_SYNCH)
  65. #ifdef DEBUG_ADDRINFO
  66. static void dump_addrinfo(struct connectdata *conn,
  67. const struct Curl_addrinfo *ai)
  68. {
  69. printf("dump_addrinfo:\n");
  70. for(; ai; ai = ai->ai_next) {
  71. char buf[INET6_ADDRSTRLEN];
  72. printf(" fam %2d, CNAME %s, ",
  73. ai->ai_family, ai->ai_canonname ? ai->ai_canonname : "<none>");
  74. Curl_printable_address(ai, buf, sizeof(buf));
  75. printf("%s\n", buf);
  76. }
  77. }
  78. #else
  79. #define dump_addrinfo(x,y) Curl_nop_stmt
  80. #endif
  81. /*
  82. * Curl_getaddrinfo() when built IPv6-enabled (non-threading and
  83. * non-ares version).
  84. *
  85. * Returns name information about the given hostname and port number. If
  86. * successful, the 'addrinfo' is returned and the fourth argument will point
  87. * to memory we need to free after use. That memory *MUST* be freed with
  88. * Curl_freeaddrinfo(), nothing else.
  89. */
  90. struct Curl_addrinfo *Curl_getaddrinfo(struct Curl_easy *data,
  91. const char *hostname,
  92. int port,
  93. int *waitp)
  94. {
  95. struct addrinfo hints;
  96. struct Curl_addrinfo *res;
  97. int error;
  98. char sbuf[12];
  99. char *sbufptr = NULL;
  100. #ifndef USE_RESOLVE_ON_IPS
  101. char addrbuf[128];
  102. #endif
  103. int pf = PF_INET;
  104. *waitp = 0; /* synchronous response only */
  105. if((data->conn->ip_version != CURL_IPRESOLVE_V4) && Curl_ipv6works(data))
  106. /* The stack seems to be IPv6-enabled */
  107. pf = PF_UNSPEC;
  108. memset(&hints, 0, sizeof(hints));
  109. hints.ai_family = pf;
  110. hints.ai_socktype = (data->conn->transport == TRNSPRT_TCP) ?
  111. SOCK_STREAM : SOCK_DGRAM;
  112. #ifndef USE_RESOLVE_ON_IPS
  113. /*
  114. * The AI_NUMERICHOST must not be set to get synthesized IPv6 address from
  115. * an IPv4 address on iOS and Mac OS X.
  116. */
  117. if((1 == Curl_inet_pton(AF_INET, hostname, addrbuf)) ||
  118. (1 == Curl_inet_pton(AF_INET6, hostname, addrbuf))) {
  119. /* the given address is numerical only, prevent a reverse lookup */
  120. hints.ai_flags = AI_NUMERICHOST;
  121. }
  122. #endif
  123. if(port) {
  124. msnprintf(sbuf, sizeof(sbuf), "%d", port);
  125. sbufptr = sbuf;
  126. }
  127. error = Curl_getaddrinfo_ex(hostname, sbufptr, &hints, &res);
  128. if(error) {
  129. infof(data, "getaddrinfo(3) failed for %s:%d", hostname, port);
  130. return NULL;
  131. }
  132. if(port) {
  133. Curl_addrinfo_set_port(res, port);
  134. }
  135. dump_addrinfo(conn, res);
  136. return res;
  137. }
  138. #endif /* CURLRES_SYNCH */
  139. #endif /* CURLRES_IPV6 */