2022-01-20 18:27:56 +00:00
|
|
|
// This file Copyright © 2008-2022 Mnemosyne LLC.
|
2022-02-07 16:25:02 +00:00
|
|
|
// It may be used under GPLv2 (SPDX: GPL-2.0-only), GPLv3 (SPDX: GPL-3.0-only),
|
2022-01-20 18:27:56 +00:00
|
|
|
// or any future license endorsed by Mnemosyne LLC.
|
|
|
|
// License text can be found in the licenses/ folder.
|
2007-01-19 04:42:31 +00:00
|
|
|
|
2021-09-19 20:41:35 +00:00
|
|
|
#include <algorithm>
|
2022-08-11 17:28:37 +00:00
|
|
|
#include <chrono>
|
2022-08-17 16:08:36 +00:00
|
|
|
#include <memory>
|
2010-05-19 16:17:51 +00:00
|
|
|
|
2022-03-15 14:52:16 +00:00
|
|
|
#include <fmt/core.h>
|
|
|
|
|
2007-07-09 20:10:42 +00:00
|
|
|
#include "transmission.h"
|
2012-02-04 01:28:15 +00:00
|
|
|
#include "natpmp_local.h"
|
2013-01-25 23:34:20 +00:00
|
|
|
#include "log.h"
|
2007-07-09 20:10:42 +00:00
|
|
|
#include "net.h"
|
2007-09-20 16:32:01 +00:00
|
|
|
#include "peer-mgr.h"
|
2008-04-11 17:01:13 +00:00
|
|
|
#include "port-forwarding.h"
|
2009-07-01 14:58:57 +00:00
|
|
|
#include "session.h"
|
2007-12-25 05:37:32 +00:00
|
|
|
#include "torrent.h"
|
2017-06-08 07:24:12 +00:00
|
|
|
#include "tr-assert.h"
|
2007-07-09 20:10:42 +00:00
|
|
|
#include "upnp.h"
|
2022-08-11 17:28:37 +00:00
|
|
|
#include "utils.h" // for _()
|
|
|
|
|
|
|
|
using namespace std::literals;
|
2007-01-19 04:42:31 +00:00
|
|
|
|
2007-09-20 16:32:01 +00:00
|
|
|
struct tr_shared
|
2007-01-19 04:42:31 +00:00
|
|
|
{
|
2022-08-13 17:11:07 +00:00
|
|
|
explicit tr_shared(tr_session& session_in)
|
2022-08-11 17:28:37 +00:00
|
|
|
: session{ session_in }
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2022-08-12 00:59:58 +00:00
|
|
|
tr_session& session;
|
2008-04-12 21:47:10 +00:00
|
|
|
|
2022-08-11 17:28:37 +00:00
|
|
|
bool isEnabled = false;
|
|
|
|
bool isShuttingDown = false;
|
|
|
|
bool doPortCheck = false;
|
2007-01-19 04:42:31 +00:00
|
|
|
|
2022-08-11 17:28:37 +00:00
|
|
|
tr_port_forwarding natpmpStatus = TR_PORT_UNMAPPED;
|
|
|
|
tr_port_forwarding upnpStatus = TR_PORT_UNMAPPED;
|
2009-05-14 13:42:29 +00:00
|
|
|
|
2022-08-11 17:28:37 +00:00
|
|
|
tr_upnp* upnp = nullptr;
|
2022-08-12 00:59:58 +00:00
|
|
|
std::unique_ptr<tr_natpmp> natpmp;
|
2022-08-11 17:28:37 +00:00
|
|
|
|
|
|
|
std::unique_ptr<libtransmission::Timer> timer;
|
2007-01-19 04:42:31 +00:00
|
|
|
};
|
|
|
|
|
2007-12-08 19:34:15 +00:00
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
2007-01-19 04:42:31 +00:00
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
static char const* getNatStateStr(int state)
|
2007-01-19 04:42:31 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
switch (state)
|
2007-12-08 19:34:15 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
case TR_PORT_MAPPING:
|
|
|
|
return _("Starting");
|
|
|
|
|
|
|
|
case TR_PORT_MAPPED:
|
|
|
|
return _("Forwarded");
|
|
|
|
|
|
|
|
case TR_PORT_UNMAPPING:
|
|
|
|
return _("Stopping");
|
|
|
|
|
|
|
|
case TR_PORT_UNMAPPED:
|
|
|
|
return _("Not forwarded");
|
|
|
|
|
|
|
|
default:
|
|
|
|
return "???";
|
2007-12-08 19:34:15 +00:00
|
|
|
}
|
2007-01-19 04:42:31 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
static void natPulse(tr_shared* s, bool do_check)
|
2007-01-19 04:42:31 +00:00
|
|
|
{
|
2022-08-12 00:59:58 +00:00
|
|
|
auto& session = s->session;
|
|
|
|
tr_port const private_peer_port = session.private_peer_port;
|
2017-04-30 16:25:26 +00:00
|
|
|
bool const is_enabled = s->isEnabled && !s->isShuttingDown;
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2022-08-12 00:59:58 +00:00
|
|
|
if (!s->natpmp)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-08-12 00:59:58 +00:00
|
|
|
s->natpmp = std::make_unique<tr_natpmp>();
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2009-05-26 20:52:08 +00:00
|
|
|
|
2021-09-15 00:18:09 +00:00
|
|
|
if (s->upnp == nullptr)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
s->upnp = tr_upnpInit();
|
|
|
|
}
|
2010-07-05 21:04:17 +00:00
|
|
|
|
2021-10-22 13:51:36 +00:00
|
|
|
auto const old_status = tr_sharedTraversalStatus(s);
|
2010-07-05 21:04:17 +00:00
|
|
|
|
2021-10-22 13:51:36 +00:00
|
|
|
auto public_peer_port = tr_port{};
|
2022-01-23 05:41:01 +00:00
|
|
|
auto received_private_port = tr_port{};
|
2022-08-12 00:59:58 +00:00
|
|
|
s->natpmpStatus = s->natpmp->pulse(private_peer_port, is_enabled, &public_peer_port, &received_private_port);
|
2010-07-05 21:04:17 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (s->natpmpStatus == TR_PORT_MAPPED)
|
|
|
|
{
|
2022-08-12 00:59:58 +00:00
|
|
|
session.public_peer_port = public_peer_port;
|
|
|
|
session.private_peer_port = received_private_port;
|
2022-03-17 22:39:06 +00:00
|
|
|
tr_logAddInfo(fmt::format(
|
2022-03-22 16:45:56 +00:00
|
|
|
_("Mapped private port {private_port} to public port {public_port}"),
|
2022-08-12 00:59:58 +00:00
|
|
|
fmt::arg("public_port", session.public_peer_port.host()),
|
|
|
|
fmt::arg("private_port", session.private_peer_port.host())));
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2008-04-12 21:47:10 +00:00
|
|
|
|
2022-08-12 00:59:58 +00:00
|
|
|
s->upnpStatus = tr_upnpPulse(s->upnp, private_peer_port, is_enabled, do_check, session.bind_ipv4.readable());
|
2013-01-24 23:59:52 +00:00
|
|
|
|
2021-10-22 13:51:36 +00:00
|
|
|
auto const new_status = tr_sharedTraversalStatus(s);
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2021-10-22 13:51:36 +00:00
|
|
|
if (new_status != old_status)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-03-17 22:39:06 +00:00
|
|
|
tr_logAddInfo(fmt::format(
|
2022-04-14 04:19:18 +00:00
|
|
|
_("State changed from '{old_state}' to '{state}'"),
|
2022-03-17 22:39:06 +00:00
|
|
|
fmt::arg("old_state", getNatStateStr(old_status)),
|
|
|
|
fmt::arg("state", getNatStateStr(new_status))));
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2007-01-19 04:42:31 +00:00
|
|
|
}
|
|
|
|
|
2022-08-11 17:28:37 +00:00
|
|
|
static void restartTimer(tr_shared* s)
|
2007-01-19 04:42:31 +00:00
|
|
|
{
|
2022-08-11 17:28:37 +00:00
|
|
|
auto& timer = s->timer;
|
|
|
|
if (!timer)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2007-09-21 05:31:29 +00:00
|
|
|
|
2022-08-11 17:28:37 +00:00
|
|
|
// when to wake up again
|
2017-04-19 12:04:45 +00:00
|
|
|
switch (tr_sharedTraversalStatus(s))
|
2008-12-15 00:17:08 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
case TR_PORT_MAPPED:
|
2022-08-11 17:28:37 +00:00
|
|
|
// if we're mapped, everything is fine... check back at `renew_time`
|
|
|
|
// to renew the port forwarding if it's expired
|
2013-01-24 23:59:52 +00:00
|
|
|
s->doPortCheck = true;
|
2022-08-12 00:59:58 +00:00
|
|
|
if (auto const now = tr_time(); s->natpmp->renewTime() > now)
|
2022-08-11 17:28:37 +00:00
|
|
|
{
|
2022-08-12 00:59:58 +00:00
|
|
|
timer->startSingleShot(std::chrono::seconds{ s->natpmp->renewTime() - now });
|
2022-08-11 17:28:37 +00:00
|
|
|
}
|
|
|
|
else // ???
|
|
|
|
{
|
|
|
|
timer->startSingleShot(1min);
|
|
|
|
}
|
2013-01-24 23:59:52 +00:00
|
|
|
break;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case TR_PORT_ERROR:
|
2022-08-11 17:28:37 +00:00
|
|
|
// some kind of an error. wait a minute and retry
|
|
|
|
timer->startSingleShot(1min);
|
2013-01-24 23:59:52 +00:00
|
|
|
break;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
default:
|
2022-08-11 17:28:37 +00:00
|
|
|
// in progress. pulse frequently.
|
|
|
|
timer->startSingleShot(333ms);
|
2013-01-24 23:59:52 +00:00
|
|
|
break;
|
2007-01-19 04:42:31 +00:00
|
|
|
}
|
2009-04-22 16:00:45 +00:00
|
|
|
}
|
|
|
|
|
2022-08-11 17:28:37 +00:00
|
|
|
static void onTimer(void* vshared)
|
2009-10-29 17:25:03 +00:00
|
|
|
{
|
2021-09-12 17:41:49 +00:00
|
|
|
auto* s = static_cast<tr_shared*>(vshared);
|
2009-10-29 17:25:03 +00:00
|
|
|
|
2021-09-15 00:18:09 +00:00
|
|
|
TR_ASSERT(s != nullptr);
|
2022-08-11 17:28:37 +00:00
|
|
|
TR_ASSERT(s->timer);
|
2009-10-29 17:25:03 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
/* do something */
|
|
|
|
natPulse(s, s->doPortCheck);
|
|
|
|
s->doPortCheck = false;
|
2009-10-29 17:25:03 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
/* set up the timer for the next pulse */
|
2022-08-11 17:28:37 +00:00
|
|
|
restartTimer(s);
|
2009-10-29 17:25:03 +00:00
|
|
|
}
|
|
|
|
|
2007-12-08 19:34:15 +00:00
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
2007-01-19 04:42:31 +00:00
|
|
|
|
2022-08-12 00:59:58 +00:00
|
|
|
tr_shared* tr_sharedInit(tr_session& session)
|
2007-01-19 04:42:31 +00:00
|
|
|
{
|
2022-08-11 17:28:37 +00:00
|
|
|
return new tr_shared{ session };
|
2007-12-08 19:34:15 +00:00
|
|
|
}
|
2007-09-20 16:32:01 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
static void stop_timer(tr_shared* s)
|
2007-12-08 19:34:15 +00:00
|
|
|
{
|
2022-08-11 17:28:37 +00:00
|
|
|
s->timer.reset();
|
2007-01-19 04:42:31 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
static void stop_forwarding(tr_shared* s)
|
2007-01-27 21:17:10 +00:00
|
|
|
{
|
2022-03-17 22:39:06 +00:00
|
|
|
tr_logAddTrace("stopped");
|
2017-04-19 12:04:45 +00:00
|
|
|
natPulse(s, false);
|
2010-02-11 14:28:40 +00:00
|
|
|
|
2022-08-12 00:59:58 +00:00
|
|
|
s->natpmp.reset();
|
2017-04-19 12:04:45 +00:00
|
|
|
s->natpmpStatus = TR_PORT_UNMAPPED;
|
2010-02-11 14:28:40 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_upnpClose(s->upnp);
|
2021-09-15 00:18:09 +00:00
|
|
|
s->upnp = nullptr;
|
2017-04-19 12:04:45 +00:00
|
|
|
s->upnpStatus = TR_PORT_UNMAPPED;
|
2010-02-11 14:28:40 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
stop_timer(s);
|
2009-05-14 13:42:29 +00:00
|
|
|
}
|
2007-01-27 21:17:10 +00:00
|
|
|
|
2022-08-12 00:59:58 +00:00
|
|
|
void tr_sharedClose(tr_session& session)
|
2009-05-14 13:42:29 +00:00
|
|
|
{
|
2022-08-12 00:59:58 +00:00
|
|
|
tr_shared* shared = session.shared;
|
2007-01-27 21:17:10 +00:00
|
|
|
|
2022-08-11 17:28:37 +00:00
|
|
|
shared->isShuttingDown = true;
|
|
|
|
stop_forwarding(shared);
|
2022-08-12 00:59:58 +00:00
|
|
|
shared->session.shared = nullptr;
|
2022-08-11 17:28:37 +00:00
|
|
|
delete shared;
|
2007-01-27 21:17:10 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
static void start_timer(tr_shared* s)
|
2009-05-20 17:35:41 +00:00
|
|
|
{
|
2022-08-12 00:59:58 +00:00
|
|
|
s->timer = s->session.timerMaker().create(onTimer, s);
|
2022-08-11 17:28:37 +00:00
|
|
|
restartTimer(s);
|
2009-05-20 17:35:41 +00:00
|
|
|
}
|
|
|
|
|
2022-08-03 06:15:37 +00:00
|
|
|
void tr_sharedTraversalEnable(tr_shared* s, bool is_enable)
|
2007-09-20 16:32:01 +00:00
|
|
|
{
|
2022-08-03 06:15:37 +00:00
|
|
|
if (is_enable)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2020-08-27 23:41:26 +00:00
|
|
|
s->isEnabled = true;
|
2017-04-19 12:04:45 +00:00
|
|
|
start_timer(s);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-08-27 23:41:26 +00:00
|
|
|
s->isEnabled = false;
|
2017-04-19 12:04:45 +00:00
|
|
|
stop_forwarding(s);
|
|
|
|
}
|
2007-12-08 19:34:15 +00:00
|
|
|
}
|
2007-01-19 04:42:31 +00:00
|
|
|
|
2022-08-12 00:59:58 +00:00
|
|
|
void tr_sharedPortChanged(tr_session& session)
|
2007-12-08 19:34:15 +00:00
|
|
|
{
|
2022-08-12 00:59:58 +00:00
|
|
|
auto* const s = session.shared;
|
2009-05-14 13:42:29 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (s->isEnabled)
|
2009-05-14 13:42:29 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
stop_timer(s);
|
|
|
|
natPulse(s, false);
|
|
|
|
start_timer(s);
|
2009-05-14 13:42:29 +00:00
|
|
|
}
|
2007-12-08 19:34:15 +00:00
|
|
|
}
|
2007-01-19 04:42:31 +00:00
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
bool tr_sharedTraversalIsEnabled(tr_shared const* s)
|
2008-05-12 16:33:17 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
return s->isEnabled;
|
2008-05-12 16:33:17 +00:00
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
int tr_sharedTraversalStatus(tr_shared const* s)
|
2007-12-08 19:34:15 +00:00
|
|
|
{
|
2021-09-19 20:41:35 +00:00
|
|
|
return std::max(s->natpmpStatus, s->upnpStatus);
|
2007-01-19 04:42:31 +00:00
|
|
|
}
|