DHTRoutingTableSerializer.cc 4.2 KB

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