| #include "p2p/base/p2p_transport_channel.h" |
| 6 | #include "p2p/base/basic_async_resolver_factory.h" |
| 7 | #include "api/packet_socket_factory.h" |
| 8 | #include "rtc_base/task_utils/to_queued_task.h" |
| 9 | #include "rtc_base/rtc_certificate_generator.h" |
| 10 | #include "p2p/base/ice_credentials_iterator.h" |
| 11 | #include "api/jsep_ice_candidate.h" |
| 12 | #include "p2p/base/dtls_transport.h" |
| 13 | #include "p2p/base/dtls_transport_factory.h" |
| 14 | #include "pc/dtls_srtp_transport.h" |
| 15 | #include "pc/dtls_transport.h" |
| 16 | #include "TurnCustomizerImpl.h" |
| 17 | #include "SctpDataChannelProviderInterfaceImpl.h" |
| 18 | #include "StaticThreads.h" |
| 19 |
| 20 | namespace tgcalls { |
| 21 |
| 22 | webrtc::CryptoOptions NativeNetworkingImpl::getDefaulCryptoOptions() { |
| 23 | auto options = webrtc::CryptoOptions(); |
| 24 | options.srtp.enable_aes128_sha1_80_crypto_cipher = true; |
| 25 | options.srtp.enable_gcm_crypto_suites = true; |
| 26 | return options; |
| 27 | } |
| 28 |
| 29 | NativeNetworkingImpl::NativeNetworkingImpl(Configuration &&configuration) : |
| 30 | _threads(std::move(configuration.threads)), |
| 31 | _isOutgoing(configuration.isOutgoing), |
| 32 | _enableStunMarking(configuration.enableStunMarking), |
| 33 | _enableTCP(configuration.enableTCP), |
| 34 | _enableP2P(configuration.enableP2P), |
| 35 | _rtcServers(configuration.rtcServers), |
| 36 | _stateUpdated(std::move(configuration.stateUpdated)), |
| 37 | _candidateGathered(std::move(configuration.candidateGathered)), |
| 38 | _transportMessageReceived(std::move(configuration.transportMessageReceived)), |
| 39 | _rtcpPacketReceived(std::move(configuration.rtcpPacketReceived)), |
| 40 | _dataChannelStateUpdated(configuration.dataChannelStateUpdated), |
| 41 | _dataChannelMessageReceived(configuration.dataChannelMessageReceived) { |
| 42 | assert(_threads->getNetworkThread()->IsCurrent()); |
| 43 |
| 44 | _localIceParameters = PeerIceParameters(rtc::CreateRandomString(cricket::ICE_UFRAG_LENGTH), rtc::CreateRandomString(cricket::ICE_PWD_LENGTH)); |
| 45 |
| 46 | _localCertificate = rtc::RTCCertificateGenerator::GenerateCertificate(rtc::KeyParams(rtc::KT_ECDSA), absl::nullopt); |
| 47 |
| 48 | _socketFactory.reset(new rtc::BasicPacketSocketFactory(_threads->getNetworkThread())); |
| 49 | _networkManager = std::make_unique<rtc::BasicNetworkManager>(); |
| 50 | _asyncResolverFactory = std::make_unique<webrtc::BasicAsyncResolverFactory>(); |
| 51 |
| 52 | _dtlsSrtpTransport = std::make_unique<webrtc::DtlsSrtpTransport>(true); |
| 53 | _dtlsSrtpTransport->SetDtlsTransports(nullptr, nullptr); |
| 54 | _dtlsSrtpTransport->SetActiveResetSrtpParams(false); |
| 55 | _dtlsSrtpTransport->SignalReadyToSend.connect(this, &NativeNetworkingImpl::DtlsReadyToSend); |
| 56 | _dtlsSrtpTransport->SignalRtpPacketReceived.connect(this, &NativeNetworkingImpl::RtpPacketReceived_n); |
| 57 | _dtlsSrtpTransport->SignalRtcpPacketReceived.connect(this, &NativeNetworkingImpl::OnRtcpPacketReceived_n); |
| 58 |
| 59 | resetDtlsSrtpTransport(); |
| 60 | } |
| 61 |
| 62 | NativeNetworkingImpl::~NativeNetworkingImpl() { |
| 63 | assert(_threads->getNetworkThread()->IsCurrent()); |
| 64 |
| 65 | RTC_LOG(LS_INFO) << "NativeNetworkingImpl::~NativeNetworkingImpl()"; |
| 66 |
| 67 | _dtlsSrtpTransport.reset(); |
| 68 | _dtlsTransport.reset(); |
| 69 | _dataChannelInterface.reset(); |
| 70 | _transportChannel.reset(); |
| 71 | _asyncResolverFactory.reset(); |
| 72 | _portAllocator.reset(); |
| 73 | _networkManager.reset(); |
| 74 | _socketFactory.reset(); |
| 75 | } |
| 76 |
| 77 | void NativeNetworkingImpl::resetDtlsSrtpTransport() { |
| 78 | if (_enableStunMarking) { |
| 79 | _turnCustomizer.reset(new TurnCustomizerImpl()); |
| 80 | } |
| 81 |
| 82 | _portAllocator.reset(new cricket::BasicPortAllocator(_networkManager.get(), _socketFactory.get(), _turnCustomizer.get(), nullptr)); |
| 83 |
| 84 | uint32_t flags = _portAllocator->flags(); |
| 85 |
| 86 | flags |= |
| 87 | //cricket::PORTALLOCATOR_ENABLE_SHARED_SOCKET | |
| 88 | cricket::PORTALLOCATOR_ENABLE_IPV6 | |
| 89 | cricket::PORTALLOCATOR_ENABLE_IPV6_ON_WIFI; |
| 90 |
| 91 | if (!_enableTCP) { |
| 92 | flags |= cricket::PORTALLOCATOR_DISABLE_TCP; |
| 93 | } |
| 94 | if (!_enableP2P) { |
| 95 | flags |= cricket::PORTALLOCATOR_DISABLE_UDP; |
| 96 | flags |= cricket::PORTALLOCATOR_DISABLE_STUN; |
| 97 | uint32_t candidateFilter = _portAllocator->candidate_filter(); |
| 98 | candidateFilter &= ~(cricket::CF_REFLEXIVE); |
| 99 | _portAllocator->SetCandidateFilter(candidateFilter); |
| 100 | } |
| 101 |
| 102 | _portAllocator->set_step_delay(cricket::kMinimumStepDelay); |
| 103 |
| 104 | //TODO: figure out the proxy setup |
| 105 | /*if (_proxy) { |
| 106 | rtc::ProxyInfo proxyInfo; |
| 107 | proxyInfo.type = rtc::ProxyType::PROXY_SOCKS5; |
| 108 | proxyInfo.address = rtc::SocketAddress(_proxy->host, _proxy->port); |
| 109 | proxyInfo.username = _proxy->login; |
| 110 | proxyInfo.password = rtc::CryptString(TgCallsCryptStringImpl(_proxy->password)); |
| 111 | _portAllocator->set_proxy("t/1.0", proxyInfo); |
| 112 | }*/ |
| 113 |
| 114 | _portAllocator->set_flags(flags); |
| 115 | _portAllocator->Initialize(); |
| 116 |
| 117 | cricket::ServerAddresses stunServers; |
| 118 | std::vector<cricket::RelayServerConfig> turnServers; |
| 119 |
| 120 | for (auto &server : _rtcServers) { |
| 121 | if (server.isTurn) { |
| 122 | turnServers.push_back(cricket::RelayServerConfig( |
| 123 | rtc::SocketAddress(server.host, server.port), |
| 124 | server.login, |
| 125 | server.password, |
| 126 | cricket::PROTO_UDP |
| 127 | )); |
| 128 | } else { |
| 129 | rtc::SocketAddress stunAddress = rtc::SocketAddress(server.host, server.port); |
| 130 | stunServers.insert(stunAddress); |
| 131 | } |
| 132 | } |
| 133 |
| 134 | _portAllocator->SetConfiguration(stunServers, turnServers, 2, webrtc::NO_PRUNE, _turnCustomizer.get()); |
| 135 |
| 136 | _transportChannel.reset(new cricket::P2PTransportChannel("transport", 0, _portAllocator.get(), _asyncResolverFactory.get(), nullptr)); |
| 137 |
| 138 | cricket::IceConfig iceConfig; |
| 139 | iceConfig.continual_gathering_policy = cricket::GATHER_CONTINUALLY; |
| 140 | iceConfig.prioritize_most_likely_candidate_pairs = true; |
| 141 | iceConfig.regather_on_failed_networks_interval = 8000; |
| 142 | _transportChannel->SetIceConfig(iceConfig); |
| 143 |
| 144 | cricket::IceParameters localIceParameters( |
| 145 | _localIceParameters.ufrag, |
| 146 | _localIceParameters.pwd, |
| 147 | false |
| 148 | ); |
| 149 |
| 150 | _transportChannel->SetIceParameters(localIceParameters); |
| 151 | _transportChannel->SetIceRole(_isOutgoing ? cricket::ICEROLE_CONTROLLING : cricket::ICEROLE_CONTROLLED); |
| 152 | _transportChannel->SetRemoteIceMode(cricket::ICEMODE_FULL); |
| 153 |
| 154 | _transportChannel->SignalCandidateGathered.connect(this, &NativeNetworkingImpl::candidateGathered); |
| 155 | _transportChannel->SignalIceTransportStateChanged.connect(this, &NativeNetworkingImpl::transportStateChanged); |
| 156 | _transportChannel->SignalReadPacket.connect(this, &NativeNetworkingImpl::transportPacketReceived); |
| 157 |
| 158 | webrtc::CryptoOptions cryptoOptions = NativeNetworkingImpl::getDefaulCryptoOptions(); |
| 159 | _dtlsTransport.reset(new cricket::DtlsTransport(_transportChannel.get(), cryptoOptions, nullptr)); |
| 160 |
| 161 | _dtlsTransport->SignalWritableState.connect( |
| 162 | this, &NativeNetworkingImpl::OnTransportWritableState_n); |
| 163 | _dtlsTransport->SignalReceivingState.connect( |
| 164 | this, &NativeNetworkingImpl::OnTransportReceivingState_n); |
| 165 |
| 166 | _dtlsTransport->SetLocalCertificate(_localCertificate); |
| 167 |
| 168 | _dtlsSrtpTransport->SetDtlsTransports(_dtlsTransport.get(), nullptr); |
| 169 | } |
| 170 |
| 171 | void NativeNetworkingImpl::start() { |
| 172 | _transportChannel->MaybeStartGathering(); |
| 173 |
| 174 | const auto weak = std::weak_ptr<NativeNetworkingImpl>(shared_from_this()); |
| 175 | _dataChannelInterface.reset(new SctpDataChannelProviderInterfaceImpl( |
| 176 | _dtlsTransport.get(), |
| 177 | _isOutgoing, |
| 178 | [weak, threads = _threads](bool state) { |
| 179 | assert(threads->getNetworkThread()->IsCurrent()); |
| 180 | const auto strong = weak.lock(); |
| 181 | if (!strong) { |
| 182 | return; |
| 183 | } |
| 184 | strong->_dataChannelStateUpdated(state); |
| 185 | }, |
| 186 | [weak, threads = _threads]() { |
| 187 | assert(threads->getNetworkThread()->IsCurrent()); |
| 188 | const auto strong = weak.lock(); |
| 189 | if (!strong) { |
| 190 | return; |
| 191 | } |
| 192 | //strong->restartDataChannel(); |
| 193 | }, |
| 194 | [weak, threads = _threads](std::string const &message) { |
| 195 | assert(threads->getNetworkThread()->IsCurrent()); |
| 196 | const auto strong = weak.lock(); |
| 197 | if (!strong) { |
| 198 | return; |
| 199 | } |
| 200 | strong->_dataChannelMessageReceived(message); |
| 201 | }, |
| 202 | _threads |
| 203 | )); |
| 204 | } |
| 205 |
| 206 | void NativeNetworkingImpl::stop() { |
| 207 | _transportChannel->SignalCandidateGathered.disconnect(this); |
| 208 | _transportChannel->SignalIceTransportStateChanged.disconnect(this); |
| 209 | _transportChannel->SignalReadPacket.disconnect(this); |
| 210 |
| 211 | _dtlsTransport->SignalWritableState.disconnect(this); |
| 212 | _dtlsTransport->SignalReceivingState.disconnect(this); |
| 213 |
| 214 | _dtlsSrtpTransport->SetDtlsTransports(nullptr, nullptr); |
| 215 |
| 216 | _dataChannelInterface.reset(); |
| 217 | _dtlsTransport.reset(); |
| 218 | _transportChannel.reset(); |
| 219 | _portAllocator.reset(); |
| 220 |
| 221 | _localIceParameters = PeerIceParameters(rtc::CreateRandomString(cricket::ICE_UFRAG_LENGTH), rtc::CreateRandomString(cricket::ICE_PWD_LENGTH)); |
| 222 |
| 223 | _localCertificate = rtc::RTCCertificateGenerator::GenerateCertificate(rtc::KeyParams(rtc::KT_ECDSA), absl::nullopt); |
| 224 |
| 225 | resetDtlsSrtpTransport(); |
| 226 | } |
| 227 |
| 228 | PeerIceParameters NativeNetworkingImpl::getLocalIceParameters() { |
| 229 | return _localIceParameters; |
| 230 | } |
| 231 |
| 232 | std::unique_ptr<rtc::SSLFingerprint> NativeNetworkingImpl::getLocalFingerprint() { |
| 233 | auto certificate = _localCertificate; |
| 234 | if (!certificate) { |
| 235 | return nullptr; |
| 236 | } |
| 237 | return rtc::SSLFingerprint::CreateFromCertificate(*certificate); |
| 238 | } |
| 239 |
| 240 | void NativeNetworkingImpl::setRemoteParams(PeerIceParameters const &remoteIceParameters, rtc::SSLFingerprint *fingerprint, std::string const &sslSetup) { |
| 241 | _remoteIceParameters = remoteIceParameters; |
| 242 |
| 243 | cricket::IceParameters parameters( |
| 244 | remoteIceParameters.ufrag, |
| 245 | remoteIceParameters.pwd, |
| 246 | false |
| 247 | ); |
| 248 |
| 249 | _transportChannel->SetRemoteIceParameters(parameters); |
| 250 |
| 251 | if (sslSetup == "active") { |
| 252 | _dtlsTransport->SetDtlsRole(rtc::SSLRole::SSL_SERVER); |
| 253 | } else if (sslSetup == "passive") { |
| 254 | _dtlsTransport->SetDtlsRole(rtc::SSLRole::SSL_CLIENT); |
| 255 | } else { |
| 256 | _dtlsTransport->SetDtlsRole(_isOutgoing ? rtc::SSLRole::SSL_CLIENT : rtc::SSLRole::SSL_SERVER); |
| 257 | } |
| 258 |
| 259 | if (fingerprint) { |
| 260 | _dtlsTransport->SetRemoteFingerprint(fingerprint->algorithm, fingerprint->digest.data(), fingerprint->digest.size()); |
| 261 | } |
| 262 | } |
| 263 |
| 264 | void NativeNetworkingImpl::addCandidates(std::vector<cricket::Candidate> const &candidates) { |
| 265 | for (const auto &candidate : candidates) { |
| 266 | _transportChannel->AddRemoteCandidate(candidate); |
| 267 | } |
| 268 | } |
| 269 |
| 270 | void NativeNetworkingImpl::sendDataChannelMessage(std::string const &message) { |
| 271 | if (_dataChannelInterface) { |
| 272 | _dataChannelInterface->sendDataChannelMessage(message); |
| 273 | } |
| 274 | } |
| 275 |
| 276 | webrtc::RtpTransport *NativeNetworkingImpl::getRtpTransport() { |
| 277 | return _dtlsSrtpTransport.get(); |
| 278 | } |
| 279 |
| 280 | void NativeNetworkingImpl::checkConnectionTimeout() { |
| 281 | const auto weak = std::weak_ptr<NativeNetworkingImpl>(shared_from_this()); |
| 282 | _threads->getNetworkThread()->PostDelayedTask(RTC_FROM_HERE, [weak]() { |
| 283 | auto strong = weak.lock(); |
| 284 | if (!strong) { |
| 285 | return; |
| 286 | } |
| 287 |
| 288 | int64_t currentTimestamp = rtc::TimeMillis(); |
| 289 | const int64_t maxTimeout = 20000; |
| 290 |
| 291 | if (strong->_lastNetworkActivityMs + maxTimeout < currentTimestamp) { |
| 292 | NativeNetworkingImpl::State emitState; |
| 293 | emitState.isReadyToSendData = false; |
| 294 | emitState.isFailed = true; |
| 295 | strong->_stateUpdated(emitState); |
| 296 | } |
| 297 |
| 298 | strong->checkConnectionTimeout(); |
| 299 | }, 1000); |
| 300 | } |
| 301 |
| 302 | void NativeNetworkingImpl::candidateGathered(cricket::IceTransportInternal *transport, const cricket::Candidate &candidate) { |
| 303 | assert(_threads->getNetworkThread()->IsCurrent()); |
| 304 |
| 305 | _candidateGathered(candidate); |
| 306 | } |
| 307 |
| 308 | void NativeNetworkingImpl::candidateGatheringState(cricket::IceTransportInternal *transport) { |
| 309 | assert(_threads->getNetworkThread()->IsCurrent()); |
| 310 | } |
| 311 |
| 312 | void NativeNetworkingImpl::OnTransportWritableState_n(rtc::PacketTransportInternal *transport) { |
| 313 | assert(_threads->getNetworkThread()->IsCurrent()); |
| 314 |
| 315 | UpdateAggregateStates_n(); |
| 316 | } |
| 317 | void NativeNetworkingImpl::OnTransportReceivingState_n(rtc::PacketTransportInternal *transport) { |
| 318 | assert(_threads->getNetworkThread()->IsCurrent()); |
| 319 |
| 320 | UpdateAggregateStates_n(); |
| 321 | } |
| 322 |
| 323 | void NativeNetworkingImpl::DtlsReadyToSend(bool isReadyToSend) { |
| 324 | UpdateAggregateStates_n(); |
| 325 |
| 326 | if (isReadyToSend) { |
| 327 | const auto weak = std::weak_ptr<NativeNetworkingImpl>(shared_from_this()); |
| 328 | _threads->getNetworkThread()->PostTask(RTC_FROM_HERE, [weak]() { |
| 329 | const auto strong = weak.lock(); |
| 330 | if (!strong) { |
| 331 | return; |
| 332 | } |
| 333 | strong->UpdateAggregateStates_n(); |
| 334 | }); |
| 335 | } |
| 336 | } |
| 337 |
| 338 | void NativeNetworkingImpl::transportStateChanged(cricket::IceTransportInternal *transport) { |
| 339 | UpdateAggregateStates_n(); |
| 340 | } |
| 341 |
| 342 | void NativeNetworkingImpl::transportReadyToSend(cricket::IceTransportInternal *transport) { |
| 343 | assert(_threads->getNetworkThread()->IsCurrent()); |
| 344 | } |
| 345 |
| 346 | void NativeNetworkingImpl::transportPacketReceived(rtc::PacketTransportInternal *transport, const char *bytes, size_t size, const int64_t ×tamp, int unused) { |
| 347 | assert(_threads->getNetworkThread()->IsCurrent()); |
| 348 |
| 349 | _lastNetworkActivityMs = rtc::TimeMillis(); |
| 350 | } |
| 351 |
| 352 | void NativeNetworkingImpl::RtpPacketReceived_n(rtc::CopyOnWriteBuffer *packet, int64_t packet_time_us, bool isUnresolved) { |
| 353 | if (_transportMessageReceived) { |
| 354 | _transportMessageReceived(*packet, isUnresolved); |
| 355 | } |
| 356 | } |
| 357 |
| 358 | void NativeNetworkingImpl::OnRtcpPacketReceived_n(rtc::CopyOnWriteBuffer *packet, int64_t packet_time_us) { |
| 359 | if (_rtcpPacketReceived) { |
| 360 | _rtcpPacketReceived(*packet, packet_time_us); |
| 361 | } |
| 362 | } |
| 363 |
| 364 | void NativeNetworkingImpl::UpdateAggregateStates_n() { |
| 365 | assert(_threads->getNetworkThread()->IsCurrent()); |
| 366 |
| 367 | auto state = _transportChannel->GetIceTransportState(); |
| 368 | bool isConnected = false; |
| 369 | switch (state) { |
| 370 | case webrtc::IceTransportState::kConnected: |
| 371 | case webrtc::IceTransportState::kCompleted: |
| 372 | isConnected = true; |
| 373 | break; |
| 374 | default: |
| 375 | break; |
| 376 | } |
| 377 |
| 378 | if (!_dtlsSrtpTransport->IsWritable(false)) { |
| 379 | isConnected = false; |
| 380 | } |
| 381 |
| 382 | if (_isConnected != isConnected) { |
| 383 | _isConnected = isConnected; |
| 384 |
| 385 | NativeNetworkingImpl::State emitState; |
| 386 | emitState.isReadyToSendData = isConnected; |
| 387 | _stateUpdated(emitState); |
| 388 |
| 389 | if (_dataChannelInterface) { |
| 390 | _dataChannelInterface->updateIsConnected(isConnected); |
| 391 | } |
| 392 | } |
| 393 | } |
| 394 |
| 395 | void NativeNetworkingImpl::sctpReadyToSendData() { |
| 396 | } |
| 397 |
| 398 | void NativeNetworkingImpl::sctpDataReceived(const cricket::ReceiveDataParams& params, const rtc::CopyOnWriteBuffer& buffer) { |
| 399 | } |
| 400 |
| 401 | } // namespace tgcalls |
| 402 |