Add read-only support in dht_settings and outgoing query messages.

This commit is contained in:
Thomas Yuan 2015-09-01 17:07:38 -04:00
parent 5c614be0ce
commit 3d4ed9f37f
4 changed files with 25 additions and 3 deletions

View File

@ -83,7 +83,8 @@ public:
rpc_manager(node_id const& our_id
, routing_table& table
, udp_socket_interface* sock
, dht_logger* log);
, dht_logger* log
, bool read_only = false);
~rpc_manager();
void unreachable(udp::endpoint const& ep);
@ -130,6 +131,7 @@ private:
node_id m_our_id;
boost::uint32_t m_allocated_observers:31;
boost::uint32_t m_destructing:1;
bool m_read_only;
};
} } // namespace libtorrent::dht

View File

@ -1403,6 +1403,7 @@ namespace libtorrent
, ignore_dark_internet(true)
, block_timeout(5 * 60)
, block_ratelimit(5)
, read_only(false)
{}
// the maximum number of peers to send in a reply to ``get_peers``
@ -1488,6 +1489,15 @@ namespace libtorrent
// the max number of packets per second a DHT node is allowed to send
// without getting banned.
int block_ratelimit;
// when set, the other nodes won't kept this node in their routing
// tables, it's meant for low-power and/or ephemeral devices that
// cannot support the DHT, it is also useful for mobile devices which
// are sensitive to network traffic and battery life.
// this node no longer responds to 'query' messages, and will place a
// 'ro' key (value = 1) in the top-level message dictionary of outgoing
// query messages.
bool read_only;
};

View File

@ -109,7 +109,7 @@ node::node(udp_socket_interface* sock
: m_settings(settings)
, m_id(calculate_node_id(nid, observer))
, m_table(m_id, 8, settings, observer)
, m_rpc(m_id, m_table, sock, observer)
, m_rpc(m_id, m_table, sock, observer, settings.read_only)
, m_observer(observer)
, m_last_tracker_tick(aux::time_now())
, m_last_self_refresh(min_time())
@ -280,6 +280,10 @@ void node::incoming(msg const& m)
case 'q':
{
TORRENT_ASSERT(m.message.dict_find_string_value("y") == "q");
// When a DHT node enters the read-only state, it no longer
// responds to 'query' messages that it receives.
if (m_settings.read_only) break;
entry e;
incoming_request(m, e);
m_sock->send_packet(e, m.addr, 0);

View File

@ -164,7 +164,8 @@ enum { observer_size = max3<
rpc_manager::rpc_manager(node_id const& our_id
, routing_table& table, udp_socket_interface* sock
, dht_logger* log)
, dht_logger* log
, bool read_only)
: m_pool_allocator(observer_size, 10)
, m_sock(sock)
, m_log(log)
@ -173,6 +174,7 @@ rpc_manager::rpc_manager(node_id const& our_id
, m_our_id(our_id)
, m_allocated_observers(0)
, m_destructing(false)
, m_read_only(read_only)
{}
rpc_manager::~rpc_manager()
@ -439,6 +441,10 @@ bool rpc_manager::invoke(entry& e, udp::endpoint target_addr
io::write_uint16(tid, out);
e["t"] = transaction_id;
// When a DHT node enters the read-only state, in each outgoing query message,
// places a 'ro' key in the top-level message dictionary and sets its value to 1.
if (m_read_only) e["ro"] = 1;
o->set_target(target_addr);
o->set_transaction_id(tid);