DHTRoutingTableSerializer.cc 5.0 KB

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