source: trunk/libtransmission/port-forwarding.c @ 7231

Last change on this file since 7231 was 7231, checked in by charles, 12 years ago

(libT) re-apply jhujhiti's IPv6 patch. This merges in my tr_port cleanup, so any new bugs are mine :/

  • Property svn:keywords set to Date Rev Author Id
File size: 5.9 KB
Line 
1/*
2 * This file Copyright (C) 2008 Charles Kerr <charles@rebelbase.com>
3 *
4 * This file is licensed by the GPL version 2.  Works owned by the
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.
7 * This exemption does not extend to derived works not owned by
8 * the Transmission project.
9 *
10 * $Id: port-forwarding.c 7231 2008-12-02 03:41:58Z charles $
11 */
12
13#include <errno.h>
14#include <string.h>
15#include <stdio.h>
16
17#include <sys/types.h>
18
19#include "transmission.h"
20#include "natpmp.h"
21#include "net.h"
22#include "peer-io.h"
23#include "peer-mgr.h"
24#include "port-forwarding.h"
25#include "torrent.h"
26#include "trevent.h"
27#include "upnp.h"
28#include "utils.h"
29
30static const char *
31getKey( void ) { return _( "Port Forwarding" ); }
32
33struct tr_shared
34{
35    tr_bool               isEnabled;
36    tr_bool               isShuttingDown;
37
38    tr_port_forwarding    natpmpStatus;
39    tr_port_forwarding    upnpStatus;
40
41    int                   bindPort;
42    int                   bindSocket;
43    int                   publicPort;
44
45    tr_timer            * pulseTimer;
46
47    tr_upnp             * upnp;
48    tr_natpmp           * natpmp;
49    tr_session          * session;
50};
51
52/***
53****
54***/
55
56static const char*
57getNatStateStr( int state )
58{
59    switch( state )
60    {
61        /* we're in the process of trying to set up port forwarding */
62        case TR_PORT_MAPPING:
63            return _( "Starting" );
64
65        /* we've successfully forwarded the port */
66        case TR_PORT_MAPPED:
67            return _( "Forwarded" );
68
69        /* we're cancelling the port forwarding */
70        case TR_PORT_UNMAPPING:
71            return _( "Stopping" );
72
73        /* the port isn't forwarded */
74        case TR_PORT_UNMAPPED:
75            return _( "Not forwarded" );
76
77        case TR_PORT_ERROR:
78            return "???";
79    }
80
81    return "notfound";
82}
83
84static void
85natPulse( tr_shared * s )
86{
87    const int port = s->publicPort;
88    const int isEnabled = s->isEnabled && !s->isShuttingDown;
89    int       oldStatus;
90    int       newStatus;
91
92    oldStatus = tr_sharedTraversalStatus( s );
93    s->natpmpStatus = tr_natpmpPulse( s->natpmp, port, isEnabled );
94    s->upnpStatus = tr_upnpPulse( s->upnp, port, isEnabled );
95    newStatus = tr_sharedTraversalStatus( s );
96
97    if( newStatus != oldStatus )
98        tr_ninf( getKey( ), _( "State changed from \"%1$s\" to \"%2$s\"" ),
99                getNatStateStr( oldStatus ),
100                getNatStateStr( newStatus ) );
101}
102
103static void
104incomingPeersPulse( tr_shared * s )
105{
106    if( s->bindSocket >= 0 && ( s->bindPort != s->publicPort ) )
107    {
108        tr_ninf( getKey( ), _( "Closing port %d" ), s->bindPort );
109        tr_netClose( s->bindSocket );
110        s->bindSocket = -1;
111    }
112
113    if( ( s->publicPort > 0 ) && ( s->bindPort != s->publicPort ) )
114    {
115        int socket;
116        errno = 0;
117        socket = tr_netBindTCP( &tr_inaddr_any, 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 );
124            s->bindPort = s->publicPort;
125            s->bindSocket = socket;
126            listen( s->bindSocket, 5 );
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 ) );
134            s->bindPort = -1;
135            s->bindSocket = -1;
136        }
137    }
138
139    for( ; ; ) /* check for new incoming peer connections */
140    {
141        int         socket;
142        tr_port     port;
143        tr_address  addr;
144
145        if( s->bindSocket < 0 )
146            break;
147
148        socket = tr_netAccept( s->session, s->bindSocket, &addr, &port );
149        if( socket < 0 )
150            break;
151
152        tr_deepLog( __FILE__, __LINE__, NULL,
153                   "New INCOMING connection %d (%s)",
154                   socket, tr_peerIoAddrStr( &addr, port ) );
155
156        tr_peerMgrAddIncoming( s->session->peerMgr, &addr, port, socket );
157    }
158}
159
160static int
161sharedPulse( void * vshared )
162{
163    int         keepPulsing = 1;
164    tr_shared * shared = vshared;
165
166    natPulse( shared );
167
168    if( !shared->isShuttingDown )
169    {
170        incomingPeersPulse( shared );
171    }
172    else
173    {
174        tr_ninf( getKey( ), _( "Stopped" ) );
175        tr_timerFree( &shared->pulseTimer );
176        tr_netClose( shared->bindSocket );
177        tr_natpmpClose( shared->natpmp );
178        tr_upnpClose( shared->upnp );
179        shared->session->shared = NULL;
180        tr_free( shared );
181        keepPulsing = 0;
182    }
183
184    return keepPulsing;
185}
186
187/***
188****
189***/
190
191tr_shared *
192tr_sharedInit( tr_session  * session,
193               int           isEnabled,
194               int           publicPort )
195{
196    tr_shared * s = tr_new0( tr_shared, 1 );
197
198    s->session      = session;
199    s->publicPort   = publicPort;
200    s->bindPort     = -1;
201    s->bindSocket   = -1;
202    s->natpmp       = tr_natpmpInit( );
203    s->upnp         = tr_upnpInit( );
204    s->pulseTimer   = tr_timerNew( session, sharedPulse, s, 1000 );
205    s->isEnabled    = isEnabled ? 1 : 0;
206    s->upnpStatus   = TR_PORT_UNMAPPED;
207    s->natpmpStatus = TR_PORT_UNMAPPED;
208
209    return s;
210}
211
212void
213tr_sharedShuttingDown( tr_shared * s )
214{
215    s->isShuttingDown = 1;
216}
217
218void
219tr_sharedSetPort( tr_shared * s,
220                  int         port )
221{
222    tr_torrent * tor = NULL;
223
224    s->publicPort = port;
225
226    while( ( tor = tr_torrentNext( s->session, tor ) ) )
227        tr_torrentChangeMyPort( tor );
228}
229
230int
231tr_sharedGetPeerPort( const tr_shared * s )
232{
233    return s->publicPort;
234}
235
236void
237tr_sharedTraversalEnable( tr_shared * s,
238                          int         isEnabled )
239{
240    s->isEnabled = isEnabled;
241}
242
243int
244tr_sharedTraversalIsEnabled( const tr_shared * s )
245{
246    return s->isEnabled;
247}
248
249int
250tr_sharedTraversalStatus( const tr_shared * s )
251{
252    return MAX( s->natpmpStatus, s->upnpStatus );
253}
254
Note: See TracBrowser for help on using the repository browser.