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