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

Last change on this file since 9437 was 9437, checked in by charles, 13 years ago

(trunk libT) #2538 startup takes too long

  • Property svn:keywords set to Date Rev Author Id
File size: 5.0 KB
Line 
1/*
2 * This file Copyright (C) 2008-2009 Charles Kerr <charles@transmissionbt.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 9437 2009-10-29 17:25:03Z 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 "session.h"
26#include "torrent.h"
27#include "trevent.h"
28#include "upnp.h"
29#include "utils.h"
30
31static const char *
32getKey( void ) { return _( "Port Forwarding" ); }
33
34struct tr_shared
35{
36    tr_bool               isEnabled;
37    tr_bool               isShuttingDown;
38    tr_bool               doPortCheck;
39
40    tr_port_forwarding    natpmpStatus;
41    tr_port_forwarding    upnpStatus;
42
43    tr_upnp             * upnp;
44    tr_natpmp           * natpmp;
45    tr_session          * session;
46
47    struct event        * timer;
48};
49
50/***
51****
52***/
53
54static const char*
55getNatStateStr( int state )
56{
57    switch( state )
58    {
59        case TR_PORT_MAPPING:   return _( "Starting" );
60        case TR_PORT_MAPPED:    return _( "Forwarded" );
61        case TR_PORT_UNMAPPING: return _( "Stopping" );
62        case TR_PORT_UNMAPPED:  return _( "Not forwarded" );
63        default:                return "???";
64    }
65}
66
67static void
68natPulse( tr_shared * s, tr_bool doPortCheck )
69{
70    const tr_port port = s->session->peerPort;
71    const int isEnabled = s->isEnabled && !s->isShuttingDown;
72    int oldStatus;
73    int newStatus;
74
75    if( s->natpmp == NULL )
76        s->natpmp = tr_natpmpInit( );
77    if( s->upnp == NULL )
78        s->upnp = tr_upnpInit( );
79
80    oldStatus = tr_sharedTraversalStatus( s );
81    s->natpmpStatus = tr_natpmpPulse( s->natpmp, port, isEnabled );
82    s->upnpStatus = tr_upnpPulse( s->upnp, port, isEnabled, doPortCheck );
83    newStatus = tr_sharedTraversalStatus( s );
84
85    if( newStatus != oldStatus )
86        tr_ninf( getKey( ), _( "State changed from \"%1$s\" to \"%2$s\"" ),
87                getNatStateStr( oldStatus ),
88                getNatStateStr( newStatus ) );
89}
90
91static void
92set_evtimer_from_status( tr_shared * s )
93{
94    int sec=0, msec=0;
95
96    /* when to wake up again */
97    switch( tr_sharedTraversalStatus( s ) )
98    {
99        case TR_PORT_MAPPED:
100            /* if we're mapped, everything is fine... check back in 20 minutes
101             * to renew the port forwarding if it's expired */
102            s->doPortCheck = TRUE;
103            sec = 60 * 20;
104            break;
105
106        case TR_PORT_ERROR:
107            /* some kind of an error.  wait 60 seconds and retry */
108            sec = 60;
109            break;
110
111        default:
112            /* in progress.  pulse frequently. */
113            msec = 333000;
114            break;
115    }
116
117    tr_timerAdd( s->timer, sec, msec );
118}
119
120static void
121onTimer( int fd UNUSED, short what UNUSED, void * vshared )
122{
123    tr_shared * s = vshared;
124
125    assert( s );
126    assert( s->timer );
127
128    /* do something */
129    natPulse( s, s->doPortCheck );
130    s->doPortCheck = FALSE;
131
132    /* set up the timer for the next pulse */
133    set_evtimer_from_status( s );
134}
135
136/***
137****
138***/
139
140tr_shared *
141tr_sharedInit( tr_session  * session )
142{
143    tr_shared * s = tr_new0( tr_shared, 1 );
144
145    s->session      = session;
146    s->isEnabled    = FALSE;
147    s->upnpStatus   = TR_PORT_UNMAPPED;
148    s->natpmpStatus = TR_PORT_UNMAPPED;
149
150#if 0
151    if( isEnabled )
152    {
153        s->timer = tr_new0( struct event, 1 );
154        evtimer_set( s->timer, onTimer, s );
155        tr_timerAdd( s->timer, 0, 333000 );
156    }
157#endif
158
159    return s;
160}
161
162static void
163stop_timer( tr_shared * s )
164{
165    if( s->timer != NULL )
166    {
167        evtimer_del( s->timer );
168        tr_free( s->timer );
169        s->timer = NULL;
170    }
171}
172
173static void
174stop_forwarding( tr_shared * s )
175{
176    tr_ninf( getKey( ), "%s", _( "Stopped" ) );
177    natPulse( s, FALSE );
178    tr_natpmpClose( s->natpmp );
179    s->natpmp = NULL;
180    tr_upnpClose( s->upnp );
181    s->upnp = NULL;
182    stop_timer( s );
183}
184
185void
186tr_sharedClose( tr_session * session )
187{
188    tr_shared * s = session->shared;
189
190    s->isShuttingDown = TRUE;
191    stop_forwarding( s );
192    s->session->shared = NULL;
193    tr_free( s );
194}
195
196static void
197start_timer( tr_shared * s )
198{
199    s->timer = tr_new0( struct event, 1 );
200    evtimer_set( s->timer, onTimer, s );
201    set_evtimer_from_status( s );
202}
203
204void
205tr_sharedTraversalEnable( tr_shared * s, tr_bool isEnabled )
206{
207    if(( s->isEnabled = isEnabled ))
208        start_timer( s );
209    else
210        stop_forwarding( s );
211}
212
213void
214tr_sharedPortChanged( tr_session * session )
215{
216    tr_shared * s = session->shared;
217
218    if( s->isEnabled )
219    {
220        stop_timer( s );
221        start_timer( s );
222    }
223}
224
225tr_bool
226tr_sharedTraversalIsEnabled( const tr_shared * s )
227{
228    return s->isEnabled;
229}
230
231int
232tr_sharedTraversalStatus( const tr_shared * s )
233{
234    return MAX( s->natpmpStatus, s->upnpStatus );
235}
Note: See TracBrowser for help on using the repository browser.