consts and refactor, hash_address returns the hash (#1093)

consts and refactor
This commit is contained in:
Alden Torres 2016-09-15 21:13:43 -04:00 committed by Arvid Norberg
parent 578e9b3558
commit 0507764d04
13 changed files with 29 additions and 40 deletions

View File

@ -623,7 +623,7 @@ namespace libtorrent
virtual void log(module_t m, char const* fmt, ...)
override TORRENT_FORMAT(3,4);
virtual void log_packet(message_direction_t dir, char const* pkt, int len
, udp::endpoint node) override;
, udp::endpoint const& node) override;
#endif
virtual bool on_dht_request(string_view query

View File

@ -164,6 +164,7 @@ namespace libtorrent
return bind_ep.address();
}
// TODO: function not used and not exported in release?
// returns true if the given device exists
TORRENT_EXTRA_EXPORT bool has_interface(char const* name, io_service& ios
, error_code& ec);

View File

@ -60,7 +60,7 @@ namespace libtorrent { namespace dht
virtual bool should_log(module_t m) const = 0;
virtual void log(module_t m, char const* fmt, ...) TORRENT_FORMAT(3,4) = 0;
virtual void log_packet(message_direction_t dir, char const* pkt, int len
, udp::endpoint node) = 0;
, udp::endpoint const& node) = 0;
#endif
protected:

View File

@ -41,4 +41,3 @@ namespace libtorrent
}
#endif // TORRENT_PEER_ID_HPP_INCLUDED

View File

@ -52,7 +52,7 @@ namespace libtorrent
TORRENT_EXTRA_EXPORT std::string address_to_bytes(address const& a);
TORRENT_EXTRA_EXPORT std::string endpoint_to_bytes(udp::endpoint const& ep);
TORRENT_EXTRA_EXPORT void hash_address(address const& ip, sha1_hash& h);
TORRENT_EXTRA_EXPORT sha1_hash hash_address(address const& ip);
namespace detail
{

View File

@ -80,7 +80,7 @@ struct obs : dht::dht_observer
}
void log_packet(message_direction_t /* dir */
, char const* /* pkt */, int /* len */
, udp::endpoint /* node */) override {}
, udp::endpoint const& /* node */) override {}
bool on_dht_request(string_view /* query */
, dht::msg const& /* request */, entry& /* response */) override
{ return false; }

View File

@ -98,7 +98,7 @@ namespace libtorrent
}
bool ip_voter::cast_vote(address const& ip
, int source_type, address const& source)
, int const source_type, address const& source)
{
if (is_any(ip)) return false;
if (is_local(ip)) return false;
@ -111,8 +111,7 @@ namespace libtorrent
// this is the key to use for the bloom filters
// it represents the identity of the voter
sha1_hash k;
hash_address(source, k);
sha1_hash const k = hash_address(source);
// do we already have an entry for this external IP?
std::vector<external_ip_t>::iterator i = std::find_if(m_external_addresses.begin()
@ -153,7 +152,7 @@ namespace libtorrent
if (i->addr == m_external_address) return maybe_rotate();
if (m_external_address != address_v4())
if (m_external_address != address())
{
// we have a temporary external address. As soon as we have
// more than 25 votes, consider deciding which one to settle for
@ -188,4 +187,3 @@ namespace libtorrent
return ext;
}
}

View File

@ -117,8 +117,7 @@ namespace
f->last_seen = aux::time_now();
// maybe increase num_announcers if we haven't seen this IP before
sha1_hash iphash;
hash_address(addr, iphash);
sha1_hash const iphash = hash_address(addr);
if (!f->ips.find(iphash))
{
f->ips.set(iphash);
@ -216,12 +215,10 @@ namespace
bloom_filter<256> downloaders;
bloom_filter<256> seeds;
for (std::set<peer_entry>::const_iterator peer_it = v.peers.begin()
, end(v.peers.end()); peer_it != end; ++peer_it)
for (auto const& p : v.peers)
{
sha1_hash iphash;
hash_address(peer_it->addr.address(), iphash);
if (peer_it->seed) seeds.set(iphash);
sha1_hash const iphash = hash_address(p.addr.address());
if (p.seed) seeds.set(iphash);
else downloaders.set(iphash);
}

View File

@ -6509,10 +6509,9 @@ namespace aux {
, this, _1, 0));
m_natpmp->start();
for (std::list<listen_socket_t>::iterator i = m_listen_sockets.begin()
, end(m_listen_sockets.end()); i != end; ++i)
for (auto& s : m_listen_sockets)
{
remap_ports(remap_natpmp, *i);
remap_ports(remap_natpmp, s);
}
return m_natpmp.get();
}
@ -6535,10 +6534,9 @@ namespace aux {
m_upnp->discover_device();
for (std::list<listen_socket_t>::iterator i = m_listen_sockets.begin()
, end(m_listen_sockets.end()); i != end; ++i)
for (auto& s : m_listen_sockets)
{
remap_ports(remap_upnp, *i);
remap_ports(remap_upnp, s);
}
return m_upnp.get();
}
@ -6661,7 +6659,7 @@ namespace aux {
}
void session_impl::log_packet(message_direction_t dir, char const* pkt, int len
, udp::endpoint node)
, udp::endpoint const& node)
{
if (!m_alerts.should_post<dht_pkt_alert>()) return;
@ -6691,7 +6689,7 @@ namespace aux {
}
void session_impl::set_external_address(address const& ip
, int source_type, address const& source)
, int const source_type, address const& source)
{
#ifndef TORRENT_DISABLE_LOGGING
if (should_log())

View File

@ -145,21 +145,20 @@ namespace libtorrent
return ret;
}
void hash_address(address const& ip, sha1_hash& h)
sha1_hash hash_address(address const& ip)
{
#if TORRENT_USE_IPV6
if (ip.is_v6())
{
address_v6::bytes_type b = ip.to_v6().to_bytes();
h = hasher(reinterpret_cast<char const*>(b.data()), int(b.size())).final();
return hasher(reinterpret_cast<char const*>(b.data()), int(b.size())).final();
}
else
#endif
{
address_v4::bytes_type b = ip.to_v4().to_bytes();
h = hasher(reinterpret_cast<char const*>(b.data()), int(b.size())).final();
return hasher(reinterpret_cast<char const*>(b.data()), int(b.size())).final();
}
}
}

View File

@ -541,14 +541,13 @@ void upnp::on_reply(udp::endpoint const& from, char* buffer
d.non_router = non_router;
TORRENT_ASSERT(d.mapping.empty());
for (std::vector<global_mapping_t>::iterator j = m_mappings.begin()
, end(m_mappings.end()); j != end; ++j)
for (auto const& j : m_mappings)
{
mapping_t m;
m.action = mapping_t::action_add;
m.local_port = j->local_port;
m.external_port = j->external_port;
m.protocol = j->protocol;
m.local_port = j.local_port;
m.external_port = j.external_port;
m.protocol = j.protocol;
d.mapping.push_back(m);
}
std::tie(i, std::ignore) = m_devices.insert(d);

View File

@ -498,7 +498,7 @@ struct obs : dht::dht_observer
m_log.push_back(buf);
}
void log_packet(message_direction_t dir, char const* pkt, int len
, udp::endpoint node) override {}
, udp::endpoint const& node) override {}
#endif
bool on_dht_request(string_view query
, dht::msg const& request, entry& response) override { return false; }
@ -1093,8 +1093,7 @@ TORRENT_TEST(bloom_filter)
char adr[50];
std::snprintf(adr, 50, "192.0.2.%d", i);
address a = addr(adr);
sha1_hash iphash;
hash_address(a, iphash);
sha1_hash const iphash = hash_address(a);
test.set(iphash);
}
@ -1105,8 +1104,7 @@ TORRENT_TEST(bloom_filter)
char adr[50];
std::snprintf(adr, 50, "2001:db8::%x", i);
address a = addr(adr);
sha1_hash iphash;
hash_address(a, iphash);
sha1_hash const iphash = hash_address(a);
test.set(iphash);
}
}

View File

@ -57,7 +57,7 @@ struct log_t : libtorrent::dht::dht_logger
}
void log_packet(message_direction_t dir, char const* pkt, int len
, udp::endpoint node) override
, udp::endpoint const& node) override
{
libtorrent::bdecode_node print;
libtorrent::error_code ec;