DHTRoutingTableDeserializer.cc 6.5 KB

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