DHTRoutingTableDeserializer.cc 4.6 KB

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