hostasyn.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2009, 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. #include <string.h>
  24. #ifdef HAVE_SYS_SOCKET_H
  25. #include <sys/socket.h>
  26. #endif
  27. #ifdef HAVE_NETINET_IN_H
  28. #include <netinet/in.h>
  29. #endif
  30. #ifdef HAVE_NETDB_H
  31. #include <netdb.h>
  32. #endif
  33. #ifdef HAVE_ARPA_INET_H
  34. #include <arpa/inet.h>
  35. #endif
  36. #ifdef HAVE_STDLIB_H
  37. #include <stdlib.h> /* required for free() prototypes */
  38. #endif
  39. #ifdef HAVE_UNISTD_H
  40. #include <unistd.h> /* for the close() proto */
  41. #endif
  42. #ifdef __VMS
  43. #include <in.h>
  44. #include <inet.h>
  45. #include <stdlib.h>
  46. #endif
  47. #ifdef HAVE_PROCESS_H
  48. #include <process.h>
  49. #endif
  50. #include "urldata.h"
  51. #include "sendf.h"
  52. #include "hostip.h"
  53. #include "hash.h"
  54. #include "share.h"
  55. #include "strerror.h"
  56. #include "url.h"
  57. #define _MPRINTF_REPLACE /* use our functions only */
  58. #include <curl/mprintf.h>
  59. #include "curl_memory.h"
  60. /* The last #include file should be: */
  61. #include "memdebug.h"
  62. /***********************************************************************
  63. * Only for builds using asynchronous name resolves
  64. **********************************************************************/
  65. #ifdef CURLRES_ASYNCH
  66. /*
  67. * Curl_addrinfo_callback() gets called by ares, gethostbyname_thread()
  68. * or getaddrinfo_thread() when we got the name resolved (or not!).
  69. *
  70. * If the status argument is CURL_ASYNC_SUCCESS, this function takes
  71. * ownership of the Curl_addrinfo passed, storing the resolved data
  72. * in the DNS cache.
  73. *
  74. * The storage operation locks and unlocks the DNS cache.
  75. */
  76. CURLcode Curl_addrinfo_callback(struct connectdata * conn,
  77. int status,
  78. struct Curl_addrinfo *ai)
  79. {
  80. struct Curl_dns_entry *dns = NULL;
  81. CURLcode rc = CURLE_OK;
  82. conn->async.status = status;
  83. if(CURL_ASYNC_SUCCESS == status) {
  84. if(ai) {
  85. struct SessionHandle *data = conn->data;
  86. if(data->share)
  87. Curl_share_lock(data, CURL_LOCK_DATA_DNS, CURL_LOCK_ACCESS_SINGLE);
  88. dns = Curl_cache_addr(data, ai,
  89. conn->async.hostname,
  90. conn->async.port);
  91. if(!dns) {
  92. /* failed to store, cleanup and return error */
  93. Curl_freeaddrinfo(ai);
  94. rc = CURLE_OUT_OF_MEMORY;
  95. }
  96. if(data->share)
  97. Curl_share_unlock(data, CURL_LOCK_DATA_DNS);
  98. }
  99. else
  100. rc = CURLE_OUT_OF_MEMORY;
  101. }
  102. conn->async.dns = dns;
  103. /* Set async.done TRUE last in this function since it may be used multi-
  104. threaded and once this is TRUE the other thread may read fields from the
  105. async struct */
  106. conn->async.done = TRUE;
  107. /* ipv4: The input hostent struct will be freed by ares when we return from
  108. this function */
  109. return rc;
  110. }
  111. #endif /* CURLRES_ASYNCH */