blob: c6aa4be5b2038c26b869a84da08b0e33bfb791b6 [file] [log] [blame]
henrike@webrtc.orgf0488722014-05-13 18:00:261/*
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 17:11:0011#ifndef RTC_BASE_NET_HELPERS_H_
12#define RTC_BASE_NET_HELPERS_H_
henrike@webrtc.orgf0488722014-05-13 18:00:2613
Henrik Kjellanderec78f1c2017-06-29 05:52:5014#if defined(WEBRTC_POSIX)
Yves Gerey988cc082018-10-23 10:03:0115#include <sys/socket.h>
Henrik Kjellanderec78f1c2017-06-29 05:52:5016#elif WEBRTC_WIN
17#include <winsock2.h> // NOLINT
18#endif
henrike@webrtc.orgf0488722014-05-13 18:00:2619
Yves Gerey2e00abc2018-10-05 13:39:2420#include <vector>
Henrik Kjellanderec78f1c2017-06-29 05:52:5021
Steve Anton10542f22019-01-11 17:11:0022#include "rtc_base/async_resolver_interface.h"
23#include "rtc_base/ip_address.h"
Steve Anton10542f22019-01-11 17:11:0024#include "rtc_base/socket_address.h"
Markus Handellfbf4ad22020-05-26 16:43:5525#include "rtc_base/synchronization/sequence_checker.h"
Mirko Bonadei35214fc2019-09-23 12:54:2826#include "rtc_base/system/rtc_export.h"
Markus Handellfbf4ad22020-05-26 16:43:5527#include "rtc_base/task_utils/pending_task_safety_flag.h"
28#include "rtc_base/thread.h"
29#include "rtc_base/thread_annotations.h"
Henrik Kjellanderec78f1c2017-06-29 05:52:5030
31namespace rtc {
32
deadbeef8290ddf2017-07-11 23:56:0533// AsyncResolver will perform async DNS resolution, signaling the result on
34// the SignalDone from AsyncResolverInterface when the operation completes.
Markus Handellfbf4ad22020-05-26 16:43:5535//
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.
40class RTC_EXPORT AsyncResolver : public AsyncResolverInterface {
Henrik Kjellanderec78f1c2017-06-29 05:52:5041 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 Handellfbf4ad22020-05-26 16:43:5550 const std::vector<IPAddress>& addresses() const;
Henrik Kjellanderec78f1c2017-06-29 05:52:5051
52 private:
Markus Handellfbf4ad22020-05-26 16:43:5553 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 Bonadei0abd5182020-10-06 19:39:5965 webrtc::SequenceChecker sequence_checker_;
Henrik Kjellanderec78f1c2017-06-29 05:52:5066};
67
68// rtc namespaced wrappers for inet_ntop and inet_pton so we can avoid
69// the windows-native versions of these.
Yves Gerey665174f2018-06-19 13:03:0570const char* inet_ntop(int af, const void* src, char* dst, socklen_t size);
71int inet_pton(int af, const char* src, void* dst);
Henrik Kjellanderec78f1c2017-06-29 05:52:5072
73bool HasIPv4Enabled();
74bool HasIPv6Enabled();
75} // namespace rtc
henrike@webrtc.orgf0488722014-05-13 18:00:2676
Steve Anton10542f22019-01-11 17:11:0077#endif // RTC_BASE_NET_HELPERS_H_