packet_socket_factory.h (3966B)
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 <cstdint> 15 #include <memory> 16 #include <string> 17 #include <vector> 18 19 #include "absl/memory/memory.h" 20 #include "api/async_dns_resolver.h" 21 #include "api/environment/environment.h" 22 #include "rtc_base/async_packet_socket.h" 23 #include "rtc_base/checks.h" 24 #include "rtc_base/socket_address.h" 25 #include "rtc_base/ssl_certificate.h" 26 #include "rtc_base/system/rtc_export.h" 27 28 namespace webrtc { 29 30 struct PacketSocketTcpOptions { 31 PacketSocketTcpOptions() = default; 32 ~PacketSocketTcpOptions() = default; 33 34 int opts = 0; 35 std::vector<std::string> tls_alpn_protocols; 36 std::vector<std::string> tls_elliptic_curves; 37 // An optional custom SSL certificate verifier that an API user can provide to 38 // inject their own certificate verification logic (not available to users 39 // outside of the WebRTC repo). 40 SSLCertificateVerifier* tls_cert_verifier = nullptr; 41 }; 42 43 class RTC_EXPORT PacketSocketFactory { 44 public: 45 enum Options { 46 OPT_STUN = 0x04, 47 48 // The TLS options below are mutually exclusive. 49 OPT_TLS = 0x02, // Real and secure TLS. 50 OPT_TLS_FAKE = 0x01, // Fake TLS with a dummy SSL handshake. 51 OPT_TLS_INSECURE = 0x08, // Insecure TLS without certificate validation. 52 53 // Deprecated, use OPT_TLS_FAKE. 54 OPT_SSLTCP = OPT_TLS_FAKE, 55 }; 56 57 PacketSocketFactory() = default; 58 59 PacketSocketFactory(const PacketSocketFactory&) = delete; 60 PacketSocketFactory& operator=(const PacketSocketFactory&) = delete; 61 62 virtual ~PacketSocketFactory() = default; 63 64 // TODO: bugs.webrtc.org/42223992 - after Oct 10, 2025 make Create*Socket 65 // functions that accept Environment pure virtual, and delete legacy 66 // Create*Socket functions. 67 virtual std::unique_ptr<AsyncPacketSocket> CreateUdpSocket( 68 const Environment& /*env*/, 69 const SocketAddress& address, 70 uint16_t min_port, 71 uint16_t max_port) { 72 return absl::WrapUnique(CreateUdpSocket(address, min_port, max_port)); 73 } 74 75 virtual std::unique_ptr<AsyncListenSocket> CreateServerTcpSocket( 76 const Environment& /*env*/, 77 const SocketAddress& local_address, 78 uint16_t min_port, 79 uint16_t max_port, 80 int opts) { 81 return absl::WrapUnique( 82 CreateServerTcpSocket(local_address, min_port, max_port, opts)); 83 } 84 85 virtual std::unique_ptr<AsyncPacketSocket> CreateClientTcpSocket( 86 const Environment& /*env*/, 87 const SocketAddress& local_address, 88 const SocketAddress& remote_address, 89 const PacketSocketTcpOptions& tcp_options) { 90 return absl::WrapUnique( 91 CreateClientTcpSocket(local_address, remote_address, tcp_options)); 92 } 93 94 virtual std::unique_ptr<AsyncDnsResolverInterface> 95 CreateAsyncDnsResolver() = 0; 96 97 private: 98 virtual AsyncPacketSocket* CreateUdpSocket(const SocketAddress& address, 99 uint16_t min_port, 100 uint16_t max_port) { 101 RTC_DCHECK_NOTREACHED(); 102 return nullptr; 103 } 104 virtual AsyncListenSocket* CreateServerTcpSocket( 105 const SocketAddress& local_address, 106 uint16_t min_port, 107 uint16_t max_port, 108 int opts) { 109 RTC_DCHECK_NOTREACHED(); 110 return nullptr; 111 } 112 113 virtual AsyncPacketSocket* CreateClientTcpSocket( 114 const SocketAddress& local_address, 115 const SocketAddress& remote_address, 116 const PacketSocketTcpOptions& tcp_options) { 117 RTC_DCHECK_NOTREACHED(); 118 return nullptr; 119 } 120 }; 121 122 } // namespace webrtc 123 124 125 #endif // API_PACKET_SOCKET_FACTORY_H_