blob: 288751efa45243aef6dc1e082149148ef268363d [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 Alvestrandfb658ca2023-10-10 07:24:5419#include "rtc_base/system/rtc_export.h"
Harald Alvestrand8d4a5f12023-08-22 13:53:1620#include "rtc_base/thread_annotations.h"
21
22namespace webrtc {
23// This file contains a default implementation of
24// webrtc::AsyncDnsResolverInterface, for use when there is no need for special
25// treatment.
26
27class AsyncDnsResolverResultImpl : public AsyncDnsResolverResult {
28 public:
29 bool GetResolvedAddress(int family, rtc::SocketAddress* addr) const override;
30 // Returns error from resolver.
31 int GetError() const override;
32
33 private:
34 friend class AsyncDnsResolver;
35 RTC_NO_UNIQUE_ADDRESS webrtc::SequenceChecker sequence_checker_;
36 rtc::SocketAddress addr_ RTC_GUARDED_BY(sequence_checker_);
37 std::vector<rtc::IPAddress> addresses_ RTC_GUARDED_BY(sequence_checker_);
38 int error_ RTC_GUARDED_BY(sequence_checker_);
39};
40
Harald Alvestrandfb658ca2023-10-10 07:24:5441class RTC_EXPORT AsyncDnsResolver : public AsyncDnsResolverInterface {
Harald Alvestrand8d4a5f12023-08-22 13:53:1642 public:
Harald Alvestrand8219cc32023-09-02 05:26:5543 AsyncDnsResolver();
44 ~AsyncDnsResolver();
Harald Alvestrand8d4a5f12023-08-22 13:53:1645 // Start address resolution of the hostname in `addr`.
46 void Start(const rtc::SocketAddress& addr,
Harald Alvestrand96e18822023-08-30 19:39:0547 absl::AnyInvocable<void()> callback) override;
Harald Alvestrand8d4a5f12023-08-22 13:53:1648 // Start address resolution of the hostname in `addr` matching `family`.
49 void Start(const rtc::SocketAddress& addr,
50 int family,
Harald Alvestrand96e18822023-08-30 19:39:0551 absl::AnyInvocable<void()> callback) override;
Harald Alvestrand8d4a5f12023-08-22 13:53:1652 const AsyncDnsResolverResult& result() const override;
53
54 private:
Harald Alvestrand8219cc32023-09-02 05:26:5555 class State;
56 ScopedTaskSafety safety_; // To check for client going away
57 rtc::scoped_refptr<State> state_; // To check for "this" going away
Harald Alvestrand8d4a5f12023-08-22 13:53:1658 AsyncDnsResolverResultImpl result_;
Harald Alvestrand96e18822023-08-30 19:39:0559 absl::AnyInvocable<void()> callback_;
Harald Alvestrand8d4a5f12023-08-22 13:53:1660};
61
62} // namespace webrtc
63#endif // RTC_BASE_ASYNC_DNS_RESOLVER_H_