From c9f43f8f81a265462be6fbfca2f2463506e19df2 Mon Sep 17 00:00:00 2001 From: Harald Alvestrand Date: Tue, 5 Oct 2021 22:13:14 +0000 Subject: [PATCH] Use AsyncDnsResolver in TurnPort class Bug: webrtc:12598 Change-Id: Ie53c27d3a614521f4a8b665fd321b1db53dc70b1 Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/234261 Reviewed-by: Markus Handell Commit-Queue: Harald Alvestrand Cr-Commit-Position: refs/heads/main@{#35148} --- p2p/base/turn_port.cc | 72 ++++++++++++++++++++----------------------- p2p/base/turn_port.h | 4 +-- 2 files changed, 35 insertions(+), 41 deletions(-) diff --git a/p2p/base/turn_port.cc b/p2p/base/turn_port.cc index 33cef13c6c..b7d37b31b1 100644 --- a/p2p/base/turn_port.cc +++ b/p2p/base/turn_port.cc @@ -230,7 +230,6 @@ TurnPort::TurnPort(rtc::Thread* thread, tls_cert_verifier_(nullptr), credentials_(credentials), socket_(socket), - resolver_(NULL), error_(0), stun_dscp_value_(rtc::DSCP_NO_CHANGE), request_manager_(thread), @@ -272,7 +271,6 @@ TurnPort::TurnPort(rtc::Thread* thread, tls_cert_verifier_(tls_cert_verifier), credentials_(credentials), socket_(NULL), - resolver_(NULL), error_(0), stun_dscp_value_(rtc::DSCP_NO_CHANGE), request_manager_(thread), @@ -297,9 +295,6 @@ TurnPort::~TurnPort() { while (!entries_.empty()) { DestroyEntry(entries_.front()); } - if (resolver_) { - resolver_->Destroy(false); - } if (!SharedSocket()) { delete socket_; } @@ -797,44 +792,43 @@ void TurnPort::ResolveTurnAddress(const rtc::SocketAddress& address) { RTC_LOG(LS_INFO) << ToString() << ": Starting TURN host lookup for " << address.ToSensitiveString(); - resolver_ = socket_factory()->CreateAsyncResolver(); - resolver_->SignalDone.connect(this, &TurnPort::OnResolveResult); - resolver_->Start(address); -} + resolver_ = socket_factory()->CreateAsyncDnsResolver(); + resolver_->Start(address, [this] { + // If DNS resolve is failed when trying to connect to the server using TCP, + // one of the reason could be due to DNS queries blocked by firewall. + // In such cases we will try to connect to the server with hostname, + // assuming socket layer will resolve the hostname through a HTTP proxy (if + // any). + auto& result = resolver_->result(); + if (result.GetError() != 0 && (server_address_.proto == PROTO_TCP || + server_address_.proto == PROTO_TLS)) { + if (!CreateTurnClientSocket()) { + OnAllocateError(SERVER_NOT_REACHABLE_ERROR, + "TURN host lookup received error."); + } + return; + } -void TurnPort::OnResolveResult(rtc::AsyncResolverInterface* resolver) { - RTC_DCHECK(resolver == resolver_); - // If DNS resolve is failed when trying to connect to the server using TCP, - // one of the reason could be due to DNS queries blocked by firewall. - // In such cases we will try to connect to the server with hostname, assuming - // socket layer will resolve the hostname through a HTTP proxy (if any). - if (resolver_->GetError() != 0 && (server_address_.proto == PROTO_TCP || - server_address_.proto == PROTO_TLS)) { - if (!CreateTurnClientSocket()) { + // Copy the original server address in `resolved_address`. For TLS based + // sockets we need hostname along with resolved address. + rtc::SocketAddress resolved_address = server_address_.address; + if (result.GetError() != 0 || + !result.GetResolvedAddress(Network()->GetBestIP().family(), + &resolved_address)) { + RTC_LOG(LS_WARNING) << ToString() << ": TURN host lookup received error " + << result.GetError(); + error_ = result.GetError(); OnAllocateError(SERVER_NOT_REACHABLE_ERROR, "TURN host lookup received error."); + return; } - return; - } - - // Copy the original server address in `resolved_address`. For TLS based - // sockets we need hostname along with resolved address. - rtc::SocketAddress resolved_address = server_address_.address; - if (resolver_->GetError() != 0 || - !resolver_->GetResolvedAddress(Network()->GetBestIP().family(), - &resolved_address)) { - RTC_LOG(LS_WARNING) << ToString() << ": TURN host lookup received error " - << resolver_->GetError(); - error_ = resolver_->GetError(); - OnAllocateError(SERVER_NOT_REACHABLE_ERROR, - "TURN host lookup received error."); - return; - } - // Signal needs both resolved and unresolved address. After signal is sent - // we can copy resolved address back into `server_address_`. - SignalResolvedServerAddress(this, server_address_.address, resolved_address); - server_address_.address = resolved_address; - PrepareAddress(); + // Signal needs both resolved and unresolved address. After signal is sent + // we can copy resolved address back into `server_address_`. + SignalResolvedServerAddress(this, server_address_.address, + resolved_address); + server_address_.address = resolved_address; + PrepareAddress(); + }); } void TurnPort::OnSendStunPacket(const void* data, diff --git a/p2p/base/turn_port.h b/p2p/base/turn_port.h index 950fc09561..18633735f6 100644 --- a/p2p/base/turn_port.h +++ b/p2p/base/turn_port.h @@ -21,10 +21,10 @@ #include #include "absl/memory/memory.h" +#include "api/async_dns_resolver.h" #include "p2p/base/port.h" #include "p2p/client/basic_port_allocator.h" #include "rtc_base/async_packet_socket.h" -#include "rtc_base/async_resolver_interface.h" #include "rtc_base/ssl_certificate.h" #include "rtc_base/task_utils/pending_task_safety_flag.h" @@ -392,7 +392,7 @@ class TurnPort : public Port { rtc::AsyncPacketSocket* socket_; SocketOptionsMap socket_options_; - rtc::AsyncResolverInterface* resolver_; + std::unique_ptr resolver_; int error_; rtc::DiffServCodePoint stun_dscp_value_;