hostasyn.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2006, 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_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. #define _MPRINTF_REPLACE /* use our functions only */
  68. #include <curl/mprintf.h>
  69. #if defined(HAVE_INET_NTOA_R) && !defined(HAVE_INET_NTOA_R_DECL)
  70. #include "inet_ntoa_r.h"
  71. #endif
  72. #include "memory.h"
  73. /* The last #include file should be: */
  74. #include "memdebug.h"
  75. /***********************************************************************
  76. * Only for builds using asynchronous name resolves
  77. **********************************************************************/
  78. #ifdef CURLRES_ASYNCH
  79. /*
  80. * addrinfo_callback() gets called by ares, gethostbyname_thread() or
  81. * getaddrinfo_thread() when we got the name resolved (or not!).
  82. *
  83. * If the status argument is CURL_ASYNC_SUCCESS, we might need to copy the
  84. * address field since it might be freed when this function returns. This
  85. * operation stores the resolved data in the DNS cache.
  86. *
  87. * NOTE: for IPv6 operations, Curl_addrinfo_copy() returns the same
  88. * pointer it is given as argument!
  89. *
  90. * The storage operation locks and unlocks the DNS cache.
  91. */
  92. static CURLcode addrinfo_callback(void *arg, /* "struct connectdata *" */
  93. int status,
  94. void *addr)
  95. {
  96. struct connectdata *conn = (struct connectdata *)arg;
  97. struct Curl_dns_entry *dns = NULL;
  98. CURLcode rc = CURLE_OK;
  99. conn->async.status = status;
  100. if(CURL_ASYNC_SUCCESS == status) {
  101. /*
  102. * IPv4/ares: Curl_addrinfo_copy() copies the address and returns an
  103. * allocated version.
  104. *
  105. * IPv6: Curl_addrinfo_copy() returns the input pointer!
  106. */
  107. Curl_addrinfo *ai = Curl_addrinfo_copy(addr, conn->async.port);
  108. if(ai) {
  109. struct SessionHandle *data = conn->data;
  110. if(data->share)
  111. Curl_share_lock(data, CURL_LOCK_DATA_DNS, CURL_LOCK_ACCESS_SINGLE);
  112. dns = Curl_cache_addr(data, ai,
  113. conn->async.hostname,
  114. conn->async.port);
  115. if(!dns) {
  116. /* failed to store, cleanup and return error */
  117. Curl_freeaddrinfo(ai);
  118. rc = CURLE_OUT_OF_MEMORY;
  119. }
  120. if(data->share)
  121. Curl_share_unlock(data, CURL_LOCK_DATA_DNS);
  122. }
  123. else
  124. rc = CURLE_OUT_OF_MEMORY;
  125. }
  126. conn->async.dns = dns;
  127. /* Set async.done TRUE last in this function since it may be used multi-
  128. threaded and once this is TRUE the other thread may read fields from the
  129. async struct */
  130. conn->async.done = TRUE;
  131. /* ipv4: The input hostent struct will be freed by ares when we return from
  132. this function */
  133. return rc;
  134. }
  135. CURLcode Curl_addrinfo4_callback(void *arg, /* "struct connectdata *" */
  136. int status,
  137. struct hostent *hostent)
  138. {
  139. return addrinfo_callback(arg, status, hostent);
  140. }
  141. #ifdef CURLRES_IPV6
  142. CURLcode Curl_addrinfo6_callback(void *arg, /* "struct connectdata *" */
  143. int status,
  144. struct addrinfo *ai)
  145. {
  146. /* NOTE: for CURLRES_ARES, the 'ai' argument is really a
  147. * 'struct hostent' pointer.
  148. */
  149. return addrinfo_callback(arg, status, ai);
  150. }
  151. #endif
  152. #endif /* CURLRES_ASYNC */