hostip6.c 8.3 KB

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