DHTRoutingTableSerializer.cc 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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 "DHTNode.h"
  37. #include "DlAbortEx.h"
  38. #include "DHTConstants.h"
  39. #include "PeerMessageUtil.h"
  40. #include "Logger.h"
  41. #include "a2netcompat.h"
  42. #include "StringFormat.h"
  43. #include <cerrno>
  44. #include <cstring>
  45. #include <ostream>
  46. namespace aria2 {
  47. DHTRoutingTableSerializer::DHTRoutingTableSerializer() {}
  48. DHTRoutingTableSerializer::~DHTRoutingTableSerializer() {}
  49. void DHTRoutingTableSerializer::setLocalNode(const SharedHandle<DHTNode>& localNode)
  50. {
  51. _localNode = localNode;
  52. }
  53. void DHTRoutingTableSerializer::setNodes(const std::deque<SharedHandle<DHTNode> >& nodes)
  54. {
  55. _nodes = nodes;
  56. }
  57. void DHTRoutingTableSerializer::serialize(std::ostream& o)
  58. {
  59. char header[8];
  60. memset(header, 0, sizeof(header));
  61. // magic
  62. header[0] = 0xa1;
  63. header[1] = 0xa2;
  64. // format ID
  65. header[2] = 0x02;
  66. // version
  67. header[6] = 0;
  68. header[7] = 0x02;
  69. char zero[16];
  70. memset(zero, 0, sizeof(zero));
  71. try {
  72. o.write(header, 8);
  73. // write save date
  74. uint32_t ntime = htonl(Time().getTime());
  75. o.write(reinterpret_cast<const char*>(&ntime), sizeof(uint32_t));
  76. // 4bytes reserved
  77. o.write(zero, 4);
  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. // nodes
  91. for(std::deque<SharedHandle<DHTNode> >::const_iterator i = _nodes.begin(); i != _nodes.end(); ++i) {
  92. const SharedHandle<DHTNode>& node = *i;
  93. // Currently, only IPv4 address and IPv4-mapped address are saved.
  94. // 6bytes: write IP address + port in Compact IP-address/port info form.
  95. unsigned char compactPeer[6];
  96. if(!PeerMessageUtil::createcompact(compactPeer, node->getIPAddress(), node->getPort())) {
  97. memset(compactPeer, 0, 6);
  98. }
  99. // 1byte compact peer format length
  100. o << static_cast<uint8_t>(sizeof(compactPeer));
  101. // 7bytes reserved
  102. o.write(zero, 7);
  103. // 6 bytes compact peer
  104. o.write(reinterpret_cast<const char*>(compactPeer), 6);
  105. // 2bytes reserved
  106. o.write(zero, 2);
  107. // 16bytes reserved
  108. o.write(zero, 16);
  109. // 20bytes: node ID
  110. o.write(reinterpret_cast<const char*>(node->getID()), DHT_ID_LENGTH);
  111. // 4bytes reserved
  112. o.write(zero, 4);
  113. }
  114. } catch(std::ios::failure const& exception) {
  115. throw DlAbortEx
  116. (StringFormat("Failed to save DHT routing table. cause:%s",
  117. strerror(errno)).str());
  118. }
  119. }
  120. } // namespace aria2