hostasyn.c 4.6 KB

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