blob: 138503b59f74e7d74d536e3f7702ad5afd094924 [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
Harald Alvestrandb7b306b2021-10-05 14:05:36 +000054// The API for a single name query.
55// The constructor, destructor and all functions must be called from
56// the same sequence, and the callback will also be called on that sequence.
57// The class guarantees that the callback will not be called if the
58// resolver's destructor has been called.
Harald Alvestrande6e2f282021-03-24 12:13:28 +000059class RTC_EXPORT AsyncDnsResolverInterface {
60 public:
61 virtual ~AsyncDnsResolverInterface() = default;
62
Artem Titov0e61fdd2021-07-25 21:50:14 +020063 // Start address resolution of the hostname in `addr`.
Harald Alvestrande6e2f282021-03-24 12:13:28 +000064 virtual void Start(const rtc::SocketAddress& addr,
65 std::function<void()> callback) = 0;
66 virtual const AsyncDnsResolverResult& result() const = 0;
67};
68
69// An abstract factory for creating AsyncDnsResolverInterfaces. This allows
70// client applications to provide WebRTC with their own mechanism for
71// performing DNS resolution.
72class AsyncDnsResolverFactoryInterface {
73 public:
74 virtual ~AsyncDnsResolverFactoryInterface() = default;
75
76 // Creates an AsyncDnsResolver and starts resolving the name. The callback
77 // will be called when resolution is finished.
Harald Alvestrandb7b306b2021-10-05 14:05:36 +000078 // The callback will be called on the sequence that the caller runs on.
Harald Alvestrande6e2f282021-03-24 12:13:28 +000079 virtual std::unique_ptr<webrtc::AsyncDnsResolverInterface> CreateAndResolve(
80 const rtc::SocketAddress& addr,
81 std::function<void()> callback) = 0;
82 // Creates an AsyncDnsResolver and does not start it.
83 // For backwards compatibility, will be deprecated and removed.
84 // One has to do a separate Start() call on the
85 // resolver to start name resolution.
86 virtual std::unique_ptr<webrtc::AsyncDnsResolverInterface> Create() = 0;
87};
88
89} // namespace webrtc
90
91#endif // API_ASYNC_DNS_RESOLVER_H_