DHTRoutingTableSerializer.cc 5.0 KB

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