more consts refs and formatting (#1080)

This commit is contained in:
Alden Torres 2016-09-12 09:20:15 -04:00 committed by Arvid Norberg
parent fbf926da1f
commit 3fa74c6fd5
8 changed files with 82 additions and 69 deletions

View File

@ -64,7 +64,7 @@ struct obfuscated_get_peers : get_peers
{
typedef get_peers::nodes_callback done_callback;
obfuscated_get_peers(node& dht_node, node_id target
obfuscated_get_peers(node& dht_node, node_id const& target
, data_callback const& dcallback
, nodes_callback const& ncallback
, bool noseeds);

View File

@ -112,7 +112,7 @@ public:
void tick();
void bootstrap(std::vector<udp::endpoint> const& nodes
, find_data::nodes_callback const& f);
void add_router_node(udp::endpoint router);
void add_router_node(udp::endpoint const& router);
void unreachable(udp::endpoint const& ep);
void incoming(msg const& m);

View File

@ -45,33 +45,33 @@ namespace libtorrent { namespace dht
struct node_entry;
typedef libtorrent::sha1_hash node_id;
using node_id = libtorrent::sha1_hash;
// returns the distance between the two nodes
// using the kademlia XOR-metric
node_id TORRENT_EXTRA_EXPORT distance(node_id const& n1, node_id const& n2);
TORRENT_EXTRA_EXPORT node_id distance(node_id const& n1, node_id const& n2);
// returns true if: distance(n1, ref) < distance(n2, ref)
bool TORRENT_EXTRA_EXPORT compare_ref(node_id const& n1, node_id const& n2, node_id const& ref);
TORRENT_EXTRA_EXPORT bool compare_ref(node_id const& n1, node_id const& n2, node_id const& ref);
// returns n in: 2^n <= distance(n1, n2) < 2^(n+1)
// useful for finding out which bucket a node belongs to
// the value that's returned is the number of trailing bits
// after the shared bit prefix of ``n1`` and ``n2``.
// if the first bits are different, that's 160.
int TORRENT_EXTRA_EXPORT distance_exp(node_id const& n1, node_id const& n2);
int TORRENT_EXTRA_EXPORT min_distance_exp(node_id const& n1, std::vector<node_id> const& ids);
TORRENT_EXTRA_EXPORT int distance_exp(node_id const& n1, node_id const& n2);
TORRENT_EXTRA_EXPORT int min_distance_exp(node_id const& n1, std::vector<node_id> const& ids);
node_id TORRENT_EXTRA_EXPORT generate_id(address const& external_ip);
node_id TORRENT_EXTRA_EXPORT generate_random_id();
void TORRENT_EXTRA_EXPORT make_id_secret(node_id& in);
node_id TORRENT_EXTRA_EXPORT generate_secret_id();
bool TORRENT_EXTRA_EXPORT verify_secret_id(node_id const& nid);
node_id TORRENT_EXTRA_EXPORT generate_id_impl(address const& ip_, std::uint32_t r);
TORRENT_EXTRA_EXPORT node_id generate_id(address const& external_ip);
TORRENT_EXTRA_EXPORT node_id generate_random_id();
TORRENT_EXTRA_EXPORT void make_id_secret(node_id& in);
TORRENT_EXTRA_EXPORT node_id generate_secret_id();
TORRENT_EXTRA_EXPORT bool verify_secret_id(node_id const& nid);
TORRENT_EXTRA_EXPORT node_id generate_id_impl(address const& ip_, std::uint32_t r);
bool TORRENT_EXTRA_EXPORT verify_id(node_id const& nid, address const& source_ip);
bool TORRENT_EXTRA_EXPORT matching_prefix(node_entry const& n, int mask, int prefix, int offset);
node_id TORRENT_EXTRA_EXPORT generate_prefix_mask(int bits);
TORRENT_EXTRA_EXPORT bool verify_id(node_id const& nid, address const& source_ip);
TORRENT_EXTRA_EXPORT bool matching_prefix(node_entry const& n, int mask, int prefix, int offset);
TORRENT_EXTRA_EXPORT node_id generate_prefix_mask(int bits);
} } // namespace libtorrent::dht

View File

@ -90,9 +90,9 @@ struct ipv6_hash
struct ip_set
{
void insert(address addr);
size_t count(address addr);
void erase(address addr);
void insert(address const& addr);
size_t count(address const& addr);
void erase(address const& addr);
void clear()
{
@ -167,7 +167,7 @@ public:
// adds an endpoint that will never be added to
// the routing table
void add_router_node(udp::endpoint router);
void add_router_node(udp::endpoint const& router);
// iterates over the router nodes added
typedef std::set<udp::endpoint>::const_iterator router_iterator;
@ -187,7 +187,7 @@ public:
// a sign of a node being alive. This node will either
// be inserted in the k-buckets or be moved to the top
// of its bucket.
bool node_seen(node_id const& id, udp::endpoint ep, int rtt);
bool node_seen(node_id const& id, udp::endpoint const& ep, int rtt);
// this may add a node to the routing table and mark it as
// not pinged. If the bucket the node falls into is full,
@ -196,7 +196,7 @@ public:
// change our node ID. This can be expensive since nodes must be moved around
// and potentially dropped
void update_node_id(node_id id);
void update_node_id(node_id const& id);
node_entry const* next_refresh();

View File

@ -111,10 +111,12 @@ void find_data::got_write_token(node_id const& n, std::string write_token)
#ifndef TORRENT_DISABLE_LOGGING
auto logger = get_node().observer();
if (logger != nullptr && logger->should_log(dht_logger::traversal))
{
logger->log(dht_logger::traversal
, "[%p] adding write token '%s' under id '%s'"
, static_cast<void*>(this), aux::to_hex(write_token).c_str()
, aux::to_hex(n).c_str());
}
#endif
m_write_tokens[n] = std::move(write_token);
}
@ -138,11 +140,13 @@ void find_data::done()
#ifndef TORRENT_DISABLE_LOGGING
auto logger = get_node().observer();
if (logger != nullptr)
{
logger->log(dht_logger::traversal, "[%p] %s DONE"
, static_cast<void*>(this), name());
}
#endif
std::vector<std::pair<node_entry, std::string> > results;
std::vector<std::pair<node_entry, std::string>> results;
int num_results = m_node.m_table.bucket_size();
for (std::vector<observer_ptr>::iterator i = m_results.begin()
, end(m_results.end()); i != end && num_results > 0; ++i)
@ -152,26 +156,32 @@ void find_data::done()
{
#ifndef TORRENT_DISABLE_LOGGING
if (logger != nullptr && logger->should_log(dht_logger::traversal))
{
logger->log(dht_logger::traversal, "[%p] not alive: %s"
, static_cast<void*>(this), print_endpoint(o->target_ep()).c_str());
}
#endif
continue;
}
std::map<node_id, std::string>::iterator j = m_write_tokens.find(o->id());
auto j = m_write_tokens.find(o->id());
if (j == m_write_tokens.end())
{
#ifndef TORRENT_DISABLE_LOGGING
if (logger != nullptr && logger->should_log(dht_logger::traversal))
{
logger->log(dht_logger::traversal, "[%p] no write token: %s"
, static_cast<void*>(this), print_endpoint(o->target_ep()).c_str());
}
#endif
continue;
}
results.push_back(std::make_pair(node_entry(o->id(), o->target_ep()), j->second));
#ifndef TORRENT_DISABLE_LOGGING
if (logger != nullptr && logger->should_log(dht_logger::traversal))
{
logger->log(dht_logger::traversal, "[%p] %s"
, static_cast<void*>(this), print_endpoint(o->target_ep()).c_str());
}
#endif
--num_results;
}

View File

@ -73,19 +73,21 @@ void get_peers_observer::reply(msg const& m)
#ifndef TORRENT_DISABLE_LOGGING
auto logger = get_observer();
bdecode_node const id = r.dict_find_string("id");
if (logger != nullptr && logger->should_log(dht_logger::traversal)
&& id && id.string_length() == 20)
if (logger != nullptr && logger->should_log(dht_logger::traversal))
{
logger->log(dht_logger::traversal, "[%p] PEERS "
"invoke-count: %d branch-factor: %d addr: %s id: %s distance: %d p: %d"
, static_cast<void*>(algorithm())
, algorithm()->invoke_count()
, algorithm()->branch_factor()
, print_endpoint(m.addr).c_str()
, aux::to_hex({id.string_ptr(), size_t(id.string_length())}).c_str()
, distance_exp(algorithm()->target(), node_id(id.string_ptr()))
, int((end - peers) / 6));
bdecode_node const id = r.dict_find_string("id");
if (id && id.string_length() == 20)
{
logger->log(dht_logger::traversal, "[%p] PEERS "
"invoke-count: %d branch-factor: %d addr: %s id: %s distance: %d p: %d"
, static_cast<void*>(algorithm())
, algorithm()->invoke_count()
, algorithm()->branch_factor()
, print_endpoint(m.addr).c_str()
, aux::to_hex({id.string_ptr(), size_t(id.string_length())}).c_str()
, distance_exp(algorithm()->target(), node_id(id.string_ptr()))
, int((end - peers) / 6));
}
}
#endif
while (end - peers >= 6)
@ -97,19 +99,21 @@ void get_peers_observer::reply(msg const& m)
read_endpoint_list<tcp::endpoint>(n, peer_list);
#ifndef TORRENT_DISABLE_LOGGING
auto logger = get_observer();
bdecode_node const id = r.dict_find_string("id");
if (logger != nullptr && logger->should_log(dht_logger::traversal)
&& id && id.string_length() == 20)
if (logger != nullptr && logger->should_log(dht_logger::traversal))
{
logger->log(dht_logger::traversal, "[%p] PEERS "
"invoke-count: %d branch-factor: %d addr: %s id: %s distance: %d p: %d"
, static_cast<void*>(algorithm())
, algorithm()->invoke_count()
, algorithm()->branch_factor()
, print_endpoint(m.addr).c_str()
, aux::to_hex({id.string_ptr(), size_t(id.string_length())}).c_str()
, distance_exp(algorithm()->target(), node_id(id.string_ptr()))
, n.list_size());
bdecode_node const id = r.dict_find_string("id");
if (id && id.string_length() == 20)
{
logger->log(dht_logger::traversal, "[%p] PEERS "
"invoke-count: %d branch-factor: %d addr: %s id: %s distance: %d p: %d"
, static_cast<void*>(algorithm())
, algorithm()->invoke_count()
, algorithm()->branch_factor()
, print_endpoint(m.addr).c_str()
, aux::to_hex({id.string_ptr(), size_t(id.string_length())}).c_str()
, distance_exp(algorithm()->target(), node_id(id.string_ptr()))
, n.list_size());
}
}
#endif
}
@ -154,7 +158,7 @@ bool get_peers::invoke(observer_ptr o)
a["info_hash"] = m_target.to_string();
if (m_noseeds) a["noseed"] = 1;
if (m_node.observer())
if (m_node.observer() != nullptr)
{
m_node.observer()->outgoing_get_peers(m_target, m_target, o->target_ep());
}
@ -176,7 +180,7 @@ observer_ptr get_peers::new_observer(udp::endpoint const& ep
obfuscated_get_peers::obfuscated_get_peers(
node& dht_node
, node_id info_hash
, node_id const& info_hash
, data_callback const& dcallback
, nodes_callback const& ncallback
, bool noseeds)
@ -215,8 +219,8 @@ bool obfuscated_get_peers::invoke(observer_ptr o)
{
if (!m_obfuscated) return get_peers::invoke(o);
const node_id id = o->id();
const int shared_prefix = 160 - distance_exp(id, m_target);
node_id const& id = o->id();
int const shared_prefix = 160 - distance_exp(id, m_target);
// when we get close to the target zone in the DHT
// start using the correct info-hash, in order to
@ -256,7 +260,7 @@ bool obfuscated_get_peers::invoke(observer_ptr o)
obfuscated_target |= m_target & mask;
a["info_hash"] = obfuscated_target.to_string();
if (m_node.observer())
if (m_node.observer() != nullptr)
{
m_node.observer()->outgoing_get_peers(m_target, obfuscated_target
, o->target_ep());

View File

@ -348,15 +348,15 @@ void node::incoming(msg const& m)
namespace
{
void announce_fun(std::vector<std::pair<node_entry, std::string>> const& v
, node& node, int listen_port, sha1_hash const& ih, int flags)
, node& node, int const listen_port, sha1_hash const& ih, int const flags)
{
#ifndef TORRENT_DISABLE_LOGGING
if (node.observer() != nullptr
&& node.observer()->should_log(dht_logger::node))
auto logger = node.observer();
if (logger != nullptr && logger->should_log(dht_logger::node))
{
char hex_ih[41];
aux::to_hex(ih, hex_ih);
node.observer()->log(dht_logger::node, "sending announce_peer [ ih: %s "
logger->log(dht_logger::node, "sending announce_peer [ ih: %s "
" p: %d nodes: %d ]", hex_ih, listen_port, int(v.size()));
}
#endif
@ -367,10 +367,9 @@ namespace
for (auto const& p : v)
{
#ifndef TORRENT_DISABLE_LOGGING
if (node.observer() != nullptr
&& node.observer()->should_log(dht_logger::node))
if (logger != nullptr && logger->should_log(dht_logger::node))
{
node.observer()->log(dht_logger::node, "announce-distance: %d"
logger->log(dht_logger::node, "announce-distance: %d"
, (160 - distance_exp(ih, p.first.id)));
}
#endif
@ -396,7 +395,7 @@ namespace
}
}
void node::add_router_node(udp::endpoint router)
void node::add_router_node(udp::endpoint const& router)
{
#ifndef TORRENT_DISABLE_LOGGING
if (m_observer != nullptr && m_observer->should_log(dht_logger::node))
@ -418,7 +417,7 @@ void node::add_node(udp::endpoint const& node)
void node::get_peers(sha1_hash const& info_hash
, std::function<void(std::vector<tcp::endpoint> const&)> dcallback
, std::function<void(std::vector<std::pair<node_entry, std::string> > const&)> ncallback
, std::function<void(std::vector<std::pair<node_entry, std::string>> const&)> ncallback
, bool noseeds)
{
// search for nodes with ids close to id or with peers

View File

@ -78,7 +78,7 @@ namespace
}
}
void ip_set::insert(address addr)
void ip_set::insert(address const& addr)
{
#if TORRENT_USE_IPV6
if (addr.is_v6())
@ -88,7 +88,7 @@ void ip_set::insert(address addr)
m_ip4s.insert(addr.to_v4().to_bytes());
}
size_t ip_set::count(address addr)
size_t ip_set::count(address const& addr)
{
#if TORRENT_USE_IPV6
if (addr.is_v6())
@ -98,7 +98,7 @@ size_t ip_set::count(address addr)
return m_ip4s.count(addr.to_v4().to_bytes());
}
void ip_set::erase(address addr)
void ip_set::erase(address const& addr)
{
#if TORRENT_USE_IPV6
if (addr.is_v6())
@ -607,7 +607,7 @@ bool routing_table::add_node(node_entry e)
split_bucket();
// if this assert triggers a lot in the wild, we should probably
// harden our resistence towards this attack. Perhaps by never
// harden our resistance towards this attack. Perhaps by never
// splitting a bucket (and discard nodes) if the two buckets above it
// are empty or close to empty
// TORRENT_ASSERT(m_buckets.size() <= 50);
@ -1126,7 +1126,7 @@ void routing_table::split_bucket()
}
}
void routing_table::update_node_id(node_id id)
void routing_table::update_node_id(node_id const& id)
{
m_id = id;
@ -1260,7 +1260,7 @@ void routing_table::node_failed(node_id const& nid, udp::endpoint const& ep)
fill_from_replacements(i);
}
void routing_table::add_router_node(udp::endpoint router)
void routing_table::add_router_node(udp::endpoint const& router)
{
m_router_nodes.insert(router);
}
@ -1278,7 +1278,7 @@ void routing_table::heard_about(node_id const& id, udp::endpoint const& ep)
// top of its bucket. the return value indicates if the table needs a refresh.
// if true, the node should refresh the table (i.e. do a find_node on its own
// id)
bool routing_table::node_seen(node_id const& id, udp::endpoint ep, int rtt)
bool routing_table::node_seen(node_id const& id, udp::endpoint const& ep, int rtt)
{
if (!verify_node_address(m_settings, id, ep.address())) return false;
return add_node(node_entry(id, ep, rtt, true));