hostip6.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2021, 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. ***************************************************************************/
  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 "strerror.h"
  49. #include "url.h"
  50. #include "inet_pton.h"
  51. #include "connect.h"
  52. /* The last 3 #include files should be in this order */
  53. #include "curl_printf.h"
  54. #include "curl_memory.h"
  55. #include "memdebug.h"
  56. /*
  57. * Curl_ipv6works() returns TRUE if IPv6 seems to work.
  58. */
  59. bool Curl_ipv6works(struct Curl_easy *data)
  60. {
  61. if(data) {
  62. /* the nature of most system is that IPv6 status doesn't come and go
  63. during a program's lifetime so we only probe the first time and then we
  64. have the info kept for fast re-use */
  65. DEBUGASSERT(data);
  66. DEBUGASSERT(data->multi);
  67. return data->multi->ipv6_works;
  68. }
  69. else {
  70. int ipv6_works = -1;
  71. /* probe to see if we have a working IPv6 stack */
  72. curl_socket_t s = socket(PF_INET6, SOCK_DGRAM, 0);
  73. if(s == CURL_SOCKET_BAD)
  74. /* an IPv6 address was requested but we can't get/use one */
  75. ipv6_works = 0;
  76. else {
  77. ipv6_works = 1;
  78. sclose(s);
  79. }
  80. return (ipv6_works>0)?TRUE:FALSE;
  81. }
  82. }
  83. /*
  84. * Curl_ipvalid() checks what CURL_IPRESOLVE_* requirements that might've
  85. * been set and returns TRUE if they are OK.
  86. */
  87. bool Curl_ipvalid(struct Curl_easy *data, struct connectdata *conn)
  88. {
  89. if(conn->ip_version == CURL_IPRESOLVE_V6)
  90. return Curl_ipv6works(data);
  91. return TRUE;
  92. }
  93. #if defined(CURLRES_SYNCH)
  94. #ifdef DEBUG_ADDRINFO
  95. static void dump_addrinfo(struct connectdata *conn,
  96. const struct Curl_addrinfo *ai)
  97. {
  98. printf("dump_addrinfo:\n");
  99. for(; ai; ai = ai->ai_next) {
  100. char buf[INET6_ADDRSTRLEN];
  101. printf(" fam %2d, CNAME %s, ",
  102. ai->ai_family, ai->ai_canonname ? ai->ai_canonname : "<none>");
  103. Curl_printable_address(ai, buf, sizeof(buf));
  104. printf("%s\n", buf);
  105. }
  106. }
  107. #else
  108. #define dump_addrinfo(x,y) Curl_nop_stmt
  109. #endif
  110. /*
  111. * Curl_getaddrinfo() when built IPv6-enabled (non-threading and
  112. * non-ares version).
  113. *
  114. * Returns name information about the given hostname and port number. If
  115. * successful, the 'addrinfo' is returned and the forth argument will point to
  116. * memory we need to free after use. That memory *MUST* be freed with
  117. * Curl_freeaddrinfo(), nothing else.
  118. */
  119. struct Curl_addrinfo *Curl_getaddrinfo(struct Curl_easy *data,
  120. const char *hostname,
  121. int port,
  122. int *waitp)
  123. {
  124. struct addrinfo hints;
  125. struct Curl_addrinfo *res;
  126. int error;
  127. char sbuf[12];
  128. char *sbufptr = NULL;
  129. #ifndef USE_RESOLVE_ON_IPS
  130. char addrbuf[128];
  131. #endif
  132. int pf;
  133. *waitp = 0; /* synchronous response only */
  134. /* Check if a limited name resolve has been requested */
  135. switch(data->set.ipver) {
  136. case CURL_IPRESOLVE_V4:
  137. pf = PF_INET;
  138. break;
  139. case CURL_IPRESOLVE_V6:
  140. pf = PF_INET6;
  141. break;
  142. default:
  143. pf = PF_UNSPEC;
  144. break;
  145. }
  146. if((pf != PF_INET) && !Curl_ipv6works(data))
  147. /* The stack seems to be a non-IPv6 one */
  148. pf = PF_INET;
  149. memset(&hints, 0, sizeof(hints));
  150. hints.ai_family = pf;
  151. hints.ai_socktype = (data->conn->transport == TRNSPRT_TCP) ?
  152. SOCK_STREAM : SOCK_DGRAM;
  153. #ifndef USE_RESOLVE_ON_IPS
  154. /*
  155. * The AI_NUMERICHOST must not be set to get synthesized IPv6 address from
  156. * an IPv4 address on iOS and Mac OS X.
  157. */
  158. if((1 == Curl_inet_pton(AF_INET, hostname, addrbuf)) ||
  159. (1 == Curl_inet_pton(AF_INET6, hostname, addrbuf))) {
  160. /* the given address is numerical only, prevent a reverse lookup */
  161. hints.ai_flags = AI_NUMERICHOST;
  162. }
  163. #endif
  164. if(port) {
  165. msnprintf(sbuf, sizeof(sbuf), "%d", port);
  166. sbufptr = sbuf;
  167. }
  168. error = Curl_getaddrinfo_ex(hostname, sbufptr, &hints, &res);
  169. if(error) {
  170. infof(data, "getaddrinfo(3) failed for %s:%d\n", hostname, port);
  171. return NULL;
  172. }
  173. if(port) {
  174. Curl_addrinfo_set_port(res, port);
  175. }
  176. dump_addrinfo(conn, res);
  177. return res;
  178. }
  179. #endif /* CURLRES_SYNCH */
  180. #endif /* CURLRES_IPV6 */