is_ip_address refactor and minor code cleanup (#1828)

is_ip_address refactor and minor code cleanup
This commit is contained in:
Alden Torres 2017-03-18 11:07:17 -04:00 committed by Arvid Norberg
parent 1d1484d689
commit 1280c7f7ae
8 changed files with 28 additions and 23 deletions

View File

@ -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 <memory>
#include <list>
@ -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();

View File

@ -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

View File

@ -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);

View File

@ -36,8 +36,6 @@ POSSIBILITY OF SUCH DAMAGE.
#include <functional>
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<address> 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();
}
}

View File

@ -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); });

View File

@ -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;

View File

@ -40,4 +40,3 @@ char const* version()
}
}

View File

@ -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
}