hostip6.c 5.5 KB

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