2
0

hostip6.c 6.3 KB

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