hostip6.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2016, 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.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. #ifdef HAVE_NETINET_IN_H
  24. #include <netinet/in.h>
  25. #endif
  26. #ifdef HAVE_NETDB_H
  27. #include <netdb.h>
  28. #endif
  29. #ifdef HAVE_ARPA_INET_H
  30. #include <arpa/inet.h>
  31. #endif
  32. #ifdef __VMS
  33. #include <in.h>
  34. #include <inet.h>
  35. #endif
  36. #ifdef HAVE_PROCESS_H
  37. #include <process.h>
  38. #endif
  39. #include "urldata.h"
  40. #include "sendf.h"
  41. #include "hostip.h"
  42. #include "hash.h"
  43. #include "share.h"
  44. #include "strerror.h"
  45. #include "url.h"
  46. #include "inet_pton.h"
  47. #include "connect.h"
  48. /* The last 3 #include files should be in this order */
  49. #include "curl_printf.h"
  50. #include "curl_memory.h"
  51. #include "memdebug.h"
  52. /***********************************************************************
  53. * Only for IPv6-enabled builds
  54. **********************************************************************/
  55. #ifdef CURLRES_IPV6
  56. #if defined(CURLDEBUG) && defined(HAVE_GETNAMEINFO)
  57. /* These are strictly for memory tracing and are using the same style as the
  58. * family otherwise present in memdebug.c. I put these ones here since they
  59. * require a bunch of structs I didn't want to include in memdebug.c
  60. */
  61. /*
  62. * For CURLRES_ARS, this should be written using ares_gethostbyaddr()
  63. * (ignoring the fact c-ares doesn't return 'serv').
  64. */
  65. int curl_dogetnameinfo(GETNAMEINFO_QUAL_ARG1 GETNAMEINFO_TYPE_ARG1 sa,
  66. GETNAMEINFO_TYPE_ARG2 salen,
  67. char *host, GETNAMEINFO_TYPE_ARG46 hostlen,
  68. char *serv, GETNAMEINFO_TYPE_ARG46 servlen,
  69. GETNAMEINFO_TYPE_ARG7 flags,
  70. int line, const char *source)
  71. {
  72. int res = (getnameinfo)(sa, salen,
  73. host, hostlen,
  74. serv, servlen,
  75. flags);
  76. if(0 == res)
  77. /* success */
  78. curl_memlog("GETNAME %s:%d getnameinfo()\n",
  79. source, line);
  80. else
  81. curl_memlog("GETNAME %s:%d getnameinfo() failed = %d\n",
  82. source, line, res);
  83. return res;
  84. }
  85. #endif /* defined(CURLDEBUG) && defined(HAVE_GETNAMEINFO) */
  86. /*
  87. * Curl_ipv6works() returns TRUE if IPv6 seems to work.
  88. */
  89. bool Curl_ipv6works(void)
  90. {
  91. /* the nature of most system is that IPv6 status doesn't come and go
  92. during a program's lifetime so we only probe the first time and then we
  93. have the info kept for fast re-use */
  94. static int ipv6_works = -1;
  95. if(-1 == ipv6_works) {
  96. /* probe to see if we have a working IPv6 stack */
  97. curl_socket_t s = socket(PF_INET6, SOCK_DGRAM, 0);
  98. if(s == CURL_SOCKET_BAD)
  99. /* an IPv6 address was requested but we can't get/use one */
  100. ipv6_works = 0;
  101. else {
  102. ipv6_works = 1;
  103. Curl_closesocket(NULL, s);
  104. }
  105. }
  106. return (ipv6_works>0)?TRUE:FALSE;
  107. }
  108. /*
  109. * Curl_ipvalid() checks what CURL_IPRESOLVE_* requirements that might've
  110. * been set and returns TRUE if they are OK.
  111. */
  112. bool Curl_ipvalid(struct connectdata *conn)
  113. {
  114. if(conn->ip_version == CURL_IPRESOLVE_V6)
  115. return Curl_ipv6works();
  116. return TRUE;
  117. }
  118. #if defined(CURLRES_SYNCH)
  119. #ifdef DEBUG_ADDRINFO
  120. static void dump_addrinfo(struct connectdata *conn, const Curl_addrinfo *ai)
  121. {
  122. printf("dump_addrinfo:\n");
  123. for(; ai; ai = ai->ai_next) {
  124. char buf[INET6_ADDRSTRLEN];
  125. printf(" fam %2d, CNAME %s, ",
  126. ai->ai_family, ai->ai_canonname ? ai->ai_canonname : "<none>");
  127. if(Curl_printable_address(ai, buf, sizeof(buf)))
  128. printf("%s\n", buf);
  129. else
  130. printf("failed; %s\n", Curl_strerror(conn, SOCKERRNO));
  131. }
  132. }
  133. #else
  134. #define dump_addrinfo(x,y) Curl_nop_stmt
  135. #endif
  136. /*
  137. * Curl_getaddrinfo() when built IPv6-enabled (non-threading and
  138. * non-ares version).
  139. *
  140. * Returns name information about the given hostname and port number. If
  141. * successful, the 'addrinfo' is returned and the forth argument will point to
  142. * memory we need to free after use. That memory *MUST* be freed with
  143. * Curl_freeaddrinfo(), nothing else.
  144. */
  145. Curl_addrinfo *Curl_getaddrinfo(struct connectdata *conn,
  146. const char *hostname,
  147. int port,
  148. int *waitp)
  149. {
  150. struct addrinfo hints;
  151. Curl_addrinfo *res;
  152. int error;
  153. char sbuf[12];
  154. char *sbufptr = NULL;
  155. #ifndef USE_RESOLVE_ON_IPS
  156. char addrbuf[128];
  157. #endif
  158. int pf;
  159. #if !defined(CURL_DISABLE_VERBOSE_STRINGS)
  160. struct Curl_easy *data = conn->data;
  161. #endif
  162. *waitp = 0; /* synchronous response only */
  163. /* Check if a limited name resolve has been requested */
  164. switch(conn->ip_version) {
  165. case CURL_IPRESOLVE_V4:
  166. pf = PF_INET;
  167. break;
  168. case CURL_IPRESOLVE_V6:
  169. pf = PF_INET6;
  170. break;
  171. default:
  172. pf = PF_UNSPEC;
  173. break;
  174. }
  175. if((pf != PF_INET) && !Curl_ipv6works())
  176. /* The stack seems to be a non-IPv6 one */
  177. pf = PF_INET;
  178. memset(&hints, 0, sizeof(hints));
  179. hints.ai_family = pf;
  180. hints.ai_socktype = conn->socktype;
  181. #ifndef USE_RESOLVE_ON_IPS
  182. /*
  183. * The AI_NUMERICHOST must not be set to get synthesized IPv6 address from
  184. * an IPv4 address on iOS and Mac OS X.
  185. */
  186. if((1 == Curl_inet_pton(AF_INET, hostname, addrbuf)) ||
  187. (1 == Curl_inet_pton(AF_INET6, hostname, addrbuf))) {
  188. /* the given address is numerical only, prevent a reverse lookup */
  189. hints.ai_flags = AI_NUMERICHOST;
  190. }
  191. #endif
  192. if(port) {
  193. snprintf(sbuf, sizeof(sbuf), "%d", port);
  194. sbufptr=sbuf;
  195. }
  196. error = Curl_getaddrinfo_ex(hostname, sbufptr, &hints, &res);
  197. if(error) {
  198. infof(data, "getaddrinfo(3) failed for %s:%d\n", hostname, port);
  199. return NULL;
  200. }
  201. if(port) {
  202. Curl_addrinfo_set_port(res, port);
  203. }
  204. dump_addrinfo(conn, res);
  205. return res;
  206. }
  207. #endif /* CURLRES_SYNCH */
  208. #endif /* CURLRES_IPV6 */