DHTBucket.cc 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. /* <!-- copyright */
  2. /*
  3. * aria2 - The high speed download utility
  4. *
  5. * Copyright (C) 2006 Tatsuhiro Tsujikawa
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. *
  21. * In addition, as a special exception, the copyright holders give
  22. * permission to link the code of portions of this program with the
  23. * OpenSSL library under certain conditions as described in each
  24. * individual source file, and distribute linked combinations
  25. * including the two.
  26. * You must obey the GNU General Public License in all respects
  27. * for all of the code used other than OpenSSL. If you modify
  28. * file(s) with this exception, you may extend this exception to your
  29. * version of the file(s), but you are not obligated to do so. If you
  30. * do not wish to do so, delete this exception statement from your
  31. * version. If you delete this exception statement from all source
  32. * files in the program, then also delete it here.
  33. */
  34. /* copyright --> */
  35. #include "DHTBucket.h"
  36. #include <cstring>
  37. #include <cassert>
  38. #include <algorithm>
  39. #include "DHTNode.h"
  40. #include "LogFactory.h"
  41. #include "Logger.h"
  42. #include "util.h"
  43. #include "DHTConstants.h"
  44. #include "a2functional.h"
  45. #include "bittorrent_helper.h"
  46. #include "bitfield.h"
  47. #include "wallclock.h"
  48. #include "fmt.h"
  49. namespace aria2 {
  50. DHTBucket::DHTBucket(size_t prefixLength, const unsigned char* max,
  51. const unsigned char* min,
  52. const std::shared_ptr<DHTNode>& localNode)
  53. : prefixLength_(prefixLength),
  54. localNode_(localNode),
  55. lastUpdated_(global::wallclock())
  56. {
  57. memcpy(max_, max, DHT_ID_LENGTH);
  58. memcpy(min_, min, DHT_ID_LENGTH);
  59. }
  60. DHTBucket::DHTBucket(const std::shared_ptr<DHTNode>& localNode)
  61. : prefixLength_(0), localNode_(localNode), lastUpdated_(global::wallclock())
  62. {
  63. memset(max_, 0xffu, DHT_ID_LENGTH);
  64. memset(min_, 0, DHT_ID_LENGTH);
  65. }
  66. DHTBucket::~DHTBucket() = default;
  67. void DHTBucket::getRandomNodeID(unsigned char* nodeID) const
  68. {
  69. if (prefixLength_ == 0) {
  70. util::generateRandomKey(nodeID);
  71. }
  72. else {
  73. size_t lastByteIndex = (prefixLength_ - 1) / 8;
  74. util::generateRandomKey(nodeID);
  75. memcpy(nodeID, min_, lastByteIndex + 1);
  76. }
  77. }
  78. bool DHTBucket::isInRange(const std::shared_ptr<DHTNode>& node) const
  79. {
  80. return isInRange(node->getID(), max_, min_);
  81. }
  82. bool DHTBucket::isInRange(const unsigned char* nodeID) const
  83. {
  84. return isInRange(nodeID, max_, min_);
  85. }
  86. // Returns true if nodeID is in [min, max] (inclusive).
  87. bool DHTBucket::isInRange(const unsigned char* nodeID, const unsigned char* max,
  88. const unsigned char* min) const
  89. {
  90. return !std::lexicographical_compare(&nodeID[0], &nodeID[DHT_ID_LENGTH],
  91. &min[0], &min[DHT_ID_LENGTH]) &&
  92. !std::lexicographical_compare(&max[0], &max[DHT_ID_LENGTH], &nodeID[0],
  93. &nodeID[DHT_ID_LENGTH]);
  94. }
  95. bool DHTBucket::addNode(const std::shared_ptr<DHTNode>& node)
  96. {
  97. notifyUpdate();
  98. auto itr = std::find_if(nodes_.begin(), nodes_.end(), derefEqual(node));
  99. if (itr == nodes_.end()) {
  100. if (nodes_.size() < K) {
  101. nodes_.push_back(node);
  102. return true;
  103. }
  104. else {
  105. if (nodes_.front()->isBad()) {
  106. nodes_.erase(nodes_.begin());
  107. nodes_.push_back(node);
  108. return true;
  109. }
  110. else {
  111. return false;
  112. }
  113. }
  114. }
  115. else {
  116. nodes_.erase(itr);
  117. nodes_.push_back(node);
  118. return true;
  119. }
  120. }
  121. void DHTBucket::cacheNode(const std::shared_ptr<DHTNode>& node)
  122. {
  123. // cachedNodes_ are sorted by last time seen
  124. cachedNodes_.push_front(node);
  125. if (cachedNodes_.size() > CACHE_SIZE) {
  126. cachedNodes_.resize(CACHE_SIZE, std::shared_ptr<DHTNode>());
  127. }
  128. }
  129. void DHTBucket::dropNode(const std::shared_ptr<DHTNode>& node)
  130. {
  131. if (!cachedNodes_.empty()) {
  132. auto itr = std::find_if(nodes_.begin(), nodes_.end(), derefEqual(node));
  133. if (itr != nodes_.end()) {
  134. nodes_.erase(itr);
  135. nodes_.push_back(cachedNodes_.front());
  136. cachedNodes_.erase(cachedNodes_.begin());
  137. }
  138. }
  139. }
  140. void DHTBucket::moveToHead(const std::shared_ptr<DHTNode>& node)
  141. {
  142. auto itr = std::find_if(nodes_.begin(), nodes_.end(), derefEqual(node));
  143. if (itr != nodes_.end()) {
  144. nodes_.erase(itr);
  145. nodes_.push_front(node);
  146. }
  147. }
  148. void DHTBucket::moveToTail(const std::shared_ptr<DHTNode>& node)
  149. {
  150. auto itr = std::find_if(nodes_.begin(), nodes_.end(), derefEqual(node));
  151. if (itr != nodes_.end()) {
  152. nodes_.erase(itr);
  153. nodes_.push_back(node);
  154. }
  155. }
  156. bool DHTBucket::splitAllowed() const
  157. {
  158. return prefixLength_ < DHT_ID_LENGTH * 8 - 1 && isInRange(localNode_);
  159. }
  160. std::unique_ptr<DHTBucket> DHTBucket::split()
  161. {
  162. assert(splitAllowed());
  163. unsigned char rMax[DHT_ID_LENGTH];
  164. memcpy(rMax, max_, DHT_ID_LENGTH);
  165. bitfield::flipBit(rMax, DHT_ID_LENGTH, prefixLength_);
  166. unsigned char rMin[DHT_ID_LENGTH];
  167. memcpy(rMin, min_, DHT_ID_LENGTH);
  168. bitfield::flipBit(min_, DHT_ID_LENGTH, prefixLength_);
  169. ++prefixLength_;
  170. auto rBucket = make_unique<DHTBucket>(prefixLength_, rMax, rMin, localNode_);
  171. std::deque<std::shared_ptr<DHTNode>> lNodes;
  172. for (auto& elem : nodes_) {
  173. if (rBucket->isInRange(elem)) {
  174. assert(rBucket->addNode(elem));
  175. }
  176. else {
  177. lNodes.push_back(elem);
  178. }
  179. }
  180. nodes_ = lNodes;
  181. // TODO create toString() and use it.
  182. A2_LOG_DEBUG(fmt("New bucket. prefixLength=%u, Range:%s-%s",
  183. static_cast<unsigned int>(rBucket->getPrefixLength()),
  184. util::toHex(rBucket->getMinID(), DHT_ID_LENGTH).c_str(),
  185. util::toHex(rBucket->getMaxID(), DHT_ID_LENGTH).c_str()));
  186. A2_LOG_DEBUG(fmt("Existing bucket. prefixLength=%u, Range:%s-%s",
  187. static_cast<unsigned int>(prefixLength_),
  188. util::toHex(getMinID(), DHT_ID_LENGTH).c_str(),
  189. util::toHex(getMaxID(), DHT_ID_LENGTH).c_str()));
  190. return rBucket;
  191. }
  192. void DHTBucket::getGoodNodes(
  193. std::vector<std::shared_ptr<DHTNode>>& goodNodes) const
  194. {
  195. goodNodes.insert(goodNodes.end(), nodes_.begin(), nodes_.end());
  196. goodNodes.erase(std::remove_if(goodNodes.begin(), goodNodes.end(),
  197. std::mem_fn(&DHTNode::isBad)),
  198. goodNodes.end());
  199. }
  200. std::shared_ptr<DHTNode> DHTBucket::getNode(const unsigned char* nodeID,
  201. const std::string& ipaddr,
  202. uint16_t port) const
  203. {
  204. auto node = std::make_shared<DHTNode>(nodeID);
  205. node->setIPAddress(ipaddr);
  206. node->setPort(port);
  207. auto itr = std::find_if(nodes_.begin(), nodes_.end(), derefEqual(node));
  208. if (itr == nodes_.end() || (*itr)->getIPAddress() != ipaddr ||
  209. (*itr)->getPort() != port) {
  210. return nullptr;
  211. }
  212. else {
  213. return *itr;
  214. }
  215. }
  216. bool DHTBucket::operator==(const DHTBucket& bucket) const
  217. {
  218. return memcmp(max_, bucket.max_, DHT_ID_LENGTH) == 0 &&
  219. memcmp(min_, bucket.min_, DHT_ID_LENGTH) == 0;
  220. }
  221. bool DHTBucket::needsRefresh() const
  222. {
  223. return nodes_.size() < K || lastUpdated_.difference(global::wallclock()) >=
  224. DHT_BUCKET_REFRESH_INTERVAL;
  225. }
  226. void DHTBucket::notifyUpdate() { lastUpdated_ = global::wallclock(); }
  227. namespace {
  228. class FindQuestionableNode {
  229. public:
  230. bool operator()(const std::shared_ptr<DHTNode>& node) const
  231. {
  232. return node->isQuestionable();
  233. }
  234. };
  235. } // namespace
  236. bool DHTBucket::containsQuestionableNode() const
  237. {
  238. return std::find_if(nodes_.begin(), nodes_.end(), FindQuestionableNode()) !=
  239. nodes_.end();
  240. }
  241. std::shared_ptr<DHTNode> DHTBucket::getLRUQuestionableNode() const
  242. {
  243. auto i = std::find_if(nodes_.begin(), nodes_.end(), FindQuestionableNode());
  244. if (i == nodes_.end()) {
  245. return nullptr;
  246. }
  247. else {
  248. return *i;
  249. }
  250. }
  251. } // namespace aria2