blob: 800ffa613dfe7fd5d59c124b63484c7667090f5d [file] [log] [blame]
Harald Alvestrande6e2f282021-03-24 12:13:28 +00001/*
2 * Copyright 2021 The WebRTC Project Authors. All rights reserved.
3 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
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
Harald Alvestrande6e2f282021-03-24 12:13:28 +000014#include <memory>
15
16#include "rtc_base/socket_address.h"
17#include "rtc_base/system/rtc_export.h"
18
19namespace webrtc {
20
21// This interface defines the methods to resolve a hostname asynchronously.
22// The AsyncDnsResolverInterface class encapsulates a single name query.
23//
24// Usage:
25// std::unique_ptr<AsyncDnsResolverInterface> resolver =
26// factory->Create(address-to-be-resolved, [r = resolver.get()]() {
27// if (r->result.GetResolvedAddress(AF_INET, &addr) {
28// // success
29// } else {
30// // failure
31// error = r->result().GetError();
32// }
33// // Release resolver.
34// resolver_list.erase(std::remove_if(resolver_list.begin(),
35// resolver_list.end(),
36// [](refptr) { refptr.get() == r; });
37// });
38// resolver_list.push_back(std::move(resolver));
39
40class AsyncDnsResolverResult {
41 public:
42 virtual ~AsyncDnsResolverResult() = default;
43 // Returns true iff the address from |Start| was successfully resolved.
44 // If the address was successfully resolved, sets |addr| to a copy of the
45 // address from |Start| with the IP address set to the top most resolved
46 // address of |family| (|addr| will have both hostname and the resolved ip).
47 virtual bool GetResolvedAddress(int family,
48 rtc::SocketAddress* addr) const = 0;
49 // Returns error from resolver.
50 virtual int GetError() const = 0;
51};
52
53class RTC_EXPORT AsyncDnsResolverInterface {
54 public:
55 virtual ~AsyncDnsResolverInterface() = default;
56
57 // Start address resolution of the hostname in |addr|.
58 virtual void Start(const rtc::SocketAddress& addr,
59 std::function<void()> callback) = 0;
60 virtual const AsyncDnsResolverResult& result() const = 0;
61};
62
63// An abstract factory for creating AsyncDnsResolverInterfaces. This allows
64// client applications to provide WebRTC with their own mechanism for
65// performing DNS resolution.
66class AsyncDnsResolverFactoryInterface {
67 public:
68 virtual ~AsyncDnsResolverFactoryInterface() = default;
69
70 // Creates an AsyncDnsResolver and starts resolving the name. The callback
71 // will be called when resolution is finished.
72 // The callback will be called on the thread that the caller runs on.
73 virtual std::unique_ptr<webrtc::AsyncDnsResolverInterface> CreateAndResolve(
74 const rtc::SocketAddress& addr,
75 std::function<void()> callback) = 0;
76 // Creates an AsyncDnsResolver and does not start it.
77 // For backwards compatibility, will be deprecated and removed.
78 // One has to do a separate Start() call on the
79 // resolver to start name resolution.
80 virtual std::unique_ptr<webrtc::AsyncDnsResolverInterface> Create() = 0;
81};
82
83} // namespace webrtc
84
85#endif // API_ASYNC_DNS_RESOLVER_H_