DHTSetup.cc 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  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 "DHTSetup.h"
  36. #include <fstream>
  37. #include <algorithm>
  38. #include "LogFactory.h"
  39. #include "Logger.h"
  40. #include "util.h"
  41. #include "DHTNode.h"
  42. #include "DHTConnectionImpl.h"
  43. #include "DHTRoutingTable.h"
  44. #include "DHTMessageFactoryImpl.h"
  45. #include "DHTMessageTracker.h"
  46. #include "DHTMessageDispatcherImpl.h"
  47. #include "DHTMessageReceiver.h"
  48. #include "DHTTaskQueueImpl.h"
  49. #include "DHTTaskFactoryImpl.h"
  50. #include "DHTPeerAnnounceStorage.h"
  51. #include "DHTTokenTracker.h"
  52. #include "DHTInteractionCommand.h"
  53. #include "DHTTokenUpdateCommand.h"
  54. #include "DHTBucketRefreshCommand.h"
  55. #include "DHTPeerAnnounceCommand.h"
  56. #include "DHTEntryPointNameResolveCommand.h"
  57. #include "DHTAutoSaveCommand.h"
  58. #include "DHTTask.h"
  59. #include "DHTRoutingTableDeserializer.h"
  60. #include "DHTRegistry.h"
  61. #include "DHTBucketRefreshTask.h"
  62. #include "DHTMessageCallback.h"
  63. #include "prefs.h"
  64. #include "Option.h"
  65. #include "SocketCore.h"
  66. #include "DlAbortEx.h"
  67. #include "RecoverableException.h"
  68. #include "a2functional.h"
  69. #include "DownloadEngine.h"
  70. #include "fmt.h"
  71. namespace aria2 {
  72. DHTSetup::DHTSetup() {}
  73. DHTSetup::~DHTSetup() {}
  74. void DHTSetup::setup
  75. (std::vector<Command*>& commands, DownloadEngine* e, int family)
  76. {
  77. if(family != AF_INET && family != AF_INET6) {
  78. return;
  79. }
  80. if((family == AF_INET && DHTRegistry::isInitialized()) ||
  81. (family == AF_INET6 && DHTRegistry::isInitialized6())) {
  82. return;
  83. }
  84. try {
  85. std::vector<Command*>* tempCommands = new std::vector<Command*>();
  86. auto_delete_container<std::vector<Command*> > commandsDel(tempCommands);
  87. // load routing table and localnode id here
  88. SharedHandle<DHTNode> localNode;
  89. DHTRoutingTableDeserializer deserializer(family);
  90. const std::string& dhtFile =
  91. e->getOption()->get(family == AF_INET?PREF_DHT_FILE_PATH:
  92. PREF_DHT_FILE_PATH6);
  93. try {
  94. std::ifstream in(dhtFile.c_str(), std::ios::binary);
  95. if(!in) {
  96. throw DL_ABORT_EX("Could not open file");
  97. }
  98. deserializer.deserialize(in);
  99. localNode = deserializer.getLocalNode();
  100. } catch(RecoverableException& e) {
  101. A2_LOG_ERROR_EX
  102. (fmt("Exception caught while loading DHT routing table from %s",
  103. dhtFile.c_str()),
  104. e);
  105. }
  106. if(!localNode) {
  107. localNode.reset(new DHTNode());
  108. }
  109. SharedHandle<DHTConnectionImpl> connection(new DHTConnectionImpl(family));
  110. {
  111. IntSequence seq =
  112. util::parseIntRange(e->getOption()->get(PREF_DHT_LISTEN_PORT));
  113. uint16_t port;
  114. const std::string& addr =
  115. e->getOption()->get(family == AF_INET?PREF_DHT_LISTEN_ADDR:
  116. PREF_DHT_LISTEN_ADDR6);
  117. if(!connection->bind(port, addr, seq)) {
  118. throw DL_ABORT_EX("Error occurred while binding port for DHT");
  119. }
  120. localNode->setPort(port);
  121. }
  122. A2_LOG_DEBUG(fmt("Initialized local node ID=%s",
  123. util::toHex(localNode->getID(), DHT_ID_LENGTH).c_str()));
  124. SharedHandle<DHTRoutingTable> routingTable(new DHTRoutingTable(localNode));
  125. SharedHandle<DHTMessageFactoryImpl> factory
  126. (new DHTMessageFactoryImpl(family));
  127. SharedHandle<DHTMessageTracker> tracker(new DHTMessageTracker());
  128. SharedHandle<DHTMessageDispatcherImpl> dispatcher(new DHTMessageDispatcherImpl(tracker));
  129. SharedHandle<DHTMessageReceiver> receiver(new DHTMessageReceiver(tracker));
  130. SharedHandle<DHTTaskQueue> taskQueue(new DHTTaskQueueImpl());
  131. SharedHandle<DHTTaskFactoryImpl> taskFactory(new DHTTaskFactoryImpl());
  132. SharedHandle<DHTPeerAnnounceStorage> peerAnnounceStorage(new DHTPeerAnnounceStorage());
  133. SharedHandle<DHTTokenTracker> tokenTracker(new DHTTokenTracker());
  134. const time_t messageTimeout = e->getOption()->getAsInt(PREF_DHT_MESSAGE_TIMEOUT);
  135. // wiring up
  136. tracker->setRoutingTable(routingTable);
  137. tracker->setMessageFactory(factory);
  138. dispatcher->setTimeout(messageTimeout);
  139. receiver->setConnection(connection);
  140. receiver->setMessageFactory(factory);
  141. receiver->setRoutingTable(routingTable);
  142. taskFactory->setLocalNode(localNode);
  143. taskFactory->setRoutingTable(routingTable.get());
  144. taskFactory->setMessageDispatcher(dispatcher.get());
  145. taskFactory->setMessageFactory(factory.get());
  146. taskFactory->setTaskQueue(taskQueue.get());
  147. taskFactory->setTimeout(messageTimeout);
  148. routingTable->setTaskQueue(taskQueue);
  149. routingTable->setTaskFactory(taskFactory);
  150. peerAnnounceStorage->setTaskQueue(taskQueue);
  151. peerAnnounceStorage->setTaskFactory(taskFactory);
  152. factory->setRoutingTable(routingTable.get());
  153. factory->setConnection(connection.get());
  154. factory->setMessageDispatcher(dispatcher.get());
  155. factory->setPeerAnnounceStorage(peerAnnounceStorage.get());
  156. factory->setTokenTracker(tokenTracker.get());
  157. factory->setLocalNode(localNode);
  158. // assign them into DHTRegistry
  159. if(family == AF_INET) {
  160. DHTRegistry::getMutableData().localNode = localNode;
  161. DHTRegistry::getMutableData().routingTable = routingTable;
  162. DHTRegistry::getMutableData().taskQueue = taskQueue;
  163. DHTRegistry::getMutableData().taskFactory = taskFactory;
  164. DHTRegistry::getMutableData().peerAnnounceStorage = peerAnnounceStorage;
  165. DHTRegistry::getMutableData().tokenTracker = tokenTracker;
  166. DHTRegistry::getMutableData().messageDispatcher = dispatcher;
  167. DHTRegistry::getMutableData().messageReceiver = receiver;
  168. DHTRegistry::getMutableData().messageFactory = factory;
  169. } else {
  170. DHTRegistry::getMutableData6().localNode = localNode;
  171. DHTRegistry::getMutableData6().routingTable = routingTable;
  172. DHTRegistry::getMutableData6().taskQueue = taskQueue;
  173. DHTRegistry::getMutableData6().taskFactory = taskFactory;
  174. DHTRegistry::getMutableData6().peerAnnounceStorage = peerAnnounceStorage;
  175. DHTRegistry::getMutableData6().tokenTracker = tokenTracker;
  176. DHTRegistry::getMutableData6().messageDispatcher = dispatcher;
  177. DHTRegistry::getMutableData6().messageReceiver = receiver;
  178. DHTRegistry::getMutableData6().messageFactory = factory;
  179. }
  180. // add deserialized nodes to routing table
  181. const std::vector<SharedHandle<DHTNode> >& desnodes =
  182. deserializer.getNodes();
  183. for(std::vector<SharedHandle<DHTNode> >::const_iterator i =
  184. desnodes.begin(), eoi = desnodes.end(); i != eoi; ++i) {
  185. routingTable->addNode(*i);
  186. }
  187. if(!desnodes.empty()) {
  188. SharedHandle<DHTBucketRefreshTask> task
  189. (static_pointer_cast<DHTBucketRefreshTask>
  190. (taskFactory->createBucketRefreshTask()));
  191. task->setForceRefresh(true);
  192. taskQueue->addPeriodicTask1(task);
  193. }
  194. const std::string& prefEntryPointHost =
  195. family == AF_INET?PREF_DHT_ENTRY_POINT_HOST:PREF_DHT_ENTRY_POINT_HOST6;
  196. if(!e->getOption()->get(prefEntryPointHost).empty()) {
  197. {
  198. const std::string& prefEntryPointPort =
  199. family == AF_INET?PREF_DHT_ENTRY_POINT_PORT:
  200. PREF_DHT_ENTRY_POINT_PORT6;
  201. std::pair<std::string, uint16_t> addr
  202. (e->getOption()->get(prefEntryPointHost),
  203. e->getOption()->getAsInt(prefEntryPointPort));
  204. std::vector<std::pair<std::string, uint16_t> > entryPoints;
  205. entryPoints.push_back(addr);
  206. DHTEntryPointNameResolveCommand* command =
  207. new DHTEntryPointNameResolveCommand(e->newCUID(), e, entryPoints);
  208. command->setBootstrapEnabled(true);
  209. command->setTaskQueue(taskQueue);
  210. command->setTaskFactory(taskFactory);
  211. command->setRoutingTable(routingTable);
  212. command->setLocalNode(localNode);
  213. tempCommands->push_back(command);
  214. }
  215. } else {
  216. A2_LOG_INFO("No DHT entry point specified.");
  217. }
  218. {
  219. DHTInteractionCommand* command =
  220. new DHTInteractionCommand(e->newCUID(), e);
  221. command->setMessageDispatcher(dispatcher);
  222. command->setMessageReceiver(receiver);
  223. command->setTaskQueue(taskQueue);
  224. command->setReadCheckSocket(connection->getSocket());
  225. tempCommands->push_back(command);
  226. }
  227. {
  228. DHTTokenUpdateCommand* command =
  229. new DHTTokenUpdateCommand(e->newCUID(), e, DHT_TOKEN_UPDATE_INTERVAL);
  230. command->setTokenTracker(tokenTracker);
  231. tempCommands->push_back(command);
  232. }
  233. {
  234. DHTBucketRefreshCommand* command =
  235. new DHTBucketRefreshCommand(e->newCUID(), e,
  236. DHT_BUCKET_REFRESH_CHECK_INTERVAL);
  237. command->setTaskQueue(taskQueue);
  238. command->setRoutingTable(routingTable);
  239. command->setTaskFactory(taskFactory);
  240. tempCommands->push_back(command);
  241. }
  242. {
  243. DHTPeerAnnounceCommand* command =
  244. new DHTPeerAnnounceCommand(e->newCUID(), e,
  245. DHT_PEER_ANNOUNCE_CHECK_INTERVAL);
  246. command->setPeerAnnounceStorage(peerAnnounceStorage);
  247. tempCommands->push_back(command);
  248. }
  249. {
  250. DHTAutoSaveCommand* command =
  251. new DHTAutoSaveCommand(e->newCUID(), e, family, 30*60);
  252. command->setLocalNode(localNode);
  253. command->setRoutingTable(routingTable);
  254. tempCommands->push_back(command);
  255. }
  256. if(family == AF_INET) {
  257. DHTRegistry::setInitialized(true);
  258. } else {
  259. DHTRegistry::setInitialized6(true);
  260. }
  261. commands.insert(commands.end(), tempCommands->begin(), tempCommands->end());
  262. tempCommands->clear();
  263. } catch(RecoverableException& e) {
  264. A2_LOG_ERROR_EX(fmt("Exception caught while initializing DHT functionality."
  265. " DHT is disabled."),
  266. e);
  267. if(family == AF_INET) {
  268. DHTRegistry::clearData();
  269. } else {
  270. DHTRegistry::clearData6();
  271. }
  272. }
  273. }
  274. } // namespace aria2