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);