1 /* 2 * Copyright 2019 The WebRTC Project Authors. All rights reserved. 3 * 4 * Use of this source code is governed by a BSD-style license 5 * that can be found in the LICENSE file in the root of the source 6 * tree. An additional intellectual property rights grant can be found 7 * in the file PATENTS. All contributing project authors may 8 * be found in the AUTHORS file in the root of the source tree. 9 */ 10 11 #ifndef API_PACKET_SOCKET_FACTORY_H_ 12 #define API_PACKET_SOCKET_FACTORY_H_ 13 14 #include <memory> 15 #include <string> 16 #include <vector> 17 18 #include "api/async_dns_resolver.h" 19 #include "api/wrapping_async_dns_resolver.h" 20 #include "rtc_base/async_packet_socket.h" 21 #include "rtc_base/proxy_info.h" 22 #include "rtc_base/system/rtc_export.h" 23 24 namespace rtc { 25 26 class SSLCertificateVerifier; 27 class AsyncResolverInterface; 28 29 struct PacketSocketTcpOptions { 30 PacketSocketTcpOptions() = default; 31 ~PacketSocketTcpOptions() = default; 32 33 int opts = 0; 34 std::vector<std::string> tls_alpn_protocols; 35 std::vector<std::string> tls_elliptic_curves; 36 // An optional custom SSL certificate verifier that an API user can provide to 37 // inject their own certificate verification logic (not available to users 38 // outside of the WebRTC repo). 39 SSLCertificateVerifier* tls_cert_verifier = nullptr; 40 }; 41 42 class RTC_EXPORT PacketSocketFactory { 43 public: 44 enum Options { 45 OPT_STUN = 0x04, 46 47 // The TLS options below are mutually exclusive. 48 OPT_TLS = 0x02, // Real and secure TLS. 49 OPT_TLS_FAKE = 0x01, // Fake TLS with a dummy SSL handshake. 50 OPT_TLS_INSECURE = 0x08, // Insecure TLS without certificate validation. 51 52 // Deprecated, use OPT_TLS_FAKE. 53 OPT_SSLTCP = OPT_TLS_FAKE, 54 }; 55 56 PacketSocketFactory() = default; 57 virtual ~PacketSocketFactory() = default; 58 59 virtual AsyncPacketSocket* CreateUdpSocket(const SocketAddress& address, 60 uint16_t min_port, 61 uint16_t max_port) = 0; 62 virtual AsyncListenSocket* CreateServerTcpSocket( 63 const SocketAddress& local_address, 64 uint16_t min_port, 65 uint16_t max_port, 66 int opts) = 0; 67 68 virtual AsyncPacketSocket* CreateClientTcpSocket( 69 const SocketAddress& local_address, 70 const SocketAddress& remote_address, 71 const ProxyInfo& proxy_info, 72 const std::string& user_agent, 73 const PacketSocketTcpOptions& tcp_options) = 0; 74 75 // The AsyncResolverInterface is deprecated; users are encouraged 76 // to switch to the AsyncDnsResolverInterface. 77 // TODO(bugs.webrtc.org/12598): Remove once all downstream users 78 // are converted. CreateAsyncResolver()79 virtual AsyncResolverInterface* CreateAsyncResolver() { 80 // Default implementation, so that downstream users can remove this 81 // immediately after changing to CreateAsyncDnsResolver 82 RTC_DCHECK_NOTREACHED(); 83 return nullptr; 84 } 85 86 virtual std::unique_ptr<webrtc::AsyncDnsResolverInterface> CreateAsyncDnsResolver()87 CreateAsyncDnsResolver() { 88 // Default implementation, to aid in transition to AsyncDnsResolverInterface 89 return std::make_unique<webrtc::WrappingAsyncDnsResolver>( 90 CreateAsyncResolver()); 91 } 92 93 private: 94 PacketSocketFactory(const PacketSocketFactory&) = delete; 95 PacketSocketFactory& operator=(const PacketSocketFactory&) = delete; 96 }; 97 98 } // namespace rtc 99 100 #endif // API_PACKET_SOCKET_FACTORY_H_ 101