hostip6.c 8.3 KB

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