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: rpcimpl.c 7331 2008-12-09 17:01:49Z charles $ |
---|
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 "rpcimpl.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 tr_rpc_callback_status |
---|
33 | notify( tr_handle * session, |
---|
34 | int type, |
---|
35 | tr_torrent * tor ) |
---|
36 | { |
---|
37 | tr_rpc_callback_status status = 0; |
---|
38 | |
---|
39 | if( session->rpc_func ) |
---|
40 | status = session->rpc_func( session, type, tor, |
---|
41 | session->rpc_func_user_data ); |
---|
42 | |
---|
43 | return status; |
---|
44 | } |
---|
45 | |
---|
46 | /*** |
---|
47 | **** |
---|
48 | ***/ |
---|
49 | |
---|
50 | static tr_torrent ** |
---|
51 | getTorrents( tr_handle * handle, |
---|
52 | tr_benc * args, |
---|
53 | int * setmeCount ) |
---|
54 | { |
---|
55 | int torrentCount = 0; |
---|
56 | int64_t id; |
---|
57 | tr_torrent ** torrents = NULL; |
---|
58 | tr_benc * ids; |
---|
59 | |
---|
60 | if( tr_bencDictFindList( args, "ids", &ids ) ) |
---|
61 | { |
---|
62 | int i; |
---|
63 | const int n = tr_bencListSize( ids ); |
---|
64 | |
---|
65 | torrents = tr_new0( tr_torrent *, n ); |
---|
66 | |
---|
67 | for( i = 0; i < n; ++i ) |
---|
68 | { |
---|
69 | tr_torrent * tor = NULL; |
---|
70 | tr_benc * node = tr_bencListChild( ids, i ); |
---|
71 | int64_t id; |
---|
72 | const char * str; |
---|
73 | if( tr_bencGetInt( node, &id ) ) |
---|
74 | tor = tr_torrentFindFromId( handle, id ); |
---|
75 | else if( tr_bencGetStr( node, &str ) ) |
---|
76 | tor = tr_torrentFindFromHashString( handle, str ); |
---|
77 | if( tor ) |
---|
78 | torrents[torrentCount++] = tor; |
---|
79 | } |
---|
80 | } |
---|
81 | else if( tr_bencDictFindInt( args, "ids", &id ) |
---|
82 | || tr_bencDictFindInt( args, "id", &id ) ) |
---|
83 | { |
---|
84 | tr_torrent * tor; |
---|
85 | torrents = tr_new0( tr_torrent *, 1 ); |
---|
86 | if( ( tor = tr_torrentFindFromId( handle, id ) ) ) |
---|
87 | torrents[torrentCount++] = tor; |
---|
88 | } |
---|
89 | else /* all of them */ |
---|
90 | { |
---|
91 | tr_torrent * tor = NULL; |
---|
92 | const int n = tr_sessionCountTorrents( handle ); |
---|
93 | torrents = tr_new0( tr_torrent *, n ); |
---|
94 | while( ( tor = tr_torrentNext( handle, tor ) ) ) |
---|
95 | torrents[torrentCount++] = tor; |
---|
96 | } |
---|
97 | |
---|
98 | *setmeCount = torrentCount; |
---|
99 | return torrents; |
---|
100 | } |
---|
101 | |
---|
102 | static const char* |
---|
103 | torrentStart( tr_handle * h, |
---|
104 | tr_benc * args_in, |
---|
105 | tr_benc * args_out UNUSED ) |
---|
106 | { |
---|
107 | int i, torrentCount; |
---|
108 | tr_torrent ** torrents = getTorrents( h, args_in, &torrentCount ); |
---|
109 | |
---|
110 | for( i = 0; i < torrentCount; ++i ) |
---|
111 | { |
---|
112 | tr_torrent * tor = torrents[i]; |
---|
113 | tr_torrentStart( tor ); |
---|
114 | notify( h, TR_RPC_TORRENT_STARTED, tor ); |
---|
115 | } |
---|
116 | tr_free( torrents ); |
---|
117 | return NULL; |
---|
118 | } |
---|
119 | |
---|
120 | static const char* |
---|
121 | torrentStop( tr_handle * h, |
---|
122 | tr_benc * args_in, |
---|
123 | tr_benc * args_out UNUSED ) |
---|
124 | { |
---|
125 | int i, torrentCount; |
---|
126 | tr_torrent ** torrents = getTorrents( h, args_in, &torrentCount ); |
---|
127 | |
---|
128 | for( i = 0; i < torrentCount; ++i ) |
---|
129 | { |
---|
130 | tr_torrent * tor = torrents[i]; |
---|
131 | tr_torrentStop( tor ); |
---|
132 | notify( h, TR_RPC_TORRENT_STOPPED, tor ); |
---|
133 | } |
---|
134 | tr_free( torrents ); |
---|
135 | return NULL; |
---|
136 | } |
---|
137 | |
---|
138 | static const char* |
---|
139 | torrentRemove( tr_handle * h, |
---|
140 | tr_benc * args_in, |
---|
141 | tr_benc * args_out UNUSED ) |
---|
142 | { |
---|
143 | int i; |
---|
144 | int torrentCount; |
---|
145 | tr_torrent ** torrents = getTorrents( h, args_in, &torrentCount ); |
---|
146 | |
---|
147 | for( i=0; i<torrentCount; ++i ) |
---|
148 | { |
---|
149 | tr_torrent * tor = torrents[i]; |
---|
150 | const tr_rpc_callback_status status = notify( h, TR_RPC_TORRENT_REMOVING, tor ); |
---|
151 | int64_t deleteFlag; |
---|
152 | if( tr_bencDictFindInt( args_in, "delete-local-data", &deleteFlag ) && deleteFlag ) |
---|
153 | tr_torrentDeleteLocalData( tor ); |
---|
154 | if( !( status & TR_RPC_NOREMOVE ) ) |
---|
155 | tr_torrentRemove( tor ); |
---|
156 | } |
---|
157 | tr_free( torrents ); |
---|
158 | return NULL; |
---|
159 | } |
---|
160 | |
---|
161 | static const char* |
---|
162 | torrentVerify( tr_handle * h, |
---|
163 | tr_benc * args_in, |
---|
164 | tr_benc * args_out UNUSED ) |
---|
165 | { |
---|
166 | int i, torrentCount; |
---|
167 | tr_torrent ** torrents = getTorrents( h, args_in, &torrentCount ); |
---|
168 | |
---|
169 | for( i = 0; i < torrentCount; ++i ) |
---|
170 | { |
---|
171 | tr_torrent * tor = torrents[i]; |
---|
172 | tr_torrentVerify( tor ); |
---|
173 | notify( h, TR_RPC_TORRENT_CHANGED, tor ); |
---|
174 | } |
---|
175 | tr_free( torrents ); |
---|
176 | return NULL; |
---|
177 | } |
---|
178 | |
---|
179 | /*** |
---|
180 | **** |
---|
181 | ***/ |
---|
182 | |
---|
183 | static void |
---|
184 | addFiles( const tr_torrent * tor, |
---|
185 | tr_benc * list ) |
---|
186 | { |
---|
187 | tr_file_index_t i; |
---|
188 | tr_file_index_t n; |
---|
189 | const tr_info * info = tr_torrentInfo( tor ); |
---|
190 | tr_file_stat * files = tr_torrentFiles( tor, &n ); |
---|
191 | |
---|
192 | for( i = 0; i < info->fileCount; ++i ) |
---|
193 | { |
---|
194 | const tr_file * file = &info->files[i]; |
---|
195 | tr_benc * d = tr_bencListAddDict( list, 3 ); |
---|
196 | tr_bencDictAddInt( d, "bytesCompleted", files[i].bytesCompleted ); |
---|
197 | tr_bencDictAddInt( d, "length", file->length ); |
---|
198 | tr_bencDictAddStr( d, "name", file->name ); |
---|
199 | } |
---|
200 | |
---|
201 | tr_torrentFilesFree( files, n ); |
---|
202 | } |
---|
203 | |
---|
204 | static void |
---|
205 | addWebseeds( const tr_info * info, |
---|
206 | tr_benc * webseeds ) |
---|
207 | { |
---|
208 | int i; |
---|
209 | |
---|
210 | for( i = 0; i < info->webseedCount; ++i ) |
---|
211 | tr_bencListAddStr( webseeds, info->webseeds[i] ); |
---|
212 | } |
---|
213 | |
---|
214 | static void |
---|
215 | addTrackers( const tr_info * info, |
---|
216 | tr_benc * trackers ) |
---|
217 | { |
---|
218 | int i; |
---|
219 | |
---|
220 | for( i = 0; i < info->trackerCount; ++i ) |
---|
221 | { |
---|
222 | const tr_tracker_info * t = &info->trackers[i]; |
---|
223 | tr_benc * d = tr_bencListAddDict( trackers, 3 ); |
---|
224 | tr_bencDictAddStr( d, "announce", t->announce ); |
---|
225 | tr_bencDictAddStr( d, "scrape", t->scrape ); |
---|
226 | tr_bencDictAddInt( d, "tier", t->tier ); |
---|
227 | } |
---|
228 | } |
---|
229 | |
---|
230 | static void |
---|
231 | addPeers( const tr_torrent * tor, |
---|
232 | tr_benc * list ) |
---|
233 | { |
---|
234 | int i; |
---|
235 | int peerCount; |
---|
236 | tr_peer_stat * peers = tr_torrentPeers( tor, &peerCount ); |
---|
237 | |
---|
238 | tr_bencInitList( list, peerCount ); |
---|
239 | |
---|
240 | for( i = 0; i < peerCount; ++i ) |
---|
241 | { |
---|
242 | tr_benc * d = tr_bencListAddDict( list, 14 ); |
---|
243 | const tr_peer_stat * peer = peers + i; |
---|
244 | tr_bencDictAddStr( d, "address", peer->addr ); |
---|
245 | tr_bencDictAddStr( d, "clientName", peer->client ); |
---|
246 | tr_bencDictAddInt( d, "clientIsChoked", peer->clientIsChoked ); |
---|
247 | tr_bencDictAddInt( d, "clientIsInterested", |
---|
248 | peer->clientIsInterested ); |
---|
249 | tr_bencDictAddStr( d, "flagStr", peer->flagStr ); |
---|
250 | tr_bencDictAddInt( d, "isDownloadingFrom", peer->isDownloadingFrom ); |
---|
251 | tr_bencDictAddInt( d, "isEncrypted", peer->isEncrypted ); |
---|
252 | tr_bencDictAddInt( d, "isIncoming", peer->isIncoming ); |
---|
253 | tr_bencDictAddInt( d, "isUploadingTo", peer->isUploadingTo ); |
---|
254 | tr_bencDictAddInt( d, "peerIsChoked", peer->peerIsChoked ); |
---|
255 | tr_bencDictAddInt( d, "peerIsInterested", peer->peerIsInterested ); |
---|
256 | tr_bencDictAddInt( d, "port", peer->port ); |
---|
257 | tr_bencDictAddDouble( d, "progress", peer->progress ); |
---|
258 | tr_bencDictAddInt( d, "rateToClient", |
---|
259 | (int)( peer->rateToClient * 1024.0 ) ); |
---|
260 | tr_bencDictAddInt( d, "rateToPeer", |
---|
261 | (int)( peer->rateToPeer * 1024.0 ) ); |
---|
262 | } |
---|
263 | |
---|
264 | tr_torrentPeersFree( peers, peerCount ); |
---|
265 | } |
---|
266 | |
---|
267 | static void |
---|
268 | addField( const tr_torrent * tor, |
---|
269 | tr_benc * d, |
---|
270 | const char * key ) |
---|
271 | { |
---|
272 | const tr_info * inf = tr_torrentInfo( tor ); |
---|
273 | const tr_stat * st = tr_torrentStat( (tr_torrent*)tor ); |
---|
274 | |
---|
275 | if( !strcmp( key, "activityDate" ) ) |
---|
276 | tr_bencDictAddInt( d, key, st->activityDate ); |
---|
277 | else if( !strcmp( key, "addedDate" ) ) |
---|
278 | tr_bencDictAddInt( d, key, st->addedDate ); |
---|
279 | else if( !strcmp( key, "announceResponse" ) ) |
---|
280 | tr_bencDictAddStr( d, key, st->announceResponse ); |
---|
281 | else if( !strcmp( key, "announceURL" ) ) |
---|
282 | tr_bencDictAddStr( d, key, st->announceURL ); |
---|
283 | else if( !strcmp( key, "comment" ) ) |
---|
284 | tr_bencDictAddStr( d, key, inf->comment ? inf->comment : "" ); |
---|
285 | else if( !strcmp( key, "corruptEver" ) ) |
---|
286 | tr_bencDictAddInt( d, key, st->corruptEver ); |
---|
287 | else if( !strcmp( key, "creator" ) ) |
---|
288 | tr_bencDictAddStr( d, key, inf->creator ? inf->creator : "" ); |
---|
289 | else if( !strcmp( key, "dateCreated" ) ) |
---|
290 | tr_bencDictAddInt( d, key, inf->dateCreated ); |
---|
291 | else if( !strcmp( key, "desiredAvailable" ) ) |
---|
292 | tr_bencDictAddInt( d, key, st->desiredAvailable ); |
---|
293 | else if( !strcmp( key, "doneDate" ) ) |
---|
294 | tr_bencDictAddInt( d, key, st->doneDate ); |
---|
295 | else if( !strcmp( key, "downloadedEver" ) ) |
---|
296 | tr_bencDictAddInt( d, key, st->downloadedEver ); |
---|
297 | else if( !strcmp( key, "downloaders" ) ) |
---|
298 | tr_bencDictAddInt( d, key, st->downloaders ); |
---|
299 | else if( !strcmp( key, "downloadLimitMode" ) ) |
---|
300 | tr_bencDictAddInt( d, key, tr_torrentGetSpeedMode( tor, TR_DOWN ) ); |
---|
301 | else if( !strcmp( key, "downloadLimit" ) ) |
---|
302 | tr_bencDictAddInt( d, key, tr_torrentGetSpeedLimit( tor, TR_DOWN ) ); |
---|
303 | else if( !strcmp( key, "error" ) ) |
---|
304 | tr_bencDictAddInt( d, key, st->error ); |
---|
305 | else if( !strcmp( key, "errorString" ) ) |
---|
306 | tr_bencDictAddStr( d, key, st->errorString ); |
---|
307 | else if( !strcmp( key, "eta" ) ) |
---|
308 | tr_bencDictAddInt( d, key, st->eta ); |
---|
309 | else if( !strcmp( key, "files" ) ) |
---|
310 | addFiles( tor, tr_bencDictAddList( d, key, inf->fileCount ) ); |
---|
311 | else if( !strcmp( key, "hashString" ) ) |
---|
312 | tr_bencDictAddStr( d, key, tor->info.hashString ); |
---|
313 | else if( !strcmp( key, "haveUnchecked" ) ) |
---|
314 | tr_bencDictAddInt( d, key, st->haveUnchecked ); |
---|
315 | else if( !strcmp( key, "haveValid" ) ) |
---|
316 | tr_bencDictAddInt( d, key, st->haveValid ); |
---|
317 | else if( !strcmp( key, "id" ) ) |
---|
318 | tr_bencDictAddInt( d, key, st->id ); |
---|
319 | else if( !strcmp( key, "isPrivate" ) ) |
---|
320 | tr_bencDictAddInt( d, key, tr_torrentIsPrivate( tor ) ); |
---|
321 | else if( !strcmp( key, "lastAnnounceTime" ) ) |
---|
322 | tr_bencDictAddInt( d, key, st->lastAnnounceTime ); |
---|
323 | else if( !strcmp( key, "lastScrapeTime" ) ) |
---|
324 | tr_bencDictAddInt( d, key, st->lastScrapeTime ); |
---|
325 | else if( !strcmp( key, "leechers" ) ) |
---|
326 | tr_bencDictAddInt( d, key, st->leechers ); |
---|
327 | else if( !strcmp( key, "leftUntilDone" ) ) |
---|
328 | tr_bencDictAddInt( d, key, st->leftUntilDone ); |
---|
329 | else if( !strcmp( key, "manualAnnounceTime" ) ) |
---|
330 | tr_bencDictAddInt( d, key, st->manualAnnounceTime ); |
---|
331 | else if( !strcmp( key, "maxConnectedPeers" ) ) |
---|
332 | tr_bencDictAddInt( d, key, tr_torrentGetPeerLimit( tor ) ); |
---|
333 | else if( !strcmp( key, "name" ) ) |
---|
334 | tr_bencDictAddStr( d, key, inf->name ); |
---|
335 | else if( !strcmp( key, "nextAnnounceTime" ) ) |
---|
336 | tr_bencDictAddInt( d, key, st->nextAnnounceTime ); |
---|
337 | else if( !strcmp( key, "nextScrapeTime" ) ) |
---|
338 | tr_bencDictAddInt( d, key, st->nextScrapeTime ); |
---|
339 | else if( !strcmp( key, "peers" ) ) |
---|
340 | addPeers( tor, tr_bencDictAdd( d, key ) ); |
---|
341 | else if( !strcmp( key, "peersConnected" ) ) |
---|
342 | tr_bencDictAddInt( d, key, st->peersConnected ); |
---|
343 | else if( !strcmp( key, "peersFrom" ) ) |
---|
344 | { |
---|
345 | tr_benc * tmp = tr_bencDictAddDict( d, key, 4 ); |
---|
346 | const int * f = st->peersFrom; |
---|
347 | tr_bencDictAddInt( tmp, "fromCache", f[TR_PEER_FROM_CACHE] ); |
---|
348 | tr_bencDictAddInt( tmp, "fromIncoming", f[TR_PEER_FROM_INCOMING] ); |
---|
349 | tr_bencDictAddInt( tmp, "fromPex", f[TR_PEER_FROM_PEX] ); |
---|
350 | tr_bencDictAddInt( tmp, "fromTracker", f[TR_PEER_FROM_TRACKER] ); |
---|
351 | } |
---|
352 | else if( !strcmp( key, "peersGettingFromUs" ) ) |
---|
353 | tr_bencDictAddInt( d, key, st->peersGettingFromUs ); |
---|
354 | else if( !strcmp( key, "peersKnown" ) ) |
---|
355 | tr_bencDictAddInt( d, key, st->peersKnown ); |
---|
356 | else if( !strcmp( key, "peersSendingToUs" ) ) |
---|
357 | tr_bencDictAddInt( d, key, st->peersSendingToUs ); |
---|
358 | else if( !strcmp( key, "pieceCount" ) ) |
---|
359 | tr_bencDictAddInt( d, key, inf->pieceCount ); |
---|
360 | else if( !strcmp( key, "pieceSize" ) ) |
---|
361 | tr_bencDictAddInt( d, key, inf->pieceSize ); |
---|
362 | else if( !strcmp( key, "priorities" ) ) |
---|
363 | { |
---|
364 | tr_file_index_t i; |
---|
365 | tr_benc * p = tr_bencDictAddList( d, key, inf->fileCount ); |
---|
366 | for( i = 0; i < inf->fileCount; ++i ) |
---|
367 | tr_bencListAddInt( p, inf->files[i].priority ); |
---|
368 | } |
---|
369 | else if( !strcmp( key, "rateDownload" ) ) |
---|
370 | tr_bencDictAddInt( d, key, (int)( st->pieceDownloadSpeed * 1024 ) ); |
---|
371 | else if( !strcmp( key, "rateUpload" ) ) |
---|
372 | tr_bencDictAddInt( d, key, (int)( st->pieceUploadSpeed * 1024 ) ); |
---|
373 | else if( !strcmp( key, "recheckProgress" ) ) |
---|
374 | tr_bencDictAddDouble( d, key, st->recheckProgress ); |
---|
375 | else if( !strcmp( key, "scrapeResponse" ) ) |
---|
376 | tr_bencDictAddStr( d, key, st->scrapeResponse ); |
---|
377 | else if( !strcmp( key, "scrapeURL" ) ) |
---|
378 | tr_bencDictAddStr( d, key, st->scrapeURL ); |
---|
379 | else if( !strcmp( key, "seeders" ) ) |
---|
380 | tr_bencDictAddInt( d, key, st->seeders ); |
---|
381 | else if( !strcmp( key, "sizeWhenDone" ) ) |
---|
382 | tr_bencDictAddInt( d, key, st->sizeWhenDone ); |
---|
383 | else if( !strcmp( key, "startDate" ) ) |
---|
384 | tr_bencDictAddInt( d, key, st->startDate ); |
---|
385 | else if( !strcmp( key, "status" ) ) |
---|
386 | tr_bencDictAddInt( d, key, st->activity ); |
---|
387 | else if( !strcmp( key, "swarmSpeed" ) ) |
---|
388 | tr_bencDictAddInt( d, key, (int)( st->swarmSpeed * 1024 ) ); |
---|
389 | else if( !strcmp( key, "timesCompleted" ) ) |
---|
390 | tr_bencDictAddInt( d, key, st->timesCompleted ); |
---|
391 | else if( !strcmp( key, "trackers" ) ) |
---|
392 | addTrackers( inf, tr_bencDictAddList( d, key, inf->trackerCount ) ); |
---|
393 | else if( !strcmp( key, "totalSize" ) ) |
---|
394 | tr_bencDictAddInt( d, key, inf->totalSize ); |
---|
395 | else if( !strcmp( key, "uploadedEver" ) ) |
---|
396 | tr_bencDictAddInt( d, key, st->uploadedEver ); |
---|
397 | else if( !strcmp( key, "uploadLimitMode" ) ) |
---|
398 | tr_bencDictAddInt( d, key, tr_torrentGetSpeedMode( tor, TR_UP ) ); |
---|
399 | else if( !strcmp( key, "uploadLimit" ) ) |
---|
400 | tr_bencDictAddInt( d, key, tr_torrentGetSpeedLimit( tor, TR_UP ) ); |
---|
401 | else if( !strcmp( key, "uploadRatio" ) ) |
---|
402 | tr_bencDictAddDouble( d, key, |
---|
403 | tr_getRatio( st->uploadedEver, |
---|
404 | st->downloadedEver ) ); |
---|
405 | else if( !strcmp( key, "wanted" ) ) |
---|
406 | { |
---|
407 | tr_file_index_t i; |
---|
408 | tr_benc * w = tr_bencDictAddList( d, key, inf->fileCount ); |
---|
409 | for( i = 0; i < inf->fileCount; ++i ) |
---|
410 | tr_bencListAddInt( w, inf->files[i].dnd ? 0 : 1 ); |
---|
411 | } |
---|
412 | else if( !strcmp( key, "webseeds" ) ) |
---|
413 | addWebseeds( inf, tr_bencDictAddList( d, key, inf->trackerCount ) ); |
---|
414 | else if( !strcmp( key, "webseedsSendingToUs" ) ) |
---|
415 | tr_bencDictAddInt( d, key, st->webseedsSendingToUs ); |
---|
416 | } |
---|
417 | |
---|
418 | static void |
---|
419 | addInfo( const tr_torrent * tor, |
---|
420 | tr_benc * d, |
---|
421 | tr_benc * fields ) |
---|
422 | { |
---|
423 | int i; |
---|
424 | const int n = tr_bencListSize( fields ); |
---|
425 | const char * str; |
---|
426 | |
---|
427 | tr_bencInitDict( d, n ); |
---|
428 | |
---|
429 | for( i = 0; i < n; ++i ) |
---|
430 | if( tr_bencGetStr( tr_bencListChild( fields, i ), &str ) ) |
---|
431 | addField( tor, d, str ); |
---|
432 | } |
---|
433 | |
---|
434 | static const char* |
---|
435 | torrentGet( tr_handle * handle, |
---|
436 | tr_benc * args_in, |
---|
437 | tr_benc * args_out ) |
---|
438 | { |
---|
439 | int i, torrentCount; |
---|
440 | tr_torrent ** torrents = getTorrents( handle, args_in, &torrentCount ); |
---|
441 | tr_benc * list = tr_bencDictAddList( args_out, "torrents", |
---|
442 | torrentCount ); |
---|
443 | tr_benc * fields; |
---|
444 | char * msg = NULL; |
---|
445 | |
---|
446 | if( !tr_bencDictFindList( args_in, "fields", &fields ) ) |
---|
447 | msg = "no fields specified"; |
---|
448 | else for( i = 0; i < torrentCount; ++i ) |
---|
449 | addInfo( torrents[i], tr_bencListAdd( list ), fields ); |
---|
450 | |
---|
451 | tr_free( torrents ); |
---|
452 | return msg; |
---|
453 | } |
---|
454 | |
---|
455 | /*** |
---|
456 | **** |
---|
457 | ***/ |
---|
458 | |
---|
459 | static void |
---|
460 | setFilePriorities( tr_torrent * tor, |
---|
461 | int priority, |
---|
462 | tr_benc * list ) |
---|
463 | { |
---|
464 | int i; |
---|
465 | int64_t tmp; |
---|
466 | int fileCount = 0; |
---|
467 | const int n = tr_bencListSize( list ); |
---|
468 | tr_file_index_t * files = tr_new0( tr_file_index_t, tor->info.fileCount ); |
---|
469 | |
---|
470 | if( n ) |
---|
471 | { |
---|
472 | for( i = 0; i < n; ++i ) |
---|
473 | if( tr_bencGetInt( tr_bencListChild( list, i ), &tmp ) ) |
---|
474 | if( 0 <= tmp && tmp < tor->info.fileCount ) |
---|
475 | files[fileCount++] = tmp; |
---|
476 | } |
---|
477 | else /* if empty set, apply to all */ |
---|
478 | { |
---|
479 | tr_file_index_t t; |
---|
480 | for( t = 0; t < tor->info.fileCount; ++t ) |
---|
481 | files[fileCount++] = t; |
---|
482 | } |
---|
483 | |
---|
484 | if( fileCount ) |
---|
485 | tr_torrentSetFilePriorities( tor, files, fileCount, priority ); |
---|
486 | |
---|
487 | tr_free( files ); |
---|
488 | } |
---|
489 | |
---|
490 | static void |
---|
491 | setFileDLs( tr_torrent * tor, |
---|
492 | int do_download, |
---|
493 | tr_benc * list ) |
---|
494 | { |
---|
495 | int i; |
---|
496 | int64_t tmp; |
---|
497 | int fileCount = 0; |
---|
498 | const int n = tr_bencListSize( list ); |
---|
499 | tr_file_index_t * files = tr_new0( tr_file_index_t, tor->info.fileCount ); |
---|
500 | |
---|
501 | if( n ) /* if argument list, process them */ |
---|
502 | { |
---|
503 | for( i = 0; i < n; ++i ) |
---|
504 | if( tr_bencGetInt( tr_bencListChild( list, i ), &tmp ) ) |
---|
505 | if( 0 <= tmp && tmp < tor->info.fileCount ) |
---|
506 | files[fileCount++] = tmp; |
---|
507 | } |
---|
508 | else /* if empty set, apply to all */ |
---|
509 | { |
---|
510 | tr_file_index_t t; |
---|
511 | for( t = 0; t < tor->info.fileCount; ++t ) |
---|
512 | files[fileCount++] = t; |
---|
513 | } |
---|
514 | |
---|
515 | if( fileCount ) |
---|
516 | tr_torrentSetFileDLs( tor, files, fileCount, do_download ); |
---|
517 | |
---|
518 | tr_free( files ); |
---|
519 | } |
---|
520 | |
---|
521 | static const char* |
---|
522 | torrentSet( tr_handle * h, |
---|
523 | tr_benc * args_in, |
---|
524 | tr_benc * args_out UNUSED ) |
---|
525 | { |
---|
526 | int i, torrentCount; |
---|
527 | tr_torrent ** torrents = getTorrents( h, args_in, &torrentCount ); |
---|
528 | |
---|
529 | for( i = 0; i < torrentCount; ++i ) |
---|
530 | { |
---|
531 | int64_t tmp; |
---|
532 | tr_benc * files; |
---|
533 | tr_torrent * tor = torrents[i]; |
---|
534 | |
---|
535 | if( tr_bencDictFindList( args_in, "files-unwanted", &files ) ) |
---|
536 | setFileDLs( tor, FALSE, files ); |
---|
537 | if( tr_bencDictFindList( args_in, "files-wanted", &files ) ) |
---|
538 | setFileDLs( tor, TRUE, files ); |
---|
539 | if( tr_bencDictFindInt( args_in, "peer-limit", &tmp ) ) |
---|
540 | tr_torrentSetPeerLimit( tor, tmp ); |
---|
541 | if( tr_bencDictFindList( args_in, "priority-high", &files ) ) |
---|
542 | setFilePriorities( tor, TR_PRI_HIGH, files ); |
---|
543 | if( tr_bencDictFindList( args_in, "priority-low", &files ) ) |
---|
544 | setFilePriorities( tor, TR_PRI_LOW, files ); |
---|
545 | if( tr_bencDictFindList( args_in, "priority-normal", &files ) ) |
---|
546 | setFilePriorities( tor, TR_PRI_NORMAL, files ); |
---|
547 | if( tr_bencDictFindInt( args_in, "speed-limit-down", &tmp ) ) |
---|
548 | tr_torrentSetSpeedLimit( tor, TR_DOWN, tmp ); |
---|
549 | if( tr_bencDictFindInt( args_in, "speed-limit-down-enabled", &tmp ) ) |
---|
550 | tr_torrentSetSpeedMode( |
---|
551 | tor, TR_DOWN, tmp ? TR_SPEEDLIMIT_SINGLE |
---|
552 | : TR_SPEEDLIMIT_GLOBAL ); |
---|
553 | if( tr_bencDictFindInt( args_in, "speed-limit-up", &tmp ) ) |
---|
554 | tr_torrentSetSpeedLimit( tor, TR_UP, tmp ); |
---|
555 | if( tr_bencDictFindInt( args_in, "speed-limit-up-enabled", &tmp ) ) |
---|
556 | tr_torrentSetSpeedMode( tor, TR_UP, tmp ? TR_SPEEDLIMIT_SINGLE |
---|
557 | : TR_SPEEDLIMIT_GLOBAL ); |
---|
558 | |
---|
559 | notify( h, TR_RPC_TORRENT_CHANGED, tor ); |
---|
560 | } |
---|
561 | |
---|
562 | tr_free( torrents ); |
---|
563 | return NULL; |
---|
564 | } |
---|
565 | |
---|
566 | /*** |
---|
567 | **** |
---|
568 | ***/ |
---|
569 | |
---|
570 | static const char* |
---|
571 | torrentAdd( tr_handle * h, |
---|
572 | tr_benc * args_in, |
---|
573 | tr_benc * args_out ) |
---|
574 | { |
---|
575 | const char * filename = NULL; |
---|
576 | const char * metainfo_base64 = NULL; |
---|
577 | |
---|
578 | tr_bencDictFindStr( args_in, "filename", &filename ); |
---|
579 | tr_bencDictFindStr( args_in, "metainfo", &metainfo_base64 ); |
---|
580 | if( !filename && !metainfo_base64 ) |
---|
581 | return "no filename or metainfo specified"; |
---|
582 | else |
---|
583 | { |
---|
584 | int64_t i; |
---|
585 | int err = 0; |
---|
586 | const char * str; |
---|
587 | tr_ctor * ctor; |
---|
588 | tr_torrent * tor; |
---|
589 | |
---|
590 | ctor = tr_ctorNew( h ); |
---|
591 | |
---|
592 | /* set the metainfo */ |
---|
593 | if( filename ) |
---|
594 | tr_ctorSetMetainfoFromFile( ctor, filename ); |
---|
595 | else |
---|
596 | { |
---|
597 | int len; |
---|
598 | char * metainfo = tr_base64_decode( metainfo_base64, -1, &len ); |
---|
599 | tr_ctorSetMetainfo( ctor, (uint8_t*)metainfo, len ); |
---|
600 | tr_free( metainfo ); |
---|
601 | } |
---|
602 | |
---|
603 | /* set the optional arguments */ |
---|
604 | if( tr_bencDictFindStr( args_in, "download-dir", &str ) ) |
---|
605 | tr_ctorSetDownloadDir( ctor, TR_FORCE, str ); |
---|
606 | if( tr_bencDictFindInt( args_in, "paused", &i ) ) |
---|
607 | tr_ctorSetPaused( ctor, TR_FORCE, i ); |
---|
608 | if( tr_bencDictFindInt( args_in, "peer-limit", &i ) ) |
---|
609 | tr_ctorSetPeerLimit( ctor, TR_FORCE, i ); |
---|
610 | |
---|
611 | tor = tr_torrentNew( h, ctor, &err ); |
---|
612 | tr_ctorFree( ctor ); |
---|
613 | |
---|
614 | if( tor ) |
---|
615 | { |
---|
616 | tr_benc fields; |
---|
617 | tr_bencInitList( &fields, 3 ); |
---|
618 | tr_bencListAddStr( &fields, "id" ); |
---|
619 | tr_bencListAddStr( &fields, "name" ); |
---|
620 | tr_bencListAddStr( &fields, "hashString" ); |
---|
621 | addInfo( tor, tr_bencDictAdd( args_out, |
---|
622 | "torrent-added" ), &fields ); |
---|
623 | notify( h, TR_RPC_TORRENT_ADDED, tor ); |
---|
624 | tr_bencFree( &fields ); |
---|
625 | } |
---|
626 | else if( err == TR_EDUPLICATE ) |
---|
627 | { |
---|
628 | return "duplicate torrent"; |
---|
629 | } |
---|
630 | else if( err == TR_EINVALID ) |
---|
631 | { |
---|
632 | return "invalid or corrupt torrent file"; |
---|
633 | } |
---|
634 | } |
---|
635 | |
---|
636 | return NULL; |
---|
637 | } |
---|
638 | |
---|
639 | /*** |
---|
640 | **** |
---|
641 | ***/ |
---|
642 | |
---|
643 | static const char* |
---|
644 | sessionSet( tr_handle * h, |
---|
645 | tr_benc * args_in, |
---|
646 | tr_benc * args_out UNUSED ) |
---|
647 | { |
---|
648 | int64_t i; |
---|
649 | const char * str; |
---|
650 | |
---|
651 | if( tr_bencDictFindStr( args_in, "download-dir", &str ) ) |
---|
652 | tr_sessionSetDownloadDir( h, str ); |
---|
653 | if( tr_bencDictFindInt( args_in, "peer-limit", &i ) ) |
---|
654 | tr_sessionSetPeerLimit( h, i ); |
---|
655 | if( tr_bencDictFindInt( args_in, "pex-allowed", &i ) ) |
---|
656 | tr_sessionSetPexEnabled( h, i ); |
---|
657 | if( tr_bencDictFindInt( args_in, "port", &i ) ) |
---|
658 | tr_sessionSetPeerPort( h, i ); |
---|
659 | if( tr_bencDictFindInt( args_in, "port-forwarding-enabled", &i ) ) |
---|
660 | tr_sessionSetPortForwardingEnabled( h, i ); |
---|
661 | if( tr_bencDictFindInt( args_in, "speed-limit-down", &i ) ) |
---|
662 | tr_sessionSetSpeedLimit( h, TR_DOWN, i ); |
---|
663 | if( tr_bencDictFindInt( args_in, "speed-limit-down-enabled", &i ) ) |
---|
664 | tr_sessionSetSpeedLimitEnabled( h, TR_DOWN, i ); |
---|
665 | if( tr_bencDictFindInt( args_in, "speed-limit-up", &i ) ) |
---|
666 | tr_sessionSetSpeedLimit( h, TR_UP, i ); |
---|
667 | if( tr_bencDictFindInt( args_in, "speed-limit-up-enabled", &i ) ) |
---|
668 | tr_sessionSetSpeedLimitEnabled( h, TR_UP, i ); |
---|
669 | if( tr_bencDictFindStr( args_in, "encryption", &str ) ) |
---|
670 | { |
---|
671 | if( !strcmp( str, "required" ) ) |
---|
672 | tr_sessionSetEncryption( h, TR_ENCRYPTION_REQUIRED ); |
---|
673 | else if( !strcmp( str, "tolerated" ) ) |
---|
674 | tr_sessionSetEncryption( h, TR_CLEAR_PREFERRED ); |
---|
675 | else |
---|
676 | tr_sessionSetEncryption( h, TR_ENCRYPTION_PREFERRED ); |
---|
677 | } |
---|
678 | |
---|
679 | notify( h, TR_RPC_SESSION_CHANGED, NULL ); |
---|
680 | |
---|
681 | return NULL; |
---|
682 | } |
---|
683 | |
---|
684 | static const char* |
---|
685 | sessionStats( tr_handle * h, |
---|
686 | tr_benc * args_in UNUSED, |
---|
687 | tr_benc * args_out ) |
---|
688 | { |
---|
689 | tr_benc * d = tr_bencDictAddDict( args_out, "session-stats", 10 ); |
---|
690 | tr_torrent * tor = NULL; |
---|
691 | int running = 0; |
---|
692 | int total = 0; |
---|
693 | |
---|
694 | while( ( tor = tr_torrentNext( h, tor ) ) ) |
---|
695 | { |
---|
696 | ++total; |
---|
697 | if( tor->isRunning ) |
---|
698 | ++running; |
---|
699 | } |
---|
700 | |
---|
701 | tr_bencDictAddInt( d, "activeTorrentCount", running ); |
---|
702 | tr_bencDictAddInt( d, "downloadSpeed", (int)( tr_sessionGetPieceSpeed( h, TR_DOWN ) * 1024 ) ); |
---|
703 | tr_bencDictAddInt( d, "pausedTorrentCount", total - running ); |
---|
704 | tr_bencDictAddInt( d, "torrentCount", total ); |
---|
705 | tr_bencDictAddInt( d, "uploadSpeed", (int)( tr_sessionGetPieceSpeed( h, TR_UP ) * 1024 ) ); |
---|
706 | return NULL; |
---|
707 | } |
---|
708 | |
---|
709 | static const char* |
---|
710 | sessionGet( tr_handle * h, |
---|
711 | tr_benc * args_in UNUSED, |
---|
712 | tr_benc * args_out ) |
---|
713 | { |
---|
714 | const char * str; |
---|
715 | tr_benc * d = args_out; |
---|
716 | |
---|
717 | tr_bencDictAddStr( d, "download-dir", |
---|
718 | tr_sessionGetDownloadDir( h ) ); |
---|
719 | tr_bencDictAddInt( d, "peer-limit", |
---|
720 | tr_sessionGetPeerLimit( h ) ); |
---|
721 | tr_bencDictAddInt( d, "pex-allowed", |
---|
722 | tr_sessionIsPexEnabled( h ) ); |
---|
723 | tr_bencDictAddInt( d, "port", |
---|
724 | tr_sessionGetPeerPort( h ) ); |
---|
725 | tr_bencDictAddInt( d, "port-forwarding-enabled", |
---|
726 | tr_sessionIsPortForwardingEnabled( h ) ); |
---|
727 | tr_bencDictAddInt( d, "speed-limit-up", |
---|
728 | tr_sessionGetSpeedLimit( h, TR_UP ) ); |
---|
729 | tr_bencDictAddInt( d, "speed-limit-up-enabled", |
---|
730 | tr_sessionIsSpeedLimitEnabled( h, TR_UP ) ); |
---|
731 | tr_bencDictAddInt( d, "speed-limit-down", |
---|
732 | tr_sessionGetSpeedLimit( h, TR_DOWN ) ); |
---|
733 | tr_bencDictAddInt( d, "speed-limit-down-enabled", |
---|
734 | tr_sessionIsSpeedLimitEnabled( h, TR_DOWN ) ); |
---|
735 | switch( tr_sessionGetEncryption( h ) ) |
---|
736 | { |
---|
737 | case TR_CLEAR_PREFERRED: |
---|
738 | str = "tolerated"; break; |
---|
739 | |
---|
740 | case TR_ENCRYPTION_REQUIRED: |
---|
741 | str = "required"; break; |
---|
742 | |
---|
743 | default: |
---|
744 | str = "preferred"; break; |
---|
745 | } |
---|
746 | tr_bencDictAddStr( d, "encryption", str ); |
---|
747 | |
---|
748 | return NULL; |
---|
749 | } |
---|
750 | |
---|
751 | /*** |
---|
752 | **** |
---|
753 | ***/ |
---|
754 | |
---|
755 | typedef const char* ( handler )( tr_handle*, tr_benc*, tr_benc* ); |
---|
756 | |
---|
757 | static struct method |
---|
758 | { |
---|
759 | const char * name; |
---|
760 | handler * func; |
---|
761 | } methods[] = { |
---|
762 | { "session-get", sessionGet }, |
---|
763 | { "session-set", sessionSet }, |
---|
764 | { "session-stats", sessionStats }, |
---|
765 | { "torrent-add", torrentAdd }, |
---|
766 | { "torrent-get", torrentGet }, |
---|
767 | { "torrent-remove", torrentRemove }, |
---|
768 | { "torrent-set", torrentSet }, |
---|
769 | { "torrent-start", torrentStart }, |
---|
770 | { "torrent-stop", torrentStop }, |
---|
771 | { "torrent-verify", torrentVerify } |
---|
772 | }; |
---|
773 | |
---|
774 | static char* |
---|
775 | request_exec( struct tr_handle * handle, |
---|
776 | tr_benc * request, |
---|
777 | int * response_len ) |
---|
778 | { |
---|
779 | int64_t i; |
---|
780 | const char * str; |
---|
781 | char * out; |
---|
782 | tr_benc response; |
---|
783 | tr_benc * args_in = tr_bencDictFind( request, "arguments" ); |
---|
784 | tr_benc * args_out = NULL; |
---|
785 | const char * result = NULL; |
---|
786 | |
---|
787 | /* build the response skeleton */ |
---|
788 | tr_bencInitDict( &response, 3 ); |
---|
789 | args_out = tr_bencDictAddDict( &response, "arguments", 0 ); |
---|
790 | |
---|
791 | /* parse the request */ |
---|
792 | if( !tr_bencDictFindStr( request, "method", &str ) ) |
---|
793 | result = "no method name"; |
---|
794 | else |
---|
795 | { |
---|
796 | const int n = TR_N_ELEMENTS( methods ); |
---|
797 | for( i = 0; i < n; ++i ) |
---|
798 | if( !strcmp( str, methods[i].name ) ) |
---|
799 | break; |
---|
800 | result = i == n |
---|
801 | ? "method name not recognized" |
---|
802 | : ( *methods[i].func )( handle, args_in, args_out ); |
---|
803 | } |
---|
804 | |
---|
805 | /* serialize & return the response */ |
---|
806 | if( !result ) |
---|
807 | result = "success"; |
---|
808 | tr_bencDictAddStr( &response, "result", result ); |
---|
809 | if( tr_bencDictFindInt( request, "tag", &i ) ) |
---|
810 | tr_bencDictAddInt( &response, "tag", i ); |
---|
811 | out = tr_bencSaveAsJSON( &response, response_len ); |
---|
812 | tr_bencFree( &response ); |
---|
813 | return out; |
---|
814 | } |
---|
815 | |
---|
816 | char* |
---|
817 | tr_rpc_request_exec_json( struct tr_handle * handle, |
---|
818 | const void * request_json, |
---|
819 | int request_len, |
---|
820 | int * response_len ) |
---|
821 | { |
---|
822 | tr_benc top; |
---|
823 | int have_content; |
---|
824 | char * ret; |
---|
825 | |
---|
826 | if( request_len < 0 ) |
---|
827 | request_len = strlen( request_json ); |
---|
828 | |
---|
829 | have_content = !tr_jsonParse( request_json, request_len, &top, NULL ); |
---|
830 | ret = request_exec( handle, have_content ? &top : NULL, response_len ); |
---|
831 | |
---|
832 | if( have_content ) |
---|
833 | tr_bencFree( &top ); |
---|
834 | return ret; |
---|
835 | } |
---|
836 | |
---|
837 | static void |
---|
838 | addToken( tr_benc * list, |
---|
839 | const char * token, |
---|
840 | size_t len ) |
---|
841 | { |
---|
842 | char * p; |
---|
843 | const char * end = token + len; |
---|
844 | const long a = strtol( token, &p, 10 ); |
---|
845 | |
---|
846 | if( p == end ) |
---|
847 | tr_bencListAddInt( list, a ); |
---|
848 | else if( *p == '-' && isdigit( p[1] ) ) |
---|
849 | { |
---|
850 | const long b = strtol( p + 1, &p, 10 ); |
---|
851 | if( ( p == end ) && ( b > a ) ) |
---|
852 | { |
---|
853 | long i; |
---|
854 | for( i = a; i <= b; ++i ) |
---|
855 | tr_bencListAddInt( list, i ); |
---|
856 | } |
---|
857 | } |
---|
858 | } |
---|
859 | |
---|
860 | /** |
---|
861 | * Munge the URI into a usable form. |
---|
862 | * |
---|
863 | * We have very loose typing on this to make the URIs as simple as possible: |
---|
864 | * - anything not a 'tag' or 'method' is automatically in 'arguments' |
---|
865 | * - values that are all-digits are numbers |
---|
866 | * - values that are all-digits or commas are number lists |
---|
867 | * - all other values are strings |
---|
868 | */ |
---|
869 | void |
---|
870 | tr_rpc_parse_list_str( tr_benc * setme, |
---|
871 | const char * str_in, |
---|
872 | size_t len ) |
---|
873 | |
---|
874 | { |
---|
875 | char * str = tr_strndup( str_in, len ); |
---|
876 | int isNum; |
---|
877 | int isNumList; |
---|
878 | int commaCount; |
---|
879 | const char * walk; |
---|
880 | |
---|
881 | isNum = 1; |
---|
882 | isNumList = 1; |
---|
883 | commaCount = 0; |
---|
884 | walk = str; |
---|
885 | for( ; *walk && ( isNumList || isNum ); ++walk ) |
---|
886 | { |
---|
887 | if( isNumList ) isNumList = *walk == '-' || isdigit( *walk ) |
---|
888 | || *walk == ','; |
---|
889 | if( isNum ) isNum = *walk == '-' || isdigit( *walk ); |
---|
890 | if( *walk == ',' ) ++commaCount; |
---|
891 | } |
---|
892 | |
---|
893 | if( isNum ) |
---|
894 | tr_bencInitInt( setme, strtol( str, NULL, 10 ) ); |
---|
895 | else if( !isNumList ) |
---|
896 | tr_bencInitStr( setme, str, len ); |
---|
897 | else |
---|
898 | { |
---|
899 | tr_bencInitList( setme, commaCount + 1 ); |
---|
900 | walk = str; |
---|
901 | while( *walk ) |
---|
902 | { |
---|
903 | const char * p = strchr( walk, ',' ); |
---|
904 | if( !p ) |
---|
905 | p = walk + strlen( walk ); |
---|
906 | addToken( setme, walk, p - walk ); |
---|
907 | if( *p != ',' ) |
---|
908 | break; |
---|
909 | walk = p + 1; |
---|
910 | } |
---|
911 | } |
---|
912 | |
---|
913 | tr_free( str ); |
---|
914 | } |
---|
915 | |
---|
916 | char* |
---|
917 | tr_rpc_request_exec_uri( struct tr_handle * handle, |
---|
918 | const void * request_uri, |
---|
919 | int request_len, |
---|
920 | int * response_len ) |
---|
921 | { |
---|
922 | char * ret = NULL; |
---|
923 | tr_benc top, * args; |
---|
924 | char * request = tr_strndup( request_uri, request_len ); |
---|
925 | const char * pch; |
---|
926 | |
---|
927 | tr_bencInitDict( &top, 3 ); |
---|
928 | args = tr_bencDictAddDict( &top, "arguments", 0 ); |
---|
929 | |
---|
930 | pch = strchr( request, '?' ); |
---|
931 | if( !pch ) pch = request; |
---|
932 | while( pch ) |
---|
933 | { |
---|
934 | const char * delim = strchr( pch, '=' ); |
---|
935 | const char * next = strchr( pch, '&' ); |
---|
936 | if( delim ) |
---|
937 | { |
---|
938 | char * key = tr_strndup( pch, delim - pch ); |
---|
939 | int isArg = strcmp( key, "method" ) && strcmp( key, "tag" ); |
---|
940 | tr_benc * parent = isArg ? args : ⊤ |
---|
941 | tr_rpc_parse_list_str( tr_bencDictAdd( parent, key ), |
---|
942 | delim + 1, |
---|
943 | next ? (size_t)( |
---|
944 | next - |
---|
945 | ( delim + 1 ) ) : strlen( delim + 1 ) ); |
---|
946 | tr_free( key ); |
---|
947 | } |
---|
948 | pch = next ? next + 1 : NULL; |
---|
949 | } |
---|
950 | |
---|
951 | ret = request_exec( handle, &top, response_len ); |
---|
952 | |
---|
953 | /* cleanup */ |
---|
954 | tr_bencFree( &top ); |
---|
955 | tr_free( request ); |
---|
956 | return ret; |
---|
957 | } |
---|
958 | |
---|