1 | /****************************************************************************** |
---|
2 | * $Id: transmission.c 3295 2007-10-06 18:20:52Z 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) */ |
---|
51 | void |
---|
52 | tr_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 | |
---|
66 | const char* |
---|
67 | getPeerId( 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 | |
---|
81 | tr_encryption_mode |
---|
82 | tr_getEncryptionMode( tr_handle * handle ) |
---|
83 | { |
---|
84 | assert( handle != NULL ); |
---|
85 | |
---|
86 | return handle->encryptionMode; |
---|
87 | } |
---|
88 | |
---|
89 | void |
---|
90 | tr_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 | **********************************************************************/ |
---|
108 | tr_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->lock = tr_lockNew( ); |
---|
125 | |
---|
126 | h->encryptionMode = TR_ENCRYPTION_PREFERRED; |
---|
127 | |
---|
128 | tr_netInit(); /* must go before tr_eventInit */ |
---|
129 | |
---|
130 | tr_eventInit( h ); |
---|
131 | while( !h->events ) |
---|
132 | tr_wait( 50 ); |
---|
133 | |
---|
134 | h->tag = strdup( tag ); |
---|
135 | if( !h->tag ) { |
---|
136 | free( h ); |
---|
137 | return NULL; |
---|
138 | } |
---|
139 | |
---|
140 | h->peerMgr = tr_peerMgrNew( h ); |
---|
141 | |
---|
142 | /* Azureus identity */ |
---|
143 | for( i=0; i < TR_AZ_ID_LEN; ++i ) |
---|
144 | h->azId[i] = tr_rand( 0xff ); |
---|
145 | |
---|
146 | /* Initialize rate and file descripts controls */ |
---|
147 | h->upload = tr_rcInit(); |
---|
148 | h->download = tr_rcInit(); |
---|
149 | |
---|
150 | tr_fdInit(); |
---|
151 | h->shared = tr_sharedInit( h ); |
---|
152 | |
---|
153 | tr_inf( TR_NAME " " LONG_VERSION_STRING " started" ); |
---|
154 | |
---|
155 | return h; |
---|
156 | } |
---|
157 | |
---|
158 | /*** |
---|
159 | **** |
---|
160 | ***/ |
---|
161 | |
---|
162 | void |
---|
163 | tr_globalLock( struct tr_handle * handle ) |
---|
164 | { |
---|
165 | tr_lockLock( handle->lock ); |
---|
166 | } |
---|
167 | |
---|
168 | void |
---|
169 | tr_globalUnlock( struct tr_handle * handle ) |
---|
170 | { |
---|
171 | tr_lockUnlock( handle->lock ); |
---|
172 | } |
---|
173 | |
---|
174 | int |
---|
175 | tr_globalIsLocked( const struct tr_handle * handle ) |
---|
176 | { |
---|
177 | return tr_lockHave( handle->lock ); |
---|
178 | } |
---|
179 | |
---|
180 | /*********************************************************************** |
---|
181 | * tr_setBindPort |
---|
182 | *********************************************************************** |
---|
183 | * |
---|
184 | **********************************************************************/ |
---|
185 | void tr_setBindPort( tr_handle * h, int port ) |
---|
186 | { |
---|
187 | h->isPortSet = 1; |
---|
188 | tr_sharedSetPort( h->shared, port ); |
---|
189 | } |
---|
190 | |
---|
191 | int |
---|
192 | tr_getPublicPort( const tr_handle * h ) |
---|
193 | { |
---|
194 | assert( h != NULL ); |
---|
195 | return tr_sharedGetPublicPort( h->shared ); |
---|
196 | } |
---|
197 | |
---|
198 | void tr_natTraversalEnable( tr_handle * h, int enable ) |
---|
199 | { |
---|
200 | tr_globalLock( h ); |
---|
201 | tr_sharedTraversalEnable( h->shared, enable ); |
---|
202 | tr_globalUnlock( h ); |
---|
203 | } |
---|
204 | |
---|
205 | tr_handle_status * tr_handleStatus( tr_handle * h ) |
---|
206 | { |
---|
207 | tr_handle_status * s; |
---|
208 | |
---|
209 | h->statCur = ( h->statCur + 1 ) % 2; |
---|
210 | s = &h->stats[h->statCur]; |
---|
211 | |
---|
212 | tr_globalLock( h ); |
---|
213 | |
---|
214 | s->natTraversalStatus = tr_sharedTraversalStatus( h->shared ); |
---|
215 | s->publicPort = tr_sharedGetPublicPort( h->shared ); |
---|
216 | |
---|
217 | tr_globalUnlock( h ); |
---|
218 | |
---|
219 | return s; |
---|
220 | } |
---|
221 | |
---|
222 | /*** |
---|
223 | **** |
---|
224 | ***/ |
---|
225 | |
---|
226 | void |
---|
227 | tr_setUseGlobalSpeedLimit( tr_handle * h, |
---|
228 | int up_or_down, |
---|
229 | int use_flag ) |
---|
230 | { |
---|
231 | char * ch = up_or_down==TR_UP ? &h->useUploadLimit |
---|
232 | : &h->useDownloadLimit; |
---|
233 | *ch = use_flag; |
---|
234 | } |
---|
235 | |
---|
236 | void |
---|
237 | tr_setGlobalSpeedLimit( tr_handle * h, |
---|
238 | int up_or_down, |
---|
239 | int KiB_sec ) |
---|
240 | { |
---|
241 | if( up_or_down == TR_DOWN ) |
---|
242 | tr_rcSetLimit( h->download, KiB_sec ); |
---|
243 | else |
---|
244 | tr_rcSetLimit( h->upload, KiB_sec ); |
---|
245 | } |
---|
246 | |
---|
247 | void |
---|
248 | tr_getGlobalSpeedLimit( tr_handle * h, |
---|
249 | int up_or_down, |
---|
250 | int * setme_enabled, |
---|
251 | int * setme_KiBsec ) |
---|
252 | { |
---|
253 | if( setme_enabled != NULL ) |
---|
254 | *setme_enabled = up_or_down==TR_UP ? h->useUploadLimit |
---|
255 | : h->useDownloadLimit; |
---|
256 | if( setme_KiBsec != NULL ) |
---|
257 | *setme_KiBsec = tr_rcGetLimit( up_or_down==TR_UP ? h->upload |
---|
258 | : h->download ); |
---|
259 | } |
---|
260 | |
---|
261 | void |
---|
262 | tr_torrentRates( tr_handle * h, float * dl, float * ul ) |
---|
263 | { |
---|
264 | tr_torrent * tor; |
---|
265 | |
---|
266 | *dl = 0.0; |
---|
267 | *ul = 0.0; |
---|
268 | tr_globalLock( h ); |
---|
269 | for( tor = h->torrentList; tor; tor = tor->next ) |
---|
270 | { |
---|
271 | tr_torrentLock( tor ); |
---|
272 | if( tor->cpStatus == TR_CP_INCOMPLETE ) |
---|
273 | *dl += tr_rcRate( tor->download ); |
---|
274 | *ul += tr_rcRate( tor->upload ); |
---|
275 | tr_torrentUnlock( tor ); |
---|
276 | } |
---|
277 | tr_globalUnlock( h ); |
---|
278 | } |
---|
279 | |
---|
280 | int |
---|
281 | tr_torrentCount( tr_handle * h ) |
---|
282 | { |
---|
283 | return h->torrentCount; |
---|
284 | } |
---|
285 | |
---|
286 | void |
---|
287 | tr_torrentIterate( tr_handle * h, tr_callback_t func, void * d ) |
---|
288 | { |
---|
289 | tr_torrent * tor, * next; |
---|
290 | |
---|
291 | for( tor = h->torrentList; tor; tor = next ) |
---|
292 | { |
---|
293 | next = tor->next; |
---|
294 | func( tor, d ); |
---|
295 | } |
---|
296 | } |
---|
297 | |
---|
298 | static void |
---|
299 | tr_closeImpl( void * vh ) |
---|
300 | { |
---|
301 | tr_handle * h = vh; |
---|
302 | tr_peerMgrFree( h->peerMgr ); |
---|
303 | |
---|
304 | tr_rcClose( h->upload ); |
---|
305 | tr_rcClose( h->download ); |
---|
306 | |
---|
307 | tr_sharedClose( h->shared ); |
---|
308 | tr_fdClose(); |
---|
309 | |
---|
310 | h->isClosed = TRUE; |
---|
311 | } |
---|
312 | void |
---|
313 | tr_close( tr_handle * h ) |
---|
314 | { |
---|
315 | //assert( tr_torrentCount( h ) == 0 ); |
---|
316 | |
---|
317 | tr_runInEventThread( h, tr_closeImpl, h ); |
---|
318 | while( !h->isClosed ) |
---|
319 | tr_wait( 100 ); |
---|
320 | |
---|
321 | tr_eventClose( h ); |
---|
322 | while( h->events != NULL ) |
---|
323 | tr_wait( 100 ); |
---|
324 | |
---|
325 | tr_lockFree( h->lock ); |
---|
326 | free( h->tag ); |
---|
327 | free( h ); |
---|
328 | fprintf( stderr, "libtransmission closed cleanly.\n" ); |
---|
329 | } |
---|
330 | |
---|
331 | tr_torrent ** |
---|
332 | tr_loadTorrents ( tr_handle * h, |
---|
333 | const char * destination, |
---|
334 | int flags, |
---|
335 | int * setmeCount ) |
---|
336 | { |
---|
337 | int i, n = 0; |
---|
338 | struct stat sb; |
---|
339 | DIR * odir = NULL; |
---|
340 | const char * torrentDir = tr_getTorrentsDirectory( ); |
---|
341 | tr_torrent ** torrents; |
---|
342 | tr_list *l=NULL, *list=NULL; |
---|
343 | |
---|
344 | if( !stat( torrentDir, &sb ) |
---|
345 | && S_ISDIR( sb.st_mode ) |
---|
346 | && (( odir = opendir ( torrentDir ) )) ) |
---|
347 | { |
---|
348 | struct dirent *d; |
---|
349 | for (d = readdir( odir ); d!=NULL; d=readdir( odir ) ) |
---|
350 | { |
---|
351 | if( d->d_name && d->d_name[0]!='.' ) /* skip dotfiles, ., and .. */ |
---|
352 | { |
---|
353 | tr_torrent * tor; |
---|
354 | char path[MAX_PATH_LENGTH]; |
---|
355 | tr_buildPath( path, sizeof(path), torrentDir, d->d_name, NULL ); |
---|
356 | tor = tr_torrentInit( h, path, destination, flags, NULL ); |
---|
357 | if( tor != NULL ) { |
---|
358 | tr_list_append( &list, tor ); |
---|
359 | //fprintf (stderr, "#%d - %s\n", n, tor->info.name ); |
---|
360 | n++; |
---|
361 | } |
---|
362 | } |
---|
363 | } |
---|
364 | closedir( odir ); |
---|
365 | } |
---|
366 | |
---|
367 | torrents = tr_new( tr_torrent*, n ); |
---|
368 | for( i=0, l=list; l!=NULL; l=l->next ) |
---|
369 | torrents[i++] = (tr_torrent*) l->data; |
---|
370 | assert( i==n ); |
---|
371 | |
---|
372 | tr_list_free( &list, NULL ); |
---|
373 | |
---|
374 | *setmeCount = n; |
---|
375 | tr_inf( "Loaded %d torrents from disk", *setmeCount ); |
---|
376 | return torrents; |
---|
377 | } |
---|