hostip6.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 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(const struct Curl_addrinfo *ai)
  67. {
  68. printf("dump_addrinfo:\n");
  69. for(; ai; ai = ai->ai_next) {
  70. char buf[INET6_ADDRSTRLEN];
  71. printf(" fam %2d, CNAME %s, ",
  72. ai->ai_family, ai->ai_canonname ? ai->ai_canonname : "<none>");
  73. Curl_printable_address(ai, buf, sizeof(buf));
  74. printf("%s\n", buf);
  75. }
  76. }
  77. #else
  78. #define dump_addrinfo(x) Curl_nop_stmt
  79. #endif
  80. /*
  81. * Curl_getaddrinfo() when built IPv6-enabled (non-threading and
  82. * non-ares version).
  83. *
  84. * Returns name information about the given hostname and port number. If
  85. * successful, the 'addrinfo' is returned and the fourth argument will point
  86. * to memory we need to free after use. That memory *MUST* be freed with
  87. * Curl_freeaddrinfo(), nothing else.
  88. */
  89. struct Curl_addrinfo *Curl_getaddrinfo(struct Curl_easy *data,
  90. const char *hostname,
  91. int port,
  92. int *waitp)
  93. {
  94. struct addrinfo hints;
  95. struct Curl_addrinfo *res;
  96. int error;
  97. char sbuf[12];
  98. char *sbufptr = NULL;
  99. #ifndef USE_RESOLVE_ON_IPS
  100. char addrbuf[128];
  101. #endif
  102. int pf = PF_INET;
  103. *waitp = 0; /* synchronous response only */
  104. if((data->conn->ip_version != CURL_IPRESOLVE_V4) && Curl_ipv6works(data))
  105. /* The stack seems to be IPv6-enabled */
  106. pf = PF_UNSPEC;
  107. memset(&hints, 0, sizeof(hints));
  108. hints.ai_family = pf;
  109. hints.ai_socktype = (data->conn->transport == TRNSPRT_TCP) ?
  110. SOCK_STREAM : SOCK_DGRAM;
  111. #ifndef USE_RESOLVE_ON_IPS
  112. /*
  113. * The AI_NUMERICHOST must not be set to get synthesized IPv6 address from
  114. * an IPv4 address on iOS and Mac OS X.
  115. */
  116. if((1 == Curl_inet_pton(AF_INET, hostname, addrbuf)) ||
  117. (1 == Curl_inet_pton(AF_INET6, hostname, addrbuf))) {
  118. /* the given address is numerical only, prevent a reverse lookup */
  119. hints.ai_flags = AI_NUMERICHOST;
  120. }
  121. #endif
  122. if(port) {
  123. msnprintf(sbuf, sizeof(sbuf), "%d", port);
  124. sbufptr = sbuf;
  125. }
  126. error = Curl_getaddrinfo_ex(hostname, sbufptr, &hints, &res);
  127. if(error) {
  128. infof(data, "getaddrinfo(3) failed for %s:%d", hostname, port);
  129. return NULL;
  130. }
  131. if(port) {
  132. Curl_addrinfo_set_port(res, port);
  133. }
  134. dump_addrinfo(res);
  135. return res;
  136. }
  137. #endif /* CURLRES_SYNCH */
  138. #endif /* CURLRES_IPV6 */