delete_files bug fix
[libtorrent.git] / src / kademlia / node_id.cpp
blob758af2f8da73cf49bc56556e82b49388bd7a22ce
1 /*
3 Copyright (c) 2006, Arvid Norberg
4 All rights reserved.
6 Redistribution and use in source and binary forms, with or without
7 modification, are permitted provided that the following conditions
8 are met:
10 * Redistributions of source code must retain the above copyright
11 notice, this list of conditions and the following disclaimer.
12 * Redistributions in binary form must reproduce the above copyright
13 notice, this list of conditions and the following disclaimer in
14 the documentation and/or other materials provided with the distribution.
15 * Neither the name of the author nor the names of its
16 contributors may be used to endorse or promote products derived
17 from this software without specific prior written permission.
19 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
23 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 POSSIBILITY OF SUCH DAMAGE.
33 #include "libtorrent/pch.hpp"
35 #include <algorithm>
36 #include <iomanip>
37 #include <ctime>
38 #include <boost/bind.hpp>
40 #include "libtorrent/kademlia/node_id.hpp"
41 #include "libtorrent/hasher.hpp"
42 #include "libtorrent/assert.hpp"
44 using boost::bind;
46 namespace libtorrent { namespace dht
49 // returns the distance between the two nodes
50 // using the kademlia XOR-metric
51 node_id distance(node_id const& n1, node_id const& n2)
53 node_id ret;
54 node_id::iterator k = ret.begin();
55 for (node_id::const_iterator i = n1.begin(), j = n2.begin()
56 , end(n1.end()); i != end; ++i, ++j, ++k)
58 *k = *i ^ *j;
60 return ret;
63 // returns true if: distance(n1, ref) < distance(n2, ref)
64 bool compare_ref(node_id const& n1, node_id const& n2, node_id const& ref)
66 for (node_id::const_iterator i = n1.begin(), j = n2.begin()
67 , k = ref.begin(), end(n1.end()); i != end; ++i, ++j, ++k)
69 boost::uint8_t lhs = (*i ^ *k);
70 boost::uint8_t rhs = (*j ^ *k);
71 if (lhs < rhs) return true;
72 if (lhs > rhs) return false;
74 return false;
77 // returns n in: 2^n <= distance(n1, n2) < 2^(n+1)
78 // useful for finding out which bucket a node belongs to
79 int distance_exp(node_id const& n1, node_id const& n2)
81 int byte = node_id::size - 1;
82 for (node_id::const_iterator i = n1.begin(), j = n2.begin()
83 , end(n1.end()); i != end; ++i, ++j, --byte)
85 TORRENT_ASSERT(byte >= 0);
86 boost::uint8_t t = *i ^ *j;
87 if (t == 0) continue;
88 // we have found the first non-zero byte
89 // return the bit-number of the first bit
90 // that differs
91 int bit = byte * 8;
92 for (int b = 7; b >= 0; --b)
93 if (t >= (1 << b)) return bit + b;
94 return bit;
97 return 0;
100 struct static_ { static_() { std::srand(std::time(0)); } } static__;
102 node_id generate_id()
104 char random[20];
105 #ifdef _MSC_VER
106 std::generate(random, random + 20, &rand);
107 #else
108 std::generate(random, random + 20, &std::rand);
109 #endif
111 hasher h;
112 h.update(random, 20);
113 return h.final();
116 } } // namespace libtorrent::dht