hostasyn.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2021, 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 https://curl.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. /***********************************************************************
  24. * Only for builds using asynchronous name resolves
  25. **********************************************************************/
  26. #ifdef CURLRES_ASYNCH
  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 __VMS
  37. #include <in.h>
  38. #include <inet.h>
  39. #endif
  40. #ifdef HAVE_PROCESS_H
  41. #include <process.h>
  42. #endif
  43. #include "urldata.h"
  44. #include "sendf.h"
  45. #include "hostip.h"
  46. #include "hash.h"
  47. #include "share.h"
  48. #include "url.h"
  49. #include "curl_memory.h"
  50. /* The last #include file should be: */
  51. #include "memdebug.h"
  52. /*
  53. * Curl_addrinfo_callback() gets called by ares, gethostbyname_thread()
  54. * or getaddrinfo_thread() when we got the name resolved (or not!).
  55. *
  56. * If the status argument is CURL_ASYNC_SUCCESS, this function takes
  57. * ownership of the Curl_addrinfo passed, storing the resolved data
  58. * in the DNS cache.
  59. *
  60. * The storage operation locks and unlocks the DNS cache.
  61. */
  62. CURLcode Curl_addrinfo_callback(struct Curl_easy *data,
  63. int status,
  64. struct Curl_addrinfo *ai)
  65. {
  66. struct Curl_dns_entry *dns = NULL;
  67. CURLcode result = CURLE_OK;
  68. data->state.async.status = status;
  69. if(CURL_ASYNC_SUCCESS == status) {
  70. if(ai) {
  71. if(data->share)
  72. Curl_share_lock(data, CURL_LOCK_DATA_DNS, CURL_LOCK_ACCESS_SINGLE);
  73. dns = Curl_cache_addr(data, ai,
  74. data->state.async.hostname,
  75. data->state.async.port);
  76. if(data->share)
  77. Curl_share_unlock(data, CURL_LOCK_DATA_DNS);
  78. if(!dns) {
  79. /* failed to store, cleanup and return error */
  80. Curl_freeaddrinfo(ai);
  81. result = CURLE_OUT_OF_MEMORY;
  82. }
  83. }
  84. else {
  85. result = CURLE_OUT_OF_MEMORY;
  86. }
  87. }
  88. data->state.async.dns = dns;
  89. /* Set async.done TRUE last in this function since it may be used multi-
  90. threaded and once this is TRUE the other thread may read fields from the
  91. async struct */
  92. data->state.async.done = TRUE;
  93. /* IPv4: The input hostent struct will be freed by ares when we return from
  94. this function */
  95. return result;
  96. }
  97. /*
  98. * Curl_getaddrinfo() is the generic low-level name resolve API within this
  99. * source file. There are several versions of this function - for different
  100. * name resolve layers (selected at build-time). They all take this same set
  101. * of arguments
  102. */
  103. struct Curl_addrinfo *Curl_getaddrinfo(struct Curl_easy *data,
  104. const char *hostname,
  105. int port,
  106. int *waitp)
  107. {
  108. return Curl_resolver_getaddrinfo(data, hostname, port, waitp);
  109. }
  110. #endif /* CURLRES_ASYNCH */