hostip6.c 5.3 KB

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