hostip6.c 6.5 KB

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