DHTRoutingTableSerializer.cc 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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 <cstdio>
  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. #include "fmt.h"
  47. #include "File.h"
  48. #include "LogFactory.h"
  49. #include "BufferedFile.h"
  50. namespace aria2 {
  51. DHTRoutingTableSerializer::DHTRoutingTableSerializer(int family)
  52. : family_(family)
  53. {
  54. }
  55. DHTRoutingTableSerializer::~DHTRoutingTableSerializer() = default;
  56. void DHTRoutingTableSerializer::setLocalNode(
  57. const std::shared_ptr<DHTNode>& localNode)
  58. {
  59. localNode_ = localNode;
  60. }
  61. void DHTRoutingTableSerializer::setNodes(
  62. const std::vector<std::shared_ptr<DHTNode>>& nodes)
  63. {
  64. nodes_ = nodes;
  65. }
  66. #define WRITE_CHECK(fp, ptr, count) \
  67. if (fp.write((ptr), (count)) != (count)) { \
  68. throw DL_ABORT_EX( \
  69. fmt("Failed to save DHT routing table to %s.", filename.c_str())); \
  70. }
  71. void DHTRoutingTableSerializer::serialize(const std::string& filename)
  72. {
  73. A2_LOG_INFO(fmt("Saving DHT routing table to %s.", filename.c_str()));
  74. std::string filenameTemp = filename;
  75. filenameTemp += "__temp";
  76. BufferedFile fp(filenameTemp.c_str(), BufferedFile::WRITE);
  77. if (!fp) {
  78. throw DL_ABORT_EX(
  79. fmt("Failed to save DHT routing table to %s.", filename.c_str()));
  80. }
  81. char header[8];
  82. memset(header, 0, sizeof(header));
  83. // magic
  84. header[0] = 0xa1u;
  85. header[1] = 0xa2u;
  86. // format ID
  87. header[2] = 0x02u;
  88. // version
  89. header[6] = 0;
  90. header[7] = 0x03u;
  91. char zero[18];
  92. memset(zero, 0, sizeof(zero));
  93. WRITE_CHECK(fp, header, 8);
  94. // write save date
  95. uint64_t ntime = hton64(Time().getTimeFromEpoch());
  96. WRITE_CHECK(fp, &ntime, sizeof(ntime));
  97. // localnode
  98. // 8bytes reserved
  99. WRITE_CHECK(fp, zero, 8);
  100. // 20bytes localnode ID
  101. WRITE_CHECK(fp, localNode_->getID(), DHT_ID_LENGTH);
  102. // 4bytes reserved
  103. WRITE_CHECK(fp, zero, 4);
  104. // number of nodes
  105. uint32_t numNodes = htonl(nodes_.size());
  106. WRITE_CHECK(fp, &numNodes, sizeof(uint32_t));
  107. // 4bytes reserved
  108. WRITE_CHECK(fp, zero, 4);
  109. const int clen = bittorrent::getCompactLength(family_);
  110. // nodes
  111. for (std::vector<std::shared_ptr<DHTNode>>::const_iterator i = nodes_.begin(),
  112. eoi = nodes_.end();
  113. i != eoi; ++i) {
  114. const std::shared_ptr<DHTNode>& node = *i;
  115. // Write IP address + port in Compact IP-address/port info form.
  116. unsigned char compactPeer[COMPACT_LEN_IPV6];
  117. int compactlen = bittorrent::packcompact(compactPeer, node->getIPAddress(),
  118. node->getPort());
  119. if (compactlen != clen) {
  120. memset(compactPeer, 0, clen);
  121. }
  122. uint8_t clen1 = clen;
  123. // 1byte compact peer format length
  124. WRITE_CHECK(fp, &clen1, sizeof(clen1));
  125. // 7bytes reserved
  126. WRITE_CHECK(fp, zero, 7);
  127. // clen bytes compact peer
  128. WRITE_CHECK(fp, compactPeer, static_cast<size_t>(clen));
  129. // 24-clen bytes reserved
  130. WRITE_CHECK(fp, zero, static_cast<size_t>(24 - clen));
  131. // 20bytes: node ID
  132. WRITE_CHECK(fp, node->getID(), DHT_ID_LENGTH);
  133. // 4bytes reserved
  134. WRITE_CHECK(fp, zero, 4);
  135. }
  136. if (fp.close() == EOF) {
  137. throw DL_ABORT_EX(
  138. fmt("Failed to save DHT routing table to %s.", filename.c_str()));
  139. }
  140. if (!File(filenameTemp).renameTo(filename)) {
  141. throw DL_ABORT_EX(
  142. fmt("Failed to save DHT routing table to %s.", filename.c_str()));
  143. }
  144. A2_LOG_INFO("DHT routing table was saved successfully");
  145. }
  146. } // namespace aria2