DHTRoutingTableDeserializer.cc 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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 "DHTRoutingTableDeserializer.h"
  36. #include <cstring>
  37. #include <cassert>
  38. #include <cstdio>
  39. #include <utility>
  40. #include "DHTNode.h"
  41. #include "DHTConstants.h"
  42. #include "bittorrent_helper.h"
  43. #include "DlAbortEx.h"
  44. #include "Logger.h"
  45. #include "a2netcompat.h"
  46. #include "fmt.h"
  47. #include "util.h"
  48. #include "array_fun.h"
  49. #include "LogFactory.h"
  50. #include "BufferedFile.h"
  51. namespace aria2 {
  52. DHTRoutingTableDeserializer::DHTRoutingTableDeserializer(int family)
  53. : family_(family)
  54. {
  55. }
  56. DHTRoutingTableDeserializer::~DHTRoutingTableDeserializer() = default;
  57. #define READ_CHECK(fp, ptr, count) \
  58. if (fp.read((ptr), (count)) != (count)) { \
  59. throw DL_ABORT_EX("Failed to load DHT routing table."); \
  60. }
  61. namespace {
  62. void readBytes(BufferedFile& fp, unsigned char* buf, size_t buflen,
  63. size_t readlen)
  64. {
  65. assert(readlen <= buflen);
  66. READ_CHECK(fp, buf, readlen);
  67. }
  68. } // namespace
  69. void DHTRoutingTableDeserializer::deserialize(const std::string& filename)
  70. {
  71. A2_LOG_INFO(fmt("Loading DHT routing table from %s.", filename.c_str()));
  72. BufferedFile fp(filename.c_str(), BufferedFile::READ);
  73. if (!fp) {
  74. throw DL_ABORT_EX(
  75. fmt("Failed to load DHT routing table from %s", filename.c_str()));
  76. }
  77. char header[8];
  78. memset(header, 0, sizeof(header));
  79. // magic
  80. header[0] = 0xa1u;
  81. header[1] = 0xa2u;
  82. // format ID
  83. header[2] = 0x02u;
  84. // version
  85. header[6] = 0;
  86. header[7] = 0x03u;
  87. char headerCompat[8];
  88. memset(headerCompat, 0, sizeof(headerCompat));
  89. // magic
  90. headerCompat[0] = 0xa1u;
  91. headerCompat[1] = 0xa2u;
  92. // format ID
  93. headerCompat[2] = 0x02u;
  94. // version
  95. headerCompat[6] = 0;
  96. headerCompat[7] = 0x02u;
  97. char zero[18];
  98. memset(zero, 0, sizeof(zero));
  99. int version;
  100. // If you change the code to read more than the size of buf, then
  101. // expand the buf size here.
  102. array_wrapper<unsigned char, 255> buf;
  103. // header
  104. readBytes(fp, buf, buf.size(), 8);
  105. if (memcmp(header, buf, 8) == 0) {
  106. version = 3;
  107. }
  108. else if (memcmp(headerCompat, buf, 8) == 0) {
  109. version = 2;
  110. }
  111. else {
  112. throw DL_ABORT_EX(fmt("Failed to load DHT routing table from %s. cause:%s",
  113. filename.c_str(), "bad header"));
  114. }
  115. uint32_t temp32;
  116. uint64_t temp64;
  117. // time
  118. if (version == 2) {
  119. READ_CHECK(fp, &temp32, sizeof(temp32));
  120. serializedTime_.setTimeFromEpoch(ntohl(temp32));
  121. // 4bytes reserved
  122. readBytes(fp, buf, buf.size(), 4);
  123. }
  124. else {
  125. READ_CHECK(fp, &temp64, sizeof(temp64));
  126. serializedTime_.setTimeFromEpoch(ntoh64(temp64));
  127. }
  128. // localnode
  129. // 8bytes reserved
  130. readBytes(fp, buf, buf.size(), 8);
  131. // localnode ID
  132. readBytes(fp, buf, buf.size(), DHT_ID_LENGTH);
  133. auto localNode = std::make_shared<DHTNode>(buf);
  134. // 4bytes reserved
  135. readBytes(fp, buf, buf.size(), 4);
  136. // number of nodes
  137. READ_CHECK(fp, &temp32, sizeof(temp32));
  138. uint32_t numNodes = ntohl(temp32);
  139. // 4bytes reserved
  140. readBytes(fp, buf, buf.size(), 4);
  141. std::vector<std::shared_ptr<DHTNode>> nodes;
  142. // nodes
  143. const int compactlen = bittorrent::getCompactLength(family_);
  144. for (size_t i = 0; i < numNodes; ++i) {
  145. // 1byte compact peer info length
  146. uint8_t peerInfoLen;
  147. READ_CHECK(fp, &peerInfoLen, sizeof(peerInfoLen));
  148. if (peerInfoLen != compactlen) {
  149. // skip this entry
  150. readBytes(fp, buf, buf.size(), 7 + 48);
  151. continue;
  152. }
  153. // 7bytes reserved
  154. readBytes(fp, buf, buf.size(), 7);
  155. // compactlen bytes compact peer info
  156. readBytes(fp, buf, buf.size(), compactlen);
  157. if (memcmp(zero, buf, compactlen) == 0) {
  158. // skip this entry
  159. readBytes(fp, buf, buf.size(), 48 - compactlen);
  160. continue;
  161. }
  162. std::pair<std::string, uint16_t> peer =
  163. bittorrent::unpackcompact(buf, family_);
  164. if (peer.first.empty()) {
  165. // skip this entry
  166. readBytes(fp, buf, buf.size(), 48 - compactlen);
  167. continue;
  168. }
  169. // 24-compactlen bytes reserved
  170. readBytes(fp, buf, buf.size(), 24 - compactlen);
  171. // node ID
  172. readBytes(fp, buf, buf.size(), DHT_ID_LENGTH);
  173. auto node = std::make_shared<DHTNode>(buf);
  174. node->setIPAddress(peer.first);
  175. node->setPort(peer.second);
  176. // 4bytes reserved
  177. readBytes(fp, buf, buf.size(), 4);
  178. nodes.push_back(node);
  179. }
  180. localNode_ = localNode;
  181. nodes_ = nodes;
  182. A2_LOG_INFO("DHT routing table was loaded successfully");
  183. }
  184. } // namespace aria2