2012-08-26 17:26:17 +02:00
|
|
|
/*
|
|
|
|
|
2018-04-09 09:04:33 +02:00
|
|
|
Copyright (c) 2012-2018, Arvid Norberg
|
2012-08-26 17:26:17 +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/config.hpp"
|
|
|
|
#include "libtorrent/string_util.hpp"
|
2012-08-27 07:39:34 +02:00
|
|
|
#include "libtorrent/random.hpp"
|
2015-04-21 02:23:00 +02:00
|
|
|
#include "libtorrent/error_code.hpp"
|
|
|
|
#include "libtorrent/parse_url.hpp"
|
2016-02-08 08:01:25 +01:00
|
|
|
#include "libtorrent/address.hpp"
|
2016-08-26 18:36:09 +02:00
|
|
|
#include "libtorrent/assert.hpp"
|
2015-04-21 02:23:00 +02:00
|
|
|
|
2015-03-15 05:25:54 +01:00
|
|
|
#include <cstdlib> // for malloc
|
2017-09-17 11:52:04 +02:00
|
|
|
#include <cstring> // for strlen
|
2017-08-22 22:20:18 +02:00
|
|
|
#include <algorithm> // for search
|
2012-08-26 17:26:17 +02:00
|
|
|
|
2017-04-12 19:00:57 +02:00
|
|
|
namespace libtorrent {
|
2012-08-26 17:26:17 +02:00
|
|
|
|
2017-12-27 10:54:11 +01:00
|
|
|
// We need well defined results that don't depend on locale
|
2016-06-18 20:01:38 +02:00
|
|
|
std::array<char, 4 + std::numeric_limits<std::int64_t>::digits10>
|
2017-01-17 03:51:49 +01:00
|
|
|
to_string(std::int64_t const n)
|
2015-03-15 00:10:20 +01:00
|
|
|
{
|
2016-06-18 20:01:38 +02:00
|
|
|
std::array<char, 4 + std::numeric_limits<std::int64_t>::digits10> ret;
|
2015-03-15 00:10:20 +01:00
|
|
|
char *p = &ret.back();
|
|
|
|
*p = '\0';
|
2017-09-17 11:52:04 +02:00
|
|
|
// we want "un" to be the absolute value
|
|
|
|
// since the absolute of INT64_MIN cannot be represented by a signed
|
|
|
|
// int64, we calculate the abs in unsigned space
|
|
|
|
std::uint64_t un = n < 0
|
|
|
|
? std::numeric_limits<std::uint64_t>::max() - std::uint64_t(n) + 1
|
|
|
|
: std::uint64_t(n);
|
2015-03-15 00:10:20 +01:00
|
|
|
do {
|
|
|
|
*--p = '0' + un % 10;
|
|
|
|
un /= 10;
|
|
|
|
} while (un);
|
|
|
|
if (n < 0) *--p = '-';
|
2017-01-17 03:51:49 +01:00
|
|
|
std::memmove(ret.data(), p, std::size_t(&ret.back() - p + 1));
|
2015-03-15 00:10:20 +01:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2012-08-26 17:26:17 +02:00
|
|
|
bool is_alpha(char c)
|
|
|
|
{
|
|
|
|
return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z');
|
|
|
|
}
|
|
|
|
|
|
|
|
bool is_print(char c)
|
|
|
|
{
|
|
|
|
return c >= 32 && c < 127;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool is_space(char c)
|
|
|
|
{
|
2017-07-12 18:19:46 +02:00
|
|
|
return c == ' ' || c == '\t' || c == '\n' || c == '\r' || c == '\f' || c == '\v';
|
2012-08-26 17:26:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
char to_lower(char c)
|
|
|
|
{
|
|
|
|
return (c >= 'A' && c <= 'Z') ? c - 'A' + 'a' : c;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool string_begins_no_case(char const* s1, char const* s2)
|
|
|
|
{
|
2016-08-26 18:36:09 +02:00
|
|
|
TORRENT_ASSERT(s1 != nullptr);
|
|
|
|
TORRENT_ASSERT(s2 != nullptr);
|
|
|
|
|
2012-08-26 17:26:17 +02:00
|
|
|
while (*s1 != 0)
|
|
|
|
{
|
|
|
|
if (to_lower(*s1) != to_lower(*s2)) return false;
|
|
|
|
++s1;
|
|
|
|
++s2;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-02-19 13:34:55 +01:00
|
|
|
bool string_equal_no_case(string_view s1, string_view s2)
|
2012-08-26 17:26:17 +02:00
|
|
|
{
|
2017-02-19 13:34:55 +01:00
|
|
|
if (s1.size() != s2.size()) return false;
|
|
|
|
return std::equal(s1.begin(), s1.end(), s2.begin()
|
|
|
|
, [] (char const c1, char const c2)
|
|
|
|
{ return to_lower(c1) == to_lower(c2); });
|
2012-08-26 17:26:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// generate a url-safe random string
|
2018-01-14 20:27:23 +01:00
|
|
|
void url_random(span<char> dest)
|
2012-08-26 17:26:17 +02:00
|
|
|
{
|
|
|
|
// http-accepted characters:
|
|
|
|
// excluding ', since some buggy trackers don't support that
|
|
|
|
static char const printable[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
|
|
|
|
"abcdefghijklmnopqrstuvwxyz-_.!~*()";
|
|
|
|
|
|
|
|
// the random number
|
2018-11-26 15:16:39 +01:00
|
|
|
std::generate(dest.begin(), dest.end()
|
|
|
|
, []{ return printable[random(sizeof(printable) - 2)]; });
|
2012-08-26 17:26:17 +02:00
|
|
|
}
|
|
|
|
|
2016-08-26 18:36:09 +02:00
|
|
|
bool string_ends_with(string_view s1, string_view s2)
|
|
|
|
{
|
|
|
|
return s1.size() >= s2.size() && std::equal(s2.rbegin(), s2.rend(), s1.rbegin());
|
|
|
|
}
|
|
|
|
|
2017-08-22 22:20:18 +02:00
|
|
|
int search(span<char const> src, span<char const> target)
|
|
|
|
{
|
|
|
|
TORRENT_ASSERT(!src.empty());
|
|
|
|
TORRENT_ASSERT(!target.empty());
|
|
|
|
TORRENT_ASSERT(target.size() >= src.size());
|
2018-11-01 23:05:30 +01:00
|
|
|
TORRENT_ASSERT(target.size() < std::numeric_limits<int>::max());
|
2017-08-22 22:20:18 +02:00
|
|
|
|
2017-11-29 01:22:50 +01:00
|
|
|
auto const it = std::search(target.begin(), target.end(), src.begin(), src.end());
|
2017-08-22 22:20:18 +02:00
|
|
|
|
|
|
|
// no complete sync
|
|
|
|
if (it == target.end()) return -1;
|
|
|
|
return static_cast<int>(it - target.begin());
|
|
|
|
}
|
|
|
|
|
2019-03-21 13:15:23 +01:00
|
|
|
char* allocate_string_copy(string_view str)
|
2012-08-26 17:26:17 +02:00
|
|
|
{
|
2019-03-21 13:15:23 +01:00
|
|
|
if (str.empty()) return nullptr;
|
|
|
|
auto* tmp = new char[str.size() + 1];
|
|
|
|
std::copy(str.data(), str.data() + str.size(), tmp);
|
|
|
|
tmp[str.size()] = '\0';
|
2012-08-26 17:26:17 +02:00
|
|
|
return tmp;
|
|
|
|
}
|
|
|
|
|
2019-12-07 16:12:47 +01:00
|
|
|
#if TORRENT_ABI_VERSION == 1 \
|
|
|
|
|| !defined TORRENT_DISABLE_LOGGING
|
2016-02-08 08:01:25 +01:00
|
|
|
std::string print_listen_interfaces(std::vector<listen_interface_t> const& in)
|
|
|
|
{
|
|
|
|
std::string ret;
|
2017-03-12 16:34:42 +01:00
|
|
|
for (auto const& i : in)
|
2016-02-08 08:01:25 +01:00
|
|
|
{
|
2017-06-01 10:17:01 +02:00
|
|
|
if (!ret.empty()) ret += ',';
|
2016-02-08 08:01:25 +01:00
|
|
|
|
|
|
|
error_code ec;
|
2018-01-04 10:48:22 +01:00
|
|
|
make_address_v6(i.device, ec);
|
2016-02-08 08:01:25 +01:00
|
|
|
if (!ec)
|
|
|
|
{
|
|
|
|
// IPv6 addresses must be wrapped in square brackets
|
2017-06-01 10:17:01 +02:00
|
|
|
ret += '[';
|
2017-03-12 16:34:42 +01:00
|
|
|
ret += i.device;
|
2017-06-01 10:17:01 +02:00
|
|
|
ret += ']';
|
2016-02-08 08:01:25 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-03-12 16:34:42 +01:00
|
|
|
ret += i.device;
|
2016-02-08 08:01:25 +01:00
|
|
|
}
|
2017-06-01 10:17:01 +02:00
|
|
|
ret += ':';
|
2017-03-12 16:34:42 +01:00
|
|
|
ret += to_string(i.port).data();
|
2017-06-01 10:17:01 +02:00
|
|
|
if (i.ssl) ret += 's';
|
2016-02-08 08:01:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
2019-12-07 16:12:47 +01:00
|
|
|
#endif
|
|
|
|
|
|
|
|
string_view strip_string(string_view in)
|
|
|
|
{
|
|
|
|
while (!in.empty() && is_space(in.front()))
|
|
|
|
in.remove_prefix(1);
|
|
|
|
|
|
|
|
while (!in.empty() && is_space(in.back()))
|
|
|
|
in.remove_suffix(1);
|
|
|
|
return in;
|
|
|
|
}
|
2016-02-08 08:01:25 +01:00
|
|
|
|
2016-05-26 19:34:13 +02:00
|
|
|
// this parses the string that's used as the listen_interfaces setting.
|
2014-07-06 21:18:00 +02:00
|
|
|
// it is a comma-separated list of IP or device names with ports. For
|
|
|
|
// example: "eth0:6881,eth1:6881" or "127.0.0.1:6881"
|
2019-12-07 16:12:47 +01:00
|
|
|
std::vector<listen_interface_t> parse_listen_interfaces(std::string const& in
|
|
|
|
, std::vector<std::string>& err)
|
2014-07-06 21:18:00 +02:00
|
|
|
{
|
2016-09-20 17:24:24 +02:00
|
|
|
std::vector<listen_interface_t> out;
|
2014-07-06 21:18:00 +02:00
|
|
|
|
2019-12-07 16:12:47 +01:00
|
|
|
string_view rest = in;
|
|
|
|
while (!rest.empty())
|
2014-07-06 21:18:00 +02:00
|
|
|
{
|
2019-12-07 16:12:47 +01:00
|
|
|
string_view element;
|
|
|
|
std::tie(element, rest) = split_string(rest, ',');
|
2014-07-06 21:18:00 +02:00
|
|
|
|
2019-12-07 16:12:47 +01:00
|
|
|
element = strip_string(element);
|
|
|
|
if (element.size() > 1 && element.front() == '"' && element.back() == '"')
|
|
|
|
element = element.substr(1, element.size() - 2);
|
|
|
|
if (element.empty()) continue;
|
2014-07-06 21:18:00 +02:00
|
|
|
|
2016-06-12 08:00:10 +02:00
|
|
|
listen_interface_t iface;
|
|
|
|
iface.ssl = false;
|
2014-07-06 21:18:00 +02:00
|
|
|
|
2019-12-07 16:12:47 +01:00
|
|
|
string_view port;
|
|
|
|
if (element.front() == '[')
|
2014-07-06 21:18:00 +02:00
|
|
|
{
|
2019-12-07 16:12:47 +01:00
|
|
|
auto const pos = find_first_of(element, ']', 0);
|
|
|
|
if (pos == string_view::npos
|
|
|
|
|| pos+1 >= element.size()
|
|
|
|
|| element[pos+1] != ':')
|
|
|
|
{
|
|
|
|
err.emplace_back(element);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
iface.device = strip_string(element.substr(1, pos - 1)).to_string();
|
|
|
|
|
|
|
|
port = strip_string(element.substr(pos + 2));
|
2016-06-12 08:00:10 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// consume device name
|
2019-12-07 16:12:47 +01:00
|
|
|
auto const pos = find_first_of(element, ':', 0);
|
|
|
|
iface.device = strip_string(element.substr(0, pos)).to_string();
|
|
|
|
if (pos == string_view::npos)
|
|
|
|
{
|
|
|
|
err.emplace_back(element);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
port = strip_string(element.substr(pos + 1));
|
2016-06-12 08:00:10 +02:00
|
|
|
}
|
2014-07-06 21:18:00 +02:00
|
|
|
|
2016-06-12 08:00:10 +02:00
|
|
|
// consume port
|
2019-12-07 16:12:47 +01:00
|
|
|
std::string port_str;
|
|
|
|
for (std::size_t i = 0; i < port.size() && is_digit(port[i]); ++i)
|
|
|
|
port_str += port[i];
|
2016-06-12 08:00:10 +02:00
|
|
|
|
2019-12-07 16:12:47 +01:00
|
|
|
if (port_str.empty() || port_str.size() > 5)
|
2016-09-05 21:17:12 +02:00
|
|
|
{
|
2019-12-07 16:12:47 +01:00
|
|
|
err.emplace_back(element);
|
|
|
|
continue;
|
2016-09-05 21:17:12 +02:00
|
|
|
}
|
2019-12-07 16:12:47 +01:00
|
|
|
|
|
|
|
iface.port = std::atoi(port_str.c_str());
|
|
|
|
if (iface.port < 0 || iface.port > 65535)
|
2016-09-05 21:17:12 +02:00
|
|
|
{
|
2019-12-07 16:12:47 +01:00
|
|
|
err.emplace_back(element);
|
|
|
|
continue;
|
2016-09-05 21:17:12 +02:00
|
|
|
}
|
2016-06-12 08:00:10 +02:00
|
|
|
|
2019-12-07 16:12:47 +01:00
|
|
|
port.remove_prefix(port_str.size());
|
|
|
|
port = strip_string(port);
|
2016-06-12 08:00:10 +02:00
|
|
|
|
|
|
|
// consume potential SSL 's'
|
2019-12-07 16:12:47 +01:00
|
|
|
for (auto const c : port)
|
2016-06-12 08:00:10 +02:00
|
|
|
{
|
2019-12-07 16:12:47 +01:00
|
|
|
switch (c)
|
|
|
|
{
|
|
|
|
case 's': iface.ssl = true; break;
|
|
|
|
}
|
2016-06-12 08:00:10 +02:00
|
|
|
}
|
|
|
|
|
2019-12-07 16:12:47 +01:00
|
|
|
TORRENT_ASSERT(iface.port >= 0);
|
|
|
|
out.emplace_back(iface);
|
2014-07-06 21:18:00 +02:00
|
|
|
}
|
2016-09-20 17:24:24 +02:00
|
|
|
|
|
|
|
return out;
|
2014-07-06 21:18:00 +02:00
|
|
|
}
|
|
|
|
|
2016-08-21 18:15:19 +02:00
|
|
|
// this parses the string that's used as the listen_interfaces setting.
|
2014-07-06 21:18:00 +02:00
|
|
|
// it is a comma-separated list of IP or device names with ports. For
|
|
|
|
// example: "eth0:6881,eth1:6881" or "127.0.0.1:6881"
|
|
|
|
void parse_comma_separated_string_port(std::string const& in
|
2016-09-02 22:42:55 +02:00
|
|
|
, std::vector<std::pair<std::string, int>>& out)
|
2014-07-06 21:18:00 +02:00
|
|
|
{
|
|
|
|
out.clear();
|
|
|
|
|
|
|
|
std::string::size_type start = 0;
|
|
|
|
std::string::size_type end = 0;
|
|
|
|
|
|
|
|
while (start < in.size())
|
|
|
|
{
|
|
|
|
// skip leading spaces
|
|
|
|
while (start < in.size()
|
|
|
|
&& is_space(in[start]))
|
|
|
|
++start;
|
|
|
|
|
|
|
|
end = in.find_first_of(',', start);
|
|
|
|
if (end == std::string::npos) end = in.size();
|
|
|
|
|
|
|
|
std::string::size_type colon = in.find_last_of(':', end);
|
|
|
|
|
|
|
|
if (colon != std::string::npos && colon > start)
|
|
|
|
{
|
2017-11-29 01:22:50 +01:00
|
|
|
int port = std::atoi(in.substr(colon + 1, end - colon - 1).c_str());
|
2014-07-06 21:18:00 +02:00
|
|
|
|
|
|
|
// skip trailing spaces
|
|
|
|
std::string::size_type soft_end = colon;
|
|
|
|
while (soft_end > start
|
|
|
|
&& is_space(in[soft_end-1]))
|
|
|
|
--soft_end;
|
|
|
|
|
|
|
|
// in case this is an IPv6 address, strip off the square brackets
|
|
|
|
// to make it more easily parseable into an ip::address
|
|
|
|
if (in[start] == '[') ++start;
|
|
|
|
if (soft_end > start && in[soft_end-1] == ']') --soft_end;
|
|
|
|
|
2017-11-29 01:22:50 +01:00
|
|
|
out.emplace_back(in.substr(start, soft_end - start), port);
|
2014-07-06 21:18:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
start = end + 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void parse_comma_separated_string(std::string const& in, std::vector<std::string>& out)
|
|
|
|
{
|
|
|
|
out.clear();
|
|
|
|
|
|
|
|
std::string::size_type start = 0;
|
|
|
|
std::string::size_type end = 0;
|
|
|
|
|
|
|
|
while (start < in.size())
|
|
|
|
{
|
|
|
|
// skip leading spaces
|
|
|
|
while (start < in.size()
|
|
|
|
&& is_space(in[start]))
|
|
|
|
++start;
|
|
|
|
|
|
|
|
end = in.find_first_of(',', start);
|
|
|
|
if (end == std::string::npos) end = in.size();
|
|
|
|
|
|
|
|
// skip trailing spaces
|
|
|
|
std::string::size_type soft_end = end;
|
|
|
|
while (soft_end > start
|
2017-11-29 01:22:50 +01:00
|
|
|
&& is_space(in[soft_end - 1]))
|
2014-07-06 21:18:00 +02:00
|
|
|
--soft_end;
|
|
|
|
|
|
|
|
out.push_back(in.substr(start, soft_end - start));
|
|
|
|
start = end + 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-02 00:17:21 +02:00
|
|
|
std::pair<string_view, string_view> split_string(string_view last, char const sep)
|
2014-05-05 00:40:30 +02:00
|
|
|
{
|
2017-09-02 00:17:21 +02:00
|
|
|
if (last.empty()) return {{}, {}};
|
|
|
|
|
|
|
|
std::size_t pos = 0;
|
|
|
|
if (last[0] == '"' && sep != '"')
|
2014-05-05 00:40:30 +02:00
|
|
|
{
|
2017-09-02 00:17:21 +02:00
|
|
|
for (auto const c : last.substr(1))
|
|
|
|
{
|
|
|
|
++pos;
|
|
|
|
if (c == '"') break;
|
|
|
|
}
|
2014-05-05 00:40:30 +02:00
|
|
|
}
|
2017-09-02 00:17:21 +02:00
|
|
|
std::size_t found_sep = 0;
|
|
|
|
for (char const c : last.substr(pos))
|
2014-05-05 00:40:30 +02:00
|
|
|
{
|
2017-09-02 00:17:21 +02:00
|
|
|
if (c == sep)
|
|
|
|
{
|
|
|
|
found_sep = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
++pos;
|
2014-05-05 00:40:30 +02:00
|
|
|
}
|
2017-09-02 00:17:21 +02:00
|
|
|
return {last.substr(0, pos), last.substr(pos + found_sep)};
|
2014-05-05 00:40:30 +02:00
|
|
|
}
|
|
|
|
|
2015-04-21 02:23:00 +02:00
|
|
|
#if TORRENT_USE_I2P
|
|
|
|
|
|
|
|
bool is_i2p_url(std::string const& url)
|
|
|
|
{
|
2016-06-20 17:32:06 +02:00
|
|
|
using std::ignore;
|
2015-04-21 02:23:00 +02:00
|
|
|
std::string hostname;
|
|
|
|
error_code ec;
|
2016-06-20 17:32:06 +02:00
|
|
|
std::tie(ignore, ignore, hostname, ignore, ignore)
|
2015-04-21 02:23:00 +02:00
|
|
|
= parse_url_components(url, ec);
|
2016-08-26 18:36:09 +02:00
|
|
|
return string_ends_with(hostname, ".i2p");
|
2015-04-21 02:23:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
2012-08-26 17:26:17 +02:00
|
|
|
}
|