DHTRoutingTableDeserializer.cc 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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 <arpa/inet.h>
  42. #include <cerrno>
  43. #include <cstring>
  44. #include <istream>
  45. #include <utility>
  46. namespace aria2 {
  47. DHTRoutingTableDeserializer::DHTRoutingTableDeserializer():
  48. _localNode(0) {}
  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] = 0x01;
  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 new DlAbortEx("Failed to load DHT routing table. cause:%s",
  78. "bad header");
  79. }
  80. // time
  81. in.read(buf, 4);
  82. _serializedTime.setTimeInSec(ntohl(*reinterpret_cast<uint32_t*>(buf)));
  83. // 4bytes reserved
  84. in.read(buf, 4);
  85. // localnode
  86. // 8bytes reserved
  87. in.read(buf, 8);
  88. // localnode ID
  89. in.read(buf, DHT_ID_LENGTH);
  90. SharedHandle<DHTNode> localNode = new DHTNode(reinterpret_cast<const unsigned char*>(buf));
  91. // 4bytes reserved
  92. in.read(buf, 4);
  93. // number of nodes
  94. in.read(buf, 4);
  95. uint32_t numNodes = ntohl(*reinterpret_cast<uint32_t*>(buf));
  96. // 4bytes reserved
  97. in.read(buf, 4);
  98. // nodes
  99. for(size_t i = 0; i < numNodes; ++i) {
  100. // 6bytes compact peer info
  101. in.read(buf, 6);
  102. if(memcmp(zero, buf, 6) == 0) {
  103. // skip this entry
  104. in.read(buf, 26);
  105. continue;
  106. }
  107. std::pair<std::string, uint16_t> peer = PeerMessageUtil::unpackcompact(buf);
  108. if(peer.first.empty()) {
  109. // skip this entry
  110. in.read(buf, 26);
  111. continue;
  112. }
  113. // 2bytes reserved
  114. in.read(buf, 2);
  115. // localnode ID
  116. in.read(buf, DHT_ID_LENGTH);
  117. SharedHandle<DHTNode> node = new DHTNode(reinterpret_cast<const unsigned char*>(buf));
  118. node->setIPAddress(peer.first);
  119. node->setPort(peer.second);
  120. // 4bytes reserved
  121. in.read(buf, 4);
  122. _nodes.push_back(node);
  123. }
  124. _localNode = localNode;
  125. } catch(std::ios::failure const& exception) {
  126. _nodes.clear();
  127. throw new DlAbortEx("Failed to load DHT routing table. cause:%s",
  128. strerror(errno));
  129. }
  130. }
  131. } // namespace aria2