Harald Alvestrand | 8d4a5f1 | 2023-08-22 13:53:16 | [diff] [blame] | 1 | /* |
| 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 Alvestrand | 8219cc3 | 2023-09-02 05:26:55 | [diff] [blame] | 18 | #include "rtc_base/ref_counted_object.h" |
Harald Alvestrand | 8d4a5f1 | 2023-08-22 13:53:16 | [diff] [blame] | 19 | #include "rtc_base/thread_annotations.h" |
| 20 | |
| 21 | namespace webrtc { |
| 22 | // This file contains a default implementation of |
| 23 | // webrtc::AsyncDnsResolverInterface, for use when there is no need for special |
| 24 | // treatment. |
| 25 | |
| 26 | class 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 | |
| 40 | class AsyncDnsResolver : public AsyncDnsResolverInterface { |
| 41 | public: |
Harald Alvestrand | 8219cc3 | 2023-09-02 05:26:55 | [diff] [blame] | 42 | AsyncDnsResolver(); |
| 43 | ~AsyncDnsResolver(); |
Harald Alvestrand | 8d4a5f1 | 2023-08-22 13:53:16 | [diff] [blame] | 44 | // Start address resolution of the hostname in `addr`. |
| 45 | void Start(const rtc::SocketAddress& addr, |
Harald Alvestrand | 96e1882 | 2023-08-30 19:39:05 | [diff] [blame] | 46 | absl::AnyInvocable<void()> callback) override; |
Harald Alvestrand | 8d4a5f1 | 2023-08-22 13:53:16 | [diff] [blame] | 47 | // Start address resolution of the hostname in `addr` matching `family`. |
| 48 | void Start(const rtc::SocketAddress& addr, |
| 49 | int family, |
Harald Alvestrand | 96e1882 | 2023-08-30 19:39:05 | [diff] [blame] | 50 | absl::AnyInvocable<void()> callback) override; |
Harald Alvestrand | 8d4a5f1 | 2023-08-22 13:53:16 | [diff] [blame] | 51 | const AsyncDnsResolverResult& result() const override; |
| 52 | |
| 53 | private: |
Harald Alvestrand | 8219cc3 | 2023-09-02 05:26:55 | [diff] [blame] | 54 | class State; |
| 55 | ScopedTaskSafety safety_; // To check for client going away |
| 56 | rtc::scoped_refptr<State> state_; // To check for "this" going away |
Harald Alvestrand | 8d4a5f1 | 2023-08-22 13:53:16 | [diff] [blame] | 57 | AsyncDnsResolverResultImpl result_; |
Harald Alvestrand | 96e1882 | 2023-08-30 19:39:05 | [diff] [blame] | 58 | absl::AnyInvocable<void()> callback_; |
Harald Alvestrand | 8d4a5f1 | 2023-08-22 13:53:16 | [diff] [blame] | 59 | }; |
| 60 | |
| 61 | } // namespace webrtc |
| 62 | #endif // RTC_BASE_ASYNC_DNS_RESOLVER_H_ |