From 19651c3ef26b69e121dd20061e4752d61d514fd7 Mon Sep 17 00:00:00 2001 From: Jonas Oreland Date: Mon, 5 Feb 2018 10:41:41 +0100 Subject: [PATCH] Handle lifetime short than 2 minutes for TURN allocations This patch modifies behaviour when TurnPort gets a lifetime back from server that is shorter than 2 minutes. Before the patch such lifetime resulted in TurnPort not scheduling any refresh, leading to timeout on the turn allocation. After then patch lifetime shorter then 2 minutes leads to refresh after half stipulated lifetime. BUG=webrtc:8826 Change-Id: I80561100f2307bd9a6a91af0924bb2814102ddd3 Reviewed-on: https://webrtc-review.googlesource.com/46741 Commit-Queue: Jonas Oreland Reviewed-by: Taylor Brandstetter Cr-Commit-Position: refs/heads/master@{#21891} --- p2p/base/turnport.cc | 29 +++++++++++++++++++++++------ p2p/base/turnport.h | 2 +- 2 files changed, 24 insertions(+), 7 deletions(-) diff --git a/p2p/base/turnport.cc b/p2p/base/turnport.cc index 28b19dc290..334410ed11 100644 --- a/p2p/base/turnport.cc +++ b/p2p/base/turnport.cc @@ -976,15 +976,32 @@ void TurnPort::DispatchPacket(const char* data, size_t size, } } -bool TurnPort::ScheduleRefresh(int lifetime) { - // Lifetime is in seconds; we schedule a refresh for one minute less. +bool TurnPort::ScheduleRefresh(uint32_t lifetime) { + // Lifetime is in seconds, delay is in milliseconds. + int delay = 1 * 60 * 1000; + + // Cutoff lifetime bigger than 1h. + constexpr uint32_t max_lifetime = 60 * 60; + if (lifetime < 2 * 60) { - LOG_J(LS_WARNING, this) << "Received response with lifetime that was " - << "too short, lifetime=" << lifetime; - return false; + // The RFC does not mention a lower limit on lifetime. + // So if server sends a value less than 2 minutes, we schedule a refresh + // for half lifetime. + LOG_J(LS_WARNING, this) << "Received response with short lifetime=" + << lifetime << " seconds."; + delay = (lifetime * 1000) / 2; + } else if (lifetime > max_lifetime) { + // Make 1 hour largest delay, and then sce + // we schedule a refresh for one minute less than max lifetime. + LOG_J(LS_WARNING, this) << "Received response with long lifetime=" + << lifetime << " seconds."; + delay = (max_lifetime - 60) * 1000; + } else { + // Normal case, + // we schedule a refresh for one minute less than requested lifetime. + delay = (lifetime - 60) * 1000; } - int delay = (lifetime - 60) * 1000; SendRequest(new TurnRefreshRequest(this), delay); LOG_J(LS_INFO, this) << "Scheduled refresh in " << delay << "ms."; return true; diff --git a/p2p/base/turnport.h b/p2p/base/turnport.h index 4ee5f891fa..798afd31fd 100644 --- a/p2p/base/turnport.h +++ b/p2p/base/turnport.h @@ -260,7 +260,7 @@ class TurnPort : public Port { const rtc::SocketAddress& remote_addr, ProtocolType proto, const rtc::PacketTime& packet_time); - bool ScheduleRefresh(int lifetime); + bool ScheduleRefresh(uint32_t lifetime); void SendRequest(StunRequest* request, int delay); int Send(const void* data, size_t size, const rtc::PacketOptions& options);