2012-12-31 07:54:54 +01:00
|
|
|
/*
|
|
|
|
|
|
|
|
Copyright (c) 2012, Arvid Norberg
|
|
|
|
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.
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
2014-10-26 08:34:31 +01:00
|
|
|
#include "libtorrent/peer_list.hpp"
|
2012-12-31 07:54:54 +01:00
|
|
|
#include "libtorrent/hasher.hpp"
|
2013-01-06 23:41:56 +01:00
|
|
|
#include "libtorrent/broadcast_socket.hpp" // for supports_ipv6()
|
2016-11-12 19:50:49 +01:00
|
|
|
#include "setup_transfer.hpp"
|
2018-02-02 08:26:21 +01:00
|
|
|
#include "libtorrent/aux_/disable_warnings_push.hpp"
|
2014-01-23 03:28:04 +01:00
|
|
|
#include <boost/crc.hpp>
|
2018-02-02 08:26:21 +01:00
|
|
|
#include "libtorrent/aux_/disable_warnings_pop.hpp"
|
2012-12-31 07:54:54 +01:00
|
|
|
|
|
|
|
#include "test.hpp"
|
|
|
|
|
2017-04-12 20:05:53 +02:00
|
|
|
using namespace lt;
|
2012-12-31 07:54:54 +01:00
|
|
|
|
2018-01-28 00:56:21 +01:00
|
|
|
namespace {
|
2016-06-18 20:01:38 +02:00
|
|
|
std::uint32_t hash_buffer(char const* buf, int len)
|
2012-12-31 07:54:54 +01:00
|
|
|
{
|
2014-01-23 03:28:04 +01:00
|
|
|
boost::crc_optimal<32, 0x1EDC6F41, 0xFFFFFFFF, 0xFFFFFFFF, true, true> crc;
|
|
|
|
crc.process_block(buf, buf + len);
|
|
|
|
return crc.checksum();
|
2012-12-31 07:54:54 +01:00
|
|
|
}
|
2018-01-28 00:56:21 +01:00
|
|
|
} // anonymous namespace
|
2012-12-31 07:54:54 +01:00
|
|
|
|
2015-05-30 06:31:23 +02:00
|
|
|
TORRENT_TEST(peer_priority)
|
2012-12-31 07:54:54 +01:00
|
|
|
{
|
|
|
|
// when the IP is the same, we hash the ports, sorted
|
2016-06-18 20:01:38 +02:00
|
|
|
std::uint32_t p = peer_priority(
|
2016-11-12 19:50:49 +01:00
|
|
|
ep("230.12.123.3", 0x4d2), ep("230.12.123.3", 0x12c));
|
2012-12-31 07:54:54 +01:00
|
|
|
TEST_EQUAL(p, hash_buffer("\x01\x2c\x04\xd2", 4));
|
|
|
|
|
|
|
|
// when we're in the same /24, we just hash the IPs
|
2016-11-12 19:50:49 +01:00
|
|
|
p = peer_priority(ep("230.12.123.1", 0x4d2), ep("230.12.123.3", 0x12c));
|
2012-12-31 07:54:54 +01:00
|
|
|
TEST_EQUAL(p, hash_buffer("\xe6\x0c\x7b\x01\xe6\x0c\x7b\x03", 8));
|
|
|
|
|
|
|
|
// when we're in the same /16, we just hash the IPs masked by
|
|
|
|
// 0xffffff55
|
2016-11-12 19:50:49 +01:00
|
|
|
p = peer_priority(ep("230.12.23.1", 0x4d2), ep("230.12.123.3", 0x12c));
|
2012-12-31 07:54:54 +01:00
|
|
|
TEST_EQUAL(p, hash_buffer("\xe6\x0c\x17\x01\xe6\x0c\x7b\x01", 8));
|
|
|
|
|
|
|
|
// when we're in different /16, we just hash the IPs masked by
|
|
|
|
// 0xffff5555
|
2016-11-12 19:50:49 +01:00
|
|
|
p = peer_priority(ep("230.120.23.1", 0x4d2), ep("230.12.123.3", 0x12c));
|
2012-12-31 07:54:54 +01:00
|
|
|
TEST_EQUAL(p, hash_buffer("\xe6\x0c\x51\x01\xe6\x78\x15\x01", 8));
|
|
|
|
|
2014-03-26 17:09:31 +01:00
|
|
|
// test vectors from BEP 40
|
2016-11-12 19:50:49 +01:00
|
|
|
TEST_EQUAL(peer_priority(ep("123.213.32.10", 0), ep("98.76.54.32", 0))
|
2014-03-26 17:09:31 +01:00
|
|
|
, 0xec2d7224);
|
|
|
|
|
|
|
|
TEST_EQUAL(peer_priority(
|
2016-11-12 19:50:49 +01:00
|
|
|
ep("123.213.32.10", 0), ep("123.213.32.234", 0))
|
2014-03-26 17:09:31 +01:00
|
|
|
, 0x99568189);
|
|
|
|
|
2013-01-06 23:41:56 +01:00
|
|
|
if (supports_ipv6())
|
|
|
|
{
|
|
|
|
// IPv6 has a twice as wide mask, and we only care about the top 64 bits
|
|
|
|
// when the IPs are the same, just hash the ports
|
|
|
|
p = peer_priority(
|
2016-11-12 19:50:49 +01:00
|
|
|
ep("ffff:ffff:ffff:ffff::1", 0x4d2), ep("ffff:ffff:ffff:ffff::1", 0x12c));
|
2013-01-06 23:41:56 +01:00
|
|
|
TEST_EQUAL(p, hash_buffer("\x01\x2c\x04\xd2", 4));
|
2015-08-29 16:21:15 +02:00
|
|
|
|
2013-01-06 23:41:56 +01:00
|
|
|
// these IPs don't belong to the same /32, so apply the full mask
|
|
|
|
// 0xffffffff55555555
|
|
|
|
p = peer_priority(
|
2016-11-12 19:50:49 +01:00
|
|
|
ep("ffff:ffff:ffff:ffff::1", 0x4d2), ep("ffff:0fff:ffff:ffff::1", 0x12c));
|
2013-01-06 23:41:56 +01:00
|
|
|
TEST_EQUAL(p, hash_buffer(
|
|
|
|
"\xff\xff\x0f\xff\x55\x55\x55\x55\x00\x00\x00\x00\x00\x00\x00\x01"
|
|
|
|
"\xff\xff\xff\xff\x55\x55\x55\x55\x00\x00\x00\x00\x00\x00\x00\x01", 32));
|
|
|
|
}
|
2012-12-31 07:54:54 +01:00
|
|
|
}
|