blob: c15af7a1cb33bc7880159c5aa6476cfe70ad2a97 [file] [log] [blame]
Harald Alvestrand8d4a5f12023-08-22 13:53:161/*
2 * Copyright 2023 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#ifndef RTC_BASE_ASYNC_DNS_RESOLVER_H_
11#define RTC_BASE_ASYNC_DNS_RESOLVER_H_
12
13#include <vector>
14
15#include "api/async_dns_resolver.h"
16#include "api/sequence_checker.h"
17#include "api/task_queue/pending_task_safety_flag.h"
Harald Alvestrand8219cc32023-09-02 05:26:5518#include "rtc_base/ref_counted_object.h"
Harald Alvestrand8d4a5f12023-08-22 13:53:1619#include "rtc_base/thread_annotations.h"
20
21namespace webrtc {
22// This file contains a default implementation of
23// webrtc::AsyncDnsResolverInterface, for use when there is no need for special
24// treatment.
25
26class AsyncDnsResolverResultImpl : public AsyncDnsResolverResult {
27 public:
28 bool GetResolvedAddress(int family, rtc::SocketAddress* addr) const override;
29 // Returns error from resolver.
30 int GetError() const override;
31
32 private:
33 friend class AsyncDnsResolver;
34 RTC_NO_UNIQUE_ADDRESS webrtc::SequenceChecker sequence_checker_;
35 rtc::SocketAddress addr_ RTC_GUARDED_BY(sequence_checker_);
36 std::vector<rtc::IPAddress> addresses_ RTC_GUARDED_BY(sequence_checker_);
37 int error_ RTC_GUARDED_BY(sequence_checker_);
38};
39
40class AsyncDnsResolver : public AsyncDnsResolverInterface {
41 public:
Harald Alvestrand8219cc32023-09-02 05:26:5542 AsyncDnsResolver();
43 ~AsyncDnsResolver();
Harald Alvestrand8d4a5f12023-08-22 13:53:1644 // Start address resolution of the hostname in `addr`.
45 void Start(const rtc::SocketAddress& addr,
Harald Alvestrand96e18822023-08-30 19:39:0546 absl::AnyInvocable<void()> callback) override;
Harald Alvestrand8d4a5f12023-08-22 13:53:1647 // Start address resolution of the hostname in `addr` matching `family`.
48 void Start(const rtc::SocketAddress& addr,
49 int family,
Harald Alvestrand96e18822023-08-30 19:39:0550 absl::AnyInvocable<void()> callback) override;
Harald Alvestrand8d4a5f12023-08-22 13:53:1651 const AsyncDnsResolverResult& result() const override;
52
53 private:
Harald Alvestrand8219cc32023-09-02 05:26:5554 class State;
55 ScopedTaskSafety safety_; // To check for client going away
56 rtc::scoped_refptr<State> state_; // To check for "this" going away
Harald Alvestrand8d4a5f12023-08-22 13:53:1657 AsyncDnsResolverResultImpl result_;
Harald Alvestrand96e18822023-08-30 19:39:0558 absl::AnyInvocable<void()> callback_;
Harald Alvestrand8d4a5f12023-08-22 13:53:1659};
60
61} // namespace webrtc
62#endif // RTC_BASE_ASYNC_DNS_RESOLVER_H_