2008-07-22 23:28:28 +00:00
|
|
|
/*
|
2014-01-19 01:09:44 +00:00
|
|
|
* This file Copyright (C) 2008-2014 Mnemosyne LLC
|
2007-01-19 04:42:31 +00:00
|
|
|
*
|
2014-01-21 03:10:30 +00:00
|
|
|
* It may be used under the GNU GPL versions 2 or 3
|
2014-01-19 01:09:44 +00:00
|
|
|
* or any future license endorsed by Mnemosyne LLC.
|
2007-01-19 04:42:31 +00:00
|
|
|
*
|
2008-07-22 23:28:28 +00:00
|
|
|
*/
|
2007-01-19 04:42:31 +00:00
|
|
|
|
2007-09-20 16:32:01 +00:00
|
|
|
#include <stdio.h>
|
2007-07-29 18:11:21 +00:00
|
|
|
|
2007-07-14 16:29:21 +00:00
|
|
|
#include <sys/types.h>
|
|
|
|
|
2010-12-24 08:58:41 +00:00
|
|
|
#include <event2/event.h>
|
2010-05-19 16:17:51 +00:00
|
|
|
|
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"
|
2007-07-30 18:04:10 +00:00
|
|
|
#include "utils.h"
|
2007-01-19 04:42:31 +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");
|
|
|
|
}
|
2008-03-07 03:26:59 +00:00
|
|
|
|
2007-09-20 16:32:01 +00:00
|
|
|
struct tr_shared
|
2007-01-19 04:42:31 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
bool isEnabled;
|
|
|
|
bool isShuttingDown;
|
|
|
|
bool doPortCheck;
|
2008-04-12 21:47:10 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_port_forwarding natpmpStatus;
|
|
|
|
tr_port_forwarding upnpStatus;
|
2007-01-19 04:42:31 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_upnp* upnp;
|
|
|
|
tr_natpmp* natpmp;
|
|
|
|
tr_session* session;
|
2009-05-14 13:42:29 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
struct event* 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
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
int oldStatus;
|
|
|
|
int newStatus;
|
|
|
|
tr_port public_peer_port;
|
2017-04-20 16:02:19 +00:00
|
|
|
tr_port const private_peer_port = s->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
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (s->natpmp == NULL)
|
|
|
|
{
|
|
|
|
s->natpmp = tr_natpmpInit();
|
|
|
|
}
|
2009-05-26 20:52:08 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (s->upnp == NULL)
|
|
|
|
{
|
|
|
|
s->upnp = tr_upnpInit();
|
|
|
|
}
|
2010-07-05 21:04:17 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
oldStatus = tr_sharedTraversalStatus(s);
|
2010-07-05 21:04:17 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
s->natpmpStatus = tr_natpmpPulse(s->natpmp, private_peer_port, is_enabled, &public_peer_port);
|
2010-07-05 21:04:17 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (s->natpmpStatus == TR_PORT_MAPPED)
|
|
|
|
{
|
|
|
|
s->session->public_peer_port = public_peer_port;
|
|
|
|
}
|
2008-04-12 21:47:10 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
s->upnpStatus = tr_upnpPulse(s->upnp, private_peer_port, is_enabled, do_check);
|
2013-01-24 23:59:52 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
newStatus = tr_sharedTraversalStatus(s);
|
|
|
|
|
|
|
|
if (newStatus != oldStatus)
|
|
|
|
{
|
|
|
|
tr_logAddNamedInfo(getKey(), _("State changed from \"%1$s\" to \"%2$s\""), getNatStateStr(oldStatus),
|
|
|
|
getNatStateStr(newStatus));
|
|
|
|
}
|
2007-01-19 04:42:31 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
static void set_evtimer_from_status(tr_shared* s)
|
2007-01-19 04:42:31 +00:00
|
|
|
{
|
2017-05-01 15:46:41 +00:00
|
|
|
int sec = 0;
|
|
|
|
int msec = 0;
|
2007-09-21 05:31:29 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
/* when to wake up again */
|
|
|
|
switch (tr_sharedTraversalStatus(s))
|
2008-12-15 00:17:08 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
case TR_PORT_MAPPED:
|
2013-01-24 23:59:52 +00:00
|
|
|
/* if we're mapped, everything is fine... check back in 20 minutes
|
|
|
|
* to renew the port forwarding if it's expired */
|
|
|
|
s->doPortCheck = true;
|
|
|
|
sec = 60 * 20;
|
|
|
|
break;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case TR_PORT_ERROR:
|
2013-01-24 23:59:52 +00:00
|
|
|
/* some kind of an error. wait 60 seconds and retry */
|
|
|
|
sec = 60;
|
|
|
|
break;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
default:
|
2013-01-24 23:59:52 +00:00
|
|
|
/* in progress. pulse frequently. */
|
|
|
|
msec = 333000;
|
|
|
|
break;
|
2007-01-19 04:42:31 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (s->timer != NULL)
|
|
|
|
{
|
|
|
|
tr_timerAdd(s->timer, sec, msec);
|
|
|
|
}
|
2009-04-22 16:00:45 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
static void onTimer(evutil_socket_t fd UNUSED, short what UNUSED, void* vshared)
|
2009-10-29 17:25:03 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_shared* s = vshared;
|
2009-10-29 17:25:03 +00:00
|
|
|
|
2017-06-08 07:24:12 +00:00
|
|
|
TR_ASSERT(s != NULL);
|
|
|
|
TR_ASSERT(s->timer != NULL);
|
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 */
|
|
|
|
set_evtimer_from_status(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
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_shared* tr_sharedInit(tr_session* session)
|
2007-01-19 04:42:31 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_shared* s = tr_new0(tr_shared, 1);
|
2007-12-08 19:34:15 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
s->session = session;
|
|
|
|
s->isEnabled = false;
|
|
|
|
s->upnpStatus = TR_PORT_UNMAPPED;
|
|
|
|
s->natpmpStatus = TR_PORT_UNMAPPED;
|
2007-01-19 04:42:31 +00:00
|
|
|
|
2009-10-23 03:41:36 +00:00
|
|
|
#if 0
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
|
|
if (isEnabled)
|
2009-05-20 17:35:41 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
s->timer = tr_new0(struct event, 1);
|
|
|
|
evtimer_set(s->timer, onTimer, s);
|
|
|
|
tr_timerAdd(s->timer, 0, 333000);
|
2009-05-20 17:35:41 +00:00
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2009-10-23 03:41:36 +00:00
|
|
|
#endif
|
2009-05-14 13:42:29 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
return s;
|
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
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
if (s->timer != NULL)
|
2009-05-14 13:42:29 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
event_free(s->timer);
|
|
|
|
s->timer = NULL;
|
2009-05-14 13:42:29 +00:00
|
|
|
}
|
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
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_logAddNamedInfo(getKey(), "%s", _("Stopped"));
|
|
|
|
natPulse(s, false);
|
2010-02-11 14:28:40 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_natpmpClose(s->natpmp);
|
|
|
|
s->natpmp = NULL;
|
|
|
|
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);
|
|
|
|
s->upnp = NULL;
|
|
|
|
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
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
void tr_sharedClose(tr_session* session)
|
2009-05-14 13:42:29 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_shared* s = session->shared;
|
2007-01-27 21:17:10 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
s->isShuttingDown = true;
|
|
|
|
stop_forwarding(s);
|
|
|
|
s->session->shared = NULL;
|
|
|
|
tr_free(s);
|
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
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
s->timer = evtimer_new(s->session->event_base, onTimer, s);
|
|
|
|
set_evtimer_from_status(s);
|
2009-05-20 17:35:41 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
void tr_sharedTraversalEnable(tr_shared* s, bool isEnabled)
|
2007-09-20 16:32:01 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
if ((s->isEnabled = isEnabled))
|
|
|
|
{
|
|
|
|
start_timer(s);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
stop_forwarding(s);
|
|
|
|
}
|
2007-12-08 19:34:15 +00:00
|
|
|
}
|
2007-01-19 04:42:31 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
void tr_sharedPortChanged(tr_session* session)
|
2007-12-08 19:34:15 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_shared* 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
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
return MAX(s->natpmpStatus, s->upnpStatus);
|
2007-01-19 04:42:31 +00:00
|
|
|
}
|