Skip to content
Permalink
Browse files
Move DNS resolution outside of NetworkRTCProvider
https://bugs.webkit.org/show_bug.cgi?id=178796

Patch by Youenn Fablet <youenn@apple.com> on 2017-10-25
Reviewed by Geoffrey Garen.

       Tested through manual testing on external STUN servers.
       Moving NetworkRTCProvider::Resolver into NetworkRTCResolver.
       Updating the implementation to use a completion handler.

* NetworkProcess/webrtc/NetworkRTCProvider.cpp:
(WebKit::NetworkRTCProvider::createResolver):
* NetworkProcess/webrtc/NetworkRTCProvider.h:
* NetworkProcess/webrtc/NetworkRTCResolver.cpp:
(WebKit::NetworkRTCResolver::NetworkRTCResolver):
(WebKit::NetworkRTCResolver::~NetworkRTCResolver):
(WebKit::NetworkRTCResolver::start):
(WebKit::NetworkRTCResolver::stop):
(WebKit::NetworkRTCResolver::completed):
(WebKit::NetworkRTCProvider::Resolver::~Resolver): Deleted.
(WebKit::NetworkRTCProvider::stop): Deleted.
(WebKit::NetworkRTCProvider::resolutionCompleted): Deleted.
* NetworkProcess/webrtc/NetworkRTCResolver.h:
* WebKit.xcodeproj/project.pbxproj:

Canonical link: https://commits.webkit.org/194984@main
git-svn-id: https://svn.webkit.org/repository/webkit/trunk@223990 268f45cc-cd09-0410-ab3c-d52691b4dbfc
  • Loading branch information
youennf authored and webkit-commit-queue committed Oct 25, 2017
1 parent c48ef49 commit 54b4917dcb8a5a0d06201d8bbd79bf5e20abfb4a
Showing 6 changed files with 205 additions and 65 deletions.
@@ -1,3 +1,29 @@
2017-10-25 Youenn Fablet <youenn@apple.com>

Move DNS resolution outside of NetworkRTCProvider
https://bugs.webkit.org/show_bug.cgi?id=178796

Reviewed by Geoffrey Garen.

Tested through manual testing on external STUN servers.
Moving NetworkRTCProvider::Resolver into NetworkRTCResolver.
Updating the implementation to use a completion handler.

* NetworkProcess/webrtc/NetworkRTCProvider.cpp:
(WebKit::NetworkRTCProvider::createResolver):
* NetworkProcess/webrtc/NetworkRTCProvider.h:
* NetworkProcess/webrtc/NetworkRTCResolver.cpp:
(WebKit::NetworkRTCResolver::NetworkRTCResolver):
(WebKit::NetworkRTCResolver::~NetworkRTCResolver):
(WebKit::NetworkRTCResolver::start):
(WebKit::NetworkRTCResolver::stop):
(WebKit::NetworkRTCResolver::completed):
(WebKit::NetworkRTCProvider::Resolver::~Resolver): Deleted.
(WebKit::NetworkRTCProvider::stop): Deleted.
(WebKit::NetworkRTCProvider::resolutionCompleted): Deleted.
* NetworkProcess/webrtc/NetworkRTCResolver.h:
* WebKit.xcodeproj/project.pbxproj:

2017-10-25 Eric Carlson <eric.carlson@apple.com>

[MediaStream] Clear cached gUM prompt state
@@ -31,6 +31,7 @@
#include "Logging.h"
#include "NetworkConnectionToWebProcess.h"
#include "NetworkProcess.h"
#include "NetworkRTCResolver.h"
#include "NetworkRTCSocket.h"
#include "WebRTCResolverMessages.h"
#include "WebRTCSocketMessages.h"
@@ -163,60 +164,22 @@ void NetworkRTCProvider::didReceiveNetworkRTCSocketMessage(IPC::Connection& conn

void NetworkRTCProvider::createResolver(uint64_t identifier, const String& address)
{
auto resolver = std::make_unique<Resolver>(identifier, *this, adoptCF(CFHostCreateWithName(kCFAllocatorDefault, address.createCFString().get())));

CFHostClientContext context = { 0, resolver.get(), nullptr, nullptr, nullptr };
CFHostSetClient(resolver->host.get(), NetworkRTCProvider::resolvedName, &context);
CFHostScheduleWithRunLoop(resolver->host.get(), CFRunLoopGetCurrent(), kCFRunLoopDefaultMode);
Boolean result = CFHostStartInfoResolution(resolver->host.get(), kCFHostAddresses, nullptr);
ASSERT_UNUSED(result, result);

auto resolver = std::make_unique<NetworkRTCResolver>([this, identifier](NetworkRTCResolver::AddressesOrError&& result) mutable {
if (!result.hasValue()) {
if (result.error() != NetworkRTCResolver::Error::Cancelled)
m_connection->connection().send(Messages::WebRTCResolver::ResolvedAddressError(1), identifier);
return;
}
m_connection->connection().send(Messages::WebRTCResolver::SetResolvedAddress(result.value()), identifier);
});
resolver->start(address);
m_resolvers.add(identifier, WTFMove(resolver));
}

NetworkRTCProvider::Resolver::~Resolver()
{
CFHostUnscheduleFromRunLoop(host.get(), CFRunLoopGetCurrent(), kCFRunLoopDefaultMode);
CFHostSetClient(host.get(), nullptr, nullptr);
}

void NetworkRTCProvider::stopResolver(uint64_t identifier)
{
ASSERT(identifier);
if (auto resolver = m_resolvers.take(identifier))
CFHostCancelInfoResolution(resolver->host.get(), CFHostInfoType::kCFHostAddresses);
}

void NetworkRTCProvider::resolvedName(CFHostRef hostRef, CFHostInfoType typeInfo, const CFStreamError *error, void *info)
{
ASSERT_UNUSED(typeInfo, !typeInfo);

if (error->domain) {
// FIXME: Need to handle failure, but info is not provided in the callback.
return;
}

ASSERT(info);
auto* resolverInfo = static_cast<Resolver*>(info);
auto resolver = resolverInfo->rtcProvider.m_resolvers.take(resolverInfo->identifier);
if (!resolver)
return;

Boolean result;
CFArrayRef resolvedAddresses = (CFArrayRef)CFHostGetAddressing(hostRef, &result);
ASSERT_UNUSED(result, result);

size_t count = CFArrayGetCount(resolvedAddresses);
Vector<RTCNetwork::IPAddress> addresses;
addresses.reserveInitialCapacity(count);

for (size_t index = 0; index < count; ++index) {
CFDataRef data = (CFDataRef)CFArrayGetValueAtIndex(resolvedAddresses, index);
auto* address = reinterpret_cast<const struct sockaddr_in*>(CFDataGetBytePtr(data));
addresses.uncheckedAppend(RTCNetwork::IPAddress(rtc::IPAddress(address->sin_addr)));
}
ASSERT(resolver->rtcProvider.m_connection);
resolver->rtcProvider.m_connection->connection().send(Messages::WebRTCResolver::SetResolvedAddress(addresses), resolver->identifier);
resolver->stop();
}

void NetworkRTCProvider::closeListeningSockets(Function<void()>&& completionHandler)
@@ -30,7 +30,6 @@
#include "LibWebRTCSocketClient.h"
#include "NetworkRTCMonitor.h"
#include "RTCNetwork.h"
#include <CFNetwork/CFHost.h>
#include <WebCore/LibWebRTCMacros.h>
#include <webrtc/base/sigslot.h>
#include <webrtc/p2p/base/basicpacketsocketfactory.h>
@@ -45,8 +44,8 @@ class Decoder;
}

namespace WebKit {

class NetworkConnectionToWebProcess;
class NetworkRTCResolver;
class NetworkRTCSocket;

class NetworkRTCProvider : public ThreadSafeRefCounted<NetworkRTCProvider>, public rtc::MessageHandler {
@@ -89,21 +88,7 @@ class NetworkRTCProvider : public ThreadSafeRefCounted<NetworkRTCProvider>, publ

void OnMessage(rtc::Message*);

static void resolvedName(CFHostRef, CFHostInfoType, const CFStreamError*, void*);

struct Resolver {
Resolver(uint64_t identifier, NetworkRTCProvider& rtcProvider, RetainPtr<CFHostRef>&& host)
: identifier(identifier)
, rtcProvider(rtcProvider)
, host(WTFMove(host)) { }
~Resolver();

uint64_t identifier;
NetworkRTCProvider& rtcProvider;
RetainPtr<CFHostRef> host;
};

HashMap<uint64_t, std::unique_ptr<Resolver>> m_resolvers;
HashMap<uint64_t, std::unique_ptr<NetworkRTCResolver>> m_resolvers;
HashMap<uint64_t, std::unique_ptr<LibWebRTCSocketClient>> m_sockets;
NetworkConnectionToWebProcess* m_connection;
bool m_isStarted { true };
@@ -0,0 +1,101 @@
/*
* Copyright (C) 2017 Apple Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
* THE POSSIBILITY OF SUCH DAMAGE.
*/

#include "config.h"
#include "NetworkRTCResolver.h"

#if USE(LIBWEBRTC)

#include <wtf/Expected.h>

namespace WebKit {

static void resolvedName(CFHostRef hostRef, CFHostInfoType typeInfo, const CFStreamError *error, void *info)
{
ASSERT_UNUSED(typeInfo, !typeInfo);

if (error->domain) {
// FIXME: Need to handle failure, but info is not provided in the callback.
return;
}

ASSERT(info);
auto* resolver = static_cast<NetworkRTCResolver*>(info);

Boolean result;
CFArrayRef resolvedAddresses = (CFArrayRef)CFHostGetAddressing(hostRef, &result);
ASSERT_UNUSED(result, result);

size_t count = CFArrayGetCount(resolvedAddresses);
Vector<RTCNetwork::IPAddress> addresses;
addresses.reserveInitialCapacity(count);

for (size_t index = 0; index < count; ++index) {
CFDataRef data = (CFDataRef)CFArrayGetValueAtIndex(resolvedAddresses, index);
auto* address = reinterpret_cast<const struct sockaddr_in*>(CFDataGetBytePtr(data));
addresses.uncheckedAppend(RTCNetwork::IPAddress(rtc::IPAddress(address->sin_addr)));
}
resolver->completed(addresses);
}

NetworkRTCResolver::NetworkRTCResolver(CompletionHandler&& completionHandler)
: m_completionHandler(WTFMove(completionHandler))
{
}

NetworkRTCResolver::~NetworkRTCResolver()
{
CFHostUnscheduleFromRunLoop(m_host.get(), CFRunLoopGetCurrent(), kCFRunLoopDefaultMode);
CFHostSetClient(m_host.get(), nullptr, nullptr);
if (auto completionHandler = WTFMove(m_completionHandler))
completionHandler(makeUnexpected(Error::Unknown));
}

void NetworkRTCResolver::start(const String& address)
{
m_host = adoptCF(CFHostCreateWithName(kCFAllocatorDefault, address.createCFString().get()));
CFHostClientContext context = { 0, this, nullptr, nullptr, nullptr };
CFHostSetClient(m_host.get(), resolvedName, &context);
CFHostScheduleWithRunLoop(m_host.get(), CFRunLoopGetCurrent(), kCFRunLoopDefaultMode);
Boolean result = CFHostStartInfoResolution(m_host.get(), kCFHostAddresses, nullptr);
ASSERT_UNUSED(result, result);
}

void NetworkRTCResolver::stop()
{
CFHostCancelInfoResolution(m_host.get(), CFHostInfoType::kCFHostAddresses);
if (auto completionHandler = WTFMove(m_completionHandler))
completionHandler(makeUnexpected(Error::Cancelled));
}

void NetworkRTCResolver::completed(const Vector<RTCNetwork::IPAddress>& addresses)
{
if (auto completionHandler = WTFMove(m_completionHandler))
completionHandler({ addresses });
}

} // namespace WebKit

#endif // USE(LIBWEBRTC)
@@ -0,0 +1,59 @@
/*
* Copyright (C) 2017 Apple Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
* THE POSSIBILITY OF SUCH DAMAGE.
*/

#pragma once

#if USE(LIBWEBRTC)

#include "RTCNetwork.h"
#include <CFNetwork/CFHost.h>
#include <wtf/CompletionHandler.h>
#include <wtf/text/WTFString.h>

namespace WebKit {

class NetworkRTCResolver {
public:
enum class Error { Unknown, Cancelled };

using AddressesOrError = Expected<std::reference_wrapper<const Vector<RTCNetwork::IPAddress>>, Error>;
using CompletionHandler = WTF::CompletionHandler<void(AddressesOrError&&)>;

explicit NetworkRTCResolver(CompletionHandler&&);
~NetworkRTCResolver();

void start(const String& address);
void stop();

void completed(const Vector<RTCNetwork::IPAddress>&);

private:
CompletionHandler m_completionHandler;
RetainPtr<CFHostRef> m_host;
};

} // namespace WebKit

#endif // USE(LIBWEBRTC)
@@ -877,6 +877,7 @@
3F87B9C0158940D80090FF62 /* WebColorPicker.h in Headers */ = {isa = PBXBuildFile; fileRef = 3F87B9BF158940D80090FF62 /* WebColorPicker.h */; };
410482CD1DDD324C00F006D0 /* RTCNetwork.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 410482CB1DDD2FB500F006D0 /* RTCNetwork.cpp */; };
410482CE1DDD324F00F006D0 /* RTCNetwork.h in Headers */ = {isa = PBXBuildFile; fileRef = 410482CC1DDD2FB500F006D0 /* RTCNetwork.h */; };
4112B5551FA0EA7A00E67875 /* NetworkRTCResolver.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 4112B5471F9FD3AB00E67875 /* NetworkRTCResolver.cpp */; };
411B22641E371BA6004F7363 /* LibWebRTCNetwork.h in Headers */ = {isa = PBXBuildFile; fileRef = 411B22621E371244004F7363 /* LibWebRTCNetwork.h */; };
413075A91DE85F2C0039EC69 /* NetworkRTCSocket.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 413075981DE84FB00039EC69 /* NetworkRTCSocket.cpp */; };
413075AA1DE85F300039EC69 /* NetworkRTCMonitor.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 4130759A1DE84FB00039EC69 /* NetworkRTCMonitor.cpp */; };
@@ -3173,6 +3174,8 @@
3F87B9BF158940D80090FF62 /* WebColorPicker.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = WebColorPicker.h; sourceTree = "<group>"; };
410482CB1DDD2FB500F006D0 /* RTCNetwork.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = RTCNetwork.cpp; sourceTree = "<group>"; };
410482CC1DDD2FB500F006D0 /* RTCNetwork.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = RTCNetwork.h; sourceTree = "<group>"; };
4112B5471F9FD3AB00E67875 /* NetworkRTCResolver.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = NetworkRTCResolver.cpp; path = NetworkProcess/webrtc/NetworkRTCResolver.cpp; sourceTree = "<group>"; };
4112B5481F9FD3AC00E67875 /* NetworkRTCResolver.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = NetworkRTCResolver.h; path = NetworkProcess/webrtc/NetworkRTCResolver.h; sourceTree = "<group>"; };
411B22621E371244004F7363 /* LibWebRTCNetwork.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = LibWebRTCNetwork.h; path = Network/webrtc/LibWebRTCNetwork.h; sourceTree = "<group>"; };
413075981DE84FB00039EC69 /* NetworkRTCSocket.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = NetworkRTCSocket.cpp; path = NetworkProcess/webrtc/NetworkRTCSocket.cpp; sourceTree = "<group>"; };
413075991DE84FB00039EC69 /* NetworkRTCSocket.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = NetworkRTCSocket.h; path = NetworkProcess/webrtc/NetworkRTCSocket.h; sourceTree = "<group>"; };
@@ -6040,6 +6043,8 @@
41DC45951E3D6E1E00B11F51 /* NetworkRTCProvider.cpp */,
41DC45941E3D6E1E00B11F51 /* NetworkRTCProvider.h */,
41DC45981E3D6ED600B11F51 /* NetworkRTCProvider.messages.in */,
4112B5471F9FD3AB00E67875 /* NetworkRTCResolver.cpp */,
4112B5481F9FD3AC00E67875 /* NetworkRTCResolver.h */,
413075981DE84FB00039EC69 /* NetworkRTCSocket.cpp */,
413075991DE84FB00039EC69 /* NetworkRTCSocket.h */,
4130759D1DE84FB00039EC69 /* NetworkRTCSocket.messages.in */,
@@ -10305,6 +10310,7 @@
51F060E11654318500F3281E /* NetworkRTCMonitorMessageReceiver.cpp in Sources */,
41DC45971E3D6E2200B11F51 /* NetworkRTCProvider.cpp in Sources */,
51F060E11654318500F3282E /* NetworkRTCProviderMessageReceiver.cpp in Sources */,
4112B5551FA0EA7A00E67875 /* NetworkRTCResolver.cpp in Sources */,
413075A91DE85F2C0039EC69 /* NetworkRTCSocket.cpp in Sources */,
51F060E11654318500F3281D /* NetworkRTCSocketMessageReceiver.cpp in Sources */,
BC8283AC16B4BF3F00A278FE /* NetworkServiceEntryPoint.mm in Sources */,

0 comments on commit 54b4917

Please sign in to comment.