| 6 | * tree. An additional intellectual property rights grant can be found |
| 7 | * in the file PATENTS. All contributing project authors may |
| 8 | * be found in the AUTHORS file in the root of the source tree. |
| 9 | */ |
| 10 |
| 11 | #ifndef API_ASYNC_DNS_RESOLVER_H_ |
| 12 | #define API_ASYNC_DNS_RESOLVER_H_ |
| 13 |
| 14 | #include <functional> |
| 15 | #include <memory> |
| 16 |
| 17 | #include "absl/functional/any_invocable.h" |
| 18 | #include "rtc_base/checks.h" |
| 19 | #include "rtc_base/socket_address.h" |
| 20 | #include "rtc_base/system/rtc_export.h" |
| 21 |
| 22 | namespace webrtc { |
| 23 |
| 24 | // This interface defines the methods to resolve a hostname asynchronously. |
| 25 | // The AsyncDnsResolverInterface class encapsulates a single name query. |
| 26 | // |
| 27 | // Usage: |
| 28 | // std::unique_ptr<AsyncDnsResolverInterface> resolver = |
| 29 | // factory->Create(address-to-be-resolved, [r = resolver.get()]() { |
| 30 | // if (r->result.GetResolvedAddress(AF_INET, &addr) { |
| 31 | // // success |
| 32 | // } else { |
| 33 | // // failure |
| 34 | // error = r->result().GetError(); |
| 35 | // } |
| 36 | // // Release resolver. |
| 37 | // resolver_list.erase(std::remove_if(resolver_list.begin(), |
| 38 | // resolver_list.end(), |
| 39 | // [](refptr) { refptr.get() == r; }); |
| 40 | // }); |
| 41 | // resolver_list.push_back(std::move(resolver)); |
| 42 |
| 43 | class AsyncDnsResolverResult { |
| 44 | public: |
| 45 | virtual ~AsyncDnsResolverResult() = default; |
| 46 | // Returns true iff the address from `Start` was successfully resolved. |
| 47 | // If the address was successfully resolved, sets `addr` to a copy of the |
| 48 | // address from `Start` with the IP address set to the top most resolved |
| 49 | // address of `family` (`addr` will have both hostname and the resolved ip). |
| 50 | virtual bool GetResolvedAddress(int family, |
| 51 | rtc::SocketAddress* addr) const = 0; |
| 52 | // Returns error from resolver. |
| 53 | virtual int GetError() const = 0; |
| 54 | }; |
| 55 |
| 56 | // The API for a single name query. |
| 57 | // The constructor, destructor and all functions must be called from |
| 58 | // the same sequence, and the callback will also be called on that sequence. |
| 59 | // The class guarantees that the callback will not be called if the |
| 60 | // resolver's destructor has been called. |
| 61 | class RTC_EXPORT AsyncDnsResolverInterface { |
| 62 | public: |
| 63 | virtual ~AsyncDnsResolverInterface() = default; |
| 64 |
| 65 | // Start address resolution of the hostname in `addr`. |
| 66 | virtual void Start(const rtc::SocketAddress& addr, |
| 67 | absl::AnyInvocable<void()> callback) = 0; |
| 68 | // Start address resolution of the hostname in `addr` matching `family`. |
| 69 | virtual void Start(const rtc::SocketAddress& addr, |
| 70 | int family, |
| 71 | absl::AnyInvocable<void()> callback) = 0; |
| 72 | virtual const AsyncDnsResolverResult& result() const = 0; |
| 73 | }; |
| 74 |
| 75 | // An abstract factory for creating AsyncDnsResolverInterfaces. This allows |
| 76 | // client applications to provide WebRTC with their own mechanism for |
| 77 | // performing DNS resolution. |
| 78 | class AsyncDnsResolverFactoryInterface { |
| 79 | public: |
| 80 | virtual ~AsyncDnsResolverFactoryInterface() = default; |
| 81 |
| 82 | // Creates an AsyncDnsResolver and starts resolving the name. The callback |
| 83 | // will be called when resolution is finished. |
| 84 | // The callback will be called on the sequence that the caller runs on. |
| 85 | virtual std::unique_ptr<webrtc::AsyncDnsResolverInterface> CreateAndResolve( |
| 86 | const rtc::SocketAddress& addr, |
| 87 | absl::AnyInvocable<void()> callback) = 0; |
| 88 | // Creates an AsyncDnsResolver and starts resolving the name to an address |
| 89 | // matching the specified family. The callback will be called when resolution |
| 90 | // is finished. The callback will be called on the sequence that the caller |
| 91 | // runs on. |
| 92 | virtual std::unique_ptr<webrtc::AsyncDnsResolverInterface> CreateAndResolve( |
| 93 | const rtc::SocketAddress& addr, |
| 94 | int family, |
| 95 | absl::AnyInvocable<void()> callback) = 0; |
| 96 | // Creates an AsyncDnsResolver and does not start it. |
| 97 | // For backwards compatibility, will be deprecated and removed. |
| 98 | // One has to do a separate Start() call on the |
| 99 | // resolver to start name resolution. |
| 100 | virtual std::unique_ptr<webrtc::AsyncDnsResolverInterface> Create() = 0; |
| 101 | }; |
| 102 |
| 103 | } // namespace webrtc |
| 104 |
| 105 | #endif // API_ASYNC_DNS_RESOLVER_H_ |
| 106 |