From 1280c7f7ae8ebd6399aa8918d3e00ac38f770b3c Mon Sep 17 00:00:00 2001 From: Alden Torres Date: Sat, 18 Mar 2017 11:07:17 -0400 Subject: [PATCH] is_ip_address refactor and minor code cleanup (#1828) is_ip_address refactor and minor code cleanup --- include/libtorrent/broadcast_socket.hpp | 3 ++- include/libtorrent/torrent.hpp | 2 +- src/broadcast_socket.cpp | 2 +- src/resolver.cpp | 12 +++++------- src/session_impl.cpp | 17 +++++++---------- src/torrent.cpp | 4 ++-- src/version.cpp | 1 - test/test_enum_net.cpp | 10 ++++++++++ 8 files changed, 28 insertions(+), 23 deletions(-) diff --git a/include/libtorrent/broadcast_socket.hpp b/include/libtorrent/broadcast_socket.hpp index 82f82d058..754717fcd 100644 --- a/include/libtorrent/broadcast_socket.hpp +++ b/include/libtorrent/broadcast_socket.hpp @@ -38,6 +38,7 @@ POSSIBILITY OF SUCH DAMAGE. #include "libtorrent/socket.hpp" #include "libtorrent/address.hpp" #include "libtorrent/error_code.hpp" +#include "libtorrent/string_view.hpp" #include #include @@ -49,7 +50,7 @@ namespace libtorrent TORRENT_EXTRA_EXPORT bool is_loopback(address const& addr); TORRENT_EXTRA_EXPORT bool is_any(address const& addr); TORRENT_EXTRA_EXPORT bool is_teredo(address const& addr); - bool is_ip_address(char const* host); + TORRENT_EXTRA_EXPORT bool is_ip_address(std::string const& host); // determines if the operating system supports IPv6 TORRENT_EXTRA_EXPORT bool supports_ipv6(); diff --git a/include/libtorrent/torrent.hpp b/include/libtorrent/torrent.hpp index c5062f36c..522bccf28 100644 --- a/include/libtorrent/torrent.hpp +++ b/include/libtorrent/torrent.hpp @@ -666,7 +666,7 @@ namespace libtorrent // these are callbacks called by the tracker_connection instance // (either http_tracker_connection or udp_tracker_connection) // when this torrent got a response from its tracker request - // or when a failure occured + // or when a failure occurred virtual void tracker_response( tracker_request const& r , address const& tracker_ip diff --git a/src/broadcast_socket.cpp b/src/broadcast_socket.cpp index 99caf53e1..deedbab3b 100644 --- a/src/broadcast_socket.cpp +++ b/src/broadcast_socket.cpp @@ -56,7 +56,7 @@ using namespace std::placeholders; namespace libtorrent { - bool is_ip_address(char const* host) + bool is_ip_address(std::string const& host) { error_code ec; address::from_string(host, ec); diff --git a/src/resolver.cpp b/src/resolver.cpp index 7e0d617be..7057edb1d 100644 --- a/src/resolver.cpp +++ b/src/resolver.cpp @@ -36,8 +36,6 @@ POSSIBILITY OF SUCH DAMAGE. #include -using namespace std::placeholders; - namespace libtorrent { resolver::resolver(io_service& ios) @@ -88,10 +86,10 @@ namespace libtorrent } } - void resolver::async_resolve(std::string const& host, int flags + void resolver::async_resolve(std::string const& host, int const flags , resolver_interface::callback_t const& h) { - cache_t::iterator i = m_cache.find(host); + auto const i = m_cache.find(host); if (i != m_cache.end()) { // keep cache entries valid for m_timeout seconds @@ -107,7 +105,7 @@ namespace libtorrent // special handling for raw IP addresses. There's no need to get in line // behind actual lookups if we can just resolve it immediately. error_code ec; - address ip = address::from_string(host.c_str(), ec); + address const ip = address::from_string(host, ec); if (!ec) { std::vector
addresses; @@ -117,8 +115,9 @@ namespace libtorrent } // the port is ignored - tcp::resolver::query q(host, "80"); + tcp::resolver::query const q(host, "80"); + using namespace std::placeholders; ADD_OUTSTANDING_ASYNC("resolver::on_lookup"); if (flags & resolver_interface::abort_on_shutdown) { @@ -137,4 +136,3 @@ namespace libtorrent m_resolver.cancel(); } } - diff --git a/src/session_impl.cpp b/src/session_impl.cpp index 3db074f56..cd808e923 100644 --- a/src/session_impl.cpp +++ b/src/session_impl.cpp @@ -633,9 +633,7 @@ namespace aux { update_dht(); update_peer_fingerprint(); update_dht_bootstrap_nodes(); -#ifndef TORRENT_DISABLE_DHT update_dht_announce_interval(); -#endif } void session_impl::async_resolve(std::string const& host, int flags @@ -3525,23 +3523,22 @@ namespace aux { TORRENT_ASSERT(m_dht); // announce to DHT every 15 minutes - int delay = (std::max)(m_settings.get_int(settings_pack::dht_announce_interval) - / (std::max)(int(m_torrents.size()), 1), 1); + int delay = std::max(m_settings.get_int(settings_pack::dht_announce_interval) + / std::max(int(m_torrents.size()), 1), 1); if (!m_dht_torrents.empty()) { // we have prioritized torrents that need // an initial DHT announce. Don't wait too long // until we announce those. - delay = (std::min)(4, delay); + delay = std::min(4, delay); } ADD_OUTSTANDING_ASYNC("session_impl::on_dht_announce"); error_code ec; m_dht_announce_timer.expires_from_now(seconds(delay), ec); - m_dht_announce_timer.async_wait([this](error_code const& err) { - this->wrap(&session_impl::on_dht_announce, err); } - ); + m_dht_announce_timer.async_wait([this](error_code const& err) + { this->wrap(&session_impl::on_dht_announce, err); }); if (!m_dht_torrents.empty()) { @@ -6189,8 +6186,8 @@ namespace aux { ADD_OUTSTANDING_ASYNC("session_impl::on_dht_announce"); error_code ec; - int delay = (std::max)(m_settings.get_int(settings_pack::dht_announce_interval) - / (std::max)(int(m_torrents.size()), 1), 1); + int delay = std::max(m_settings.get_int(settings_pack::dht_announce_interval) + / std::max(int(m_torrents.size()), 1), 1); m_dht_announce_timer.expires_from_now(seconds(delay), ec); m_dht_announce_timer.async_wait([this](error_code const& e) { this->wrap(&session_impl::on_dht_announce, e); }); diff --git a/src/torrent.cpp b/src/torrent.cpp index a8ba96374..15a81ef1e 100644 --- a/src/torrent.cpp +++ b/src/torrent.cpp @@ -5790,8 +5790,8 @@ namespace libtorrent return; } - bool const is_ip = is_ip_address(hostname.c_str()); - if (is_ip) a.address(address::from_string(hostname.c_str(), ec)); + bool const is_ip = is_ip_address(hostname); + if (is_ip) a.address(address::from_string(hostname, ec)); bool const proxy_hostnames = settings().get_bool(settings_pack::proxy_hostnames) && !is_ip; diff --git a/src/version.cpp b/src/version.cpp index dc4722b39..6b0c1fe40 100644 --- a/src/version.cpp +++ b/src/version.cpp @@ -40,4 +40,3 @@ char const* version() } } - diff --git a/test/test_enum_net.cpp b/test/test_enum_net.cpp index 25102098c..06a78b259 100644 --- a/test/test_enum_net.cpp +++ b/test/test_enum_net.cpp @@ -101,3 +101,13 @@ TORRENT_TEST(match_addr_mask) TEST_CHECK(!ec); } +TORRENT_TEST(is_ip_address) +{ + TEST_EQUAL(is_ip_address("1.2.3.4"), true); + TEST_EQUAL(is_ip_address("a.b.c.d"), false); + TEST_EQUAL(is_ip_address("a:b:b:c"), false); +#if TORRENT_USE_IPV6 + TEST_EQUAL(is_ip_address("::1"), true); + TEST_EQUAL(is_ip_address("2001:db8:85a3:0:0:8a2e:370:7334"), true); +#endif +}