hostip6.c 4.4 KB

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