2022-01-20 18:27:56 +00:00
|
|
|
// This file Copyright © 2007-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.
|
2006-09-25 18:37:45 +00:00
|
|
|
|
2021-10-17 20:17:18 +00:00
|
|
|
#include <cerrno>
|
|
|
|
#include <ctime>
|
2022-04-08 01:50:26 +00:00
|
|
|
#include <cstdint> // uint32_t
|
2007-07-29 18:11:21 +00:00
|
|
|
|
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
|
2012-02-04 01:28:15 +00:00
|
|
|
#include "natpmp.h"
|
2007-07-14 16:29:21 +00:00
|
|
|
|
2006-09-25 18:37:45 +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"
|
2011-03-24 22:57:39 +00:00
|
|
|
#include "net.h" /* tr_netCloseSocket */
|
2008-04-11 17:01:13 +00:00
|
|
|
#include "port-forwarding.h"
|
2007-07-30 18:04:10 +00:00
|
|
|
#include "utils.h"
|
2006-09-25 18:37:45 +00:00
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
static void logVal(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
|
|
|
}
|
|
|
|
|
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-08-12 00:59:58 +00:00
|
|
|
tr_port_forwarding tr_natpmp::pulse(tr_port private_port, bool is_enabled, tr_port* public_port, tr_port* real_private_port)
|
2006-09-25 18:37:45 +00:00
|
|
|
{
|
2022-08-12 00:59:58 +00:00
|
|
|
if (is_enabled && state_ == TR_NATPMP_DISCOVER)
|
2007-12-13 18:56:22 +00:00
|
|
|
{
|
2022-08-12 00:59:58 +00:00
|
|
|
int val = initnatpmp(&natpmp_, 0, 0);
|
2017-04-19 12:04:45 +00:00
|
|
|
logVal("initnatpmp", val);
|
2022-08-12 00:59:58 +00:00
|
|
|
val = sendpublicaddressrequest(&natpmp_);
|
2017-04-19 12:04:45 +00:00
|
|
|
logVal("sendpublicaddressrequest", val);
|
2022-08-12 00:59:58 +00:00
|
|
|
state_ = val < 0 ? TR_NATPMP_ERR : TR_NATPMP_RECV_PUB;
|
|
|
|
has_discovered_ = true;
|
|
|
|
setCommandTime();
|
2007-12-13 18:56:22 +00:00
|
|
|
}
|
|
|
|
|
2022-08-12 00:59:58 +00:00
|
|
|
if (state_ == TR_NATPMP_RECV_PUB && canSendCommand())
|
2006-09-25 18:37:45 +00:00
|
|
|
{
|
2007-12-08 19:34:15 +00:00
|
|
|
natpmpresp_t response;
|
2022-08-12 00:59:58 +00:00
|
|
|
int const val = readnatpmpresponseorretry(&natpmp_, &response);
|
2017-04-19 12:04:45 +00:00
|
|
|
logVal("readnatpmpresponseorretry", val);
|
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
if (val >= 0)
|
2008-09-23 19:11:04 +00:00
|
|
|
{
|
2011-03-24 22:57:39 +00:00
|
|
|
char str[128];
|
2017-04-19 12:04:45 +00:00
|
|
|
evutil_inet_ntop(AF_INET, &response.pnu.publicaddress.addr, str, sizeof(str));
|
2022-03-17 22:39:06 +00:00
|
|
|
tr_logAddInfo(fmt::format(_("Found public address '{address}'"), fmt::arg("address", str)));
|
2022-08-12 00:59:58 +00:00
|
|
|
state_ = TR_NATPMP_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-08-12 00:59:58 +00:00
|
|
|
state_ = TR_NATPMP_ERR;
|
2006-09-25 18:37:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-12 00:59:58 +00:00
|
|
|
if ((state_ == TR_NATPMP_IDLE || state_ == TR_NATPMP_ERR) && is_mapped_ && (!is_enabled || private_port_ != private_port))
|
2007-02-06 03:24:55 +00:00
|
|
|
{
|
2022-08-12 00:59:58 +00:00
|
|
|
state_ = TR_NATPMP_SEND_UNMAP;
|
2007-02-06 03:24:55 +00:00
|
|
|
}
|
2006-09-25 18:37:45 +00:00
|
|
|
|
2022-08-12 00:59:58 +00:00
|
|
|
if (state_ == TR_NATPMP_SEND_UNMAP && canSendCommand())
|
2006-09-25 18:37:45 +00:00
|
|
|
{
|
2022-08-12 00:59:58 +00:00
|
|
|
int const val = sendnewportmappingrequest(&natpmp_, NATPMP_PROTOCOL_TCP, private_port_.host(), public_port_.host(), 0);
|
2017-04-19 12:04:45 +00:00
|
|
|
logVal("sendnewportmappingrequest", val);
|
2022-08-12 00:59:58 +00:00
|
|
|
state_ = val < 0 ? TR_NATPMP_ERR : TR_NATPMP_RECV_UNMAP;
|
|
|
|
setCommandTime();
|
2006-09-25 18:37:45 +00:00
|
|
|
}
|
|
|
|
|
2022-08-12 00:59:58 +00:00
|
|
|
if (state_ == TR_NATPMP_RECV_UNMAP)
|
2006-09-25 18:37:45 +00:00
|
|
|
{
|
2007-12-08 19:34:15 +00:00
|
|
|
natpmpresp_t resp;
|
2022-08-12 00:59:58 +00:00
|
|
|
int const val = readnatpmpresponseorretry(&natpmp_, &resp);
|
2017-04-19 12:04:45 +00:00
|
|
|
logVal("readnatpmpresponseorretry", val);
|
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
if (val >= 0)
|
2008-09-23 19:11:04 +00:00
|
|
|
{
|
2022-04-21 15:58:13 +00:00
|
|
|
auto const unmapped_port = tr_port::fromHost(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-08-12 00:59:58 +00:00
|
|
|
if (private_port_ == unmapped_port)
|
2008-11-01 16:39:57 +00:00
|
|
|
{
|
2022-08-12 00:59:58 +00:00
|
|
|
private_port_.clear();
|
|
|
|
public_port_.clear();
|
|
|
|
state_ = TR_NATPMP_IDLE;
|
|
|
|
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-08-12 00:59:58 +00:00
|
|
|
state_ = TR_NATPMP_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-08-12 00:59:58 +00:00
|
|
|
if (state_ == TR_NATPMP_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-08-12 00:59:58 +00:00
|
|
|
state_ = TR_NATPMP_SEND_MAP;
|
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-08-12 00:59:58 +00:00
|
|
|
state_ = TR_NATPMP_SEND_MAP;
|
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-08-12 00:59:58 +00:00
|
|
|
if (state_ == TR_NATPMP_SEND_MAP && canSendCommand())
|
2006-09-25 18:37:45 +00:00
|
|
|
{
|
2022-04-21 15:58:13 +00:00
|
|
|
int const val = sendnewportmappingrequest(
|
2022-08-12 00:59:58 +00:00
|
|
|
&natpmp_,
|
2022-04-21 15:58:13 +00:00
|
|
|
NATPMP_PROTOCOL_TCP,
|
|
|
|
private_port.host(),
|
|
|
|
private_port.host(),
|
|
|
|
LifetimeSecs);
|
2017-04-19 12:04:45 +00:00
|
|
|
logVal("sendnewportmappingrequest", val);
|
2022-08-12 00:59:58 +00:00
|
|
|
state_ = val < 0 ? TR_NATPMP_ERR : TR_NATPMP_RECV_MAP;
|
|
|
|
setCommandTime();
|
2006-09-25 18:37:45 +00:00
|
|
|
}
|
|
|
|
|
2022-08-12 00:59:58 +00:00
|
|
|
if (state_ == TR_NATPMP_RECV_MAP)
|
2006-09-25 18:37:45 +00:00
|
|
|
{
|
2007-12-08 19:34:15 +00:00
|
|
|
natpmpresp_t resp;
|
2022-08-12 00:59:58 +00:00
|
|
|
int const val = readnatpmpresponseorretry(&natpmp_, &resp);
|
2017-04-19 12:04:45 +00:00
|
|
|
logVal("readnatpmpresponseorretry", val);
|
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
if (val >= 0)
|
2008-09-23 19:11:04 +00:00
|
|
|
{
|
2022-08-12 00:59:58 +00:00
|
|
|
state_ = TR_NATPMP_IDLE;
|
|
|
|
is_mapped_ = true;
|
|
|
|
renew_time_ = tr_time() + (resp.pnu.newportmapping.lifetime / 2);
|
|
|
|
private_port_ = tr_port::fromHost(resp.pnu.newportmapping.privateport);
|
|
|
|
public_port_ = tr_port::fromHost(resp.pnu.newportmapping.mappedpublicport);
|
|
|
|
tr_logAddInfo(fmt::format(_("Port {port} forwarded successfully"), fmt::arg("port", private_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-08-12 00:59:58 +00:00
|
|
|
state_ = TR_NATPMP_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
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
case TR_NATPMP_IDLE:
|
2022-08-12 00:59:58 +00:00
|
|
|
*public_port = public_port_;
|
|
|
|
*real_private_port = private_port_;
|
|
|
|
return is_mapped_ ? TR_PORT_MAPPED : TR_PORT_UNMAPPED;
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
|
|
case TR_NATPMP_DISCOVER:
|
2021-10-26 18:02:07 +00:00
|
|
|
return TR_PORT_UNMAPPED;
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
|
|
case TR_NATPMP_RECV_PUB:
|
|
|
|
case TR_NATPMP_SEND_MAP:
|
|
|
|
case TR_NATPMP_RECV_MAP:
|
2021-10-26 18:02:07 +00:00
|
|
|
return TR_PORT_MAPPING;
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
|
|
case TR_NATPMP_SEND_UNMAP:
|
|
|
|
case TR_NATPMP_RECV_UNMAP:
|
2021-10-26 18:02:07 +00:00
|
|
|
return TR_PORT_UNMAPPING;
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
|
|
default:
|
2021-10-26 18:02:07 +00:00
|
|
|
return TR_PORT_ERROR;
|
2007-12-13 18:56:22 +00:00
|
|
|
}
|
2006-09-25 18:37:45 +00:00
|
|
|
}
|