henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [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 | |
Steve Anton | 10542f2 | 2019-01-11 17:11:00 | [diff] [blame] | 11 | #ifndef RTC_BASE_NET_HELPERS_H_ |
| 12 | #define RTC_BASE_NET_HELPERS_H_ |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 13 | |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 14 | #if defined(WEBRTC_POSIX) |
Yves Gerey | 988cc08 | 2018-10-23 10:03:01 | [diff] [blame] | 15 | #include <sys/socket.h> |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 16 | #elif WEBRTC_WIN |
| 17 | #include <winsock2.h> // NOLINT |
| 18 | #endif |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 19 | |
Yves Gerey | 2e00abc | 2018-10-05 13:39:24 | [diff] [blame] | 20 | #include <vector> |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 21 | |
Steve Anton | 10542f2 | 2019-01-11 17:11:00 | [diff] [blame] | 22 | #include "rtc_base/async_resolver_interface.h" |
| 23 | #include "rtc_base/ip_address.h" |
Steve Anton | 10542f2 | 2019-01-11 17:11:00 | [diff] [blame] | 24 | #include "rtc_base/socket_address.h" |
Markus Handell | fbf4ad2 | 2020-05-26 16:43:55 | [diff] [blame] | 25 | #include "rtc_base/synchronization/sequence_checker.h" |
Mirko Bonadei | 35214fc | 2019-09-23 12:54:28 | [diff] [blame] | 26 | #include "rtc_base/system/rtc_export.h" |
Markus Handell | fbf4ad2 | 2020-05-26 16:43:55 | [diff] [blame] | 27 | #include "rtc_base/task_utils/pending_task_safety_flag.h" |
| 28 | #include "rtc_base/thread.h" |
| 29 | #include "rtc_base/thread_annotations.h" |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 30 | |
| 31 | namespace rtc { |
| 32 | |
deadbeef | 8290ddf | 2017-07-11 23:56:05 | [diff] [blame] | 33 | // AsyncResolver will perform async DNS resolution, signaling the result on |
| 34 | // the SignalDone from AsyncResolverInterface when the operation completes. |
Markus Handell | fbf4ad2 | 2020-05-26 16:43:55 | [diff] [blame] | 35 | // |
| 36 | // This class is thread-compatible, and all methods and destruction needs to |
| 37 | // happen from the same rtc::Thread, except for Destroy which is allowed to |
| 38 | // happen on another context provided it's not happening concurrently to another |
| 39 | // public API call, and is the last access to the object. |
| 40 | class RTC_EXPORT AsyncResolver : public AsyncResolverInterface { |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 41 | public: |
| 42 | AsyncResolver(); |
| 43 | ~AsyncResolver() override; |
| 44 | |
| 45 | void Start(const SocketAddress& addr) override; |
| 46 | bool GetResolvedAddress(int family, SocketAddress* addr) const override; |
| 47 | int GetError() const override; |
| 48 | void Destroy(bool wait) override; |
| 49 | |
Markus Handell | fbf4ad2 | 2020-05-26 16:43:55 | [diff] [blame] | 50 | const std::vector<IPAddress>& addresses() const; |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 51 | |
| 52 | private: |
Markus Handell | fbf4ad2 | 2020-05-26 16:43:55 | [diff] [blame] | 53 | void ResolveDone(std::vector<IPAddress> addresses, int error) |
| 54 | RTC_EXCLUSIVE_LOCKS_REQUIRED(sequence_checker_); |
| 55 | void MaybeSelfDestruct(); |
| 56 | |
| 57 | SocketAddress addr_ RTC_GUARDED_BY(sequence_checker_); |
| 58 | std::vector<IPAddress> addresses_ RTC_GUARDED_BY(sequence_checker_); |
| 59 | int error_ RTC_GUARDED_BY(sequence_checker_); |
| 60 | webrtc::ScopedTaskSafety safety_ RTC_GUARDED_BY(sequence_checker_); |
| 61 | std::unique_ptr<Thread> popup_thread_ RTC_GUARDED_BY(sequence_checker_); |
| 62 | bool recursion_check_ = |
| 63 | false; // Protects against SignalDone calling into Destroy. |
| 64 | bool destroy_called_ = false; |
Mirko Bonadei | 0abd518 | 2020-10-06 19:39:59 | [diff] [blame] | 65 | webrtc::SequenceChecker sequence_checker_; |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 66 | }; |
| 67 | |
| 68 | // rtc namespaced wrappers for inet_ntop and inet_pton so we can avoid |
| 69 | // the windows-native versions of these. |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 70 | const char* inet_ntop(int af, const void* src, char* dst, socklen_t size); |
| 71 | int inet_pton(int af, const char* src, void* dst); |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 72 | |
| 73 | bool HasIPv4Enabled(); |
| 74 | bool HasIPv6Enabled(); |
| 75 | } // namespace rtc |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 76 | |
Steve Anton | 10542f2 | 2019-01-11 17:11:00 | [diff] [blame] | 77 | #endif // RTC_BASE_NET_HELPERS_H_ |