1 /* 2 3 Copyright (c) 2006-2018, Arvid Norberg 4 All rights reserved. 5 6 Redistribution and use in source and binary forms, with or without 7 modification, are permitted provided that the following conditions 8 are met: 9 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. 18 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. 30 31 */ 32 33 #ifndef KADEMLIA_NODE_ENTRY_HPP 34 #define KADEMLIA_NODE_ENTRY_HPP 35 36 #include "libtorrent/kademlia/node_id.hpp" 37 #include "libtorrent/socket.hpp" 38 #include "libtorrent/address.hpp" 39 #include "libtorrent/union_endpoint.hpp" 40 #include "libtorrent/time.hpp" // for time_point 41 #include "libtorrent/aux_/time.hpp" // for time_now 42 43 namespace libtorrent { namespace dht { 44 45 struct TORRENT_EXTRA_EXPORT node_entry 46 { 47 node_entry(node_id const& id_, udp::endpoint const& ep, int roundtriptime = 0xffff 48 , bool pinged = false); 49 explicit node_entry(udp::endpoint const& ep); 50 node_entry() = default; 51 void update_rtt(int new_rtt); 52 pingedlibtorrent::dht::node_entry53 bool pinged() const { return timeout_count != 0xff; } set_pingedlibtorrent::dht::node_entry54 void set_pinged() { if (timeout_count == 0xff) timeout_count = 0; } timed_outlibtorrent::dht::node_entry55 void timed_out() { if (pinged() && timeout_count < 0xfe) ++timeout_count; } fail_countlibtorrent::dht::node_entry56 int fail_count() const { return pinged() ? timeout_count : 0; } reset_fail_countlibtorrent::dht::node_entry57 void reset_fail_count() { if (pinged()) timeout_count = 0; } eplibtorrent::dht::node_entry58 udp::endpoint ep() const { return endpoint; } confirmedlibtorrent::dht::node_entry59 bool confirmed() const { return timeout_count == 0; } addrlibtorrent::dht::node_entry60 address addr() const { return endpoint.address(); } portlibtorrent::dht::node_entry61 int port() const { return endpoint.port; } 62 63 // compares which node_entry is "better". Smaller is better operator <libtorrent::dht::node_entry64 bool operator<(node_entry const& rhs) const 65 { 66 return std::make_tuple(!verified, rtt) < std::make_tuple(!rhs.verified, rhs.rtt); 67 } 68 69 #ifndef TORRENT_DISABLE_LOGGING 70 time_point first_seen = aux::time_now(); 71 #endif 72 73 // the time we last received a response for a request to this peer 74 time_point last_queried = min_time(); 75 76 node_id id{nullptr}; 77 78 union_endpoint endpoint; 79 80 // the average RTT of this node 81 std::uint16_t rtt = 0xffff; 82 83 // the number of times this node has failed to 84 // respond in a row 85 // 0xff is a special value to indicate we have not pinged this node yet 86 std::uint8_t timeout_count = 0xff; 87 88 bool verified = false; 89 }; 90 91 } } // namespace libtorrent::dht 92 93 #endif 94