From 0507764d0420b7e2de55fe80a3e9672e79ea13be Mon Sep 17 00:00:00 2001 From: Alden Torres Date: Thu, 15 Sep 2016 21:13:43 -0400 Subject: [PATCH] consts and refactor, hash_address returns the hash (#1093) consts and refactor --- include/libtorrent/aux_/session_impl.hpp | 2 +- include/libtorrent/enum_net.hpp | 1 + include/libtorrent/kademlia/dht_observer.hpp | 2 +- include/libtorrent/peer_id.hpp | 1 - include/libtorrent/socket_io.hpp | 2 +- simulation/test_dht_rate_limit.cpp | 2 +- src/ip_voter.cpp | 8 +++----- src/kademlia/dht_storage.cpp | 11 ++++------- src/session_impl.cpp | 14 ++++++-------- src/socket_io.cpp | 7 +++---- src/upnp.cpp | 9 ++++----- test/test_dht.cpp | 8 +++----- test/test_dos_blocker.cpp | 2 +- 13 files changed, 29 insertions(+), 40 deletions(-) diff --git a/include/libtorrent/aux_/session_impl.hpp b/include/libtorrent/aux_/session_impl.hpp index 2b15576e8..e934c26b0 100644 --- a/include/libtorrent/aux_/session_impl.hpp +++ b/include/libtorrent/aux_/session_impl.hpp @@ -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 diff --git a/include/libtorrent/enum_net.hpp b/include/libtorrent/enum_net.hpp index af59ff3c4..a222a741d 100644 --- a/include/libtorrent/enum_net.hpp +++ b/include/libtorrent/enum_net.hpp @@ -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); diff --git a/include/libtorrent/kademlia/dht_observer.hpp b/include/libtorrent/kademlia/dht_observer.hpp index 921ab4597..4e71bd35d 100644 --- a/include/libtorrent/kademlia/dht_observer.hpp +++ b/include/libtorrent/kademlia/dht_observer.hpp @@ -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: diff --git a/include/libtorrent/peer_id.hpp b/include/libtorrent/peer_id.hpp index 4b561d2fd..3027416d6 100644 --- a/include/libtorrent/peer_id.hpp +++ b/include/libtorrent/peer_id.hpp @@ -41,4 +41,3 @@ namespace libtorrent } #endif // TORRENT_PEER_ID_HPP_INCLUDED - diff --git a/include/libtorrent/socket_io.hpp b/include/libtorrent/socket_io.hpp index 484d03399..6441b06ae 100644 --- a/include/libtorrent/socket_io.hpp +++ b/include/libtorrent/socket_io.hpp @@ -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 { diff --git a/simulation/test_dht_rate_limit.cpp b/simulation/test_dht_rate_limit.cpp index 2c7e16d2d..e671d6602 100644 --- a/simulation/test_dht_rate_limit.cpp +++ b/simulation/test_dht_rate_limit.cpp @@ -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; } diff --git a/src/ip_voter.cpp b/src/ip_voter.cpp index d807d4e8b..29738daae 100644 --- a/src/ip_voter.cpp +++ b/src/ip_voter.cpp @@ -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::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; } } - diff --git a/src/kademlia/dht_storage.cpp b/src/kademlia/dht_storage.cpp index 361067cd5..f388ae8c3 100644 --- a/src/kademlia/dht_storage.cpp +++ b/src/kademlia/dht_storage.cpp @@ -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::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); } diff --git a/src/session_impl.cpp b/src/session_impl.cpp index 49c68a8f3..c4671c282 100644 --- a/src/session_impl.cpp +++ b/src/session_impl.cpp @@ -6509,10 +6509,9 @@ namespace aux { , this, _1, 0)); m_natpmp->start(); - for (std::list::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::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()) 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()) diff --git a/src/socket_io.cpp b/src/socket_io.cpp index b71cb7a62..62d7d72c7 100644 --- a/src/socket_io.cpp +++ b/src/socket_io.cpp @@ -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(b.data()), int(b.size())).final(); + return hasher(reinterpret_cast(b.data()), int(b.size())).final(); } else #endif { address_v4::bytes_type b = ip.to_v4().to_bytes(); - h = hasher(reinterpret_cast(b.data()), int(b.size())).final(); + return hasher(reinterpret_cast(b.data()), int(b.size())).final(); } } } - diff --git a/src/upnp.cpp b/src/upnp.cpp index 94c2b0c87..066ab1b47 100644 --- a/src/upnp.cpp +++ b/src/upnp.cpp @@ -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::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); diff --git a/test/test_dht.cpp b/test/test_dht.cpp index fdb449178..e9224fe99 100644 --- a/test/test_dht.cpp +++ b/test/test_dht.cpp @@ -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); } } diff --git a/test/test_dos_blocker.cpp b/test/test_dos_blocker.cpp index 9637a7df7..62d2a715a 100644 --- a/test/test_dos_blocker.cpp +++ b/test/test_dos_blocker.cpp @@ -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;