Mirko Bonadei | 8592111 | 2020-12-16 13:08:43 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2008 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 RTC_BASE_ASYNC_RESOLVER_H_ |
| 12 | #define RTC_BASE_ASYNC_RESOLVER_H_ |
| 13 | |
Mirko Bonadei | e5f4c6b | 2021-01-15 09:41:01 | [diff] [blame] | 14 | #if defined(WEBRTC_POSIX) |
| 15 | #include <sys/socket.h> |
| 16 | #elif WEBRTC_WIN |
| 17 | #include <winsock2.h> // NOLINT |
| 18 | #endif |
| 19 | |
Mirko Bonadei | e5f4c6b | 2021-01-15 09:41:01 | [diff] [blame] | 20 | #include <vector> |
| 21 | |
Artem Titov | d15a575 | 2021-02-10 13:31:24 | [diff] [blame] | 22 | #include "api/sequence_checker.h" |
Artem Titov | c374d11 | 2022-06-16 19:27:45 | [diff] [blame] | 23 | #include "api/task_queue/pending_task_safety_flag.h" |
Mirko Bonadei | e5f4c6b | 2021-01-15 09:41:01 | [diff] [blame] | 24 | #include "rtc_base/async_resolver_interface.h" |
Markus Handell | 1366b0f | 2021-04-21 08:22:34 | [diff] [blame] | 25 | #include "rtc_base/event.h" |
Mirko Bonadei | e5f4c6b | 2021-01-15 09:41:01 | [diff] [blame] | 26 | #include "rtc_base/ip_address.h" |
| 27 | #include "rtc_base/socket_address.h" |
Mirko Bonadei | e5f4c6b | 2021-01-15 09:41:01 | [diff] [blame] | 28 | #include "rtc_base/system/no_unique_address.h" |
| 29 | #include "rtc_base/system/rtc_export.h" |
Mirko Bonadei | e5f4c6b | 2021-01-15 09:41:01 | [diff] [blame] | 30 | #include "rtc_base/thread.h" |
| 31 | #include "rtc_base/thread_annotations.h" |
| 32 | |
| 33 | namespace rtc { |
| 34 | |
| 35 | // AsyncResolver will perform async DNS resolution, signaling the result on |
| 36 | // the SignalDone from AsyncResolverInterface when the operation completes. |
| 37 | // |
| 38 | // This class is thread-compatible, and all methods and destruction needs to |
| 39 | // happen from the same rtc::Thread, except for Destroy which is allowed to |
| 40 | // happen on another context provided it's not happening concurrently to another |
| 41 | // public API call, and is the last access to the object. |
| 42 | class RTC_EXPORT AsyncResolver : public AsyncResolverInterface { |
| 43 | public: |
| 44 | AsyncResolver(); |
| 45 | ~AsyncResolver() override; |
| 46 | |
| 47 | void Start(const SocketAddress& addr) override; |
Sameer Vijaykar | b787e26 | 2022-08-11 09:52:57 | [diff] [blame] | 48 | void Start(const SocketAddress& addr, int family) override; |
Mirko Bonadei | e5f4c6b | 2021-01-15 09:41:01 | [diff] [blame] | 49 | bool GetResolvedAddress(int family, SocketAddress* addr) const override; |
| 50 | int GetError() const override; |
| 51 | void Destroy(bool wait) override; |
| 52 | |
| 53 | const std::vector<IPAddress>& addresses() const; |
| 54 | |
| 55 | private: |
Markus Handell | 1366b0f | 2021-04-21 08:22:34 | [diff] [blame] | 56 | // Fwd decl. |
| 57 | struct State; |
| 58 | |
Mirko Bonadei | e5f4c6b | 2021-01-15 09:41:01 | [diff] [blame] | 59 | void ResolveDone(std::vector<IPAddress> addresses, int error) |
| 60 | RTC_EXCLUSIVE_LOCKS_REQUIRED(sequence_checker_); |
| 61 | void MaybeSelfDestruct(); |
| 62 | |
| 63 | SocketAddress addr_ RTC_GUARDED_BY(sequence_checker_); |
| 64 | std::vector<IPAddress> addresses_ RTC_GUARDED_BY(sequence_checker_); |
| 65 | int error_ RTC_GUARDED_BY(sequence_checker_); |
Mirko Bonadei | e5f4c6b | 2021-01-15 09:41:01 | [diff] [blame] | 66 | bool recursion_check_ = |
| 67 | false; // Protects against SignalDone calling into Destroy. |
| 68 | bool destroy_called_ = false; |
Markus Handell | 1366b0f | 2021-04-21 08:22:34 | [diff] [blame] | 69 | scoped_refptr<State> state_; |
Mirko Bonadei | e5f4c6b | 2021-01-15 09:41:01 | [diff] [blame] | 70 | RTC_NO_UNIQUE_ADDRESS webrtc::SequenceChecker sequence_checker_; |
| 71 | }; |
| 72 | |
| 73 | } // namespace rtc |
Mirko Bonadei | 8592111 | 2020-12-16 13:08:43 | [diff] [blame] | 74 | |
| 75 | #endif // RTC_BASE_ASYNC_RESOLVER_H_ |