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#pragma once
27
28#if USE(LIBWEBRTC)
29
30#include "LibWebRTCResolver.h"
31#include "LibWebRTCSocket.h"
32#include <WebCore/LibWebRTCMacros.h>
33#include <webrtc/rtc_base/nethelpers.h>
34#include <webrtc/p2p/base/packetsocketfactory.h>
35#include <wtf/HashMap.h>
36
37namespace WebKit {
38
39class LibWebRTCSocketFactory final : public rtc::PacketSocketFactory {
40public:
41 LibWebRTCSocketFactory() { }
42
43 void detach(LibWebRTCSocket&);
44
45 LibWebRTCSocket* socket(uint64_t identifier) { return m_sockets.get(identifier); }
46 LibWebRTCResolver* resolver(uint64_t identifier) { return m_resolvers.get(identifier); }
47
48 std::unique_ptr<LibWebRTCResolver> takeResolver(uint64_t identifier) { return m_resolvers.take(identifier); }
49
50 rtc::AsyncPacketSocket* createNewConnectionSocket(LibWebRTCSocket&, uint64_t newConnectionSocketIdentifier, const rtc::SocketAddress&);
51
52 void disableNonLocalhostConnections() { m_disableNonLocalhostConnections = true; }
53
54 rtc::AsyncResolverInterface* createAsyncResolver() { return CreateAsyncResolver(); }
55
56private:
57 rtc::AsyncPacketSocket* CreateUdpSocket(const rtc::SocketAddress&, uint16_t minPort, uint16_t maxPort) final;
58 rtc::AsyncPacketSocket* CreateServerTcpSocket(const rtc::SocketAddress&, uint16_t min_port, uint16_t max_port, int options) final;
59 rtc::AsyncPacketSocket* CreateClientTcpSocket(const rtc::SocketAddress& localAddress, const rtc::SocketAddress& remoteAddress, const rtc::ProxyInfo&, const std::string&, int options);
60 rtc::AsyncResolverInterface* CreateAsyncResolver() final;
61
62 // We cannot own sockets, clients of the factory are responsible to free them.
63 HashMap<uint64_t, LibWebRTCSocket*> m_sockets;
64 static uint64_t s_uniqueSocketIdentifier;
65
66 // We can own resolvers as we control their Destroy method.
67 HashMap<uint64_t, std::unique_ptr<LibWebRTCResolver>> m_resolvers;
68 static uint64_t s_uniqueResolverIdentifier;
69 bool m_disableNonLocalhostConnections { false };
70};
71
72} // namespace WebKit
73
74#endif // USE(LIBWEBRTC)
75