make upnp lease duration configurable on the fly

This commit is contained in:
arvidn 2020-04-23 13:53:47 +02:00 committed by Arvid Norberg
parent a9968916ca
commit c537f6277f
3 changed files with 18 additions and 8 deletions

View File

@ -1,3 +1,4 @@
* allow UPnP lease duration to be changed after device discovery
* fix IPv6 address change detection on Windows * fix IPv6 address change detection on Windows
1.2.6 released 1.2.6 released

View File

@ -212,6 +212,8 @@ private:
void next(rootdevice& d, port_mapping_t i); void next(rootdevice& d, port_mapping_t i);
void update_map(rootdevice& d, port_mapping_t i); void update_map(rootdevice& d, port_mapping_t i);
int lease_duration(rootdevice const& d) const;
void connect(rootdevice& d); void connect(rootdevice& d);
void on_upnp_xml(error_code const& e void on_upnp_xml(error_code const& e
@ -288,8 +290,8 @@ private:
std::string path; std::string path;
address external_ip; address external_ip;
// default lease duration for a port map. // set to false if the router doesn't support lease durations
int lease_duration = 3600; bool use_lease_duration = true;
// true if the device supports specifying a // true if the device supports specifying a
// specific external port, false if it doesn't // specific external port, false if it doesn't

View File

@ -576,7 +576,6 @@ void upnp::on_reply(udp::socket& s, error_code const& ec)
rootdevice d; rootdevice d;
d.url = url; d.url = url;
d.lease_duration = m_settings.get_int(settings_pack::upnp_lease_duration);
auto i = m_devices.find(d); auto i = m_devices.find(d);
@ -724,6 +723,13 @@ void upnp::post(upnp::rootdevice const& d, char const* soap
#endif #endif
} }
int upnp::lease_duration(rootdevice const& d) const
{
return d.use_lease_duration
? m_settings.get_int(settings_pack::upnp_lease_duration)
: 0;
}
void upnp::create_port_mapping(http_connection& c, rootdevice& d void upnp::create_port_mapping(http_connection& c, rootdevice& d
, port_mapping_t const i) , port_mapping_t const i)
{ {
@ -765,7 +771,7 @@ void upnp::create_port_mapping(http_connection& c, rootdevice& d
, local_endpoint.c_str() , local_endpoint.c_str()
, m_settings.get_bool(settings_pack::anonymous_mode) , m_settings.get_bool(settings_pack::anonymous_mode)
? "" : m_settings.get_str(settings_pack::user_agent).c_str() ? "" : m_settings.get_str(settings_pack::user_agent).c_str()
, d.lease_duration, soap_action); , lease_duration(d), soap_action);
post(d, soap, soap_action); post(d, soap, soap_action);
} }
@ -1418,7 +1424,7 @@ void upnp::on_upnp_map_response(error_code const& e
if (s.error_code == 725) if (s.error_code == 725)
{ {
// The gateway only supports permanent leases // The gateway only supports permanent leases
d.lease_duration = 0; d.use_lease_duration = false;
m.act = portmap_action::add; m.act = portmap_action::add;
++m.failcount; ++m.failcount;
update_map(d, mapping); update_map(d, mapping);
@ -1456,11 +1462,12 @@ void upnp::on_upnp_map_response(error_code const& e
{ {
m_callback.on_port_mapping(mapping, d.external_ip, m.external_port, m.protocol, error_code() m_callback.on_port_mapping(mapping, d.external_ip, m.external_port, m.protocol, error_code()
, portmap_transport::upnp); , portmap_transport::upnp);
if (d.lease_duration > 0) if (d.use_lease_duration)
{ {
time_point const now = aux::time_now(); time_point const now = aux::time_now();
m.expires = now m.expires = now + seconds(
+ seconds(d.lease_duration * 3 / 4); m_settings.get_int(settings_pack::upnp_lease_duration) * 3 / 4);
time_point next_expire = m_refresh_timer.expires_at(); time_point next_expire = m_refresh_timer.expires_at();
if (next_expire < now || next_expire > m.expires) if (next_expire < now || next_expire > m.expires)
{ {