blob: 172a2224568fdebeef87deab364531a9f52fde8d [file] [log] [blame]
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001/*
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 Anton10542f22019-01-11 09:11:00 -080011#ifndef RTC_BASE_NET_HELPERS_H_
12#define RTC_BASE_NET_HELPERS_H_
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000013
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020014#if defined(WEBRTC_POSIX)
Yves Gerey988cc082018-10-23 12:03:01 +020015#include <sys/socket.h>
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020016#elif WEBRTC_WIN
17#include <winsock2.h> // NOLINT
18#endif
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000019
Mirko Bonadei7acc2d92021-01-14 21:03:55 +000020#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/no_unique_address.h"
27#include "rtc_base/system/rtc_export.h"
28#include "rtc_base/task_utils/pending_task_safety_flag.h"
29#include "rtc_base/thread.h"
30#include "rtc_base/thread_annotations.h"
31
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020032namespace rtc {
33
Mirko Bonadei7acc2d92021-01-14 21:03:55 +000034// AsyncResolver will perform async DNS resolution, signaling the result on
35// the SignalDone from AsyncResolverInterface when the operation completes.
36//
37// This class is thread-compatible, and all methods and destruction needs to
38// happen from the same rtc::Thread, except for Destroy which is allowed to
39// happen on another context provided it's not happening concurrently to another
40// public API call, and is the last access to the object.
41class RTC_EXPORT AsyncResolver : public AsyncResolverInterface {
42 public:
43 AsyncResolver();
44 ~AsyncResolver() override;
45
46 void Start(const SocketAddress& addr) override;
47 bool GetResolvedAddress(int family, SocketAddress* addr) const override;
48 int GetError() const override;
49 void Destroy(bool wait) override;
50
51 const std::vector<IPAddress>& addresses() const;
52
53 private:
54 void ResolveDone(std::vector<IPAddress> addresses, int error)
55 RTC_EXCLUSIVE_LOCKS_REQUIRED(sequence_checker_);
56 void MaybeSelfDestruct();
57
58 SocketAddress addr_ RTC_GUARDED_BY(sequence_checker_);
59 std::vector<IPAddress> addresses_ RTC_GUARDED_BY(sequence_checker_);
60 int error_ RTC_GUARDED_BY(sequence_checker_);
61 webrtc::ScopedTaskSafety safety_ RTC_GUARDED_BY(sequence_checker_);
62 std::unique_ptr<Thread> popup_thread_ RTC_GUARDED_BY(sequence_checker_);
63 bool recursion_check_ =
64 false; // Protects against SignalDone calling into Destroy.
65 bool destroy_called_ = false;
66 RTC_NO_UNIQUE_ADDRESS webrtc::SequenceChecker sequence_checker_;
67};
68
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020069// rtc namespaced wrappers for inet_ntop and inet_pton so we can avoid
70// the windows-native versions of these.
Yves Gerey665174f2018-06-19 15:03:05 +020071const char* inet_ntop(int af, const void* src, char* dst, socklen_t size);
72int inet_pton(int af, const char* src, void* dst);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020073
74bool HasIPv4Enabled();
75bool HasIPv6Enabled();
76} // namespace rtc
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000077
Steve Anton10542f22019-01-11 09:11:00 -080078#endif // RTC_BASE_NET_HELPERS_H_