Ignore:
Timestamp:
Oct 9, 2011, 2:05:52 AM (10 years ago)
Author:
jordan
Message:

(trunk libT) #4323 "Allow usage of system miniupnpc" -- fixed.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/libtransmission/upnp.c

    r12593 r12957  
    1414#include <errno.h>
    1515
    16 #include <miniupnp/miniupnpc.h>
    17 #include <miniupnp/upnpcommands.h>
     16#ifdef SYSTEM_MINIUPNP
     17  #include <miniupnpc/miniupnpc.h>
     18  #include <miniupnpc/upnpcommands.h>
     19#else
     20  #include <miniupnp/miniupnpc.h>
     21  #include <miniupnp/upnpcommands.h>
     22#endif
    1823
    1924#include "transmission.h"
     
    7580
    7681/**
     82***  Wrappers for miniupnpc functions
     83**/
     84
     85static struct UPNPDev *
     86tr_upnpDiscover( int msec )
     87{
     88    int err = 0;
     89    struct UPNPDev * ret = NULL;
     90
     91#if defined(HAVE_MINIUPNP_16)
     92    ret = upnpDiscover( msec, NULL, NULL, 0, 0, &err );
     93#elif defined(HAVE_MINIUPNP_15)
     94    ret = upnpDiscover( msec, NULL, NULL, 0 );
     95#else
     96    ret = UPNPCOMMAND_UNKNOWN_ERROR;
     97#endif
     98
     99    if( ret != UPNPCOMMAND_SUCCESS )
     100        tr_ndbg( getKey( ), "upnpDiscover failed (errno %d - %s)", err, tr_strerror( err ) );
     101
     102    return ret;
     103}
     104
     105static int
     106tr_upnpGetSpecificPortMappingEntry( tr_upnp * handle, const char * proto )
     107{
     108    int err;
     109    char intClient[16];
     110    char intPort[16];
     111    char portStr[16];
     112
     113    *intClient = '\0';
     114    *intPort = '\0';
     115
     116    tr_snprintf( portStr, sizeof( portStr ), "%d", (int)handle->port );
     117
     118#if defined(HAVE_MINIUPNP_16)
     119    err = UPNP_GetSpecificPortMappingEntry( handle->urls.controlURL, handle->data.first.servicetype, portStr, proto, intClient, intPort, NULL, NULL, NULL );
     120#elif defined(HAVE_MINIUPNP_15)
     121    err = UPNP_GetSpecificPortMappingEntry( handle->urls.controlURL, handle->data.first.servicetype, portStr, proto, intClient, intPort );
     122#else
     123    err = UPNPCOMMAND_UNKNOWN_ERROR;
     124#endif
     125
     126    return err;
     127}
     128
     129static int
     130tr_upnpAddPortMapping( const tr_upnp * handle, const char * proto, tr_port port, const char * desc )
     131{
     132    int err;
     133    const int old_errno = errno;
     134    char portStr[16];
     135    errno = 0;
     136
     137    tr_snprintf( portStr, sizeof( portStr ), "%d", (int)port );
     138
     139#if defined(HAVE_MINIUPNP_16)
     140    err = UPNP_AddPortMapping( handle->urls.controlURL, handle->data.first.servicetype, portStr, portStr, handle->lanaddr, desc, proto, NULL, NULL );
     141#elif defined(HAVE_MINIUPNP_15)
     142    err = UPNP_AddPortMapping( handle->urls.controlURL, handle->data.first.servicetype, portStr, portStr, handle->lanaddr, desc, proto, NULL );
     143#else
     144    err = UPNPCOMMAND_UNKNOWN_ERROR;
     145#endif
     146
     147    if( err )
     148        tr_ndbg( getKey( ), "%s Port forwarding failed with error %d (errno %d - %s)", proto, err, errno, tr_strerror( errno ) );
     149
     150    errno = old_errno;
     151    return err;
     152}
     153
     154static void
     155tr_upnpDeletePortMapping( const tr_upnp * handle, const char * proto, tr_port port )
     156{
     157    char portStr[16];
     158
     159    tr_snprintf( portStr, sizeof( portStr ), "%d", (int)port );
     160
     161    UPNP_DeletePortMapping( handle->urls.controlURL,
     162                            handle->data.first.servicetype,
     163                            portStr, proto, NULL );
     164}
     165
     166/**
    77167***
    78168**/
     
    97187    {
    98188        struct UPNPDev * devlist;
    99         errno = 0;
    100         devlist = upnpDiscover( 2000, NULL, NULL, 0, 0, &errno );
    101         if( devlist == NULL )
    102         {
    103             tr_ndbg(
    104                  getKey( ), "upnpDiscover failed (errno %d - %s)", errno,
    105                 tr_strerror( errno ) );
    106         }
     189
     190        devlist = tr_upnpDiscover( 2000 );
     191
    107192        errno = 0;
    108193        if( UPNP_GetValidIGD( devlist, &handle->urls, &handle->data,
     
    139224    if( isEnabled && handle->isMapped && doPortCheck )
    140225    {
    141         char portStr[8];
    142         char intPort[8];
    143         char intClient[16];
    144 
    145         tr_snprintf( portStr, sizeof( portStr ), "%d", handle->port );
    146         if( UPNP_GetSpecificPortMappingEntry( handle->urls.controlURL, handle->data.first.servicetype,
    147             portStr, "TCP", intClient, intPort, NULL, NULL, NULL ) != UPNPCOMMAND_SUCCESS  ||
    148             UPNP_GetSpecificPortMappingEntry( handle->urls.controlURL, handle->data.first.servicetype,
    149             portStr, "UDP", intClient, intPort, NULL, NULL, NULL ) != UPNPCOMMAND_SUCCESS )
     226        if( ( tr_upnpGetSpecificPortMappingEntry( handle, "TCP" ) != UPNPCOMMAND_SUCCESS ) ||
     227            ( tr_upnpGetSpecificPortMappingEntry( handle, "UDP" ) != UPNPCOMMAND_SUCCESS ) )
    150228        {
    151229            tr_ninf( getKey( ), _( "Port %d isn't forwarded" ), handle->port );
     
    156234    if( handle->state == TR_UPNP_UNMAP )
    157235    {
    158         char portStr[16];
    159         tr_snprintf( portStr, sizeof( portStr ), "%d", handle->port );
    160         UPNP_DeletePortMapping( handle->urls.controlURL,
    161                                 handle->data.first.servicetype,
    162                                 portStr, "TCP", NULL );
    163         UPNP_DeletePortMapping( handle->urls.controlURL,
    164                                 handle->data.first.servicetype,
    165                                 portStr, "UDP", NULL );
     236        tr_upnpDeletePortMapping( handle, "TCP", handle->port );
     237        tr_upnpDeletePortMapping( handle, "UDP", handle->port );
     238
    166239        tr_ninf( getKey( ),
    167                  _(
    168                      "Stopping port forwarding through \"%s\", service \"%s\"" ),
     240                 _( "Stopping port forwarding through \"%s\", service \"%s\"" ),
    169241                 handle->urls.controlURL, handle->data.first.servicetype );
     242
    170243        handle->isMapped = 0;
    171244        handle->state = TR_UPNP_IDLE;
     
    189262        else
    190263        {
    191             char portStr[16];
    192264            char desc[64];
    193             const int prev_errno = errno;
    194             tr_snprintf( portStr, sizeof( portStr ), "%d", port );
    195265            tr_snprintf( desc, sizeof( desc ), "%s at %d", TR_NAME, port );
    196266
    197             errno = 0;
    198             err_tcp = UPNP_AddPortMapping( handle->urls.controlURL,
    199                                        handle->data.first.servicetype,
    200                                        portStr, portStr, handle->lanaddr,
    201                                        desc, "TCP", NULL, NULL );
    202             if( err_tcp )
    203                 tr_ndbg( getKey( ), "TCP Port forwarding failed with error %d (errno %d - %s)",
    204                          err_tcp, errno, tr_strerror( errno ) );
    205 
    206             errno = 0;
    207             err_udp = UPNP_AddPortMapping( handle->urls.controlURL,
    208                                        handle->data.first.servicetype,
    209                                        portStr, portStr, handle->lanaddr,
    210                                        desc, "UDP", NULL, NULL );
    211             if( err_udp )
    212                 tr_ndbg( getKey( ), "UDP Port forwarding failed with error %d (errno %d - %s)",
    213                          err_udp, errno, tr_strerror( errno ) );
    214 
    215             errno = prev_errno;
     267            err_tcp = tr_upnpAddPortMapping( handle, "TCP", port, desc );
     268            err_udp = tr_upnpAddPortMapping( handle, "UDP", port, desc );
     269
    216270            handle->isMapped = !err_tcp | !err_udp;
    217271        }
Note: See TracChangeset for help on using the changeset viewer.