2007-11-06 16:02:50 +00:00
|
|
|
/*
|
2014-01-19 01:09:44 +00:00
|
|
|
* This file Copyright (C) 2007-2014 Mnemosyne LLC
|
2006-09-25 18:37:45 +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.
|
2006-09-25 18:37:45 +00:00
|
|
|
*
|
2007-11-07 00:42:19 +00:00
|
|
|
* $Id$
|
2007-11-06 16:02:50 +00:00
|
|
|
*/
|
2007-07-29 18:11:21 +00:00
|
|
|
|
2007-12-08 19:34:15 +00:00
|
|
|
#include <assert.h>
|
2007-12-09 17:50:05 +00:00
|
|
|
#include <errno.h>
|
2007-08-04 02:55:06 +00:00
|
|
|
|
2011-10-09 02:05:52 +00:00
|
|
|
#ifdef SYSTEM_MINIUPNP
|
|
|
|
#include <miniupnpc/miniupnpc.h>
|
|
|
|
#include <miniupnpc/upnpcommands.h>
|
|
|
|
#else
|
|
|
|
#include <miniupnp/miniupnpc.h>
|
|
|
|
#include <miniupnp/upnpcommands.h>
|
|
|
|
#endif
|
2007-07-14 16:29:21 +00:00
|
|
|
|
2006-09-25 18:37:45 +00:00
|
|
|
#include "transmission.h"
|
2013-01-25 23:34:20 +00:00
|
|
|
#include "log.h"
|
2008-04-11 17:01:13 +00:00
|
|
|
#include "port-forwarding.h"
|
|
|
|
#include "session.h"
|
2007-11-06 16:02:50 +00:00
|
|
|
#include "upnp.h"
|
2009-07-01 14:58:57 +00:00
|
|
|
#include "utils.h"
|
2006-09-25 18:37:45 +00:00
|
|
|
|
2008-09-23 19:11:04 +00:00
|
|
|
static const char *
|
2012-12-07 01:53:31 +00:00
|
|
|
getKey (void) { return _("Port Forwarding (UPnP)"); }
|
2008-03-07 03:26:59 +00:00
|
|
|
|
2007-12-08 19:34:15 +00:00
|
|
|
typedef enum
|
|
|
|
{
|
|
|
|
TR_UPNP_IDLE,
|
|
|
|
TR_UPNP_ERR,
|
|
|
|
TR_UPNP_DISCOVER,
|
|
|
|
TR_UPNP_MAP,
|
|
|
|
TR_UPNP_UNMAP
|
|
|
|
}
|
|
|
|
tr_upnp_state;
|
|
|
|
|
2007-11-06 16:02:50 +00:00
|
|
|
struct tr_upnp
|
2006-09-25 18:37:45 +00:00
|
|
|
{
|
2011-03-22 15:19:54 +00:00
|
|
|
bool hasDiscovered;
|
2008-09-23 19:11:04 +00:00
|
|
|
struct UPNPUrls urls;
|
|
|
|
struct IGDdatas data;
|
|
|
|
int port;
|
|
|
|
char lanaddr[16];
|
|
|
|
unsigned int isMapped;
|
|
|
|
tr_upnp_state state;
|
2006-09-25 18:37:45 +00:00
|
|
|
};
|
|
|
|
|
2007-11-06 16:02:50 +00:00
|
|
|
/**
|
|
|
|
***
|
|
|
|
**/
|
2007-06-10 22:26:59 +00:00
|
|
|
|
2007-11-06 16:02:50 +00:00
|
|
|
tr_upnp*
|
2012-12-05 17:29:46 +00:00
|
|
|
tr_upnpInit (void)
|
2006-09-25 18:37:45 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
tr_upnp * ret = tr_new0 (tr_upnp, 1);
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2007-12-13 18:56:22 +00:00
|
|
|
ret->state = TR_UPNP_DISCOVER;
|
2007-11-06 16:02:50 +00:00
|
|
|
ret->port = -1;
|
2006-09-25 18:37:45 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2012-12-05 17:29:46 +00:00
|
|
|
tr_upnpClose (tr_upnp * handle)
|
2006-09-25 18:37:45 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
assert (!handle->isMapped);
|
|
|
|
assert ((handle->state == TR_UPNP_IDLE)
|
|
|
|
|| (handle->state == TR_UPNP_ERR)
|
|
|
|
|| (handle->state == TR_UPNP_DISCOVER));
|
|
|
|
|
|
|
|
if (handle->hasDiscovered)
|
|
|
|
FreeUPNPUrls (&handle->urls);
|
|
|
|
tr_free (handle);
|
2006-09-25 18:37:45 +00:00
|
|
|
}
|
|
|
|
|
2011-10-09 02:05:52 +00:00
|
|
|
/**
|
|
|
|
*** Wrappers for miniupnpc functions
|
|
|
|
**/
|
|
|
|
|
|
|
|
static struct UPNPDev *
|
2012-12-05 17:29:46 +00:00
|
|
|
tr_upnpDiscover (int msec)
|
2011-10-09 02:05:52 +00:00
|
|
|
{
|
2014-04-27 19:31:10 +00:00
|
|
|
struct UPNPDev * ret;
|
|
|
|
bool have_err;
|
|
|
|
|
|
|
|
#if (MINIUPNPC_API_VERSION >= 8) /* adds ipv6 and error args */
|
|
|
|
int err = UPNPDISCOVER_SUCCESS;
|
|
|
|
ret = upnpDiscover (msec, NULL, NULL, 0, 0, &err);
|
|
|
|
have_err = err != UPNPDISCOVER_SUCCESS;
|
|
|
|
#else
|
|
|
|
ret = upnpDiscover (msec, NULL, NULL, 0);
|
|
|
|
have_err = ret == NULL;
|
2011-10-09 02:05:52 +00:00
|
|
|
#endif
|
|
|
|
|
2014-04-27 19:31:10 +00:00
|
|
|
if (have_err)
|
|
|
|
tr_logAddNamedDbg (getKey (), "upnpDiscover failed (errno %d - %s)", errno, tr_strerror (errno));
|
2011-10-09 02:05:52 +00:00
|
|
|
|
2014-04-27 19:31:10 +00:00
|
|
|
return ret;
|
2011-10-09 02:05:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2012-12-05 17:29:46 +00:00
|
|
|
tr_upnpGetSpecificPortMappingEntry (tr_upnp * handle, const char * proto)
|
2011-10-09 02:05:52 +00:00
|
|
|
{
|
|
|
|
int err;
|
|
|
|
char intClient[16];
|
|
|
|
char intPort[16];
|
|
|
|
char portStr[16];
|
|
|
|
|
|
|
|
*intClient = '\0';
|
|
|
|
*intPort = '\0';
|
|
|
|
|
2014-04-27 19:31:10 +00:00
|
|
|
tr_snprintf (portStr, sizeof(portStr), "%d", (int)handle->port);
|
|
|
|
|
|
|
|
#if (MINIUPNPC_API_VERSION >= 10) /* adds remoteHost arg */
|
|
|
|
|
2015-01-02 11:15:31 +00:00
|
|
|
err = UPNP_GetSpecificPortMappingEntry (handle->urls.controlURL,
|
|
|
|
handle->data.first.servicetype,
|
2014-04-27 19:31:10 +00:00
|
|
|
portStr,
|
|
|
|
proto,
|
2015-01-02 11:15:31 +00:00
|
|
|
NULL /*remoteHost*/,
|
2014-04-27 19:31:10 +00:00
|
|
|
intClient,
|
|
|
|
intPort,
|
2015-01-02 11:15:31 +00:00
|
|
|
NULL /*desc*/,
|
2014-04-27 19:31:10 +00:00
|
|
|
NULL /*enabled*/,
|
|
|
|
NULL /*duration*/);
|
|
|
|
|
|
|
|
#elif (MINIUPNPC_API_VERSION >= 8) /* adds desc, enabled and leaseDuration args */
|
|
|
|
|
|
|
|
err = UPNP_GetSpecificPortMappingEntry (handle->urls.controlURL,
|
|
|
|
handle->data.first.servicetype,
|
|
|
|
portStr,
|
|
|
|
proto,
|
|
|
|
intClient,
|
|
|
|
intPort,
|
|
|
|
NULL /*desc*/,
|
|
|
|
NULL /*enabled*/,
|
|
|
|
NULL /*duration*/);
|
2011-10-09 02:05:52 +00:00
|
|
|
|
|
|
|
#else
|
2014-04-27 19:31:10 +00:00
|
|
|
|
|
|
|
err = UPNP_GetSpecificPortMappingEntry (handle->urls.controlURL,
|
|
|
|
handle->data.first.servicetype,
|
|
|
|
portStr,
|
|
|
|
proto,
|
|
|
|
intClient,
|
|
|
|
intPort);
|
|
|
|
|
2011-10-09 02:05:52 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2012-12-05 17:29:46 +00:00
|
|
|
tr_upnpAddPortMapping (const tr_upnp * handle, const char * proto, tr_port port, const char * desc)
|
2011-10-09 02:05:52 +00:00
|
|
|
{
|
|
|
|
int err;
|
|
|
|
const int old_errno = errno;
|
|
|
|
char portStr[16];
|
|
|
|
errno = 0;
|
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
tr_snprintf (portStr, sizeof (portStr), "%d", (int)port);
|
2011-10-09 02:05:52 +00:00
|
|
|
|
2014-04-27 19:31:10 +00:00
|
|
|
#if (MINIUPNPC_API_VERSION >= 8)
|
2012-12-05 17:29:46 +00:00
|
|
|
err = UPNP_AddPortMapping (handle->urls.controlURL, handle->data.first.servicetype, portStr, portStr, handle->lanaddr, desc, proto, NULL, NULL);
|
2011-10-09 02:05:52 +00:00
|
|
|
#else
|
2014-04-27 19:31:10 +00:00
|
|
|
err = UPNP_AddPortMapping (handle->urls.controlURL, handle->data.first.servicetype, portStr, portStr, handle->lanaddr, desc, proto, NULL);
|
2011-10-09 02:05:52 +00:00
|
|
|
#endif
|
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
if (err)
|
2013-01-25 23:34:20 +00:00
|
|
|
tr_logAddNamedDbg (getKey (), "%s Port forwarding failed with error %d (errno %d - %s)", proto, err, errno, tr_strerror (errno));
|
2011-10-09 02:05:52 +00:00
|
|
|
|
|
|
|
errno = old_errno;
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2012-12-05 17:29:46 +00:00
|
|
|
tr_upnpDeletePortMapping (const tr_upnp * handle, const char * proto, tr_port port)
|
2011-10-09 02:05:52 +00:00
|
|
|
{
|
|
|
|
char portStr[16];
|
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
tr_snprintf (portStr, sizeof (portStr), "%d", (int)port);
|
2011-10-09 02:05:52 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
UPNP_DeletePortMapping (handle->urls.controlURL,
|
2011-10-09 02:05:52 +00:00
|
|
|
handle->data.first.servicetype,
|
2012-12-05 17:29:46 +00:00
|
|
|
portStr, proto, NULL);
|
2011-10-09 02:05:52 +00:00
|
|
|
}
|
|
|
|
|
2007-11-06 16:02:50 +00:00
|
|
|
/**
|
|
|
|
***
|
|
|
|
**/
|
2007-04-15 07:36:24 +00:00
|
|
|
|
2010-06-27 15:29:23 +00:00
|
|
|
enum
|
|
|
|
{
|
|
|
|
UPNP_IGD_NONE = 0,
|
|
|
|
UPNP_IGD_VALID_CONNECTED = 1,
|
|
|
|
UPNP_IGD_VALID_NOT_CONNECTED = 2,
|
|
|
|
UPNP_IGD_INVALID = 3
|
|
|
|
};
|
|
|
|
|
2007-12-08 19:34:15 +00:00
|
|
|
int
|
2012-12-05 17:29:46 +00:00
|
|
|
tr_upnpPulse (tr_upnp * handle,
|
2008-09-23 19:11:04 +00:00
|
|
|
int port,
|
2009-04-22 16:00:45 +00:00
|
|
|
int isEnabled,
|
2012-12-05 17:29:46 +00:00
|
|
|
int doPortCheck)
|
2006-09-25 18:37:45 +00:00
|
|
|
{
|
2007-12-08 19:34:15 +00:00
|
|
|
int ret;
|
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
if (isEnabled && (handle->state == TR_UPNP_DISCOVER))
|
2007-11-09 16:10:48 +00:00
|
|
|
{
|
2007-12-13 02:48:30 +00:00
|
|
|
struct UPNPDev * devlist;
|
2011-10-09 02:05:52 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
devlist = tr_upnpDiscover (2000);
|
2011-10-09 02:05:52 +00:00
|
|
|
|
2007-12-09 17:50:05 +00:00
|
|
|
errno = 0;
|
2012-12-05 17:29:46 +00:00
|
|
|
if (UPNP_GetValidIGD (devlist, &handle->urls, &handle->data,
|
|
|
|
handle->lanaddr, sizeof (handle->lanaddr)) == UPNP_IGD_VALID_CONNECTED)
|
2008-09-23 19:11:04 +00:00
|
|
|
{
|
2013-01-25 23:34:20 +00:00
|
|
|
tr_logAddNamedInfo (getKey (), _(
|
2012-12-05 17:29:46 +00:00
|
|
|
"Found Internet Gateway Device \"%s\""),
|
|
|
|
handle->urls.controlURL);
|
2013-01-25 23:34:20 +00:00
|
|
|
tr_logAddNamedInfo (getKey (), _(
|
2012-12-05 17:29:46 +00:00
|
|
|
"Local Address is \"%s\""), handle->lanaddr);
|
2007-12-08 19:34:15 +00:00
|
|
|
handle->state = TR_UPNP_IDLE;
|
2014-11-30 19:38:47 +00:00
|
|
|
handle->hasDiscovered = true;
|
2008-09-23 19:11:04 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2007-12-08 19:34:15 +00:00
|
|
|
handle->state = TR_UPNP_ERR;
|
2013-01-25 23:34:20 +00:00
|
|
|
tr_logAddNamedDbg (
|
2012-12-05 17:29:46 +00:00
|
|
|
getKey (), "UPNP_GetValidIGD failed (errno %d - %s)",
|
2008-09-23 19:11:04 +00:00
|
|
|
errno,
|
2012-12-05 17:29:46 +00:00
|
|
|
tr_strerror (errno));
|
2013-01-25 23:34:20 +00:00
|
|
|
tr_logAddNamedDbg (
|
2012-12-05 17:29:46 +00:00
|
|
|
getKey (),
|
|
|
|
"If your router supports UPnP, please make sure UPnP is enabled!");
|
2007-11-09 16:10:48 +00:00
|
|
|
}
|
2012-12-05 17:29:46 +00:00
|
|
|
freeUPNPDevlist (devlist);
|
2007-11-09 16:10:48 +00:00
|
|
|
}
|
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
if (handle->state == TR_UPNP_IDLE)
|
2006-09-25 18:37:45 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
if (handle->isMapped && (!isEnabled || (handle->port != port)))
|
2007-12-08 19:34:15 +00:00
|
|
|
handle->state = TR_UPNP_UNMAP;
|
2007-06-10 22:26:59 +00:00
|
|
|
}
|
2006-09-25 18:37:45 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
if (isEnabled && handle->isMapped && doPortCheck)
|
2009-04-22 16:00:45 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
if ((tr_upnpGetSpecificPortMappingEntry (handle, "TCP") != UPNPCOMMAND_SUCCESS) ||
|
|
|
|
(tr_upnpGetSpecificPortMappingEntry (handle, "UDP") != UPNPCOMMAND_SUCCESS))
|
2009-04-22 16:00:45 +00:00
|
|
|
{
|
2013-01-25 23:34:20 +00:00
|
|
|
tr_logAddNamedInfo (getKey (), _("Port %d isn't forwarded"), handle->port);
|
2011-03-22 15:19:54 +00:00
|
|
|
handle->isMapped = false;
|
2009-04-22 16:00:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
if (handle->state == TR_UPNP_UNMAP)
|
2006-09-25 18:37:45 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
tr_upnpDeletePortMapping (handle, "TCP", handle->port);
|
|
|
|
tr_upnpDeletePortMapping (handle, "UDP", handle->port);
|
2011-10-09 02:05:52 +00:00
|
|
|
|
2013-01-25 23:34:20 +00:00
|
|
|
tr_logAddNamedInfo (getKey (),
|
2012-12-07 01:53:31 +00:00
|
|
|
_("Stopping port forwarding through \"%s\", service \"%s\""),
|
2012-12-05 17:29:46 +00:00
|
|
|
handle->urls.controlURL, handle->data.first.servicetype);
|
2011-10-09 02:05:52 +00:00
|
|
|
|
2007-12-08 19:34:15 +00:00
|
|
|
handle->isMapped = 0;
|
|
|
|
handle->state = TR_UPNP_IDLE;
|
|
|
|
handle->port = -1;
|
2006-09-25 18:37:45 +00:00
|
|
|
}
|
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
if (handle->state == TR_UPNP_IDLE)
|
2007-12-08 19:34:15 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
if (isEnabled && !handle->isMapped)
|
2007-12-08 19:34:15 +00:00
|
|
|
handle->state = TR_UPNP_MAP;
|
|
|
|
}
|
2006-09-25 18:37:45 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
if (handle->state == TR_UPNP_MAP)
|
2007-12-08 19:34:15 +00:00
|
|
|
{
|
2011-01-15 17:06:11 +00:00
|
|
|
int err_tcp = -1;
|
|
|
|
int err_udp = -1;
|
2007-12-09 17:50:05 +00:00
|
|
|
errno = 0;
|
2007-12-24 07:02:40 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
if (!handle->urls.controlURL || !handle->data.first.servicetype)
|
2007-12-24 07:02:40 +00:00
|
|
|
handle->isMapped = 0;
|
2008-09-23 19:11:04 +00:00
|
|
|
else
|
|
|
|
{
|
2008-12-10 06:12:04 +00:00
|
|
|
char desc[64];
|
2012-12-05 17:29:46 +00:00
|
|
|
tr_snprintf (desc, sizeof (desc), "%s at %d", TR_NAME, port);
|
2011-01-15 17:06:11 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
err_tcp = tr_upnpAddPortMapping (handle, "TCP", port, desc);
|
|
|
|
err_udp = tr_upnpAddPortMapping (handle, "UDP", port, desc);
|
2011-10-09 02:05:52 +00:00
|
|
|
|
2011-01-15 17:06:11 +00:00
|
|
|
handle->isMapped = !err_tcp | !err_udp;
|
2007-12-24 07:02:40 +00:00
|
|
|
}
|
2013-01-25 23:34:20 +00:00
|
|
|
tr_logAddNamedInfo (getKey (),
|
2012-12-07 01:53:31 +00:00
|
|
|
_("Port forwarding through \"%s\", service \"%s\". (local address: %s:%d)"),
|
2010-04-07 22:45:35 +00:00
|
|
|
handle->urls.controlURL, handle->data.first.servicetype,
|
2012-12-05 17:29:46 +00:00
|
|
|
handle->lanaddr, port);
|
|
|
|
if (handle->isMapped)
|
2008-09-23 19:11:04 +00:00
|
|
|
{
|
2013-01-25 23:34:20 +00:00
|
|
|
tr_logAddNamedInfo (getKey (), "%s", _("Port forwarding successful!"));
|
2007-12-08 19:34:15 +00:00
|
|
|
handle->port = port;
|
|
|
|
handle->state = TR_UPNP_IDLE;
|
2008-09-23 19:11:04 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-01-25 23:34:20 +00:00
|
|
|
tr_logAddNamedDbg (getKey (), "If your router supports UPnP, please make sure UPnP is enabled!");
|
2007-12-08 19:34:15 +00:00
|
|
|
handle->port = -1;
|
|
|
|
handle->state = TR_UPNP_ERR;
|
|
|
|
}
|
|
|
|
}
|
2006-09-25 18:37:45 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
switch (handle->state)
|
2007-12-13 18:56:22 +00:00
|
|
|
{
|
2008-09-23 19:11:04 +00:00
|
|
|
case TR_UPNP_DISCOVER:
|
|
|
|
ret = TR_PORT_UNMAPPED; break;
|
|
|
|
|
|
|
|
case TR_UPNP_MAP:
|
|
|
|
ret = TR_PORT_MAPPING; break;
|
|
|
|
|
|
|
|
case TR_UPNP_UNMAP:
|
|
|
|
ret = TR_PORT_UNMAPPING; break;
|
|
|
|
|
|
|
|
case TR_UPNP_IDLE:
|
|
|
|
ret = handle->isMapped ? TR_PORT_MAPPED
|
|
|
|
: TR_PORT_UNMAPPED; break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
ret = TR_PORT_ERROR; break;
|
2007-12-13 18:56:22 +00:00
|
|
|
}
|
|
|
|
|
2007-12-08 19:34:15 +00:00
|
|
|
return ret;
|
2006-09-25 18:37:45 +00:00
|
|
|
}
|
2008-09-23 19:11:04 +00:00
|
|
|
|