source: trunk/libtransmission/transmission.c @ 3389

Last change on this file since 3389 was 3389, checked in by charles, 15 years ago

break the build so svn users won't corrupt their fastresume files

  • Property svn:keywords set to Date Rev Author Id
File size: 9.1 KB
Line 
1/******************************************************************************
2 * $Id: transmission.c 3389 2007-10-13 04:11:11Z charles $
3 *
4 * Copyright (c) 2005-2007 Transmission authors and contributors
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 * DEALINGS IN THE SOFTWARE.
23 *****************************************************************************/
24
25#include <assert.h>
26#include <stdio.h>
27#include <stdlib.h>
28#include <string.h>
29
30#include <signal.h>
31#include <sys/types.h> /* stat */
32#include <sys/stat.h> /* stat */
33#include <unistd.h> /* stat */
34#include <dirent.h> /* opendir */
35
36#include "transmission.h"
37#include "fdlimit.h"
38#include "list.h"
39#include "net.h"
40#include "peer-mgr.h"
41#include "platform.h"
42#include "ratecontrol.h"
43#include "shared.h"
44#include "trevent.h"
45#include "utils.h"
46
47/* Generate a peer id : "-TRxyzb-" + 12 random alphanumeric
48   characters, where x is the major version number, y is the
49   minor version number, z is the maintenance number, and b
50   designates beta (Azureus-style) */
51void
52tr_peerIdNew ( char * buf, int buflen )
53{
54    int i;
55    assert( buflen == TR_ID_LEN + 1 );
56
57    snprintf( buf, TR_ID_LEN, "%s", PEERID_PREFIX );
58    assert( strlen(buf) == 8 );
59    for( i=8; i<TR_ID_LEN; ++i ) {
60        const int r = tr_rand( 36 );
61        buf[i] = ( r < 26 ) ? ( 'a' + r ) : ( '0' + r - 26 ) ;
62    }
63    buf[TR_ID_LEN] = '\0';
64}
65
66const char*
67getPeerId( void )
68{
69    static char * peerId = NULL;
70    if( !peerId ) {
71        peerId = tr_new0( char, TR_ID_LEN + 1 );
72        tr_peerIdNew( peerId, TR_ID_LEN + 1 );
73    }
74    return peerId;
75}
76
77/***
78****
79***/
80
81tr_encryption_mode
82tr_getEncryptionMode( tr_handle * handle )
83{
84    assert( handle != NULL );
85
86    return handle->encryptionMode;
87}
88
89void
90tr_setEncryptionMode( tr_handle * handle, tr_encryption_mode mode )
91{
92    assert( handle != NULL );
93    assert( mode==TR_ENCRYPTION_PREFERRED || mode==TR_ENCRYPTION_REQUIRED );
94
95    handle->encryptionMode = mode;
96}
97
98/***
99****
100***/
101
102
103/***********************************************************************
104 * tr_init
105 ***********************************************************************
106 * Allocates a tr_handle structure and initializes a few things
107 **********************************************************************/
108tr_handle * tr_init( const char * tag )
109{
110    tr_handle * h;
111    int         i;
112
113ccc
114#ifndef WIN32
115    /* Don't exit when writing on a broken socket */
116    signal( SIGPIPE, SIG_IGN );
117#endif
118
119    tr_msgInit();
120
121    h = tr_new0( tr_handle, 1 );
122    if( !h )
123        return NULL;
124
125    h->lock = tr_lockNew( );
126
127    h->encryptionMode = TR_ENCRYPTION_PREFERRED;
128
129    tr_netInit(); /* must go before tr_eventInit */
130
131    tr_eventInit( h );
132    while( !h->events )
133        tr_wait( 50 );
134
135    h->tag = strdup( tag );
136    if( !h->tag ) {
137        free( h );
138        return NULL;
139    }
140
141    h->peerMgr = tr_peerMgrNew( h );
142
143    /* Azureus identity */
144    for( i=0; i < TR_AZ_ID_LEN; ++i )
145        h->azId[i] = tr_rand( 0xff );
146
147    /* Initialize rate and file descripts controls */
148    h->upload   = tr_rcInit();
149    h->download = tr_rcInit();
150
151    tr_fdInit();
152    h->shared = tr_sharedInit( h );
153
154    tr_inf( TR_NAME " " LONG_VERSION_STRING " started" );
155
156    return h;
157}
158
159/***
160****
161***/
162
163void
164tr_globalLock( struct tr_handle * handle )
165{
166    tr_lockLock( handle->lock );
167}
168
169void
170tr_globalUnlock( struct tr_handle * handle )
171{
172    tr_lockUnlock( handle->lock );
173}
174
175int
176tr_globalIsLocked( const struct tr_handle * handle )
177{
178    return tr_lockHave( handle->lock );
179}
180
181/***********************************************************************
182 * tr_setBindPort
183 ***********************************************************************
184 *
185 **********************************************************************/
186void tr_setBindPort( tr_handle * h, int port )
187{
188    h->isPortSet = 1;
189    tr_sharedSetPort( h->shared, port );
190}
191
192int
193tr_getPublicPort( const tr_handle * h )
194{
195    assert( h != NULL );
196    return tr_sharedGetPublicPort( h->shared );
197}
198
199void tr_natTraversalEnable( tr_handle * h, int enable )
200{
201    tr_globalLock( h );
202    tr_sharedTraversalEnable( h->shared, enable );
203    tr_globalUnlock( h );
204}
205
206tr_handle_status * tr_handleStatus( tr_handle * h )
207{
208    tr_handle_status * s;
209
210    h->statCur = ( h->statCur + 1 ) % 2;
211    s = &h->stats[h->statCur];
212
213    tr_globalLock( h );
214
215    s->natTraversalStatus = tr_sharedTraversalStatus( h->shared );
216    s->publicPort = tr_sharedGetPublicPort( h->shared );
217
218    tr_globalUnlock( h );
219
220    return s;
221}
222
223/***
224****
225***/
226
227void
228tr_setUseGlobalSpeedLimit( tr_handle  * h,
229                           int          up_or_down,
230                           int          use_flag )
231{
232    char * ch = up_or_down==TR_UP ? &h->useUploadLimit
233                                  : &h->useDownloadLimit;
234    *ch = use_flag;
235}
236
237void
238tr_setGlobalSpeedLimit( tr_handle  * h,
239                        int          up_or_down,
240                        int          KiB_sec )
241{
242    if( up_or_down == TR_DOWN )
243        tr_rcSetLimit( h->download, KiB_sec );
244    else
245        tr_rcSetLimit( h->upload, KiB_sec );
246}
247
248void
249tr_getGlobalSpeedLimit( tr_handle  * h,
250                        int          up_or_down,
251                        int        * setme_enabled,
252                        int          * setme_KiBsec )
253{
254    if( setme_enabled != NULL )
255       *setme_enabled = up_or_down==TR_UP ? h->useUploadLimit
256                                          : h->useDownloadLimit;
257    if( setme_KiBsec != NULL )
258       *setme_KiBsec = tr_rcGetLimit( up_or_down==TR_UP ? h->upload
259                                                        : h->download );
260}
261
262void
263tr_torrentRates( tr_handle * h, float * dl, float * ul )
264{
265    tr_torrent * tor;
266
267    *dl = 0.0;
268    *ul = 0.0;
269    tr_globalLock( h );
270    for( tor = h->torrentList; tor; tor = tor->next )
271    {
272        tr_torrentLock( tor );
273        if( tor->cpStatus == TR_CP_INCOMPLETE )
274            *dl += tr_rcRate( tor->download );
275        *ul += tr_rcRate( tor->upload );
276        tr_torrentUnlock( tor );
277    }
278    tr_globalUnlock( h );
279}
280
281int
282tr_torrentCount( tr_handle * h )
283{
284    return h->torrentCount;
285}
286
287void
288tr_torrentIterate( tr_handle * h, tr_callback_t func, void * d )
289{
290    tr_torrent * tor, * next;
291
292    for( tor = h->torrentList; tor; tor = next )
293    {
294        next = tor->next;
295        func( tor, d );
296    }
297}
298
299static void
300tr_closeImpl( void * vh )
301{
302    tr_handle * h = vh;
303    tr_peerMgrFree( h->peerMgr );
304
305    tr_rcClose( h->upload );
306    tr_rcClose( h->download );
307
308    tr_sharedClose( h->shared );
309    tr_fdClose();
310
311    h->isClosed = TRUE;
312}
313void
314tr_close( tr_handle * h )
315{
316    tr_runInEventThread( h, tr_closeImpl, h );
317    while( !h->isClosed )
318        tr_wait( 100 );
319
320    tr_eventClose( h );
321    while( h->events != NULL )
322        tr_wait( 100 );
323
324    tr_lockFree( h->lock );
325    free( h->tag );
326    free( h );
327    fprintf( stderr, "libtransmission closed cleanly.\n" );
328}
329
330tr_torrent **
331tr_loadTorrents ( tr_handle   * h,
332                  const char  * destination,
333                  int           flags,
334                  int         * setmeCount )
335{
336    int i, n = 0;
337    struct stat sb;
338    DIR * odir = NULL;
339    const char * torrentDir = tr_getTorrentsDirectory( );
340    tr_torrent ** torrents;
341    tr_list *l=NULL, *list=NULL;
342
343    if( !stat( torrentDir, &sb )
344        && S_ISDIR( sb.st_mode )
345        && (( odir = opendir ( torrentDir ) )) )
346    {
347        struct dirent *d;
348        for (d = readdir( odir ); d!=NULL; d=readdir( odir ) )
349        {
350            if( d->d_name && d->d_name[0]!='.' ) /* skip dotfiles, ., and .. */
351            {
352                tr_torrent * tor;
353                char path[MAX_PATH_LENGTH];
354                tr_buildPath( path, sizeof(path), torrentDir, d->d_name, NULL );
355                tor = tr_torrentInit( h, path, destination, flags, NULL );
356                if( tor != NULL ) {
357                    tr_list_append( &list, tor );
358                    n++;
359                }
360            }
361        }
362        closedir( odir );
363    }
364
365    torrents = tr_new( tr_torrent*, n );
366    for( i=0, l=list; l!=NULL; l=l->next )
367        torrents[i++] = (tr_torrent*) l->data;
368    assert( i==n );
369
370    tr_list_free( &list, NULL );
371
372    *setmeCount = n;
373    tr_inf( "Loaded %d torrents from disk", *setmeCount );
374    return torrents;
375}
Note: See TracBrowser for help on using the repository browser.