Changeset 14081


Ignore:
Timestamp:
May 23, 2013, 5:43:12 AM (9 years ago)
Author:
jordan
Message:

whitespace/indentation

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/libtransmission/session.c

    r14077 r14081  
    6060{
    6161#ifdef TR_LIGHTWEIGHT
    62     DEFAULT_CACHE_SIZE_MB = 2,
    63     DEFAULT_PREFETCH_ENABLED = false,
     62  DEFAULT_CACHE_SIZE_MB = 2,
     63  DEFAULT_PREFETCH_ENABLED = false,
    6464#else
    65     DEFAULT_CACHE_SIZE_MB = 4,
    66     DEFAULT_PREFETCH_ENABLED = true,
     65  DEFAULT_CACHE_SIZE_MB = 4,
     66  DEFAULT_PREFETCH_ENABLED = true,
    6767#endif
    68     SAVE_INTERVAL_SECS = 360
     68  SAVE_INTERVAL_SECS = 360
    6969};
    7070
     
    8181getRandomPort (tr_session * s)
    8282{
    83     return tr_cryptoWeakRandInt (s->randomPortHigh - s->randomPortLow + 1) + s->randomPortLow;
     83  return tr_cryptoWeakRandInt (s->randomPortHigh - s->randomPortLow + 1) + s->randomPortLow;
    8484}
    8585
     
    9191tr_peerIdInit (uint8_t * buf)
    9292{
    93     int          i;
    94     int          val;
    95     int          total = 0;
    96     const char * pool = "0123456789abcdefghijklmnopqrstuvwxyz";
    97     const int    base = 36;
    98 
    99     memcpy (buf, PEERID_PREFIX, 8);
    100 
    101     tr_cryptoRandBuf (buf+8, 11);
    102     for (i=8; i<19; ++i) {
    103         val = buf[i] % base;
    104         total += val;
    105         buf[i] = pool[val];
    106     }
    107 
    108     val = total % base ? base - (total % base) : 0;
    109     buf[19] = pool[val];
    110     buf[20] = '\0';
     93  int i;
     94  int val;
     95  int total = 0;
     96  const char * pool = "0123456789abcdefghijklmnopqrstuvwxyz";
     97  const int base = 36;
     98
     99  memcpy (buf, PEERID_PREFIX, 8);
     100
     101  tr_cryptoRandBuf (buf+8, 11);
     102  for (i=8; i<19; ++i)
     103    {
     104      val = buf[i] % base;
     105      total += val;
     106      buf[i] = pool[val];
     107    }
     108
     109  val = total % base ? base - (total % base) : 0;
     110  buf[19] = pool[val];
     111  buf[20] = '\0';
    111112}
    112113
     
    118119tr_sessionGetEncryption (tr_session * session)
    119120{
    120     assert (session);
    121 
    122     return session->encryptionMode;
    123 }
    124 
    125 void
    126 tr_sessionSetEncryption (tr_session *      session,
    127                          tr_encryption_mode mode)
    128 {
    129     assert (session);
    130     assert (mode == TR_ENCRYPTION_PREFERRED
    131           || mode == TR_ENCRYPTION_REQUIRED
    132           || mode == TR_CLEAR_PREFERRED);
    133 
    134     session->encryptionMode = mode;
     121  assert (session);
     122
     123  return session->encryptionMode;
     124}
     125
     126void
     127tr_sessionSetEncryption (tr_session          * session,
     128                         tr_encryption_mode    mode)
     129{
     130  assert (session);
     131  assert (mode == TR_ENCRYPTION_PREFERRED
     132       || mode == TR_ENCRYPTION_REQUIRED
     133       || mode == TR_CLEAR_PREFERRED);
     134
     135  session->encryptionMode = mode;
    135136}
    136137
     
    141142struct tr_bindinfo
    142143{
    143     int socket;
    144     tr_address addr;
    145     struct event * ev;
     144  int socket;
     145  tr_address addr;
     146  struct event * ev;
    146147};
    147148
     
    150151close_bindinfo (struct tr_bindinfo * b)
    151152{
    152     if ((b != NULL) && (b->socket >=0))
    153     {
    154         event_free (b->ev);
    155         b->ev = NULL;
    156         tr_netCloseSocket (b->socket);
     153  if ((b != NULL) && (b->socket >=0))
     154    {
     155      event_free (b->ev);
     156      b->ev = NULL;
     157      tr_netCloseSocket (b->socket);
    157158    }
    158159}
     
    161162close_incoming_peer_port (tr_session * session)
    162163{
    163     close_bindinfo (session->public_ipv4);
    164     close_bindinfo (session->public_ipv6);
     164  close_bindinfo (session->public_ipv4);
     165  close_bindinfo (session->public_ipv6);
    165166}
    166167
     
    168169free_incoming_peer_port (tr_session * session)
    169170{
    170     close_bindinfo (session->public_ipv4);
    171     tr_free (session->public_ipv4);
    172     session->public_ipv4 = NULL;
    173 
    174     close_bindinfo (session->public_ipv6);
    175     tr_free (session->public_ipv6);
    176     session->public_ipv6 = NULL;
     171  close_bindinfo (session->public_ipv4);
     172  tr_free (session->public_ipv4);
     173  session->public_ipv4 = NULL;
     174
     175  close_bindinfo (session->public_ipv6);
     176  tr_free (session->public_ipv6);
     177  session->public_ipv6 = NULL;
    177178}
    178179
     
    180181accept_incoming_peer (int fd, short what UNUSED, void * vsession)
    181182{
    182     int clientSocket;
    183     tr_port clientPort;
    184     tr_address clientAddr;
    185     tr_session * session = vsession;
    186 
    187     clientSocket = tr_netAccept (session, fd, &clientAddr, &clientPort);
    188     if (clientSocket > 0) {
    189         tr_logAddDeep (__FILE__, __LINE__, NULL, "new incoming connection %d (%s)",
     183  int clientSocket;
     184  tr_port clientPort;
     185  tr_address clientAddr;
     186  tr_session * session = vsession;
     187
     188  clientSocket = tr_netAccept (session, fd, &clientAddr, &clientPort);
     189  if (clientSocket > 0)
     190    {
     191      tr_logAddDeep (__FILE__, __LINE__, NULL, "new incoming connection %d (%s)",
    190192                       clientSocket, tr_peerIoAddrStr (&clientAddr, clientPort));
    191         tr_peerMgrAddIncoming (session->peerMgr, &clientAddr, clientPort,
    192                                clientSocket, NULL);
     193      tr_peerMgrAddIncoming (session->peerMgr, &clientAddr, clientPort,
     194                             clientSocket, NULL);
    193195    }
    194196}
     
    197199open_incoming_peer_port (tr_session * session)
    198200{
    199     struct tr_bindinfo * b;
    200 
    201     /* bind an ipv4 port to listen for incoming peers... */
    202     b = session->public_ipv4;
    203     b->socket = tr_netBindTCP (&b->addr, session->private_peer_port, false);
    204     if (b->socket >= 0) {
    205         b->ev = event_new (session->event_base, b->socket, EV_READ | EV_PERSIST, accept_incoming_peer, session);
    206         event_add (b->ev, NULL);
    207     }
    208 
    209     /* and do the exact same thing for ipv6, if it's supported... */
    210     if (tr_net_hasIPv6 (session->private_peer_port)) {
    211         b = session->public_ipv6;
    212         b->socket = tr_netBindTCP (&b->addr, session->private_peer_port, false);
    213         if (b->socket >= 0) {
    214             b->ev = event_new (session->event_base, b->socket, EV_READ | EV_PERSIST, accept_incoming_peer, session);
    215             event_add (b->ev, NULL);
     201  struct tr_bindinfo * b;
     202
     203  /* bind an ipv4 port to listen for incoming peers... */
     204  b = session->public_ipv4;
     205  b->socket = tr_netBindTCP (&b->addr, session->private_peer_port, false);
     206  if (b->socket >= 0)
     207    {
     208      b->ev = event_new (session->event_base, b->socket, EV_READ | EV_PERSIST, accept_incoming_peer, session);
     209      event_add (b->ev, NULL);
     210    }
     211
     212  /* and do the exact same thing for ipv6, if it's supported... */
     213  if (tr_net_hasIPv6 (session->private_peer_port))
     214    {
     215      b = session->public_ipv6;
     216      b->socket = tr_netBindTCP (&b->addr, session->private_peer_port, false);
     217      if (b->socket >= 0)
     218        {
     219          b->ev = event_new (session->event_base, b->socket, EV_READ | EV_PERSIST, accept_incoming_peer, session);
     220          event_add (b->ev, NULL);
    216221        }
    217222    }
     
    221226tr_sessionGetPublicAddress (const tr_session * session, int tr_af_type, bool * is_default_value)
    222227{
    223     const char * default_value;
    224     const struct tr_bindinfo * bindinfo;
    225 
    226     switch (tr_af_type)
    227     {
    228         case TR_AF_INET:
    229             bindinfo = session->public_ipv4;
    230             default_value = TR_DEFAULT_BIND_ADDRESS_IPV4;
    231             break;
    232 
    233         case TR_AF_INET6:
    234             bindinfo = session->public_ipv6;
    235             default_value = TR_DEFAULT_BIND_ADDRESS_IPV6;
    236             break;
    237 
    238         default:
    239             bindinfo = NULL;
    240             default_value = "";
    241             break;
    242     }
    243 
    244     if (is_default_value && bindinfo)
    245         *is_default_value = !tr_strcmp0 (default_value, tr_address_to_string (&bindinfo->addr));
    246 
    247     return bindinfo ? &bindinfo->addr : NULL;
     228  const char * default_value;
     229  const struct tr_bindinfo * bindinfo;
     230
     231  switch (tr_af_type)
     232    {
     233      case TR_AF_INET:
     234        bindinfo = session->public_ipv4;
     235        default_value = TR_DEFAULT_BIND_ADDRESS_IPV4;
     236        break;
     237
     238      case TR_AF_INET6:
     239        bindinfo = session->public_ipv6;
     240        default_value = TR_DEFAULT_BIND_ADDRESS_IPV6;
     241        break;
     242
     243      default:
     244        bindinfo = NULL;
     245        default_value = "";
     246        break;
     247    }
     248
     249  if (is_default_value && bindinfo)
     250    *is_default_value = !tr_strcmp0 (default_value, tr_address_to_string (&bindinfo->addr));
     251
     252  return bindinfo ? &bindinfo->addr : NULL;
    248253}
    249254
     
    261266parse_tos (const char *str)
    262267{
    263     char *p;
    264     int value;
    265 
    266     if (!evutil_ascii_strcasecmp (str, ""))
    267         return 0;
    268     if (!evutil_ascii_strcasecmp (str, "default"))
    269         return 0;
    270 
    271     if (!evutil_ascii_strcasecmp (str, "lowcost"))
    272         return 0x10;
    273     if (!evutil_ascii_strcasecmp (str, "mincost"))
    274         return 0x10;
    275 
    276     if (!evutil_ascii_strcasecmp (str, "throughput"))
    277         return 0x08;
    278     if (!evutil_ascii_strcasecmp (str, "reliability"))
    279         return 0x04;
    280     if (!evutil_ascii_strcasecmp (str, "lowdelay"))
    281         return 0x02;
    282 
    283     value = strtol (str, &p, 0);
    284     if (!p || (p == str))
    285         return 0;
    286 
    287     return value;
     268  char *p;
     269  int value;
     270
     271  if (!evutil_ascii_strcasecmp (str, ""))
     272    return 0;
     273  if (!evutil_ascii_strcasecmp (str, "default"))
     274    return 0;
     275
     276  if (!evutil_ascii_strcasecmp (str, "lowcost"))
     277    return 0x10;
     278  if (!evutil_ascii_strcasecmp (str, "mincost"))
     279    return 0x10;
     280
     281  if (!evutil_ascii_strcasecmp (str, "throughput"))
     282    return 0x08;
     283  if (!evutil_ascii_strcasecmp (str, "reliability"))
     284    return 0x04;
     285  if (!evutil_ascii_strcasecmp (str, "lowdelay"))
     286    return 0x02;
     287
     288  value = strtol (str, &p, 0);
     289  if (!p || (p == str))
     290    return 0;
     291
     292  return value;
    288293}
    289294
     
    291296format_tos (int value)
    292297{
    293     static char buf[8];
    294     switch (value) {
    295     case 0: return "default";
    296     case 0x10: return "lowcost";
    297     case 0x08: return "throughput";
    298     case 0x04: return "reliability";
    299     case 0x02: return "lowdelay";
    300     default:
     298  static char buf[8];
     299  switch (value)
     300    {
     301      case 0: return "default";
     302      case 0x10: return "lowcost";
     303      case 0x08: return "throughput";
     304      case 0x04: return "reliability";
     305      case 0x02: return "lowdelay";
     306      default:
    301307        tr_snprintf (buf, 8, "%d", value);
    302308        return buf;
     
    307313tr_sessionGetDefaultSettings (tr_variant * d)
    308314{
    309     assert (tr_variantIsDict (d));
    310 
    311     tr_variantDictReserve (d, 63);
    312     tr_variantDictAddBool (d, TR_KEY_blocklist_enabled,               false);
    313     tr_variantDictAddStr  (d, TR_KEY_blocklist_url,                   "http://www.example.com/blocklist");
    314     tr_variantDictAddInt  (d, TR_KEY_cache_size_mb,                   DEFAULT_CACHE_SIZE_MB);
    315     tr_variantDictAddBool (d, TR_KEY_dht_enabled,                     true);
    316     tr_variantDictAddBool (d, TR_KEY_utp_enabled,                     true);
    317     tr_variantDictAddBool (d, TR_KEY_lpd_enabled,                     false);
    318     tr_variantDictAddStr  (d, TR_KEY_download_dir,                    tr_getDefaultDownloadDir ());
    319     tr_variantDictAddInt  (d, TR_KEY_speed_limit_down,                100);
    320     tr_variantDictAddBool (d, TR_KEY_speed_limit_down_enabled,        false);
    321     tr_variantDictAddInt  (d, TR_KEY_encryption,                      TR_DEFAULT_ENCRYPTION);
    322     tr_variantDictAddInt  (d, TR_KEY_idle_seeding_limit,              30);
    323     tr_variantDictAddBool (d, TR_KEY_idle_seeding_limit_enabled,      false);
    324     tr_variantDictAddStr  (d, TR_KEY_incomplete_dir,                  tr_getDefaultDownloadDir ());
    325     tr_variantDictAddBool (d, TR_KEY_incomplete_dir_enabled,          false);
    326     tr_variantDictAddInt  (d, TR_KEY_message_level,                   TR_LOG_INFO);
    327     tr_variantDictAddInt  (d, TR_KEY_download_queue_size,             5);
    328     tr_variantDictAddBool (d, TR_KEY_download_queue_enabled,          true);
    329     tr_variantDictAddInt  (d, TR_KEY_peer_limit_global,               atoi (TR_DEFAULT_PEER_LIMIT_GLOBAL_STR));
    330     tr_variantDictAddInt  (d, TR_KEY_peer_limit_per_torrent,          atoi (TR_DEFAULT_PEER_LIMIT_TORRENT_STR));
    331     tr_variantDictAddInt  (d, TR_KEY_peer_port,                       atoi (TR_DEFAULT_PEER_PORT_STR));
    332     tr_variantDictAddBool (d, TR_KEY_peer_port_random_on_start,       false);
    333     tr_variantDictAddInt  (d, TR_KEY_peer_port_random_low,            49152);
    334     tr_variantDictAddInt  (d, TR_KEY_peer_port_random_high,           65535);
    335     tr_variantDictAddStr  (d, TR_KEY_peer_socket_tos,                 TR_DEFAULT_PEER_SOCKET_TOS_STR);
    336     tr_variantDictAddBool (d, TR_KEY_pex_enabled,                     true);
    337     tr_variantDictAddBool (d, TR_KEY_port_forwarding_enabled,         true);
    338     tr_variantDictAddInt  (d, TR_KEY_preallocation,                   TR_PREALLOCATE_SPARSE);
    339     tr_variantDictAddBool (d, TR_KEY_prefetch_enabled,                DEFAULT_PREFETCH_ENABLED);
    340     tr_variantDictAddInt  (d, TR_KEY_peer_id_ttl_hours,               6);
    341     tr_variantDictAddBool (d, TR_KEY_queue_stalled_enabled,           true);
    342     tr_variantDictAddInt  (d, TR_KEY_queue_stalled_minutes,           30);
    343     tr_variantDictAddReal (d, TR_KEY_ratio_limit,                     2.0);
    344     tr_variantDictAddBool (d, TR_KEY_ratio_limit_enabled,             false);
    345     tr_variantDictAddBool (d, TR_KEY_rename_partial_files,            true);
    346     tr_variantDictAddBool (d, TR_KEY_rpc_authentication_required,     false);
    347     tr_variantDictAddStr  (d, TR_KEY_rpc_bind_address,                "0.0.0.0");
    348     tr_variantDictAddBool (d, TR_KEY_rpc_enabled,                     false);
    349     tr_variantDictAddStr  (d, TR_KEY_rpc_password,                    "");
    350     tr_variantDictAddStr  (d, TR_KEY_rpc_username,                    "");
    351     tr_variantDictAddStr  (d, TR_KEY_rpc_whitelist,                   TR_DEFAULT_RPC_WHITELIST);
    352     tr_variantDictAddBool (d, TR_KEY_rpc_whitelist_enabled,           true);
    353     tr_variantDictAddInt  (d, TR_KEY_rpc_port,                        atoi (TR_DEFAULT_RPC_PORT_STR));
    354     tr_variantDictAddStr  (d, TR_KEY_rpc_url,                         TR_DEFAULT_RPC_URL_STR);
    355     tr_variantDictAddBool (d, TR_KEY_scrape_paused_torrents_enabled,  true);
    356     tr_variantDictAddStr  (d, TR_KEY_script_torrent_done_filename,    "");
    357     tr_variantDictAddBool (d, TR_KEY_script_torrent_done_enabled,     false);
    358     tr_variantDictAddInt  (d, TR_KEY_seed_queue_size,                 10);
    359     tr_variantDictAddBool (d, TR_KEY_seed_queue_enabled,              false);
    360     tr_variantDictAddBool (d, TR_KEY_alt_speed_enabled,               false);
    361     tr_variantDictAddInt  (d, TR_KEY_alt_speed_up,                    50); /* half the regular */
    362     tr_variantDictAddInt  (d, TR_KEY_alt_speed_down,                  50); /* half the regular */
    363     tr_variantDictAddInt  (d, TR_KEY_alt_speed_time_begin,            540); /* 9am */
    364     tr_variantDictAddBool (d, TR_KEY_alt_speed_time_enabled,          false);
    365     tr_variantDictAddInt  (d, TR_KEY_alt_speed_time_end,              1020); /* 5pm */
    366     tr_variantDictAddInt  (d, TR_KEY_alt_speed_time_day,              TR_SCHED_ALL);
    367     tr_variantDictAddInt  (d, TR_KEY_speed_limit_up,                  100);
    368     tr_variantDictAddBool (d, TR_KEY_speed_limit_up_enabled,          false);
    369     tr_variantDictAddInt  (d, TR_KEY_umask,                           022);
    370     tr_variantDictAddInt  (d, TR_KEY_upload_slots_per_torrent,        14);
    371     tr_variantDictAddStr  (d, TR_KEY_bind_address_ipv4,               TR_DEFAULT_BIND_ADDRESS_IPV4);
    372     tr_variantDictAddStr  (d, TR_KEY_bind_address_ipv6,               TR_DEFAULT_BIND_ADDRESS_IPV6);
    373     tr_variantDictAddBool (d, TR_KEY_start_added_torrents,            true);
    374     tr_variantDictAddBool (d, TR_KEY_trash_original_torrent_files,    false);
     315  assert (tr_variantIsDict (d));
     316
     317  tr_variantDictReserve (d, 63);
     318  tr_variantDictAddBool (d, TR_KEY_blocklist_enabled,               false);
     319  tr_variantDictAddStr  (d, TR_KEY_blocklist_url,                   "http://www.example.com/blocklist");
     320  tr_variantDictAddInt  (d, TR_KEY_cache_size_mb,                   DEFAULT_CACHE_SIZE_MB);
     321  tr_variantDictAddBool (d, TR_KEY_dht_enabled,                     true);
     322  tr_variantDictAddBool (d, TR_KEY_utp_enabled,                     true);
     323  tr_variantDictAddBool (d, TR_KEY_lpd_enabled,                     false);
     324  tr_variantDictAddStr  (d, TR_KEY_download_dir,                    tr_getDefaultDownloadDir ());
     325  tr_variantDictAddInt  (d, TR_KEY_speed_limit_down,                100);
     326  tr_variantDictAddBool (d, TR_KEY_speed_limit_down_enabled,        false);
     327  tr_variantDictAddInt  (d, TR_KEY_encryption,                      TR_DEFAULT_ENCRYPTION);
     328  tr_variantDictAddInt  (d, TR_KEY_idle_seeding_limit,              30);
     329  tr_variantDictAddBool (d, TR_KEY_idle_seeding_limit_enabled,      false);
     330  tr_variantDictAddStr  (d, TR_KEY_incomplete_dir,                  tr_getDefaultDownloadDir ());
     331  tr_variantDictAddBool (d, TR_KEY_incomplete_dir_enabled,          false);
     332  tr_variantDictAddInt  (d, TR_KEY_message_level,                   TR_LOG_INFO);
     333  tr_variantDictAddInt  (d, TR_KEY_download_queue_size,             5);
     334  tr_variantDictAddBool (d, TR_KEY_download_queue_enabled,          true);
     335  tr_variantDictAddInt  (d, TR_KEY_peer_limit_global,               atoi (TR_DEFAULT_PEER_LIMIT_GLOBAL_STR));
     336  tr_variantDictAddInt  (d, TR_KEY_peer_limit_per_torrent,          atoi (TR_DEFAULT_PEER_LIMIT_TORRENT_STR));
     337  tr_variantDictAddInt  (d, TR_KEY_peer_port,                       atoi (TR_DEFAULT_PEER_PORT_STR));
     338  tr_variantDictAddBool (d, TR_KEY_peer_port_random_on_start,       false);
     339  tr_variantDictAddInt  (d, TR_KEY_peer_port_random_low,            49152);
     340  tr_variantDictAddInt  (d, TR_KEY_peer_port_random_high,           65535);
     341  tr_variantDictAddStr  (d, TR_KEY_peer_socket_tos,                 TR_DEFAULT_PEER_SOCKET_TOS_STR);
     342  tr_variantDictAddBool (d, TR_KEY_pex_enabled,                     true);
     343  tr_variantDictAddBool (d, TR_KEY_port_forwarding_enabled,         true);
     344  tr_variantDictAddInt  (d, TR_KEY_preallocation,                   TR_PREALLOCATE_SPARSE);
     345  tr_variantDictAddBool (d, TR_KEY_prefetch_enabled,                DEFAULT_PREFETCH_ENABLED);
     346  tr_variantDictAddInt  (d, TR_KEY_peer_id_ttl_hours,               6);
     347  tr_variantDictAddBool (d, TR_KEY_queue_stalled_enabled,           true);
     348  tr_variantDictAddInt  (d, TR_KEY_queue_stalled_minutes,           30);
     349  tr_variantDictAddReal (d, TR_KEY_ratio_limit,                     2.0);
     350  tr_variantDictAddBool (d, TR_KEY_ratio_limit_enabled,             false);
     351  tr_variantDictAddBool (d, TR_KEY_rename_partial_files,            true);
     352  tr_variantDictAddBool (d, TR_KEY_rpc_authentication_required,     false);
     353  tr_variantDictAddStr  (d, TR_KEY_rpc_bind_address,                "0.0.0.0");
     354  tr_variantDictAddBool (d, TR_KEY_rpc_enabled,                     false);
     355  tr_variantDictAddStr  (d, TR_KEY_rpc_password,                    "");
     356  tr_variantDictAddStr  (d, TR_KEY_rpc_username,                    "");
     357  tr_variantDictAddStr  (d, TR_KEY_rpc_whitelist,                   TR_DEFAULT_RPC_WHITELIST);
     358  tr_variantDictAddBool (d, TR_KEY_rpc_whitelist_enabled,           true);
     359  tr_variantDictAddInt  (d, TR_KEY_rpc_port,                        atoi (TR_DEFAULT_RPC_PORT_STR));
     360  tr_variantDictAddStr  (d, TR_KEY_rpc_url,                         TR_DEFAULT_RPC_URL_STR);
     361  tr_variantDictAddBool (d, TR_KEY_scrape_paused_torrents_enabled,  true);
     362  tr_variantDictAddStr  (d, TR_KEY_script_torrent_done_filename,    "");
     363  tr_variantDictAddBool (d, TR_KEY_script_torrent_done_enabled,     false);
     364  tr_variantDictAddInt  (d, TR_KEY_seed_queue_size,                 10);
     365  tr_variantDictAddBool (d, TR_KEY_seed_queue_enabled,              false);
     366  tr_variantDictAddBool (d, TR_KEY_alt_speed_enabled,               false);
     367  tr_variantDictAddInt  (d, TR_KEY_alt_speed_up,                    50); /* half the regular */
     368  tr_variantDictAddInt  (d, TR_KEY_alt_speed_down,                  50); /* half the regular */
     369  tr_variantDictAddInt  (d, TR_KEY_alt_speed_time_begin,            540); /* 9am */
     370  tr_variantDictAddBool (d, TR_KEY_alt_speed_time_enabled,          false);
     371  tr_variantDictAddInt  (d, TR_KEY_alt_speed_time_end,              1020); /* 5pm */
     372  tr_variantDictAddInt  (d, TR_KEY_alt_speed_time_day,              TR_SCHED_ALL);
     373  tr_variantDictAddInt  (d, TR_KEY_speed_limit_up,                  100);
     374  tr_variantDictAddBool (d, TR_KEY_speed_limit_up_enabled,          false);
     375  tr_variantDictAddInt  (d, TR_KEY_umask,                           022);
     376  tr_variantDictAddInt  (d, TR_KEY_upload_slots_per_torrent,        14);
     377  tr_variantDictAddStr  (d, TR_KEY_bind_address_ipv4,               TR_DEFAULT_BIND_ADDRESS_IPV4);
     378  tr_variantDictAddStr  (d, TR_KEY_bind_address_ipv6,               TR_DEFAULT_BIND_ADDRESS_IPV6);
     379  tr_variantDictAddBool (d, TR_KEY_start_added_torrents,            true);
     380  tr_variantDictAddBool (d, TR_KEY_trash_original_torrent_files,    false);
    375381}
    376382
     
    543549onSaveTimer (int foo UNUSED, short bar UNUSED, void * vsession)
    544550{
    545     tr_torrent * tor = NULL;
    546     tr_session * session = vsession;
    547 
    548     if (tr_cacheFlushDone (session->cache))
    549         tr_logAddError ("Error while flushing completed pieces from cache");
    550 
    551     while ((tor = tr_torrentNext (session, tor)))
    552         tr_torrentSave (tor);
    553 
    554     tr_statsSaveDirty (session);
    555 
    556     tr_timerAdd (session->saveTimer, SAVE_INTERVAL_SECS, 0);
     551  tr_torrent * tor = NULL;
     552  tr_session * session = vsession;
     553
     554  if (tr_cacheFlushDone (session->cache))
     555    tr_logAddError ("Error while flushing completed pieces from cache");
     556
     557  while ((tor = tr_torrentNext (session, tor)))
     558    tr_torrentSave (tor);
     559
     560  tr_statsSaveDirty (session);
     561
     562  tr_timerAdd (session->saveTimer, SAVE_INTERVAL_SECS, 0);
    557563}
    558564
     
    565571struct init_data
    566572{
    567     tr_session  * session;
    568     const char  * configDir;
    569     bool          done;
    570     bool          messageQueuingEnabled;
    571     tr_variant * clientSettings;
     573  bool done;
     574  bool messageQueuingEnabled;
     575  tr_session * session;
     576  const char * configDir;
     577  tr_variant * clientSettings;
    572578};
    573579
     
    578584                tr_variant     * clientSettings)
    579585{
    580     int64_t i;
    581     tr_session * session;
    582     struct init_data data;
    583 
    584     assert (tr_variantIsDict (clientSettings));
    585 
    586     tr_timeUpdate (time (NULL));
    587 
    588     /* initialize the bare skeleton of the session object */
    589     session = tr_new0 (tr_session, 1);
    590     session->udp_socket = -1;
    591     session->udp6_socket = -1;
    592     session->lock = tr_lockNew ();
    593     session->cache = tr_cacheNew (1024*1024*2);
    594     session->tag = tr_strdup (tag);
    595     session->magicNumber = SESSION_MAGIC_NUMBER;
    596     tr_bandwidthConstruct (&session->bandwidth, session, NULL);
    597     tr_variantInitList (&session->removedTorrents, 0);
    598 
    599     /* nice to start logging at the very beginning */
    600     if (tr_variantDictFindInt (clientSettings, TR_KEY_message_level, &i))
    601         tr_logSetLevel (i);
    602 
    603     /* start the libtransmission thread */
    604     tr_netInit (); /* must go before tr_eventInit */
    605     tr_eventInit (session);
    606     assert (session->events != NULL);
    607 
    608     /* run the rest in the libtransmission thread */
    609     data.done = false;
    610     data.session = session;
    611     data.configDir = configDir;
    612     data.messageQueuingEnabled = messageQueuingEnabled;
    613     data.clientSettings = clientSettings;
    614     tr_runInEventThread (session, tr_sessionInitImpl, &data);
    615     while (!data.done)
    616         tr_wait_msec (50);
    617 
    618     return session;
     586  int64_t i;
     587  tr_session * session;
     588  struct init_data data;
     589
     590  assert (tr_variantIsDict (clientSettings));
     591
     592  tr_timeUpdate (time (NULL));
     593
     594  /* initialize the bare skeleton of the session object */
     595  session = tr_new0 (tr_session, 1);
     596  session->udp_socket = -1;
     597  session->udp6_socket = -1;
     598  session->lock = tr_lockNew ();
     599  session->cache = tr_cacheNew (1024*1024*2);
     600  session->tag = tr_strdup (tag);
     601  session->magicNumber = SESSION_MAGIC_NUMBER;
     602  tr_bandwidthConstruct (&session->bandwidth, session, NULL);
     603  tr_variantInitList (&session->removedTorrents, 0);
     604
     605  /* nice to start logging at the very beginning */
     606  if (tr_variantDictFindInt (clientSettings, TR_KEY_message_level, &i))
     607    tr_logSetLevel (i);
     608
     609  /* start the libtransmission thread */
     610  tr_netInit (); /* must go before tr_eventInit */
     611  tr_eventInit (session);
     612  assert (session->events != NULL);
     613
     614  /* run the rest in the libtransmission thread */
     615  data.done = false;
     616  data.session = session;
     617  data.configDir = configDir;
     618  data.messageQueuingEnabled = messageQueuingEnabled;
     619  data.clientSettings = clientSettings;
     620  tr_runInEventThread (session, tr_sessionInitImpl, &data);
     621  while (!data.done)
     622    tr_wait_msec (50);
     623
     624  return session;
    619625}
    620626
     
    624630onNowTimer (int foo UNUSED, short bar UNUSED, void * vsession)
    625631{
    626     int usec;
    627     const int min = 100;
    628     const int max = 999999;
    629     struct timeval tv;
    630     tr_torrent * tor = NULL;
    631     tr_session * session = vsession;
    632     const time_t now = time (NULL);
    633 
    634     assert (tr_isSession (session));
    635     assert (session->nowTimer != NULL);
    636 
    637     /**
    638     ***  tr_session things to do once per second
    639     **/
    640 
    641     tr_timeUpdate (now);
    642 
    643     tr_dhtUpkeep (session);
    644 
    645     if (session->turtle.isClockEnabled)
    646         turtleCheckClock (session, &session->turtle);
    647 
    648     while ((tor = tr_torrentNext (session, tor))) {
    649         if (tor->isRunning) {
    650             if (tr_torrentIsSeed (tor))
    651                 ++tor->secondsSeeding;
    652             else
    653                 ++tor->secondsDownloading;
     632  int usec;
     633  const int min = 100;
     634  const int max = 999999;
     635  struct timeval tv;
     636  tr_torrent * tor = NULL;
     637  tr_session * session = vsession;
     638  const time_t now = time (NULL);
     639
     640  assert (tr_isSession (session));
     641  assert (session->nowTimer != NULL);
     642
     643  /**
     644  ***  tr_session things to do once per second
     645  **/
     646
     647  tr_timeUpdate (now);
     648
     649  tr_dhtUpkeep (session);
     650
     651  if (session->turtle.isClockEnabled)
     652    turtleCheckClock (session, &session->turtle);
     653
     654  while ((tor = tr_torrentNext (session, tor)))
     655    {
     656      if (tor->isRunning)
     657        {
     658          if (tr_torrentIsSeed (tor))
     659            ++tor->secondsSeeding;
     660          else
     661            ++tor->secondsDownloading;
    654662        }
    655663    }
    656664
    657     /**
    658     ***  Set the timer
    659     **/
    660 
    661     /* schedule the next timer for right after the next second begins */
    662     gettimeofday (&tv, NULL);
    663     usec = 1000000 - tv.tv_usec;
    664     if (usec > max) usec = max;
    665     if (usec < min) usec = min;
    666     tr_timerAdd (session->nowTimer, 0, usec);
    667     /* fprintf (stderr, "time %zu sec, %zu microsec\n", (size_t)tr_time (), (size_t)tv.tv_usec); */
     665  /**
     666  ***  Set the timer
     667  **/
     668
     669  /* schedule the next timer for right after the next second begins */
     670  gettimeofday (&tv, NULL);
     671  usec = 1000000 - tv.tv_usec;
     672  if (usec > max)
     673    usec = max;
     674  if (usec < min)
     675    usec = min;
     676  tr_timerAdd (session->nowTimer, 0, usec);
     677  /* fprintf (stderr, "time %zu sec, %zu microsec\n", (size_t)tr_time (), (size_t)tv.tv_usec); */
    668678}
    669679
     
    673683tr_sessionInitImpl (void * vdata)
    674684{
    675     tr_variant settings;
    676     struct init_data * data = vdata;
    677     tr_variant * clientSettings = data->clientSettings;
    678     tr_session * session = data->session;
    679 
    680     assert (tr_amInEventThread (session));
    681     assert (tr_variantIsDict (clientSettings));
    682 
    683     dbgmsg ("tr_sessionInit: the session's top-level bandwidth object is %p",
    684             &session->bandwidth);
    685 
    686     tr_variantInitDict (&settings, 0);
    687     tr_sessionGetDefaultSettings (&settings);
    688     tr_variantMergeDicts (&settings, clientSettings);
    689 
    690     assert (session->event_base != NULL);
    691     session->nowTimer = evtimer_new (session->event_base, onNowTimer, session);
    692     onNowTimer (0, 0, session);
     685  tr_variant settings;
     686  struct init_data * data = vdata;
     687  tr_variant * clientSettings = data->clientSettings;
     688  tr_session * session = data->session;
     689
     690  assert (tr_amInEventThread (session));
     691  assert (tr_variantIsDict (clientSettings));
     692
     693  dbgmsg ("tr_sessionInit: the session's top-level bandwidth object is %p",
     694          &session->bandwidth);
     695
     696  tr_variantInitDict (&settings, 0);
     697  tr_sessionGetDefaultSettings (&settings);
     698  tr_variantMergeDicts (&settings, clientSettings);
     699
     700  assert (session->event_base != NULL);
     701  session->nowTimer = evtimer_new (session->event_base, onNowTimer, session);
     702  onNowTimer (0, 0, session);
    693703
    694704#ifndef WIN32
    695     /* Don't exit when writing on a broken socket */
    696     signal (SIGPIPE, SIG_IGN);
     705  /* Don't exit when writing on a broken socket */
     706  signal (SIGPIPE, SIG_IGN);
    697707#endif
    698708
    699     tr_logSetQueueEnabled (data->messageQueuingEnabled);
    700 
    701     tr_setConfigDir (session, data->configDir);
    702 
    703     session->peerMgr = tr_peerMgrNew (session);
    704 
    705     session->shared = tr_sharedInit (session);
    706 
    707     /**
    708     ***  Blocklist
    709     **/
    710 
    711     {
    712         char * filename = tr_buildPath (session->configDir, "blocklists", NULL);
    713         tr_mkdirp (filename, 0777);
    714         tr_free (filename);
    715         loadBlocklists (session);
    716     }
    717 
    718     assert (tr_isSession (session));
    719 
    720     session->saveTimer = evtimer_new (session->event_base, onSaveTimer, session);
    721     tr_timerAdd (session->saveTimer, SAVE_INTERVAL_SECS, 0);
    722 
    723     tr_announcerInit (session);
    724 
    725     /* first %s is the application name
    726        second %s is the version number */
    727     tr_logAddInfo (_("%s %s started"), TR_NAME, LONG_VERSION_STRING);
    728 
    729     tr_statsInit (session);
    730 
    731     tr_sessionSet (session, &settings);
    732 
    733     tr_udpInit (session);
    734 
    735     if (session->isLPDEnabled)
    736         tr_lpdInit (session, &session->public_ipv4->addr);
    737 
    738     /* cleanup */
    739     tr_variantFree (&settings);
    740     data->done = true;
     709  tr_logSetQueueEnabled (data->messageQueuingEnabled);
     710
     711  tr_setConfigDir (session, data->configDir);
     712
     713  session->peerMgr = tr_peerMgrNew (session);
     714
     715  session->shared = tr_sharedInit (session);
     716
     717  /**
     718  ***  Blocklist
     719  **/
     720
     721  {
     722    char * filename = tr_buildPath (session->configDir, "blocklists", NULL);
     723    tr_mkdirp (filename, 0777);
     724    tr_free (filename);
     725    loadBlocklists (session);
     726  }
     727
     728  assert (tr_isSession (session));
     729
     730  session->saveTimer = evtimer_new (session->event_base, onSaveTimer, session);
     731  tr_timerAdd (session->saveTimer, SAVE_INTERVAL_SECS, 0);
     732
     733  tr_announcerInit (session);
     734
     735  /* first %s is the application name
     736     second %s is the version number */
     737  tr_logAddInfo (_("%s %s started"), TR_NAME, LONG_VERSION_STRING);
     738
     739  tr_statsInit (session);
     740
     741  tr_sessionSet (session, &settings);
     742
     743  tr_udpInit (session);
     744
     745  if (session->isLPDEnabled)
     746    tr_lpdInit (session, &session->public_ipv4->addr);
     747
     748  /* cleanup */
     749  tr_variantFree (&settings);
     750  data->done = true;
    741751}
    742752
     
    747757sessionSetImpl (void * vdata)
    748758{
    749     int64_t i;
    750     double  d;
    751     bool boolVal;
    752     const char * str;
    753     struct tr_bindinfo b;
    754     struct init_data * data = vdata;
    755     tr_session * session = data->session;
    756     tr_variant * settings = data->clientSettings;
    757     struct tr_turtle_info * turtle = &session->turtle;
    758 
    759     assert (tr_isSession (session));
    760     assert (tr_variantIsDict (settings));
    761     assert (tr_amInEventThread (session));
    762 
    763     if (tr_variantDictFindInt (settings, TR_KEY_message_level, &i))
    764         tr_logSetLevel (i);
    765 
    766     if (tr_variantDictFindInt (settings, TR_KEY_umask, &i)) {
    767         session->umask = (mode_t)i;
    768         umask (session->umask);
    769     }
    770 
    771     /* misc features */
    772     if (tr_variantDictFindInt (settings, TR_KEY_cache_size_mb, &i))
    773         tr_sessionSetCacheLimit_MB (session, i);
    774     if (tr_variantDictFindInt (settings, TR_KEY_peer_limit_per_torrent, &i))
    775         tr_sessionSetPeerLimitPerTorrent (session, i);
    776     if (tr_variantDictFindBool (settings, TR_KEY_pex_enabled, &boolVal))
    777         tr_sessionSetPexEnabled (session, boolVal);
    778     if (tr_variantDictFindBool (settings, TR_KEY_dht_enabled, &boolVal))
    779         tr_sessionSetDHTEnabled (session, boolVal);
    780     if (tr_variantDictFindBool (settings, TR_KEY_utp_enabled, &boolVal))
    781         tr_sessionSetUTPEnabled (session, boolVal);
    782     if (tr_variantDictFindBool (settings, TR_KEY_lpd_enabled, &boolVal))
    783         tr_sessionSetLPDEnabled (session, boolVal);
    784     if (tr_variantDictFindInt (settings, TR_KEY_encryption, &i))
    785         tr_sessionSetEncryption (session, i);
    786     if (tr_variantDictFindStr (settings, TR_KEY_peer_socket_tos, &str, NULL))
    787         session->peerSocketTOS = parse_tos (str);
    788     if (tr_variantDictFindStr (settings, TR_KEY_peer_congestion_algorithm, &str, NULL))
    789         session->peer_congestion_algorithm = tr_strdup (str);
    790     else
    791         session->peer_congestion_algorithm = tr_strdup ("");
    792     if (tr_variantDictFindBool (settings, TR_KEY_blocklist_enabled, &boolVal))
    793         tr_blocklistSetEnabled (session, boolVal);
    794     if (tr_variantDictFindStr (settings, TR_KEY_blocklist_url, &str, NULL))
    795         tr_blocklistSetURL (session, str);
    796     if (tr_variantDictFindBool (settings, TR_KEY_start_added_torrents, &boolVal))
    797         tr_sessionSetPaused (session, !boolVal);
    798     if (tr_variantDictFindBool (settings, TR_KEY_trash_original_torrent_files, &boolVal))
    799         tr_sessionSetDeleteSource (session, boolVal);
    800     if (tr_variantDictFindInt (settings, TR_KEY_peer_id_ttl_hours, &i))
    801         session->peer_id_ttl_hours = i;
    802 
    803     /* torrent queues */
    804     if (tr_variantDictFindInt (settings, TR_KEY_queue_stalled_minutes, &i))
    805         tr_sessionSetQueueStalledMinutes (session, i);
    806     if (tr_variantDictFindBool (settings, TR_KEY_queue_stalled_enabled, &boolVal))
    807         tr_sessionSetQueueStalledEnabled (session, boolVal);
    808     if (tr_variantDictFindInt (settings, TR_KEY_download_queue_size, &i))
    809         tr_sessionSetQueueSize (session, TR_DOWN, i);
    810     if (tr_variantDictFindBool (settings, TR_KEY_download_queue_enabled, &boolVal))
    811         tr_sessionSetQueueEnabled (session, TR_DOWN, boolVal);
    812     if (tr_variantDictFindInt (settings, TR_KEY_seed_queue_size, &i))
    813         tr_sessionSetQueueSize (session, TR_UP, i);
    814     if (tr_variantDictFindBool (settings, TR_KEY_seed_queue_enabled, &boolVal))
    815         tr_sessionSetQueueEnabled (session, TR_UP, boolVal);
    816 
    817     /* files and directories */
    818     if (tr_variantDictFindBool (settings, TR_KEY_prefetch_enabled, &boolVal))
    819         session->isPrefetchEnabled = boolVal;
    820     if (tr_variantDictFindInt (settings, TR_KEY_preallocation, &i))
    821         session->preallocationMode = i;
    822     if (tr_variantDictFindStr (settings, TR_KEY_download_dir, &str, NULL))
    823         tr_sessionSetDownloadDir (session, str);
    824     if (tr_variantDictFindStr (settings, TR_KEY_incomplete_dir, &str, NULL))
    825         tr_sessionSetIncompleteDir (session, str);
    826     if (tr_variantDictFindBool (settings, TR_KEY_incomplete_dir_enabled, &boolVal))
    827         tr_sessionSetIncompleteDirEnabled (session, boolVal);
    828     if (tr_variantDictFindBool (settings, TR_KEY_rename_partial_files, &boolVal))
    829         tr_sessionSetIncompleteFileNamingEnabled (session, boolVal);
    830 
    831     /* rpc server */
    832     if (session->rpcServer != NULL) /* close the old one */
    833         tr_rpcClose (&session->rpcServer);
    834     session->rpcServer = tr_rpcInit (session, settings);
    835 
    836     /* public addresses */
    837 
    838     free_incoming_peer_port (session);
    839 
    840     tr_variantDictFindStr (settings, TR_KEY_bind_address_ipv4, &str, NULL);
    841     if (!tr_address_from_string (&b.addr, str) || (b.addr.type != TR_AF_INET))
    842         b.addr = tr_inaddr_any;
    843     b.socket = -1;
    844     session->public_ipv4 = tr_memdup (&b, sizeof (struct tr_bindinfo));
    845 
    846     tr_variantDictFindStr (settings, TR_KEY_bind_address_ipv6, &str, NULL);
    847     if (!tr_address_from_string (&b.addr, str) || (b.addr.type != TR_AF_INET6))
    848         b.addr = tr_in6addr_any;
    849     b.socket = -1;
    850     session->public_ipv6 = tr_memdup (&b, sizeof (struct tr_bindinfo));
    851 
    852     /* incoming peer port */
    853     if (tr_variantDictFindInt (settings, TR_KEY_peer_port_random_low, &i))
    854         session->randomPortLow = i;
    855     if (tr_variantDictFindInt (settings, TR_KEY_peer_port_random_high, &i))
    856         session->randomPortHigh = i;
    857     if (tr_variantDictFindBool (settings, TR_KEY_peer_port_random_on_start, &boolVal))
    858         tr_sessionSetPeerPortRandomOnStart (session, boolVal);
    859     if (!tr_variantDictFindInt (settings, TR_KEY_peer_port, &i))
    860         i = session->private_peer_port;
    861     setPeerPort (session, boolVal ? getRandomPort (session) : i);
    862     if (tr_variantDictFindBool (settings, TR_KEY_port_forwarding_enabled, &boolVal))
    863         tr_sessionSetPortForwardingEnabled (session, boolVal);
    864 
    865     if (tr_variantDictFindInt (settings, TR_KEY_peer_limit_global, &i))
    866         session->peerLimit = i;
    867 
    868     /**
    869     **/
    870 
    871     if (tr_variantDictFindInt (settings, TR_KEY_upload_slots_per_torrent, &i))
    872         session->uploadSlotsPerTorrent = i;
    873 
    874     if (tr_variantDictFindInt (settings, TR_KEY_speed_limit_up, &i))
    875         tr_sessionSetSpeedLimit_KBps (session, TR_UP, i);
    876     if (tr_variantDictFindBool (settings, TR_KEY_speed_limit_up_enabled, &boolVal))
    877         tr_sessionLimitSpeed (session, TR_UP, boolVal);
    878 
    879     if (tr_variantDictFindInt (settings, TR_KEY_speed_limit_down, &i))
    880         tr_sessionSetSpeedLimit_KBps (session, TR_DOWN, i);
    881     if (tr_variantDictFindBool (settings, TR_KEY_speed_limit_down_enabled, &boolVal))
    882         tr_sessionLimitSpeed (session, TR_DOWN, boolVal);
    883 
    884     if (tr_variantDictFindReal (settings, TR_KEY_ratio_limit, &d))
    885         tr_sessionSetRatioLimit (session, d);
    886     if (tr_variantDictFindBool (settings, TR_KEY_ratio_limit_enabled, &boolVal))
    887         tr_sessionSetRatioLimited (session, boolVal);
    888 
    889     if (tr_variantDictFindInt (settings, TR_KEY_idle_seeding_limit, &i))
    890         tr_sessionSetIdleLimit (session, i);
    891     if (tr_variantDictFindBool (settings, TR_KEY_idle_seeding_limit_enabled, &boolVal))
    892         tr_sessionSetIdleLimited (session, boolVal);
    893 
    894     /**
    895     ***  Turtle Mode
    896     **/
    897 
    898     /* update the turtle mode's fields */
    899     if (tr_variantDictFindInt (settings, TR_KEY_alt_speed_up, &i))
    900         turtle->speedLimit_Bps[TR_UP] = toSpeedBytes (i);
    901     if (tr_variantDictFindInt (settings, TR_KEY_alt_speed_down, &i))
    902         turtle->speedLimit_Bps[TR_DOWN] = toSpeedBytes (i);
    903     if (tr_variantDictFindInt (settings, TR_KEY_alt_speed_time_begin, &i))
    904         turtle->beginMinute = i;
    905     if (tr_variantDictFindInt (settings, TR_KEY_alt_speed_time_end, &i))
    906         turtle->endMinute = i;
    907     if (tr_variantDictFindInt (settings, TR_KEY_alt_speed_time_day, &i))
    908         turtle->days = i;
    909     if (tr_variantDictFindBool (settings, TR_KEY_alt_speed_time_enabled, &boolVal))
    910         turtle->isClockEnabled = boolVal;
    911     if (tr_variantDictFindBool (settings, TR_KEY_alt_speed_enabled, &boolVal))
    912         turtle->isEnabled = boolVal;
    913     turtleBootstrap (session, turtle);
    914 
    915     /**
    916     ***  Scripts
    917     **/
    918 
    919     if (tr_variantDictFindBool (settings, TR_KEY_script_torrent_done_enabled, &boolVal))
    920         tr_sessionSetTorrentDoneScriptEnabled (session, boolVal);
    921     if (tr_variantDictFindStr (settings, TR_KEY_script_torrent_done_filename, &str, NULL))
    922         tr_sessionSetTorrentDoneScript (session, str);
    923 
    924 
    925     if (tr_variantDictFindBool (settings, TR_KEY_scrape_paused_torrents_enabled, &boolVal))
    926         session->scrapePausedTorrents = boolVal;
    927 
    928     data->done = true;
     759  int64_t i;
     760  double  d;
     761  bool boolVal;
     762  const char * str;
     763  struct tr_bindinfo b;
     764  struct init_data * data = vdata;
     765  tr_session * session = data->session;
     766  tr_variant * settings = data->clientSettings;
     767  struct tr_turtle_info * turtle = &session->turtle;
     768
     769  assert (tr_isSession (session));
     770  assert (tr_variantIsDict (settings));
     771  assert (tr_amInEventThread (session));
     772
     773  if (tr_variantDictFindInt (settings, TR_KEY_message_level, &i))
     774    tr_logSetLevel (i);
     775
     776  if (tr_variantDictFindInt (settings, TR_KEY_umask, &i))
     777    {
     778      session->umask = (mode_t)i;
     779      umask (session->umask);
     780    }
     781
     782  /* misc features */
     783  if (tr_variantDictFindInt (settings, TR_KEY_cache_size_mb, &i))
     784    tr_sessionSetCacheLimit_MB (session, i);
     785  if (tr_variantDictFindInt (settings, TR_KEY_peer_limit_per_torrent, &i))
     786    tr_sessionSetPeerLimitPerTorrent (session, i);
     787  if (tr_variantDictFindBool (settings, TR_KEY_pex_enabled, &boolVal))
     788    tr_sessionSetPexEnabled (session, boolVal);
     789  if (tr_variantDictFindBool (settings, TR_KEY_dht_enabled, &boolVal))
     790    tr_sessionSetDHTEnabled (session, boolVal);
     791  if (tr_variantDictFindBool (settings, TR_KEY_utp_enabled, &boolVal))
     792    tr_sessionSetUTPEnabled (session, boolVal);
     793  if (tr_variantDictFindBool (settings, TR_KEY_lpd_enabled, &boolVal))
     794    tr_sessionSetLPDEnabled (session, boolVal);
     795  if (tr_variantDictFindInt (settings, TR_KEY_encryption, &i))
     796    tr_sessionSetEncryption (session, i);
     797  if (tr_variantDictFindStr (settings, TR_KEY_peer_socket_tos, &str, NULL))
     798    session->peerSocketTOS = parse_tos (str);
     799  if (tr_variantDictFindStr (settings, TR_KEY_peer_congestion_algorithm, &str, NULL))
     800    session->peer_congestion_algorithm = tr_strdup (str);
     801  else
     802    session->peer_congestion_algorithm = tr_strdup ("");
     803  if (tr_variantDictFindBool (settings, TR_KEY_blocklist_enabled, &boolVal))
     804    tr_blocklistSetEnabled (session, boolVal);
     805  if (tr_variantDictFindStr (settings, TR_KEY_blocklist_url, &str, NULL))
     806    tr_blocklistSetURL (session, str);
     807  if (tr_variantDictFindBool (settings, TR_KEY_start_added_torrents, &boolVal))
     808    tr_sessionSetPaused (session, !boolVal);
     809  if (tr_variantDictFindBool (settings, TR_KEY_trash_original_torrent_files, &boolVal))
     810    tr_sessionSetDeleteSource (session, boolVal);
     811  if (tr_variantDictFindInt (settings, TR_KEY_peer_id_ttl_hours, &i))
     812    session->peer_id_ttl_hours = i;
     813
     814  /* torrent queues */
     815  if (tr_variantDictFindInt (settings, TR_KEY_queue_stalled_minutes, &i))
     816    tr_sessionSetQueueStalledMinutes (session, i);
     817  if (tr_variantDictFindBool (settings, TR_KEY_queue_stalled_enabled, &boolVal))
     818    tr_sessionSetQueueStalledEnabled (session, boolVal);
     819  if (tr_variantDictFindInt (settings, TR_KEY_download_queue_size, &i))
     820    tr_sessionSetQueueSize (session, TR_DOWN, i);
     821  if (tr_variantDictFindBool (settings, TR_KEY_download_queue_enabled, &boolVal))
     822    tr_sessionSetQueueEnabled (session, TR_DOWN, boolVal);
     823  if (tr_variantDictFindInt (settings, TR_KEY_seed_queue_size, &i))
     824    tr_sessionSetQueueSize (session, TR_UP, i);
     825  if (tr_variantDictFindBool (settings, TR_KEY_seed_queue_enabled, &boolVal))
     826    tr_sessionSetQueueEnabled (session, TR_UP, boolVal);
     827
     828  /* files and directories */
     829  if (tr_variantDictFindBool (settings, TR_KEY_prefetch_enabled, &boolVal))
     830    session->isPrefetchEnabled = boolVal;
     831  if (tr_variantDictFindInt (settings, TR_KEY_preallocation, &i))
     832    session->preallocationMode = i;
     833  if (tr_variantDictFindStr (settings, TR_KEY_download_dir, &str, NULL))
     834    tr_sessionSetDownloadDir (session, str);
     835  if (tr_variantDictFindStr (settings, TR_KEY_incomplete_dir, &str, NULL))
     836    tr_sessionSetIncompleteDir (session, str);
     837  if (tr_variantDictFindBool (settings, TR_KEY_incomplete_dir_enabled, &boolVal))
     838    tr_sessionSetIncompleteDirEnabled (session, boolVal);
     839  if (tr_variantDictFindBool (settings, TR_KEY_rename_partial_files, &boolVal))
     840    tr_sessionSetIncompleteFileNamingEnabled (session, boolVal);
     841
     842  /* rpc server */
     843  if (session->rpcServer != NULL) /* close the old one */
     844    tr_rpcClose (&session->rpcServer);
     845  session->rpcServer = tr_rpcInit (session, settings);
     846
     847  /* public addresses */
     848
     849  free_incoming_peer_port (session);
     850
     851  tr_variantDictFindStr (settings, TR_KEY_bind_address_ipv4, &str, NULL);
     852  if (!tr_address_from_string (&b.addr, str) || (b.addr.type != TR_AF_INET))
     853    b.addr = tr_inaddr_any;
     854  b.socket = -1;
     855  session->public_ipv4 = tr_memdup (&b, sizeof (struct tr_bindinfo));
     856
     857  tr_variantDictFindStr (settings, TR_KEY_bind_address_ipv6, &str, NULL);
     858  if (!tr_address_from_string (&b.addr, str) || (b.addr.type != TR_AF_INET6))
     859    b.addr = tr_in6addr_any;
     860  b.socket = -1;
     861  session->public_ipv6 = tr_memdup (&b, sizeof (struct tr_bindinfo));
     862
     863  /* incoming peer port */
     864  if (tr_variantDictFindInt (settings, TR_KEY_peer_port_random_low, &i))
     865    session->randomPortLow = i;
     866  if (tr_variantDictFindInt (settings, TR_KEY_peer_port_random_high, &i))
     867    session->randomPortHigh = i;
     868  if (tr_variantDictFindBool (settings, TR_KEY_peer_port_random_on_start, &boolVal))
     869    tr_sessionSetPeerPortRandomOnStart (session, boolVal);
     870  if (!tr_variantDictFindInt (settings, TR_KEY_peer_port, &i))
     871    i = session->private_peer_port;
     872  setPeerPort (session, boolVal ? getRandomPort (session) : i);
     873  if (tr_variantDictFindBool (settings, TR_KEY_port_forwarding_enabled, &boolVal))
     874    tr_sessionSetPortForwardingEnabled (session, boolVal);
     875
     876  if (tr_variantDictFindInt (settings, TR_KEY_peer_limit_global, &i))
     877    session->peerLimit = i;
     878
     879  /**
     880  **/
     881
     882  if (tr_variantDictFindInt (settings, TR_KEY_upload_slots_per_torrent, &i))
     883    session->uploadSlotsPerTorrent = i;
     884
     885  if (tr_variantDictFindInt (settings, TR_KEY_speed_limit_up, &i))
     886    tr_sessionSetSpeedLimit_KBps (session, TR_UP, i);
     887  if (tr_variantDictFindBool (settings, TR_KEY_speed_limit_up_enabled, &boolVal))
     888    tr_sessionLimitSpeed (session, TR_UP, boolVal);
     889
     890  if (tr_variantDictFindInt (settings, TR_KEY_speed_limit_down, &i))
     891    tr_sessionSetSpeedLimit_KBps (session, TR_DOWN, i);
     892  if (tr_variantDictFindBool (settings, TR_KEY_speed_limit_down_enabled, &boolVal))
     893    tr_sessionLimitSpeed (session, TR_DOWN, boolVal);
     894
     895  if (tr_variantDictFindReal (settings, TR_KEY_ratio_limit, &d))
     896    tr_sessionSetRatioLimit (session, d);
     897  if (tr_variantDictFindBool (settings, TR_KEY_ratio_limit_enabled, &boolVal))
     898    tr_sessionSetRatioLimited (session, boolVal);
     899
     900  if (tr_variantDictFindInt (settings, TR_KEY_idle_seeding_limit, &i))
     901    tr_sessionSetIdleLimit (session, i);
     902  if (tr_variantDictFindBool (settings, TR_KEY_idle_seeding_limit_enabled, &boolVal))
     903    tr_sessionSetIdleLimited (session, boolVal);
     904
     905  /**
     906  ***  Turtle Mode
     907  **/
     908
     909  /* update the turtle mode's fields */
     910  if (tr_variantDictFindInt (settings, TR_KEY_alt_speed_up, &i))
     911    turtle->speedLimit_Bps[TR_UP] = toSpeedBytes (i);
     912  if (tr_variantDictFindInt (settings, TR_KEY_alt_speed_down, &i))
     913    turtle->speedLimit_Bps[TR_DOWN] = toSpeedBytes (i);
     914  if (tr_variantDictFindInt (settings, TR_KEY_alt_speed_time_begin, &i))
     915    turtle->beginMinute = i;
     916  if (tr_variantDictFindInt (settings, TR_KEY_alt_speed_time_end, &i))
     917    turtle->endMinute = i;
     918  if (tr_variantDictFindInt (settings, TR_KEY_alt_speed_time_day, &i))
     919    turtle->days = i;
     920  if (tr_variantDictFindBool (settings, TR_KEY_alt_speed_time_enabled, &boolVal))
     921    turtle->isClockEnabled = boolVal;
     922  if (tr_variantDictFindBool (settings, TR_KEY_alt_speed_enabled, &boolVal))
     923    turtle->isEnabled = boolVal;
     924  turtleBootstrap (session, turtle);
     925
     926  /**
     927  ***  Scripts
     928  **/
     929
     930  if (tr_variantDictFindBool (settings, TR_KEY_script_torrent_done_enabled, &boolVal))
     931    tr_sessionSetTorrentDoneScriptEnabled (session, boolVal);
     932  if (tr_variantDictFindStr (settings, TR_KEY_script_torrent_done_filename, &str, NULL))
     933    tr_sessionSetTorrentDoneScript (session, str);
     934
     935
     936  if (tr_variantDictFindBool (settings, TR_KEY_scrape_paused_torrents_enabled, &boolVal))
     937    session->scrapePausedTorrents = boolVal;
     938
     939  data->done = true;
    929940}
    930941
     
    9931004tr_sessionSetIncompleteFileNamingEnabled (tr_session * session, bool b)
    9941005{
    995     assert (tr_isSession (session));
    996     assert (tr_isBool (b));
    997 
    998     session->isIncompleteFileNamingEnabled = b;
     1006  assert (tr_isSession (session));
     1007  assert (tr_isBool (b));
     1008
     1009  session->isIncompleteFileNamingEnabled = b;
    9991010}
    10001011
     
    10021013tr_sessionIsIncompleteFileNamingEnabled (const tr_session * session)
    10031014{
    1004     assert (tr_isSession (session));
    1005 
    1006     return session->isIncompleteFileNamingEnabled;
     1015  assert (tr_isSession (session));
     1016
     1017  return session->isIncompleteFileNamingEnabled;
    10071018}
    10081019
     
    10151026tr_sessionSetIncompleteDir (tr_session * session, const char * dir)
    10161027{
    1017     assert (tr_isSession (session));
    1018 
    1019     if (session->incompleteDir != dir)
    1020     {
    1021         tr_free (session->incompleteDir);
    1022 
    1023         session->incompleteDir = tr_strdup (dir);
     1028  assert (tr_isSession (session));
     1029
     1030  if (session->incompleteDir != dir)
     1031    {
     1032      tr_free (session->incompleteDir);
     1033
     1034      session->incompleteDir = tr_strdup (dir);
    10241035    }
    10251036}
     
    10281039tr_sessionGetIncompleteDir (const tr_session * session)
    10291040{
    1030     assert (tr_isSession (session));
    1031 
    1032     return session->incompleteDir;
     1041  assert (tr_isSession (session));
     1042
     1043  return session->incompleteDir;
    10331044}
    10341045
     
    10361047tr_sessionSetIncompleteDirEnabled (tr_session * session, bool b)
    10371048{
    1038     assert (tr_isSession (session));
    1039     assert (tr_isBool (b));
    1040 
    1041     session->isIncompleteDirEnabled = b;
     1049  assert (tr_isSession (session));
     1050  assert (tr_isBool (b));
     1051
     1052  session->isIncompleteDirEnabled = b;
    10421053}
    10431054
     
    10451056tr_sessionIsIncompleteDirEnabled (const tr_session * session)
    10461057{
    1047     assert (tr_isSession (session));
    1048 
    1049     return session->isIncompleteDirEnabled;
     1058  assert (tr_isSession (session));
     1059
     1060  return session->isIncompleteDirEnabled;
    10501061}
    10511062
     
    10571068tr_sessionLock (tr_session * session)
    10581069{
    1059     assert (tr_isSession (session));
    1060 
    1061     tr_lockLock (session->lock);
     1070  assert (tr_isSession (session));
     1071
     1072  tr_lockLock (session->lock);
    10621073}
    10631074
     
    10651076tr_sessionUnlock (tr_session * session)
    10661077{
    1067     assert (tr_isSession (session));
    1068 
    1069     tr_lockUnlock (session->lock);
     1078  assert (tr_isSession (session));
     1079
     1080  tr_lockUnlock (session->lock);
    10701081}
    10711082
     
    10731084tr_sessionIsLocked (const tr_session * session)
    10741085{
    1075     return tr_isSession (session) && tr_lockHave (session->lock);
    1076 }
    1077 
    1078 /***********************************************************************
    1079  * tr_setBindPort
    1080  ***********************************************************************
    1081  *
    1082  **********************************************************************/
     1086  return tr_isSession (session) && tr_lockHave (session->lock);
     1087}
     1088
     1089/***
     1090****  Peer Port
     1091***/
    10831092
    10841093static void
    10851094peerPortChanged (void * session)
    10861095{
    1087     tr_torrent * tor = NULL;
    1088 
    1089     assert (tr_isSession (session));
    1090 
    1091     close_incoming_peer_port (session);
    1092     open_incoming_peer_port (session);
    1093     tr_sharedPortChanged (session);
    1094 
    1095     while ((tor = tr_torrentNext (session, tor)))
    1096         tr_torrentChangeMyPort (tor);
     1096  tr_torrent * tor = NULL;
     1097
     1098  assert (tr_isSession (session));
     1099
     1100  close_incoming_peer_port (session);
     1101  open_incoming_peer_port (session);
     1102  tr_sharedPortChanged (session);
     1103
     1104  while ((tor = tr_torrentNext (session, tor)))
     1105    tr_torrentChangeMyPort (tor);
    10971106}
    10981107
     
    11001109setPeerPort (tr_session * session, tr_port port)
    11011110{
    1102     session->private_peer_port = port;
    1103     session->public_peer_port = port;
    1104 
    1105     tr_runInEventThread (session, peerPortChanged, session);
     1111  session->private_peer_port = port;
     1112  session->public_peer_port = port;
     1113
     1114  tr_runInEventThread (session, peerPortChanged, session);
    11061115}
    11071116
     
    11091118tr_sessionSetPeerPort (tr_session * session, tr_port port)
    11101119{
    1111     if (tr_isSession (session) && (session->private_peer_port != port))
    1112     {
    1113         setPeerPort (session, port);
     1120  if (tr_isSession (session) && (session->private_peer_port != port))
     1121    {
     1122      setPeerPort (session, port);
    11141123    }
    11151124}
     
    11181127tr_sessionGetPeerPort (const tr_session * session)
    11191128{
    1120     return tr_isSession (session) ? session->private_peer_port : 0;
     1129  return tr_isSession (session) ? session->private_peer_port : 0;
    11211130}
    11221131
     
    11241133tr_sessionSetPeerPortRandom (tr_session * session)
    11251134{
    1126     assert (tr_isSession (session));
    1127 
    1128     tr_sessionSetPeerPort (session, getRandomPort (session));
    1129     return session->private_peer_port;
     1135  assert (tr_isSession (session));
     1136
     1137  tr_sessionSetPeerPort (session, getRandomPort (session));
     1138  return session->private_peer_port;
    11301139}
    11311140
     
    11341143                                    bool random)
    11351144{
    1136     assert (tr_isSession (session));
    1137 
    1138     session->isPortRandom = random;
     1145  assert (tr_isSession (session));
     1146
     1147  session->isPortRandom = random;
    11391148}
    11401149
     
    11421151tr_sessionGetPeerPortRandomOnStart (tr_session * session)
    11431152{
    1144     assert (tr_isSession (session));
    1145 
    1146     return session->isPortRandom;
     1153  assert (tr_isSession (session));
     1154
     1155  return session->isPortRandom;
    11471156}
    11481157
     
    11501159tr_sessionGetPortForwarding (const tr_session * session)
    11511160{
    1152     assert (tr_isSession (session));
    1153 
    1154     return tr_sharedTraversalStatus (session->shared);
     1161  assert (tr_isSession (session));
     1162
     1163  return tr_sharedTraversalStatus (session->shared);
    11551164}
    11561165
     
    11621171tr_sessionSetRatioLimited (tr_session * session, bool isLimited)
    11631172{
    1164     assert (tr_isSession (session));
    1165 
    1166     session->isRatioLimited = isLimited;
     1173  assert (tr_isSession (session));
     1174
     1175  session->isRatioLimited = isLimited;
    11671176}
    11681177
     
    11701179tr_sessionSetRatioLimit (tr_session * session, double desiredRatio)
    11711180{
    1172     assert (tr_isSession (session));
    1173 
    1174     session->desiredRatio = desiredRatio;
     1181  assert (tr_isSession (session));
     1182
     1183  session->desiredRatio = desiredRatio;
    11751184}
    11761185
     
    11781187tr_sessionIsRatioLimited (const tr_session  * session)
    11791188{
    1180     assert (tr_isSession (session));
    1181 
    1182     return session->isRatioLimited;
     1189  assert (tr_isSession (session));
     1190
     1191  return session->isRatioLimited;
    11831192}
    11841193
     
    11861195tr_sessionGetRatioLimit (const tr_session * session)
    11871196{
    1188     assert (tr_isSession (session));
    1189 
    1190     return session->desiredRatio;
     1197  assert (tr_isSession (session));
     1198
     1199  return session->desiredRatio;
    11911200}
    11921201
     
    11981207tr_sessionSetIdleLimited (tr_session * session, bool isLimited)
    11991208{
    1200     assert (tr_isSession (session));
    1201 
    1202     session->isIdleLimited = isLimited;
     1209  assert (tr_isSession (session));
     1210
     1211  session->isIdleLimited = isLimited;
    12031212}
    12041213
     
    12061215tr_sessionSetIdleLimit (tr_session * session, uint16_t idleMinutes)
    12071216{
    1208     assert (tr_isSession (session));
    1209 
    1210     session->idleLimitMinutes = idleMinutes;
     1217  assert (tr_isSession (session));
     1218
     1219  session->idleLimitMinutes = idleMinutes;
    12111220}
    12121221
     
    12141223tr_sessionIsIdleLimited (const tr_session  * session)
    12151224{
    1216     assert (tr_isSession (session));
    1217 
    1218     return session->isIdleLimited;
     1225  assert (tr_isSession (session));
     1226
     1227  return session->isIdleLimited;
    12191228}
    12201229
     
    12221231tr_sessionGetIdleLimit (const tr_session * session)
    12231232{
    1224     assert (tr_isSession (session));
    1225 
    1226     return session->idleLimitMinutes;
     1233  assert (tr_isSession (session));
     1234
     1235  return session->idleLimitMinutes;
    12271236}
    12281237
     
    12361245tr_sessionGetActiveSpeedLimit_Bps (const tr_session * session, tr_direction dir, unsigned int * setme_Bps)
    12371246{
    1238     int isLimited = true;
    1239 
    1240     if (!tr_isSession (session))
    1241         return false;
    1242 
    1243     if (tr_sessionUsesAltSpeed (session))
    1244         *setme_Bps = tr_sessionGetAltSpeed_Bps (session, dir);
    1245     else if (tr_sessionIsSpeedLimited (session, dir))
    1246         *setme_Bps = tr_sessionGetSpeedLimit_Bps (session, dir);
    1247     else
    1248         isLimited = false;
    1249 
    1250     return isLimited;
     1247  int isLimited = true;
     1248
     1249  if (!tr_isSession (session))
     1250    return false;
     1251
     1252  if (tr_sessionUsesAltSpeed (session))
     1253    *setme_Bps = tr_sessionGetAltSpeed_Bps (session, dir);
     1254  else if (tr_sessionIsSpeedLimited (session, dir))
     1255    *setme_Bps = tr_sessionGetSpeedLimit_Bps (session, dir);
     1256  else
     1257    isLimited = false;
     1258
     1259  return isLimited;
    12511260}
    12521261bool
     
    12551264                                    double            * setme_KBps)
    12561265{
    1257     unsigned int Bps = 0;
    1258     const bool is_active = tr_sessionGetActiveSpeedLimit_Bps (session, dir, &Bps);
    1259     *setme_KBps = toSpeedKBps (Bps);
    1260     return is_active;
     1266  unsigned int Bps = 0;
     1267  const bool is_active = tr_sessionGetActiveSpeedLimit_Bps (session, dir, &Bps);
     1268  *setme_KBps = toSpeedKBps (Bps);
     1269  return is_active;
    12611270}
    12621271
     
    12641273updateBandwidth (tr_session * session, tr_direction dir)
    12651274{
    1266     unsigned int limit_Bps = 0;
    1267     const bool isLimited = tr_sessionGetActiveSpeedLimit_Bps (session, dir, &limit_Bps);
    1268     const bool zeroCase = isLimited && !limit_Bps;
    1269 
    1270     tr_bandwidthSetLimited (&session->bandwidth, dir, isLimited && !zeroCase);
    1271 
    1272     tr_bandwidthSetDesiredSpeed_Bps (&session->bandwidth, dir, limit_Bps);
     1275  unsigned int limit_Bps = 0;
     1276  const bool isLimited = tr_sessionGetActiveSpeedLimit_Bps (session, dir, &limit_Bps);
     1277  const bool zeroCase = isLimited && !limit_Bps;
     1278
     1279  tr_bandwidthSetLimited (&session->bandwidth, dir, isLimited && !zeroCase);
     1280
     1281  tr_bandwidthSetDesiredSpeed_Bps (&session->bandwidth, dir, limit_Bps);
    12731282}
    12741283
    12751284enum
    12761285{
    1277     MINUTES_PER_HOUR = 60,
    1278     MINUTES_PER_DAY = MINUTES_PER_HOUR * 24,
    1279     MINUTES_PER_WEEK = MINUTES_PER_DAY * 7
     1286  MINUTES_PER_HOUR = 60,
     1287  MINUTES_PER_DAY = MINUTES_PER_HOUR * 24,
     1288  MINUTES_PER_WEEK = MINUTES_PER_DAY * 7
    12801289};
    12811290
     
    12831292turtleUpdateTable (struct tr_turtle_info * t)
    12841293{
    1285     int day;
    1286     tr_bitfield * b = &t->minutes;
    1287 
    1288     tr_bitfieldSetHasNone (b);
    1289 
    1290     for (day=0; day<7; ++day)
    1291     {
    1292         if (t->days & (1<<day))
     1294  int day;
     1295  tr_bitfield * b = &t->minutes;
     1296
     1297  tr_bitfieldSetHasNone (b);
     1298
     1299  for (day=0; day<7; ++day)
     1300    {
     1301      if (t->days & (1<<day))
    12931302        {
    1294             int i;
    1295             const time_t begin = t->beginMinute;
    1296             time_t end = t->endMinute;
    1297 
    1298             if (end <= begin)
    1299                 end += MINUTES_PER_DAY;
    1300 
    1301             for (i=begin; i<end; ++i)
    1302                 tr_bitfieldAdd (b, (i+day*MINUTES_PER_DAY) % MINUTES_PER_WEEK);
     1303          int i;
     1304          const time_t begin = t->beginMinute;
     1305          time_t end = t->endMinute;
     1306
     1307          if (end <= begin)
     1308            end += MINUTES_PER_DAY;
     1309
     1310          for (i=begin; i<end; ++i)
     1311            tr_bitfieldAdd (b, (i+day*MINUTES_PER_DAY) % MINUTES_PER_WEEK);
    13031312        }
    13041313    }
     
    13081317altSpeedToggled (void * vsession)
    13091318{
    1310     tr_session * session = vsession;
    1311     struct tr_turtle_info * t = &session->turtle;
    1312 
    1313     assert (tr_isSession (session));
    1314 
    1315     updateBandwidth (session, TR_UP);
    1316     updateBandwidth (session, TR_DOWN);
    1317 
    1318     if (t->callback != NULL)
    1319       (*t->callback)(session, t->isEnabled, t->changedByUser, t->callbackUserData);
     1319  tr_session * session = vsession;
     1320  struct tr_turtle_info * t = &session->turtle;
     1321
     1322  assert (tr_isSession (session));
     1323
     1324  updateBandwidth (session, TR_UP);
     1325  updateBandwidth (session, TR_DOWN);
     1326
     1327  if (t->callback != NULL)
     1328    (*t->callback)(session, t->isEnabled, t->changedByUser, t->callbackUserData);
    13201329}
    13211330
     
    13241333             bool enabled, bool byUser)
    13251334{
    1326     assert (tr_isSession (s));
    1327     assert (t != NULL);
    1328     assert (tr_isBool (enabled));
    1329     assert (tr_isBool (byUser));
    1330 
    1331     if (t->isEnabled != enabled)
    1332     {
    1333         t->isEnabled = enabled;
    1334         t->changedByUser = byUser;
    1335         tr_runInEventThread (s, altSpeedToggled, s);
     1335  assert (tr_isSession (s));
     1336  assert (t != NULL);
     1337  assert (tr_isBool (enabled));
     1338  assert (tr_isBool (byUser));
     1339
     1340  if (t->isEnabled != enabled)
     1341    {
     1342      t->isEnabled = enabled;
     1343      t->changedByUser = byUser;
     1344      tr_runInEventThread (s, altSpeedToggled, s);
    13361345    }
    13371346}
     
    13431352getInTurtleTime (const struct tr_turtle_info * t)
    13441353{
    1345     struct tm tm;
    1346     size_t minute_of_the_week;
    1347     const time_t now = tr_time ();
    1348 
    1349     tr_localtime_r (&now, &tm);
    1350 
    1351     minute_of_the_week = tm.tm_wday * MINUTES_PER_DAY
    1352                        + tm.tm_hour * MINUTES_PER_HOUR
    1353                        + tm.tm_min;
    1354     if (minute_of_the_week >= MINUTES_PER_WEEK) /* leap minutes? */
    1355         minute_of_the_week = MINUTES_PER_WEEK - 1;
    1356 
    1357     return tr_bitfieldHas (&t->minutes, minute_of_the_week);
     1354  struct tm tm;
     1355  size_t minute_of_the_week;
     1356  const time_t now = tr_time ();
     1357
     1358  tr_localtime_r (&now, &tm);
     1359
     1360  minute_of_the_week = tm.tm_wday * MINUTES_PER_DAY
     1361                     + tm.tm_hour * MINUTES_PER_HOUR
     1362                     + tm.tm_min;
     1363  if (minute_of_the_week >= MINUTES_PER_WEEK) /* leap minutes? */
     1364    minute_of_the_week = MINUTES_PER_WEEK - 1;
     1365
     1366  return tr_bitfieldHas (&t->minutes, minute_of_the_week);
    13581367}
    13591368
     
    13671376turtleCheckClock (tr_session * s, struct tr_turtle_info * t)
    13681377{
    1369     bool enabled;
    1370     bool alreadySwitched;
    1371     tr_auto_switch_state_t newAutoTurtleState;
    1372 
    1373     assert (t->isClockEnabled);
    1374 
    1375     enabled = getInTurtleTime (t);
    1376     newAutoTurtleState = autoSwitchState (enabled);
    1377     alreadySwitched = (t->autoTurtleState == newAutoTurtleState);
    1378 
    1379     if (!alreadySwitched)
    1380     {
    1381         tr_logAddInfo ("Time to turn %s turtle mode!", (enabled?"on":"off"));
    1382         t->autoTurtleState = newAutoTurtleState;
    1383         useAltSpeed (s, t, enabled, false);
     1378  bool enabled;
     1379  bool alreadySwitched;
     1380  tr_auto_switch_state_t newAutoTurtleState;
     1381
     1382  assert (t->isClockEnabled);
     1383
     1384  enabled = getInTurtleTime (t);
     1385  newAutoTurtleState = autoSwitchState (enabled);
     1386  alreadySwitched = (t->autoTurtleState == newAutoTurtleState);
     1387
     1388  if (!alreadySwitched)
     1389    {
     1390      tr_logAddInfo ("Time to turn %s turtle mode!", (enabled?"on":"off"));
     1391      t->autoTurtleState = newAutoTurtleState;
     1392      useAltSpeed (s, t, enabled, false);
    13841393    }
    13851394}
     
    13911400turtleBootstrap (tr_session * session, struct tr_turtle_info * turtle)
    13921401{
    1393     turtle->changedByUser = false;
    1394     turtle->autoTurtleState = TR_AUTO_SWITCH_UNUSED;
    1395 
    1396     tr_bitfieldConstruct (&turtle->minutes, MINUTES_PER_WEEK);
    1397 
    1398     turtleUpdateTable (turtle);
    1399 
    1400     if (turtle->isClockEnabled)
    1401     {
    1402         turtle->isEnabled = getInTurtleTime (turtle);
    1403         turtle->autoTurtleState = autoSwitchState (turtle->isEnabled);
    1404     }
    1405 
    1406     altSpeedToggled (session);
     1402  turtle->changedByUser = false;
     1403  turtle->autoTurtleState = TR_AUTO_SWITCH_UNUSED;
     1404
     1405  tr_bitfieldConstruct (&turtle->minutes, MINUTES_PER_WEEK);
     1406
     1407  turtleUpdateTable (turtle);
     1408
     1409  if (turtle->isClockEnabled)
     1410    {
     1411      turtle->isEnabled = getInTurtleTime (turtle);
     1412      turtle->autoTurtleState = autoSwitchState (turtle->isEnabled);
     1413    }
     1414
     1415  altSpeedToggled (session);
    14071416
    14081417}
     
    14151424tr_sessionSetSpeedLimit_Bps (tr_session * s, tr_direction d, unsigned int Bps)
    14161425{
    1417     assert (tr_isSession (s));
    1418     assert (tr_isDirection (d));
    1419 
    1420     s->speedLimit_Bps[d] = Bps;
    1421 
    1422     updateBandwidth (s, d);
     1426  assert (tr_isSession (s));
     1427  assert (tr_isDirection (d));
     1428
     1429  s->speedLimit_Bps[d] = Bps;
     1430
     1431  updateBandwidth (s, d);
    14231432}
    14241433void
    14251434tr_sessionSetSpeedLimit_KBps (tr_session * s, tr_direction d, unsigned int KBps)
    14261435{
    1427     tr_sessionSetSpeedLimit_Bps (s, d, toSpeedBytes (KBps));
     1436  tr_sessionSetSpeedLimit_Bps (s, d, toSpeedBytes (KBps));
    14281437}
    14291438
     
    14311440tr_sessionGetSpeedLimit_Bps (const tr_session * s, tr_direction d)
    14321441{
    1433     assert (tr_isSession (s));
    1434     assert (tr_isDirection (d));
    1435 
    1436     return s->speedLimit_Bps[d];
     1442  assert (tr_isSession (s));
     1443  assert (tr_isDirection (d));
     1444
     1445  return s->speedLimit_Bps[d];
    14371446}
    14381447unsigned int
     
    14451454tr_sessionLimitSpeed (tr_session * s, tr_direction d, bool b)
    14461455{
    1447     assert (tr_isSession (s));
    1448     assert (tr_isDirection (d));
    1449     assert (tr_isBool (b));
    1450 
    1451     s->speedLimitEnabled[d] = b;
    1452 
    1453     updateBandwidth (s, d);
     1456  assert (tr_isSession (s));
     1457  assert (tr_isDirection (d));
     1458  assert (tr_isBool (b));
     1459
     1460  s->speedLimitEnabled[d] = b;
     1461
     1462  updateBandwidth (s, d);
    14541463}
    14551464
     
    14571466tr_sessionIsSpeedLimited (const tr_session * s, tr_direction d)
    14581467{
    1459     assert (tr_isSession (s));
    1460     assert (tr_isDirection (d));
    1461 
    1462     return s->speedLimitEnabled[d];
     1468  assert (tr_isSession (s));
     1469  assert (tr_isDirection (d));
     1470
     1471  return s->speedLimitEnabled[d];
    14631472}
    14641473
     
    14701479tr_sessionSetAltSpeed_Bps (tr_session * s, tr_direction d, unsigned int Bps)
    14711480{
    1472     assert (tr_isSession (s));
    1473     assert (tr_isDirection (d));
    1474 
    1475     s->turtle.speedLimit_Bps[d] = Bps;
    1476 
    1477     updateBandwidth (s, d);
     1481  assert (tr_isSession (s));
     1482  assert (tr_isDirection (d));
     1483
     1484  s->turtle.speedLimit_Bps[d] = Bps;
     1485
     1486  updateBandwidth (s, d);
    14781487}
    14791488
     
    14811490tr_sessionSetAltSpeed_KBps (tr_session * s, tr_direction d, unsigned int KBps)
    14821491{
    1483     tr_sessionSetAltSpeed_Bps (s, d, toSpeedBytes (KBps));
     1492  tr_sessionSetAltSpeed_Bps (s, d, toSpeedBytes (KBps));
    14841493}
    14851494
     
    14871496tr_sessionGetAltSpeed_Bps (const tr_session * s, tr_direction d)
    14881497{
    1489     assert (tr_isSession (s));
    1490     assert (tr_isDirection (d));
    1491 
    1492     return s->turtle.speedLimit_Bps[d];
     1498  assert (tr_isSession (s));
     1499  assert (tr_isDirection (d));
     1500
     1501  return s->turtle.speedLimit_Bps[d];
    14931502}
    14941503unsigned int
    14951504tr_sessionGetAltSpeed_KBps (const tr_session * s, tr_direction d)
    14961505{
    1497     return toSpeedKBps (tr_sessionGetAltSpeed_Bps (s, d));
     1506  return toSpeedKBps (tr_sessionGetAltSpeed_Bps (s, d));
    14981507}
    14991508
     
    15011510userPokedTheClock (tr_session * s, struct tr_turtle_info * t)
    15021511{
    1503     tr_logAddDebug ("Refreshing the turtle mode clock due to user changes");
    1504 
    1505     t->autoTurtleState = TR_AUTO_SWITCH_UNUSED;
    1506 
    1507     turtleUpdateTable (t);
    1508 
    1509     if (t->isClockEnabled)
    1510     {
    1511         const bool enabled = getInTurtleTime (t);
    1512         useAltSpeed (s, t, enabled, true);
    1513         t->autoTurtleState = autoSwitchState (enabled);
     1512  tr_logAddDebug ("Refreshing the turtle mode clock due to user changes");
     1513
     1514  t->autoTurtleState = TR_AUTO_SWITCH_UNUSED;
     1515
     1516  turtleUpdateTable (t);
     1517
     1518  if (t->isClockEnabled)
     1519    {
     1520      const bool enabled = getInTurtleTime (t);
     1521      useAltSpeed (s, t, enabled, true);
     1522      t->autoTurtleState = autoSwitchState (enabled);
    15141523    }
    15151524}
     
    15181527tr_sessionUseAltSpeedTime (tr_session * s, bool b)
    15191528{
    1520     struct tr_turtle_info * t = &s->turtle;
    1521 
    1522     assert (tr_isSession (s));
    1523     assert (tr_isBool (b));
    1524 
    1525     if (t->isClockEnabled != b) {
    1526         t->isClockEnabled = b;
    1527         userPokedTheClock (s, t);
     1529  struct tr_turtle_info * t = &s->turtle;
     1530
     1531  assert (tr_isSession (s));
     1532  assert (tr_isBool (b));
     1533
     1534  if (t->isClockEnabled != b)
     1535    {
     1536      t->isClockEnabled = b;
     1537      userPokedTheClock (s, t);
    15281538    }
    15291539}
     
    15321542tr_sessionUsesAltSpeedTime (const tr_session * s)
    15331543{
    1534     assert (tr_isSession (s));
    1535 
    1536     return s->turtle.isClockEnabled;
     1544  assert (tr_isSession (s));
     1545
     1546  return s->turtle.isClockEnabled;
    15371547}
    15381548
     
    15401550tr_sessionSetAltSpeedBegin (tr_session * s, int minute)
    15411551{
    1542     assert (tr_isSession (s));
    1543     assert (0<=minute && minute< (60*24));
    1544 
    1545     if (s->turtle.beginMinute != minute) {
    1546         s->turtle.beginMinute = minute;
    1547         userPokedTheClock (s, &s->turtle);
     1552  assert (tr_isSession (s));
     1553  assert (0<=minute && minute< (60*24));
     1554
     1555  if (s->turtle.beginMinute != minute)
     1556    {
     1557      s->turtle.beginMinute = minute;
     1558      userPokedTheClock (s, &s->turtle);
    15481559    }
    15491560}
     
    15521563tr_sessionGetAltSpeedBegin (const tr_session * s)
    15531564{
    1554     assert (tr_isSession (s));
    1555 
    1556     return s->turtle.beginMinute;
     1565  assert (tr_isSession (s));
     1566
     1567  return s->turtle.beginMinute;
    15571568}
    15581569
     
    15601571tr_sessionSetAltSpeedEnd (tr_session * s, int minute)
    15611572{
    1562     assert (tr_isSession (s));
    1563     assert (0<=minute && minute< (60*24));
    1564 
    1565     if (s->turtle.endMinute != minute) {
    1566         s->turtle.endMinute = minute;
    1567         userPokedTheClock (s, &s->turtle);
     1573  assert (tr_isSession (s));
     1574  assert (0<=minute && minute< (60*24));
     1575
     1576  if (s->turtle.endMinute != minute)
     1577    {
     1578      s->turtle.endMinute = minute;
     1579      userPokedTheClock (s, &s->turtle);
    15681580    }
    15691581}
     
    15721584tr_sessionGetAltSpeedEnd (const tr_session * s)
    15731585{
    1574     assert (tr_isSession (s));
    1575 
    1576     return s->turtle.endMinute;
     1586  assert (tr_isSession (s));
     1587
     1588  return s->turtle.endMinute;
    15771589}
    15781590
     
    15801592tr_sessionSetAltSpeedDay (tr_session * s, tr_sched_day days)
    15811593{
    1582     assert (tr_isSession (s));
    1583 
    1584     if (s->turtle.days != days) {
    1585         s->turtle.days = days;
    1586         userPokedTheClock (s, &s->turtle);
     1594  assert (tr_isSession (s));
     1595
     1596  if (s->turtle.days != days)
     1597    {
     1598      s->turtle.days = days;
     1599      userPokedTheClock (s, &s->turtle);
    15871600    }
    15881601}
     
    15911604tr_sessionGetAltSpeedDay (const tr_session * s)
    15921605{
    1593     assert (tr_isSession (s));
    1594 
    1595     return s->turtle.days;
     1606  assert (tr_isSession (s));
     1607
     1608  return s->turtle.days;
    15961609}
    15971610
     
    15991612tr_sessionUseAltSpeed (tr_session * session, bool enabled)
    16001613{
    1601     useAltSpeed (session, &session->turtle, enabled, true);
     1614  useAltSpeed (session, &session->turtle, enabled, true);
    16021615}
    16031616
     
    16051618tr_sessionUsesAltSpeed (const tr_session * s)
    16061619{
    1607     assert (tr_isSession (s));
    1608 
    1609     return s->turtle.isEnabled;
     1620  assert (tr_isSession (s));
     1621
     1622  return s->turtle.isEnabled;
    16101623}
    16111624
     
    16151628                           void             * userData)
    16161629{
    1617     assert (tr_isSession (session));
    1618 
    1619     session->turtle.callback = func;
    1620     session->turtle.callbackUserData = userData;
     1630  assert (tr_isSession (session));
     1631
     1632  session->turtle.callback = func;
     1633  session->turtle.callbackUserData = userData;
    16211634}
    16221635
     
    16241637tr_sessionClearAltSpeedFunc (tr_session * session)
    16251638{
    1626     tr_sessionSetAltSpeedFunc (session, NULL, NULL);
     1639  tr_sessionSetAltSpeedFunc (session, NULL, NULL);
    16271640}
    16281641
     
    16341647tr_sessionSetPeerLimit (tr_session * session, uint16_t n)
    16351648{
    1636     assert (tr_isSession (session));
    1637 
    1638     session->peerLimit = n;
     1649  assert (tr_isSession (session));
     1650
     1651  session->peerLimit = n;
    16391652}
    16401653
     
    16421655tr_sessionGetPeerLimit (const tr_session * session)
    16431656{
    1644     assert (tr_isSession (session));
    1645 
    1646     return session->peerLimit;
     1657  assert (tr_isSession (session));
     1658
     1659  return session->peerLimit;
    16471660}
    16481661
     
    16581671tr_sessionGetPeerLimitPerTorrent (const tr_session * session)
    16591672{
    1660     assert (tr_isSession (session));
    1661 
    1662     return session->peerLimitPerTorrent;
     1673  assert (tr_isSession (session));
     1674
     1675  return session->peerLimitPerTorrent;
    16631676}
    16641677
     
    16701683tr_sessionSetPaused (tr_session * session, bool isPaused)
    16711684{
    1672     assert (tr_isSession (session));
    1673 
    1674     session->pauseAddedTorrent = isPaused;
     1685  assert (tr_isSession (session));
     1686
     1687  session->pauseAddedTorrent = isPaused;
    16751688}
    16761689
     
    16781691tr_sessionGetPaused (const tr_session * session)
    16791692{
    1680     assert (tr_isSession (session));
    1681 
    1682     return session->pauseAddedTorrent;
     1693  assert (tr_isSession (session));
     1694
     1695  return session->pauseAddedTorrent;
    16831696}
    16841697
     
    16861699tr_sessionSetDeleteSource (tr_session * session, bool deleteSource)
    16871700{
    1688     assert (tr_isSession (session));
    1689 
    1690     session->deleteSourceTorrent = deleteSource;
     1701  assert (tr_isSession (session));
     1702
     1703  session->deleteSourceTorrent = deleteSource;
    16911704}
    16921705
     
    16941707tr_sessionGetDeleteSource (const tr_session * session)
    16951708{
    1696     assert (tr_isSession (session));
    1697 
    1698     return session->deleteSourceTorrent;
     1709  assert (tr_isSession (session));
     1710
     1711  return session->deleteSourceTorrent;
    16991712}
    17001713
     
    17061719tr_sessionGetPieceSpeed_Bps (const tr_session * session, tr_direction dir)
    17071720{
    1708     return tr_isSession (session) ? tr_bandwidthGetPieceSpeed_Bps (&session->bandwidth, 0, dir) : 0;
     1721  return tr_isSession (session) ? tr_bandwidthGetPieceSpeed_Bps (&session->bandwidth, 0, dir) : 0;
    17091722}
    17101723
     
    17121725tr_sessionGetRawSpeed_Bps (const tr_session * session, tr_direction dir)
    17131726{
    1714     return tr_isSession (session) ? tr_bandwidthGetRawSpeed_Bps (&session->bandwidth, 0, dir) : 0;
     1727  return tr_isSession (session) ? tr_bandwidthGetRawSpeed_Bps (&session->bandwidth, 0, dir) : 0;
    17151728}
    17161729double
    17171730tr_sessionGetRawSpeed_KBps (const tr_session * session, tr_direction dir)
    17181731{
    1719     return toSpeedKBps (tr_sessionGetRawSpeed_Bps (session, dir));
     1732  return toSpeedKBps (tr_sessionGetRawSpeed_Bps (session, dir));
    17201733}
    17211734
     
    17241737tr_sessionCountTorrents (const tr_session * session)
    17251738{
    1726     return tr_isSession (session) ? session->torrentCount : 0;
     1739  return tr_isSession (session) ? session->torrentCount : 0;
    17271740}
    17281741
     
    17301743compareTorrentByCur (const void * va, const void * vb)
    17311744{
    1732     const tr_torrent * a = * (const tr_torrent**)va;
    1733     const tr_torrent * b = * (const tr_torrent**)vb;
    1734     const uint64_t    aCur = a->downloadedCur + a->uploadedCur;
    1735     const uint64_t    bCur = b->downloadedCur + b->uploadedCur;
    1736 
    1737     if (aCur != bCur)
    1738         return aCur > bCur ? -1 : 1; /* close the biggest torrents first */
    1739 
    1740     return 0;
     1745  const tr_torrent * a = * (const tr_torrent**)va;
     1746  const tr_torrent * b = * (const tr_torrent**)vb;
     1747  const uint64_t aCur = a->downloadedCur + a->uploadedCur;
     1748  const uint64_t bCur = b->downloadedCur + b->uploadedCur;
     1749
     1750  if (aCur != bCur)
     1751    return aCur > bCur ? -1 : 1; /* close the biggest torrents first */
     1752
     1753  return 0;
    17411754}
    17421755
     
    17461759sessionCloseImpl (void * vsession)
    17471760{
    1748     tr_session *  session = vsession;
    1749     tr_torrent *  tor;
    1750     int           i, n;
    1751     tr_torrent ** torrents;
    1752 
    1753     assert (tr_isSession (session));
    1754 
    1755     session->isClosing = true;
    1756 
    1757     free_incoming_peer_port (session);
    1758 
    1759     if (session->isLPDEnabled)
    1760         tr_lpdUninit (session);
    1761 
    1762     tr_utpClose (session);
    1763     tr_dhtUninit (session);
    1764 
    1765     event_free (session->saveTimer);
    1766     session->saveTimer = NULL;
    1767 
    1768     event_free (session->nowTimer);
    1769     session->nowTimer = NULL;
    1770 
    1771     tr_verifyClose (session);
    1772     tr_sharedClose (session);
    1773     tr_rpcClose (&session->rpcServer);
    1774 
    1775     /* Close the torrents. Get the most active ones first so that
    1776      * if we can't get them all closed in a reasonable amount of time,
    1777      * at least we get the most important ones first. */
    1778     tor = NULL;
    1779     n = session->torrentCount;
    1780     torrents = tr_new (tr_torrent *, session->torrentCount);
    1781     for (i = 0; i < n; ++i)
    1782         torrents[i] = tor = tr_torrentNext (session, tor);
    1783     qsort (torrents, n, sizeof (tr_torrent*), compareTorrentByCur);
    1784     for (i = 0; i < n; ++i)
    1785         tr_torrentFree (torrents[i]);
    1786     tr_free (torrents);
    1787 
    1788     /* Close the announcer *after* closing the torrents
    1789        so that all the &event=stopped messages will be
    1790        queued to be sent by tr_announcerClose () */
    1791     tr_announcerClose (session);
    1792 
    1793     /* and this goes *after* announcer close so that
    1794        it won't be idle until the announce events are sent... */
    1795     tr_webClose (session, TR_WEB_CLOSE_WHEN_IDLE);
    1796 
    1797     tr_cacheFree (session->cache);
    1798     session->cache = NULL;
    1799 
    1800     /* gotta keep udp running long enough to send out all
    1801        the &event=stopped UDP tracker messages */
    1802     while (!tr_tracker_udp_is_idle (session)) {
    1803         tr_tracker_udp_upkeep (session);
    1804         tr_wait_msec (100);
    1805     }
    1806 
    1807     /* we had to wait until UDP trackers were closed before closing these: */
    1808     evdns_base_free (session->evdns_base, 0);
    1809     session->evdns_base = NULL;
    1810     tr_tracker_udp_close (session);
    1811     tr_udpUninit (session);
    1812 
    1813     tr_statsClose (session);
    1814     tr_peerMgrFree (session->peerMgr);
    1815 
    1816     closeBlocklists (session);
    1817 
    1818     tr_fdClose (session);
    1819 
    1820     session->isClosed = true;
     1761  int i, n;
     1762  tr_torrent * tor;
     1763  tr_torrent ** torrents;
     1764  tr_session * session = vsession;
     1765
     1766  assert (tr_isSession (session));
     1767
     1768  session->isClosing = true;
     1769
     1770  free_incoming_peer_port (session);
     1771
     1772  if (session->isLPDEnabled)
     1773    tr_lpdUninit (session);
     1774
     1775  tr_utpClose (session);
     1776  tr_dhtUninit (session);
     1777
     1778  event_free (session->saveTimer);
     1779  session->saveTimer = NULL;
     1780
     1781  event_free (session->nowTimer);
     1782  session->nowTimer = NULL;
     1783
     1784  tr_verifyClose (session);
     1785  tr_sharedClose (session);
     1786  tr_rpcClose (&session->rpcServer);
     1787
     1788  /* Close the torrents. Get the most active ones first so that
     1789   * if we can't get them all closed in a reasonable amount of time,
     1790   * at least we get the most important ones first. */
     1791  tor = NULL;
     1792  n = session->torrentCount;
     1793  torrents = tr_new (tr_torrent *, session->torrentCount);
     1794  for (i=0; i<n; ++i)
     1795    torrents[i] = tor = tr_torrentNext (session, tor);
     1796  qsort (torrents, n, sizeof (tr_torrent*), compareTorrentByCur);
     1797  for (i=0; i<n; ++i)
     1798    tr_torrentFree (torrents[i]);
     1799  tr_free (torrents);
     1800
     1801  /* Close the announcer *after* closing the torrents
     1802     so that all the &event=stopped messages will be
     1803     queued to be sent by tr_announcerClose () */
     1804  tr_announcerClose (session);
     1805
     1806  /* and this goes *after* announcer close so that
     1807     it won't be idle until the announce events are sent... */
     1808  tr_webClose (session, TR_WEB_CLOSE_WHEN_IDLE);
     1809
     1810  tr_cacheFree (session->cache);
     1811  session->cache = NULL;
     1812
     1813  /* gotta keep udp running long enough to send out all
     1814     the &event=stopped UDP tracker messages */
     1815  while (!tr_tracker_udp_is_idle (session))
     1816    {
     1817      tr_tracker_udp_upkeep (session);
     1818      tr_wait_msec (100);
     1819    }
     1820
     1821  /* we had to wait until UDP trackers were closed before closing these: */
     1822  evdns_base_free (session->evdns_base, 0);
     1823  session->evdns_base = NULL;
     1824  tr_tracker_udp_close (session);
     1825  tr_udpUninit (session);
     1826
     1827  tr_statsClose (session);
     1828  tr_peerMgrFree (session->peerMgr);
     1829
     1830  closeBlocklists (session);
     1831
     1832  tr_fdClose (session);
     1833
     1834  session->isClosed = true;
    18211835}
    18221836
     
    18241838deadlineReached (const time_t deadline)
    18251839{
    1826     return time (NULL) >= deadline;
     1840  return time (NULL) >= deadline;
    18271841}
    18281842
     
    18321846tr_sessionClose (tr_session * session)
    18331847{
    1834     const time_t deadline = time (NULL) + SHUTDOWN_MAX_SECONDS;
    1835 
    1836     assert (tr_isSession (session));
    1837 
    1838     dbgmsg ("shutting down transmission session %p... now is %zu, deadline is %zu", session, (size_t)time (NULL), (size_t)deadline);
    1839 
    1840     /* close the session */
    1841     tr_runInEventThread (session, sessionCloseImpl, session);
    1842     while (!session->isClosed && !deadlineReached (deadline))
    1843     {
    1844         dbgmsg ("waiting for the libtransmission thread to finish");
    1845         tr_wait_msec (100);
    1846     }
    1847 
    1848     /* "shared" and "tracker" have live sockets,
    1849      * so we need to keep the transmission thread alive
    1850      * for a bit while they tell the router & tracker
    1851      * that we're closing now */
    1852     while ((session->shared || session->web || session->announcer || session->announcer_udp)
     1848  const time_t deadline = time (NULL) + SHUTDOWN_MAX_SECONDS;
     1849
     1850  assert (tr_isSession (session));
     1851
     1852  dbgmsg ("shutting down transmission session %p... now is %zu, deadline is %zu", session, (size_t)time (NULL), (size_t)deadline);
     1853
     1854  /* close the session */
     1855  tr_runInEventThread (session, sessionCloseImpl, session);
     1856  while (!session->isClosed && !deadlineReached (deadline))
     1857    {
     1858      dbgmsg ("waiting for the libtransmission thread to finish");
     1859      tr_wait_msec (100);
     1860    }
     1861
     1862  /* "shared" and "tracker" have live sockets,
     1863   * so we need to keep the transmission thread alive
     1864   * for a bit while they tell the router & tracker
     1865   * that we're closing now */
     1866  while ((session->shared || session->web || session->announcer || session->announcer_udp)
    18531867           && !deadlineReached (deadline))
    18541868    {
    1855         dbgmsg ("waiting on port unmap (%p) or announcer (%p)... now %zu deadline %zu",
    1856                 session->shared, session->announcer, (size_t)time (NULL), (size_t)deadline);
    1857         tr_wait_msec (50);
    1858     }
    1859 
    1860     tr_webClose (session, TR_WEB_CLOSE_NOW);
    1861 
    1862     /* close the libtransmission thread */
    1863     tr_eventClose (session);
    1864     while (session->events != NULL)
    1865     {
    1866         static bool forced = false;
    1867         dbgmsg ("waiting for libtransmission thread to finish... now %zu deadline %zu", (size_t)time (NULL), (size_t)deadline);
    1868         tr_wait_msec (100);
    1869         if (deadlineReached (deadline) && !forced)
     1869      dbgmsg ("waiting on port unmap (%p) or announcer (%p)... now %zu deadline %zu",
     1870              session->shared, session->announcer, (size_t)time (NULL), (size_t)deadline);
     1871      tr_wait_msec (50);
     1872    }
     1873
     1874  tr_webClose (session, TR_WEB_CLOSE_NOW);
     1875
     1876  /* close the libtransmission thread */
     1877  tr_eventClose (session);
     1878  while (session->events != NULL)
     1879    {
     1880      static bool forced = false;
     1881      dbgmsg ("waiting for libtransmission thread to finish... now %zu deadline %zu", (size_t)time (NULL), (size_t)deadline);
     1882      tr_wait_msec (100);
     1883
     1884      if (deadlineReached (deadline) && !forced)
    18701885        {
    1871             dbgmsg ("calling event_loopbreak ()");
    1872             forced = true;
    1873             event_base_loopbreak (session->event_base);
     1886          dbgmsg ("calling event_loopbreak ()");
     1887          forced = true;
     1888          event_base_loopbreak (session->event_base);
    18741889        }
    1875         if (deadlineReached (deadline+3))
     1890
     1891      if (deadlineReached (deadline+3))
    18761892        {
    1877             dbgmsg ("deadline+3 reached... calling break...\n");
    1878             break;
     1893          dbgmsg ("deadline+3 reached... calling break...\n");
     1894          break;
    18791895        }
    18801896    }
    18811897
    1882     /* free the session memory */
    1883     tr_variantFree (&session->removedTorrents);
    1884     tr_bandwidthDestruct (&session->bandwidth);
    1885     tr_bitfieldDestruct (&session->turtle.minutes);
    1886     tr_lockFree (session->lock);
    1887     if (session->metainfoLookup) {
    1888         tr_variantFree (session->metainfoLookup);
    1889         tr_free (session->metainfoLookup);
    1890     }
    1891     tr_device_info_free (session->downloadDir);
    1892     tr_free (session->torrentDoneScript);
    1893     tr_free (session->tag);
    1894     tr_free (session->configDir);
    1895     tr_free (session->resumeDir);
    1896     tr_free (session->torrentDir);
    1897     tr_free (session->incompleteDir);
    1898     tr_free (session->blocklist_url);
    1899     tr_free (session->peer_congestion_algorithm);
    1900     tr_free (session);
     1898  /* free the session memory */
     1899  tr_variantFree (&session->removedTorrents);
     1900  tr_bandwidthDestruct (&session->bandwidth);
     1901  tr_bitfieldDestruct (&session->turtle.minutes);
     1902  tr_lockFree (session->lock);
     1903  if (session->metainfoLookup)
     1904    {
     1905      tr_variantFree (session->metainfoLookup);
     1906      tr_free (session->metainfoLookup);
     1907    }
     1908  tr_device_info_free (session->downloadDir);
     1909  tr_free (session->torrentDoneScript);
     1910  tr_free (session->tag);
     1911  tr_free (session->configDir);
     1912  tr_free (session->resumeDir);
     1913  tr_free (session->torrentDir);
     1914  tr_free (session->incompleteDir);
     1915  tr_free (session->blocklist_url);
     1916  tr_free (session->peer_congestion_algorithm);
     1917  tr_free (session);
    19011918}
    19021919
    19031920struct sessionLoadTorrentsData
    19041921{
    1905     tr_session * session;
    1906     tr_ctor * ctor;
    1907     int * setmeCount;
    1908     tr_torrent ** torrents;
    1909     bool done;
     1922  tr_session * session;
     1923  tr_ctor * ctor;
     1924  int * setmeCount;
     1925  tr_torrent ** torrents;
     1926  bool done;
    19101927};
    19111928
     
    19131930sessionLoadTorrents (void * vdata)
    19141931{
    1915     int i;
    1916     int n = 0;
    1917     struct stat sb;
    1918     DIR * odir = NULL;
    1919     tr_list * l = NULL;
    1920     tr_list * list = NULL;
    1921     struct sessionLoadTorrentsData * data = vdata;
    1922     const char * dirname = tr_getTorrentDir (data->session);
    1923 
    1924     assert (tr_isSession (data->session));
    1925 
    1926     tr_ctorSetSave (data->ctor, false); /* since we already have them */
    1927 
    1928     if (!stat (dirname, &sb)
     1932  int i;
     1933  int n = 0;
     1934  struct stat sb;
     1935  DIR * odir = NULL;
     1936  tr_list * l = NULL;
     1937  tr_list * list = NULL;
     1938  struct sessionLoadTorrentsData * data = vdata;
     1939  const char * dirname = tr_getTorrentDir (data->session);
     1940
     1941  assert (tr_isSession (data->session));
     1942
     1943  tr_ctorSetSave (data->ctor, false); /* since we already have them */
     1944
     1945  if (!stat (dirname, &sb)
    19291946      && S_ISDIR (sb.st_mode)
    19301947      && ((odir = opendir (dirname))))
    19311948    {
    1932         struct dirent *d;
    1933         for (d = readdir (odir); d != NULL; d = readdir (odir))
     1949      struct dirent *d;
     1950      for (d = readdir (odir); d != NULL; d = readdir (odir))
    19341951        {
    1935             if (tr_str_has_suffix (d->d_name, ".torrent"))
     1952          if (tr_str_has_suffix (d->d_name, ".torrent"))
    19361953            {
    1937                 tr_torrent * tor;
    1938                 char * path = tr_buildPath (dirname, d->d_name, NULL);
    1939                 tr_ctorSetMetainfoFromFile (data->ctor, path);
    1940                 if ((tor = tr_torrentNew (data->ctor, NULL, NULL)))
     1954              tr_torrent * tor;
     1955              char * path = tr_buildPath (dirname, d->d_name, NULL);
     1956              tr_ctorSetMetainfoFromFile (data->ctor, path);
     1957              if ((tor = tr_torrentNew (data->ctor, NULL, NULL)))
    19411958                {
    1942                     tr_list_prepend (&list, tor);
    1943                     ++n;
     1959                  tr_list_prepend (&list, tor);
     1960                  ++n;
    19441961                }
    1945                 tr_free (path);
     1962              tr_free (path);
    19461963            }
    19471964        }
    1948         closedir (odir);
    1949     }
    1950 
    1951     data->torrents = tr_new (tr_torrent *, n);
    1952     for (i = 0, l = list; l != NULL; l = l->next)
    1953         data->torrents[i++] = (tr_torrent*) l->data;
    1954     assert (i == n);
     1965      closedir (odir);
     1966    }
     1967
     1968  data->torrents = tr_new (tr_torrent *, n);
     1969  for (i=0, l=list; l!=NULL; l=l->next)
     1970    data->torrents[i++] = (tr_torrent*) l->data;
     1971  assert (i == n);
    19551972
    19561973    tr_list_free (&list, NULL);
    19571974
    1958     if (n)
    1959         tr_logAddInfo (_("Loaded %d torrents"), n);
    1960 
    1961     if (data->setmeCount)
    1962         *data->setmeCount = n;
    1963 
    1964     data->done = true;
     1975  if (n)
     1976    tr_logAddInfo (_("Loaded %d torrents"), n);
     1977
     1978  if (data->setmeCount)
     1979    *data->setmeCount = n;
     1980
     1981  data->done = true;
    19651982}
    19661983
     
    19701987                        int        * setmeCount)
    19711988{
    1972     struct sessionLoadTorrentsData data;
    1973 
    1974     data.session = session;
    1975     data.ctor = ctor;
    1976     data.setmeCount = setmeCount;
    1977     data.torrents = NULL;
    1978     data.done = false;
    1979 
    1980     tr_runInEventThread (session, sessionLoadTorrents, &data);
    1981     while (!data.done)
    1982         tr_wait_msec (100);
    1983 
    1984     return data.torrents;
     1989  struct sessionLoadTorrentsData data;
     1990
     1991  data.session = session;
     1992  data.ctor = ctor;
     1993  data.setmeCount = setmeCount;
     1994  data.torrents = NULL;
     1995  data.done = false;
     1996
     1997  tr_runInEventThread (session, sessionLoadTorrents, &data);
     1998  while (!data.done)
     1999    tr_wait_msec (100);
     2000
     2001  return data.torrents;
    19852002}
    19862003
     
    19922009tr_sessionSetPexEnabled (tr_session * session, bool enabled)
    19932010{
    1994     assert (tr_isSession (session));
    1995 
    1996     session->isPexEnabled = enabled != 0;
     2011  assert (tr_isSession (session));
     2012
     2013  session->isPexEnabled = enabled != 0;
    19972014}
    19982015
     
    20002017tr_sessionIsPexEnabled (const tr_session * session)
    20012018{
    2002     assert (tr_isSession (session));
    2003 
    2004     return session->isPexEnabled;
     2019  assert (tr_isSession (session));
     2020
     2021  return session->isPexEnabled;
    20052022}
    20062023
     
    20082025tr_sessionAllowsDHT (const tr_session * session)
    20092026{
    2010     return tr_sessionIsDHTEnabled (session);
     2027  return tr_sessionIsDHTEnabled (session);
    20112028}
    20122029
     
    20142031tr_sessionIsDHTEnabled (const tr_session * session)
    20152032{
    2016     assert (tr_isSession (session));
    2017 
    2018     return session->isDHTEnabled;
     2033  assert (tr_isSession (session));
     2034
     2035  return session->isDHTEnabled;
    20192036}
    20202037
     
    20222039toggleDHTImpl (void * data)
    20232040{
    2024     tr_session * session = data;
    2025     assert (tr_isSession (session));
    2026 
    2027     tr_udpUninit (session);
    2028     session->isDHTEnabled = !session->isDHTEnabled;
    2029     tr_udpInit (session);
     2041  tr_session * session = data;
     2042  assert (tr_isSession (session));
     2043
     2044  tr_udpUninit (session);
     2045  session->isDHTEnabled = !session->isDHTEnabled;
     2046  tr_udpInit (session);
    20302047}
    20312048
     
    20332050tr_sessionSetDHTEnabled (tr_session * session, bool enabled)
    20342051{
    2035     assert (tr_isSession (session));
    2036     assert (tr_isBool (enabled));
    2037 
    2038     if ((enabled != 0) != (session->isDHTEnabled != 0))
    2039         tr_runInEventThread (session, toggleDHTImpl, session);
     2052  assert (tr_isSession (session));
     2053  assert (tr_isBool (enabled));
     2054
     2055  if ((enabled != 0) != (session->isDHTEnabled != 0))
     2056    tr_runInEventThread (session, toggleDHTImpl, session);
    20402057}
    20412058
     
    20472064tr_sessionIsUTPEnabled (const tr_session * session)
    20482065{
    2049     assert (tr_isSession (session));
     2066  assert (tr_isSession (session));
    20502067
    20512068#ifdef WITH_UTP
    2052     return session->isUTPEnabled;
     2069  return session->isUTPEnabled;
    20532070#else
    2054     return false;
     2071  return false;
    20552072#endif
    20562073}
     
    20592076toggle_utp (void * data)
    20602077{
    2061     tr_session * session = data;
    2062     assert (tr_isSession (session));
    2063 
    2064     session->isUTPEnabled = !session->isUTPEnabled;
    2065 
    2066     tr_udpSetSocketBuffers (session);
    2067 
    2068     /* But don't call tr_utpClose -- see reset_timer in tr-utp.c for an
    2069        explanation. */
     2078  tr_session * session = data;
     2079
     2080  assert (tr_isSession (session));
     2081
     2082  session->isUTPEnabled = !session->isUTPEnabled;
     2083
     2084  tr_udpSetSocketBuffers (session);
     2085
     2086  /* But don't call tr_utpClose -- see reset_timer in tr-utp.c for an
     2087     explanation. */
    20702088}
    20712089
     
    20732091tr_sessionSetUTPEnabled (tr_session * session, bool enabled)
    20742092{
    2075     assert (tr_isSession (session));
    2076     assert (tr_isBool (enabled));
    2077 
    2078     if ((enabled != 0) != (session->isUTPEnabled != 0))
    2079         tr_runInEventThread (session, toggle_utp, session);
     2093  assert (tr_isSession (session));
     2094  assert (tr_isBool (enabled));
     2095
     2096  if ((enabled != 0) != (session->isUTPEnabled != 0))
     2097    tr_runInEventThread (session, toggle_utp, session);
    20802098}
    20812099
     
    20872105toggleLPDImpl (void * data)
    20882106{
    2089     tr_session * session = data;
    2090     assert (tr_isSession (session));
    2091 
    2092     if (session->isLPDEnabled)
    2093         tr_lpdUninit (session);
    2094 
    2095     session->isLPDEnabled = !session->isLPDEnabled;
    2096 
    2097     if (session->isLPDEnabled)
    2098         tr_lpdInit (session, &session->public_ipv4->addr);
     2107  tr_session * session = data;
     2108  assert (tr_isSession (session));
     2109
     2110  if (session->isLPDEnabled)
     2111    tr_lpdUninit (session);
     2112
     2113  session->isLPDEnabled = !session->isLPDEnabled;
     2114
     2115  if (session->isLPDEnabled)
     2116    tr_lpdInit (session, &session->public_ipv4->addr);
    20992117}
    21002118
     
    21022120tr_sessionSetLPDEnabled (tr_session * session, bool enabled)
    21032121{
    2104     assert (tr_isSession (session));
    2105     assert (tr_isBool (enabled));
    2106 
    2107     if ((enabled != 0) != (session->isLPDEnabled != 0))
    2108         tr_runInEventThread (session, toggleLPDImpl, session);
     2122  assert (tr_isSession (session));
     2123  assert (tr_isBool (enabled));
     2124
     2125  if ((enabled != 0) != (session->isLPDEnabled != 0))
     2126    tr_runInEventThread (session, toggleLPDImpl, session);
    21092127}
    21102128
     
    21122130tr_sessionIsLPDEnabled (const tr_session * session)
    21132131{
    2114     assert (tr_isSession (session));
    2115 
    2116     return session->isLPDEnabled;
     2132  assert (tr_isSession (session));
     2133
     2134  return session->isLPDEnabled;
    21172135}
    21182136
     
    21202138tr_sessionAllowsLPD (const tr_session * session)
    21212139{
    2122     return tr_sessionIsLPDEnabled (session);
     2140  return tr_sessionIsLPDEnabled (session);
    21232141}
    21242142
     
    21302148tr_sessionSetCacheLimit_MB (tr_session * session, int max_bytes)
    21312149{
    2132     assert (tr_isSession (session));
    2133 
    2134     tr_cacheSetLimit (session->cache, toMemBytes (max_bytes));
     2150  assert (tr_isSession (session));
     2151
     2152  tr_cacheSetLimit (session->cache, toMemBytes (max_bytes));
    21352153}
    21362154
     
    21382156tr_sessionGetCacheLimit_MB (const tr_session * session)
    21392157{
    2140     assert (tr_isSession (session));
    2141 
    2142     return toMemMB (tr_cacheGetLimit (session->cache));
     2158  assert (tr_isSession (session));
     2159
     2160  return toMemMB (tr_cacheGetLimit (session->cache));
    21432161}
    21442162
     
    21492167struct port_forwarding_data
    21502168{
    2151     bool enabled;
    2152     struct tr_shared * shared;
     2169  bool enabled;
     2170  struct tr_shared * shared;
    21532171};
    21542172
     
    21562174setPortForwardingEnabled (void * vdata)
    21572175{
    2158     struct port_forwarding_data * data = vdata;
    2159     tr_sharedTraversalEnable (data->shared, data->enabled);
    2160     tr_free (data);
     2176  struct port_forwarding_data * data = vdata;
     2177  tr_sharedTraversalEnable (data->shared, data->enabled);
     2178  tr_free (data);
    21612179}
    21622180
     
    21642182tr_sessionSetPortForwardingEnabled (tr_session  * session, bool enabled)
    21652183{
    2166     struct port_forwarding_data * d;
    2167     d = tr_new0 (struct port_forwarding_data, 1);
    2168     d->shared = session->shared;
    2169     d->enabled = enabled;
    2170     tr_runInEventThread (session, setPortForwardingEnabled, d);
     2184  struct port_forwarding_data * d;
     2185  d = tr_new0 (struct port_forwarding_data, 1);
     2186  d->shared = session->shared;
     2187  d->enabled = enabled;
     2188  tr_runInEventThread (session, setPortForwardingEnabled, d);
    21712189}
    21722190
     
    21742192tr_sessionIsPortForwardingEnabled (const tr_session * session)
    21752193{
    2176     assert (tr_isSession (session));
    2177 
    2178     return tr_sharedTraversalIsEnabled (session->shared);
     2194  assert (tr_isSession (session));
     2195
     2196  return tr_sharedTraversalIsEnabled (session->shared);
    21792197}
    21802198
     
    21862204tr_stringEndsWith (const char * str, const char * end)
    21872205{
    2188     const size_t slen = strlen (str);
    2189     const size_t elen = strlen (end);
    2190 
    2191     return slen >= elen && !memcmp (&str[slen - elen], end, elen);
     2206  const size_t slen = strlen (str);
     2207  const size_t elen = strlen (end);
     2208
     2209  return slen >= elen && !memcmp (&str[slen - elen], end, elen);
    21922210}
    21932211
     
    22962314closeBlocklists (tr_session * session)
    22972315{
    2298     tr_list_free (&session->blocklists,
    2299                 (TrListForeachFunc)tr_blocklistFileFree);
     2316  tr_list_free (&session->blocklists, (TrListForeachFunc)tr_blocklistFileFree);
    23002317}
    23012318
     
    23032320tr_sessionReloadBlocklists (tr_session * session)
    23042321{
    2305     closeBlocklists (session);
    2306     loadBlocklists (session);
    2307 
    2308     tr_peerMgrOnBlocklistChanged (session->peerMgr);
     2322  closeBlocklists (session);
     2323  loadBlocklists (session);
     2324
     2325  tr_peerMgrOnBlocklistChanged (session->peerMgr);
    23092326}
    23102327
     
    23122329tr_blocklistGetRuleCount (const tr_session * session)
    23132330{
    2314     int       n = 0;
    2315     tr_list * l;
    2316 
    2317     assert (tr_isSession (session));
    2318 
    2319     for (l = session->blocklists; l; l = l->next)
    2320         n += tr_blocklistFileGetRuleCount (l->data);
    2321     return n;
     2331  tr_list * l;
     2332  int n = 0;
     2333
     2334  assert (tr_isSession (session));
     2335
     2336  for (l = session->blocklists; l; l = l->next)
     2337    n += tr_blocklistFileGetRuleCount (l->data);
     2338
     2339  return n;
    23222340}
    23232341
     
    23252343tr_blocklistIsEnabled (const tr_session * session)
    23262344{
    2327     assert (tr_isSession (session));
    2328 
    2329     return session->isBlocklistEnabled;
     2345  assert (tr_isSession (session));
     2346
     2347  return session->isBlocklistEnabled;
    23302348}
    23312349
     
    23332351tr_blocklistSetEnabled (tr_session * session, bool isEnabled)
    23342352{
    2335     tr_list * l;
    2336 
    2337     assert (tr_isSession (session));
    2338 
    2339     session->isBlocklistEnabled = isEnabled != 0;
    2340 
    2341     for (l=session->blocklists; l!=NULL; l=l->next)
    2342         tr_blocklistFileSetEnabled (l->data, isEnabled);
     2353  tr_list * l;
     2354
     2355  assert (tr_isSession (session));
     2356
     2357  session->isBlocklistEnabled = isEnabled != 0;
     2358
     2359  for (l=session->blocklists; l!=NULL; l=l->next)
     2360    tr_blocklistFileSetEnabled (l->data, isEnabled);
    23432361}
    23442362
     
    23462364tr_blocklistExists (const tr_session * session)
    23472365{
    2348     assert (tr_isSession (session));
    2349 
    2350     return session->blocklists != NULL;
     2366  assert (tr_isSession (session));
     2367
     2368  return session->blocklists != NULL;
    23512369}
    23522370
     
    23542372tr_blocklistSetContent (tr_session * session, const char * contentFilename)
    23552373{
    2356     tr_list * l;
    2357     int ruleCount;
    2358     tr_blocklistFile * b;
    2359     const char * defaultName = DEFAULT_BLOCKLIST_FILENAME;
    2360     tr_sessionLock (session);
    2361 
    2362     for (b = NULL, l = session->blocklists; !b && l; l = l->next)
    2363         if (tr_stringEndsWith (tr_blocklistFileGetFilename (l->data),
    2364                                defaultName))
    2365             b = l->data;
    2366 
    2367     if (!b)
    2368     {
    2369         char * path = tr_buildPath (session->configDir, "blocklists", defaultName, NULL);
    2370         b = tr_blocklistFileNew (path, session->isBlocklistEnabled);
    2371         tr_list_append (&session->blocklists, b);
    2372         tr_free (path);
    2373     }
    2374 
    2375     ruleCount = tr_blocklistFileSetContent (b, contentFilename);
    2376     tr_sessionUnlock (session);
    2377     return ruleCount;
     2374  tr_list * l;
     2375  int ruleCount;
     2376  tr_blocklistFile * b;
     2377  const char * defaultName = DEFAULT_BLOCKLIST_FILENAME;
     2378  tr_sessionLock (session);
     2379
     2380  for (b=NULL, l=session->blocklists; !b && l; l=l->next)
     2381    if (tr_stringEndsWith (tr_blocklistFileGetFilename (l->data), defaultName))
     2382      b = l->data;
     2383
     2384  if (!b)
     2385    {
     2386      char * path = tr_buildPath (session->configDir, "blocklists", defaultName, NULL);
     2387      b = tr_blocklistFileNew (path, session->isBlocklistEnabled);
     2388      tr_list_append (&session->blocklists, b);
     2389      tr_free (path);
     2390    }
     2391
     2392  ruleCount = tr_blocklistFileSetContent (b, contentFilename);
     2393  tr_sessionUnlock (session);
     2394  return ruleCount;
    23782395}
    23792396
     
    23822399                            const tr_address * addr)
    23832400{
    2384     tr_list * l;
    2385 
    2386     assert (tr_isSession (session));
    2387 
    2388     for (l = session->blocklists; l; l = l->next)
    2389         if (tr_blocklistFileHasAddress (l->data, addr))
    2390             return true;
    2391     return false;
     2401  tr_list * l;
     2402
     2403  assert (tr_isSession (session));
     2404
     2405  for (l = session->blocklists; l; l = l->next)
     2406    if (tr_blocklistFileHasAddress (l->data, addr))
     2407      return true;
     2408
     2409  return false;
    23922410}
    23932411
     
    23952413tr_blocklistSetURL (tr_session * session, const char * url)
    23962414{
    2397     if (session->blocklist_url != url)
    2398     {
    2399         tr_free (session->blocklist_url);
    2400         session->blocklist_url = tr_strdup (url);
     2415  if (session->blocklist_url != url)
     2416    {
     2417      tr_free (session->blocklist_url);
     2418      session->blocklist_url = tr_strdup (url);
    24012419    }
    24022420}
     
    24052423tr_blocklistGetURL (const tr_session * session)
    24062424{
    2407     return session->blocklist_url;
     2425  return session->blocklist_url;
    24082426}
    24092427
     
    24162434metainfoLookupInit (tr_session * session)
    24172435{
    2418     struct stat sb;
    2419     const char * dirname = tr_getTorrentDir (session);
    2420     DIR *        odir = NULL;
    2421     tr_ctor *    ctor = NULL;
    2422     tr_variant * lookup;
    2423     int n = 0;
    2424 
    2425     assert (tr_isSession (session));
    2426 
    2427     /* walk through the directory and find the mappings */
    2428     lookup = tr_new0 (tr_variant, 1);
    2429     tr_variantInitDict (lookup, 0);
    2430     ctor = tr_ctorNew (session);
    2431     tr_ctorSetSave (ctor, false); /* since we already have them */
    2432     if (!stat (dirname, &sb) && S_ISDIR (sb.st_mode) && ((odir = opendir (dirname))))
    2433     {
    2434         struct dirent *d;
    2435         while ((d = readdir (odir)))
     2436  struct stat sb;
     2437  const char * dirname = tr_getTorrentDir (session);
     2438  DIR * odir = NULL;
     2439  tr_ctor * ctor = NULL;
     2440  tr_variant * lookup;
     2441  int n = 0;
     2442
     2443  assert (tr_isSession (session));
     2444
     2445  /* walk through the directory and find the mappings */
     2446  lookup = tr_new0 (tr_variant, 1);
     2447  tr_variantInitDict (lookup, 0);
     2448  ctor = tr_ctorNew (session);
     2449  tr_ctorSetSave (ctor, false); /* since we already have them */
     2450  if (!stat (dirname, &sb) && S_ISDIR (sb.st_mode) && ((odir = opendir (dirname))))
     2451    {
     2452      struct dirent *d;
     2453      while ((d = readdir (odir)))
    24362454        {
    2437             if (tr_str_has_suffix (d->d_name, ".torrent"))
     2455          if (tr_str_has_suffix (d->d_name, ".torrent"))
    24382456            {
    2439                 tr_info inf;
    2440                 char * path = tr_buildPath (dirname, d->d_name, NULL);
    2441                 tr_ctorSetMetainfoFromFile (ctor, path);
    2442                 if (!tr_torrentParse (ctor, &inf))
     2457              tr_info inf;
     2458              char * path = tr_buildPath (dirname, d->d_name, NULL);
     2459              tr_ctorSetMetainfoFromFile (ctor, path);
     2460              if (!tr_torrentParse (ctor, &inf))
    24432461                {
    2444                     ++n;
    2445                     tr_variantDictAddStr (lookup, tr_quark_new(inf.hashString,-1), path);
     2462                  ++n;
     2463                  tr_variantDictAddStr (lookup, tr_quark_new(inf.hashString,-1), path);
    24462464                }
    2447                 tr_free (path);
     2465              tr_free (path);
    24482466            }
    24492467        }
    2450         closedir (odir);
    2451     }
    2452     tr_ctorFree (ctor);
    2453 
    2454     session->metainfoLookup = lookup;
    2455     tr_logAddDebug ("Found %d torrents in \"%s\"", n, dirname);
     2468      closedir (odir);
     2469    }
     2470  tr_ctorFree (ctor);
     2471
     2472  session->metainfoLookup = lookup;
     2473  tr_logAddDebug ("Found %d torrents in \"%s\"", n, dirname);
    24562474}
    24572475
     
    24602478                           const char       * hashString)
    24612479{
    2462     const char * filename = NULL;
    2463     if (!session->metainfoLookup)
    2464         metainfoLookupInit ((tr_session*)session);
    2465     tr_variantDictFindStr (session->metainfoLookup, tr_quark_new(hashString,-1), &filename, NULL);
    2466     return filename;
     2480  const char * filename = NULL;
     2481
     2482  if (!session->metainfoLookup)
     2483    metainfoLookupInit ((tr_session*)session);
     2484  tr_variantDictFindStr (session->metainfoLookup, tr_quark_new(hashString,-1), &filename, NULL);
     2485
     2486  return filename;
    24672487}
    24682488
     
    24722492                          const char * filename)
    24732493{
    2474     /* since we walk session->configDir/torrents/ to build the lookup table,
    2475      * and tr_sessionSetTorrentFile () is just to tell us there's a new file
    2476      * in that same directory, we don't need to do anything here if the
    2477      * lookup table hasn't been built yet */
    2478     if (session->metainfoLookup)
    2479         tr_variantDictAddStr (session->metainfoLookup, tr_quark_new(hashString,-1), filename);
     2494  /* since we walk session->configDir/torrents/ to build the lookup table,
     2495   * and tr_sessionSetTorrentFile () is just to tell us there's a new file
     2496   * in that same directory, we don't need to do anything here if the
     2497   * lookup table hasn't been built yet */
     2498  if (session->metainfoLookup)
     2499    tr_variantDictAddStr (session->metainfoLookup, tr_quark_new(hashString,-1), filename);
    24802500}
    24812501
     
    24872507tr_sessionSetRPCEnabled (tr_session * session, bool isEnabled)
    24882508{
    2489     assert (tr_isSession (session));
    2490 
    2491     tr_rpcSetEnabled (session->rpcServer, isEnabled);
     2509  assert (tr_isSession (session));
     2510
     2511  tr_rpcSetEnabled (session->rpcServer, isEnabled);
    24922512}
    24932513
     
    24952515tr_sessionIsRPCEnabled (const tr_session * session)
    24962516{
    2497     assert (tr_isSession (session));
    2498 
    2499     return tr_rpcIsEnabled (session->rpcServer);
     2517  assert (tr_isSession (session));
     2518
     2519  return tr_rpcIsEnabled (session->rpcServer);
    25002520}
    25012521
     
    25042524                      tr_port      port)
    25052525{
    2506     assert (tr_isSession (session));
    2507 
    2508     tr_rpcSetPort (session->rpcServer, port);
     2526  assert (tr_isSession (session));
     2527
     2528  tr_rpcSetPort (session->rpcServer, port);
    25092529}
    25102530
     
    25122532tr_sessionGetRPCPort (const tr_session * session)
    25132533{
    2514     assert (tr_isSession (session));
    2515 
    2516     return tr_rpcGetPort (session->rpcServer);
     2534  assert (tr_isSession (session));
     2535
     2536  return tr_rpcGetPort (session->rpcServer);
    25172537}
    25182538
     
    25212541                     const char * url)
    25222542{
    2523     assert (tr_isSession (session));
    2524 
    2525     tr_rpcSetUrl (session->rpcServer, url);
     2543  assert (tr_isSession (session));
     2544
     2545  tr_rpcSetUrl (session->rpcServer, url);
    25262546}
    25272547
     
    25292549tr_sessionGetRPCUrl (const tr_session * session)
    25302550{
    2531     assert (tr_isSession (session));
    2532 
    2533     return tr_rpcGetUrl (session->rpcServer);
     2551  assert (tr_isSession (session));
     2552
     2553  return tr_rpcGetUrl (session->rpcServer);
    25342554}
    25352555
     
    25392559                          void *       user_data)
    25402560{
    2541     assert (tr_isSession (session));
    2542 
    2543     session->rpc_func = func;
    2544     session->rpc_func_user_data = user_data;
     2561  assert (tr_isSession (session));
     2562
     2563  session->rpc_func = func;
     2564  session->rpc_func_user_data = user_data;
    25452565}
    25462566
     
    25492569                           const char * whitelist)
    25502570{
    2551     assert (tr_isSession (session));
    2552 
    2553     tr_rpcSetWhitelist (session->rpcServer, whitelist);
     2571  assert (tr_isSession (session));
     2572
     2573  tr_rpcSetWhitelist (session->rpcServer, whitelist);
    25542574}
    25552575
     
    25572577tr_sessionGetRPCWhitelist (const tr_session * session)
    25582578{
    2559     assert (tr_isSession (session));
    2560 
    2561     return tr_rpcGetWhitelist (session->rpcServer);
     2579  assert (tr_isSession (session));
     2580
     2581  return tr_rpcGetWhitelist (session->rpcServer);
    25622582}
    25632583
     
    25652585tr_sessionSetRPCWhitelistEnabled (tr_session * session, bool isEnabled)
    25662586{
    2567     assert (tr_isSession (session));
    2568 
    2569     tr_rpcSetWhitelistEnabled (session->rpcServer, isEnabled);
     2587  assert (tr_isSession (session));
     2588
     2589  tr_rpcSetWhitelistEnabled (session->rpcServer, isEnabled);
    25702590}
    25712591
     
    25732593tr_sessionGetRPCWhitelistEnabled (const tr_session * session)
    25742594{
    2575     assert (tr_isSession (session));
    2576 
    2577     return tr_rpcGetWhitelistEnabled (session->rpcServer);
     2595  assert (tr_isSession (session));
     2596
     2597  return tr_rpcGetWhitelistEnabled (session->rpcServer);
    25782598}
    25792599
     
    25832603                          const char * password)
    25842604{
    2585     assert (tr_isSession (session));
    2586 
    2587     tr_rpcSetPassword (session->rpcServer, password);
     2605  assert (tr_isSession (session));
     2606
     2607  tr_rpcSetPassword (session->rpcServer, password);
    25882608}
    25892609
     
    25912611tr_sessionGetRPCPassword (const tr_session * session)
    25922612{
    2593     assert (tr_isSession (session));
    2594 
    2595     return tr_rpcGetPassword (session->rpcServer);
     2613  assert (tr_isSession (session));
     2614
     2615  return tr_rpcGetPassword (session->rpcServer);
    25962616}
    25972617
     
    26002620                          const char * username)
    26012621{
    2602     assert (tr_isSession (session));
    2603 
    2604     tr_rpcSetUsername (session->rpcServer, username);
     2622  assert (tr_isSession (session));
     2623
     2624  tr_rpcSetUsername (session->rpcServer, username);
    26052625}
    26062626
     
    26082628tr_sessionGetRPCUsername (const tr_session * session)
    26092629{
    2610     assert (tr_isSession (session));
    2611 
    2612     return tr_rpcGetUsername (session->rpcServer);
     2630  assert (tr_isSession (session));
     2631
     2632  return tr_rpcGetUsername (session->rpcServer);
    26132633}
    26142634
     
    26162636tr_sessionSetRPCPasswordEnabled (tr_session * session, bool isEnabled)
    26172637{
    2618     assert (tr_isSession (session));
    2619 
    2620     tr_rpcSetPasswordEnabled (session->rpcServer, isEnabled);
     2638  assert (tr_isSession (session));
     2639
     2640  tr_rpcSetPasswordEnabled (session->rpcServer, isEnabled);
    26212641}
    26222642
     
    26242644tr_sessionIsRPCPasswordEnabled (const tr_session * session)
    26252645{
    2626     assert (tr_isSession (session));
    2627 
    2628     return tr_rpcIsPasswordEnabled (session->rpcServer);
     2646  assert (tr_isSession (session));
     2647
     2648  return tr_rpcIsPasswordEnabled (session->rpcServer);
    26292649}
    26302650
     
    26322652tr_sessionGetRPCBindAddress (const tr_session * session)
    26332653{
    2634     assert (tr_isSession (session));
    2635 
    2636     return tr_rpcGetBindAddress (session->rpcServer);
     2654  assert (tr_isSession (session));
     2655
     2656  return tr_rpcGetBindAddress (session->rpcServer);
    26372657}
    26382658
     
    26442664tr_sessionIsTorrentDoneScriptEnabled (const tr_session * session)
    26452665{
    2646     assert (tr_isSession (session));
    2647 
    2648     return session->isTorrentDoneScriptEnabled;
     2666  assert (tr_isSession (session));
     2667
     2668  return session->isTorrentDoneScriptEnabled;
    26492669}
    26502670
     
    26522672tr_sessionSetTorrentDoneScriptEnabled (tr_session * session, bool isEnabled)
    26532673{
    2654     assert (tr_isSession (session));
    2655     assert (tr_isBool (isEnabled));
    2656 
    2657     session->isTorrentDoneScriptEnabled = isEnabled;
     2674  assert (tr_isSession (session));
     2675  assert (tr_isBool (isEnabled));
     2676
     2677  session->isTorrentDoneScriptEnabled = isEnabled;
    26582678}
    26592679
     
    26612681tr_sessionGetTorrentDoneScript (const tr_session * session)
    26622682{
    2663     assert (tr_isSession (session));
    2664 
    2665     return session->torrentDoneScript;
     2683  assert (tr_isSession (session));
     2684
     2685  return session->torrentDoneScript;
    26662686}
    26672687
     
    26692689tr_sessionSetTorrentDoneScript (tr_session * session, const char * scriptFilename)
    26702690{
    2671     assert (tr_isSession (session));
    2672 
    2673     if (session->torrentDoneScript != scriptFilename)
    2674     {
    2675         tr_free (session->torrentDoneScript);
    2676         session->torrentDoneScript = tr_strdup (scriptFilename);
     2691  assert (tr_isSession (session));
     2692
     2693  if (session->torrentDoneScript != scriptFilename)
     2694    {
     2695      tr_free (session->torrentDoneScript);
     2696      session->torrentDoneScript = tr_strdup (scriptFilename);
    26772697    }
    26782698}
     
    26852705tr_sessionSetQueueSize (tr_session * session, tr_direction dir, int n)
    26862706{
    2687     assert (tr_isSession (session));
    2688     assert (tr_isDirection (dir));
    2689 
    2690     session->queueSize[dir] = n;
     2707  assert (tr_isSession (session));
     2708  assert (tr_isDirection (dir));
     2709
     2710  session->queueSize[dir] = n;
    26912711}
    26922712
     
    26942714tr_sessionGetQueueSize (const tr_session * session, tr_direction dir)
    26952715{
    2696     assert (tr_isSession (session));
    2697     assert (tr_isDirection (dir));
    2698 
    2699     return session->queueSize[dir];
     2716  assert (tr_isSession (session));
     2717  assert (tr_isDirection (dir));
     2718
     2719  return session->queueSize[dir];
    27002720}
    27012721
     
    27032723tr_sessionSetQueueEnabled (tr_session * session, tr_direction dir, bool is_enabled)
    27042724{
    2705     assert (tr_isSession (session));
    2706     assert (tr_isDirection (dir));
    2707     assert (tr_isBool (is_enabled));
    2708 
    2709     session->queueEnabled[dir] = is_enabled;
     2725  assert (tr_isSession (session));
     2726  assert (tr_isDirection (dir));
     2727  assert (tr_isBool (is_enabled));
     2728
     2729  session->queueEnabled[dir] = is_enabled;
    27102730}
    27112731
     
    27132733tr_sessionGetQueueEnabled (const tr_session * session, tr_direction dir)
    27142734{
    2715     assert (tr_isSession (session));
    2716     assert (tr_isDirection (dir));
    2717 
    2718     return session->queueEnabled[dir];
     2735  assert (tr_isSession (session));
     2736  assert (tr_isDirection (dir));
     2737
     2738  return session->queueEnabled[dir];
    27192739}
    27202740
     
    27222742tr_sessionSetQueueStalledMinutes (tr_session * session, int minutes)
    27232743{
    2724     assert (tr_isSession (session));
    2725     assert (minutes > 0);
    2726 
    2727     session->queueStalledMinutes = minutes;
     2744  assert (tr_isSession (session));
     2745  assert (minutes > 0);
     2746
     2747  session->queueStalledMinutes = minutes;
    27282748}
    27292749
     
    27312751tr_sessionSetQueueStalledEnabled (tr_session * session, bool is_enabled)
    27322752{
    2733     assert (tr_isSession (session));
    2734     assert (tr_isBool (is_enabled));
    2735 
    2736     session->stalledEnabled = is_enabled;
     2753  assert (tr_isSession (session));
     2754  assert (tr_isBool (is_enabled));
     2755
     2756  session->stalledEnabled = is_enabled;
    27372757}
    27382758
     
    27402760tr_sessionGetQueueStalledEnabled (const tr_session * session)
    27412761{
    2742     assert (tr_isSession (session));
    2743 
    2744     return session->stalledEnabled;
     2762  assert (tr_isSession (session));
     2763
     2764  return session->stalledEnabled;
    27452765}
    27462766
     
    27482768tr_sessionGetQueueStalledMinutes (const tr_session * session)
    27492769{
    2750     assert (tr_isSession (session));
    2751 
    2752     return session->queueStalledMinutes;
     2770  assert (tr_isSession (session));
     2771
     2772  return session->queueStalledMinutes;
    27532773}
    27542774
     
    28272847tr_sessionCountQueueFreeSlots (tr_session * session, tr_direction dir)
    28282848{
    2829     tr_torrent * tor;
    2830     int active_count;
    2831     const int max = tr_sessionGetQueueSize (session, dir);
    2832     const tr_torrent_activity activity = dir == TR_UP ? TR_STATUS_SEED : TR_STATUS_DOWNLOAD;
    2833 
    2834     if (!tr_sessionGetQueueEnabled (session, dir))
    2835         return INT_MAX;
     2849  tr_torrent * tor;
     2850  int active_count;
     2851  const int max = tr_sessionGetQueueSize (session, dir);
     2852  const tr_torrent_activity activity = dir == TR_UP ? TR_STATUS_SEED : TR_STATUS_DOWNLOAD;
     2853
     2854  if (!tr_sessionGetQueueEnabled (session, dir))
     2855    return INT_MAX;
    28362856
    28372857    tor = NULL;
Note: See TracChangeset for help on using the changeset viewer.