hostip6.c 7.2 KB

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