hostasyn.c 3.8 KB

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