DHTRoutingTableSerializer.cc 4.2 KB

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