Add a new API to DNS resolution
This API should allow existing factories to be used unmodified, but offers a new API that documents ownership better and does not use sigslot. Bug: webrtc:12598 Change-Id: I0f68371059cd4a18ab07b87fc0e7526dcc0ac669 Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/212609 Reviewed-by: Tommi <tommi@webrtc.org> Commit-Queue: Harald Alvestrand <hta@webrtc.org> Cr-Commit-Position: refs/heads/master@{#33553}
This commit is contained in:

committed by
Commit Bot

parent
ac732f6a13
commit
e6e2f280ff
@ -249,12 +249,14 @@ rtc_library("rtc_error") {
|
||||
rtc_source_set("packet_socket_factory") {
|
||||
visibility = [ "*" ]
|
||||
sources = [
|
||||
"async_dns_resolver.h",
|
||||
"async_resolver_factory.h",
|
||||
"packet_socket_factory.h",
|
||||
]
|
||||
deps = [
|
||||
"../rtc_base:async_resolver_interface",
|
||||
"../rtc_base:rtc_base",
|
||||
"../rtc_base:socket_address",
|
||||
"../rtc_base/system:rtc_export",
|
||||
]
|
||||
}
|
||||
|
4
api/DEPS
4
api/DEPS
@ -63,6 +63,10 @@ specific_include_rules = {
|
||||
"+rtc_base/async_resolver_interface.h",
|
||||
],
|
||||
|
||||
"async_dns_resolver\.h": [
|
||||
"+rtc_base/socket_address.h",
|
||||
],
|
||||
|
||||
"candidate\.h": [
|
||||
"+rtc_base/network_constants.h",
|
||||
"+rtc_base/socket_address.h",
|
||||
|
85
api/async_dns_resolver.h
Normal file
85
api/async_dns_resolver.h
Normal file
@ -0,0 +1,85 @@
|
||||
/*
|
||||
* Copyright 2021 The WebRTC Project Authors. All rights reserved.
|
||||
*
|
||||
* Use of this source code is governed by a BSD-style license
|
||||
* that can be found in the LICENSE file in the root of the source
|
||||
* tree. An additional intellectual property rights grant can be found
|
||||
* in the file PATENTS. All contributing project authors may
|
||||
* be found in the AUTHORS file in the root of the source tree.
|
||||
*/
|
||||
|
||||
#ifndef API_ASYNC_DNS_RESOLVER_H_
|
||||
#define API_ASYNC_DNS_RESOLVER_H_
|
||||
|
||||
#include <memory>
|
||||
|
||||
#include "rtc_base/socket_address.h"
|
||||
#include "rtc_base/system/rtc_export.h"
|
||||
|
||||
namespace webrtc {
|
||||
|
||||
// This interface defines the methods to resolve a hostname asynchronously.
|
||||
// The AsyncDnsResolverInterface class encapsulates a single name query.
|
||||
//
|
||||
// Usage:
|
||||
// std::unique_ptr<AsyncDnsResolverInterface> resolver =
|
||||
// factory->Create(address-to-be-resolved, [r = resolver.get()]() {
|
||||
// if (r->result.GetResolvedAddress(AF_INET, &addr) {
|
||||
// // success
|
||||
// } else {
|
||||
// // failure
|
||||
// error = r->result().GetError();
|
||||
// }
|
||||
// // Release resolver.
|
||||
// resolver_list.erase(std::remove_if(resolver_list.begin(),
|
||||
// resolver_list.end(),
|
||||
// [](refptr) { refptr.get() == r; });
|
||||
// });
|
||||
// resolver_list.push_back(std::move(resolver));
|
||||
|
||||
class AsyncDnsResolverResult {
|
||||
public:
|
||||
virtual ~AsyncDnsResolverResult() = default;
|
||||
// Returns true iff the address from |Start| was successfully resolved.
|
||||
// If the address was successfully resolved, sets |addr| to a copy of the
|
||||
// address from |Start| with the IP address set to the top most resolved
|
||||
// address of |family| (|addr| will have both hostname and the resolved ip).
|
||||
virtual bool GetResolvedAddress(int family,
|
||||
rtc::SocketAddress* addr) const = 0;
|
||||
// Returns error from resolver.
|
||||
virtual int GetError() const = 0;
|
||||
};
|
||||
|
||||
class RTC_EXPORT AsyncDnsResolverInterface {
|
||||
public:
|
||||
virtual ~AsyncDnsResolverInterface() = default;
|
||||
|
||||
// Start address resolution of the hostname in |addr|.
|
||||
virtual void Start(const rtc::SocketAddress& addr,
|
||||
std::function<void()> callback) = 0;
|
||||
virtual const AsyncDnsResolverResult& result() const = 0;
|
||||
};
|
||||
|
||||
// An abstract factory for creating AsyncDnsResolverInterfaces. This allows
|
||||
// client applications to provide WebRTC with their own mechanism for
|
||||
// performing DNS resolution.
|
||||
class AsyncDnsResolverFactoryInterface {
|
||||
public:
|
||||
virtual ~AsyncDnsResolverFactoryInterface() = default;
|
||||
|
||||
// Creates an AsyncDnsResolver and starts resolving the name. The callback
|
||||
// will be called when resolution is finished.
|
||||
// The callback will be called on the thread that the caller runs on.
|
||||
virtual std::unique_ptr<webrtc::AsyncDnsResolverInterface> CreateAndResolve(
|
||||
const rtc::SocketAddress& addr,
|
||||
std::function<void()> callback) = 0;
|
||||
// Creates an AsyncDnsResolver and does not start it.
|
||||
// For backwards compatibility, will be deprecated and removed.
|
||||
// One has to do a separate Start() call on the
|
||||
// resolver to start name resolution.
|
||||
virtual std::unique_ptr<webrtc::AsyncDnsResolverInterface> Create() = 0;
|
||||
};
|
||||
|
||||
} // namespace webrtc
|
||||
|
||||
#endif // API_ASYNC_DNS_RESOLVER_H_
|
@ -10,6 +10,11 @@
|
||||
|
||||
#include "p2p/base/basic_async_resolver_factory.h"
|
||||
|
||||
#include <memory>
|
||||
#include <utility>
|
||||
|
||||
#include "absl/memory/memory.h"
|
||||
#include "api/async_dns_resolver.h"
|
||||
#include "rtc_base/async_resolver.h"
|
||||
|
||||
namespace webrtc {
|
||||
@ -18,4 +23,93 @@ rtc::AsyncResolverInterface* BasicAsyncResolverFactory::Create() {
|
||||
return new rtc::AsyncResolver();
|
||||
}
|
||||
|
||||
class WrappingAsyncDnsResolver;
|
||||
|
||||
class WrappingAsyncDnsResolverResult : public AsyncDnsResolverResult {
|
||||
public:
|
||||
explicit WrappingAsyncDnsResolverResult(WrappingAsyncDnsResolver* owner)
|
||||
: owner_(owner) {}
|
||||
~WrappingAsyncDnsResolverResult() {}
|
||||
|
||||
// Note: Inline declaration not possible, since it refers to
|
||||
// WrappingAsyncDnsResolver.
|
||||
bool GetResolvedAddress(int family, rtc::SocketAddress* addr) const override;
|
||||
int GetError() const override;
|
||||
|
||||
private:
|
||||
WrappingAsyncDnsResolver* const owner_;
|
||||
};
|
||||
|
||||
class WrappingAsyncDnsResolver : public AsyncDnsResolverInterface,
|
||||
public sigslot::has_slots<> {
|
||||
public:
|
||||
explicit WrappingAsyncDnsResolver(rtc::AsyncResolverInterface* wrapped)
|
||||
: wrapped_(absl::WrapUnique(wrapped)), result_(this) {}
|
||||
|
||||
~WrappingAsyncDnsResolver() override { wrapped_.release()->Destroy(false); }
|
||||
|
||||
void Start(const rtc::SocketAddress& addr,
|
||||
std::function<void()> callback) override {
|
||||
RTC_DCHECK(state_ == State::kNotStarted);
|
||||
state_ = State::kStarted;
|
||||
callback_ = callback;
|
||||
wrapped_->SignalDone.connect(this,
|
||||
&WrappingAsyncDnsResolver::OnResolveResult);
|
||||
wrapped_->Start(addr);
|
||||
}
|
||||
|
||||
const AsyncDnsResolverResult& result() const override {
|
||||
RTC_DCHECK(state_ == State::kResolved);
|
||||
return result_;
|
||||
}
|
||||
|
||||
// For use by WrappingAsyncDnsResolverResult
|
||||
rtc::AsyncResolverInterface* wrapped() const { return wrapped_.get(); }
|
||||
|
||||
private:
|
||||
enum class State { kNotStarted, kStarted, kResolved };
|
||||
|
||||
void OnResolveResult(rtc::AsyncResolverInterface* ref) {
|
||||
RTC_DCHECK(state_ == State::kStarted);
|
||||
RTC_DCHECK_EQ(ref, wrapped_.get());
|
||||
state_ = State::kResolved;
|
||||
callback_();
|
||||
}
|
||||
|
||||
std::function<void()> callback_;
|
||||
std::unique_ptr<rtc::AsyncResolverInterface> wrapped_;
|
||||
State state_ = State::kNotStarted;
|
||||
WrappingAsyncDnsResolverResult result_;
|
||||
};
|
||||
|
||||
bool WrappingAsyncDnsResolverResult::GetResolvedAddress(
|
||||
int family,
|
||||
rtc::SocketAddress* addr) const {
|
||||
if (!owner_->wrapped()) {
|
||||
return false;
|
||||
}
|
||||
return owner_->wrapped()->GetResolvedAddress(family, addr);
|
||||
}
|
||||
|
||||
int WrappingAsyncDnsResolverResult::GetError() const {
|
||||
if (!owner_->wrapped()) {
|
||||
return -1; // FIXME: Find a code that makes sense.
|
||||
}
|
||||
return owner_->wrapped()->GetError();
|
||||
}
|
||||
|
||||
std::unique_ptr<webrtc::AsyncDnsResolverInterface>
|
||||
WrappingAsyncDnsResolverFactory::Create() {
|
||||
return std::make_unique<WrappingAsyncDnsResolver>(wrapped_factory_->Create());
|
||||
}
|
||||
|
||||
std::unique_ptr<webrtc::AsyncDnsResolverInterface>
|
||||
WrappingAsyncDnsResolverFactory::CreateAndResolve(
|
||||
const rtc::SocketAddress& addr,
|
||||
std::function<void()> callback) {
|
||||
std::unique_ptr<webrtc::AsyncDnsResolverInterface> resolver = Create();
|
||||
resolver->Start(addr, callback);
|
||||
return resolver;
|
||||
}
|
||||
|
||||
} // namespace webrtc
|
||||
|
@ -11,16 +11,40 @@
|
||||
#ifndef P2P_BASE_BASIC_ASYNC_RESOLVER_FACTORY_H_
|
||||
#define P2P_BASE_BASIC_ASYNC_RESOLVER_FACTORY_H_
|
||||
|
||||
#include <functional>
|
||||
#include <memory>
|
||||
#include <utility>
|
||||
|
||||
#include "api/async_dns_resolver.h"
|
||||
#include "api/async_resolver_factory.h"
|
||||
#include "rtc_base/async_resolver_interface.h"
|
||||
|
||||
namespace webrtc {
|
||||
|
||||
class BasicAsyncResolverFactory : public AsyncResolverFactory {
|
||||
class BasicAsyncResolverFactory final : public AsyncResolverFactory {
|
||||
public:
|
||||
rtc::AsyncResolverInterface* Create() override;
|
||||
};
|
||||
|
||||
// This class wraps a factory using the older webrtc::AsyncResolverFactory API,
|
||||
// and produces webrtc::AsyncDnsResolver objects that contain an
|
||||
// rtc::AsyncResolver object.
|
||||
class WrappingAsyncDnsResolverFactory final
|
||||
: public AsyncDnsResolverFactoryInterface {
|
||||
public:
|
||||
WrappingAsyncDnsResolverFactory(
|
||||
std::unique_ptr<AsyncResolverFactory> wrapped_factory)
|
||||
: wrapped_factory_(std::move(wrapped_factory)) {}
|
||||
std::unique_ptr<webrtc::AsyncDnsResolverInterface> CreateAndResolve(
|
||||
const rtc::SocketAddress& addr,
|
||||
std::function<void()> callback) override;
|
||||
|
||||
std::unique_ptr<webrtc::AsyncDnsResolverInterface> Create() override;
|
||||
|
||||
private:
|
||||
const std::unique_ptr<AsyncResolverFactory> wrapped_factory_;
|
||||
};
|
||||
|
||||
} // namespace webrtc
|
||||
|
||||
#endif // P2P_BASE_BASIC_ASYNC_RESOLVER_FACTORY_H_
|
||||
|
@ -47,4 +47,18 @@ TEST_F(BasicAsyncResolverFactoryTest, TestCreate) {
|
||||
TestCreate();
|
||||
}
|
||||
|
||||
TEST(WrappingAsyncDnsResolverFactoryTest, TestCreate) {
|
||||
WrappingAsyncDnsResolverFactory factory(
|
||||
std::make_unique<BasicAsyncResolverFactory>());
|
||||
|
||||
std::unique_ptr<AsyncDnsResolverInterface> resolver(factory.Create());
|
||||
ASSERT_TRUE(resolver);
|
||||
|
||||
bool address_resolved = false;
|
||||
rtc::SocketAddress address("", 0);
|
||||
resolver->Start(address, [&address_resolved]() { address_resolved = true; });
|
||||
ASSERT_TRUE_WAIT(address_resolved, 10000 /*ms*/);
|
||||
resolver.reset();
|
||||
}
|
||||
|
||||
} // namespace webrtc
|
||||
|
Reference in New Issue
Block a user