blob: 99d0602943bab800162748228888b46aad856daf [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
Henrik Kjellanderc0362762017-06-29 08:03:04 +020011#ifndef WEBRTC_RTC_BASE_NETHELPERS_H_
12#define WEBRTC_RTC_BASE_NETHELPERS_H_
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000013
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020014#if defined(WEBRTC_POSIX)
15#include <netdb.h>
16#include <stddef.h>
17#elif WEBRTC_WIN
18#include <winsock2.h> // NOLINT
19#endif
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000020
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020021#include <list>
nissebc8feda2017-06-29 06:21:20 -070022#include <memory>
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020023
Henrik Kjellander00725112017-06-30 15:14:45 +020024#include "webrtc/base/asyncresolverinterface.h"
25#include "webrtc/base/refcount.h"
26#include "webrtc/base/scoped_ref_ptr.h"
27#include "webrtc/base/sigslot.h"
28#include "webrtc/base/socketaddress.h"
29#include "webrtc/base/thread_checker.h"
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020030
31namespace rtc {
32
nissebc8feda2017-06-29 06:21:20 -070033class Thread;
34class TaskQueue;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020035
nissebc8feda2017-06-29 06:21:20 -070036// AsyncResolver will perform async DNS resolution, signaling the result on the
37// SignalDone from AsyncResolverInterface when the operation completes.
38// SignalDone is fired on the same thread on which the AsyncResolver was
39// constructed.
40class AsyncResolver : public AsyncResolverInterface {
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020041 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 { return addresses_; }
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020051
52 private:
nissebc8feda2017-06-29 06:21:20 -070053 void ResolveDone(int error, std::vector<IPAddress> addresses);
54
55 class Trampoline : public RefCountInterface {
56 public:
57 Trampoline(AsyncResolver* resolver) : resolver(resolver) {}
58 // Points back to the resolver, as long as it is alive. Cleared
59 // by the AsyncResolver destructor.
60 AsyncResolver* resolver;
61 };
62
63 // |state_| is non-null while resolution is pending, i.e., set
64 // non-null by Start() and cleared by ResolveDone(). The destructor
65 // clears state_->resolver (assuming |state_| is non-null), to
66 // indicate that the resolver can no longer be accessed.
67 scoped_refptr<Trampoline> state_ ACCESS_ON(construction_thread_);
68
69 Thread* const construction_thread_;
70 // Set to true when Destroy() can't delete the object immediately.
71 // Indicate that the ResolveDone method is now responsible for
72 // deletion. method should delete the object.
73 bool destroyed_ = false;
74 // Queue used only for a single task.
75 std::unique_ptr<TaskQueue> resolver_queue_;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020076 SocketAddress addr_;
77 std::vector<IPAddress> addresses_;
nissebc8feda2017-06-29 06:21:20 -070078 int error_ = -1;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020079};
80
81// rtc namespaced wrappers for inet_ntop and inet_pton so we can avoid
82// the windows-native versions of these.
83const char* inet_ntop(int af, const void *src, char* dst, socklen_t size);
84int inet_pton(int af, const char* src, void *dst);
85
86bool HasIPv4Enabled();
87bool HasIPv6Enabled();
88} // namespace rtc
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000089
Henrik Kjellanderc0362762017-06-29 08:03:04 +020090#endif // WEBRTC_RTC_BASE_NETHELPERS_H_