hostasyn.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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 builds using asynchronous name resolves
  84. **********************************************************************/
  85. #ifdef CURLRES_ASYNCH
  86. /*
  87. * addrinfo_callback() gets called by ares, gethostbyname_thread() or
  88. * getaddrinfo_thread() when we got the name resolved (or not!).
  89. *
  90. * If the status argument is CURL_ASYNC_SUCCESS, we might need to copy the
  91. * address field since it might be freed when this function returns. This
  92. * operation stores the resolved data in the DNS cache.
  93. *
  94. * NOTE: for IPv6 operations, Curl_addrinfo_copy() returns the same
  95. * pointer it is given as argument!
  96. *
  97. * The storage operation locks and unlocks the DNS cache.
  98. */
  99. static void addrinfo_callback(void *arg, /* "struct connectdata *" */
  100. int status,
  101. void *addr)
  102. {
  103. struct connectdata *conn = (struct connectdata *)arg;
  104. struct Curl_dns_entry *dns = NULL;
  105. conn->async.done = TRUE;
  106. conn->async.status = status;
  107. if(CURL_ASYNC_SUCCESS == status) {
  108. /*
  109. * IPv4: Curl_addrinfo_copy() copies the address and returns an allocated
  110. * version.
  111. *
  112. * IPv6: Curl_addrinfo_copy() returns the input pointer!
  113. */
  114. Curl_addrinfo *ai = Curl_addrinfo_copy(addr, conn->async.port);
  115. if(ai) {
  116. struct SessionHandle *data = conn->data;
  117. if(data->share)
  118. Curl_share_lock(data, CURL_LOCK_DATA_DNS, CURL_LOCK_ACCESS_SINGLE);
  119. dns = Curl_cache_addr(data, ai,
  120. conn->async.hostname,
  121. conn->async.port);
  122. if(!dns)
  123. /* failed to store, cleanup and return error */
  124. Curl_freeaddrinfo(ai);
  125. if(data->share)
  126. Curl_share_unlock(data, CURL_LOCK_DATA_DNS);
  127. }
  128. }
  129. conn->async.dns = dns;
  130. /* ipv4: The input hostent struct will be freed by ares when we return from
  131. this function */
  132. }
  133. void Curl_addrinfo4_callback(void *arg, /* "struct connectdata *" */
  134. int status,
  135. struct hostent *hostent)
  136. {
  137. addrinfo_callback(arg, status, hostent);
  138. }
  139. #ifdef CURLRES_IPV6
  140. void Curl_addrinfo6_callback(void *arg, /* "struct connectdata *" */
  141. int status,
  142. struct addrinfo *ai)
  143. {
  144. addrinfo_callback(arg, status, ai);
  145. }
  146. #endif
  147. #endif /* CURLRES_ASYNC */