1
0

hostip6.c 7.0 KB

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