DHTRoutingTableDeserializer.cc 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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 "DHTNode.h"
  37. #include "DHTConstants.h"
  38. #include "PeerMessageUtil.h"
  39. #include "DlAbortEx.h"
  40. #include "Logger.h"
  41. #include "a2netcompat.h"
  42. #include "StringFormat.h"
  43. #include "Util.h"
  44. #include <cerrno>
  45. #include <cstring>
  46. #include <istream>
  47. #include <utility>
  48. namespace aria2 {
  49. DHTRoutingTableDeserializer::DHTRoutingTableDeserializer() {}
  50. DHTRoutingTableDeserializer::~DHTRoutingTableDeserializer() {}
  51. SharedHandle<DHTNode> DHTRoutingTableDeserializer::getLocalNode() const
  52. {
  53. return _localNode;
  54. }
  55. const std::deque<SharedHandle<DHTNode> >& DHTRoutingTableDeserializer::getNodes() const
  56. {
  57. return _nodes;
  58. }
  59. void DHTRoutingTableDeserializer::deserialize(std::istream& in)
  60. {
  61. try {
  62. char header[8];
  63. memset(header, 0, sizeof(header));
  64. // magic
  65. header[0] = 0xa1;
  66. header[1] = 0xa2;
  67. // format ID
  68. header[2] = 0x02;
  69. // version
  70. header[6] = 0;
  71. header[7] = 0x03;
  72. char headerCompat[8];
  73. memset(headerCompat, 0, sizeof(headerCompat));
  74. // magic
  75. headerCompat[0] = 0xa1;
  76. headerCompat[1] = 0xa2;
  77. // format ID
  78. headerCompat[2] = 0x02;
  79. // version
  80. headerCompat[6] = 0;
  81. headerCompat[7] = 0x02;
  82. char zero[8];
  83. memset(zero, 0, sizeof(zero));
  84. int version;
  85. char buf[26];
  86. // header
  87. in.read(buf, 8);
  88. if(memcmp(header, buf, 8) == 0) {
  89. version = 3;
  90. } else if(memcmp(headerCompat, buf, 8) == 0) {
  91. version = 2;
  92. } else {
  93. throw DlAbortEx
  94. (StringFormat("Failed to load DHT routing table. cause:%s",
  95. "bad header").str());
  96. }
  97. // time
  98. if(version == 2) {
  99. in.read(buf, 4);
  100. _serializedTime.setTimeInSec(ntohl(*reinterpret_cast<uint32_t*>(buf)));
  101. // 4bytes reserved
  102. in.read(buf, 4);
  103. } else {
  104. in.read(buf, 8);
  105. _serializedTime.setTimeInSec(ntoh64(*reinterpret_cast<uint64_t*>(buf)));
  106. }
  107. // localnode
  108. // 8bytes reserved
  109. in.read(buf, 8);
  110. // localnode ID
  111. in.read(buf, DHT_ID_LENGTH);
  112. SharedHandle<DHTNode> localNode(new DHTNode(reinterpret_cast<const unsigned char*>(buf)));
  113. // 4bytes reserved
  114. in.read(buf, 4);
  115. // number of nodes
  116. in.read(buf, 4);
  117. uint32_t numNodes = ntohl(*reinterpret_cast<uint32_t*>(buf));
  118. // 4bytes reserved
  119. in.read(buf, 4);
  120. // nodes
  121. for(size_t i = 0; i < numNodes; ++i) {
  122. // Currently, only IPv4 addresses are supported.
  123. // 1byte compact peer info length
  124. uint8_t peerInfoLen;
  125. in >> peerInfoLen;
  126. if(peerInfoLen != 6) {
  127. // skip this entry
  128. in.read(buf, 42+7+6);
  129. continue;
  130. }
  131. // 7bytes reserved
  132. in.read(buf, 7);
  133. // 6bytes compact peer info
  134. in.read(buf, 6);
  135. if(memcmp(zero, buf, 6) == 0) {
  136. // skip this entry
  137. in.read(buf, 42);
  138. continue;
  139. }
  140. std::pair<std::string, uint16_t> peer =
  141. PeerMessageUtil::unpackcompact(reinterpret_cast<const unsigned char*>(buf));
  142. if(peer.first.empty()) {
  143. // skip this entry
  144. in.read(buf, 42);
  145. continue;
  146. }
  147. // 2bytes reserved
  148. in.read(buf, 2);
  149. // 16byte reserved
  150. in.read(buf, 16);
  151. // localnode ID
  152. in.read(buf, DHT_ID_LENGTH);
  153. SharedHandle<DHTNode> node(new DHTNode(reinterpret_cast<const unsigned char*>(buf)));
  154. node->setIPAddress(peer.first);
  155. node->setPort(peer.second);
  156. // 4bytes reserved
  157. in.read(buf, 4);
  158. _nodes.push_back(node);
  159. }
  160. _localNode = localNode;
  161. } catch(std::ios::failure const& exception) {
  162. _nodes.clear();
  163. throw DlAbortEx
  164. (StringFormat("Failed to load DHT routing table. cause:%s",
  165. strerror(errno)).str());
  166. }
  167. }
  168. } // namespace aria2