1 // rTorrent - BitTorrent client
2 // Copyright (C) 2005-2011, Jari Sundell
3 //
4 // This program is free software; you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation; either version 2 of the License, or
7 // (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 // GNU General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with this program; if not, write to the Free Software
16 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17 //
18 // In addition, as a special exception, the copyright holders give
19 // permission to link the code of portions of this program with the
20 // OpenSSL library under certain conditions as described in each
21 // individual source file, and distribute linked combinations
22 // including the two.
23 //
24 // You must obey the GNU General Public License in all respects for
25 // all of the code used other than OpenSSL.  If you modify file(s)
26 // with this exception, you may extend this exception to your version
27 // of the file(s), but you are not obligated to do so.  If you do not
28 // wish to do so, delete this exception statement from your version.
29 // If you delete this exception statement from all source files in the
30 // program, then also delete it here.
31 //
32 // Contact:  Jari Sundell <jaris@ifi.uio.no>
33 //
34 //           Skomakerveien 33
35 //           3185 Skoppum, NORWAY
36 
37 #include "config.h"
38 
39 #include <cstdio>
40 #include <rak/address_info.h>
41 #include <rak/error_number.h>
42 #include <torrent/dht_manager.h>
43 #include <torrent/tracker.h>
44 #include <torrent/utils/log.h>
45 
46 #include "core/download.h"
47 #include "core/manager.h"
48 
49 #include "globals.h"
50 #include "control.h"
51 #include "command_helpers.h"
52 #include "core/dht_manager.h"
53 
54 void
tracker_set_enabled(torrent::Tracker * tracker,bool state)55 tracker_set_enabled(torrent::Tracker* tracker, bool state) {
56   if (state)
57     tracker->enable();
58   else
59     tracker->disable();
60 }
61 
62 struct call_add_node_t {
call_add_node_tcall_add_node_t63   call_add_node_t(int port) : m_port(port) { }
64 
operator ()call_add_node_t65   void operator() (const sockaddr* sa, int err) {
66     if (sa == NULL) {
67       lt_log_print(torrent::LOG_DHT_WARN, "Could not resolve host.");
68     } else {
69       torrent::dht_manager()->add_node(sa, m_port);
70     }
71   }
72 
73   int m_port;
74 };
75 
76 torrent::Object
apply_dht_add_node(const std::string & arg)77 apply_dht_add_node(const std::string& arg) {
78   if (!torrent::dht_manager()->is_valid())
79     throw torrent::input_error("DHT not enabled.");
80 
81   int port, ret;
82   char dummy;
83   char host[1024];
84 
85   ret = std::sscanf(arg.c_str(), "%1023[^:]:%i%c", host, &port, &dummy);
86 
87   if (ret == 1)
88     port = 6881;
89   else if (ret != 2)
90     throw torrent::input_error("Could not parse host.");
91 
92   if (port < 1 || port > 65535)
93     throw torrent::input_error("Invalid port number.");
94 
95   torrent::connection_manager()->resolver()(host, (int)rak::socket_address::pf_inet, SOCK_DGRAM, call_add_node_t(port));
96   return torrent::Object();
97 }
98 
99 torrent::Object
apply_enable_trackers(int64_t arg)100 apply_enable_trackers(int64_t arg) {
101   for (core::Manager::DListItr itr = control->core()->download_list()->begin(), last = control->core()->download_list()->end(); itr != last; ++itr) {
102     std::for_each((*itr)->tracker_list()->begin(), (*itr)->tracker_list()->end(),
103                   arg ? std::mem_fun(&torrent::Tracker::enable) : std::mem_fun(&torrent::Tracker::disable));
104 
105     if (arg && !rpc::call_command_value("trackers.use_udp"))
106       (*itr)->enable_udp_trackers(false);
107   }
108 
109   return torrent::Object();
110 }
111 
112 void
initialize_command_tracker()113 initialize_command_tracker() {
114   CMD2_TRACKER        ("t.is_open",           std::bind(&torrent::Tracker::is_busy, std::placeholders::_1));
115   CMD2_TRACKER        ("t.is_enabled",        std::bind(&torrent::Tracker::is_enabled, std::placeholders::_1));
116   CMD2_TRACKER        ("t.is_usable",         std::bind(&torrent::Tracker::is_usable, std::placeholders::_1));
117   CMD2_TRACKER        ("t.is_busy",           std::bind(&torrent::Tracker::is_busy, std::placeholders::_1));
118   CMD2_TRACKER        ("t.is_extra_tracker",  std::bind(&torrent::Tracker::is_extra_tracker, std::placeholders::_1));
119   CMD2_TRACKER        ("t.can_scrape",        std::bind(&torrent::Tracker::can_scrape, std::placeholders::_1));
120 
121   CMD2_TRACKER_V      ("t.enable",            std::bind(&torrent::Tracker::enable, std::placeholders::_1));
122   CMD2_TRACKER_V      ("t.disable",           std::bind(&torrent::Tracker::disable, std::placeholders::_1));
123 
124   CMD2_TRACKER_VALUE_V("t.is_enabled.set",    std::bind(&tracker_set_enabled, std::placeholders::_1, std::placeholders::_2));
125 
126   CMD2_TRACKER        ("t.url",               std::bind(&torrent::Tracker::url, std::placeholders::_1));
127   CMD2_TRACKER        ("t.group",             std::bind(&torrent::Tracker::group, std::placeholders::_1));
128   CMD2_TRACKER        ("t.type",              std::bind(&torrent::Tracker::type, std::placeholders::_1));
129   CMD2_TRACKER        ("t.id",                std::bind(&torrent::Tracker::tracker_id, std::placeholders::_1));
130 
131   CMD2_TRACKER        ("t.latest_event",      std::bind(&torrent::Tracker::latest_event, std::placeholders::_1));
132   CMD2_TRACKER        ("t.latest_new_peers",  std::bind(&torrent::Tracker::latest_new_peers, std::placeholders::_1));
133   CMD2_TRACKER        ("t.latest_sum_peers",  std::bind(&torrent::Tracker::latest_sum_peers, std::placeholders::_1));
134 
135   // Time since last connection, connection attempt.
136 
137   CMD2_TRACKER        ("t.normal_interval",   std::bind(&torrent::Tracker::normal_interval, std::placeholders::_1));
138   CMD2_TRACKER        ("t.min_interval",      std::bind(&torrent::Tracker::min_interval, std::placeholders::_1));
139 
140   CMD2_TRACKER        ("t.activity_time_next", std::bind(&torrent::Tracker::activity_time_next, std::placeholders::_1));
141   CMD2_TRACKER        ("t.activity_time_last", std::bind(&torrent::Tracker::activity_time_last, std::placeholders::_1));
142 
143   CMD2_TRACKER        ("t.success_time_next", std::bind(&torrent::Tracker::success_time_next, std::placeholders::_1));
144   CMD2_TRACKER        ("t.success_time_last", std::bind(&torrent::Tracker::success_time_last, std::placeholders::_1));
145   CMD2_TRACKER        ("t.success_counter",   std::bind(&torrent::Tracker::success_counter, std::placeholders::_1));
146 
147   CMD2_TRACKER        ("t.failed_time_next",  std::bind(&torrent::Tracker::failed_time_next, std::placeholders::_1));
148   CMD2_TRACKER        ("t.failed_time_last",  std::bind(&torrent::Tracker::failed_time_last, std::placeholders::_1));
149   CMD2_TRACKER        ("t.failed_counter",    std::bind(&torrent::Tracker::failed_counter, std::placeholders::_1));
150 
151   CMD2_TRACKER        ("t.scrape_time_last",  std::bind(&torrent::Tracker::scrape_time_last, std::placeholders::_1));
152   CMD2_TRACKER        ("t.scrape_counter",    std::bind(&torrent::Tracker::scrape_counter, std::placeholders::_1));
153 
154   CMD2_TRACKER        ("t.scrape_complete",   std::bind(&torrent::Tracker::scrape_complete, std::placeholders::_1));
155   CMD2_TRACKER        ("t.scrape_incomplete", std::bind(&torrent::Tracker::scrape_incomplete, std::placeholders::_1));
156   CMD2_TRACKER        ("t.scrape_downloaded", std::bind(&torrent::Tracker::scrape_downloaded, std::placeholders::_1));
157 
158   CMD2_ANY_VALUE      ("trackers.enable",     std::bind(&apply_enable_trackers, int64_t(1)));
159   CMD2_ANY_VALUE      ("trackers.disable",    std::bind(&apply_enable_trackers, int64_t(0)));
160   CMD2_VAR_VALUE      ("trackers.numwant",    -1);
161   CMD2_VAR_BOOL       ("trackers.use_udp",    true);
162 
163   CMD2_ANY_STRING_V   ("dht.mode.set",          std::bind(&core::DhtManager::set_mode, control->dht_manager(), std::placeholders::_2));
164   CMD2_VAR_VALUE      ("dht.port",              int64_t(6881));
165   CMD2_ANY_STRING     ("dht.add_node",          std::bind(&apply_dht_add_node, std::placeholders::_2));
166   CMD2_ANY            ("dht.statistics",        std::bind(&core::DhtManager::dht_statistics, control->dht_manager()));
167   CMD2_ANY            ("dht.throttle.name",     std::bind(&core::DhtManager::throttle_name, control->dht_manager()));
168   CMD2_ANY_STRING_V   ("dht.throttle.name.set", std::bind(&core::DhtManager::set_throttle_name, control->dht_manager(), std::placeholders::_2));
169 }
170