| 1 | /* |
| 2 | * Copyright (C) 2017 Apple Inc. All rights reserved. |
| 3 | * |
| 4 | * Redistribution and use in source and binary forms, with or without |
| 5 | * modification, are permitted provided that the following conditions |
| 6 | * are met: |
| 7 | * 1. Redistributions of source code must retain the above copyright |
| 8 | * notice, this list of conditions and the following disclaimer. |
| 9 | * 2. Redistributions in binary form must reproduce the above copyright |
| 10 | * notice, this list of conditions and the following disclaimer in the |
| 11 | * documentation and/or other materials provided with the distribution. |
| 12 | * |
| 13 | * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' |
| 14 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, |
| 15 | * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 16 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS |
| 17 | * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 18 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 19 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 20 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 21 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 22 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF |
| 23 | * THE POSSIBILITY OF SUCH DAMAGE. |
| 24 | */ |
| 25 | |
| 26 | #include "config.h" |
| 27 | #include "LibWebRTCSocketFactory.h" |
| 28 | |
| 29 | #if USE(LIBWEBRTC) |
| 30 | |
| 31 | #include "NetworkProcessConnection.h" |
| 32 | #include "NetworkRTCMonitorMessages.h" |
| 33 | #include "NetworkRTCProviderMessages.h" |
| 34 | #include "WebProcess.h" |
| 35 | #include "WebRTCSocket.h" |
| 36 | #include <wtf/MainThread.h> |
| 37 | |
| 38 | namespace WebKit { |
| 39 | |
| 40 | uint64_t LibWebRTCSocketFactory::s_uniqueSocketIdentifier = 0; |
| 41 | uint64_t LibWebRTCSocketFactory::s_uniqueResolverIdentifier = 0; |
| 42 | |
| 43 | static inline rtc::SocketAddress prepareSocketAddress(const rtc::SocketAddress& address, bool disableNonLocalhostConnections) |
| 44 | { |
| 45 | auto result = RTCNetwork::isolatedCopy(address); |
| 46 | if (disableNonLocalhostConnections) |
| 47 | result.SetIP("127.0.0.1" ); |
| 48 | return result; |
| 49 | } |
| 50 | |
| 51 | rtc::AsyncPacketSocket* LibWebRTCSocketFactory::CreateServerTcpSocket(const rtc::SocketAddress& address, uint16_t minPort, uint16_t maxPort, int options) |
| 52 | { |
| 53 | auto socket = std::make_unique<LibWebRTCSocket>(*this, ++s_uniqueSocketIdentifier, LibWebRTCSocket::Type::ServerTCP, address, rtc::SocketAddress()); |
| 54 | m_sockets.set(socket->identifier(), socket.get()); |
| 55 | |
| 56 | callOnMainThread([identifier = socket->identifier(), address = prepareSocketAddress(address, m_disableNonLocalhostConnections), minPort, maxPort, options]() { |
| 57 | if (!WebProcess::singleton().ensureNetworkProcessConnection().connection().send(Messages::NetworkRTCProvider::CreateServerTCPSocket(identifier, RTCNetwork::SocketAddress(address), minPort, maxPort, options), 0)) { |
| 58 | // FIXME: Set error back to socket |
| 59 | return; |
| 60 | } |
| 61 | |
| 62 | }); |
| 63 | |
| 64 | return socket.release(); |
| 65 | } |
| 66 | |
| 67 | rtc::AsyncPacketSocket* LibWebRTCSocketFactory::CreateUdpSocket(const rtc::SocketAddress& address, uint16_t minPort, uint16_t maxPort) |
| 68 | { |
| 69 | auto socket = std::make_unique<LibWebRTCSocket>(*this, ++s_uniqueSocketIdentifier, LibWebRTCSocket::Type::UDP, address, rtc::SocketAddress()); |
| 70 | m_sockets.set(socket->identifier(), socket.get()); |
| 71 | |
| 72 | callOnMainThread([identifier = socket->identifier(), address = prepareSocketAddress(address, m_disableNonLocalhostConnections), minPort, maxPort]() { |
| 73 | if (!WebProcess::singleton().ensureNetworkProcessConnection().connection().send(Messages::NetworkRTCProvider::CreateUDPSocket(identifier, RTCNetwork::SocketAddress(address), minPort, maxPort), 0)) { |
| 74 | // FIXME: Set error back to socket |
| 75 | return; |
| 76 | } |
| 77 | }); |
| 78 | return socket.release(); |
| 79 | } |
| 80 | |
| 81 | rtc::AsyncPacketSocket* LibWebRTCSocketFactory::CreateClientTcpSocket(const rtc::SocketAddress& localAddress, const rtc::SocketAddress& remoteAddress, const rtc::ProxyInfo&, const std::string&, int options) |
| 82 | { |
| 83 | auto socket = std::make_unique<LibWebRTCSocket>(*this, ++s_uniqueSocketIdentifier, LibWebRTCSocket::Type::ClientTCP, localAddress, remoteAddress); |
| 84 | socket->setState(LibWebRTCSocket::STATE_CONNECTING); |
| 85 | m_sockets.set(socket->identifier(), socket.get()); |
| 86 | |
| 87 | callOnMainThread([identifier = socket->identifier(), localAddress = prepareSocketAddress(localAddress, m_disableNonLocalhostConnections), remoteAddress = prepareSocketAddress(remoteAddress, m_disableNonLocalhostConnections), options]() { |
| 88 | if (!WebProcess::singleton().ensureNetworkProcessConnection().connection().send(Messages::NetworkRTCProvider::CreateClientTCPSocket(identifier, RTCNetwork::SocketAddress(localAddress), RTCNetwork::SocketAddress(remoteAddress), options), 0)) { |
| 89 | // FIXME: Set error back to socket |
| 90 | return; |
| 91 | } |
| 92 | }); |
| 93 | |
| 94 | return socket.release(); |
| 95 | } |
| 96 | |
| 97 | rtc::AsyncPacketSocket* LibWebRTCSocketFactory::createNewConnectionSocket(LibWebRTCSocket& serverSocket, uint64_t newConnectionSocketIdentifier, const rtc::SocketAddress& remoteAddress) |
| 98 | { |
| 99 | auto socket = std::make_unique<LibWebRTCSocket>(*this, ++s_uniqueSocketIdentifier, LibWebRTCSocket::Type::ServerConnectionTCP, serverSocket.localAddress(), remoteAddress); |
| 100 | socket->setState(LibWebRTCSocket::STATE_CONNECTED); |
| 101 | m_sockets.set(socket->identifier(), socket.get()); |
| 102 | |
| 103 | callOnMainThread([identifier = socket->identifier(), newConnectionSocketIdentifier]() { |
| 104 | if (!WebProcess::singleton().ensureNetworkProcessConnection().connection().send(Messages::NetworkRTCProvider::WrapNewTCPConnection(identifier, newConnectionSocketIdentifier), 0)) { |
| 105 | // FIXME: Set error back to socket |
| 106 | return; |
| 107 | } |
| 108 | }); |
| 109 | return socket.release(); |
| 110 | } |
| 111 | |
| 112 | void LibWebRTCSocketFactory::detach(LibWebRTCSocket& socket) |
| 113 | { |
| 114 | ASSERT(m_sockets.contains(socket.identifier())); |
| 115 | m_sockets.remove(socket.identifier()); |
| 116 | } |
| 117 | |
| 118 | rtc::AsyncResolverInterface* LibWebRTCSocketFactory::CreateAsyncResolver() |
| 119 | { |
| 120 | auto resolver = std::make_unique<LibWebRTCResolver>(++s_uniqueResolverIdentifier); |
| 121 | auto* resolverPointer = resolver.get(); |
| 122 | m_resolvers.set(resolverPointer->identifier(), WTFMove(resolver)); |
| 123 | return resolverPointer; |
| 124 | } |
| 125 | |
| 126 | } // namespace WebKit |
| 127 | |
| 128 | #endif // USE(LIBWEBRTC) |
| 129 | |