DHTRoutingTableSerializer.cc 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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 "DHTRoutingTableSerializer.h"
  36. #include <cstring>
  37. #include <ostream>
  38. #include "DHTNode.h"
  39. #include "DlAbortEx.h"
  40. #include "DHTConstants.h"
  41. #include "bittorrent_helper.h"
  42. #include "Logger.h"
  43. #include "a2netcompat.h"
  44. #include "util.h"
  45. #include "TimeA2.h"
  46. namespace aria2 {
  47. DHTRoutingTableSerializer::DHTRoutingTableSerializer(int family):
  48. family_(family) {}
  49. DHTRoutingTableSerializer::~DHTRoutingTableSerializer() {}
  50. void DHTRoutingTableSerializer::setLocalNode
  51. (const SharedHandle<DHTNode>& localNode)
  52. {
  53. localNode_ = localNode;
  54. }
  55. void DHTRoutingTableSerializer::setNodes
  56. (const std::vector<SharedHandle<DHTNode> >& nodes)
  57. {
  58. nodes_ = nodes;
  59. }
  60. void DHTRoutingTableSerializer::serialize(std::ostream& o)
  61. {
  62. char header[8];
  63. memset(header, 0, sizeof(header));
  64. // magic
  65. header[0] = 0xa1u;
  66. header[1] = 0xa2u;
  67. // format ID
  68. header[2] = 0x02u;
  69. // version
  70. header[6] = 0;
  71. header[7] = 0x03u;
  72. char zero[18];
  73. memset(zero, 0, sizeof(zero));
  74. o.write(header, 8);
  75. // write save date
  76. uint64_t ntime = hton64(Time().getTime());
  77. o.write(reinterpret_cast<const char*>(&ntime), sizeof(ntime));
  78. // localnode
  79. // 8bytes reserved
  80. o.write(zero, 8);
  81. // 20bytes localnode ID
  82. o.write(reinterpret_cast<const char*>(localNode_->getID()), DHT_ID_LENGTH);
  83. // 4bytes reserved
  84. o.write(zero, 4);
  85. // number of nodes
  86. uint32_t numNodes = htonl(nodes_.size());
  87. o.write(reinterpret_cast<const char*>(&numNodes), sizeof(uint32_t));
  88. // 4bytes reserved
  89. o.write(zero, 4);
  90. const int clen = bittorrent::getCompactLength(family_);
  91. // nodes
  92. for(std::vector<SharedHandle<DHTNode> >::const_iterator i = nodes_.begin(),
  93. eoi = nodes_.end(); i != eoi; ++i) {
  94. const SharedHandle<DHTNode>& node = *i;
  95. // Write IP address + port in Compact IP-address/port info form.
  96. unsigned char compactPeer[COMPACT_LEN_IPV6];
  97. int compactlen = bittorrent::packcompact
  98. (compactPeer, node->getIPAddress(), node->getPort());
  99. if(compactlen != clen) {
  100. memset(compactPeer, 0, clen);
  101. }
  102. // 1byte compact peer format length
  103. o << static_cast<uint8_t>(clen);
  104. // 7bytes reserved
  105. o.write(zero, 7);
  106. // clen bytes compact peer
  107. o.write(reinterpret_cast<const char*>(compactPeer), clen);
  108. // 24-clen bytes reserved
  109. o.write(zero, 24-clen);
  110. // 20bytes: node ID
  111. o.write(reinterpret_cast<const char*>(node->getID()), DHT_ID_LENGTH);
  112. // 4bytes reserved
  113. o.write(zero, 4);
  114. }
  115. o.flush();
  116. if(!o) {
  117. throw DL_ABORT_EX("Failed to save DHT routing table.");
  118. }
  119. }
  120. } // namespace aria2