DHTRoutingTableDeserializer.cc 6.4 KB

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