/* */ #include "DHTEntryPointNameResolveCommand.h" #include "DownloadEngine.h" #include "NameResolver.h" #include "DlAbortEx.h" #include "prefs.h" #include "message.h" #include "util.h" #include "Option.h" #include "DHTNode.h" #include "DHTTaskQueue.h" #include "DHTTaskFactory.h" #include "DHTRoutingTable.h" #include "DHTTask.h" #include "RequestGroupMan.h" #include "Logger.h" #include "LogFactory.h" #include "fmt.h" #include "SocketCore.h" #ifdef ENABLE_ASYNC_DNS #include "AsyncNameResolverMan.h" #endif // ENABLE_ASYNC_DNS namespace aria2 { DHTEntryPointNameResolveCommand::DHTEntryPointNameResolveCommand (cuid_t cuid, DownloadEngine* e, const std::vector >& entryPoints): Command(cuid), e_(e), asyncNameResolverMan_(new AsyncNameResolverMan()), entryPoints_(entryPoints.begin(), entryPoints.end()), numSuccess_(0), bootstrapEnabled_(false) { #ifdef ENABLE_ASYNC_DNS configureAsyncNameResolverMan(asyncNameResolverMan_.get(), e_->getOption()); #endif // ENABLE_ASYNC_DNS } DHTEntryPointNameResolveCommand::~DHTEntryPointNameResolveCommand() { #ifdef ENABLE_ASYNC_DNS asyncNameResolverMan_->disableNameResolverCheck(e_, this); #endif // ENABLE_ASYNC_DNS } bool DHTEntryPointNameResolveCommand::execute() { if(e_->getRequestGroupMan()->downloadFinished() || e_->isHaltRequested()) { return true; } try { #ifdef ENABLE_ASYNC_DNS if(e_->getOption()->getAsBool(PREF_ASYNC_DNS)) { while(!entryPoints_.empty()) { std::string hostname = entryPoints_.front().first; if(util::isNumericHost(hostname)) { ++numSuccess_; std::pair p(hostname, entryPoints_.front().second); addPingTask(p); } else { try { if(resolveHostname(hostname)) { std::vector addrs; asyncNameResolverMan_->getResolvedAddress(addrs); if(addrs.empty()) { A2_LOG_ERROR(fmt("No address returned for %s", hostname.c_str())); } else { A2_LOG_INFO(fmt(MSG_NAME_RESOLUTION_COMPLETE, getCuid(), hostname.c_str(), addrs.front().c_str())); ++numSuccess_; std::pair p (addrs.front(), entryPoints_.front().second); addPingTask(p); } } else { e_->addCommand(this); return false; } } catch(RecoverableException& e) { A2_LOG_ERROR_EX(EX_EXCEPTION_CAUGHT, e); } asyncNameResolverMan_->reset(e_, this); } entryPoints_.pop_front(); } } else #endif // ENABLE_ASYNC_DNS { NameResolver res; res.setSocktype(SOCK_DGRAM); while(!entryPoints_.empty()) { std::string hostname = entryPoints_.front().first; try { std::vector addrs; res.resolve(addrs, hostname); ++numSuccess_; std::pair p(addrs.front(), entryPoints_.front().second); addPingTask(p); } catch(RecoverableException& e) { A2_LOG_ERROR_EX(EX_EXCEPTION_CAUGHT, e); } entryPoints_.pop_front(); } } if(bootstrapEnabled_ && numSuccess_) { taskQueue_->addPeriodicTask1(taskFactory_->createNodeLookupTask (localNode_->getID())); taskQueue_->addPeriodicTask1(taskFactory_->createBucketRefreshTask()); } } catch(RecoverableException& e) { A2_LOG_ERROR_EX(EX_EXCEPTION_CAUGHT, e); } return true; } void DHTEntryPointNameResolveCommand::addPingTask (const std::pair& addr) { SharedHandle entryNode(new DHTNode()); entryNode->setIPAddress(addr.first); entryNode->setPort(addr.second); taskQueue_->addPeriodicTask1(taskFactory_->createPingTask(entryNode, 10)); } #ifdef ENABLE_ASYNC_DNS bool DHTEntryPointNameResolveCommand::resolveHostname (const std::string& hostname) { if(!asyncNameResolverMan_->started()) { asyncNameResolverMan_->startAsync(hostname, e_, this); } else { switch(asyncNameResolverMan_->getStatus()) { case -1: throw DL_ABORT_EX2 (fmt(MSG_NAME_RESOLUTION_FAILED, getCuid(), hostname.c_str(), asyncNameResolverMan_->getLastError().c_str()), error_code::NAME_RESOLVE_ERROR); case 0: return false; case 1: return true; } } return false; } #endif // ENABLE_ASYNC_DNS void DHTEntryPointNameResolveCommand::setBootstrapEnabled(bool f) { bootstrapEnabled_ = f; } void DHTEntryPointNameResolveCommand::setTaskQueue (const SharedHandle& taskQueue) { taskQueue_ = taskQueue; } void DHTEntryPointNameResolveCommand::setTaskFactory (const SharedHandle& taskFactory) { taskFactory_ = taskFactory; } void DHTEntryPointNameResolveCommand::setRoutingTable (const SharedHandle& routingTable) { routingTable_ = routingTable; } void DHTEntryPointNameResolveCommand::setLocalNode (const SharedHandle& localNode) { localNode_ = localNode; } } // namespace aria2