diff --git a/src/kademlia/node_id.cpp b/src/kademlia/node_id.cpp index 0603c6da1..f2ee6746a 100644 --- a/src/kademlia/node_id.cpp +++ b/src/kademlia/node_id.cpp @@ -134,6 +134,9 @@ node_id generate_id_impl(address const& ip_, boost::uint32_t r) ip[0] |= (r & 0x7) << 5; // this is the crc32c (Castagnoli) polynomial + // TODO: 2 this could be optimized if SSE 4.2 is + // available. It could also be optimized given + // that we have a fixed length boost::crc_optimal<32, 0x1EDC6F41, 0xFFFFFFFF, 0xFFFFFFFF, true, true> crc; crc.process_block(ip, ip + num_octets); boost::uint32_t c = crc.checksum(); diff --git a/src/policy.cpp b/src/policy.cpp index f54b6084d..79113da4e 100644 --- a/src/policy.cpp +++ b/src/policy.cpp @@ -38,6 +38,7 @@ POSSIBILITY OF SUCH DAMAGE. #include #include +#include #ifdef _MSC_VER #pragma warning(pop) @@ -150,7 +151,12 @@ namespace libtorrent using std::swap; - hasher h; + // this is the crc32c (Castagnoli) polynomial + // TODO: 2 this could be optimized if SSE 4.2 is + // available. It could also be optimized given + // that we have a fixed length + boost::crc_optimal<32, 0x1EDC6F41, 0xFFFFFFFF, 0xFFFFFFFF, true, true> crc; + if (e1.address() == e2.address()) { if (e1.port() > e2.port()) @@ -158,7 +164,7 @@ namespace libtorrent boost::uint16_t p[2]; p[0] = htons(e1.port()); p[1] = htons(e2.port()); - h.update((char const*)&p[0], 4); + crc.process_block((char const*)&p[0], (char const*)&p[2]); } #if TORRENT_USE_IPV6 else if (e1.address().is_v6()) @@ -176,8 +182,9 @@ namespace libtorrent : memcmp(&b1[0], &b2[0], 6) ? 1 : 2; apply_mask(&b1[0], v6mask[mask], 8); apply_mask(&b2[0], v6mask[mask], 8); - h.update((char const*)&b1[0], b1.size()); - h.update((char const*)&b2[0], b2.size()); + + crc.process_block((char const*)&b1[0], (char const*)&b1[16]); + crc.process_block((char const*)&b2[0], (char const*)&b2[16]); } #endif else @@ -195,17 +202,14 @@ namespace libtorrent : memcmp(&b1[0], &b2[0], 3) ? 1 : 2; apply_mask(&b1[0], v4mask[mask], 4); apply_mask(&b2[0], v4mask[mask], 4); - h.update((char const*)&b1[0], b1.size()); - h.update((char const*)&b2[0], b2.size()); + + crc.process_block((char const*)&b1[0], (char const*)&b1[4]); + crc.process_block((char const*)&b2[0], (char const*)&b2[4]); } - boost::uint32_t ret; - sha1_hash digest = h.final(); - memcpy(&ret, &digest[0], 4); - return ntohl(ret); + return crc.checksum(); } - // returns the rank of a peer's source. We have an affinity // to connecting to peers with higher rank. This is to avoid // problems when our peer list is diluted by stale peers from @@ -1899,7 +1903,7 @@ namespace libtorrent // TOOD: pass in both an IPv6 and IPv4 address here boost::uint32_t policy::peer::rank(external_ip const& external, int external_port) const { -//TODO: how do we deal with our external address changing? Pass in a force-update maybe? and keep a version number in policy +//TODO 3: how do we deal with our external address changing? Pass in a force-update maybe? and keep a version number in policy if (peer_rank == 0) peer_rank = peer_priority( tcp::endpoint(external.external_address(this->address()), external_port) diff --git a/test/test_peer_priority.cpp b/test/test_peer_priority.cpp index 0fd9e1b32..b12d45783 100644 --- a/test/test_peer_priority.cpp +++ b/test/test_peer_priority.cpp @@ -33,6 +33,7 @@ POSSIBILITY OF SUCH DAMAGE. #include "libtorrent/policy.hpp" #include "libtorrent/hasher.hpp" #include "libtorrent/broadcast_socket.hpp" // for supports_ipv6() +#include #include "test.hpp" @@ -40,12 +41,9 @@ using namespace libtorrent; boost::uint32_t hash_buffer(char const* buf, int len) { - hasher h; - h.update(buf, len); - sha1_hash digest = h.final(); - boost::uint32_t ret; - memcpy(&ret, &digest[0], 4); - return ntohl(ret); + boost::crc_optimal<32, 0x1EDC6F41, 0xFFFFFFFF, 0xFFFFFFFF, true, true> crc; + crc.process_block(buf, buf + len); + return crc.checksum(); } int test_main()