From 6e9df49912a607d7cb4fb62b877e8e480b2b064a Mon Sep 17 00:00:00 2001 From: rakshasa Date: Sun, 27 Mar 2011 10:23:16 +0000 Subject: [PATCH] * Fixed some missing headers. * Added ip table framework. git-svn-id: svn://rakshasa.no/libtorrent/trunk/rtorrent@1193 e378c898-3ddf-0310-93e7-cc216c733640 --- src/command_network.cc | 76 ++++++++++++++++++++++++++++ src/globals.cc | 1 + src/globals.h | 2 + src/main.cc | 8 ++- src/rpc/Makefile.am | 1 + src/rpc/ip_table_list.h | 90 +++++++++++++++++++++++++++++++++ src/ui/element_download_list.cc | 3 +- 7 files changed, 179 insertions(+), 2 deletions(-) create mode 100644 src/rpc/ip_table_list.h diff --git a/src/command_network.cc b/src/command_network.cc index ee6b5541..aa5951df 100644 --- a/src/command_network.cc +++ b/src/command_network.cc @@ -411,6 +411,78 @@ apply_xmlrpc_dialect(const std::string& arg) { return torrent::Object(); } +torrent::Object +apply_ip_tables_insert_table(const std::string& args) { + if (ip_tables.find(args) != ip_tables.end()) + throw torrent::input_error("IP table already exists."); + + ip_tables.insert(args); + return torrent::Object(); +} + +torrent::Object +apply_ip_tables_get(const torrent::Object::list_type& args) { + if (args.size() != 2) + throw torrent::input_error("Incorrect number of arguments."); + + torrent::Object::list_const_iterator args_itr = args.begin(); + + const std::string& name = (args_itr++)->as_string(); + const std::string& address = (args_itr++)->as_string(); + + // Move to a helper function, add support for addresses. + uint32_t ip_values[4]; + + if (sscanf(address.c_str(), "%u.%u.%u.%u", + ip_values + 0, ip_values + 1, ip_values + 2, ip_values + 3) != 4) + throw torrent::input_error("Invalid address format."); + + rpc::ip_table_list::iterator table_itr = ip_tables.find(name); + + if (table_itr == ip_tables.end()) + throw torrent::input_error("Could not find ip table."); + + return table_itr->table.at((ip_values[0] << 24) + (ip_values[1] << 16) + (ip_values[2] << 8) + ip_values[3]); +} + +torrent::Object +apply_ip_tables_add_address(const torrent::Object::list_type& args) { + if (args.size() != 3) + throw torrent::input_error("Incorrect number of arguments."); + + torrent::Object::list_const_iterator args_itr = args.begin(); + + const std::string& name = (args_itr++)->as_string(); + const std::string& address = (args_itr++)->as_string(); + const std::string& value_str = (args_itr++)->as_string(); + + // Move to a helper function, add support for addresses. + uint32_t ip_values[4]; + unsigned int block = rpc::ipv4_table::mask_bits; + + if (sscanf(address.c_str(), "%u.%u.%u.%u/%u", + ip_values + 0, ip_values + 1, ip_values + 2, ip_values + 3, &block) < 4 || + block > rpc::ipv4_table::mask_bits) + throw torrent::input_error("Invalid address format."); + + int value; + + if (value_str == "block") + value = 1; + else + throw torrent::input_error("Invalid value."); + + rpc::ip_table_list::iterator table_itr = ip_tables.find(name); + + if (table_itr == ip_tables.end()) + throw torrent::input_error("Could not find ip table."); + + table_itr->table.insert((ip_values[0] << 24) + (ip_values[1] << 16) + (ip_values[2] << 8) + ip_values[3], + rpc::ipv4_table::mask_bits - block, value); + + return torrent::Object(); +} + void initialize_command_network() { torrent::ConnectionManager* cm = torrent::connection_manager(); @@ -519,6 +591,10 @@ initialize_command_network() { CMD2_VAR_VALUE ("trackers.numwant", -1); CMD2_VAR_BOOL ("trackers.use_udp", true); + CMD2_ANY_STRING ("ip_tables.insert_table", std::tr1::bind(&apply_ip_tables_insert_table, std::tr1::placeholders::_2)); + CMD2_ANY_LIST ("ip_tables.get", std::tr1::bind(&apply_ip_tables_get, std::tr1::placeholders::_2)); + CMD2_ANY_LIST ("ip_tables.add_address", std::tr1::bind(&apply_ip_tables_add_address, std::tr1::placeholders::_2)); + // CMD2_ANY_V ("dht.enable", std::tr1::bind(&core::DhtManager::set_start, control->dht_manager())); // CMD2_ANY_V ("dht.disable", std::tr1::bind(&core::DhtManager::set_stop, control->dht_manager())); CMD2_ANY_STRING_V("dht.mode.set", std::tr1::bind(&core::DhtManager::set_mode, control->dht_manager(), std::tr1::placeholders::_2)); diff --git a/src/globals.cc b/src/globals.cc index 0d7ff709..aa63b70f 100644 --- a/src/globals.cc +++ b/src/globals.cc @@ -40,6 +40,7 @@ rak::priority_queue_default taskScheduler; rak::timer cachedTime; +rpc::ip_table_list ip_tables; Control* control = NULL; //__thread ThreadBase* main_thread = NULL; diff --git a/src/globals.h b/src/globals.h index d5b55247..22c4b8f9 100644 --- a/src/globals.h +++ b/src/globals.h @@ -43,6 +43,7 @@ #include "thread_base.h" #include "thread_main.h" #include "thread_worker.h" +#include "rpc/ip_table_list.h" class Control; @@ -53,6 +54,7 @@ class Control; extern rak::priority_queue_default taskScheduler; extern rak::timer cachedTime; +extern rpc::ip_table_list ip_tables; extern Control* control; // extern __thread ThreadBase* main_thread; // Only use for worker threads for now. diff --git a/src/main.cc b/src/main.cc index 49d01fb1..19bff2e0 100644 --- a/src/main.cc +++ b/src/main.cc @@ -297,12 +297,18 @@ main(int argc, char** argv) { // "view.sort_new = hashing,less=d.state_changed=\n" // "view.sort_current = hashing,less=d.state_changed=\n" - "view.add = seeding\n" + "view.add = seeding\n" "view.filter = seeding,\"and=d.state=,d.complete=\"\n" "view.filter_on = seeding,event.download.resumed,event.download.paused,event.download.finished\n" "view.sort_new = seeding,less=d.state_changed=\n" "view.sort_current = seeding,less=d.state_changed=\n" + "view.add = leeching\n" + "view.filter = leeching,\"and=d.state=,not=$d.complete=\"\n" + "view.filter_on = leeching,event.download.resumed,event.download.paused,event.download.finished\n" + "view.sort_new = leeching,less=d.state_changed=\n" + "view.sort_current = leeching,less=d.state_changed=\n" + "schedule2 = view.main,10,10,((view.sort,main,20))\n" "schedule2 = view.name,10,10,((view.sort,name,20))\n" diff --git a/src/rpc/Makefile.am b/src/rpc/Makefile.am index 60ebd5d0..849a9c4c 100644 --- a/src/rpc/Makefile.am +++ b/src/rpc/Makefile.am @@ -13,6 +13,7 @@ libsub_rpc_a_SOURCES = \ exec_file.cc \ exec_file.h \ fixed_key.h \ + ip_table_list.h \ object_storage.cc \ object_storage.h \ parse.cc \ diff --git a/src/rpc/ip_table_list.h b/src/rpc/ip_table_list.h new file mode 100644 index 00000000..d624a8e3 --- /dev/null +++ b/src/rpc/ip_table_list.h @@ -0,0 +1,90 @@ +// rTorrent - BitTorrent client +// Copyright (C) 2005-2007, Jari Sundell +// +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +// +// In addition, as a special exception, the copyright holders give +// permission to link the code of portions of this program with the +// OpenSSL library under certain conditions as described in each +// individual source file, and distribute linked combinations +// including the two. +// +// You must obey the GNU General Public License in all respects for +// all of the code used other than OpenSSL. If you modify file(s) +// with this exception, you may extend this exception to your version +// of the file(s), but you are not obligated to do so. If you do not +// wish to do so, delete this exception statement from your version. +// If you delete this exception statement from all source files in the +// program, then also delete it here. +// +// Contact: Jari Sundell +// +// Skomakerveien 33 +// 3185 Skoppum, NORWAY + +#ifndef RTORRENT_RPC_IP_TABLE_LISTS_H +#define RTORRENT_RPC_IP_TABLE_LISTS_H + +#include +#include +#include +#include +#include + +namespace rpc { + +typedef torrent::extents ipv4_table; + +struct ip_table_node { + std::string name; + ipv4_table table; + + bool equal_name(const std::string& str) const { return str == name; } +}; + +class ip_table_list : private std::vector { +public: + typedef std::vector base_type; + + using base_type::iterator; + using base_type::const_iterator; + using base_type::value_type; + + using base_type::begin; + using base_type::end; + + iterator insert(const std::string& name); + iterator find(const std::string& name); +}; + +inline ip_table_list::iterator +ip_table_list::insert(const std::string& name) { + ip_table_node tmp = { name }; + + return base_type::insert(end(), tmp); +} + +inline ip_table_list::iterator +ip_table_list::find(const std::string& name) { + for (iterator itr = begin(), last = end(); itr != last; itr++) + if (itr->equal_name(name)) + return itr; + + return end(); +} + +} + +#endif diff --git a/src/ui/element_download_list.cc b/src/ui/element_download_list.cc index 8fc08528..e8b26f07 100644 --- a/src/ui/element_download_list.cc +++ b/src/ui/element_download_list.cc @@ -93,7 +93,8 @@ ElementDownloadList::ElementDownloadList() : m_bindings['6'] = sigc::bind(sigc::mem_fun(*this, &ElementDownloadList::receive_change_view), "incomplete"); m_bindings['7'] = sigc::bind(sigc::mem_fun(*this, &ElementDownloadList::receive_change_view), "hashing"); m_bindings['8'] = sigc::bind(sigc::mem_fun(*this, &ElementDownloadList::receive_change_view), "seeding"); - m_bindings['9'] = sigc::bind(sigc::mem_fun(*this, &ElementDownloadList::receive_change_view), "active"); + m_bindings['9'] = sigc::bind(sigc::mem_fun(*this, &ElementDownloadList::receive_change_view), "leeching"); + m_bindings['0'] = sigc::bind(sigc::mem_fun(*this, &ElementDownloadList::receive_change_view), "active"); m_bindings[KEY_UP] = m_bindings['P' - '@'] = sigc::mem_fun(*this, &ElementDownloadList::receive_prev); m_bindings[KEY_DOWN] = m_bindings['N' - '@'] = sigc::mem_fun(*this, &ElementDownloadList::receive_next);