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_NET_HELPERS_H_
12 #define RTC_BASE_NET_HELPERS_H_
13 
14 #if defined(WEBRTC_POSIX)
15 #include <sys/socket.h>
16 #elif WEBRTC_WIN
17 #include <winsock2.h>  // NOLINT
18 #endif
19 
20 #include <vector>
21 
22 #include "rtc_base/async_resolver_interface.h"
23 #include "rtc_base/ip_address.h"
24 #include "rtc_base/socket_address.h"
25 #include "rtc_base/synchronization/sequence_checker.h"
26 #include "rtc_base/system/rtc_export.h"
27 #include "rtc_base/task_utils/pending_task_safety_flag.h"
28 #include "rtc_base/thread.h"
29 #include "rtc_base/thread_annotations.h"
30 
31 namespace rtc {
32 
33 // AsyncResolver will perform async DNS resolution, signaling the result on
34 // the SignalDone from AsyncResolverInterface when the operation completes.
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 {
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 
50   const std::vector<IPAddress>& addresses() const;
51 
52  private:
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;
65   webrtc::SequenceChecker sequence_checker_;
66 };
67 
68 // rtc namespaced wrappers for inet_ntop and inet_pton so we can avoid
69 // the windows-native versions of these.
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);
72 
73 bool HasIPv4Enabled();
74 bool HasIPv6Enabled();
75 }  // namespace rtc
76 
77 #endif  // RTC_BASE_NET_HELPERS_H_
78