hostip6.c 6.6 KB

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