diff --git a/src/jamidht/p2p.cpp b/src/jamidht/p2p.cpp index fbf4e7f8af6ccf820e9df84e57aa42a6ba73afcc..b13954263aeb74d3de639b7ae882b481cd2cfe86 100644 --- a/src/jamidht/p2p.cpp +++ b/src/jamidht/p2p.cpp @@ -51,7 +51,7 @@ static constexpr std::chrono::seconds NET_CONNECTION_TIMEOUT{10}; static constexpr std::chrono::seconds SOCK_TIMEOUT{3}; static constexpr std::chrono::seconds ICE_READY_TIMEOUT{10}; static constexpr std::chrono::seconds ICE_INIT_TIMEOUT{10}; -static constexpr std::chrono::seconds ICE_NOGOTIATION_TIMEOUT{10}; +static constexpr std::chrono::seconds ICE_NEGOTIATION_TIMEOUT{10}; using Clock = std::chrono::system_clock; using ValueIdDist = std::uniform_int_distribution<dht::Value::Id>; @@ -293,8 +293,6 @@ private: std::future<void> loopFut_; // keep it last member - std::shared_ptr<IceTransport> ice_; - std::vector<std::thread> answer_threads_; }; @@ -439,7 +437,7 @@ private: break; } - ice->waitForNegotiation(ICE_NOGOTIATION_TIMEOUT); + ice->waitForNegotiation(ICE_NEGOTIATION_TIMEOUT); if (ice->isRunning()) { peer_ep = std::make_shared<IceSocketEndpoint>(ice, true); JAMI_DBG("[Account:%s] ICE negotiation succeed. Starting file transfer", @@ -475,6 +473,11 @@ private: } } + if (!peer_ep) { + cancel(); + return; + } + // Negotiate a TLS session JAMI_DBG() << parent_.account << "[CNX] start TLS session"; auto tls_ep = std::make_unique<TlsSocketEndpoint>( @@ -731,6 +734,7 @@ DhtPeerConnector::Impl::answerToRequest(PeerConnectionMsg&& request, bool ready {false}; }; auto iceReady = std::make_shared<IceReady>(); + std::shared_ptr<IceTransport> ice; for (auto& ip: request.addresses) { try { if (IpAddr(ip).isIpv4()) { @@ -761,32 +765,33 @@ DhtPeerConnector::Impl::answerToRequest(PeerConnectionMsg&& request, ir.ready = true; ir.cv.notify_one(); }; - ice_ = iceTransportFactory.createTransport(account.getAccountID().c_str(), 1, true, ice_config); + ice = iceTransportFactory.createTransport(account.getAccountID().c_str(), 1, true, ice_config); - if (ice_->waitForInitialization(ICE_INIT_TIMEOUT) <= 0) { + if (ice->waitForInitialization(ICE_INIT_TIMEOUT) <= 0) { JAMI_ERR("Cannot initialize ICE session."); continue; } - account.registerDhtAddress(*ice_); + account.registerDhtAddress(*ice); - auto sdp = parse_SDP(ip, *ice_); + auto sdp = parse_SDP(ip, *ice); // NOTE: hasPubIp is used for compability (because ICE is waiting for a certain state in old versions) // This can be removed when old versions will be unsupported (version before this patch) hasPubIp = hasPublicIp(sdp); - if (not ice_->start({sdp.rem_ufrag, sdp.rem_pwd}, sdp.rem_candidates)) { + if (not ice->start({sdp.rem_ufrag, sdp.rem_pwd}, sdp.rem_candidates)) { JAMI_WARN("[Account:%s] start ICE failed - fallback to TURN", account.getAccountID().c_str()); continue; } if (!hasPubIp) { - ice_->waitForNegotiation(ICE_NOGOTIATION_TIMEOUT); - if (ice_->isRunning()) { + ice->waitForNegotiation(ICE_NEGOTIATION_TIMEOUT); + if (ice->isRunning()) { sendIce = true; JAMI_DBG("[Account:%s] ICE negotiation succeed. Answering with local SDP", account.getAccountID().c_str()); } else { JAMI_WARN("[Account:%s] ICE negotation failed", account.getAccountID().c_str()); + ice->cancelOperations(); } } else sendIce = true; // Ice started with success, we can use it. @@ -801,11 +806,11 @@ DhtPeerConnector::Impl::answerToRequest(PeerConnectionMsg&& request, if (sendIce) { // NOTE: This is a shortest version of a real SDP message to save some bits - auto iceAttributes = ice_->getLocalAttributes(); + auto iceAttributes = ice->getLocalAttributes(); std::stringstream icemsg; icemsg << iceAttributes.ufrag << "\n"; icemsg << iceAttributes.pwd << "\n"; - for (const auto &addr : ice_->getLocalCandidates(0)) { + for (const auto &addr : ice->getLocalCandidates(0)) { icemsg << addr << "\n"; } addresses = {icemsg.str()}; @@ -837,8 +842,8 @@ DhtPeerConnector::Impl::answerToRequest(PeerConnectionMsg&& request, if (sendIce) { if (hasPubIp) { - ice_->waitForNegotiation(ICE_NOGOTIATION_TIMEOUT); - if (ice_->isRunning()) { + ice->waitForNegotiation(ICE_NEGOTIATION_TIMEOUT); + if (ice->isRunning()) { JAMI_DBG("[Account:%s] ICE negotiation succeed. Answering with local SDP", account.getAccountID().c_str()); } else { JAMI_WARN("[Account:%s] ICE negotation failed - Fallbacking to TURN", account.getAccountID().c_str()); @@ -847,7 +852,7 @@ DhtPeerConnector::Impl::answerToRequest(PeerConnectionMsg&& request, } if (not iceReady->ready) { - if (!hasPubIp) ice_->setSlaveSession(); + if (!hasPubIp) ice->setSlaveSession(); std::unique_lock<std::mutex> lk {iceReady->mtx}; if (not iceReady->cv.wait_for(lk, ICE_READY_TIMEOUT, [&]{ return iceReady->ready; })) { // This will fallback on TURN if ICE is not ready @@ -856,10 +861,10 @@ DhtPeerConnector::Impl::answerToRequest(PeerConnectionMsg&& request, } std::unique_ptr<AbstractSocketEndpoint> peer_ep = - std::make_unique<IceSocketEndpoint>(ice_, false); + std::make_unique<IceSocketEndpoint>(ice, false); JAMI_DBG() << account << "[CNX] start TLS session"; auto ph = peer_h; - if (hasPubIp) ice_->setSlaveSession(); + if (hasPubIp) ice->setSlaveSession(); auto tls_ep = std::make_unique<TlsSocketEndpoint>( *peer_ep, account.identity(), account.dhParams(), [&, this](const dht::crypto::Certificate &cert) { @@ -880,9 +885,9 @@ DhtPeerConnector::Impl::answerToRequest(PeerConnectionMsg&& request, std::move(tls_ep)); connection->attachOutputStream(std::make_shared<FtpServer>( account.getAccountID(), peer_h.toString())); - servers_.emplace(std::make_pair(peer_h, ice_->getRemoteAddress(0)), + servers_.emplace(std::make_pair(peer_h, ice->getRemoteAddress(0)), std::move(connection)); - p2pEndpoints_.emplace(std::make_pair(peer_h, ice_->getRemoteAddress(0)), + p2pEndpoints_.emplace(std::make_pair(peer_h, ice->getRemoteAddress(0)), std::move(peer_ep)); } // Now wait for a TURN connection from peer (see onTurnPeerConnection) if fallbacking