2022-01-20 18:27:56 +00:00
|
|
|
// This file Copyright © 2007-2022 Mnemosyne LLC.
|
|
|
|
// It may be used under GPLv2 (SPDX: GPL-2.0), GPLv3 (SPDX: GPL-3.0),
|
|
|
|
// 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>
|
|
|
|
#include <cinttypes>
|
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
|
|
|
|
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
|
|
|
|
2022-01-17 18:39:50 +00:00
|
|
|
static auto constexpr LifetimeSecs = uint32_t{ 3600 };
|
|
|
|
static auto constexpr CommandWaitSecs = time_t{ 8 };
|
2006-09-25 18:37:45 +00:00
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
static char const* getKey(void)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
return _("Port Forwarding (NAT-PMP)");
|
|
|
|
}
|
2008-03-07 03:26:59 +00:00
|
|
|
|
2007-12-08 19:34:15 +00:00
|
|
|
/**
|
|
|
|
***
|
|
|
|
**/
|
2007-02-06 03:24:55 +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
|
|
|
{
|
|
|
|
tr_logAddNamedInfo(getKey(), _("%s succeeded (%d)"), func, ret);
|
|
|
|
}
|
2006-09-25 18:37:45 +00:00
|
|
|
else
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2021-08-15 09:41:48 +00:00
|
|
|
tr_logAddNamedDbg(
|
|
|
|
getKey(),
|
|
|
|
"%s failed. Natpmp returned %d (%s); errno is %d (%s)",
|
|
|
|
func,
|
|
|
|
ret,
|
|
|
|
strnatpmperr(ret),
|
|
|
|
errno,
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_strerror(errno));
|
|
|
|
}
|
2006-09-25 18:37:45 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
struct tr_natpmp* tr_natpmpInit(void)
|
2007-04-15 07:36:24 +00:00
|
|
|
{
|
2021-10-26 18:02:07 +00:00
|
|
|
auto* const nat = tr_new0(struct tr_natpmp, 1);
|
2007-12-13 18:56:22 +00:00
|
|
|
nat->state = TR_NATPMP_DISCOVER;
|
2010-07-05 21:04:17 +00:00
|
|
|
nat->public_port = 0;
|
|
|
|
nat->private_port = 0;
|
2015-03-18 07:34:26 +00:00
|
|
|
nat->natpmp.s = TR_BAD_SOCKET; /* socket */
|
2007-12-08 19:34:15 +00:00
|
|
|
return nat;
|
2006-09-25 18:37:45 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
void tr_natpmpClose(tr_natpmp* nat)
|
2006-09-25 18:37:45 +00:00
|
|
|
{
|
2021-09-15 00:18:09 +00:00
|
|
|
if (nat != nullptr)
|
2008-04-12 02:26:45 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
closenatpmp(&nat->natpmp);
|
|
|
|
tr_free(nat);
|
2008-04-12 02:26:45 +00:00
|
|
|
}
|
2006-09-25 18:37:45 +00:00
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
static bool canSendCommand(struct tr_natpmp const* nat)
|
2007-12-13 19:28:51 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
return tr_time() >= nat->command_time;
|
2007-12-13 19:28:51 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
static void setCommandTime(struct tr_natpmp* nat)
|
2007-12-13 19:28:51 +00:00
|
|
|
{
|
2022-01-17 18:39:50 +00:00
|
|
|
nat->command_time = tr_time() + CommandWaitSecs;
|
2007-12-13 19:28:51 +00:00
|
|
|
}
|
|
|
|
|
2022-01-22 00:07:24 +00:00
|
|
|
tr_port_forwarding tr_natpmpPulse(
|
|
|
|
struct tr_natpmp* nat,
|
|
|
|
tr_port private_port,
|
|
|
|
bool is_enabled,
|
|
|
|
tr_port* public_port,
|
|
|
|
tr_port* real_private_port)
|
2006-09-25 18:37:45 +00:00
|
|
|
{
|
2017-04-30 16:25:26 +00:00
|
|
|
if (is_enabled && nat->state == TR_NATPMP_DISCOVER)
|
2007-12-13 18:56:22 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
int val = initnatpmp(&nat->natpmp, 0, 0);
|
|
|
|
logVal("initnatpmp", val);
|
|
|
|
val = sendpublicaddressrequest(&nat->natpmp);
|
|
|
|
logVal("sendpublicaddressrequest", val);
|
2007-12-13 18:56:22 +00:00
|
|
|
nat->state = val < 0 ? TR_NATPMP_ERR : TR_NATPMP_RECV_PUB;
|
2011-03-22 15:19:54 +00:00
|
|
|
nat->has_discovered = true;
|
2017-04-19 12:04:45 +00:00
|
|
|
setCommandTime(nat);
|
2007-12-13 18:56:22 +00:00
|
|
|
}
|
|
|
|
|
2017-04-30 16:25:26 +00:00
|
|
|
if (nat->state == TR_NATPMP_RECV_PUB && canSendCommand(nat))
|
2006-09-25 18:37:45 +00:00
|
|
|
{
|
2007-12-08 19:34:15 +00:00
|
|
|
natpmpresp_t response;
|
2017-04-20 16:02:19 +00:00
|
|
|
int const val = readnatpmpresponseorretry(&nat->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));
|
|
|
|
tr_logAddNamedInfo(getKey(), _("Found public address \"%s\""), str);
|
2007-12-08 19:34:15 +00:00
|
|
|
nat->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
|
|
|
{
|
2008-04-12 21:47:10 +00:00
|
|
|
nat->state = TR_NATPMP_ERR;
|
2006-09-25 18:37:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
if ((nat->state == TR_NATPMP_IDLE || nat->state == TR_NATPMP_ERR) && (nat->is_mapped) &&
|
2020-11-01 21:47:57 +00:00
|
|
|
(!is_enabled || nat->private_port != private_port))
|
2007-02-06 03:24:55 +00:00
|
|
|
{
|
2020-11-01 21:47:57 +00:00
|
|
|
nat->state = TR_NATPMP_SEND_UNMAP;
|
2007-02-06 03:24:55 +00:00
|
|
|
}
|
2006-09-25 18:37:45 +00:00
|
|
|
|
2017-04-30 16:25:26 +00:00
|
|
|
if (nat->state == TR_NATPMP_SEND_UNMAP && canSendCommand(nat))
|
2006-09-25 18:37:45 +00:00
|
|
|
{
|
2017-04-20 16:02:19 +00:00
|
|
|
int const val = sendnewportmappingrequest(&nat->natpmp, NATPMP_PROTOCOL_TCP, nat->private_port, nat->public_port, 0);
|
2017-04-19 12:04:45 +00:00
|
|
|
logVal("sendnewportmappingrequest", val);
|
2007-12-08 19:34:15 +00:00
|
|
|
nat->state = val < 0 ? TR_NATPMP_ERR : TR_NATPMP_RECV_UNMAP;
|
2017-04-19 12:04:45 +00:00
|
|
|
setCommandTime(nat);
|
2006-09-25 18:37:45 +00:00
|
|
|
}
|
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
if (nat->state == TR_NATPMP_RECV_UNMAP)
|
2006-09-25 18:37:45 +00:00
|
|
|
{
|
2007-12-08 19:34:15 +00:00
|
|
|
natpmpresp_t resp;
|
2017-04-20 16:02:19 +00:00
|
|
|
int const val = readnatpmpresponseorretry(&nat->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
|
|
|
{
|
2020-08-11 18:11:55 +00:00
|
|
|
int const unmapped_port = resp.pnu.newportmapping.privateport;
|
2010-07-05 21:04:17 +00:00
|
|
|
|
2020-08-11 18:11:55 +00:00
|
|
|
tr_logAddNamedInfo(getKey(), _("no longer forwarding port %d"), unmapped_port);
|
2010-07-05 21:04:17 +00:00
|
|
|
|
2020-08-11 18:11:55 +00:00
|
|
|
if (nat->private_port == unmapped_port)
|
2008-11-01 16:39:57 +00:00
|
|
|
{
|
2010-07-05 21:04:17 +00:00
|
|
|
nat->private_port = 0;
|
|
|
|
nat->public_port = 0;
|
2008-11-01 16:39:57 +00:00
|
|
|
nat->state = TR_NATPMP_IDLE;
|
2011-03-22 15:19:54 +00:00
|
|
|
nat->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
|
|
|
{
|
2008-04-12 21:47:10 +00:00
|
|
|
nat->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
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
if (nat->state == TR_NATPMP_IDLE)
|
2006-09-25 18:37:45 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
if (is_enabled && !nat->is_mapped && nat->has_discovered)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2007-12-08 19:34:15 +00:00
|
|
|
nat->state = TR_NATPMP_SEND_MAP;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
else if (nat->is_mapped && tr_time() >= nat->renew_time)
|
|
|
|
{
|
2007-12-08 19:34:15 +00:00
|
|
|
nat->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
|
|
|
|
2017-04-30 16:25:26 +00:00
|
|
|
if (nat->state == TR_NATPMP_SEND_MAP && canSendCommand(nat))
|
2006-09-25 18:37:45 +00:00
|
|
|
{
|
2022-01-17 18:39:50 +00:00
|
|
|
int const val = sendnewportmappingrequest(&nat->natpmp, NATPMP_PROTOCOL_TCP, private_port, private_port, LifetimeSecs);
|
2017-04-19 12:04:45 +00:00
|
|
|
logVal("sendnewportmappingrequest", val);
|
2007-12-08 19:34:15 +00:00
|
|
|
nat->state = val < 0 ? TR_NATPMP_ERR : TR_NATPMP_RECV_MAP;
|
2017-04-19 12:04:45 +00:00
|
|
|
setCommandTime(nat);
|
2006-09-25 18:37:45 +00:00
|
|
|
}
|
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
if (nat->state == TR_NATPMP_RECV_MAP)
|
2006-09-25 18:37:45 +00:00
|
|
|
{
|
2007-12-08 19:34:15 +00:00
|
|
|
natpmpresp_t resp;
|
2017-04-20 16:02:19 +00:00
|
|
|
int const val = readnatpmpresponseorretry(&nat->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
|
|
|
{
|
2007-12-08 19:34:15 +00:00
|
|
|
nat->state = TR_NATPMP_IDLE;
|
2011-03-22 15:19:54 +00:00
|
|
|
nat->is_mapped = true;
|
2017-04-19 12:04:45 +00:00
|
|
|
nat->renew_time = tr_time() + (resp.pnu.newportmapping.lifetime / 2);
|
2010-07-05 21:04:17 +00:00
|
|
|
nat->private_port = resp.pnu.newportmapping.privateport;
|
|
|
|
nat->public_port = resp.pnu.newportmapping.mappedpublicport;
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_logAddNamedInfo(getKey(), _("Port %d forwarded successfully"), nat->private_port);
|
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
|
|
|
{
|
2008-04-12 21:47:10 +00:00
|
|
|
nat->state = TR_NATPMP_ERR;
|
2006-09-25 18:37:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
switch (nat->state)
|
2008-09-23 19:11:04 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
case TR_NATPMP_IDLE:
|
|
|
|
*public_port = nat->public_port;
|
2022-01-22 00:07:24 +00:00
|
|
|
*real_private_port = nat->private_port;
|
2017-04-19 12:04:45 +00:00
|
|
|
return nat->is_mapped ? TR_PORT_MAPPED : TR_PORT_UNMAPPED;
|
|
|
|
|
|
|
|
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
|
|
|
}
|