source: trunk/libtransmission/transmission.c @ 3217

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

fix a couple of memory corruption errors while trying to track down tiennou's report on peer-msgs.c:pulse() -> inout.c:163 assertion failure.

  • Property svn:keywords set to Date Rev Author Id
File size: 8.9 KB
Line 
1/******************************************************************************
2 * $Id: transmission.c 3217 2007-09-28 14:27:56Z 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
113#ifndef WIN32
114    /* Don't exit when writing on a broken socket */
115    signal( SIGPIPE, SIG_IGN );
116#endif
117
118    tr_msgInit();
119
120    h = tr_new0( tr_handle, 1 );
121    if( !h )
122        return NULL;
123
124    h->encryptionMode = TR_ENCRYPTION_PREFERRED;
125
126    tr_netInit(); /* must go before tr_eventInit */
127
128    tr_eventInit( h );
129    while( !h->events )
130        tr_wait( 50 );
131
132    h->tag = strdup( tag );
133    if( !h->tag ) {
134        free( h );
135        return NULL;
136    }
137
138    h->peerMgr = tr_peerMgrNew( h );
139
140    /* Azureus identity */
141    for( i=0; i < TR_AZ_ID_LEN; ++i )
142        h->azId[i] = tr_rand( 0xff );
143
144    /* Initialize rate and file descripts controls */
145    h->upload   = tr_rcInit();
146    h->download = tr_rcInit();
147
148    tr_fdInit();
149    h->shared = tr_sharedInit( h );
150
151    tr_inf( TR_NAME " " LONG_VERSION_STRING " started" );
152
153    return h;
154}
155
156/***********************************************************************
157 * tr_setBindPort
158 ***********************************************************************
159 *
160 **********************************************************************/
161void tr_setBindPort( tr_handle * h, int port )
162{
163    h->isPortSet = 1;
164    tr_sharedSetPort( h->shared, port );
165}
166
167int
168tr_getPublicPort( const tr_handle * h )
169{
170    assert( h != NULL );
171    return tr_sharedGetPublicPort( h->shared );
172}
173
174void tr_natTraversalEnable( tr_handle * h, int enable )
175{
176    tr_sharedLock( h->shared );
177    tr_sharedTraversalEnable( h->shared, enable );
178    tr_sharedUnlock( h->shared );
179}
180
181tr_handle_status * tr_handleStatus( tr_handle * h )
182{
183    tr_handle_status * s;
184
185    h->statCur = ( h->statCur + 1 ) % 2;
186    s = &h->stats[h->statCur];
187
188    tr_sharedLock( h->shared );
189
190    s->natTraversalStatus = tr_sharedTraversalStatus( h->shared );
191    s->publicPort = tr_sharedGetPublicPort( h->shared );
192
193    tr_sharedUnlock( h->shared );
194
195    return s;
196}
197
198/***
199****
200***/
201
202void
203tr_setUseGlobalSpeedLimit( tr_handle  * h,
204                           int          up_or_down,
205                           int          use_flag )
206{
207    char * ch = up_or_down==TR_UP ? &h->useUploadLimit
208                                  : &h->useDownloadLimit;
209    *ch = use_flag;
210}
211
212void
213tr_setGlobalSpeedLimit( tr_handle  * h,
214                        int          up_or_down,
215                        int          KiB_sec )
216{
217    if( up_or_down == TR_DOWN )
218        tr_rcSetLimit( h->download, KiB_sec );
219    else
220        tr_rcSetLimit( h->upload, KiB_sec );
221}
222
223void
224tr_getGlobalSpeedLimit( tr_handle  * h,
225                        int          up_or_down,
226                        int        * setme_enabled,
227                        int          * setme_KiBsec )
228{
229    if( setme_enabled != NULL )
230       *setme_enabled = up_or_down==TR_UP ? h->useUploadLimit
231                                          : h->useDownloadLimit;
232    if( setme_KiBsec != NULL )
233       *setme_KiBsec = tr_rcGetLimit( up_or_down==TR_UP ? h->upload
234                                                        : h->download );
235}
236
237void
238tr_torrentRates( tr_handle * h, float * dl, float * ul )
239{
240    tr_torrent * tor;
241
242    *dl = 0.0;
243    *ul = 0.0;
244    tr_sharedLock( h->shared );
245    for( tor = h->torrentList; tor; tor = tor->next )
246    {
247        tr_torrentLock( tor );
248        if( tor->cpStatus == TR_CP_INCOMPLETE )
249            *dl += tr_rcRate( tor->download );
250        *ul += tr_rcRate( tor->upload );
251        tr_torrentUnlock( tor );
252    }
253    tr_sharedUnlock( h->shared );
254}
255
256int
257tr_torrentCount( tr_handle * h )
258{
259    return h->torrentCount;
260}
261
262void
263tr_torrentIterate( tr_handle * h, tr_callback_t func, void * d )
264{
265    tr_torrent * tor, * next;
266
267    for( tor = h->torrentList; tor; tor = next )
268    {
269        next = tor->next;
270        func( tor, d );
271    }
272}
273
274static void
275tr_closeImpl( void * vh )
276{
277    tr_handle * h = vh;
278    tr_peerMgrFree( h->peerMgr );
279
280    tr_rcClose( h->upload );
281    tr_rcClose( h->download );
282
283    tr_sharedClose( h->shared );
284    tr_fdClose();
285
286    h->isClosed = TRUE;
287}
288void
289tr_close( tr_handle * h )
290{
291    assert( tr_torrentCount( h ) == 0 );
292
293    tr_runInEventThread( h, tr_closeImpl, h );
294    while( !h->isClosed )
295        tr_wait( 200 );
296
297    tr_eventClose( h );
298    while( h->events != NULL ) {
299        fprintf( stderr, "waiting for libevent thread to close...\n" );
300        tr_wait( 200 );
301    }
302
303    free( h->tag );
304    free( h );
305    fprintf( stderr, "tr_close() completed.\n" );
306}
307
308tr_torrent **
309tr_loadTorrents ( tr_handle   * h,
310                  const char  * destination,
311                  int           flags,
312                  int         * setmeCount )
313{
314    int i, n = 0;
315    struct stat sb;
316    DIR * odir = NULL;
317    const char * torrentDir = tr_getTorrentsDirectory( );
318    tr_torrent ** torrents;
319    tr_list *l=NULL, *list=NULL;
320
321    if( !stat( torrentDir, &sb )
322        && S_ISDIR( sb.st_mode )
323        && (( odir = opendir ( torrentDir ) )) )
324    {
325        struct dirent *d;
326        for (d = readdir( odir ); d!=NULL; d=readdir( odir ) )
327        {
328            if( d->d_name && d->d_name[0]!='.' ) /* skip dotfiles, ., and .. */
329            {
330                tr_torrent * tor;
331                char path[MAX_PATH_LENGTH];
332                tr_buildPath( path, sizeof(path), torrentDir, d->d_name, NULL );
333                tor = tr_torrentInit( h, path, destination, flags, NULL );
334                if( tor != NULL ) {
335                    tr_list_append( &list, tor );
336                    //fprintf (stderr, "#%d - %s\n", n, tor->info.name );
337                    n++;
338                }
339            }
340        }
341        closedir( odir );
342    }
343
344    torrents = tr_new( tr_torrent*, n );
345    for( i=0, l=list; l!=NULL; l=l->next )
346        torrents[i++] = (tr_torrent*) l->data;
347    assert( i==n );
348
349    tr_list_free( &list, NULL );
350
351    *setmeCount = n;
352    tr_inf( "Loaded %d torrents from disk", *setmeCount );
353    return torrents;
354}
Note: See TracBrowser for help on using the repository browser.