1 | /* |
---|
2 | * This file Copyright (C) 2008-2014 Mnemosyne LLC |
---|
3 | * |
---|
4 | * It may be used under the GNU GPL versions 2 or 3 |
---|
5 | * or any future license endorsed by Mnemosyne LLC. |
---|
6 | * |
---|
7 | * $Id: session.c 14524 2015-05-07 06:45:35Z mikedld $ |
---|
8 | */ |
---|
9 | |
---|
10 | #include <assert.h> |
---|
11 | #include <errno.h> /* ENOENT */ |
---|
12 | #include <limits.h> /* INT_MAX */ |
---|
13 | #include <stdlib.h> |
---|
14 | #include <string.h> /* memcpy */ |
---|
15 | |
---|
16 | #include <signal.h> |
---|
17 | |
---|
18 | #ifndef _WIN32 |
---|
19 | #include <sys/types.h> /* umask () */ |
---|
20 | #include <sys/stat.h> /* umask () */ |
---|
21 | #endif |
---|
22 | |
---|
23 | #include <event2/dns.h> /* evdns_base_free () */ |
---|
24 | #include <event2/event.h> |
---|
25 | |
---|
26 | #include <libutp/utp.h> |
---|
27 | |
---|
28 | //#define TR_SHOW_DEPRECATED |
---|
29 | #include "transmission.h" |
---|
30 | #include "announcer.h" |
---|
31 | #include "bandwidth.h" |
---|
32 | #include "blocklist.h" |
---|
33 | #include "cache.h" |
---|
34 | #include "crypto-utils.h" |
---|
35 | #include "error.h" |
---|
36 | #include "error-types.h" |
---|
37 | #include "fdlimit.h" |
---|
38 | #include "file.h" |
---|
39 | #include "list.h" |
---|
40 | #include "log.h" |
---|
41 | #include "net.h" |
---|
42 | #include "peer-io.h" |
---|
43 | #include "peer-mgr.h" |
---|
44 | #include "platform.h" /* tr_lock, tr_getTorrentDir () */ |
---|
45 | #include "platform-quota.h" /* tr_device_info_free() */ |
---|
46 | #include "port-forwarding.h" |
---|
47 | #include "rpc-server.h" |
---|
48 | #include "session.h" |
---|
49 | #include "stats.h" |
---|
50 | #include "torrent.h" |
---|
51 | #include "tr-dht.h" /* tr_dhtUpkeep () */ |
---|
52 | #include "tr-udp.h" |
---|
53 | #include "tr-utp.h" |
---|
54 | #include "tr-lpd.h" |
---|
55 | #include "trevent.h" |
---|
56 | #include "utils.h" |
---|
57 | #include "variant.h" |
---|
58 | #include "verify.h" |
---|
59 | #include "version.h" |
---|
60 | #include "web.h" |
---|
61 | |
---|
62 | enum |
---|
63 | { |
---|
64 | #ifdef TR_LIGHTWEIGHT |
---|
65 | DEFAULT_CACHE_SIZE_MB = 2, |
---|
66 | DEFAULT_PREFETCH_ENABLED = false, |
---|
67 | #else |
---|
68 | DEFAULT_CACHE_SIZE_MB = 4, |
---|
69 | DEFAULT_PREFETCH_ENABLED = true, |
---|
70 | #endif |
---|
71 | SAVE_INTERVAL_SECS = 360 |
---|
72 | }; |
---|
73 | |
---|
74 | |
---|
75 | #define dbgmsg(...) \ |
---|
76 | do \ |
---|
77 | { \ |
---|
78 | if (tr_logGetDeepEnabled ()) \ |
---|
79 | tr_logAddDeep (__FILE__, __LINE__, NULL, __VA_ARGS__); \ |
---|
80 | } \ |
---|
81 | while (0) |
---|
82 | |
---|
83 | static tr_port |
---|
84 | getRandomPort (tr_session * s) |
---|
85 | { |
---|
86 | return tr_rand_int_weak (s->randomPortHigh - s->randomPortLow + 1) + s->randomPortLow; |
---|
87 | } |
---|
88 | |
---|
89 | /* Generate a peer id : "-TRxyzb-" + 12 random alphanumeric |
---|
90 | characters, where x is the major version number, y is the |
---|
91 | minor version number, z is the maintenance number, and b |
---|
92 | designates beta (Azureus-style) */ |
---|
93 | void |
---|
94 | tr_peerIdInit (uint8_t * buf) |
---|
95 | { |
---|
96 | int i; |
---|
97 | int val; |
---|
98 | int total = 0; |
---|
99 | const char * pool = "0123456789abcdefghijklmnopqrstuvwxyz"; |
---|
100 | const int base = 36; |
---|
101 | |
---|
102 | memcpy (buf, PEERID_PREFIX, 8); |
---|
103 | |
---|
104 | tr_rand_buffer (buf+8, 11); |
---|
105 | for (i=8; i<19; ++i) |
---|
106 | { |
---|
107 | val = buf[i] % base; |
---|
108 | total += val; |
---|
109 | buf[i] = pool[val]; |
---|
110 | } |
---|
111 | |
---|
112 | val = total % base ? base - (total % base) : 0; |
---|
113 | buf[19] = pool[val]; |
---|
114 | buf[20] = '\0'; |
---|
115 | } |
---|
116 | |
---|
117 | /*** |
---|
118 | **** |
---|
119 | ***/ |
---|
120 | |
---|
121 | tr_encryption_mode |
---|
122 | tr_sessionGetEncryption (tr_session * session) |
---|
123 | { |
---|
124 | assert (session); |
---|
125 | |
---|
126 | return session->encryptionMode; |
---|
127 | } |
---|
128 | |
---|
129 | void |
---|
130 | tr_sessionSetEncryption (tr_session * session, |
---|
131 | tr_encryption_mode mode) |
---|
132 | { |
---|
133 | assert (session); |
---|
134 | assert (mode == TR_ENCRYPTION_PREFERRED |
---|
135 | || mode == TR_ENCRYPTION_REQUIRED |
---|
136 | || mode == TR_CLEAR_PREFERRED); |
---|
137 | |
---|
138 | session->encryptionMode = mode; |
---|
139 | } |
---|
140 | |
---|
141 | /*** |
---|
142 | **** |
---|
143 | ***/ |
---|
144 | |
---|
145 | struct tr_bindinfo |
---|
146 | { |
---|
147 | tr_socket_t socket; |
---|
148 | tr_address addr; |
---|
149 | struct event * ev; |
---|
150 | }; |
---|
151 | |
---|
152 | |
---|
153 | static void |
---|
154 | close_bindinfo (struct tr_bindinfo * b) |
---|
155 | { |
---|
156 | if ((b != NULL) && (b->socket != TR_BAD_SOCKET)) |
---|
157 | { |
---|
158 | event_free (b->ev); |
---|
159 | b->ev = NULL; |
---|
160 | tr_netCloseSocket (b->socket); |
---|
161 | } |
---|
162 | } |
---|
163 | |
---|
164 | static void |
---|
165 | close_incoming_peer_port (tr_session * session) |
---|
166 | { |
---|
167 | close_bindinfo (session->public_ipv4); |
---|
168 | close_bindinfo (session->public_ipv6); |
---|
169 | } |
---|
170 | |
---|
171 | static void |
---|
172 | free_incoming_peer_port (tr_session * session) |
---|
173 | { |
---|
174 | close_bindinfo (session->public_ipv4); |
---|
175 | tr_free (session->public_ipv4); |
---|
176 | session->public_ipv4 = NULL; |
---|
177 | |
---|
178 | close_bindinfo (session->public_ipv6); |
---|
179 | tr_free (session->public_ipv6); |
---|
180 | session->public_ipv6 = NULL; |
---|
181 | } |
---|
182 | |
---|
183 | static void |
---|
184 | accept_incoming_peer (evutil_socket_t fd, short what UNUSED, void * vsession) |
---|
185 | { |
---|
186 | tr_socket_t clientSocket; |
---|
187 | tr_port clientPort; |
---|
188 | tr_address clientAddr; |
---|
189 | tr_session * session = vsession; |
---|
190 | |
---|
191 | clientSocket = tr_netAccept (session, fd, &clientAddr, &clientPort); |
---|
192 | if (clientSocket != TR_BAD_SOCKET) |
---|
193 | { |
---|
194 | tr_logAddDeep (__FILE__, __LINE__, NULL, "new incoming connection %"TR_PRI_SOCK" (%s)", |
---|
195 | clientSocket, tr_peerIoAddrStr (&clientAddr, clientPort)); |
---|
196 | tr_peerMgrAddIncoming (session->peerMgr, &clientAddr, clientPort, |
---|
197 | clientSocket, NULL); |
---|
198 | } |
---|
199 | } |
---|
200 | |
---|
201 | static void |
---|
202 | open_incoming_peer_port (tr_session * session) |
---|
203 | { |
---|
204 | struct tr_bindinfo * b; |
---|
205 | |
---|
206 | /* bind an ipv4 port to listen for incoming peers... */ |
---|
207 | b = session->public_ipv4; |
---|
208 | b->socket = tr_netBindTCP (&b->addr, session->private_peer_port, false); |
---|
209 | if (b->socket != TR_BAD_SOCKET) |
---|
210 | { |
---|
211 | b->ev = event_new (session->event_base, b->socket, EV_READ | EV_PERSIST, accept_incoming_peer, session); |
---|
212 | event_add (b->ev, NULL); |
---|
213 | } |
---|
214 | |
---|
215 | /* and do the exact same thing for ipv6, if it's supported... */ |
---|
216 | if (tr_net_hasIPv6 (session->private_peer_port)) |
---|
217 | { |
---|
218 | b = session->public_ipv6; |
---|
219 | b->socket = tr_netBindTCP (&b->addr, session->private_peer_port, false); |
---|
220 | if (b->socket != TR_BAD_SOCKET) |
---|
221 | { |
---|
222 | b->ev = event_new (session->event_base, b->socket, EV_READ | EV_PERSIST, accept_incoming_peer, session); |
---|
223 | event_add (b->ev, NULL); |
---|
224 | } |
---|
225 | } |
---|
226 | } |
---|
227 | |
---|
228 | const tr_address* |
---|
229 | tr_sessionGetPublicAddress (const tr_session * session, int tr_af_type, bool * is_default_value) |
---|
230 | { |
---|
231 | const char * default_value; |
---|
232 | const struct tr_bindinfo * bindinfo; |
---|
233 | |
---|
234 | switch (tr_af_type) |
---|
235 | { |
---|
236 | case TR_AF_INET: |
---|
237 | bindinfo = session->public_ipv4; |
---|
238 | default_value = TR_DEFAULT_BIND_ADDRESS_IPV4; |
---|
239 | break; |
---|
240 | |
---|
241 | case TR_AF_INET6: |
---|
242 | bindinfo = session->public_ipv6; |
---|
243 | default_value = TR_DEFAULT_BIND_ADDRESS_IPV6; |
---|
244 | break; |
---|
245 | |
---|
246 | default: |
---|
247 | bindinfo = NULL; |
---|
248 | default_value = ""; |
---|
249 | break; |
---|
250 | } |
---|
251 | |
---|
252 | if (is_default_value && bindinfo) |
---|
253 | *is_default_value = !tr_strcmp0 (default_value, tr_address_to_string (&bindinfo->addr)); |
---|
254 | |
---|
255 | return bindinfo ? &bindinfo->addr : NULL; |
---|
256 | } |
---|
257 | |
---|
258 | /*** |
---|
259 | **** |
---|
260 | ***/ |
---|
261 | |
---|
262 | #ifdef TR_LIGHTWEIGHT |
---|
263 | #define TR_DEFAULT_ENCRYPTION TR_CLEAR_PREFERRED |
---|
264 | #else |
---|
265 | #define TR_DEFAULT_ENCRYPTION TR_ENCRYPTION_PREFERRED |
---|
266 | #endif |
---|
267 | |
---|
268 | static int |
---|
269 | parse_tos (const char *str) |
---|
270 | { |
---|
271 | char *p; |
---|
272 | int value; |
---|
273 | |
---|
274 | if (!evutil_ascii_strcasecmp (str, "")) |
---|
275 | return 0; |
---|
276 | if (!evutil_ascii_strcasecmp (str, "default")) |
---|
277 | return 0; |
---|
278 | |
---|
279 | if (!evutil_ascii_strcasecmp (str, "lowcost")) |
---|
280 | return 0x10; |
---|
281 | if (!evutil_ascii_strcasecmp (str, "mincost")) |
---|
282 | return 0x10; |
---|
283 | |
---|
284 | if (!evutil_ascii_strcasecmp (str, "throughput")) |
---|
285 | return 0x08; |
---|
286 | if (!evutil_ascii_strcasecmp (str, "reliability")) |
---|
287 | return 0x04; |
---|
288 | if (!evutil_ascii_strcasecmp (str, "lowdelay")) |
---|
289 | return 0x02; |
---|
290 | |
---|
291 | value = strtol (str, &p, 0); |
---|
292 | if (!p || (p == str)) |
---|
293 | return 0; |
---|
294 | |
---|
295 | return value; |
---|
296 | } |
---|
297 | |
---|
298 | static const char * |
---|
299 | format_tos (int value) |
---|
300 | { |
---|
301 | static char buf[8]; |
---|
302 | switch (value) |
---|
303 | { |
---|
304 | case 0: return "default"; |
---|
305 | case 0x10: return "lowcost"; |
---|
306 | case 0x08: return "throughput"; |
---|
307 | case 0x04: return "reliability"; |
---|
308 | case 0x02: return "lowdelay"; |
---|
309 | default: |
---|
310 | tr_snprintf (buf, 8, "%d", value); |
---|
311 | return buf; |
---|
312 | } |
---|
313 | } |
---|
314 | |
---|
315 | void |
---|
316 | tr_sessionGetDefaultSettings (tr_variant * d) |
---|
317 | { |
---|
318 | assert (tr_variantIsDict (d)); |
---|
319 | |
---|
320 | tr_variantDictReserve (d, 63); |
---|
321 | tr_variantDictAddBool (d, TR_KEY_blocklist_enabled, false); |
---|
322 | tr_variantDictAddStr (d, TR_KEY_blocklist_url, "http://www.example.com/blocklist"); |
---|
323 | tr_variantDictAddInt (d, TR_KEY_cache_size_mb, DEFAULT_CACHE_SIZE_MB); |
---|
324 | tr_variantDictAddBool (d, TR_KEY_dht_enabled, true); |
---|
325 | tr_variantDictAddBool (d, TR_KEY_utp_enabled, true); |
---|
326 | tr_variantDictAddBool (d, TR_KEY_lpd_enabled, false); |
---|
327 | tr_variantDictAddStr (d, TR_KEY_download_dir, tr_getDefaultDownloadDir ()); |
---|
328 | tr_variantDictAddInt (d, TR_KEY_speed_limit_down, 100); |
---|
329 | tr_variantDictAddBool (d, TR_KEY_speed_limit_down_enabled, false); |
---|
330 | tr_variantDictAddInt (d, TR_KEY_encryption, TR_DEFAULT_ENCRYPTION); |
---|
331 | tr_variantDictAddInt (d, TR_KEY_idle_seeding_limit, 30); |
---|
332 | tr_variantDictAddBool (d, TR_KEY_idle_seeding_limit_enabled, false); |
---|
333 | tr_variantDictAddStr (d, TR_KEY_incomplete_dir, tr_getDefaultDownloadDir ()); |
---|
334 | tr_variantDictAddBool (d, TR_KEY_incomplete_dir_enabled, false); |
---|
335 | tr_variantDictAddInt (d, TR_KEY_message_level, TR_LOG_INFO); |
---|
336 | tr_variantDictAddInt (d, TR_KEY_download_queue_size, 5); |
---|
337 | tr_variantDictAddBool (d, TR_KEY_download_queue_enabled, true); |
---|
338 | tr_variantDictAddInt (d, TR_KEY_peer_limit_global, atoi (TR_DEFAULT_PEER_LIMIT_GLOBAL_STR)); |
---|
339 | tr_variantDictAddInt (d, TR_KEY_peer_limit_per_torrent, atoi (TR_DEFAULT_PEER_LIMIT_TORRENT_STR)); |
---|
340 | tr_variantDictAddInt (d, TR_KEY_peer_port, atoi (TR_DEFAULT_PEER_PORT_STR)); |
---|
341 | tr_variantDictAddBool (d, TR_KEY_peer_port_random_on_start, false); |
---|
342 | tr_variantDictAddInt (d, TR_KEY_peer_port_random_low, 49152); |
---|
343 | tr_variantDictAddInt (d, TR_KEY_peer_port_random_high, 65535); |
---|
344 | tr_variantDictAddStr (d, TR_KEY_peer_socket_tos, TR_DEFAULT_PEER_SOCKET_TOS_STR); |
---|
345 | tr_variantDictAddBool (d, TR_KEY_pex_enabled, true); |
---|
346 | tr_variantDictAddBool (d, TR_KEY_port_forwarding_enabled, true); |
---|
347 | tr_variantDictAddInt (d, TR_KEY_preallocation, TR_PREALLOCATE_SPARSE); |
---|
348 | tr_variantDictAddBool (d, TR_KEY_prefetch_enabled, DEFAULT_PREFETCH_ENABLED); |
---|
349 | tr_variantDictAddInt (d, TR_KEY_peer_id_ttl_hours, 6); |
---|
350 | tr_variantDictAddBool (d, TR_KEY_queue_stalled_enabled, true); |
---|
351 | tr_variantDictAddInt (d, TR_KEY_queue_stalled_minutes, 30); |
---|
352 | tr_variantDictAddReal (d, TR_KEY_ratio_limit, 2.0); |
---|
353 | tr_variantDictAddBool (d, TR_KEY_ratio_limit_enabled, false); |
---|
354 | tr_variantDictAddBool (d, TR_KEY_rename_partial_files, true); |
---|
355 | tr_variantDictAddBool (d, TR_KEY_rpc_authentication_required, false); |
---|
356 | tr_variantDictAddStr (d, TR_KEY_rpc_bind_address, "0.0.0.0"); |
---|
357 | tr_variantDictAddBool (d, TR_KEY_rpc_enabled, false); |
---|
358 | tr_variantDictAddStr (d, TR_KEY_rpc_password, ""); |
---|
359 | tr_variantDictAddStr (d, TR_KEY_rpc_username, ""); |
---|
360 | tr_variantDictAddStr (d, TR_KEY_rpc_whitelist, TR_DEFAULT_RPC_WHITELIST); |
---|
361 | tr_variantDictAddBool (d, TR_KEY_rpc_whitelist_enabled, true); |
---|
362 | tr_variantDictAddInt (d, TR_KEY_rpc_port, atoi (TR_DEFAULT_RPC_PORT_STR)); |
---|
363 | tr_variantDictAddStr (d, TR_KEY_rpc_url, TR_DEFAULT_RPC_URL_STR); |
---|
364 | tr_variantDictAddBool (d, TR_KEY_scrape_paused_torrents_enabled, true); |
---|
365 | tr_variantDictAddStr (d, TR_KEY_script_torrent_done_filename, ""); |
---|
366 | tr_variantDictAddBool (d, TR_KEY_script_torrent_done_enabled, false); |
---|
367 | tr_variantDictAddInt (d, TR_KEY_seed_queue_size, 10); |
---|
368 | tr_variantDictAddBool (d, TR_KEY_seed_queue_enabled, false); |
---|
369 | tr_variantDictAddBool (d, TR_KEY_alt_speed_enabled, false); |
---|
370 | tr_variantDictAddInt (d, TR_KEY_alt_speed_up, 50); /* half the regular */ |
---|
371 | tr_variantDictAddInt (d, TR_KEY_alt_speed_down, 50); /* half the regular */ |
---|
372 | tr_variantDictAddInt (d, TR_KEY_alt_speed_time_begin, 540); /* 9am */ |
---|
373 | tr_variantDictAddBool (d, TR_KEY_alt_speed_time_enabled, false); |
---|
374 | tr_variantDictAddInt (d, TR_KEY_alt_speed_time_end, 1020); /* 5pm */ |
---|
375 | tr_variantDictAddInt (d, TR_KEY_alt_speed_time_day, TR_SCHED_ALL); |
---|
376 | tr_variantDictAddInt (d, TR_KEY_speed_limit_up, 100); |
---|
377 | tr_variantDictAddBool (d, TR_KEY_speed_limit_up_enabled, false); |
---|
378 | tr_variantDictAddInt (d, TR_KEY_umask, 022); |
---|
379 | tr_variantDictAddInt (d, TR_KEY_upload_slots_per_torrent, 14); |
---|
380 | tr_variantDictAddStr (d, TR_KEY_bind_address_ipv4, TR_DEFAULT_BIND_ADDRESS_IPV4); |
---|
381 | tr_variantDictAddStr (d, TR_KEY_bind_address_ipv6, TR_DEFAULT_BIND_ADDRESS_IPV6); |
---|
382 | tr_variantDictAddBool (d, TR_KEY_start_added_torrents, true); |
---|
383 | tr_variantDictAddBool (d, TR_KEY_trash_original_torrent_files, false); |
---|
384 | } |
---|
385 | |
---|
386 | void |
---|
387 | tr_sessionGetSettings (tr_session * s, tr_variant * d) |
---|
388 | { |
---|
389 | assert (tr_variantIsDict (d)); |
---|
390 | |
---|
391 | tr_variantDictReserve (d, 63); |
---|
392 | tr_variantDictAddBool (d, TR_KEY_blocklist_enabled, tr_blocklistIsEnabled (s)); |
---|
393 | tr_variantDictAddStr (d, TR_KEY_blocklist_url, tr_blocklistGetURL (s)); |
---|
394 | tr_variantDictAddInt (d, TR_KEY_cache_size_mb, tr_sessionGetCacheLimit_MB (s)); |
---|
395 | tr_variantDictAddBool (d, TR_KEY_dht_enabled, s->isDHTEnabled); |
---|
396 | tr_variantDictAddBool (d, TR_KEY_utp_enabled, s->isUTPEnabled); |
---|
397 | tr_variantDictAddBool (d, TR_KEY_lpd_enabled, s->isLPDEnabled); |
---|
398 | tr_variantDictAddStr (d, TR_KEY_download_dir, tr_sessionGetDownloadDir (s)); |
---|
399 | tr_variantDictAddInt (d, TR_KEY_download_queue_size, tr_sessionGetQueueSize (s, TR_DOWN)); |
---|
400 | tr_variantDictAddBool (d, TR_KEY_download_queue_enabled, tr_sessionGetQueueEnabled (s, TR_DOWN)); |
---|
401 | tr_variantDictAddInt (d, TR_KEY_speed_limit_down, tr_sessionGetSpeedLimit_KBps (s, TR_DOWN)); |
---|
402 | tr_variantDictAddBool (d, TR_KEY_speed_limit_down_enabled, tr_sessionIsSpeedLimited (s, TR_DOWN)); |
---|
403 | tr_variantDictAddInt (d, TR_KEY_encryption, s->encryptionMode); |
---|
404 | tr_variantDictAddInt (d, TR_KEY_idle_seeding_limit, tr_sessionGetIdleLimit (s)); |
---|
405 | tr_variantDictAddBool (d, TR_KEY_idle_seeding_limit_enabled, tr_sessionIsIdleLimited (s)); |
---|
406 | tr_variantDictAddStr (d, TR_KEY_incomplete_dir, tr_sessionGetIncompleteDir (s)); |
---|
407 | tr_variantDictAddBool (d, TR_KEY_incomplete_dir_enabled, tr_sessionIsIncompleteDirEnabled (s)); |
---|
408 | tr_variantDictAddInt (d, TR_KEY_message_level, tr_logGetLevel ()); |
---|
409 | tr_variantDictAddInt (d, TR_KEY_peer_limit_global, s->peerLimit); |
---|
410 | tr_variantDictAddInt (d, TR_KEY_peer_limit_per_torrent, s->peerLimitPerTorrent); |
---|
411 | tr_variantDictAddInt (d, TR_KEY_peer_port, tr_sessionGetPeerPort (s)); |
---|
412 | tr_variantDictAddBool (d, TR_KEY_peer_port_random_on_start, s->isPortRandom); |
---|
413 | tr_variantDictAddInt (d, TR_KEY_peer_port_random_low, s->randomPortLow); |
---|
414 | tr_variantDictAddInt (d, TR_KEY_peer_port_random_high, s->randomPortHigh); |
---|
415 | tr_variantDictAddStr (d, TR_KEY_peer_socket_tos, format_tos (s->peerSocketTOS)); |
---|
416 | tr_variantDictAddStr (d, TR_KEY_peer_congestion_algorithm, s->peer_congestion_algorithm); |
---|
417 | tr_variantDictAddBool (d, TR_KEY_pex_enabled, s->isPexEnabled); |
---|
418 | tr_variantDictAddBool (d, TR_KEY_port_forwarding_enabled, tr_sessionIsPortForwardingEnabled (s)); |
---|
419 | tr_variantDictAddInt (d, TR_KEY_preallocation, s->preallocationMode); |
---|
420 | tr_variantDictAddBool (d, TR_KEY_prefetch_enabled, s->isPrefetchEnabled); |
---|
421 | tr_variantDictAddInt (d, TR_KEY_peer_id_ttl_hours, s->peer_id_ttl_hours); |
---|
422 | tr_variantDictAddBool (d, TR_KEY_queue_stalled_enabled, tr_sessionGetQueueStalledEnabled (s)); |
---|
423 | tr_variantDictAddInt (d, TR_KEY_queue_stalled_minutes, tr_sessionGetQueueStalledMinutes (s)); |
---|
424 | tr_variantDictAddReal (d, TR_KEY_ratio_limit, s->desiredRatio); |
---|
425 | tr_variantDictAddBool (d, TR_KEY_ratio_limit_enabled, s->isRatioLimited); |
---|
426 | tr_variantDictAddBool (d, TR_KEY_rename_partial_files, tr_sessionIsIncompleteFileNamingEnabled (s)); |
---|
427 | tr_variantDictAddBool (d, TR_KEY_rpc_authentication_required, tr_sessionIsRPCPasswordEnabled (s)); |
---|
428 | tr_variantDictAddStr (d, TR_KEY_rpc_bind_address, tr_sessionGetRPCBindAddress (s)); |
---|
429 | tr_variantDictAddBool (d, TR_KEY_rpc_enabled, tr_sessionIsRPCEnabled (s)); |
---|
430 | tr_variantDictAddStr (d, TR_KEY_rpc_password, tr_sessionGetRPCPassword (s)); |
---|
431 | tr_variantDictAddInt (d, TR_KEY_rpc_port, tr_sessionGetRPCPort (s)); |
---|
432 | tr_variantDictAddStr (d, TR_KEY_rpc_url, tr_sessionGetRPCUrl (s)); |
---|
433 | tr_variantDictAddStr (d, TR_KEY_rpc_username, tr_sessionGetRPCUsername (s)); |
---|
434 | tr_variantDictAddStr (d, TR_KEY_rpc_whitelist, tr_sessionGetRPCWhitelist (s)); |
---|
435 | tr_variantDictAddBool (d, TR_KEY_rpc_whitelist_enabled, tr_sessionGetRPCWhitelistEnabled (s)); |
---|
436 | tr_variantDictAddBool (d, TR_KEY_scrape_paused_torrents_enabled, s->scrapePausedTorrents); |
---|
437 | tr_variantDictAddBool (d, TR_KEY_script_torrent_done_enabled, tr_sessionIsTorrentDoneScriptEnabled (s)); |
---|
438 | tr_variantDictAddStr (d, TR_KEY_script_torrent_done_filename, tr_sessionGetTorrentDoneScript (s)); |
---|
439 | tr_variantDictAddInt (d, TR_KEY_seed_queue_size, tr_sessionGetQueueSize (s, TR_UP)); |
---|
440 | tr_variantDictAddBool (d, TR_KEY_seed_queue_enabled, tr_sessionGetQueueEnabled (s, TR_UP)); |
---|
441 | tr_variantDictAddBool (d, TR_KEY_alt_speed_enabled, tr_sessionUsesAltSpeed (s)); |
---|
442 | tr_variantDictAddInt (d, TR_KEY_alt_speed_up, tr_sessionGetAltSpeed_KBps (s, TR_UP)); |
---|
443 | tr_variantDictAddInt (d, TR_KEY_alt_speed_down, tr_sessionGetAltSpeed_KBps (s, TR_DOWN)); |
---|
444 | tr_variantDictAddInt (d, TR_KEY_alt_speed_time_begin, tr_sessionGetAltSpeedBegin (s)); |
---|
445 | tr_variantDictAddBool (d, TR_KEY_alt_speed_time_enabled, tr_sessionUsesAltSpeedTime (s)); |
---|
446 | tr_variantDictAddInt (d, TR_KEY_alt_speed_time_end, tr_sessionGetAltSpeedEnd (s)); |
---|
447 | tr_variantDictAddInt (d, TR_KEY_alt_speed_time_day, tr_sessionGetAltSpeedDay (s)); |
---|
448 | tr_variantDictAddInt (d, TR_KEY_speed_limit_up, tr_sessionGetSpeedLimit_KBps (s, TR_UP)); |
---|
449 | tr_variantDictAddBool (d, TR_KEY_speed_limit_up_enabled, tr_sessionIsSpeedLimited (s, TR_UP)); |
---|
450 | tr_variantDictAddInt (d, TR_KEY_umask, s->umask); |
---|
451 | tr_variantDictAddInt (d, TR_KEY_upload_slots_per_torrent, s->uploadSlotsPerTorrent); |
---|
452 | tr_variantDictAddStr (d, TR_KEY_bind_address_ipv4, tr_address_to_string (&s->public_ipv4->addr)); |
---|
453 | tr_variantDictAddStr (d, TR_KEY_bind_address_ipv6, tr_address_to_string (&s->public_ipv6->addr)); |
---|
454 | tr_variantDictAddBool (d, TR_KEY_start_added_torrents, !tr_sessionGetPaused (s)); |
---|
455 | tr_variantDictAddBool (d, TR_KEY_trash_original_torrent_files, tr_sessionGetDeleteSource (s)); |
---|
456 | } |
---|
457 | |
---|
458 | bool |
---|
459 | tr_sessionLoadSettings (tr_variant * dict, const char * configDir, const char * appName) |
---|
460 | { |
---|
461 | char * filename; |
---|
462 | tr_variant fileSettings; |
---|
463 | tr_variant sessionDefaults; |
---|
464 | tr_variant tmp; |
---|
465 | bool success; |
---|
466 | tr_error * error = NULL; |
---|
467 | |
---|
468 | assert (tr_variantIsDict (dict)); |
---|
469 | |
---|
470 | /* initializing the defaults: caller may have passed in some app-level defaults. |
---|
471 | * preserve those and use the session defaults to fill in any missing gaps. */ |
---|
472 | tr_variantInitDict (&sessionDefaults, 0); |
---|
473 | tr_sessionGetDefaultSettings (&sessionDefaults); |
---|
474 | tr_variantMergeDicts (&sessionDefaults, dict); |
---|
475 | tmp = *dict; |
---|
476 | *dict = sessionDefaults; |
---|
477 | sessionDefaults = tmp; |
---|
478 | |
---|
479 | /* if caller didn't specify a config dir, use the default */ |
---|
480 | if (!configDir || !*configDir) |
---|
481 | configDir = tr_getDefaultConfigDir (appName); |
---|
482 | |
---|
483 | /* file settings override the defaults */ |
---|
484 | filename = tr_buildPath (configDir, "settings.json", NULL); |
---|
485 | if (tr_variantFromFile (&fileSettings, TR_VARIANT_FMT_JSON, filename, &error)) |
---|
486 | { |
---|
487 | tr_variantMergeDicts (dict, &fileSettings); |
---|
488 | tr_variantFree (&fileSettings); |
---|
489 | success = true; |
---|
490 | } |
---|
491 | else |
---|
492 | { |
---|
493 | success = TR_ERROR_IS_ENOENT (error->code); |
---|
494 | tr_error_free (error); |
---|
495 | } |
---|
496 | |
---|
497 | /* cleanup */ |
---|
498 | tr_variantFree (&sessionDefaults); |
---|
499 | tr_free (filename); |
---|
500 | return success; |
---|
501 | } |
---|
502 | |
---|
503 | void |
---|
504 | tr_sessionSaveSettings (tr_session * session, |
---|
505 | const char * configDir, |
---|
506 | const tr_variant * clientSettings) |
---|
507 | { |
---|
508 | tr_variant settings; |
---|
509 | char * filename = tr_buildPath (configDir, "settings.json", NULL); |
---|
510 | |
---|
511 | assert (tr_variantIsDict (clientSettings)); |
---|
512 | |
---|
513 | tr_variantInitDict (&settings, 0); |
---|
514 | |
---|
515 | /* the existing file settings are the fallback values */ |
---|
516 | { |
---|
517 | tr_variant fileSettings; |
---|
518 | if (tr_variantFromFile (&fileSettings, TR_VARIANT_FMT_JSON, filename, NULL)) |
---|
519 | { |
---|
520 | tr_variantMergeDicts (&settings, &fileSettings); |
---|
521 | tr_variantFree (&fileSettings); |
---|
522 | } |
---|
523 | } |
---|
524 | |
---|
525 | /* the client's settings override the file settings */ |
---|
526 | tr_variantMergeDicts (&settings, clientSettings); |
---|
527 | |
---|
528 | /* the session's true values override the file & client settings */ |
---|
529 | { |
---|
530 | tr_variant sessionSettings; |
---|
531 | tr_variantInitDict (&sessionSettings, 0); |
---|
532 | tr_sessionGetSettings (session, &sessionSettings); |
---|
533 | tr_variantMergeDicts (&settings, &sessionSettings); |
---|
534 | tr_variantFree (&sessionSettings); |
---|
535 | } |
---|
536 | |
---|
537 | /* save the result */ |
---|
538 | tr_variantToFile (&settings, TR_VARIANT_FMT_JSON, filename); |
---|
539 | |
---|
540 | /* cleanup */ |
---|
541 | tr_free (filename); |
---|
542 | tr_variantFree (&settings); |
---|
543 | } |
---|
544 | |
---|
545 | /*** |
---|
546 | **** |
---|
547 | ***/ |
---|
548 | |
---|
549 | /** |
---|
550 | * Periodically save the .resume files of any torrents whose |
---|
551 | * status has recently changed. This prevents loss of metadata |
---|
552 | * in the case of a crash, unclean shutdown, clumsy user, etc. |
---|
553 | */ |
---|
554 | static void |
---|
555 | onSaveTimer (evutil_socket_t foo UNUSED, short bar UNUSED, void * vsession) |
---|
556 | { |
---|
557 | tr_torrent * tor = NULL; |
---|
558 | tr_session * session = vsession; |
---|
559 | |
---|
560 | if (tr_cacheFlushDone (session->cache)) |
---|
561 | tr_logAddError ("Error while flushing completed pieces from cache"); |
---|
562 | |
---|
563 | while ((tor = tr_torrentNext (session, tor))) |
---|
564 | tr_torrentSave (tor); |
---|
565 | |
---|
566 | tr_statsSaveDirty (session); |
---|
567 | |
---|
568 | tr_timerAdd (session->saveTimer, SAVE_INTERVAL_SECS, 0); |
---|
569 | } |
---|
570 | |
---|
571 | /*** |
---|
572 | **** |
---|
573 | ***/ |
---|
574 | |
---|
575 | static void tr_sessionInitImpl (void *); |
---|
576 | |
---|
577 | struct init_data |
---|
578 | { |
---|
579 | bool done; |
---|
580 | bool messageQueuingEnabled; |
---|
581 | tr_session * session; |
---|
582 | const char * configDir; |
---|
583 | tr_variant * clientSettings; |
---|
584 | }; |
---|
585 | |
---|
586 | tr_session * |
---|
587 | tr_sessionInit (const char * tag, |
---|
588 | const char * configDir, |
---|
589 | bool messageQueuingEnabled, |
---|
590 | tr_variant * clientSettings) |
---|
591 | { |
---|
592 | int64_t i; |
---|
593 | tr_session * session; |
---|
594 | struct init_data data; |
---|
595 | |
---|
596 | assert (tr_variantIsDict (clientSettings)); |
---|
597 | |
---|
598 | tr_timeUpdate (time (NULL)); |
---|
599 | |
---|
600 | /* initialize the bare skeleton of the session object */ |
---|
601 | session = tr_new0 (tr_session, 1); |
---|
602 | session->udp_socket = TR_BAD_SOCKET; |
---|
603 | session->udp6_socket = TR_BAD_SOCKET; |
---|
604 | session->lock = tr_lockNew (); |
---|
605 | session->cache = tr_cacheNew (1024*1024*2); |
---|
606 | session->tag = tr_strdup (tag); |
---|
607 | session->magicNumber = SESSION_MAGIC_NUMBER; |
---|
608 | tr_bandwidthConstruct (&session->bandwidth, session, NULL); |
---|
609 | tr_variantInitList (&session->removedTorrents, 0); |
---|
610 | |
---|
611 | /* nice to start logging at the very beginning */ |
---|
612 | if (tr_variantDictFindInt (clientSettings, TR_KEY_message_level, &i)) |
---|
613 | tr_logSetLevel (i); |
---|
614 | |
---|
615 | /* start the libtransmission thread */ |
---|
616 | tr_netInit (); /* must go before tr_eventInit */ |
---|
617 | tr_eventInit (session); |
---|
618 | assert (session->events != NULL); |
---|
619 | |
---|
620 | /* run the rest in the libtransmission thread */ |
---|
621 | data.done = false; |
---|
622 | data.session = session; |
---|
623 | data.configDir = configDir; |
---|
624 | data.messageQueuingEnabled = messageQueuingEnabled; |
---|
625 | data.clientSettings = clientSettings; |
---|
626 | tr_runInEventThread (session, tr_sessionInitImpl, &data); |
---|
627 | while (!data.done) |
---|
628 | tr_wait_msec (50); |
---|
629 | |
---|
630 | return session; |
---|
631 | } |
---|
632 | |
---|
633 | static void turtleCheckClock (tr_session * s, struct tr_turtle_info * t); |
---|
634 | |
---|
635 | static void |
---|
636 | onNowTimer (evutil_socket_t foo UNUSED, short bar UNUSED, void * vsession) |
---|
637 | { |
---|
638 | int usec; |
---|
639 | const int min = 100; |
---|
640 | const int max = 999999; |
---|
641 | struct timeval tv; |
---|
642 | tr_torrent * tor = NULL; |
---|
643 | tr_session * session = vsession; |
---|
644 | const time_t now = time (NULL); |
---|
645 | |
---|
646 | assert (tr_isSession (session)); |
---|
647 | assert (session->nowTimer != NULL); |
---|
648 | |
---|
649 | /** |
---|
650 | *** tr_session things to do once per second |
---|
651 | **/ |
---|
652 | |
---|
653 | tr_timeUpdate (now); |
---|
654 | |
---|
655 | tr_dhtUpkeep (session); |
---|
656 | |
---|
657 | if (session->turtle.isClockEnabled) |
---|
658 | turtleCheckClock (session, &session->turtle); |
---|
659 | |
---|
660 | while ((tor = tr_torrentNext (session, tor))) |
---|
661 | { |
---|
662 | if (tor->isRunning) |
---|
663 | { |
---|
664 | if (tr_torrentIsSeed (tor)) |
---|
665 | ++tor->secondsSeeding; |
---|
666 | else |
---|
667 | ++tor->secondsDownloading; |
---|
668 | } |
---|
669 | } |
---|
670 | |
---|
671 | /** |
---|
672 | *** Set the timer |
---|
673 | **/ |
---|
674 | |
---|
675 | /* schedule the next timer for right after the next second begins */ |
---|
676 | tr_gettimeofday (&tv); |
---|
677 | usec = 1000000 - tv.tv_usec; |
---|
678 | if (usec > max) |
---|
679 | usec = max; |
---|
680 | if (usec < min) |
---|
681 | usec = min; |
---|
682 | tr_timerAdd (session->nowTimer, 0, usec); |
---|
683 | /* fprintf (stderr, "time %"TR_PRIuSIZE" sec, %"TR_PRIuSIZE" microsec\n", (size_t)tr_time (), (size_t)tv.tv_usec); */ |
---|
684 | } |
---|
685 | |
---|
686 | static void loadBlocklists (tr_session * session); |
---|
687 | |
---|
688 | static void |
---|
689 | tr_sessionInitImpl (void * vdata) |
---|
690 | { |
---|
691 | tr_variant settings; |
---|
692 | struct init_data * data = vdata; |
---|
693 | tr_variant * clientSettings = data->clientSettings; |
---|
694 | tr_session * session = data->session; |
---|
695 | |
---|
696 | assert (tr_amInEventThread (session)); |
---|
697 | assert (tr_variantIsDict (clientSettings)); |
---|
698 | |
---|
699 | dbgmsg ("tr_sessionInit: the session's top-level bandwidth object is %p", |
---|
700 | (void*)&session->bandwidth); |
---|
701 | |
---|
702 | tr_variantInitDict (&settings, 0); |
---|
703 | tr_sessionGetDefaultSettings (&settings); |
---|
704 | tr_variantMergeDicts (&settings, clientSettings); |
---|
705 | |
---|
706 | assert (session->event_base != NULL); |
---|
707 | session->nowTimer = evtimer_new (session->event_base, onNowTimer, session); |
---|
708 | onNowTimer (0, 0, session); |
---|
709 | |
---|
710 | #ifndef _WIN32 |
---|
711 | /* Don't exit when writing on a broken socket */ |
---|
712 | signal (SIGPIPE, SIG_IGN); |
---|
713 | #endif |
---|
714 | |
---|
715 | tr_logSetQueueEnabled (data->messageQueuingEnabled); |
---|
716 | |
---|
717 | tr_setConfigDir (session, data->configDir); |
---|
718 | |
---|
719 | session->peerMgr = tr_peerMgrNew (session); |
---|
720 | |
---|
721 | session->shared = tr_sharedInit (session); |
---|
722 | |
---|
723 | /** |
---|
724 | *** Blocklist |
---|
725 | **/ |
---|
726 | |
---|
727 | { |
---|
728 | char * filename = tr_buildPath (session->configDir, "blocklists", NULL); |
---|
729 | tr_sys_dir_create (filename, TR_SYS_DIR_CREATE_PARENTS, 0777, NULL); |
---|
730 | tr_free (filename); |
---|
731 | loadBlocklists (session); |
---|
732 | } |
---|
733 | |
---|
734 | assert (tr_isSession (session)); |
---|
735 | |
---|
736 | session->saveTimer = evtimer_new (session->event_base, onSaveTimer, session); |
---|
737 | tr_timerAdd (session->saveTimer, SAVE_INTERVAL_SECS, 0); |
---|
738 | |
---|
739 | tr_announcerInit (session); |
---|
740 | |
---|
741 | /* first %s is the application name |
---|
742 | second %s is the version number */ |
---|
743 | tr_logAddInfo (_("%s %s started"), TR_NAME, LONG_VERSION_STRING); |
---|
744 | |
---|
745 | tr_statsInit (session); |
---|
746 | |
---|
747 | tr_sessionSet (session, &settings); |
---|
748 | |
---|
749 | tr_udpInit (session); |
---|
750 | |
---|
751 | if (session->isLPDEnabled) |
---|
752 | tr_lpdInit (session, &session->public_ipv4->addr); |
---|
753 | |
---|
754 | /* cleanup */ |
---|
755 | tr_variantFree (&settings); |
---|
756 | data->done = true; |
---|
757 | } |
---|
758 | |
---|
759 | static void turtleBootstrap (tr_session *, struct tr_turtle_info *); |
---|
760 | static void setPeerPort (tr_session * session, tr_port port); |
---|
761 | |
---|
762 | static void |
---|
763 | sessionSetImpl (void * vdata) |
---|
764 | { |
---|
765 | int64_t i; |
---|
766 | double d; |
---|
767 | bool boolVal; |
---|
768 | const char * str; |
---|
769 | struct tr_bindinfo b; |
---|
770 | struct init_data * data = vdata; |
---|
771 | tr_session * session = data->session; |
---|
772 | tr_variant * settings = data->clientSettings; |
---|
773 | struct tr_turtle_info * turtle = &session->turtle; |
---|
774 | |
---|
775 | assert (tr_isSession (session)); |
---|
776 | assert (tr_variantIsDict (settings)); |
---|
777 | assert (tr_amInEventThread (session)); |
---|
778 | |
---|
779 | if (tr_variantDictFindInt (settings, TR_KEY_message_level, &i)) |
---|
780 | tr_logSetLevel (i); |
---|
781 | |
---|
782 | #ifndef _WIN32 |
---|
783 | if (tr_variantDictFindInt (settings, TR_KEY_umask, &i)) |
---|
784 | { |
---|
785 | session->umask = (mode_t)i; |
---|
786 | umask (session->umask); |
---|
787 | } |
---|
788 | #endif |
---|
789 | |
---|
790 | /* misc features */ |
---|
791 | if (tr_variantDictFindInt (settings, TR_KEY_cache_size_mb, &i)) |
---|
792 | tr_sessionSetCacheLimit_MB (session, i); |
---|
793 | if (tr_variantDictFindInt (settings, TR_KEY_peer_limit_per_torrent, &i)) |
---|
794 | tr_sessionSetPeerLimitPerTorrent (session, i); |
---|
795 | if (tr_variantDictFindBool (settings, TR_KEY_pex_enabled, &boolVal)) |
---|
796 | tr_sessionSetPexEnabled (session, boolVal); |
---|
797 | if (tr_variantDictFindBool (settings, TR_KEY_dht_enabled, &boolVal)) |
---|
798 | tr_sessionSetDHTEnabled (session, boolVal); |
---|
799 | if (tr_variantDictFindBool (settings, TR_KEY_utp_enabled, &boolVal)) |
---|
800 | tr_sessionSetUTPEnabled (session, boolVal); |
---|
801 | if (tr_variantDictFindBool (settings, TR_KEY_lpd_enabled, &boolVal)) |
---|
802 | tr_sessionSetLPDEnabled (session, boolVal); |
---|
803 | if (tr_variantDictFindInt (settings, TR_KEY_encryption, &i)) |
---|
804 | tr_sessionSetEncryption (session, i); |
---|
805 | if (tr_variantDictFindStr (settings, TR_KEY_peer_socket_tos, &str, NULL)) |
---|
806 | session->peerSocketTOS = parse_tos (str); |
---|
807 | if (tr_variantDictFindStr (settings, TR_KEY_peer_congestion_algorithm, &str, NULL)) |
---|
808 | session->peer_congestion_algorithm = tr_strdup (str); |
---|
809 | else |
---|
810 | session->peer_congestion_algorithm = tr_strdup (""); |
---|
811 | if (tr_variantDictFindBool (settings, TR_KEY_blocklist_enabled, &boolVal)) |
---|
812 | tr_blocklistSetEnabled (session, boolVal); |
---|
813 | if (tr_variantDictFindStr (settings, TR_KEY_blocklist_url, &str, NULL)) |
---|
814 | tr_blocklistSetURL (session, str); |
---|
815 | if (tr_variantDictFindBool (settings, TR_KEY_start_added_torrents, &boolVal)) |
---|
816 | tr_sessionSetPaused (session, !boolVal); |
---|
817 | if (tr_variantDictFindBool (settings, TR_KEY_trash_original_torrent_files, &boolVal)) |
---|
818 | tr_sessionSetDeleteSource (session, boolVal); |
---|
819 | if (tr_variantDictFindInt (settings, TR_KEY_peer_id_ttl_hours, &i)) |
---|
820 | session->peer_id_ttl_hours = i; |
---|
821 | |
---|
822 | /* torrent queues */ |
---|
823 | if (tr_variantDictFindInt (settings, TR_KEY_queue_stalled_minutes, &i)) |
---|
824 | tr_sessionSetQueueStalledMinutes (session, i); |
---|
825 | if (tr_variantDictFindBool (settings, TR_KEY_queue_stalled_enabled, &boolVal)) |
---|
826 | tr_sessionSetQueueStalledEnabled (session, boolVal); |
---|
827 | if (tr_variantDictFindInt (settings, TR_KEY_download_queue_size, &i)) |
---|
828 | tr_sessionSetQueueSize (session, TR_DOWN, i); |
---|
829 | if (tr_variantDictFindBool (settings, TR_KEY_download_queue_enabled, &boolVal)) |
---|
830 | tr_sessionSetQueueEnabled (session, TR_DOWN, boolVal); |
---|
831 | if (tr_variantDictFindInt (settings, TR_KEY_seed_queue_size, &i)) |
---|
832 | tr_sessionSetQueueSize (session, TR_UP, i); |
---|
833 | if (tr_variantDictFindBool (settings, TR_KEY_seed_queue_enabled, &boolVal)) |
---|
834 | tr_sessionSetQueueEnabled (session, TR_UP, boolVal); |
---|
835 | |
---|
836 | /* files and directories */ |
---|
837 | if (tr_variantDictFindBool (settings, TR_KEY_prefetch_enabled, &boolVal)) |
---|
838 | session->isPrefetchEnabled = boolVal; |
---|
839 | if (tr_variantDictFindInt (settings, TR_KEY_preallocation, &i)) |
---|
840 | session->preallocationMode = i; |
---|
841 | if (tr_variantDictFindStr (settings, TR_KEY_download_dir, &str, NULL)) |
---|
842 | tr_sessionSetDownloadDir (session, str); |
---|
843 | if (tr_variantDictFindStr (settings, TR_KEY_incomplete_dir, &str, NULL)) |
---|
844 | tr_sessionSetIncompleteDir (session, str); |
---|
845 | if (tr_variantDictFindBool (settings, TR_KEY_incomplete_dir_enabled, &boolVal)) |
---|
846 | tr_sessionSetIncompleteDirEnabled (session, boolVal); |
---|
847 | if (tr_variantDictFindBool (settings, TR_KEY_rename_partial_files, &boolVal)) |
---|
848 | tr_sessionSetIncompleteFileNamingEnabled (session, boolVal); |
---|
849 | |
---|
850 | /* rpc server */ |
---|
851 | if (session->rpcServer != NULL) /* close the old one */ |
---|
852 | tr_rpcClose (&session->rpcServer); |
---|
853 | session->rpcServer = tr_rpcInit (session, settings); |
---|
854 | |
---|
855 | /* public addresses */ |
---|
856 | |
---|
857 | free_incoming_peer_port (session); |
---|
858 | |
---|
859 | tr_variantDictFindStr (settings, TR_KEY_bind_address_ipv4, &str, NULL); |
---|
860 | if (!tr_address_from_string (&b.addr, str) || (b.addr.type != TR_AF_INET)) |
---|
861 | b.addr = tr_inaddr_any; |
---|
862 | b.socket = TR_BAD_SOCKET; |
---|
863 | session->public_ipv4 = tr_memdup (&b, sizeof (struct tr_bindinfo)); |
---|
864 | |
---|
865 | tr_variantDictFindStr (settings, TR_KEY_bind_address_ipv6, &str, NULL); |
---|
866 | if (!tr_address_from_string (&b.addr, str) || (b.addr.type != TR_AF_INET6)) |
---|
867 | b.addr = tr_in6addr_any; |
---|
868 | b.socket = TR_BAD_SOCKET; |
---|
869 | session->public_ipv6 = tr_memdup (&b, sizeof (struct tr_bindinfo)); |
---|
870 | |
---|
871 | /* incoming peer port */ |
---|
872 | if (tr_variantDictFindInt (settings, TR_KEY_peer_port_random_low, &i)) |
---|
873 | session->randomPortLow = i; |
---|
874 | if (tr_variantDictFindInt (settings, TR_KEY_peer_port_random_high, &i)) |
---|
875 | session->randomPortHigh = i; |
---|
876 | if (tr_variantDictFindBool (settings, TR_KEY_peer_port_random_on_start, &boolVal)) |
---|
877 | tr_sessionSetPeerPortRandomOnStart (session, boolVal); |
---|
878 | if (!tr_variantDictFindInt (settings, TR_KEY_peer_port, &i)) |
---|
879 | i = session->private_peer_port; |
---|
880 | setPeerPort (session, boolVal ? getRandomPort (session) : i); |
---|
881 | if (tr_variantDictFindBool (settings, TR_KEY_port_forwarding_enabled, &boolVal)) |
---|
882 | tr_sessionSetPortForwardingEnabled (session, boolVal); |
---|
883 | |
---|
884 | if (tr_variantDictFindInt (settings, TR_KEY_peer_limit_global, &i)) |
---|
885 | session->peerLimit = i; |
---|
886 | |
---|
887 | /** |
---|
888 | **/ |
---|
889 | |
---|
890 | if (tr_variantDictFindInt (settings, TR_KEY_upload_slots_per_torrent, &i)) |
---|
891 | session->uploadSlotsPerTorrent = i; |
---|
892 | |
---|
893 | if (tr_variantDictFindInt (settings, TR_KEY_speed_limit_up, &i)) |
---|
894 | tr_sessionSetSpeedLimit_KBps (session, TR_UP, i); |
---|
895 | if (tr_variantDictFindBool (settings, TR_KEY_speed_limit_up_enabled, &boolVal)) |
---|
896 | tr_sessionLimitSpeed (session, TR_UP, boolVal); |
---|
897 | |
---|
898 | if (tr_variantDictFindInt (settings, TR_KEY_speed_limit_down, &i)) |
---|
899 | tr_sessionSetSpeedLimit_KBps (session, TR_DOWN, i); |
---|
900 | if (tr_variantDictFindBool (settings, TR_KEY_speed_limit_down_enabled, &boolVal)) |
---|
901 | tr_sessionLimitSpeed (session, TR_DOWN, boolVal); |
---|
902 | |
---|
903 | if (tr_variantDictFindReal (settings, TR_KEY_ratio_limit, &d)) |
---|
904 | tr_sessionSetRatioLimit (session, d); |
---|
905 | if (tr_variantDictFindBool (settings, TR_KEY_ratio_limit_enabled, &boolVal)) |
---|
906 | tr_sessionSetRatioLimited (session, boolVal); |
---|
907 | |
---|
908 | if (tr_variantDictFindInt (settings, TR_KEY_idle_seeding_limit, &i)) |
---|
909 | tr_sessionSetIdleLimit (session, i); |
---|
910 | if (tr_variantDictFindBool (settings, TR_KEY_idle_seeding_limit_enabled, &boolVal)) |
---|
911 | tr_sessionSetIdleLimited (session, boolVal); |
---|
912 | |
---|
913 | /** |
---|
914 | *** Turtle Mode |
---|
915 | **/ |
---|
916 | |
---|
917 | /* update the turtle mode's fields */ |
---|
918 | if (tr_variantDictFindInt (settings, TR_KEY_alt_speed_up, &i)) |
---|
919 | turtle->speedLimit_Bps[TR_UP] = toSpeedBytes (i); |
---|
920 | if (tr_variantDictFindInt (settings, TR_KEY_alt_speed_down, &i)) |
---|
921 | turtle->speedLimit_Bps[TR_DOWN] = toSpeedBytes (i); |
---|
922 | if (tr_variantDictFindInt (settings, TR_KEY_alt_speed_time_begin, &i)) |
---|
923 | turtle->beginMinute = i; |
---|
924 | if (tr_variantDictFindInt (settings, TR_KEY_alt_speed_time_end, &i)) |
---|
925 | turtle->endMinute = i; |
---|
926 | if (tr_variantDictFindInt (settings, TR_KEY_alt_speed_time_day, &i)) |
---|
927 | turtle->days = i; |
---|
928 | if (tr_variantDictFindBool (settings, TR_KEY_alt_speed_time_enabled, &boolVal)) |
---|
929 | turtle->isClockEnabled = boolVal; |
---|
930 | if (tr_variantDictFindBool (settings, TR_KEY_alt_speed_enabled, &boolVal)) |
---|
931 | turtle->isEnabled = boolVal; |
---|
932 | turtleBootstrap (session, turtle); |
---|
933 | |
---|
934 | /** |
---|
935 | *** Scripts |
---|
936 | **/ |
---|
937 | |
---|
938 | if (tr_variantDictFindBool (settings, TR_KEY_script_torrent_done_enabled, &boolVal)) |
---|
939 | tr_sessionSetTorrentDoneScriptEnabled (session, boolVal); |
---|
940 | if (tr_variantDictFindStr (settings, TR_KEY_script_torrent_done_filename, &str, NULL)) |
---|
941 | tr_sessionSetTorrentDoneScript (session, str); |
---|
942 | |
---|
943 | |
---|
944 | if (tr_variantDictFindBool (settings, TR_KEY_scrape_paused_torrents_enabled, &boolVal)) |
---|
945 | session->scrapePausedTorrents = boolVal; |
---|
946 | |
---|
947 | data->done = true; |
---|
948 | } |
---|
949 | |
---|
950 | void |
---|
951 | tr_sessionSet (tr_session * session, tr_variant * settings) |
---|
952 | { |
---|
953 | struct init_data data; |
---|
954 | data.done = false; |
---|
955 | data.session = session; |
---|
956 | data.clientSettings = settings; |
---|
957 | |
---|
958 | /* run the rest in the libtransmission thread */ |
---|
959 | tr_runInEventThread (session, sessionSetImpl, &data); |
---|
960 | while (!data.done) |
---|
961 | tr_wait_msec (100); |
---|
962 | } |
---|
963 | |
---|
964 | /*** |
---|
965 | **** |
---|
966 | ***/ |
---|
967 | |
---|
968 | void |
---|
969 | tr_sessionSetDownloadDir (tr_session * session, const char * dir) |
---|
970 | { |
---|
971 | struct tr_device_info * info = NULL; |
---|
972 | |
---|
973 | assert (tr_isSession (session)); |
---|
974 | |
---|
975 | if (dir != NULL) |
---|
976 | info = tr_device_info_create (dir); |
---|
977 | tr_device_info_free (session->downloadDir); |
---|
978 | session->downloadDir = info; |
---|
979 | } |
---|
980 | |
---|
981 | const char * |
---|
982 | tr_sessionGetDownloadDir (const tr_session * session) |
---|
983 | { |
---|
984 | const char * dir = NULL; |
---|
985 | |
---|
986 | assert (tr_isSession (session)); |
---|
987 | |
---|
988 | if ((session != NULL) && (session->downloadDir != NULL)) |
---|
989 | dir = session->downloadDir->path; |
---|
990 | |
---|
991 | return dir; |
---|
992 | } |
---|
993 | |
---|
994 | int64_t |
---|
995 | tr_sessionGetDirFreeSpace (tr_session * session, const char * dir) |
---|
996 | { |
---|
997 | int64_t free_space; |
---|
998 | |
---|
999 | if (!tr_strcmp0 (dir, tr_sessionGetDownloadDir (session))) |
---|
1000 | free_space = tr_device_info_get_free_space (session->downloadDir); |
---|
1001 | else |
---|
1002 | free_space = tr_getDirFreeSpace (dir); |
---|
1003 | |
---|
1004 | return free_space; |
---|
1005 | } |
---|
1006 | |
---|
1007 | /*** |
---|
1008 | **** |
---|
1009 | ***/ |
---|
1010 | |
---|
1011 | void |
---|
1012 | tr_sessionSetIncompleteFileNamingEnabled (tr_session * session, bool b) |
---|
1013 | { |
---|
1014 | assert (tr_isSession (session)); |
---|
1015 | assert (tr_isBool (b)); |
---|
1016 | |
---|
1017 | session->isIncompleteFileNamingEnabled = b; |
---|
1018 | } |
---|
1019 | |
---|
1020 | bool |
---|
1021 | tr_sessionIsIncompleteFileNamingEnabled (const tr_session * session) |
---|
1022 | { |
---|
1023 | assert (tr_isSession (session)); |
---|
1024 | |
---|
1025 | return session->isIncompleteFileNamingEnabled; |
---|
1026 | } |
---|
1027 | |
---|
1028 | /*** |
---|
1029 | **** |
---|
1030 | ***/ |
---|
1031 | |
---|
1032 | |
---|
1033 | void |
---|
1034 | tr_sessionSetIncompleteDir (tr_session * session, const char * dir) |
---|
1035 | { |
---|
1036 | assert (tr_isSession (session)); |
---|
1037 | |
---|
1038 | if (session->incompleteDir != dir) |
---|
1039 | { |
---|
1040 | tr_free (session->incompleteDir); |
---|
1041 | |
---|
1042 | session->incompleteDir = tr_strdup (dir); |
---|
1043 | } |
---|
1044 | } |
---|
1045 | |
---|
1046 | const char* |
---|
1047 | tr_sessionGetIncompleteDir (const tr_session * session) |
---|
1048 | { |
---|
1049 | assert (tr_isSession (session)); |
---|
1050 | |
---|
1051 | return session->incompleteDir; |
---|
1052 | } |
---|
1053 | |
---|
1054 | void |
---|
1055 | tr_sessionSetIncompleteDirEnabled (tr_session * session, bool b) |
---|
1056 | { |
---|
1057 | assert (tr_isSession (session)); |
---|
1058 | assert (tr_isBool (b)); |
---|
1059 | |
---|
1060 | session->isIncompleteDirEnabled = b; |
---|
1061 | } |
---|
1062 | |
---|
1063 | bool |
---|
1064 | tr_sessionIsIncompleteDirEnabled (const tr_session * session) |
---|
1065 | { |
---|
1066 | assert (tr_isSession (session)); |
---|
1067 | |
---|
1068 | return session->isIncompleteDirEnabled; |
---|
1069 | } |
---|
1070 | |
---|
1071 | /*** |
---|
1072 | **** |
---|
1073 | ***/ |
---|
1074 | |
---|
1075 | void |
---|
1076 | tr_sessionLock (tr_session * session) |
---|
1077 | { |
---|
1078 | assert (tr_isSession (session)); |
---|
1079 | |
---|
1080 | tr_lockLock (session->lock); |
---|
1081 | } |
---|
1082 | |
---|
1083 | void |
---|
1084 | tr_sessionUnlock (tr_session * session) |
---|
1085 | { |
---|
1086 | assert (tr_isSession (session)); |
---|
1087 | |
---|
1088 | tr_lockUnlock (session->lock); |
---|
1089 | } |
---|
1090 | |
---|
1091 | bool |
---|
1092 | tr_sessionIsLocked (const tr_session * session) |
---|
1093 | { |
---|
1094 | return tr_isSession (session) && tr_lockHave (session->lock); |
---|
1095 | } |
---|
1096 | |
---|
1097 | /*** |
---|
1098 | **** Peer Port |
---|
1099 | ***/ |
---|
1100 | |
---|
1101 | static void |
---|
1102 | peerPortChanged (void * session) |
---|
1103 | { |
---|
1104 | tr_torrent * tor = NULL; |
---|
1105 | |
---|
1106 | assert (tr_isSession (session)); |
---|
1107 | |
---|
1108 | close_incoming_peer_port (session); |
---|
1109 | open_incoming_peer_port (session); |
---|
1110 | tr_sharedPortChanged (session); |
---|
1111 | |
---|
1112 | while ((tor = tr_torrentNext (session, tor))) |
---|
1113 | tr_torrentChangeMyPort (tor); |
---|
1114 | } |
---|
1115 | |
---|
1116 | static void |
---|
1117 | setPeerPort (tr_session * session, tr_port port) |
---|
1118 | { |
---|
1119 | session->private_peer_port = port; |
---|
1120 | session->public_peer_port = port; |
---|
1121 | |
---|
1122 | tr_runInEventThread (session, peerPortChanged, session); |
---|
1123 | } |
---|
1124 | |
---|
1125 | void |
---|
1126 | tr_sessionSetPeerPort (tr_session * session, tr_port port) |
---|
1127 | { |
---|
1128 | if (tr_isSession (session) && (session->private_peer_port != port)) |
---|
1129 | { |
---|
1130 | setPeerPort (session, port); |
---|
1131 | } |
---|
1132 | } |
---|
1133 | |
---|
1134 | tr_port |
---|
1135 | tr_sessionGetPeerPort (const tr_session * session) |
---|
1136 | { |
---|
1137 | return tr_isSession (session) ? session->private_peer_port : 0; |
---|
1138 | } |
---|
1139 | |
---|
1140 | tr_port |
---|
1141 | tr_sessionSetPeerPortRandom (tr_session * session) |
---|
1142 | { |
---|
1143 | assert (tr_isSession (session)); |
---|
1144 | |
---|
1145 | tr_sessionSetPeerPort (session, getRandomPort (session)); |
---|
1146 | return session->private_peer_port; |
---|
1147 | } |
---|
1148 | |
---|
1149 | void |
---|
1150 | tr_sessionSetPeerPortRandomOnStart (tr_session * session, |
---|
1151 | bool random) |
---|
1152 | { |
---|
1153 | assert (tr_isSession (session)); |
---|
1154 | |
---|
1155 | session->isPortRandom = random; |
---|
1156 | } |
---|
1157 | |
---|
1158 | bool |
---|
1159 | tr_sessionGetPeerPortRandomOnStart (tr_session * session) |
---|
1160 | { |
---|
1161 | assert (tr_isSession (session)); |
---|
1162 | |
---|
1163 | return session->isPortRandom; |
---|
1164 | } |
---|
1165 | |
---|
1166 | tr_port_forwarding |
---|
1167 | tr_sessionGetPortForwarding (const tr_session * session) |
---|
1168 | { |
---|
1169 | assert (tr_isSession (session)); |
---|
1170 | |
---|
1171 | return tr_sharedTraversalStatus (session->shared); |
---|
1172 | } |
---|
1173 | |
---|
1174 | /*** |
---|
1175 | **** |
---|
1176 | ***/ |
---|
1177 | |
---|
1178 | void |
---|
1179 | tr_sessionSetRatioLimited (tr_session * session, bool isLimited) |
---|
1180 | { |
---|
1181 | assert (tr_isSession (session)); |
---|
1182 | |
---|
1183 | session->isRatioLimited = isLimited; |
---|
1184 | } |
---|
1185 | |
---|
1186 | void |
---|
1187 | tr_sessionSetRatioLimit (tr_session * session, double desiredRatio) |
---|
1188 | { |
---|
1189 | assert (tr_isSession (session)); |
---|
1190 | |
---|
1191 | session->desiredRatio = desiredRatio; |
---|
1192 | } |
---|
1193 | |
---|
1194 | bool |
---|
1195 | tr_sessionIsRatioLimited (const tr_session * session) |
---|
1196 | { |
---|
1197 | assert (tr_isSession (session)); |
---|
1198 | |
---|
1199 | return session->isRatioLimited; |
---|
1200 | } |
---|
1201 | |
---|
1202 | double |
---|
1203 | tr_sessionGetRatioLimit (const tr_session * session) |
---|
1204 | { |
---|
1205 | assert (tr_isSession (session)); |
---|
1206 | |
---|
1207 | return session->desiredRatio; |
---|
1208 | } |
---|
1209 | |
---|
1210 | /*** |
---|
1211 | **** |
---|
1212 | ***/ |
---|
1213 | |
---|
1214 | void |
---|
1215 | tr_sessionSetIdleLimited (tr_session * session, bool isLimited) |
---|
1216 | { |
---|
1217 | assert (tr_isSession (session)); |
---|
1218 | |
---|
1219 | session->isIdleLimited = isLimited; |
---|
1220 | } |
---|
1221 | |
---|
1222 | void |
---|
1223 | tr_sessionSetIdleLimit (tr_session * session, uint16_t idleMinutes) |
---|
1224 | { |
---|
1225 | assert (tr_isSession (session)); |
---|
1226 | |
---|
1227 | session->idleLimitMinutes = idleMinutes; |
---|
1228 | } |
---|
1229 | |
---|
1230 | bool |
---|
1231 | tr_sessionIsIdleLimited (const tr_session * session) |
---|
1232 | { |
---|
1233 | assert (tr_isSession (session)); |
---|
1234 | |
---|
1235 | return session->isIdleLimited; |
---|
1236 | } |
---|
1237 | |
---|
1238 | uint16_t |
---|
1239 | tr_sessionGetIdleLimit (const tr_session * session) |
---|
1240 | { |
---|
1241 | assert (tr_isSession (session)); |
---|
1242 | |
---|
1243 | return session->idleLimitMinutes; |
---|
1244 | } |
---|
1245 | |
---|
1246 | /*** |
---|
1247 | **** |
---|
1248 | **** SPEED LIMITS |
---|
1249 | **** |
---|
1250 | ***/ |
---|
1251 | |
---|
1252 | bool |
---|
1253 | tr_sessionGetActiveSpeedLimit_Bps (const tr_session * session, tr_direction dir, unsigned int * setme_Bps) |
---|
1254 | { |
---|
1255 | bool isLimited = true; |
---|
1256 | |
---|
1257 | if (!tr_isSession (session)) |
---|
1258 | return false; |
---|
1259 | |
---|
1260 | if (tr_sessionUsesAltSpeed (session)) |
---|
1261 | *setme_Bps = tr_sessionGetAltSpeed_Bps (session, dir); |
---|
1262 | else if (tr_sessionIsSpeedLimited (session, dir)) |
---|
1263 | *setme_Bps = tr_sessionGetSpeedLimit_Bps (session, dir); |
---|
1264 | else |
---|
1265 | isLimited = false; |
---|
1266 | |
---|
1267 | return isLimited; |
---|
1268 | } |
---|
1269 | bool |
---|
1270 | tr_sessionGetActiveSpeedLimit_KBps (const tr_session * session, |
---|
1271 | tr_direction dir, |
---|
1272 | double * setme_KBps) |
---|
1273 | { |
---|
1274 | unsigned int Bps = 0; |
---|
1275 | const bool is_active = tr_sessionGetActiveSpeedLimit_Bps (session, dir, &Bps); |
---|
1276 | *setme_KBps = toSpeedKBps (Bps); |
---|
1277 | return is_active; |
---|
1278 | } |
---|
1279 | |
---|
1280 | static void |
---|
1281 | updateBandwidth (tr_session * session, tr_direction dir) |
---|
1282 | { |
---|
1283 | unsigned int limit_Bps = 0; |
---|
1284 | const bool isLimited = tr_sessionGetActiveSpeedLimit_Bps (session, dir, &limit_Bps); |
---|
1285 | const bool zeroCase = isLimited && !limit_Bps; |
---|
1286 | |
---|
1287 | tr_bandwidthSetLimited (&session->bandwidth, dir, isLimited && !zeroCase); |
---|
1288 | |
---|
1289 | tr_bandwidthSetDesiredSpeed_Bps (&session->bandwidth, dir, limit_Bps); |
---|
1290 | } |
---|
1291 | |
---|
1292 | enum |
---|
1293 | { |
---|
1294 | MINUTES_PER_HOUR = 60, |
---|
1295 | MINUTES_PER_DAY = MINUTES_PER_HOUR * 24, |
---|
1296 | MINUTES_PER_WEEK = MINUTES_PER_DAY * 7 |
---|
1297 | }; |
---|
1298 | |
---|
1299 | static void |
---|
1300 | turtleUpdateTable (struct tr_turtle_info * t) |
---|
1301 | { |
---|
1302 | int day; |
---|
1303 | tr_bitfield * b = &t->minutes; |
---|
1304 | |
---|
1305 | tr_bitfieldSetHasNone (b); |
---|
1306 | |
---|
1307 | for (day=0; day<7; ++day) |
---|
1308 | { |
---|
1309 | if (t->days & (1<<day)) |
---|
1310 | { |
---|
1311 | int i; |
---|
1312 | const time_t begin = t->beginMinute; |
---|
1313 | time_t end = t->endMinute; |
---|
1314 | |
---|
1315 | if (end <= begin) |
---|
1316 | end += MINUTES_PER_DAY; |
---|
1317 | |
---|
1318 | for (i=begin; i<end; ++i) |
---|
1319 | tr_bitfieldAdd (b, (i+day*MINUTES_PER_DAY) % MINUTES_PER_WEEK); |
---|
1320 | } |
---|
1321 | } |
---|
1322 | } |
---|
1323 | |
---|
1324 | static void |
---|
1325 | altSpeedToggled (void * vsession) |
---|
1326 | { |
---|
1327 | tr_session * session = vsession; |
---|
1328 | struct tr_turtle_info * t = &session->turtle; |
---|
1329 | |
---|
1330 | assert (tr_isSession (session)); |
---|
1331 | |
---|
1332 | updateBandwidth (session, TR_UP); |
---|
1333 | updateBandwidth (session, TR_DOWN); |
---|
1334 | |
---|
1335 | if (t->callback != NULL) |
---|
1336 | (*t->callback)(session, t->isEnabled, t->changedByUser, t->callbackUserData); |
---|
1337 | } |
---|
1338 | |
---|
1339 | static void |
---|
1340 | useAltSpeed (tr_session * s, struct tr_turtle_info * t, |
---|
1341 | bool enabled, bool byUser) |
---|
1342 | { |
---|
1343 | assert (tr_isSession (s)); |
---|
1344 | assert (t != NULL); |
---|
1345 | assert (tr_isBool (enabled)); |
---|
1346 | assert (tr_isBool (byUser)); |
---|
1347 | |
---|
1348 | if (t->isEnabled != enabled) |
---|
1349 | { |
---|
1350 | t->isEnabled = enabled; |
---|
1351 | t->changedByUser = byUser; |
---|
1352 | tr_runInEventThread (s, altSpeedToggled, s); |
---|
1353 | } |
---|
1354 | } |
---|
1355 | |
---|
1356 | /** |
---|
1357 | * @return whether turtle should be on/off according to the scheduler |
---|
1358 | */ |
---|
1359 | static bool |
---|
1360 | getInTurtleTime (const struct tr_turtle_info * t) |
---|
1361 | { |
---|
1362 | struct tm tm; |
---|
1363 | size_t minute_of_the_week; |
---|
1364 | const time_t now = tr_time (); |
---|
1365 | |
---|
1366 | tr_localtime_r (&now, &tm); |
---|
1367 | |
---|
1368 | minute_of_the_week = tm.tm_wday * MINUTES_PER_DAY |
---|
1369 | + tm.tm_hour * MINUTES_PER_HOUR |
---|
1370 | + tm.tm_min; |
---|
1371 | if (minute_of_the_week >= MINUTES_PER_WEEK) /* leap minutes? */ |
---|
1372 | minute_of_the_week = MINUTES_PER_WEEK - 1; |
---|
1373 | |
---|
1374 | return tr_bitfieldHas (&t->minutes, minute_of_the_week); |
---|
1375 | } |
---|
1376 | |
---|
1377 | static inline tr_auto_switch_state_t |
---|
1378 | autoSwitchState (bool enabled) |
---|
1379 | { |
---|
1380 | return enabled ? TR_AUTO_SWITCH_ON : TR_AUTO_SWITCH_OFF; |
---|
1381 | } |
---|
1382 | |
---|
1383 | static void |
---|
1384 | turtleCheckClock (tr_session * s, struct tr_turtle_info * t) |
---|
1385 | { |
---|
1386 | bool enabled; |
---|
1387 | bool alreadySwitched; |
---|
1388 | tr_auto_switch_state_t newAutoTurtleState; |
---|
1389 | |
---|
1390 | assert (t->isClockEnabled); |
---|
1391 | |
---|
1392 | enabled = getInTurtleTime (t); |
---|
1393 | newAutoTurtleState = autoSwitchState (enabled); |
---|
1394 | alreadySwitched = (t->autoTurtleState == newAutoTurtleState); |
---|
1395 | |
---|
1396 | if (!alreadySwitched) |
---|
1397 | { |
---|
1398 | tr_logAddInfo ("Time to turn %s turtle mode!", (enabled?"on":"off")); |
---|
1399 | t->autoTurtleState = newAutoTurtleState; |
---|
1400 | useAltSpeed (s, t, enabled, false); |
---|
1401 | } |
---|
1402 | } |
---|
1403 | |
---|
1404 | /* Called after the turtle's fields are loaded from an outside source. |
---|
1405 | * It initializes the implementation fields |
---|
1406 | * and turns on turtle mode if the clock settings say to. */ |
---|
1407 | static void |
---|
1408 | turtleBootstrap (tr_session * session, struct tr_turtle_info * turtle) |
---|
1409 | { |
---|
1410 | turtle->changedByUser = false; |
---|
1411 | turtle->autoTurtleState = TR_AUTO_SWITCH_UNUSED; |
---|
1412 | |
---|
1413 | tr_bitfieldConstruct (&turtle->minutes, MINUTES_PER_WEEK); |
---|
1414 | |
---|
1415 | turtleUpdateTable (turtle); |
---|
1416 | |
---|
1417 | if (turtle->isClockEnabled) |
---|
1418 | { |
---|
1419 | turtle->isEnabled = getInTurtleTime (turtle); |
---|
1420 | turtle->autoTurtleState = autoSwitchState (turtle->isEnabled); |
---|
1421 | } |
---|
1422 | |
---|
1423 | altSpeedToggled (session); |
---|
1424 | |
---|
1425 | } |
---|
1426 | |
---|
1427 | /*** |
---|
1428 | **** Primary session speed limits |
---|
1429 | ***/ |
---|
1430 | |
---|
1431 | void |
---|
1432 | tr_sessionSetSpeedLimit_Bps (tr_session * s, tr_direction d, unsigned int Bps) |
---|
1433 | { |
---|
1434 | assert (tr_isSession (s)); |
---|
1435 | assert (tr_isDirection (d)); |
---|
1436 | |
---|
1437 | s->speedLimit_Bps[d] = Bps; |
---|
1438 | |
---|
1439 | updateBandwidth (s, d); |
---|
1440 | } |
---|
1441 | void |
---|
1442 | tr_sessionSetSpeedLimit_KBps (tr_session * s, tr_direction d, unsigned int KBps) |
---|
1443 | { |
---|
1444 | tr_sessionSetSpeedLimit_Bps (s, d, toSpeedBytes (KBps)); |
---|
1445 | } |
---|
1446 | |
---|
1447 | unsigned int |
---|
1448 | tr_sessionGetSpeedLimit_Bps (const tr_session * s, tr_direction d) |
---|
1449 | { |
---|
1450 | assert (tr_isSession (s)); |
---|
1451 | assert (tr_isDirection (d)); |
---|
1452 | |
---|
1453 | return s->speedLimit_Bps[d]; |
---|
1454 | } |
---|
1455 | unsigned int |
---|
1456 | tr_sessionGetSpeedLimit_KBps (const tr_session * s, tr_direction d) |
---|
1457 | { |
---|
1458 | return toSpeedKBps (tr_sessionGetSpeedLimit_Bps (s, d)); |
---|
1459 | } |
---|
1460 | |
---|
1461 | void |
---|
1462 | tr_sessionLimitSpeed (tr_session * s, tr_direction d, bool b) |
---|
1463 | { |
---|
1464 | assert (tr_isSession (s)); |
---|
1465 | assert (tr_isDirection (d)); |
---|
1466 | assert (tr_isBool (b)); |
---|
1467 | |
---|
1468 | s->speedLimitEnabled[d] = b; |
---|
1469 | |
---|
1470 | updateBandwidth (s, d); |
---|
1471 | } |
---|
1472 | |
---|
1473 | bool |
---|
1474 | tr_sessionIsSpeedLimited (const tr_session * s, tr_direction d) |
---|
1475 | { |
---|
1476 | assert (tr_isSession (s)); |
---|
1477 | assert (tr_isDirection (d)); |
---|
1478 | |
---|
1479 | return s->speedLimitEnabled[d]; |
---|
1480 | } |
---|
1481 | |
---|
1482 | /*** |
---|
1483 | **** Alternative speed limits that are used during scheduled times |
---|
1484 | ***/ |
---|
1485 | |
---|
1486 | void |
---|
1487 | tr_sessionSetAltSpeed_Bps (tr_session * s, tr_direction d, unsigned int Bps) |
---|
1488 | { |
---|
1489 | assert (tr_isSession (s)); |
---|
1490 | assert (tr_isDirection (d)); |
---|
1491 | |
---|
1492 | s->turtle.speedLimit_Bps[d] = Bps; |
---|
1493 | |
---|
1494 | updateBandwidth (s, d); |
---|
1495 | } |
---|
1496 | |
---|
1497 | void |
---|
1498 | tr_sessionSetAltSpeed_KBps (tr_session * s, tr_direction d, unsigned int KBps) |
---|
1499 | { |
---|
1500 | tr_sessionSetAltSpeed_Bps (s, d, toSpeedBytes (KBps)); |
---|
1501 | } |
---|
1502 | |
---|
1503 | unsigned int |
---|
1504 | tr_sessionGetAltSpeed_Bps (const tr_session * s, tr_direction d) |
---|
1505 | { |
---|
1506 | assert (tr_isSession (s)); |
---|
1507 | assert (tr_isDirection (d)); |
---|
1508 | |
---|
1509 | return s->turtle.speedLimit_Bps[d]; |
---|
1510 | } |
---|
1511 | unsigned int |
---|
1512 | tr_sessionGetAltSpeed_KBps (const tr_session * s, tr_direction d) |
---|
1513 | { |
---|
1514 | return toSpeedKBps (tr_sessionGetAltSpeed_Bps (s, d)); |
---|
1515 | } |
---|
1516 | |
---|
1517 | static void |
---|
1518 | userPokedTheClock (tr_session * s, struct tr_turtle_info * t) |
---|
1519 | { |
---|
1520 | tr_logAddDebug ("Refreshing the turtle mode clock due to user changes"); |
---|
1521 | |
---|
1522 | t->autoTurtleState = TR_AUTO_SWITCH_UNUSED; |
---|
1523 | |
---|
1524 | turtleUpdateTable (t); |
---|
1525 | |
---|
1526 | if (t->isClockEnabled) |
---|
1527 | { |
---|
1528 | const bool enabled = getInTurtleTime (t); |
---|
1529 | useAltSpeed (s, t, enabled, true); |
---|
1530 | t->autoTurtleState = autoSwitchState (enabled); |
---|
1531 | } |
---|
1532 | } |
---|
1533 | |
---|
1534 | void |
---|
1535 | tr_sessionUseAltSpeedTime (tr_session * s, bool b) |
---|
1536 | { |
---|
1537 | struct tr_turtle_info * t = &s->turtle; |
---|
1538 | |
---|
1539 | assert (tr_isSession (s)); |
---|
1540 | assert (tr_isBool (b)); |
---|
1541 | |
---|
1542 | if (t->isClockEnabled != b) |
---|
1543 | { |
---|
1544 | t->isClockEnabled = b; |
---|
1545 | userPokedTheClock (s, t); |
---|
1546 | } |
---|
1547 | } |
---|
1548 | |
---|
1549 | bool |
---|
1550 | tr_sessionUsesAltSpeedTime (const tr_session * s) |
---|
1551 | { |
---|
1552 | assert (tr_isSession (s)); |
---|
1553 | |
---|
1554 | return s->turtle.isClockEnabled; |
---|
1555 | } |
---|
1556 | |
---|
1557 | void |
---|
1558 | tr_sessionSetAltSpeedBegin (tr_session * s, int minute) |
---|
1559 | { |
---|
1560 | assert (tr_isSession (s)); |
---|
1561 | assert (0<=minute && minute< (60*24)); |
---|
1562 | |
---|
1563 | if (s->turtle.beginMinute != minute) |
---|
1564 | { |
---|
1565 | s->turtle.beginMinute = minute; |
---|
1566 | userPokedTheClock (s, &s->turtle); |
---|
1567 | } |
---|
1568 | } |
---|
1569 | |
---|
1570 | int |
---|
1571 | tr_sessionGetAltSpeedBegin (const tr_session * s) |
---|
1572 | { |
---|
1573 | assert (tr_isSession (s)); |
---|
1574 | |
---|
1575 | return s->turtle.beginMinute; |
---|
1576 | } |
---|
1577 | |
---|
1578 | void |
---|
1579 | tr_sessionSetAltSpeedEnd (tr_session * s, int minute) |
---|
1580 | { |
---|
1581 | assert (tr_isSession (s)); |
---|
1582 | assert (0<=minute && minute< (60*24)); |
---|
1583 | |
---|
1584 | if (s->turtle.endMinute != minute) |
---|
1585 | { |
---|
1586 | s->turtle.endMinute = minute; |
---|
1587 | userPokedTheClock (s, &s->turtle); |
---|
1588 | } |
---|
1589 | } |
---|
1590 | |
---|
1591 | int |
---|
1592 | tr_sessionGetAltSpeedEnd (const tr_session * s) |
---|
1593 | { |
---|
1594 | assert (tr_isSession (s)); |
---|
1595 | |
---|
1596 | return s->turtle.endMinute; |
---|
1597 | } |
---|
1598 | |
---|
1599 | void |
---|
1600 | tr_sessionSetAltSpeedDay (tr_session * s, tr_sched_day days) |
---|
1601 | { |
---|
1602 | assert (tr_isSession (s)); |
---|
1603 | |
---|
1604 | if (s->turtle.days != days) |
---|
1605 | { |
---|
1606 | s->turtle.days = days; |
---|
1607 | userPokedTheClock (s, &s->turtle); |
---|
1608 | } |
---|
1609 | } |
---|
1610 | |
---|
1611 | tr_sched_day |
---|
1612 | tr_sessionGetAltSpeedDay (const tr_session * s) |
---|
1613 | { |
---|
1614 | assert (tr_isSession (s)); |
---|
1615 | |
---|
1616 | return s->turtle.days; |
---|
1617 | } |
---|
1618 | |
---|
1619 | void |
---|
1620 | tr_sessionUseAltSpeed (tr_session * session, bool enabled) |
---|
1621 | { |
---|
1622 | useAltSpeed (session, &session->turtle, enabled, true); |
---|
1623 | } |
---|
1624 | |
---|
1625 | bool |
---|
1626 | tr_sessionUsesAltSpeed (const tr_session * s) |
---|
1627 | { |
---|
1628 | assert (tr_isSession (s)); |
---|
1629 | |
---|
1630 | return s->turtle.isEnabled; |
---|
1631 | } |
---|
1632 | |
---|
1633 | void |
---|
1634 | tr_sessionSetAltSpeedFunc (tr_session * session, |
---|
1635 | tr_altSpeedFunc func, |
---|
1636 | void * userData) |
---|
1637 | { |
---|
1638 | assert (tr_isSession (session)); |
---|
1639 | |
---|
1640 | session->turtle.callback = func; |
---|
1641 | session->turtle.callbackUserData = userData; |
---|
1642 | } |
---|
1643 | |
---|
1644 | void |
---|
1645 | tr_sessionClearAltSpeedFunc (tr_session * session) |
---|
1646 | { |
---|
1647 | tr_sessionSetAltSpeedFunc (session, NULL, NULL); |
---|
1648 | } |
---|
1649 | |
---|
1650 | /*** |
---|
1651 | **** |
---|
1652 | ***/ |
---|
1653 | |
---|
1654 | void |
---|
1655 | tr_sessionSetPeerLimit (tr_session * session, uint16_t n) |
---|
1656 | { |
---|
1657 | assert (tr_isSession (session)); |
---|
1658 | |
---|
1659 | session->peerLimit = n; |
---|
1660 | } |
---|
1661 | |
---|
1662 | uint16_t |
---|
1663 | tr_sessionGetPeerLimit (const tr_session * session) |
---|
1664 | { |
---|
1665 | assert (tr_isSession (session)); |
---|
1666 | |
---|
1667 | return session->peerLimit; |
---|
1668 | } |
---|
1669 | |
---|
1670 | void |
---|
1671 | tr_sessionSetPeerLimitPerTorrent (tr_session * session, uint16_t n) |
---|
1672 | { |
---|
1673 | assert (tr_isSession (session)); |
---|
1674 | |
---|
1675 | session->peerLimitPerTorrent = n; |
---|
1676 | } |
---|
1677 | |
---|
1678 | uint16_t |
---|
1679 | tr_sessionGetPeerLimitPerTorrent (const tr_session * session) |
---|
1680 | { |
---|
1681 | assert (tr_isSession (session)); |
---|
1682 | |
---|
1683 | return session->peerLimitPerTorrent; |
---|
1684 | } |
---|
1685 | |
---|
1686 | /*** |
---|
1687 | **** |
---|
1688 | ***/ |
---|
1689 | |
---|
1690 | void |
---|
1691 | tr_sessionSetPaused (tr_session * session, bool isPaused) |
---|
1692 | { |
---|
1693 | assert (tr_isSession (session)); |
---|
1694 | |
---|
1695 | session->pauseAddedTorrent = isPaused; |
---|
1696 | } |
---|
1697 | |
---|
1698 | bool |
---|
1699 | tr_sessionGetPaused (const tr_session * session) |
---|
1700 | { |
---|
1701 | assert (tr_isSession (session)); |
---|
1702 | |
---|
1703 | return session->pauseAddedTorrent; |
---|
1704 | } |
---|
1705 | |
---|
1706 | void |
---|
1707 | tr_sessionSetDeleteSource (tr_session * session, bool deleteSource) |
---|
1708 | { |
---|
1709 | assert (tr_isSession (session)); |
---|
1710 | |
---|
1711 | session->deleteSourceTorrent = deleteSource; |
---|
1712 | } |
---|
1713 | |
---|
1714 | bool |
---|
1715 | tr_sessionGetDeleteSource (const tr_session * session) |
---|
1716 | { |
---|
1717 | assert (tr_isSession (session)); |
---|
1718 | |
---|
1719 | return session->deleteSourceTorrent; |
---|
1720 | } |
---|
1721 | |
---|
1722 | /*** |
---|
1723 | **** |
---|
1724 | ***/ |
---|
1725 | |
---|
1726 | unsigned int |
---|
1727 | tr_sessionGetPieceSpeed_Bps (const tr_session * session, tr_direction dir) |
---|
1728 | { |
---|
1729 | return tr_isSession (session) ? tr_bandwidthGetPieceSpeed_Bps (&session->bandwidth, 0, dir) : 0; |
---|
1730 | } |
---|
1731 | |
---|
1732 | unsigned int |
---|
1733 | tr_sessionGetRawSpeed_Bps (const tr_session * session, tr_direction dir) |
---|
1734 | { |
---|
1735 | return tr_isSession (session) ? tr_bandwidthGetRawSpeed_Bps (&session->bandwidth, 0, dir) : 0; |
---|
1736 | } |
---|
1737 | double |
---|
1738 | tr_sessionGetRawSpeed_KBps (const tr_session * session, tr_direction dir) |
---|
1739 | { |
---|
1740 | return toSpeedKBps (tr_sessionGetRawSpeed_Bps (session, dir)); |
---|
1741 | } |
---|
1742 | |
---|
1743 | |
---|
1744 | int |
---|
1745 | tr_sessionCountTorrents (const tr_session * session) |
---|
1746 | { |
---|
1747 | return tr_isSession (session) ? session->torrentCount : 0; |
---|
1748 | } |
---|
1749 | |
---|
1750 | tr_torrent ** |
---|
1751 | tr_sessionGetTorrents (tr_session * session, int * setme_n) |
---|
1752 | { |
---|
1753 | int i; |
---|
1754 | int n; |
---|
1755 | tr_torrent ** torrents; |
---|
1756 | tr_torrent * tor; |
---|
1757 | |
---|
1758 | assert (tr_isSession (session)); |
---|
1759 | assert (setme_n != NULL); |
---|
1760 | |
---|
1761 | n = tr_sessionCountTorrents (session); |
---|
1762 | *setme_n = n; |
---|
1763 | |
---|
1764 | torrents = tr_new (tr_torrent *, n); |
---|
1765 | tor = NULL; |
---|
1766 | for (i=0; i<n; ++i) |
---|
1767 | torrents[i] = tor = tr_torrentNext (session, tor); |
---|
1768 | |
---|
1769 | return torrents; |
---|
1770 | } |
---|
1771 | |
---|
1772 | static int |
---|
1773 | compareTorrentByCur (const void * va, const void * vb) |
---|
1774 | { |
---|
1775 | const tr_torrent * a = * (const tr_torrent**)va; |
---|
1776 | const tr_torrent * b = * (const tr_torrent**)vb; |
---|
1777 | const uint64_t aCur = a->downloadedCur + a->uploadedCur; |
---|
1778 | const uint64_t bCur = b->downloadedCur + b->uploadedCur; |
---|
1779 | |
---|
1780 | if (aCur != bCur) |
---|
1781 | return aCur > bCur ? -1 : 1; /* close the biggest torrents first */ |
---|
1782 | |
---|
1783 | return 0; |
---|
1784 | } |
---|
1785 | |
---|
1786 | static void closeBlocklists (tr_session *); |
---|
1787 | |
---|
1788 | static void |
---|
1789 | sessionCloseImpl (void * vsession) |
---|
1790 | { |
---|
1791 | int i, n; |
---|
1792 | tr_torrent ** torrents; |
---|
1793 | tr_session * session = vsession; |
---|
1794 | |
---|
1795 | assert (tr_isSession (session)); |
---|
1796 | |
---|
1797 | session->isClosing = true; |
---|
1798 | |
---|
1799 | free_incoming_peer_port (session); |
---|
1800 | |
---|
1801 | if (session->isLPDEnabled) |
---|
1802 | tr_lpdUninit (session); |
---|
1803 | |
---|
1804 | tr_utpClose (session); |
---|
1805 | tr_dhtUninit (session); |
---|
1806 | |
---|
1807 | event_free (session->saveTimer); |
---|
1808 | session->saveTimer = NULL; |
---|
1809 | |
---|
1810 | event_free (session->nowTimer); |
---|
1811 | session->nowTimer = NULL; |
---|
1812 | |
---|
1813 | tr_verifyClose (session); |
---|
1814 | tr_sharedClose (session); |
---|
1815 | tr_rpcClose (&session->rpcServer); |
---|
1816 | |
---|
1817 | /* Close the torrents. Get the most active ones first so that |
---|
1818 | * if we can't get them all closed in a reasonable amount of time, |
---|
1819 | * at least we get the most important ones first. */ |
---|
1820 | torrents = tr_sessionGetTorrents (session, &n); |
---|
1821 | qsort (torrents, n, sizeof (tr_torrent*), compareTorrentByCur); |
---|
1822 | for (i=0; i<n; ++i) |
---|
1823 | tr_torrentFree (torrents[i]); |
---|
1824 | tr_free (torrents); |
---|
1825 | |
---|
1826 | /* Close the announcer *after* closing the torrents |
---|
1827 | so that all the &event=stopped messages will be |
---|
1828 | queued to be sent by tr_announcerClose () */ |
---|
1829 | tr_announcerClose (session); |
---|
1830 | |
---|
1831 | /* and this goes *after* announcer close so that |
---|
1832 | it won't be idle until the announce events are sent... */ |
---|
1833 | tr_webClose (session, TR_WEB_CLOSE_WHEN_IDLE); |
---|
1834 | |
---|
1835 | tr_cacheFree (session->cache); |
---|
1836 | session->cache = NULL; |
---|
1837 | |
---|
1838 | /* gotta keep udp running long enough to send out all |
---|
1839 | the &event=stopped UDP tracker messages */ |
---|
1840 | while (!tr_tracker_udp_is_idle (session)) |
---|
1841 | { |
---|
1842 | tr_tracker_udp_upkeep (session); |
---|
1843 | tr_wait_msec (100); |
---|
1844 | } |
---|
1845 | |
---|
1846 | /* we had to wait until UDP trackers were closed before closing these: */ |
---|
1847 | evdns_base_free (session->evdns_base, 0); |
---|
1848 | session->evdns_base = NULL; |
---|
1849 | tr_tracker_udp_close (session); |
---|
1850 | tr_udpUninit (session); |
---|
1851 | |
---|
1852 | tr_statsClose (session); |
---|
1853 | tr_peerMgrFree (session->peerMgr); |
---|
1854 | |
---|
1855 | closeBlocklists (session); |
---|
1856 | |
---|
1857 | tr_fdClose (session); |
---|
1858 | |
---|
1859 | session->isClosed = true; |
---|
1860 | } |
---|
1861 | |
---|
1862 | static int |
---|
1863 | deadlineReached (const time_t deadline) |
---|
1864 | { |
---|
1865 | return time (NULL) >= deadline; |
---|
1866 | } |
---|
1867 | |
---|
1868 | #define SHUTDOWN_MAX_SECONDS 20 |
---|
1869 | |
---|
1870 | void |
---|
1871 | tr_sessionClose (tr_session * session) |
---|
1872 | { |
---|
1873 | const time_t deadline = time (NULL) + SHUTDOWN_MAX_SECONDS; |
---|
1874 | |
---|
1875 | assert (tr_isSession (session)); |
---|
1876 | |
---|
1877 | dbgmsg ("shutting down transmission session %p... now is %"TR_PRIuSIZE", deadline is %"TR_PRIuSIZE, (void*)session, (size_t)time (NULL), (size_t)deadline); |
---|
1878 | |
---|
1879 | /* close the session */ |
---|
1880 | tr_runInEventThread (session, sessionCloseImpl, session); |
---|
1881 | while (!session->isClosed && !deadlineReached (deadline)) |
---|
1882 | { |
---|
1883 | dbgmsg ("waiting for the libtransmission thread to finish"); |
---|
1884 | tr_wait_msec (100); |
---|
1885 | } |
---|
1886 | |
---|
1887 | /* "shared" and "tracker" have live sockets, |
---|
1888 | * so we need to keep the transmission thread alive |
---|
1889 | * for a bit while they tell the router & tracker |
---|
1890 | * that we're closing now */ |
---|
1891 | while ((session->shared || session->web || session->announcer || session->announcer_udp) |
---|
1892 | && !deadlineReached (deadline)) |
---|
1893 | { |
---|
1894 | dbgmsg ("waiting on port unmap (%p) or announcer (%p)... now %"TR_PRIuSIZE" deadline %"TR_PRIuSIZE, |
---|
1895 | (void*)session->shared, (void*)session->announcer, (size_t)time (NULL), (size_t)deadline); |
---|
1896 | tr_wait_msec (50); |
---|
1897 | } |
---|
1898 | |
---|
1899 | tr_webClose (session, TR_WEB_CLOSE_NOW); |
---|
1900 | |
---|
1901 | /* close the libtransmission thread */ |
---|
1902 | tr_eventClose (session); |
---|
1903 | while (session->events != NULL) |
---|
1904 | { |
---|
1905 | static bool forced = false; |
---|
1906 | dbgmsg ("waiting for libtransmission thread to finish... now %"TR_PRIuSIZE" deadline %"TR_PRIuSIZE, (size_t)time (NULL), (size_t)deadline); |
---|
1907 | tr_wait_msec (100); |
---|
1908 | |
---|
1909 | if (deadlineReached (deadline) && !forced) |
---|
1910 | { |
---|
1911 | dbgmsg ("calling event_loopbreak ()"); |
---|
1912 | forced = true; |
---|
1913 | event_base_loopbreak (session->event_base); |
---|
1914 | } |
---|
1915 | |
---|
1916 | if (deadlineReached (deadline+3)) |
---|
1917 | { |
---|
1918 | dbgmsg ("deadline+3 reached... calling break...\n"); |
---|
1919 | break; |
---|
1920 | } |
---|
1921 | } |
---|
1922 | |
---|
1923 | /* free the session memory */ |
---|
1924 | tr_variantFree (&session->removedTorrents); |
---|
1925 | tr_bandwidthDestruct (&session->bandwidth); |
---|
1926 | tr_bitfieldDestruct (&session->turtle.minutes); |
---|
1927 | tr_lockFree (session->lock); |
---|
1928 | if (session->metainfoLookup) |
---|
1929 | { |
---|
1930 | tr_variantFree (session->metainfoLookup); |
---|
1931 | tr_free (session->metainfoLookup); |
---|
1932 | } |
---|
1933 | tr_device_info_free (session->downloadDir); |
---|
1934 | tr_free (session->torrentDoneScript); |
---|
1935 | tr_free (session->tag); |
---|
1936 | tr_free (session->configDir); |
---|
1937 | tr_free (session->resumeDir); |
---|
1938 | tr_free (session->torrentDir); |
---|
1939 | tr_free (session->incompleteDir); |
---|
1940 | tr_free (session->blocklist_url); |
---|
1941 | tr_free (session->peer_congestion_algorithm); |
---|
1942 | tr_free (session); |
---|
1943 | } |
---|
1944 | |
---|
1945 | struct sessionLoadTorrentsData |
---|
1946 | { |
---|
1947 | tr_session * session; |
---|
1948 | tr_ctor * ctor; |
---|
1949 | int * setmeCount; |
---|
1950 | tr_torrent ** torrents; |
---|
1951 | bool done; |
---|
1952 | }; |
---|
1953 | |
---|
1954 | static void |
---|
1955 | sessionLoadTorrents (void * vdata) |
---|
1956 | { |
---|
1957 | int i; |
---|
1958 | int n = 0; |
---|
1959 | tr_sys_path_info info; |
---|
1960 | tr_sys_dir_t odir = NULL; |
---|
1961 | tr_list * l = NULL; |
---|
1962 | tr_list * list = NULL; |
---|
1963 | struct sessionLoadTorrentsData * data = vdata; |
---|
1964 | const char * dirname = tr_getTorrentDir (data->session); |
---|
1965 | |
---|
1966 | assert (tr_isSession (data->session)); |
---|
1967 | |
---|
1968 | tr_ctorSetSave (data->ctor, false); /* since we already have them */ |
---|
1969 | |
---|
1970 | if (tr_sys_path_get_info (dirname, 0, &info, NULL) && |
---|
1971 | info.type == TR_SYS_PATH_IS_DIRECTORY && |
---|
1972 | (odir = tr_sys_dir_open (dirname, NULL)) != TR_BAD_SYS_DIR) |
---|
1973 | { |
---|
1974 | const char * name; |
---|
1975 | while ((name = tr_sys_dir_read_name (odir, NULL)) != NULL) |
---|
1976 | { |
---|
1977 | if (tr_str_has_suffix (name, ".torrent")) |
---|
1978 | { |
---|
1979 | tr_torrent * tor; |
---|
1980 | char * path = tr_buildPath (dirname, name, NULL); |
---|
1981 | tr_ctorSetMetainfoFromFile (data->ctor, path); |
---|
1982 | if ((tor = tr_torrentNew (data->ctor, NULL, NULL))) |
---|
1983 | { |
---|
1984 | tr_list_prepend (&list, tor); |
---|
1985 | ++n; |
---|
1986 | } |
---|
1987 | tr_free (path); |
---|
1988 | } |
---|
1989 | } |
---|
1990 | tr_sys_dir_close (odir, NULL); |
---|
1991 | } |
---|
1992 | |
---|
1993 | data->torrents = tr_new (tr_torrent *, n); |
---|
1994 | for (i=0, l=list; l!=NULL; l=l->next) |
---|
1995 | data->torrents[i++] = (tr_torrent*) l->data; |
---|
1996 | assert (i == n); |
---|
1997 | |
---|
1998 | tr_list_free (&list, NULL); |
---|
1999 | |
---|
2000 | if (n) |
---|
2001 | tr_logAddInfo (_("Loaded %d torrents"), n); |
---|
2002 | |
---|
2003 | if (data->setmeCount) |
---|
2004 | *data->setmeCount = n; |
---|
2005 | |
---|
2006 | data->done = true; |
---|
2007 | } |
---|
2008 | |
---|
2009 | tr_torrent ** |
---|
2010 | tr_sessionLoadTorrents (tr_session * session, |
---|
2011 | tr_ctor * ctor, |
---|
2012 | int * setmeCount) |
---|
2013 | { |
---|
2014 | struct sessionLoadTorrentsData data; |
---|
2015 | |
---|
2016 | data.session = session; |
---|
2017 | data.ctor = ctor; |
---|
2018 | data.setmeCount = setmeCount; |
---|
2019 | data.torrents = NULL; |
---|
2020 | data.done = false; |
---|
2021 | |
---|
2022 | tr_runInEventThread (session, sessionLoadTorrents, &data); |
---|
2023 | while (!data.done) |
---|
2024 | tr_wait_msec (100); |
---|
2025 | |
---|
2026 | return data.torrents; |
---|
2027 | } |
---|
2028 | |
---|
2029 | /*** |
---|
2030 | **** |
---|
2031 | ***/ |
---|
2032 | |
---|
2033 | void |
---|
2034 | tr_sessionSetPexEnabled (tr_session * session, bool enabled) |
---|
2035 | { |
---|
2036 | assert (tr_isSession (session)); |
---|
2037 | assert (tr_isBool (enabled)); |
---|
2038 | |
---|
2039 | session->isPexEnabled = enabled; |
---|
2040 | } |
---|
2041 | |
---|
2042 | bool |
---|
2043 | tr_sessionIsPexEnabled (const tr_session * session) |
---|
2044 | { |
---|
2045 | assert (tr_isSession (session)); |
---|
2046 | |
---|
2047 | return session->isPexEnabled; |
---|
2048 | } |
---|
2049 | |
---|
2050 | bool |
---|
2051 | tr_sessionAllowsDHT (const tr_session * session) |
---|
2052 | { |
---|
2053 | return tr_sessionIsDHTEnabled (session); |
---|
2054 | } |
---|
2055 | |
---|
2056 | bool |
---|
2057 | tr_sessionIsDHTEnabled (const tr_session * session) |
---|
2058 | { |
---|
2059 | assert (tr_isSession (session)); |
---|
2060 | |
---|
2061 | return session->isDHTEnabled; |
---|
2062 | } |
---|
2063 | |
---|
2064 | static void |
---|
2065 | toggleDHTImpl (void * data) |
---|
2066 | { |
---|
2067 | tr_session * session = data; |
---|
2068 | assert (tr_isSession (session)); |
---|
2069 | |
---|
2070 | tr_udpUninit (session); |
---|
2071 | session->isDHTEnabled = !session->isDHTEnabled; |
---|
2072 | tr_udpInit (session); |
---|
2073 | } |
---|
2074 | |
---|
2075 | void |
---|
2076 | tr_sessionSetDHTEnabled (tr_session * session, bool enabled) |
---|
2077 | { |
---|
2078 | assert (tr_isSession (session)); |
---|
2079 | assert (tr_isBool (enabled)); |
---|
2080 | |
---|
2081 | if (enabled != session->isDHTEnabled) |
---|
2082 | tr_runInEventThread (session, toggleDHTImpl, session); |
---|
2083 | } |
---|
2084 | |
---|
2085 | /*** |
---|
2086 | **** |
---|
2087 | ***/ |
---|
2088 | |
---|
2089 | bool |
---|
2090 | tr_sessionIsUTPEnabled (const tr_session * session) |
---|
2091 | { |
---|
2092 | assert (tr_isSession (session)); |
---|
2093 | |
---|
2094 | #ifdef WITH_UTP |
---|
2095 | return session->isUTPEnabled; |
---|
2096 | #else |
---|
2097 | return false; |
---|
2098 | #endif |
---|
2099 | } |
---|
2100 | |
---|
2101 | static void |
---|
2102 | toggle_utp (void * data) |
---|
2103 | { |
---|
2104 | tr_session * session = data; |
---|
2105 | |
---|
2106 | assert (tr_isSession (session)); |
---|
2107 | |
---|
2108 | session->isUTPEnabled = !session->isUTPEnabled; |
---|
2109 | |
---|
2110 | tr_udpSetSocketBuffers (session); |
---|
2111 | |
---|
2112 | /* But don't call tr_utpClose -- see reset_timer in tr-utp.c for an |
---|
2113 | explanation. */ |
---|
2114 | } |
---|
2115 | |
---|
2116 | void |
---|
2117 | tr_sessionSetUTPEnabled (tr_session * session, bool enabled) |
---|
2118 | { |
---|
2119 | assert (tr_isSession (session)); |
---|
2120 | assert (tr_isBool (enabled)); |
---|
2121 | |
---|
2122 | if (enabled != session->isUTPEnabled) |
---|
2123 | tr_runInEventThread (session, toggle_utp, session); |
---|
2124 | } |
---|
2125 | |
---|
2126 | /*** |
---|
2127 | **** |
---|
2128 | ***/ |
---|
2129 | |
---|
2130 | static void |
---|
2131 | toggleLPDImpl (void * data) |
---|
2132 | { |
---|
2133 | tr_session * session = data; |
---|
2134 | assert (tr_isSession (session)); |
---|
2135 | |
---|
2136 | if (session->isLPDEnabled) |
---|
2137 | tr_lpdUninit (session); |
---|
2138 | |
---|
2139 | session->isLPDEnabled = !session->isLPDEnabled; |
---|
2140 | |
---|
2141 | if (session->isLPDEnabled) |
---|
2142 | tr_lpdInit (session, &session->public_ipv4->addr); |
---|
2143 | } |
---|
2144 | |
---|
2145 | void |
---|
2146 | tr_sessionSetLPDEnabled (tr_session * session, bool enabled) |
---|
2147 | { |
---|
2148 | assert (tr_isSession (session)); |
---|
2149 | assert (tr_isBool (enabled)); |
---|
2150 | |
---|
2151 | if (enabled != session->isLPDEnabled) |
---|
2152 | tr_runInEventThread (session, toggleLPDImpl, session); |
---|
2153 | } |
---|
2154 | |
---|
2155 | bool |
---|
2156 | tr_sessionIsLPDEnabled (const tr_session * session) |
---|
2157 | { |
---|
2158 | assert (tr_isSession (session)); |
---|
2159 | |
---|
2160 | return session->isLPDEnabled; |
---|
2161 | } |
---|
2162 | |
---|
2163 | bool |
---|
2164 | tr_sessionAllowsLPD (const tr_session * session) |
---|
2165 | { |
---|
2166 | return tr_sessionIsLPDEnabled (session); |
---|
2167 | } |
---|
2168 | |
---|
2169 | /*** |
---|
2170 | **** |
---|
2171 | ***/ |
---|
2172 | |
---|
2173 | void |
---|
2174 | tr_sessionSetCacheLimit_MB (tr_session * session, int max_bytes) |
---|
2175 | { |
---|
2176 | assert (tr_isSession (session)); |
---|
2177 | |
---|
2178 | tr_cacheSetLimit (session->cache, toMemBytes (max_bytes)); |
---|
2179 | } |
---|
2180 | |
---|
2181 | int |
---|
2182 | tr_sessionGetCacheLimit_MB (const tr_session * session) |
---|
2183 | { |
---|
2184 | assert (tr_isSession (session)); |
---|
2185 | |
---|
2186 | return toMemMB (tr_cacheGetLimit (session->cache)); |
---|
2187 | } |
---|
2188 | |
---|
2189 | /*** |
---|
2190 | **** |
---|
2191 | ***/ |
---|
2192 | |
---|
2193 | struct port_forwarding_data |
---|
2194 | { |
---|
2195 | bool enabled; |
---|
2196 | struct tr_shared * shared; |
---|
2197 | }; |
---|
2198 | |
---|
2199 | static void |
---|
2200 | setPortForwardingEnabled (void * vdata) |
---|
2201 | { |
---|
2202 | struct port_forwarding_data * data = vdata; |
---|
2203 | tr_sharedTraversalEnable (data->shared, data->enabled); |
---|
2204 | tr_free (data); |
---|
2205 | } |
---|
2206 | |
---|
2207 | void |
---|
2208 | tr_sessionSetPortForwardingEnabled (tr_session * session, bool enabled) |
---|
2209 | { |
---|
2210 | struct port_forwarding_data * d; |
---|
2211 | d = tr_new0 (struct port_forwarding_data, 1); |
---|
2212 | d->shared = session->shared; |
---|
2213 | d->enabled = enabled; |
---|
2214 | tr_runInEventThread (session, setPortForwardingEnabled, d); |
---|
2215 | } |
---|
2216 | |
---|
2217 | bool |
---|
2218 | tr_sessionIsPortForwardingEnabled (const tr_session * session) |
---|
2219 | { |
---|
2220 | assert (tr_isSession (session)); |
---|
2221 | |
---|
2222 | return tr_sharedTraversalIsEnabled (session->shared); |
---|
2223 | } |
---|
2224 | |
---|
2225 | /*** |
---|
2226 | **** |
---|
2227 | ***/ |
---|
2228 | |
---|
2229 | static int |
---|
2230 | tr_stringEndsWith (const char * str, const char * end) |
---|
2231 | { |
---|
2232 | const size_t slen = strlen (str); |
---|
2233 | const size_t elen = strlen (end); |
---|
2234 | |
---|
2235 | return slen >= elen && !memcmp (&str[slen - elen], end, elen); |
---|
2236 | } |
---|
2237 | |
---|
2238 | static void |
---|
2239 | loadBlocklists (tr_session * session) |
---|
2240 | { |
---|
2241 | tr_sys_dir_t odir; |
---|
2242 | char * dirname; |
---|
2243 | const char * name; |
---|
2244 | tr_list * blocklists = NULL; |
---|
2245 | tr_ptrArray loadme = TR_PTR_ARRAY_INIT; |
---|
2246 | const bool isEnabled = session->isBlocklistEnabled; |
---|
2247 | |
---|
2248 | /* walk the blocklist directory... */ |
---|
2249 | dirname = tr_buildPath (session->configDir, "blocklists", NULL); |
---|
2250 | odir = tr_sys_dir_open (dirname, NULL); |
---|
2251 | if (odir == TR_BAD_SYS_DIR) |
---|
2252 | { |
---|
2253 | tr_free (dirname); |
---|
2254 | return; |
---|
2255 | } |
---|
2256 | |
---|
2257 | while ((name = tr_sys_dir_read_name (odir, NULL)) != NULL) |
---|
2258 | { |
---|
2259 | char * path; |
---|
2260 | char * load = NULL; |
---|
2261 | |
---|
2262 | if (name[0] == '.') /* ignore dotfiles */ |
---|
2263 | continue; |
---|
2264 | |
---|
2265 | path = tr_buildPath (dirname, name, NULL); |
---|
2266 | |
---|
2267 | if (tr_stringEndsWith (path, ".bin")) |
---|
2268 | { |
---|
2269 | load = tr_strdup (path); |
---|
2270 | } |
---|
2271 | else |
---|
2272 | { |
---|
2273 | char * binname; |
---|
2274 | char * basename; |
---|
2275 | tr_sys_path_info path_info; |
---|
2276 | tr_sys_path_info binname_info; |
---|
2277 | |
---|
2278 | basename = tr_sys_path_basename (name, NULL); |
---|
2279 | binname = tr_strdup_printf ("%s" TR_PATH_DELIMITER_STR "%s.bin", dirname, basename); |
---|
2280 | |
---|
2281 | if (!tr_sys_path_get_info (binname, 0, &binname_info, NULL)) /* create it */ |
---|
2282 | { |
---|
2283 | tr_blocklistFile * b = tr_blocklistFileNew (binname, isEnabled); |
---|
2284 | const int n = tr_blocklistFileSetContent (b, path); |
---|
2285 | if (n > 0) |
---|
2286 | load = tr_strdup (binname); |
---|
2287 | |
---|
2288 | tr_blocklistFileFree (b); |
---|
2289 | } |
---|
2290 | else if (tr_sys_path_get_info (path, 0, &path_info, NULL) && |
---|
2291 | path_info.last_modified_at >= binname_info.last_modified_at) /* update it */ |
---|
2292 | { |
---|
2293 | char * old; |
---|
2294 | tr_blocklistFile * b; |
---|
2295 | |
---|
2296 | old = tr_strdup_printf ("%s.old", binname); |
---|
2297 | tr_sys_path_remove (old, NULL); |
---|
2298 | tr_sys_path_rename (binname, old, NULL); |
---|
2299 | b = tr_blocklistFileNew (binname, isEnabled); |
---|
2300 | if (tr_blocklistFileSetContent (b, path) > 0) |
---|
2301 | { |
---|
2302 | tr_sys_path_remove (old, NULL); |
---|
2303 | } |
---|
2304 | else |
---|
2305 | { |
---|
2306 | tr_sys_path_remove (binname, NULL); |
---|
2307 | tr_sys_path_rename (old, binname, NULL); |
---|
2308 | } |
---|
2309 | |
---|
2310 | tr_blocklistFileFree (b); |
---|
2311 | tr_free (old); |
---|
2312 | } |
---|
2313 | |
---|
2314 | tr_free (basename); |
---|
2315 | tr_free (binname); |
---|
2316 | } |
---|
2317 | |
---|
2318 | if (load != NULL) |
---|
2319 | { |
---|
2320 | if (tr_ptrArrayFindSorted (&loadme, load, (PtrArrayCompareFunc)strcmp) == NULL) |
---|
2321 | tr_ptrArrayInsertSorted (&loadme, load, (PtrArrayCompareFunc)strcmp); |
---|
2322 | else |
---|
2323 | tr_free (load); |
---|
2324 | } |
---|
2325 | |
---|
2326 | tr_free (path); |
---|
2327 | } |
---|
2328 | |
---|
2329 | if (!tr_ptrArrayEmpty (&loadme)) |
---|
2330 | { |
---|
2331 | int i; |
---|
2332 | const int n = tr_ptrArraySize (&loadme); |
---|
2333 | const char ** paths = (const char **) tr_ptrArrayBase (&loadme); |
---|
2334 | |
---|
2335 | for (i=0; i<n; ++i) |
---|
2336 | tr_list_append (&blocklists, tr_blocklistFileNew (paths[i], isEnabled)); |
---|
2337 | } |
---|
2338 | |
---|
2339 | /* cleanup */ |
---|
2340 | tr_sys_dir_close (odir, NULL); |
---|
2341 | tr_free (dirname); |
---|
2342 | tr_ptrArrayDestruct (&loadme, (PtrArrayForeachFunc)tr_free); |
---|
2343 | session->blocklists = blocklists; |
---|
2344 | } |
---|
2345 | |
---|
2346 | static void |
---|
2347 | closeBlocklists (tr_session * session) |
---|
2348 | { |
---|
2349 | tr_list_free (&session->blocklists, (TrListForeachFunc)tr_blocklistFileFree); |
---|
2350 | } |
---|
2351 | |
---|
2352 | void |
---|
2353 | tr_sessionReloadBlocklists (tr_session * session) |
---|
2354 | { |
---|
2355 | closeBlocklists (session); |
---|
2356 | loadBlocklists (session); |
---|
2357 | |
---|
2358 | tr_peerMgrOnBlocklistChanged (session->peerMgr); |
---|
2359 | } |
---|
2360 | |
---|
2361 | int |
---|
2362 | tr_blocklistGetRuleCount (const tr_session * session) |
---|
2363 | { |
---|
2364 | tr_list * l; |
---|
2365 | int n = 0; |
---|
2366 | |
---|
2367 | assert (tr_isSession (session)); |
---|
2368 | |
---|
2369 | for (l = session->blocklists; l; l = l->next) |
---|
2370 | n += tr_blocklistFileGetRuleCount (l->data); |
---|
2371 | |
---|
2372 | return n; |
---|
2373 | } |
---|
2374 | |
---|
2375 | bool |
---|
2376 | tr_blocklistIsEnabled (const tr_session * session) |
---|
2377 | { |
---|
2378 | assert (tr_isSession (session)); |
---|
2379 | |
---|
2380 | return session->isBlocklistEnabled; |
---|
2381 | } |
---|
2382 | |
---|
2383 | void |
---|
2384 | tr_blocklistSetEnabled (tr_session * session, bool isEnabled) |
---|
2385 | { |
---|
2386 | tr_list * l; |
---|
2387 | |
---|
2388 | assert (tr_isSession (session)); |
---|
2389 | assert (tr_isBool (isEnabled)); |
---|
2390 | |
---|
2391 | session->isBlocklistEnabled = isEnabled; |
---|
2392 | |
---|
2393 | for (l=session->blocklists; l!=NULL; l=l->next) |
---|
2394 | tr_blocklistFileSetEnabled (l->data, isEnabled); |
---|
2395 | } |
---|
2396 | |
---|
2397 | bool |
---|
2398 | tr_blocklistExists (const tr_session * session) |
---|
2399 | { |
---|
2400 | assert (tr_isSession (session)); |
---|
2401 | |
---|
2402 | return session->blocklists != NULL; |
---|
2403 | } |
---|
2404 | |
---|
2405 | int |
---|
2406 | tr_blocklistSetContent (tr_session * session, const char * contentFilename) |
---|
2407 | { |
---|
2408 | tr_list * l; |
---|
2409 | int ruleCount; |
---|
2410 | tr_blocklistFile * b; |
---|
2411 | const char * defaultName = DEFAULT_BLOCKLIST_FILENAME; |
---|
2412 | tr_sessionLock (session); |
---|
2413 | |
---|
2414 | for (b=NULL, l=session->blocklists; !b && l; l=l->next) |
---|
2415 | if (tr_stringEndsWith (tr_blocklistFileGetFilename (l->data), defaultName)) |
---|
2416 | b = l->data; |
---|
2417 | |
---|
2418 | if (!b) |
---|
2419 | { |
---|
2420 | char * path = tr_buildPath (session->configDir, "blocklists", defaultName, NULL); |
---|
2421 | b = tr_blocklistFileNew (path, session->isBlocklistEnabled); |
---|
2422 | tr_list_append (&session->blocklists, b); |
---|
2423 | tr_free (path); |
---|
2424 | } |
---|
2425 | |
---|
2426 | ruleCount = tr_blocklistFileSetContent (b, contentFilename); |
---|
2427 | tr_sessionUnlock (session); |
---|
2428 | return ruleCount; |
---|
2429 | } |
---|
2430 | |
---|
2431 | bool |
---|
2432 | tr_sessionIsAddressBlocked (const tr_session * session, |
---|
2433 | const tr_address * addr) |
---|
2434 | { |
---|
2435 | tr_list * l; |
---|
2436 | |
---|
2437 | assert (tr_isSession (session)); |
---|
2438 | |
---|
2439 | for (l = session->blocklists; l; l = l->next) |
---|
2440 | if (tr_blocklistFileHasAddress (l->data, addr)) |
---|
2441 | return true; |
---|
2442 | |
---|
2443 | return false; |
---|
2444 | } |
---|
2445 | |
---|
2446 | void |
---|
2447 | tr_blocklistSetURL (tr_session * session, const char * url) |
---|
2448 | { |
---|
2449 | if (session->blocklist_url != url) |
---|
2450 | { |
---|
2451 | tr_free (session->blocklist_url); |
---|
2452 | session->blocklist_url = tr_strdup (url); |
---|
2453 | } |
---|
2454 | } |
---|
2455 | |
---|
2456 | const char * |
---|
2457 | tr_blocklistGetURL (const tr_session * session) |
---|
2458 | { |
---|
2459 | return session->blocklist_url; |
---|
2460 | } |
---|
2461 | |
---|
2462 | |
---|
2463 | /*** |
---|
2464 | **** |
---|
2465 | ***/ |
---|
2466 | |
---|
2467 | static void |
---|
2468 | metainfoLookupInit (tr_session * session) |
---|
2469 | { |
---|
2470 | tr_sys_path_info info; |
---|
2471 | const char * dirname = tr_getTorrentDir (session); |
---|
2472 | tr_sys_dir_t odir; |
---|
2473 | tr_ctor * ctor = NULL; |
---|
2474 | tr_variant * lookup; |
---|
2475 | int n = 0; |
---|
2476 | |
---|
2477 | assert (tr_isSession (session)); |
---|
2478 | |
---|
2479 | /* walk through the directory and find the mappings */ |
---|
2480 | lookup = tr_new0 (tr_variant, 1); |
---|
2481 | tr_variantInitDict (lookup, 0); |
---|
2482 | ctor = tr_ctorNew (session); |
---|
2483 | tr_ctorSetSave (ctor, false); /* since we already have them */ |
---|
2484 | if (tr_sys_path_get_info (dirname, 0, &info, NULL) && |
---|
2485 | info.type == TR_SYS_PATH_IS_DIRECTORY && |
---|
2486 | (odir = tr_sys_dir_open (dirname, NULL)) != TR_BAD_SYS_DIR) |
---|
2487 | { |
---|
2488 | const char * name; |
---|
2489 | while ((name = tr_sys_dir_read_name (odir, NULL)) != NULL) |
---|
2490 | { |
---|
2491 | if (tr_str_has_suffix (name, ".torrent")) |
---|
2492 | { |
---|
2493 | tr_info inf; |
---|
2494 | char * path = tr_buildPath (dirname, name, NULL); |
---|
2495 | tr_ctorSetMetainfoFromFile (ctor, path); |
---|
2496 | if (!tr_torrentParse (ctor, &inf)) |
---|
2497 | { |
---|
2498 | ++n; |
---|
2499 | tr_variantDictAddStr (lookup, tr_quark_new(inf.hashString,-1), path); |
---|
2500 | } |
---|
2501 | tr_free (path); |
---|
2502 | } |
---|
2503 | } |
---|
2504 | tr_sys_dir_close (odir, NULL); |
---|
2505 | } |
---|
2506 | tr_ctorFree (ctor); |
---|
2507 | |
---|
2508 | session->metainfoLookup = lookup; |
---|
2509 | tr_logAddDebug ("Found %d torrents in \"%s\"", n, dirname); |
---|
2510 | } |
---|
2511 | |
---|
2512 | const char* |
---|
2513 | tr_sessionFindTorrentFile (const tr_session * session, |
---|
2514 | const char * hashString) |
---|
2515 | { |
---|
2516 | const char * filename = NULL; |
---|
2517 | |
---|
2518 | if (!session->metainfoLookup) |
---|
2519 | metainfoLookupInit ((tr_session*)session); |
---|
2520 | tr_variantDictFindStr (session->metainfoLookup, tr_quark_new(hashString,-1), &filename, NULL); |
---|
2521 | |
---|
2522 | return filename; |
---|
2523 | } |
---|
2524 | |
---|
2525 | void |
---|
2526 | tr_sessionSetTorrentFile (tr_session * session, |
---|
2527 | const char * hashString, |
---|
2528 | const char * filename) |
---|
2529 | { |
---|
2530 | /* since we walk session->configDir/torrents/ to build the lookup table, |
---|
2531 | * and tr_sessionSetTorrentFile () is just to tell us there's a new file |
---|
2532 | * in that same directory, we don't need to do anything here if the |
---|
2533 | * lookup table hasn't been built yet */ |
---|
2534 | if (session->metainfoLookup) |
---|
2535 | tr_variantDictAddStr (session->metainfoLookup, tr_quark_new(hashString,-1), filename); |
---|
2536 | } |
---|
2537 | |
---|
2538 | /*** |
---|
2539 | **** |
---|
2540 | ***/ |
---|
2541 | |
---|
2542 | void |
---|
2543 | tr_sessionSetRPCEnabled (tr_session * session, bool isEnabled) |
---|
2544 | { |
---|
2545 | assert (tr_isSession (session)); |
---|
2546 | |
---|
2547 | tr_rpcSetEnabled (session->rpcServer, isEnabled); |
---|
2548 | } |
---|
2549 | |
---|
2550 | bool |
---|
2551 | tr_sessionIsRPCEnabled (const tr_session * session) |
---|
2552 | { |
---|
2553 | assert (tr_isSession (session)); |
---|
2554 | |
---|
2555 | return tr_rpcIsEnabled (session->rpcServer); |
---|
2556 | } |
---|
2557 | |
---|
2558 | void |
---|
2559 | tr_sessionSetRPCPort (tr_session * session, |
---|
2560 | tr_port port) |
---|
2561 | { |
---|
2562 | assert (tr_isSession (session)); |
---|
2563 | |
---|
2564 | tr_rpcSetPort (session->rpcServer, port); |
---|
2565 | } |
---|
2566 | |
---|
2567 | tr_port |
---|
2568 | tr_sessionGetRPCPort (const tr_session * session) |
---|
2569 | { |
---|
2570 | assert (tr_isSession (session)); |
---|
2571 | |
---|
2572 | return tr_rpcGetPort (session->rpcServer); |
---|
2573 | } |
---|
2574 | |
---|
2575 | void |
---|
2576 | tr_sessionSetRPCUrl (tr_session * session, |
---|
2577 | const char * url) |
---|
2578 | { |
---|
2579 | assert (tr_isSession (session)); |
---|
2580 | |
---|
2581 | tr_rpcSetUrl (session->rpcServer, url); |
---|
2582 | } |
---|
2583 | |
---|
2584 | const char* |
---|
2585 | tr_sessionGetRPCUrl (const tr_session * session) |
---|
2586 | { |
---|
2587 | assert (tr_isSession (session)); |
---|
2588 | |
---|
2589 | return tr_rpcGetUrl (session->rpcServer); |
---|
2590 | } |
---|
2591 | |
---|
2592 | void |
---|
2593 | tr_sessionSetRPCCallback (tr_session * session, |
---|
2594 | tr_rpc_func func, |
---|
2595 | void * user_data) |
---|
2596 | { |
---|
2597 | assert (tr_isSession (session)); |
---|
2598 | |
---|
2599 | session->rpc_func = func; |
---|
2600 | session->rpc_func_user_data = user_data; |
---|
2601 | } |
---|
2602 | |
---|
2603 | void |
---|
2604 | tr_sessionSetRPCWhitelist (tr_session * session, |
---|
2605 | const char * whitelist) |
---|
2606 | { |
---|
2607 | assert (tr_isSession (session)); |
---|
2608 | |
---|
2609 | tr_rpcSetWhitelist (session->rpcServer, whitelist); |
---|
2610 | } |
---|
2611 | |
---|
2612 | const char* |
---|
2613 | tr_sessionGetRPCWhitelist (const tr_session * session) |
---|
2614 | { |
---|
2615 | assert (tr_isSession (session)); |
---|
2616 | |
---|
2617 | return tr_rpcGetWhitelist (session->rpcServer); |
---|
2618 | } |
---|
2619 | |
---|
2620 | void |
---|
2621 | tr_sessionSetRPCWhitelistEnabled (tr_session * session, bool isEnabled) |
---|
2622 | { |
---|
2623 | assert (tr_isSession (session)); |
---|
2624 | |
---|
2625 | tr_rpcSetWhitelistEnabled (session->rpcServer, isEnabled); |
---|
2626 | } |
---|
2627 | |
---|
2628 | bool |
---|
2629 | tr_sessionGetRPCWhitelistEnabled (const tr_session * session) |
---|
2630 | { |
---|
2631 | assert (tr_isSession (session)); |
---|
2632 | |
---|
2633 | return tr_rpcGetWhitelistEnabled (session->rpcServer); |
---|
2634 | } |
---|
2635 | |
---|
2636 | |
---|
2637 | void |
---|
2638 | tr_sessionSetRPCPassword (tr_session * session, |
---|
2639 | const char * password) |
---|
2640 | { |
---|
2641 | assert (tr_isSession (session)); |
---|
2642 | |
---|
2643 | tr_rpcSetPassword (session->rpcServer, password); |
---|
2644 | } |
---|
2645 | |
---|
2646 | const char* |
---|
2647 | tr_sessionGetRPCPassword (const tr_session * session) |
---|
2648 | { |
---|
2649 | assert (tr_isSession (session)); |
---|
2650 | |
---|
2651 | return tr_rpcGetPassword (session->rpcServer); |
---|
2652 | } |
---|
2653 | |
---|
2654 | void |
---|
2655 | tr_sessionSetRPCUsername (tr_session * session, |
---|
2656 | const char * username) |
---|
2657 | { |
---|
2658 | assert (tr_isSession (session)); |
---|
2659 | |
---|
2660 | tr_rpcSetUsername (session->rpcServer, username); |
---|
2661 | } |
---|
2662 | |
---|
2663 | const char* |
---|
2664 | tr_sessionGetRPCUsername (const tr_session * session) |
---|
2665 | { |
---|
2666 | assert (tr_isSession (session)); |
---|
2667 | |
---|
2668 | return tr_rpcGetUsername (session->rpcServer); |
---|
2669 | } |
---|
2670 | |
---|
2671 | void |
---|
2672 | tr_sessionSetRPCPasswordEnabled (tr_session * session, bool isEnabled) |
---|
2673 | { |
---|
2674 | assert (tr_isSession (session)); |
---|
2675 | |
---|
2676 | tr_rpcSetPasswordEnabled (session->rpcServer, isEnabled); |
---|
2677 | } |
---|
2678 | |
---|
2679 | bool |
---|
2680 | tr_sessionIsRPCPasswordEnabled (const tr_session * session) |
---|
2681 | { |
---|
2682 | assert (tr_isSession (session)); |
---|
2683 | |
---|
2684 | return tr_rpcIsPasswordEnabled (session->rpcServer); |
---|
2685 | } |
---|
2686 | |
---|
2687 | const char * |
---|
2688 | tr_sessionGetRPCBindAddress (const tr_session * session) |
---|
2689 | { |
---|
2690 | assert (tr_isSession (session)); |
---|
2691 | |
---|
2692 | return tr_rpcGetBindAddress (session->rpcServer); |
---|
2693 | } |
---|
2694 | |
---|
2695 | /**** |
---|
2696 | ***** |
---|
2697 | ****/ |
---|
2698 | |
---|
2699 | bool |
---|
2700 | tr_sessionIsTorrentDoneScriptEnabled (const tr_session * session) |
---|
2701 | { |
---|
2702 | assert (tr_isSession (session)); |
---|
2703 | |
---|
2704 | return session->isTorrentDoneScriptEnabled; |
---|
2705 | } |
---|
2706 | |
---|
2707 | void |
---|
2708 | tr_sessionSetTorrentDoneScriptEnabled (tr_session * session, bool isEnabled) |
---|
2709 | { |
---|
2710 | assert (tr_isSession (session)); |
---|
2711 | assert (tr_isBool (isEnabled)); |
---|
2712 | |
---|
2713 | session->isTorrentDoneScriptEnabled = isEnabled; |
---|
2714 | } |
---|
2715 | |
---|
2716 | const char * |
---|
2717 | tr_sessionGetTorrentDoneScript (const tr_session * session) |
---|
2718 | { |
---|
2719 | assert (tr_isSession (session)); |
---|
2720 | |
---|
2721 | return session->torrentDoneScript; |
---|
2722 | } |
---|
2723 | |
---|
2724 | void |
---|
2725 | tr_sessionSetTorrentDoneScript (tr_session * session, const char * scriptFilename) |
---|
2726 | { |
---|
2727 | assert (tr_isSession (session)); |
---|
2728 | |
---|
2729 | if (session->torrentDoneScript != scriptFilename) |
---|
2730 | { |
---|
2731 | tr_free (session->torrentDoneScript); |
---|
2732 | session->torrentDoneScript = tr_strdup (scriptFilename); |
---|
2733 | } |
---|
2734 | } |
---|
2735 | |
---|
2736 | /*** |
---|
2737 | **** |
---|
2738 | ***/ |
---|
2739 | |
---|
2740 | void |
---|
2741 | tr_sessionSetQueueSize (tr_session * session, tr_direction dir, int n) |
---|
2742 | { |
---|
2743 | assert (tr_isSession (session)); |
---|
2744 | assert (tr_isDirection (dir)); |
---|
2745 | |
---|
2746 | session->queueSize[dir] = n; |
---|
2747 | } |
---|
2748 | |
---|
2749 | int |
---|
2750 | tr_sessionGetQueueSize (const tr_session * session, tr_direction dir) |
---|
2751 | { |
---|
2752 | assert (tr_isSession (session)); |
---|
2753 | assert (tr_isDirection (dir)); |
---|
2754 | |
---|
2755 | return session->queueSize[dir]; |
---|
2756 | } |
---|
2757 | |
---|
2758 | void |
---|
2759 | tr_sessionSetQueueEnabled (tr_session * session, tr_direction dir, bool is_enabled) |
---|
2760 | { |
---|
2761 | assert (tr_isSession (session)); |
---|
2762 | assert (tr_isDirection (dir)); |
---|
2763 | assert (tr_isBool (is_enabled)); |
---|
2764 | |
---|
2765 | session->queueEnabled[dir] = is_enabled; |
---|
2766 | } |
---|
2767 | |
---|
2768 | bool |
---|
2769 | tr_sessionGetQueueEnabled (const tr_session * session, tr_direction dir) |
---|
2770 | { |
---|
2771 | assert (tr_isSession (session)); |
---|
2772 | assert (tr_isDirection (dir)); |
---|
2773 | |
---|
2774 | return session->queueEnabled[dir]; |
---|
2775 | } |
---|
2776 | |
---|
2777 | void |
---|
2778 | tr_sessionSetQueueStalledMinutes (tr_session * session, int minutes) |
---|
2779 | { |
---|
2780 | assert (tr_isSession (session)); |
---|
2781 | assert (minutes > 0); |
---|
2782 | |
---|
2783 | session->queueStalledMinutes = minutes; |
---|
2784 | } |
---|
2785 | |
---|
2786 | void |
---|
2787 | tr_sessionSetQueueStalledEnabled (tr_session * session, bool is_enabled) |
---|
2788 | { |
---|
2789 | assert (tr_isSession (session)); |
---|
2790 | assert (tr_isBool (is_enabled)); |
---|
2791 | |
---|
2792 | session->stalledEnabled = is_enabled; |
---|
2793 | } |
---|
2794 | |
---|
2795 | bool |
---|
2796 | tr_sessionGetQueueStalledEnabled (const tr_session * session) |
---|
2797 | { |
---|
2798 | assert (tr_isSession (session)); |
---|
2799 | |
---|
2800 | return session->stalledEnabled; |
---|
2801 | } |
---|
2802 | |
---|
2803 | int |
---|
2804 | tr_sessionGetQueueStalledMinutes (const tr_session * session) |
---|
2805 | { |
---|
2806 | assert (tr_isSession (session)); |
---|
2807 | |
---|
2808 | return session->queueStalledMinutes; |
---|
2809 | } |
---|
2810 | |
---|
2811 | struct TorrentAndPosition |
---|
2812 | { |
---|
2813 | tr_torrent * tor; |
---|
2814 | int position; |
---|
2815 | }; |
---|
2816 | |
---|
2817 | static int |
---|
2818 | compareTorrentAndPositions (const void * va, const void * vb) |
---|
2819 | { |
---|
2820 | int ret; |
---|
2821 | const struct TorrentAndPosition * a = va; |
---|
2822 | const struct TorrentAndPosition * b = vb; |
---|
2823 | |
---|
2824 | if (a->position > b->position) |
---|
2825 | ret = 1; |
---|
2826 | else if (a->position < b->position) |
---|
2827 | ret = -1; |
---|
2828 | else |
---|
2829 | ret = 0; |
---|
2830 | |
---|
2831 | return ret; |
---|
2832 | } |
---|
2833 | |
---|
2834 | |
---|
2835 | void |
---|
2836 | tr_sessionGetNextQueuedTorrents (tr_session * session, |
---|
2837 | tr_direction direction, |
---|
2838 | size_t num_wanted, |
---|
2839 | tr_ptrArray * setme) |
---|
2840 | { |
---|
2841 | size_t i; |
---|
2842 | size_t n; |
---|
2843 | tr_torrent * tor; |
---|
2844 | struct TorrentAndPosition * candidates; |
---|
2845 | |
---|
2846 | assert (tr_isSession (session)); |
---|
2847 | assert (tr_isDirection (direction)); |
---|
2848 | |
---|
2849 | /* build an array of the candidates */ |
---|
2850 | n = tr_sessionCountTorrents (session); |
---|
2851 | candidates = tr_new (struct TorrentAndPosition, n); |
---|
2852 | i = 0; |
---|
2853 | tor = NULL; |
---|
2854 | while ((tor = tr_torrentNext (session, tor))) |
---|
2855 | { |
---|
2856 | if (!tr_torrentIsQueued (tor)) |
---|
2857 | continue; |
---|
2858 | |
---|
2859 | if (direction != tr_torrentGetQueueDirection (tor)) |
---|
2860 | continue; |
---|
2861 | |
---|
2862 | candidates[i].tor = tor; |
---|
2863 | candidates[i].position = tr_torrentGetQueuePosition (tor); |
---|
2864 | ++i; |
---|
2865 | } |
---|
2866 | |
---|
2867 | /* find the best n candidates */ |
---|
2868 | if (num_wanted > i) |
---|
2869 | num_wanted = i; |
---|
2870 | else if (num_wanted < i) |
---|
2871 | tr_quickfindFirstK (candidates, i, |
---|
2872 | sizeof(struct TorrentAndPosition), |
---|
2873 | compareTorrentAndPositions, num_wanted); |
---|
2874 | |
---|
2875 | /* add them to the return array */ |
---|
2876 | for (i=0; i<num_wanted; ++i) |
---|
2877 | tr_ptrArrayAppend (setme, candidates[i].tor); |
---|
2878 | |
---|
2879 | /* cleanup */ |
---|
2880 | tr_free (candidates); |
---|
2881 | } |
---|
2882 | |
---|
2883 | int |
---|
2884 | tr_sessionCountQueueFreeSlots (tr_session * session, tr_direction dir) |
---|
2885 | { |
---|
2886 | tr_torrent * tor; |
---|
2887 | int active_count; |
---|
2888 | const int max = tr_sessionGetQueueSize (session, dir); |
---|
2889 | const tr_torrent_activity activity = dir == TR_UP ? TR_STATUS_SEED : TR_STATUS_DOWNLOAD; |
---|
2890 | |
---|
2891 | if (!tr_sessionGetQueueEnabled (session, dir)) |
---|
2892 | return INT_MAX; |
---|
2893 | |
---|
2894 | tor = NULL; |
---|
2895 | active_count = 0; |
---|
2896 | while ((tor = tr_torrentNext (session, tor))) |
---|
2897 | if (!tr_torrentIsStalled (tor)) |
---|
2898 | if (tr_torrentGetActivity (tor) == activity) |
---|
2899 | ++active_count; |
---|
2900 | |
---|
2901 | if (active_count >= max) |
---|
2902 | return 0; |
---|
2903 | |
---|
2904 | return max - active_count; |
---|
2905 | } |
---|