HostResolver.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /**
  2. * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
  3. * SPDX-License-Identifier: Apache-2.0.
  4. */
  5. #include <aws/crt/io/HostResolver.h>
  6. #include <aws/crt/io/EventLoopGroup.h>
  7. #include <aws/common/string.h>
  8. #include <aws/crt/Api.h>
  9. namespace Aws
  10. {
  11. namespace Crt
  12. {
  13. namespace Io
  14. {
  15. HostResolver::~HostResolver() {}
  16. DefaultHostResolver::DefaultHostResolver(
  17. EventLoopGroup &elGroup,
  18. size_t maxHosts,
  19. size_t maxTTL,
  20. Allocator *allocator) noexcept
  21. : m_resolver(nullptr), m_allocator(allocator), m_initialized(false)
  22. {
  23. AWS_ZERO_STRUCT(m_config);
  24. struct aws_host_resolver_default_options resolver_options;
  25. AWS_ZERO_STRUCT(resolver_options);
  26. resolver_options.max_entries = maxHosts;
  27. resolver_options.el_group = elGroup.GetUnderlyingHandle();
  28. m_resolver = aws_host_resolver_new_default(allocator, &resolver_options);
  29. if (m_resolver != nullptr)
  30. {
  31. m_initialized = true;
  32. }
  33. m_config.impl = aws_default_dns_resolve;
  34. m_config.impl_data = nullptr;
  35. m_config.max_ttl = maxTTL;
  36. }
  37. DefaultHostResolver::DefaultHostResolver(size_t maxHosts, size_t maxTTL, Allocator *allocator) noexcept
  38. : DefaultHostResolver(
  39. *Crt::ApiHandle::GetOrCreateStaticDefaultEventLoopGroup(),
  40. maxHosts,
  41. maxTTL,
  42. allocator)
  43. {
  44. }
  45. DefaultHostResolver::~DefaultHostResolver()
  46. {
  47. aws_host_resolver_release(m_resolver);
  48. m_initialized = false;
  49. }
  50. /**
  51. * @private
  52. */
  53. struct DefaultHostResolveArgs
  54. {
  55. Allocator *allocator;
  56. HostResolver *resolver;
  57. OnHostResolved onResolved;
  58. aws_string *host;
  59. };
  60. void DefaultHostResolver::s_onHostResolved(
  61. struct aws_host_resolver *,
  62. const struct aws_string *hostName,
  63. int errCode,
  64. const struct aws_array_list *hostAddresses,
  65. void *userData)
  66. {
  67. DefaultHostResolveArgs *args = static_cast<DefaultHostResolveArgs *>(userData);
  68. size_t len = aws_array_list_length(hostAddresses);
  69. Vector<HostAddress> addresses;
  70. for (size_t i = 0; i < len; ++i)
  71. {
  72. HostAddress *address_ptr = NULL;
  73. aws_array_list_get_at_ptr(hostAddresses, reinterpret_cast<void **>(&address_ptr), i);
  74. addresses.push_back(*address_ptr);
  75. }
  76. String host(aws_string_c_str(hostName), hostName->len);
  77. args->onResolved(*args->resolver, addresses, errCode);
  78. aws_string_destroy(args->host);
  79. Delete(args, args->allocator);
  80. }
  81. bool DefaultHostResolver::ResolveHost(const String &host, const OnHostResolved &onResolved) noexcept
  82. {
  83. DefaultHostResolveArgs *args = New<DefaultHostResolveArgs>(m_allocator);
  84. if (!args)
  85. {
  86. return false;
  87. }
  88. args->host = aws_string_new_from_array(
  89. m_allocator, reinterpret_cast<const uint8_t *>(host.data()), host.length());
  90. args->onResolved = onResolved;
  91. args->resolver = this;
  92. args->allocator = m_allocator;
  93. if (!args->host ||
  94. aws_host_resolver_resolve_host(m_resolver, args->host, s_onHostResolved, &m_config, args))
  95. {
  96. Delete(args, m_allocator);
  97. return false;
  98. }
  99. return true;
  100. }
  101. } // namespace Io
  102. } // namespace Crt
  103. } // namespace Aws