2014-07-06 21:18:00 +02:00
|
|
|
/*
|
|
|
|
|
2018-04-09 09:04:33 +02:00
|
|
|
Copyright (c) 2013-2018, Arvid Norberg
|
2014-07-06 21:18:00 +02:00
|
|
|
All rights reserved.
|
|
|
|
|
|
|
|
Redistribution and use in source and binary forms, with or without
|
|
|
|
modification, are permitted provided that the following conditions
|
|
|
|
are met:
|
|
|
|
|
|
|
|
* Redistributions of source code must retain the above copyright
|
|
|
|
notice, this list of conditions and the following disclaimer.
|
|
|
|
* Redistributions in binary form must reproduce the above copyright
|
|
|
|
notice, this list of conditions and the following disclaimer in
|
|
|
|
the documentation and/or other materials provided with the distribution.
|
|
|
|
* Neither the name of the author nor the names of its
|
|
|
|
contributors may be used to endorse or promote products derived
|
|
|
|
from this software without specific prior written permission.
|
|
|
|
|
|
|
|
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
|
|
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
|
|
|
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
|
|
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
|
|
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
|
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "libtorrent/resolver.hpp"
|
|
|
|
#include "libtorrent/debug.hpp"
|
2015-03-12 05:34:54 +01:00
|
|
|
#include "libtorrent/aux_/time.hpp"
|
2014-07-06 21:18:00 +02:00
|
|
|
|
2017-04-12 19:00:57 +02:00
|
|
|
namespace libtorrent {
|
|
|
|
|
2017-07-15 02:59:20 +02:00
|
|
|
|
|
|
|
constexpr resolver_flags resolver_interface::cache_only;
|
|
|
|
constexpr resolver_flags resolver_interface::abort_on_shutdown;
|
|
|
|
|
2014-07-06 21:18:00 +02:00
|
|
|
resolver::resolver(io_service& ios)
|
|
|
|
: m_ios(ios)
|
|
|
|
, m_resolver(ios)
|
2014-12-17 03:44:27 +01:00
|
|
|
, m_critical_resolver(ios)
|
2014-07-06 21:18:00 +02:00
|
|
|
, m_max_size(700)
|
2014-08-16 22:55:44 +02:00
|
|
|
, m_timeout(seconds(1200))
|
2014-07-06 21:18:00 +02:00
|
|
|
{}
|
|
|
|
|
2019-05-14 07:46:17 +02:00
|
|
|
|
|
|
|
void resolver::callback(resolver_interface::callback_t const& h
|
|
|
|
, error_code const& ec, std::vector<address> const& ips)
|
|
|
|
{
|
|
|
|
try {
|
|
|
|
h(ec, ips);
|
|
|
|
} catch (std::exception&) {
|
|
|
|
TORRENT_ASSERT_FAIL();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-06 21:18:00 +02:00
|
|
|
void resolver::on_lookup(error_code const& ec, tcp::resolver::iterator i
|
2019-05-14 07:46:17 +02:00
|
|
|
, resolver_interface::callback_t const& h, std::string const& hostname)
|
2014-07-06 21:18:00 +02:00
|
|
|
{
|
2016-04-23 23:29:25 +02:00
|
|
|
COMPLETE_ASYNC("resolver::on_lookup");
|
2014-07-06 21:18:00 +02:00
|
|
|
if (ec)
|
|
|
|
{
|
2019-05-14 07:46:17 +02:00
|
|
|
callback(h, ec, {});
|
2014-07-06 21:18:00 +02:00
|
|
|
return;
|
|
|
|
}
|
2015-06-06 07:22:53 +02:00
|
|
|
|
2014-07-06 21:18:00 +02:00
|
|
|
dns_cache_entry& ce = m_cache[hostname];
|
2018-06-07 02:35:00 +02:00
|
|
|
ce.last_seen = aux::time_now();
|
2014-07-06 21:18:00 +02:00
|
|
|
ce.addresses.clear();
|
|
|
|
while (i != tcp::resolver::iterator())
|
|
|
|
{
|
|
|
|
ce.addresses.push_back(i->endpoint().address());
|
|
|
|
++i;
|
|
|
|
}
|
|
|
|
|
2019-05-14 07:46:17 +02:00
|
|
|
callback(h, ec, ce.addresses);
|
2014-07-06 21:18:00 +02:00
|
|
|
|
|
|
|
// if m_cache grows too big, weed out the
|
|
|
|
// oldest entries
|
2016-11-21 07:49:56 +01:00
|
|
|
if (int(m_cache.size()) > m_max_size)
|
2014-07-06 21:18:00 +02:00
|
|
|
{
|
2017-03-27 20:40:19 +02:00
|
|
|
auto oldest = m_cache.begin();
|
|
|
|
for (auto k = m_cache.begin(); k != m_cache.end(); ++k)
|
2014-07-06 21:18:00 +02:00
|
|
|
{
|
2015-08-16 18:17:23 +02:00
|
|
|
if (k->second.last_seen < oldest->second.last_seen)
|
|
|
|
oldest = k;
|
2014-07-06 21:18:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// remove the oldest entry
|
|
|
|
m_cache.erase(oldest);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-28 21:01:46 +02:00
|
|
|
void resolver::async_resolve(std::string const& host, resolver_flags const flags
|
2014-07-06 21:18:00 +02:00
|
|
|
, resolver_interface::callback_t const& h)
|
|
|
|
{
|
2017-05-25 20:58:29 +02:00
|
|
|
// 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;
|
2018-01-04 10:48:22 +01:00
|
|
|
address const ip = make_address(host, ec);
|
2017-05-25 20:58:29 +02:00
|
|
|
if (!ec)
|
|
|
|
{
|
2019-05-14 07:46:17 +02:00
|
|
|
m_ios.post([=]{ callback(h, ec, std::vector<address>{ip}); });
|
2017-05-25 20:58:29 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
ec.clear();
|
|
|
|
|
2017-03-18 16:07:17 +01:00
|
|
|
auto const i = m_cache.find(host);
|
2014-07-06 21:18:00 +02:00
|
|
|
if (i != m_cache.end())
|
|
|
|
{
|
|
|
|
// keep cache entries valid for m_timeout seconds
|
2017-07-15 02:59:20 +02:00
|
|
|
if ((flags & resolver_interface::cache_only)
|
2015-03-12 05:34:54 +01:00
|
|
|
|| i->second.last_seen + m_timeout >= aux::time_now())
|
2014-07-06 21:18:00 +02:00
|
|
|
{
|
2019-05-14 07:46:17 +02:00
|
|
|
std::vector<address> ips = i->second.addresses;
|
|
|
|
m_ios.post([=] { callback(h, ec, ips); });
|
2014-07-06 21:18:00 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2015-06-06 07:22:53 +02:00
|
|
|
|
2017-07-15 02:59:20 +02:00
|
|
|
if (flags & resolver_interface::cache_only)
|
2015-06-17 05:45:05 +02:00
|
|
|
{
|
2017-05-25 20:58:29 +02:00
|
|
|
// we did not find a cache entry, fail the lookup
|
2019-05-14 07:46:17 +02:00
|
|
|
m_ios.post([=] {
|
|
|
|
callback(h, boost::asio::error::host_not_found, std::vector<address>{});
|
|
|
|
});
|
2015-06-17 05:45:05 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-07-06 21:18:00 +02:00
|
|
|
// the port is ignored
|
2017-03-18 16:07:17 +01:00
|
|
|
tcp::resolver::query const q(host, "80");
|
2014-07-06 21:18:00 +02:00
|
|
|
|
2017-03-18 16:07:17 +01:00
|
|
|
using namespace std::placeholders;
|
2016-04-23 23:29:25 +02:00
|
|
|
ADD_OUTSTANDING_ASYNC("resolver::on_lookup");
|
2017-07-15 02:59:20 +02:00
|
|
|
if (flags & resolver_interface::abort_on_shutdown)
|
2014-12-17 03:44:27 +01:00
|
|
|
{
|
2016-05-25 06:31:52 +02:00
|
|
|
m_resolver.async_resolve(q, std::bind(&resolver::on_lookup, this, _1, _2
|
2014-12-17 03:44:27 +01:00
|
|
|
, h, host));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-05-25 06:31:52 +02:00
|
|
|
m_critical_resolver.async_resolve(q, std::bind(&resolver::on_lookup, this, _1, _2
|
2014-12-17 03:44:27 +01:00
|
|
|
, h, host));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void resolver::abort()
|
|
|
|
{
|
|
|
|
m_resolver.cancel();
|
2014-07-06 21:18:00 +02:00
|
|
|
}
|
2017-04-05 00:23:37 +02:00
|
|
|
|
2018-06-07 02:35:00 +02:00
|
|
|
void resolver::set_cache_timeout(seconds const timeout)
|
2017-04-05 00:23:37 +02:00
|
|
|
{
|
|
|
|
if (timeout >= seconds(0))
|
|
|
|
m_timeout = timeout;
|
|
|
|
else
|
|
|
|
m_timeout = seconds(0);
|
|
|
|
}
|
2014-07-06 21:18:00 +02:00
|
|
|
}
|