hostip6.c 6.4 KB

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