~ubuntu-branches/ubuntu/jaunty/transmission/jaunty-updates

« back to all changes in this revision

Viewing changes to libtransmission/port-forwarding.c

  • Committer: Bazaar Package Importer
  • Author(s): Chris Coulson
  • Date: 2008-11-28 15:33:48 UTC
  • mfrom: (1.1.19 upstream)
  • Revision ID: james.westby@ubuntu.com-20081128153348-it70trfnxiroblmc
Tags: 1.40-0ubuntu1
* New upstream release (LP: #302672)
  - Tracker communication uses fewer resources
  - More accurate bandwidth limits
  - Reduce disk fragmentation by preallocating files (LP: #287726)
  - Stability, security and performance improvements to the RPC /
    Web UI server (closes LP: #290423)
  - Support compression when serving Web UI and RPC responses
  - Simplify the RPC whitelist
  - Fix bug that prevented handshakes with encrypted BitComet peers
  - Fix 1.3x bug that could re-download some data unnecessarily
    (LP: #295040)
  - Option to automatically update the blocklist weekly
  - Added off-hour bandwidth scheduling
  - Simplify file/priority selection in the details dialog
  - Fix a couple of crashes
  - New / updated translations
  - Don't inhibit hibernation by default (LP: #292929)
  - Use "close" animation when sending to notification area (LP: #130811)
  - Fix resize problems (LP: #269872)
  - Support "--version" option when launching from command line
    (LP: #292011)
  - Correctly parse announce URLs that have leading or trailing
    spaces (LP: #262411)
  - Display an error when "Open Torrent" fails (LP: #281463)
* Dropped 10_fix_crasher_from_upstream.dpatch: Fix is in this
  upstream release.
* debian/control: Don't just build-depend on libcurl-dev, which is
  a virtual package.

Show diffs side-by-side

added added

removed removed

Lines of Context:
3
3
 *
4
4
 * This file is licensed by the GPL version 2.  Works owned by the
5
5
 * Transmission project are granted a special exemption to clause 2(b)
6
 
 * so that the bulk of its code can remain under the MIT license. 
 
6
 * so that the bulk of its code can remain under the MIT license.
7
7
 * This exemption does not extend to derived works not owned by
8
8
 * the Transmission project.
9
9
 *
10
 
 * $Id: port-forwarding.c 6389 2008-07-22 23:28:28Z charles $
 
10
 * $Id: port-forwarding.c 6795 2008-09-23 19:11:04Z charles $
11
11
 */
12
12
 
13
13
#include <errno.h>
19
19
#include "transmission.h"
20
20
#include "natpmp.h"
21
21
#include "net.h"
 
22
#include "peer-io.h"
22
23
#include "peer-mgr.h"
23
24
#include "port-forwarding.h"
24
25
#include "torrent.h"
26
27
#include "upnp.h"
27
28
#include "utils.h"
28
29
 
29
 
static const char * getKey( void ) { return _( "Port Forwarding" ); }
 
30
static const char *
 
31
getKey( void ) { return _( "Port Forwarding" ); }
30
32
 
31
33
struct tr_shared
32
34
{
33
 
    unsigned int isEnabled      : 1;
34
 
    unsigned int isShuttingDown : 1;
35
 
 
36
 
    tr_port_forwarding natpmpStatus;
37
 
    tr_port_forwarding upnpStatus;
38
 
 
39
 
    int bindPort;
40
 
    int bindSocket;
41
 
    int publicPort;
42
 
 
43
 
    tr_handle * h;
44
 
    tr_timer  * pulseTimer;
45
 
 
46
 
    tr_upnp    * upnp;
47
 
    tr_natpmp  * natpmp;
 
35
    unsigned int          isEnabled      : 1;
 
36
    unsigned int          isShuttingDown : 1;
 
37
 
 
38
    tr_port_forwarding    natpmpStatus;
 
39
    tr_port_forwarding    upnpStatus;
 
40
 
 
41
    int                   bindPort;
 
42
    int                   bindSocket;
 
43
    int                   publicPort;
 
44
 
 
45
    tr_handle *           h;
 
46
    tr_timer *            pulseTimer;
 
47
 
 
48
    tr_upnp *             upnp;
 
49
    tr_natpmp *           natpmp;
48
50
};
49
51
 
50
52
/***
57
59
    switch( state )
58
60
    {
59
61
        /* we're in the process of trying to set up port forwarding */
60
 
        case TR_PORT_MAPPING:   return _( "Starting" );
 
62
        case TR_PORT_MAPPING:
 
63
            return _( "Starting" );
 
64
 
61
65
        /* we've successfully forwarded the port */
62
 
        case TR_PORT_MAPPED:    return _( "Forwarded" );
 
66
        case TR_PORT_MAPPED:
 
67
            return _( "Forwarded" );
 
68
 
63
69
        /* we're cancelling the port forwarding */
64
 
        case TR_PORT_UNMAPPING: return _( "Stopping" );
 
70
        case TR_PORT_UNMAPPING:
 
71
            return _( "Stopping" );
 
72
 
65
73
        /* the port isn't forwarded */
66
 
        case TR_PORT_UNMAPPED:  return _( "Not forwarded" );
67
 
        case TR_PORT_ERROR:     return "???";
 
74
        case TR_PORT_UNMAPPED:
 
75
            return _( "Not forwarded" );
 
76
 
 
77
        case TR_PORT_ERROR:
 
78
            return "???";
68
79
    }
69
80
 
70
81
    return "notfound";
75
86
{
76
87
    const int port = s->publicPort;
77
88
    const int isEnabled = s->isEnabled && !s->isShuttingDown;
78
 
    int oldStatus;
79
 
    int newStatus;
80
 
    
 
89
    int       oldStatus;
 
90
    int       newStatus;
 
91
 
81
92
    oldStatus = tr_sharedTraversalStatus( s );
82
93
    s->natpmpStatus = tr_natpmpPulse( s->natpmp, port, isEnabled );
83
94
    s->upnpStatus = tr_upnpPulse( s->upnp, port, isEnabled );
84
95
    newStatus = tr_sharedTraversalStatus( s );
85
96
 
86
97
    if( newStatus != oldStatus )
87
 
        tr_ninf( getKey(), _( "State changed from \"%1$s\" to \"%2$s\"" ),
88
 
                 getNatStateStr(oldStatus),
89
 
                 getNatStateStr(newStatus) );
 
98
        tr_ninf( getKey( ), _( "State changed from \"%1$s\" to \"%2$s\"" ),
 
99
                getNatStateStr( oldStatus ),
 
100
                getNatStateStr( newStatus ) );
90
101
}
91
102
 
92
103
static void
94
105
{
95
106
    if( s->bindSocket >= 0 && ( s->bindPort != s->publicPort ) )
96
107
    {
97
 
        tr_ninf( getKey(), _( "Closing port %d" ), s->bindPort );
 
108
        tr_ninf( getKey( ), _( "Closing port %d" ), s->bindPort );
98
109
        tr_netClose( s->bindSocket );
99
110
        s->bindSocket = -1;
100
111
    }
104
115
        int socket;
105
116
        errno = 0;
106
117
        socket = tr_netBindTCP( s->publicPort );
107
 
        if( socket >= 0 ) {
108
 
            tr_ninf( getKey(), _( "Opened port %d to listen for incoming peer connections" ), s->publicPort );
 
118
        if( socket >= 0 )
 
119
        {
 
120
            tr_ninf( getKey( ),
 
121
                     _(
 
122
                         "Opened port %d to listen for incoming peer connections" ),
 
123
                     s->publicPort );
109
124
            s->bindPort = s->publicPort;
110
125
            s->bindSocket = socket;
111
126
            listen( s->bindSocket, 5 );
112
 
        } else {
113
 
            tr_nerr( getKey(), _( "Couldn't open port %d to listen for incoming peer connections (errno %d - %s)" ),
114
 
                     s->publicPort, errno, tr_strerror(errno) );
 
127
        }
 
128
        else
 
129
        {
 
130
            tr_nerr( getKey( ),
 
131
                    _(
 
132
                        "Couldn't open port %d to listen for incoming peer connections (errno %d - %s)" ),
 
133
                    s->publicPort, errno, tr_strerror( errno ) );
115
134
            s->bindPort = -1;
116
135
            s->bindSocket = -1;
117
136
        }
118
137
    }
119
 
    
120
 
    for( ;; ) /* check for new incoming peer connections */    
 
138
 
 
139
    for( ; ; ) /* check for new incoming peer connections */
121
140
    {
122
 
        int socket;
123
 
        uint16_t port;
 
141
        int            socket;
 
142
        uint16_t       port;
124
143
        struct in_addr addr;
125
144
 
126
145
        if( s->bindSocket < 0 )
130
149
        if( socket < 0 )
131
150
            break;
132
151
 
 
152
        tr_deepLog( __FILE__, __LINE__, NULL,
 
153
                   "New INCOMING connection %d (%s)",
 
154
                   socket, tr_peerIoAddrStr( &addr,
 
155
                                             port ) );
 
156
 
133
157
        tr_peerMgrAddIncoming( s->h->peerMgr, &addr, port, socket );
134
158
    }
135
159
}
137
161
static int
138
162
sharedPulse( void * vshared )
139
163
{
140
 
    int keepPulsing = 1;
 
164
    int         keepPulsing = 1;
141
165
    tr_shared * shared = vshared;
142
166
 
143
167
    natPulse( shared );
148
172
    }
149
173
    else
150
174
    {
151
 
        tr_ninf( getKey(), _( "Stopped" ) );
 
175
        tr_ninf( getKey( ), _( "Stopped" ) );
152
176
        tr_timerFree( &shared->pulseTimer );
153
177
        tr_netClose( shared->bindSocket );
154
178
        tr_natpmpClose( shared->natpmp );
166
190
***/
167
191
 
168
192
tr_shared *
169
 
tr_sharedInit( tr_handle * h, int isEnabled, int publicPort )
 
193
tr_sharedInit( tr_handle * h,
 
194
               int         isEnabled,
 
195
               int         publicPort )
170
196
{
171
197
    tr_shared * s = tr_new0( tr_shared, 1 );
172
198
 
174
200
    s->publicPort   = publicPort;
175
201
    s->bindPort     = -1;
176
202
    s->bindSocket   = -1;
177
 
    s->natpmp       = tr_natpmpInit();
178
 
    s->upnp         = tr_upnpInit();
 
203
    s->natpmp       = tr_natpmpInit( );
 
204
    s->upnp         = tr_upnpInit( );
179
205
    s->pulseTimer   = tr_timerNew( h, sharedPulse, s, 1000 );
180
206
    s->isEnabled    = isEnabled ? 1 : 0;
181
207
    s->upnpStatus   = TR_PORT_UNMAPPED;
191
217
}
192
218
 
193
219
void
194
 
tr_sharedSetPort( tr_shared * s, int port )
 
220
tr_sharedSetPort( tr_shared * s,
 
221
                  int         port )
195
222
{
196
223
    tr_torrent * tor = NULL;
197
224
 
198
225
    s->publicPort = port;
199
226
 
200
 
    while(( tor = tr_torrentNext( s->h, tor )))
 
227
    while( ( tor = tr_torrentNext( s->h, tor ) ) )
201
228
        tr_torrentChangeMyPort( tor );
202
229
}
203
230
 
208
235
}
209
236
 
210
237
void
211
 
tr_sharedTraversalEnable( tr_shared * s, int isEnabled )
 
238
tr_sharedTraversalEnable( tr_shared * s,
 
239
                          int         isEnabled )
212
240
{
213
241
    s->isEnabled = isEnabled;
214
242
}
224
252
{
225
253
    return MAX( s->natpmpStatus, s->upnpStatus );
226
254
}
 
255