2
0

hostasyn.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2011, 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. #ifdef HAVE_SYS_SOCKET_H
  24. #include <sys/socket.h>
  25. #endif
  26. #ifdef HAVE_NETINET_IN_H
  27. #include <netinet/in.h>
  28. #endif
  29. #ifdef HAVE_NETDB_H
  30. #include <netdb.h>
  31. #endif
  32. #ifdef HAVE_ARPA_INET_H
  33. #include <arpa/inet.h>
  34. #endif
  35. #ifdef HAVE_UNISTD_H
  36. #include <unistd.h> /* for the close() proto */
  37. #endif
  38. #ifdef __VMS
  39. #include <in.h>
  40. #include <inet.h>
  41. #endif
  42. #ifdef HAVE_PROCESS_H
  43. #include <process.h>
  44. #endif
  45. #include "urldata.h"
  46. #include "sendf.h"
  47. #include "hostip.h"
  48. #include "hash.h"
  49. #include "share.h"
  50. #include "strerror.h"
  51. #include "url.h"
  52. #define _MPRINTF_REPLACE /* use our functions only */
  53. #include <curl/mprintf.h>
  54. #include "curl_memory.h"
  55. /* The last #include file should be: */
  56. #include "memdebug.h"
  57. /***********************************************************************
  58. * Only for builds using asynchronous name resolves
  59. **********************************************************************/
  60. #ifdef CURLRES_ASYNCH
  61. /*
  62. * Curl_addrinfo_callback() gets called by ares, gethostbyname_thread()
  63. * or getaddrinfo_thread() when we got the name resolved (or not!).
  64. *
  65. * If the status argument is CURL_ASYNC_SUCCESS, this function takes
  66. * ownership of the Curl_addrinfo passed, storing the resolved data
  67. * in the DNS cache.
  68. *
  69. * The storage operation locks and unlocks the DNS cache.
  70. */
  71. CURLcode Curl_addrinfo_callback(struct connectdata *conn,
  72. int status,
  73. struct Curl_addrinfo *ai)
  74. {
  75. struct Curl_dns_entry *dns = NULL;
  76. CURLcode rc = CURLE_OK;
  77. conn->async.status = status;
  78. if(CURL_ASYNC_SUCCESS == status) {
  79. if(ai) {
  80. struct SessionHandle *data = conn->data;
  81. if(data->share)
  82. Curl_share_lock(data, CURL_LOCK_DATA_DNS, CURL_LOCK_ACCESS_SINGLE);
  83. dns = Curl_cache_addr(data, ai,
  84. conn->async.hostname,
  85. conn->async.port);
  86. if(!dns) {
  87. /* failed to store, cleanup and return error */
  88. Curl_freeaddrinfo(ai);
  89. rc = CURLE_OUT_OF_MEMORY;
  90. }
  91. if(data->share)
  92. Curl_share_unlock(data, CURL_LOCK_DATA_DNS);
  93. }
  94. else {
  95. rc = CURLE_OUT_OF_MEMORY;
  96. }
  97. }
  98. conn->async.dns = dns;
  99. /* Set async.done TRUE last in this function since it may be used multi-
  100. threaded and once this is TRUE the other thread may read fields from the
  101. async struct */
  102. conn->async.done = TRUE;
  103. /* ipv4: The input hostent struct will be freed by ares when we return from
  104. this function */
  105. return rc;
  106. }
  107. /* Call this function after Curl_connect() has returned async=TRUE and
  108. then a successful name resolve has been received.
  109. Note: this function disconnects and frees the conn data in case of
  110. resolve failure */
  111. CURLcode Curl_async_resolved(struct connectdata *conn,
  112. bool *protocol_done)
  113. {
  114. CURLcode code;
  115. if(conn->async.dns) {
  116. conn->dns_entry = conn->async.dns;
  117. conn->async.dns = NULL;
  118. }
  119. code = Curl_setup_conn(conn, protocol_done);
  120. if(code)
  121. /* We're not allowed to return failure with memory left allocated
  122. in the connectdata struct, free those here */
  123. Curl_disconnect(conn, FALSE); /* close the connection */
  124. return code;
  125. }
  126. /*
  127. * Curl_getaddrinfo() is the generic low-level name resolve API within this
  128. * source file. There are several versions of this function - for different
  129. * name resolve layers (selected at build-time). They all take this same set
  130. * of arguments
  131. */
  132. Curl_addrinfo *Curl_getaddrinfo(struct connectdata *conn,
  133. const char *hostname,
  134. int port,
  135. int *waitp)
  136. {
  137. return Curl_resolver_getaddrinfo(conn, hostname, port, waitp);
  138. }
  139. #endif /* CURLRES_ASYNCH */