blob: 8257f46f5c9dbff3ae3ad74044d461b33baaf458 [file] [log] [blame]
Mirko Bonadei85921112020-12-16 14:08:43 +01001/*
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 Bonadeie5f4c6b2021-01-15 10:41:01 +010014#if defined(WEBRTC_POSIX)
15#include <sys/socket.h>
16#elif WEBRTC_WIN
17#include <winsock2.h> // NOLINT
18#endif
19
Mirko Bonadeie5f4c6b2021-01-15 10:41:01 +010020#include <vector>
21
Artem Titovd15a5752021-02-10 14:31:24 +010022#include "api/sequence_checker.h"
Mirko Bonadeie5f4c6b2021-01-15 10:41:01 +010023#include "rtc_base/async_resolver_interface.h"
Markus Handell1366b0f2021-04-21 10:22:34 +020024#include "rtc_base/event.h"
Mirko Bonadeie5f4c6b2021-01-15 10:41:01 +010025#include "rtc_base/ip_address.h"
26#include "rtc_base/socket_address.h"
Mirko Bonadeie5f4c6b2021-01-15 10:41:01 +010027#include "rtc_base/system/no_unique_address.h"
28#include "rtc_base/system/rtc_export.h"
29#include "rtc_base/task_utils/pending_task_safety_flag.h"
30#include "rtc_base/thread.h"
31#include "rtc_base/thread_annotations.h"
32
33namespace 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.
42class RTC_EXPORT AsyncResolver : public AsyncResolverInterface {
43 public:
44 AsyncResolver();
45 ~AsyncResolver() override;
46
47 void Start(const SocketAddress& addr) override;
48 bool GetResolvedAddress(int family, SocketAddress* addr) const override;
49 int GetError() const override;
50 void Destroy(bool wait) override;
51
52 const std::vector<IPAddress>& addresses() const;
53
54 private:
Markus Handell1366b0f2021-04-21 10:22:34 +020055 // Fwd decl.
56 struct State;
57
Mirko Bonadeie5f4c6b2021-01-15 10:41:01 +010058 void ResolveDone(std::vector<IPAddress> addresses, int error)
59 RTC_EXCLUSIVE_LOCKS_REQUIRED(sequence_checker_);
60 void MaybeSelfDestruct();
61
62 SocketAddress addr_ RTC_GUARDED_BY(sequence_checker_);
63 std::vector<IPAddress> addresses_ RTC_GUARDED_BY(sequence_checker_);
64 int error_ RTC_GUARDED_BY(sequence_checker_);
Mirko Bonadeie5f4c6b2021-01-15 10:41:01 +010065 bool recursion_check_ =
66 false; // Protects against SignalDone calling into Destroy.
67 bool destroy_called_ = false;
Markus Handell1366b0f2021-04-21 10:22:34 +020068 scoped_refptr<State> state_;
Mirko Bonadeie5f4c6b2021-01-15 10:41:01 +010069 RTC_NO_UNIQUE_ADDRESS webrtc::SequenceChecker sequence_checker_;
70};
71
72} // namespace rtc
Mirko Bonadei85921112020-12-16 14:08:43 +010073
74#endif // RTC_BASE_ASYNC_RESOLVER_H_