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:$ |
---|
11 | */ |
---|
12 | |
---|
13 | #include <assert.h> |
---|
14 | #include <ctype.h> /* isdigit */ |
---|
15 | #include <stdlib.h> /* strtol */ |
---|
16 | #include <string.h> /* strcmp */ |
---|
17 | |
---|
18 | #include "transmission.h" |
---|
19 | #include "bencode.h" |
---|
20 | #include "rpc.h" |
---|
21 | #include "json.h" |
---|
22 | #include "session.h" |
---|
23 | #include "torrent.h" |
---|
24 | #include "utils.h" |
---|
25 | |
---|
26 | #define TR_N_ELEMENTS( ary ) ( sizeof( ary ) / sizeof( *ary ) ) |
---|
27 | |
---|
28 | /*** |
---|
29 | **** |
---|
30 | ***/ |
---|
31 | |
---|
32 | static void |
---|
33 | notify( tr_handle * session, int type, tr_torrent * tor ) |
---|
34 | { |
---|
35 | if( session->rpc_func != NULL ) |
---|
36 | session->rpc_func( session, type, tor, session->rpc_func_user_data ); |
---|
37 | } |
---|
38 | |
---|
39 | /*** |
---|
40 | **** |
---|
41 | ***/ |
---|
42 | |
---|
43 | static tr_torrent ** |
---|
44 | getTorrents( tr_handle * handle, tr_benc * args, int * setmeCount ) |
---|
45 | { |
---|
46 | int64_t id; |
---|
47 | tr_torrent ** torrents = NULL; |
---|
48 | int torrentCount = 0; |
---|
49 | tr_benc * ids; |
---|
50 | |
---|
51 | if( tr_bencDictFindList( args, "ids", &ids ) ) |
---|
52 | { |
---|
53 | int i; |
---|
54 | const int n = tr_bencListSize( ids ); |
---|
55 | |
---|
56 | torrents = tr_new0( tr_torrent*, n ); |
---|
57 | |
---|
58 | for( i=0; i<n; ++i ) |
---|
59 | { |
---|
60 | tr_torrent * tor = NULL; |
---|
61 | tr_benc * node = tr_bencListChild( ids, i ); |
---|
62 | int64_t id; |
---|
63 | const char * str; |
---|
64 | if( tr_bencGetInt( node, &id ) ) |
---|
65 | tor = tr_torrentFindFromId( handle, id ); |
---|
66 | else if( tr_bencGetStr( node, &str ) ) |
---|
67 | tor = tr_torrentFindFromHashString( handle, str ); |
---|
68 | if( tor ) |
---|
69 | torrents[torrentCount++] = tor; |
---|
70 | } |
---|
71 | } |
---|
72 | else if( tr_bencDictFindInt( args, "ids", &id ) |
---|
73 | || tr_bencDictFindInt( args, "id", &id ) ) |
---|
74 | { |
---|
75 | tr_torrent * tor; |
---|
76 | torrents = tr_new0( tr_torrent*, 1 ); |
---|
77 | if(( tor = tr_torrentFindFromId( handle, id ))) |
---|
78 | torrents[torrentCount++] = tor; |
---|
79 | } |
---|
80 | else /* all of them */ |
---|
81 | { |
---|
82 | tr_torrent * tor = NULL; |
---|
83 | const int n = tr_torrentCount( handle ); |
---|
84 | torrents = tr_new0( tr_torrent*, n ); |
---|
85 | while(( tor = tr_torrentNext( handle, tor ))) |
---|
86 | torrents[torrentCount++] = tor; |
---|
87 | } |
---|
88 | |
---|
89 | *setmeCount = torrentCount; |
---|
90 | return torrents; |
---|
91 | } |
---|
92 | |
---|
93 | static const char* |
---|
94 | torrentStart( tr_handle * h, tr_benc * args_in, tr_benc * args_out UNUSED ) |
---|
95 | { |
---|
96 | int i, torrentCount; |
---|
97 | tr_torrent ** torrents = getTorrents( h, args_in, &torrentCount ); |
---|
98 | for( i=0; i<torrentCount; ++i ) |
---|
99 | { |
---|
100 | tr_torrent * tor = torrents[i]; |
---|
101 | tr_torrentStart( tor ); |
---|
102 | notify( h, TR_RPC_TORRENT_STARTED, tor ); |
---|
103 | } |
---|
104 | tr_free( torrents ); |
---|
105 | return NULL; |
---|
106 | } |
---|
107 | |
---|
108 | static const char* |
---|
109 | torrentStop( tr_handle * h, tr_benc * args_in, tr_benc * args_out UNUSED ) |
---|
110 | { |
---|
111 | int i, torrentCount; |
---|
112 | tr_torrent ** torrents = getTorrents( h, args_in, &torrentCount ); |
---|
113 | for( i=0; i<torrentCount; ++i ) |
---|
114 | { |
---|
115 | tr_torrent * tor = torrents[i]; |
---|
116 | tr_torrentStop( tor ); |
---|
117 | notify( h, TR_RPC_TORRENT_STOPPED, tor ); |
---|
118 | } |
---|
119 | tr_free( torrents ); |
---|
120 | return NULL; |
---|
121 | } |
---|
122 | |
---|
123 | static const char* |
---|
124 | torrentClose( tr_handle * h, tr_benc * args_in, tr_benc * args_out UNUSED ) |
---|
125 | { |
---|
126 | int i, torrentCount; |
---|
127 | tr_torrent ** torrents = getTorrents( h, args_in, &torrentCount ); |
---|
128 | for( i=0; i<torrentCount; ++i ) |
---|
129 | { |
---|
130 | tr_torrent * tor = torrents[i]; |
---|
131 | notify( h, TR_RPC_TORRENT_CLOSING, tor ); |
---|
132 | tr_torrentClose( tor ); |
---|
133 | } |
---|
134 | tr_free( torrents ); |
---|
135 | return NULL; |
---|
136 | } |
---|
137 | |
---|
138 | static const char* |
---|
139 | torrentVerify( tr_handle * h, tr_benc * args_in, tr_benc * args_out UNUSED ) |
---|
140 | { |
---|
141 | int i, torrentCount; |
---|
142 | tr_torrent ** torrents = getTorrents( h, args_in, &torrentCount ); |
---|
143 | for( i=0; i<torrentCount; ++i ) |
---|
144 | { |
---|
145 | tr_torrent * tor = torrents[i]; |
---|
146 | tr_torrentVerify( tor ); |
---|
147 | notify( h, TR_RPC_TORRENT_CHANGED, tor ); |
---|
148 | } |
---|
149 | tr_free( torrents ); |
---|
150 | return NULL; |
---|
151 | } |
---|
152 | |
---|
153 | /*** |
---|
154 | **** |
---|
155 | ***/ |
---|
156 | |
---|
157 | static const char* |
---|
158 | torrentStatus( tr_handle * handle, tr_benc * args_in, tr_benc * args_out ) |
---|
159 | { |
---|
160 | int i, torrentCount; |
---|
161 | tr_torrent ** torrents = getTorrents( handle, args_in, &torrentCount ); |
---|
162 | tr_benc * list = tr_bencDictAddList( args_out, "torrent-status", torrentCount ); |
---|
163 | |
---|
164 | for( i=0; i<torrentCount; ++i ) |
---|
165 | { |
---|
166 | tr_torrent * tor = torrents[i]; |
---|
167 | const tr_stat * st = tr_torrentStat( tor ); |
---|
168 | const int * f = st->peersFrom; |
---|
169 | const struct tr_tracker_stat * s = &st->trackerStat; |
---|
170 | tr_benc * d = tr_bencListAddDict( list, 33 ); |
---|
171 | tr_benc * t; |
---|
172 | |
---|
173 | tr_bencDictAddInt( d, "activityDate", st->activityDate ); |
---|
174 | tr_bencDictAddInt( d, "completedFromTracker", st->completedFromTracker ); |
---|
175 | tr_bencDictAddInt( d, "corruptEver", st->corruptEver ); |
---|
176 | tr_bencDictAddInt( d, "desiredAvailable", st->desiredAvailable ); |
---|
177 | tr_bencDictAddInt( d, "downloadedEver", st->downloadedEver ); |
---|
178 | tr_bencDictAddInt( d, "error", st->error ); |
---|
179 | tr_bencDictAddStr( d, "errorString", st->errorString ); |
---|
180 | tr_bencDictAddInt( d, "eta", st->eta ); |
---|
181 | tr_bencDictAddInt( d, "haveUnchecked", st->haveUnchecked ); |
---|
182 | tr_bencDictAddInt( d, "haveValid", st->haveValid ); |
---|
183 | tr_bencDictAddInt( d, "id", tr_torrentId( tor ) ); |
---|
184 | tr_bencDictAddInt( d, "leechers", st->leechers ); |
---|
185 | tr_bencDictAddInt( d, "leftUntilDone", st->leftUntilDone ); |
---|
186 | tr_bencDictAddInt( d, "manualAnnounceTime", st->manualAnnounceTime ); |
---|
187 | tr_bencDictAddStr( d, "name", tor->info.name ); |
---|
188 | tr_bencDictAddInt( d, "peersConnected", st->peersConnected ); |
---|
189 | t = tr_bencDictAddDict( d, "peersFrom", 4 ); |
---|
190 | tr_bencDictAddInt( t, "cache", f[TR_PEER_FROM_CACHE] ); |
---|
191 | tr_bencDictAddInt( t, "incoming", f[TR_PEER_FROM_INCOMING] ); |
---|
192 | tr_bencDictAddInt( t, "pex", f[TR_PEER_FROM_PEX] ); |
---|
193 | tr_bencDictAddInt( t, "tracker", f[TR_PEER_FROM_TRACKER] ); |
---|
194 | tr_bencDictAddInt( d, "peersGettingFromUs", st->peersGettingFromUs ); |
---|
195 | tr_bencDictAddInt( d, "peersKnown", st->peersKnown ); |
---|
196 | tr_bencDictAddInt( d, "peersSendingToUs", st->peersSendingToUs ); |
---|
197 | tr_bencDictAddDouble( d, "percentComplete", st->percentComplete ); |
---|
198 | tr_bencDictAddDouble( d, "percentDone", st->percentDone ); |
---|
199 | tr_bencDictAddDouble( d, "rateDownload", st->rateDownload ); |
---|
200 | tr_bencDictAddDouble( d, "rateUpload", st->rateUpload ); |
---|
201 | tr_bencDictAddDouble( d, "ratio", st->ratio ); |
---|
202 | tr_bencDictAddDouble( d, "recheckProgress", st->recheckProgress ); |
---|
203 | tr_bencDictAddInt( d, "seeders", st->seeders ); |
---|
204 | tr_bencDictAddInt( d, "sizeWhenDone", st->sizeWhenDone ); |
---|
205 | tr_bencDictAddInt( d, "startDate", st->startDate ); |
---|
206 | tr_bencDictAddInt( d, "status", st->status ); |
---|
207 | tr_bencDictAddDouble( d, "swarmSpeed", st->swarmSpeed ); |
---|
208 | t = tr_bencDictAddDict( d, "trackerStat", 7 ); |
---|
209 | tr_bencDictAddStr( t, "announceResponse", s->announceResponse ); |
---|
210 | tr_bencDictAddInt( t, "lastAnnounceTime", s->lastAnnounceTime ); |
---|
211 | tr_bencDictAddInt( t, "lastScrapeTime", s->lastScrapeTime ); |
---|
212 | tr_bencDictAddInt( t, "nextAnnounceTime", s->nextAnnounceTime ); |
---|
213 | tr_bencDictAddInt( t, "nextScrapeTime", s->nextScrapeTime ); |
---|
214 | tr_bencDictAddInt( t, "nextScrapeTime", s->nextScrapeTime ); |
---|
215 | tr_bencDictAddStr( t, "scrapeResponse", s->scrapeResponse ); |
---|
216 | tr_bencDictAddInt( d, "uploadedEver", st->uploadedEver ); |
---|
217 | } |
---|
218 | |
---|
219 | tr_free( torrents ); |
---|
220 | return NULL; |
---|
221 | } |
---|
222 | |
---|
223 | /** |
---|
224 | *** |
---|
225 | **/ |
---|
226 | |
---|
227 | static void |
---|
228 | addFiles( const tr_info * info, tr_benc * files ) |
---|
229 | { |
---|
230 | tr_file_index_t i; |
---|
231 | for( i=0; i<info->fileCount; ++i ) |
---|
232 | { |
---|
233 | const tr_file * file = &info->files[i]; |
---|
234 | tr_benc * d = tr_bencListAddDict( files, 4 ); |
---|
235 | tr_bencDictAddInt( d, "dnd", file->dnd ); |
---|
236 | tr_bencDictAddInt( d, "length", file->length ); |
---|
237 | tr_bencDictAddStr( d, "name", file->name ); |
---|
238 | tr_bencDictAddInt( d, "priority", file->priority ); |
---|
239 | } |
---|
240 | } |
---|
241 | |
---|
242 | static void |
---|
243 | addTrackers( const tr_info * info, tr_benc * trackers ) |
---|
244 | { |
---|
245 | int i; |
---|
246 | for( i=0; i<info->trackerCount; ++i ) |
---|
247 | { |
---|
248 | const tr_tracker_info * t = &info->trackers[i]; |
---|
249 | tr_benc * d = tr_bencListAddDict( trackers, 3 ); |
---|
250 | tr_bencDictAddStr( d, "announce", t->announce ); |
---|
251 | tr_bencDictAddStr( d, "scrape", t->scrape ); |
---|
252 | tr_bencDictAddInt( d, "tier", t->tier ); |
---|
253 | } |
---|
254 | } |
---|
255 | |
---|
256 | static void |
---|
257 | addInfo( const tr_torrent * tor, tr_benc * d ) |
---|
258 | { |
---|
259 | const tr_info * inf = tr_torrentInfo( tor ); |
---|
260 | tr_bencInitDict( d, 14 ); |
---|
261 | tr_bencDictAddStr( d, "comment", inf->comment ? inf->comment : "" ); |
---|
262 | tr_bencDictAddStr( d, "creator", inf->creator ? inf->creator : "" ); |
---|
263 | tr_bencDictAddInt( d, "dateCreated", inf->dateCreated ); |
---|
264 | addFiles( inf, tr_bencDictAddList( d, "files", inf->fileCount ) ); |
---|
265 | tr_bencDictAddStr( d, "hashString", inf->hashString ); |
---|
266 | tr_bencDictAddInt( d, "id", tr_torrentId( tor ) ); |
---|
267 | tr_bencDictAddInt( d, "isMultifile", inf->isMultifile ); |
---|
268 | tr_bencDictAddInt( d, "isPrivate", inf->isPrivate ); |
---|
269 | tr_bencDictAddStr( d, "name", inf->name ); |
---|
270 | tr_bencDictAddInt( d, "pieceCount", inf->pieceCount ); |
---|
271 | tr_bencDictAddInt( d, "pieceSize", inf->pieceSize ); |
---|
272 | tr_bencDictAddStr( d, "torrent", inf->torrent ); |
---|
273 | tr_bencDictAddInt( d, "totalSize", inf->totalSize ); |
---|
274 | addTrackers( inf, tr_bencDictAddList( d, "trackers", inf->trackerCount ) ); |
---|
275 | } |
---|
276 | |
---|
277 | static const char* |
---|
278 | torrentInfo( tr_handle * handle, tr_benc * args_in, tr_benc * args_out ) |
---|
279 | { |
---|
280 | int i, torrentCount; |
---|
281 | tr_torrent ** torrents = getTorrents( handle, args_in, &torrentCount ); |
---|
282 | tr_benc * list = tr_bencDictAddList( args_out, "torrent-info", torrentCount ); |
---|
283 | |
---|
284 | for( i=0; i<torrentCount; ++i ) |
---|
285 | addInfo( torrents[i], tr_bencListAdd( list ) ); |
---|
286 | |
---|
287 | tr_free( torrents ); |
---|
288 | return NULL; |
---|
289 | } |
---|
290 | |
---|
291 | /** |
---|
292 | *** |
---|
293 | **/ |
---|
294 | |
---|
295 | static const char* |
---|
296 | torrentList( tr_handle * handle, tr_benc * args_in, tr_benc * args_out ) |
---|
297 | { |
---|
298 | int i, torrentCount; |
---|
299 | tr_torrent ** torrents = getTorrents( handle, args_in, &torrentCount ); |
---|
300 | tr_benc * list = tr_bencDictAddList( args_out, "list", torrentCount ); |
---|
301 | |
---|
302 | for( i=0; i<torrentCount; ++i ) { |
---|
303 | tr_torrent * tor = torrents[i]; |
---|
304 | const tr_stat * st = tr_torrentStat( tor ); |
---|
305 | tr_benc * d = tr_bencListAddDict( list, 6 ); |
---|
306 | tr_bencDictAddInt( d, "id", tr_torrentId( tor ) ); |
---|
307 | tr_bencDictAddStr( d, "name", tor->info.name ); |
---|
308 | tr_bencDictAddDouble( d, "rateDownload", st->rateDownload ); |
---|
309 | tr_bencDictAddDouble( d, "rateUpload", st->rateUpload ); |
---|
310 | tr_bencDictAddDouble( d, "ratio", st->ratio ); |
---|
311 | tr_bencDictAddInt( d, "status", st->status ); |
---|
312 | } |
---|
313 | |
---|
314 | tr_free( torrents ); |
---|
315 | return NULL; |
---|
316 | } |
---|
317 | |
---|
318 | /** |
---|
319 | *** |
---|
320 | **/ |
---|
321 | |
---|
322 | static const char* |
---|
323 | torrentGet( tr_handle * handle, tr_benc * args_in, tr_benc * args_out ) |
---|
324 | { |
---|
325 | int i, torrentCount; |
---|
326 | tr_torrent ** torrents = getTorrents( handle, args_in, &torrentCount ); |
---|
327 | tr_benc * list = tr_bencDictAddList( args_out, "torrents", torrentCount ); |
---|
328 | |
---|
329 | for( i=0; i<torrentCount; ++i ) |
---|
330 | { |
---|
331 | tr_torrent * tor = torrents[i]; |
---|
332 | tr_benc * d = tr_bencListAddDict( list, 6 ); |
---|
333 | tr_bencDictAddInt( d, "id", tr_torrentId( tor ) ); |
---|
334 | tr_bencDictAddInt( d, "peer-limit", |
---|
335 | tr_torrentGetPeerLimit( tor ) ); |
---|
336 | tr_bencDictAddInt( d, "speed-limit-down", |
---|
337 | tr_torrentGetSpeedLimit( tor, TR_DOWN ) ); |
---|
338 | tr_bencDictAddInt( d, "speed-limit-down-enabled", |
---|
339 | tr_torrentGetSpeedMode( tor, TR_DOWN ) |
---|
340 | == TR_SPEEDLIMIT_SINGLE ); |
---|
341 | tr_bencDictAddInt( d, "speed-limit-up", |
---|
342 | tr_torrentGetSpeedLimit( tor, TR_UP ) ); |
---|
343 | tr_bencDictAddInt( d, "speed-limit-up-enabled", |
---|
344 | tr_torrentGetSpeedMode( tor, TR_UP ) |
---|
345 | == TR_SPEEDLIMIT_SINGLE ); |
---|
346 | } |
---|
347 | |
---|
348 | tr_free( torrents ); |
---|
349 | return NULL; |
---|
350 | } |
---|
351 | |
---|
352 | static const char* |
---|
353 | torrentSet( tr_handle * h, tr_benc * args_in, tr_benc * args_out UNUSED ) |
---|
354 | { |
---|
355 | int i, torrentCount; |
---|
356 | tr_torrent ** torrents = getTorrents( h, args_in, &torrentCount ); |
---|
357 | |
---|
358 | for( i=0; i<torrentCount; ++i ) |
---|
359 | { |
---|
360 | int64_t tmp; |
---|
361 | tr_torrent * tor = torrents[i]; |
---|
362 | if( tr_bencDictFindInt( args_in, "peer-limit", &tmp ) ) |
---|
363 | tr_torrentSetPeerLimit( tor, tmp ); |
---|
364 | if( tr_bencDictFindInt( args_in, "speed-limit-down", &tmp ) ) |
---|
365 | tr_torrentSetSpeedLimit( tor, TR_DOWN, tmp ); |
---|
366 | if( tr_bencDictFindInt( args_in, "speed-limit-down-enabled", &tmp ) ) |
---|
367 | tr_torrentSetSpeedMode( tor, TR_DOWN, tmp ? TR_SPEEDLIMIT_SINGLE |
---|
368 | : TR_SPEEDLIMIT_GLOBAL ); |
---|
369 | if( tr_bencDictFindInt( args_in, "speed-limit-up", &tmp ) ) |
---|
370 | tr_torrentSetSpeedLimit( tor, TR_UP, tmp ); |
---|
371 | if( tr_bencDictFindInt( args_in, "speed-limit-up-enabled", &tmp ) ) |
---|
372 | tr_torrentSetSpeedMode( tor, TR_UP, tmp ? TR_SPEEDLIMIT_SINGLE |
---|
373 | : TR_SPEEDLIMIT_GLOBAL ); |
---|
374 | notify( h, TR_RPC_TORRENT_CHANGED, tor ); |
---|
375 | } |
---|
376 | |
---|
377 | tr_free( torrents ); |
---|
378 | return NULL; |
---|
379 | } |
---|
380 | |
---|
381 | typedef int( *fileTestFunc )( const tr_torrent * tor, int i ); |
---|
382 | |
---|
383 | static int |
---|
384 | testFileHigh( const tr_torrent * tor, int i ) |
---|
385 | { |
---|
386 | return tor->info.files[i].priority == TR_PRI_HIGH; |
---|
387 | } |
---|
388 | static int |
---|
389 | testFileLow( const tr_torrent * tor, int i ) |
---|
390 | { |
---|
391 | return tor->info.files[i].priority == TR_PRI_LOW; |
---|
392 | } |
---|
393 | static int |
---|
394 | testFileNormal( const tr_torrent * tor, int i ) |
---|
395 | { |
---|
396 | return tor->info.files[i].priority == TR_PRI_NORMAL; |
---|
397 | } |
---|
398 | static int |
---|
399 | testFileDND( const tr_torrent * tor, int i ) |
---|
400 | { |
---|
401 | return tor->info.files[i].dnd != 0; |
---|
402 | } |
---|
403 | static int |
---|
404 | testFileDownload( const tr_torrent * tor, int i ) |
---|
405 | { |
---|
406 | return tor->info.files[i].dnd == 0; |
---|
407 | } |
---|
408 | |
---|
409 | static void |
---|
410 | buildFileList( const tr_torrent * tor, tr_benc * dict, |
---|
411 | const char * key, fileTestFunc func ) |
---|
412 | { |
---|
413 | int i; |
---|
414 | const int n = tor->info.fileCount; |
---|
415 | tr_benc * list; |
---|
416 | int * files = tr_new0( int, n ); |
---|
417 | int fileCount = 0; |
---|
418 | |
---|
419 | for( i=0; i<n; ++i ) |
---|
420 | if( func( tor, i ) ) |
---|
421 | files[fileCount++] = i; |
---|
422 | |
---|
423 | list = tr_bencDictAddList( dict, key, fileCount ); |
---|
424 | |
---|
425 | for( i=0; i<fileCount; ++i ) |
---|
426 | tr_bencListAddInt( list, files[i] ); |
---|
427 | |
---|
428 | tr_free( files ); |
---|
429 | } |
---|
430 | |
---|
431 | static const char* |
---|
432 | torrentGetPriorities( tr_handle * handle, |
---|
433 | tr_benc * args_in, tr_benc * args_out ) |
---|
434 | { |
---|
435 | int i, torrentCount; |
---|
436 | tr_torrent ** torrents = getTorrents( handle, args_in, &torrentCount ); |
---|
437 | tr_benc * list = tr_bencDictAddList( args_out, "torrents", torrentCount ); |
---|
438 | |
---|
439 | for( i=0; i<torrentCount; ++i ) |
---|
440 | { |
---|
441 | const tr_torrent * tor = torrents[i]; |
---|
442 | tr_benc * d = tr_bencListAddDict( list, 6 ); |
---|
443 | tr_bencDictAddInt( d, "id", tr_torrentId( tor ) ); |
---|
444 | buildFileList( tor, d, "files-unwanted", testFileDND ); |
---|
445 | buildFileList( tor, d, "files-wanted", testFileDownload ); |
---|
446 | buildFileList( tor, d, "priority-low", testFileLow ); |
---|
447 | buildFileList( tor, d, "priority-normal", testFileNormal ); |
---|
448 | buildFileList( tor, d, "priority-high", testFileHigh ); |
---|
449 | } |
---|
450 | |
---|
451 | tr_free( torrents ); |
---|
452 | return NULL; |
---|
453 | } |
---|
454 | |
---|
455 | static void |
---|
456 | setFilePriorities( tr_torrent * tor, int priority, tr_benc * list ) |
---|
457 | { |
---|
458 | int i; |
---|
459 | int64_t tmp; |
---|
460 | int fileCount = 0; |
---|
461 | const int n = tr_bencListSize( list ); |
---|
462 | tr_file_index_t * files = tr_new0( tr_file_index_t, n ); |
---|
463 | |
---|
464 | for( i=0; i<n; ++i ) |
---|
465 | if( tr_bencGetInt( tr_bencListChild( list, i ), &tmp ) ) |
---|
466 | files[fileCount++] = tmp; |
---|
467 | |
---|
468 | if( fileCount ) |
---|
469 | tr_torrentSetFilePriorities( tor, files, fileCount, priority ); |
---|
470 | |
---|
471 | tr_free( files ); |
---|
472 | } |
---|
473 | |
---|
474 | static void |
---|
475 | setFileDLs( tr_torrent * tor, int do_download, tr_benc * list ) |
---|
476 | { |
---|
477 | int i; |
---|
478 | int64_t tmp; |
---|
479 | int fileCount = 0; |
---|
480 | const int n = tr_bencListSize( list ); |
---|
481 | tr_file_index_t * files = tr_new0( tr_file_index_t, n ); |
---|
482 | |
---|
483 | for( i=0; i<n; ++i ) |
---|
484 | if( tr_bencGetInt( tr_bencListChild( list, i ), &tmp ) ) |
---|
485 | files[fileCount++] = tmp; |
---|
486 | |
---|
487 | if( fileCount ) |
---|
488 | tr_torrentSetFileDLs( tor, files, fileCount, do_download ); |
---|
489 | |
---|
490 | tr_free( files ); |
---|
491 | } |
---|
492 | |
---|
493 | static const char* |
---|
494 | torrentSetPriorities( tr_handle * h, |
---|
495 | tr_benc * args_in, tr_benc * args_out UNUSED ) |
---|
496 | { |
---|
497 | int i, torrentCount; |
---|
498 | tr_torrent ** torrents = getTorrents( h, args_in, &torrentCount ); |
---|
499 | |
---|
500 | for( i=0; i<torrentCount; ++i ) |
---|
501 | { |
---|
502 | tr_benc * files; |
---|
503 | tr_torrent * tor = torrents[i]; |
---|
504 | |
---|
505 | if( tr_bencDictFindList( args_in, "files-unwanted", &files ) ) |
---|
506 | setFileDLs( tor, FALSE, files ); |
---|
507 | if( tr_bencDictFindList( args_in, "files-wanted", &files ) ) |
---|
508 | setFileDLs( tor, TRUE, files ); |
---|
509 | if( tr_bencDictFindList( args_in, "priority-high", &files ) ) |
---|
510 | setFilePriorities( tor, TR_PRI_HIGH, files ); |
---|
511 | if( tr_bencDictFindList( args_in, "priority-low", &files ) ) |
---|
512 | setFilePriorities( tor, TR_PRI_LOW, files ); |
---|
513 | if( tr_bencDictFindList( args_in, "priority-normal", &files ) ) |
---|
514 | setFilePriorities( tor, TR_PRI_NORMAL, files ); |
---|
515 | |
---|
516 | notify( h, TR_RPC_TORRENT_CHANGED, tor ); |
---|
517 | } |
---|
518 | |
---|
519 | tr_free( torrents ); |
---|
520 | return NULL; |
---|
521 | } |
---|
522 | |
---|
523 | /*** |
---|
524 | **** |
---|
525 | ***/ |
---|
526 | |
---|
527 | static const char* |
---|
528 | torrentAdd( tr_handle * h, tr_benc * args_in, tr_benc * args_out ) |
---|
529 | { |
---|
530 | const char * filename; |
---|
531 | if( !tr_bencDictFindStr( args_in, "filename", &filename ) ) |
---|
532 | return "no filename specified"; |
---|
533 | else |
---|
534 | { |
---|
535 | int64_t i; |
---|
536 | int err = 0; |
---|
537 | const char * str; |
---|
538 | tr_ctor * ctor; |
---|
539 | tr_torrent * tor; |
---|
540 | |
---|
541 | ctor = tr_ctorNew( h ); |
---|
542 | tr_ctorSetMetainfoFromFile( ctor, filename ); |
---|
543 | if( tr_bencDictFindStr( args_in, "download-dir", &str ) ) |
---|
544 | tr_ctorSetDownloadDir( ctor, TR_FORCE, str ); |
---|
545 | if( tr_bencDictFindInt( args_in, "paused", &i ) ) |
---|
546 | tr_ctorSetPaused( ctor, TR_FORCE, i ); |
---|
547 | if( tr_bencDictFindInt( args_in, "peer-limit", &i ) ) |
---|
548 | tr_ctorSetPeerLimit( ctor, TR_FORCE, i ); |
---|
549 | tor = tr_torrentNew( h, ctor, &err ); |
---|
550 | tr_ctorFree( ctor ); |
---|
551 | |
---|
552 | if( tor ) { |
---|
553 | addInfo( tor, tr_bencDictAdd( args_out, "torrent-added" ) ); |
---|
554 | notify( h, TR_RPC_TORRENT_ADDED, tor ); |
---|
555 | } else if( err == TR_EDUPLICATE ) { |
---|
556 | return "duplicate torrent"; |
---|
557 | } else if( err == TR_EINVALID ) { |
---|
558 | return "invalid or corrupt torrent file"; |
---|
559 | } |
---|
560 | } |
---|
561 | |
---|
562 | return NULL; |
---|
563 | } |
---|
564 | |
---|
565 | /*** |
---|
566 | **** |
---|
567 | ***/ |
---|
568 | |
---|
569 | static const char* |
---|
570 | sessionSet( tr_handle * h, tr_benc * args_in, tr_benc * args_out UNUSED ) |
---|
571 | { |
---|
572 | int64_t i; |
---|
573 | const char * str; |
---|
574 | |
---|
575 | if( tr_bencDictFindStr( args_in, "download-dir", &str ) ) |
---|
576 | tr_sessionSetDownloadDir( h, str ); |
---|
577 | if( tr_bencDictFindInt( args_in, "peer-limit", &i ) ) |
---|
578 | tr_sessionSetPeerLimit( h, i ); |
---|
579 | if( tr_bencDictFindInt( args_in, "pex-allowed", &i ) ) |
---|
580 | tr_sessionSetPexEnabled( h, i ); |
---|
581 | if( tr_bencDictFindInt( args_in, "port", &i ) ) |
---|
582 | tr_sessionSetPublicPort( h, i ); |
---|
583 | if( tr_bencDictFindInt( args_in, "port-forwarding-enabled", &i ) ) |
---|
584 | tr_sessionSetPortForwardingEnabled( h, i ); |
---|
585 | if( tr_bencDictFindInt( args_in, "speed-limit-down", &i ) ) |
---|
586 | tr_sessionSetSpeedLimit( h, TR_DOWN, i ); |
---|
587 | if( tr_bencDictFindInt( args_in, "speed-limit-down-enabled", &i ) ) |
---|
588 | tr_sessionSetSpeedLimitEnabled( h, TR_DOWN, i ); |
---|
589 | if( tr_bencDictFindInt( args_in, "speed-limit-up", &i ) ) |
---|
590 | tr_sessionSetSpeedLimit( h, TR_UP, i ); |
---|
591 | if( tr_bencDictFindInt( args_in, "speed-limit-up-enabled", &i ) ) |
---|
592 | tr_sessionSetSpeedLimitEnabled( h, TR_UP, i ); |
---|
593 | if( tr_bencDictFindStr( args_in, "encryption", &str ) ) { |
---|
594 | if( !strcmp( str, "required" ) ) |
---|
595 | tr_sessionSetEncryption( h, TR_ENCRYPTION_REQUIRED ); |
---|
596 | else if( !strcmp( str, "tolerated" ) ) |
---|
597 | tr_sessionSetEncryption( h, TR_PLAINTEXT_PREFERRED ); |
---|
598 | else |
---|
599 | tr_sessionSetEncryption( h, TR_ENCRYPTION_PREFERRED ); |
---|
600 | } |
---|
601 | |
---|
602 | notify( h, TR_RPC_SESSION_CHANGED, NULL ); |
---|
603 | |
---|
604 | return NULL; |
---|
605 | } |
---|
606 | |
---|
607 | static const char* |
---|
608 | sessionGet( tr_handle * h, tr_benc * args_in UNUSED, tr_benc * args_out ) |
---|
609 | { |
---|
610 | const char * str; |
---|
611 | tr_benc * d = tr_bencDictAddDict( args_out, "session", 10 ); |
---|
612 | |
---|
613 | tr_bencDictAddStr( d, "download-dir", |
---|
614 | tr_sessionGetDownloadDir( h ) ); |
---|
615 | tr_bencDictAddInt( d, "peer-limit", |
---|
616 | tr_sessionGetPeerLimit( h ) ); |
---|
617 | tr_bencDictAddInt( d, "pex-allowed", |
---|
618 | tr_sessionIsPexEnabled( h ) ); |
---|
619 | tr_bencDictAddInt( d, "port", |
---|
620 | tr_sessionGetPublicPort( h ) ); |
---|
621 | tr_bencDictAddInt( d, "port-forwarding-enabled", |
---|
622 | tr_sessionIsPortForwardingEnabled( h ) ); |
---|
623 | tr_bencDictAddInt( d, "speed-limit-up", |
---|
624 | tr_sessionGetSpeedLimit( h, TR_UP ) ); |
---|
625 | tr_bencDictAddInt( d, "speed-limit-up-enabled", |
---|
626 | tr_sessionIsSpeedLimitEnabled( h, TR_UP ) ); |
---|
627 | tr_bencDictAddInt( d, "speed-limit-down", |
---|
628 | tr_sessionGetSpeedLimit( h, TR_DOWN ) ); |
---|
629 | tr_bencDictAddInt( d, "speed-limit-down-enabled", |
---|
630 | tr_sessionIsSpeedLimitEnabled( h, TR_DOWN ) ); |
---|
631 | switch( tr_sessionGetEncryption( h ) ) { |
---|
632 | case TR_PLAINTEXT_PREFERRED: str = "tolerated"; break; |
---|
633 | case TR_ENCRYPTION_REQUIRED: str = "required"; break; |
---|
634 | default: str = "preferred"; break; |
---|
635 | } |
---|
636 | tr_bencDictAddStr( d, "encryption", str ); |
---|
637 | |
---|
638 | |
---|
639 | |
---|
640 | return NULL; |
---|
641 | } |
---|
642 | |
---|
643 | /*** |
---|
644 | **** |
---|
645 | ***/ |
---|
646 | |
---|
647 | typedef const char* (handler)( tr_handle*, tr_benc*, tr_benc* ); |
---|
648 | |
---|
649 | struct method { |
---|
650 | const char * name; |
---|
651 | handler * func; |
---|
652 | } methods[] = { |
---|
653 | { "session-get", sessionGet }, |
---|
654 | { "session-set", sessionSet }, |
---|
655 | { "torrent-add", torrentAdd }, |
---|
656 | { "torrent-close", torrentClose }, |
---|
657 | { "torrent-get-priorities", torrentGetPriorities }, |
---|
658 | { "torrent-get", torrentGet }, |
---|
659 | { "torrent-info", torrentInfo }, |
---|
660 | { "torrent-list", torrentList }, |
---|
661 | { "torrent-set-priorities", torrentSetPriorities }, |
---|
662 | { "torrent-set", torrentSet }, |
---|
663 | { "torrent-start", torrentStart }, |
---|
664 | { "torrent-status", torrentStatus }, |
---|
665 | { "torrent-stop", torrentStop }, |
---|
666 | { "torrent-verify", torrentVerify } |
---|
667 | }; |
---|
668 | |
---|
669 | static char* |
---|
670 | request_exec( struct tr_handle * handle, |
---|
671 | tr_benc * request, |
---|
672 | int * response_len ) |
---|
673 | { |
---|
674 | int64_t i; |
---|
675 | const char * str; |
---|
676 | char * out; |
---|
677 | tr_benc response; |
---|
678 | tr_benc * args_in = tr_bencDictFind( request, "arguments" ); |
---|
679 | tr_benc * args_out = NULL; |
---|
680 | const char * result = NULL; |
---|
681 | |
---|
682 | /* build the response skeleton */ |
---|
683 | tr_bencInitDict( &response, 3 ); |
---|
684 | args_out = tr_bencDictAddDict( &response, "arguments", 0 ); |
---|
685 | |
---|
686 | /* parse the request */ |
---|
687 | if( !tr_bencDictFindStr( request, "method", &str ) ) |
---|
688 | result = "no method name"; |
---|
689 | else { |
---|
690 | const int n = TR_N_ELEMENTS( methods ); |
---|
691 | for( i=0; i<n; ++i ) |
---|
692 | if( !strcmp( str, methods[i].name ) ) |
---|
693 | break; |
---|
694 | result = i==n |
---|
695 | ? "method name not recognized" |
---|
696 | : (*methods[i].func)( handle, args_in, args_out ); |
---|
697 | } |
---|
698 | |
---|
699 | /* serialize & return the response */ |
---|
700 | if( !result ) |
---|
701 | result = "success"; |
---|
702 | tr_bencDictAddStr( &response, "result", result ); |
---|
703 | if( tr_bencDictFindInt( request, "tag", &i ) ) |
---|
704 | tr_bencDictAddInt( &response, "tag", i ); |
---|
705 | out = tr_bencSaveAsJSON( &response, response_len ); |
---|
706 | tr_bencFree( &response ); |
---|
707 | return out; |
---|
708 | } |
---|
709 | |
---|
710 | char* |
---|
711 | tr_rpc_request_exec_json( struct tr_handle * handle, |
---|
712 | const void * request_json, |
---|
713 | int request_len, |
---|
714 | int * response_len ) |
---|
715 | { |
---|
716 | tr_benc top; |
---|
717 | int have_content; |
---|
718 | char * ret; |
---|
719 | |
---|
720 | if( request_len < 0 ) |
---|
721 | request_len = strlen( request_json ); |
---|
722 | |
---|
723 | have_content = !tr_jsonParse( request_json, request_len, &top, NULL ); |
---|
724 | ret = request_exec( handle, have_content ? &top : NULL, response_len ); |
---|
725 | |
---|
726 | if( have_content ) |
---|
727 | tr_bencFree( &top ); |
---|
728 | return ret; |
---|
729 | } |
---|
730 | |
---|
731 | char* |
---|
732 | tr_rpc_request_exec_uri( struct tr_handle * handle, |
---|
733 | const void * request_uri, |
---|
734 | int request_len, |
---|
735 | int * response_len ) |
---|
736 | { |
---|
737 | char * ret = NULL; |
---|
738 | tr_benc top, * args; |
---|
739 | char * request = tr_strndup( request_uri, request_len ); |
---|
740 | const char * pch; |
---|
741 | |
---|
742 | tr_bencInitDict( &top, 3 ); |
---|
743 | args = tr_bencDictAddDict( &top, "arguments", 0 ); |
---|
744 | |
---|
745 | /* munge the URI into a usable form. |
---|
746 | * we have very loose typing on this to make the URIs as simple as possible: |
---|
747 | * - anything not a 'tag' or 'method' is automatically in 'arguments' |
---|
748 | * - values that are all-digits are numbers |
---|
749 | * - values that are all-digits or commas are number lists |
---|
750 | * - all other values are strings |
---|
751 | */ |
---|
752 | |
---|
753 | pch = strchr( request, '?' ); |
---|
754 | if( !pch ) pch = request; |
---|
755 | while( pch ) |
---|
756 | { |
---|
757 | const char * delim = strchr( pch, '=' ); |
---|
758 | const char * next = strchr( pch, '&' ); |
---|
759 | if( delim ) |
---|
760 | { |
---|
761 | int isNum = 1; |
---|
762 | int isNumList = 1; |
---|
763 | const char * walk; |
---|
764 | char * key = tr_strndup( pch, delim-pch ); |
---|
765 | int isArg = strcmp( key, "method" ) && strcmp( key, "tag" ); |
---|
766 | tr_benc * parent = isArg ? args : ⊤ |
---|
767 | char * val = next ? tr_strndup( delim+1, next-(delim+1) ) |
---|
768 | : tr_strdup( delim+1 ); |
---|
769 | for( walk=val; *walk && ( isNumList || isNum ); ++walk ) { |
---|
770 | if( isNumList ) isNumList = *walk=='-' || isdigit(*walk) || *walk==','; |
---|
771 | if( isNum ) isNum = *walk=='-' || isdigit(*walk); |
---|
772 | } |
---|
773 | if( isNum ) |
---|
774 | tr_bencDictAddInt( parent, key, strtol( val, NULL, 10 ) ); |
---|
775 | else if( !isNumList ) |
---|
776 | tr_bencDictAddStr( parent, key, val ); |
---|
777 | else { |
---|
778 | tr_benc * numList = tr_bencDictAddList( parent, key, 10 ); |
---|
779 | walk = val; |
---|
780 | for( ;; ) { |
---|
781 | char * end; |
---|
782 | tr_bencListAddInt( numList, strtol( walk, &end, 10 ) ); |
---|
783 | if( *end!=',' ) |
---|
784 | break; |
---|
785 | walk = end + 1; |
---|
786 | } |
---|
787 | } |
---|
788 | } |
---|
789 | pch = next ? next+1 : NULL; |
---|
790 | } |
---|
791 | |
---|
792 | ret = request_exec( handle, &top, response_len ); |
---|
793 | |
---|
794 | /* cleanup */ |
---|
795 | tr_bencFree( &top ); |
---|
796 | tr_free( request ); |
---|
797 | return ret; |
---|
798 | } |
---|