2023-02-11 20:49:42 +00:00
|
|
|
// This file Copyright © 2007-2023 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.
|
2006-09-25 18:37:45 +00:00
|
|
|
|
2022-08-25 01:19:21 +00:00
|
|
|
#include <array>
|
2021-10-17 20:17:18 +00:00
|
|
|
#include <cerrno>
|
2007-07-29 18:11:21 +00:00
|
|
|
|
2023-07-08 15:24:03 +00:00
|
|
|
#ifdef _WIN32
|
|
|
|
#include <ws2tcpip.h>
|
|
|
|
#else
|
|
|
|
#include <sys/socket.h> // AF_INET
|
|
|
|
#endif
|
|
|
|
|
2017-04-21 07:40:57 +00:00
|
|
|
#include <event2/util.h> /* evutil_inet_ntop() */
|
2011-03-24 22:57:39 +00:00
|
|
|
|
2022-03-14 04:43:35 +00:00
|
|
|
#include <fmt/core.h>
|
|
|
|
|
2008-07-15 03:26:53 +00:00
|
|
|
#define ENABLE_STRNATPMPERR
|
2023-05-20 04:17:24 +00:00
|
|
|
#include "natpmp.h"
|
2007-07-14 16:29:21 +00:00
|
|
|
|
2022-09-30 13:59:10 +00:00
|
|
|
#define LIBTRANSMISSION_PORT_FORWARDING_MODULE
|
|
|
|
|
2023-04-14 19:33:23 +00:00
|
|
|
#include "libtransmission/transmission.h"
|
2022-09-30 13:59:10 +00:00
|
|
|
|
2023-04-14 19:33:23 +00:00
|
|
|
#include "libtransmission/log.h"
|
2023-07-08 15:24:03 +00:00
|
|
|
#include "libtransmission/net.h"
|
2023-04-14 19:33:23 +00:00
|
|
|
#include "libtransmission/port-forwarding-natpmp.h"
|
|
|
|
#include "libtransmission/utils.h"
|
2006-09-25 18:37:45 +00:00
|
|
|
|
2023-01-07 22:55:00 +00:00
|
|
|
namespace
|
|
|
|
{
|
|
|
|
void log_val(char const* func, int ret)
|
2006-09-25 18:37:45 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
if (ret == NATPMP_TRYAGAIN)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2008-04-14 20:06:37 +00:00
|
|
|
return;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
if (ret >= 0)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-03-17 22:39:06 +00:00
|
|
|
tr_logAddDebug(fmt::format("{} succeeded ({})", func, ret));
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2006-09-25 18:37:45 +00:00
|
|
|
else
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-03-17 22:39:06 +00:00
|
|
|
tr_logAddDebug(fmt::format(
|
|
|
|
"{} failed. Natpmp returned {} ({}); errno is {} ({})",
|
|
|
|
func,
|
|
|
|
ret,
|
|
|
|
strnatpmperr(ret),
|
|
|
|
errno,
|
|
|
|
tr_strerror(errno)));
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2006-09-25 18:37:45 +00:00
|
|
|
}
|
2023-01-07 22:55:00 +00:00
|
|
|
} // namespace
|
2006-09-25 18:37:45 +00:00
|
|
|
|
2022-08-12 00:59:58 +00:00
|
|
|
bool tr_natpmp::canSendCommand() const
|
2007-12-13 19:28:51 +00:00
|
|
|
{
|
2022-08-12 00:59:58 +00:00
|
|
|
return tr_time() >= command_time_;
|
2007-12-13 19:28:51 +00:00
|
|
|
}
|
|
|
|
|
2022-08-12 00:59:58 +00:00
|
|
|
void tr_natpmp::setCommandTime()
|
2007-12-13 19:28:51 +00:00
|
|
|
{
|
2022-08-12 00:59:58 +00:00
|
|
|
command_time_ = tr_time() + CommandWaitSecs;
|
2007-12-13 19:28:51 +00:00
|
|
|
}
|
|
|
|
|
2022-11-02 00:32:26 +00:00
|
|
|
tr_natpmp::PulseResult tr_natpmp::pulse(tr_port local_port, bool is_enabled)
|
2006-09-25 18:37:45 +00:00
|
|
|
{
|
2022-09-30 13:59:10 +00:00
|
|
|
if (is_enabled && state_ == State::Discover)
|
2007-12-13 18:56:22 +00:00
|
|
|
{
|
2022-08-12 00:59:58 +00:00
|
|
|
int val = initnatpmp(&natpmp_, 0, 0);
|
2023-01-07 22:55:00 +00:00
|
|
|
log_val("initnatpmp", val);
|
2022-08-12 00:59:58 +00:00
|
|
|
val = sendpublicaddressrequest(&natpmp_);
|
2023-01-07 22:55:00 +00:00
|
|
|
log_val("sendpublicaddressrequest", val);
|
2022-09-30 13:59:10 +00:00
|
|
|
state_ = val < 0 ? State::Err : State::RecvPub;
|
2022-08-12 00:59:58 +00:00
|
|
|
has_discovered_ = true;
|
|
|
|
setCommandTime();
|
2007-12-13 18:56:22 +00:00
|
|
|
}
|
|
|
|
|
2022-09-30 13:59:10 +00:00
|
|
|
if (state_ == State::RecvPub && canSendCommand())
|
2006-09-25 18:37:45 +00:00
|
|
|
{
|
2007-12-08 19:34:15 +00:00
|
|
|
natpmpresp_t response;
|
2022-09-30 13:59:10 +00:00
|
|
|
auto const val = readnatpmpresponseorretry(&natpmp_, &response);
|
2023-01-07 22:55:00 +00:00
|
|
|
log_val("readnatpmpresponseorretry", val);
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
if (val >= 0)
|
2008-09-23 19:11:04 +00:00
|
|
|
{
|
2022-08-25 01:19:21 +00:00
|
|
|
auto str = std::array<char, 128>{};
|
|
|
|
evutil_inet_ntop(AF_INET, &response.pnu.publicaddress.addr, std::data(str), std::size(str));
|
|
|
|
tr_logAddInfo(fmt::format(_("Found public address '{address}'"), fmt::arg("address", std::data(str))));
|
2022-09-30 13:59:10 +00:00
|
|
|
state_ = State::Idle;
|
2008-09-23 19:11:04 +00:00
|
|
|
}
|
2012-12-05 17:29:46 +00:00
|
|
|
else if (val != NATPMP_TRYAGAIN)
|
2008-09-23 19:11:04 +00:00
|
|
|
{
|
2022-09-30 13:59:10 +00:00
|
|
|
state_ = State::Err;
|
2006-09-25 18:37:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-02 00:32:26 +00:00
|
|
|
if ((state_ == State::Idle || state_ == State::Err) && is_mapped_ && (!is_enabled || local_port_ != local_port))
|
2007-02-06 03:24:55 +00:00
|
|
|
{
|
2022-09-30 13:59:10 +00:00
|
|
|
state_ = State::SendUnmap;
|
2007-02-06 03:24:55 +00:00
|
|
|
}
|
2006-09-25 18:37:45 +00:00
|
|
|
|
2022-09-30 13:59:10 +00:00
|
|
|
if (state_ == State::SendUnmap && canSendCommand())
|
2006-09-25 18:37:45 +00:00
|
|
|
{
|
2022-11-02 00:32:26 +00:00
|
|
|
auto const val = sendnewportmappingrequest(
|
|
|
|
&natpmp_,
|
|
|
|
NATPMP_PROTOCOL_TCP,
|
|
|
|
local_port_.host(),
|
|
|
|
advertised_port_.host(),
|
|
|
|
0);
|
2023-01-07 22:55:00 +00:00
|
|
|
log_val("sendnewportmappingrequest", val);
|
2022-09-30 13:59:10 +00:00
|
|
|
state_ = val < 0 ? State::Err : State::RecvUnmap;
|
2022-08-12 00:59:58 +00:00
|
|
|
setCommandTime();
|
2006-09-25 18:37:45 +00:00
|
|
|
}
|
|
|
|
|
2022-09-30 13:59:10 +00:00
|
|
|
if (state_ == State::RecvUnmap)
|
2006-09-25 18:37:45 +00:00
|
|
|
{
|
2022-09-30 13:59:10 +00:00
|
|
|
auto resp = natpmpresp_t{};
|
|
|
|
auto const val = readnatpmpresponseorretry(&natpmp_, &resp);
|
2023-01-07 22:55:00 +00:00
|
|
|
log_val("readnatpmpresponseorretry", val);
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
if (val >= 0)
|
2008-09-23 19:11:04 +00:00
|
|
|
{
|
2023-08-18 03:13:01 +00:00
|
|
|
auto const unmapped_port = tr_port::from_host(resp.pnu.newportmapping.privateport);
|
2010-07-05 21:04:17 +00:00
|
|
|
|
2022-04-21 15:58:13 +00:00
|
|
|
tr_logAddInfo(fmt::format(_("Port {port} is no longer forwarded"), fmt::arg("port", unmapped_port.host())));
|
2010-07-05 21:04:17 +00:00
|
|
|
|
2022-11-02 00:32:26 +00:00
|
|
|
if (local_port_ == unmapped_port)
|
2008-11-01 16:39:57 +00:00
|
|
|
{
|
2022-11-02 00:32:26 +00:00
|
|
|
local_port_.clear();
|
|
|
|
advertised_port_.clear();
|
2022-09-30 13:59:10 +00:00
|
|
|
state_ = State::Idle;
|
2022-08-12 00:59:58 +00:00
|
|
|
is_mapped_ = false;
|
2008-11-01 16:39:57 +00:00
|
|
|
}
|
2008-09-23 19:11:04 +00:00
|
|
|
}
|
2012-12-05 17:29:46 +00:00
|
|
|
else if (val != NATPMP_TRYAGAIN)
|
2008-09-23 19:11:04 +00:00
|
|
|
{
|
2022-09-30 13:59:10 +00:00
|
|
|
state_ = State::Err;
|
2007-02-06 03:24:55 +00:00
|
|
|
}
|
2007-01-28 08:54:19 +00:00
|
|
|
}
|
2007-02-06 03:24:55 +00:00
|
|
|
|
2022-09-30 13:59:10 +00:00
|
|
|
if (state_ == State::Idle)
|
2006-09-25 18:37:45 +00:00
|
|
|
{
|
2022-08-12 00:59:58 +00:00
|
|
|
if (is_enabled && !is_mapped_ && has_discovered_)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-09-30 13:59:10 +00:00
|
|
|
state_ = State::SendMap;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2022-08-12 00:59:58 +00:00
|
|
|
else if (is_mapped_ && tr_time() >= renew_time_)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-09-30 13:59:10 +00:00
|
|
|
state_ = State::SendMap;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2006-09-25 18:37:45 +00:00
|
|
|
}
|
2007-02-06 03:24:55 +00:00
|
|
|
|
2022-09-30 13:59:10 +00:00
|
|
|
if (state_ == State::SendMap && canSendCommand())
|
2006-09-25 18:37:45 +00:00
|
|
|
{
|
2022-09-30 13:59:10 +00:00
|
|
|
auto const val = sendnewportmappingrequest(
|
2022-08-12 00:59:58 +00:00
|
|
|
&natpmp_,
|
2022-04-21 15:58:13 +00:00
|
|
|
NATPMP_PROTOCOL_TCP,
|
2022-11-02 00:32:26 +00:00
|
|
|
local_port.host(),
|
|
|
|
local_port.host(),
|
2022-04-21 15:58:13 +00:00
|
|
|
LifetimeSecs);
|
2023-01-07 22:55:00 +00:00
|
|
|
log_val("sendnewportmappingrequest", val);
|
2022-09-30 13:59:10 +00:00
|
|
|
state_ = val < 0 ? State::Err : State::RecvMap;
|
2022-08-12 00:59:58 +00:00
|
|
|
setCommandTime();
|
2006-09-25 18:37:45 +00:00
|
|
|
}
|
|
|
|
|
2022-09-30 13:59:10 +00:00
|
|
|
if (state_ == State::RecvMap)
|
2006-09-25 18:37:45 +00:00
|
|
|
{
|
2022-09-30 13:59:10 +00:00
|
|
|
auto resp = natpmpresp_t{};
|
|
|
|
auto const val = readnatpmpresponseorretry(&natpmp_, &resp);
|
2023-01-07 22:55:00 +00:00
|
|
|
log_val("readnatpmpresponseorretry", val);
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
if (val >= 0)
|
2008-09-23 19:11:04 +00:00
|
|
|
{
|
2022-09-30 13:59:10 +00:00
|
|
|
state_ = State::Idle;
|
2022-08-12 00:59:58 +00:00
|
|
|
is_mapped_ = true;
|
|
|
|
renew_time_ = tr_time() + (resp.pnu.newportmapping.lifetime / 2);
|
2023-08-18 03:13:01 +00:00
|
|
|
local_port_ = tr_port::from_host(resp.pnu.newportmapping.privateport);
|
|
|
|
advertised_port_ = tr_port::from_host(resp.pnu.newportmapping.mappedpublicport);
|
2022-11-02 00:32:26 +00:00
|
|
|
tr_logAddInfo(fmt::format(_("Port {port} forwarded successfully"), fmt::arg("port", local_port_.host())));
|
2008-09-23 19:11:04 +00:00
|
|
|
}
|
2012-12-05 17:29:46 +00:00
|
|
|
else if (val != NATPMP_TRYAGAIN)
|
2008-09-23 19:11:04 +00:00
|
|
|
{
|
2022-09-30 13:59:10 +00:00
|
|
|
state_ = State::Err;
|
2006-09-25 18:37:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-12 00:59:58 +00:00
|
|
|
switch (state_)
|
2008-09-23 19:11:04 +00:00
|
|
|
{
|
2022-09-30 13:59:10 +00:00
|
|
|
case State::Idle:
|
2022-11-02 00:32:26 +00:00
|
|
|
return { is_mapped_ ? TR_PORT_MAPPED : TR_PORT_UNMAPPED, local_port_, advertised_port_ };
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2022-09-30 13:59:10 +00:00
|
|
|
case State::Discover:
|
|
|
|
return { TR_PORT_UNMAPPED, {}, {} };
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2022-09-30 13:59:10 +00:00
|
|
|
case State::RecvPub:
|
|
|
|
case State::SendMap:
|
|
|
|
case State::RecvMap:
|
|
|
|
return { TR_PORT_MAPPING, {}, {} };
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2022-09-30 13:59:10 +00:00
|
|
|
case State::SendUnmap:
|
|
|
|
case State::RecvUnmap:
|
|
|
|
return { TR_PORT_UNMAPPING, {}, {} };
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
|
|
default:
|
2022-09-30 13:59:10 +00:00
|
|
|
return { TR_PORT_ERROR, {}, {} };
|
2007-12-13 18:56:22 +00:00
|
|
|
}
|
2006-09-25 18:37:45 +00:00
|
|
|
}
|