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