hostip6.c 4.4 KB

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