Compare commits

...

7 Commits

Author SHA1 Message Date
Yat Ho 510a71e9a3
Merge 74ad24b410 into 821a6816ef 2024-04-24 02:04:10 +00:00
Yat Ho 74ad24b410 refactor: remove `UpnpState::Failed` 2024-04-24 10:04:06 +08:00
Yat Ho 9dfc4bbfde fix: recover from failed UPnP discovery 2024-04-24 10:04:06 +08:00
Yat Ho 4f4831a82b code review: explicitly list all states to start discovering from 2024-04-24 10:04:06 +08:00
Yat Ho effc776e1a chore: housekeeping 2024-04-24 10:04:06 +08:00
Yat Ho 1a5671efbd feat: allow natpmp to recover from errors 2024-04-24 10:04:06 +08:00
Yat Ho 62c8062bba feat: allow upnp to recover from errors 2024-04-24 10:04:06 +08:00
2 changed files with 10 additions and 20 deletions

View File

@ -140,16 +140,10 @@ tr_natpmp::PulseResult tr_natpmp::pulse(tr_port local_port, bool is_enabled)
} }
} }
if (state_ == State::Idle) if ((state_ == State::Idle || state_ == State::Err) &&
(is_mapped_ ? tr_time() >= renew_time_ : is_enabled && has_discovered_))
{ {
if (is_enabled && !is_mapped_ && has_discovered_) state_ = State::SendMap;
{
state_ = State::SendMap;
}
else if (is_mapped_ && tr_time() >= renew_time_)
{
state_ = State::SendMap;
}
} }
if (state_ == State::SendMap && canSendCommand()) if (state_ == State::SendMap && canSendCommand())

View File

@ -39,7 +39,6 @@ namespace
enum class UpnpState : uint8_t enum class UpnpState : uint8_t
{ {
Idle, Idle,
Failed,
WillDiscover, // next action is upnpDiscover() WillDiscover, // next action is upnpDiscover()
Discovering, // currently making blocking upnpDiscover() call in a worker thread Discovering, // currently making blocking upnpDiscover() call in a worker thread
WillMap, // next action is UPNP_AddPortMapping() WillMap, // next action is UPNP_AddPortMapping()
@ -58,9 +57,7 @@ struct tr_upnp
~tr_upnp() ~tr_upnp()
{ {
TR_ASSERT(!isMapped); TR_ASSERT(!isMapped);
TR_ASSERT( TR_ASSERT(state == UpnpState::Idle || state == UpnpState::WillDiscover || state == UpnpState::Discovering);
state == UpnpState::Idle || state == UpnpState::Failed || state == UpnpState::WillDiscover ||
state == UpnpState::Discovering);
FreeUPNPUrls(&urls); FreeUPNPUrls(&urls);
} }
@ -298,7 +295,7 @@ tr_port_forwarding_state tr_upnpPulse(
} }
else else
{ {
handle->state = UpnpState::Failed; handle->state = UpnpState::WillDiscover;
tr_logAddDebug(fmt::format("UPNP_GetValidIGD failed: {} ({})", tr_strerror(errno), errno)); tr_logAddDebug(fmt::format("UPNP_GetValidIGD failed: {} ({})", tr_strerror(errno), errno));
tr_logAddDebug("If your router supports UPnP, please make sure UPnP is enabled!"); tr_logAddDebug("If your router supports UPnP, please make sure UPnP is enabled!");
} }
@ -306,15 +303,15 @@ tr_port_forwarding_state tr_upnpPulse(
freeUPNPDevlist(devlist); freeUPNPDevlist(devlist);
} }
if ((handle->state == UpnpState::Idle) && (handle->isMapped) && if (handle->state == UpnpState::Idle && handle->isMapped &&
(!is_enabled || handle->advertised_port != advertised_port || handle->local_port != local_port)) (!is_enabled || handle->advertised_port != advertised_port || handle->local_port != local_port))
{ {
handle->state = UpnpState::WillUnmap; handle->state = UpnpState::WillUnmap;
} }
if (is_enabled && handle->isMapped && do_port_check && if (is_enabled && handle->isMapped && do_port_check &&
((get_specific_port_mapping_entry(handle, "TCP") != UPNPCOMMAND_SUCCESS) || (get_specific_port_mapping_entry(handle, "TCP") != UPNPCOMMAND_SUCCESS ||
(get_specific_port_mapping_entry(handle, "UDP") != UPNPCOMMAND_SUCCESS))) get_specific_port_mapping_entry(handle, "UDP") != UPNPCOMMAND_SUCCESS))
{ {
tr_logAddInfo(fmt::format( tr_logAddInfo(fmt::format(
_("Local port {local_port} is not forwarded to {advertised_port}"), _("Local port {local_port} is not forwarded to {advertised_port}"),
@ -339,7 +336,7 @@ tr_port_forwarding_state tr_upnpPulse(
handle->local_port = {}; handle->local_port = {};
} }
if ((handle->state == UpnpState::Idle) && is_enabled && !handle->isMapped) if (handle->state == UpnpState::Idle && is_enabled && !handle->isMapped)
{ {
handle->state = UpnpState::WillMap; handle->state = UpnpState::WillMap;
} }
@ -376,15 +373,14 @@ tr_port_forwarding_state tr_upnpPulse(
fmt::arg("advertised_port", advertised_port.host()))); fmt::arg("advertised_port", advertised_port.host())));
handle->advertised_port = advertised_port; handle->advertised_port = advertised_port;
handle->local_port = local_port; handle->local_port = local_port;
handle->state = UpnpState::Idle;
} }
else else
{ {
tr_logAddInfo(_("If your router supports UPnP, please make sure UPnP is enabled!")); tr_logAddInfo(_("If your router supports UPnP, please make sure UPnP is enabled!"));
handle->advertised_port = {}; handle->advertised_port = {};
handle->local_port = {}; handle->local_port = {};
handle->state = UpnpState::Failed;
} }
handle->state = UpnpState::Idle;
} }
return port_fwd_state(handle->state, handle->isMapped); return port_fwd_state(handle->state, handle->isMapped);