aria2/src/DHTEntryPointNameResolveCom...

240 lines
7.4 KiB
C++
Raw Normal View History

/* <!-- copyright */
/*
* aria2 - The high speed download utility
*
* Copyright (C) 2006 Tatsuhiro Tsujikawa
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
* In addition, as a special exception, the copyright holders give
* permission to link the code of portions of this program with the
* OpenSSL library under certain conditions as described in each
* individual source file, and distribute linked combinations
* including the two.
* You must obey the GNU General Public License in all respects
* for all of the code used other than OpenSSL. If you modify
* file(s) with this exception, you may extend this exception to your
* version of the file(s), but you are not obligated to do so. If you
* do not wish to do so, delete this exception statement from your
* version. If you delete this exception statement from all source
* files in the program, then also delete it here.
*/
/* copyright --> */
#include "DHTEntryPointNameResolveCommand.h"
#include "DownloadEngine.h"
#ifdef ENABLE_ASYNC_DNS
#include "AsyncNameResolver.h"
#endif // ENABLE_ASYNC_DNS
#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 "StringFormat.h"
2009-06-29 Tatsuhiro Tsujikawa <t-tujikawa@users.sourceforge.net> Removed _uris from RequestGroup. All functions that refer to _uris were moved to FileEntry. Exit status code are now defined in DownloadResultCode.h. * src/AbstractCommand.cc * src/AdaptiveURISelector.cc * src/AdaptiveURISelector.h * src/AutoSaveCommand.cc * src/BtPostDownloadHandler.cc * src/CheckIntegrityDispatcherCommand.cc * src/CookieStorage.cc * src/DHTAutoSaveCommand.cc * src/DHTBucketRefreshCommand.cc * src/DHTEntryPointNameResolveCommand.cc * src/DHTInteractionCommand.cc * src/DHTPeerAnnounceCommand.cc * src/DHTTokenUpdateCommand.cc * src/DlAbortEx.h * src/DlRetryEx.h * src/DownloadCommand.cc * src/DownloadContext.h * src/DownloadFailureException.h * src/DownloadResult.h * src/DownloadResultCode.h * src/FeedbackURISelector.cc * src/FeedbackURISelector.h * src/FileEntry.cc * src/FileEntry.h * src/FtpNegotiationCommand.cc * src/HttpListenCommand.cc * src/HttpResponseCommand.cc * src/HttpServerResponseCommand.cc * src/HttpSkipResponseCommand.cc * src/InOrderURISelector.cc * src/InOrderURISelector.h * src/Makefile.am * src/Makefile.in * src/Metalink2RequestGroup.cc * src/MultiUrlRequestInfo.cc * src/MultiUrlRequestInfo.h * src/OptionHandlerFactory.cc * src/PeerListenCommand.cc * src/RecoverableException.h * src/RequestGroup.cc * src/RequestGroup.h * src/RequestGroupMan.cc * src/RequestGroupMan.h * src/TimedHaltCommand.cc * src/TrackerWatcherCommand.cc * src/URIResult.cc * src/URIResult.h * src/URISelector.h * src/XmlRpcMethodImpl.cc * src/bittorrent_helper.cc * src/bittorrent_helper.h * src/download_helper.cc * src/main.cc * src/option_processing.cc * test/BtDependencyTest.cc * test/BtPostDownloadHandlerTest.cc * test/CookieStorageTest.cc * test/DefaultBtMessageDispatcherTest.cc * test/DownloadHandlerFactoryTest.cc * test/DownloadHelperTest.cc * test/FeedbackURISelectorTest.cc * test/FileEntryTest.cc * test/InOrderURISelectorTest.cc * test/Metalink2RequestGroupTest.cc * test/MetalinkPostDownloadHandlerTest.cc * test/RequestGroupManTest.cc * test/RequestGroupTest.cc * test/XmlRpcMethodTest.cc
2009-06-29 08:42:58 +00:00
#include "FileEntry.h"
#include "ServerStatMan.h"
#include "FileAllocationEntry.h"
#include "CheckIntegrityEntry.h"
namespace aria2 {
DHTEntryPointNameResolveCommand::DHTEntryPointNameResolveCommand
(cuid_t cuid, DownloadEngine* e,
const std::vector<std::pair<std::string, uint16_t> >& entryPoints):
Command(cuid),
e_(e),
entryPoints_(entryPoints.begin(), entryPoints.end()),
bootstrapEnabled_(false)
{}
DHTEntryPointNameResolveCommand::~DHTEntryPointNameResolveCommand()
{
#ifdef ENABLE_ASYNC_DNS
disableNameResolverCheck(resolver_);
#endif // ENABLE_ASYNC_DNS
}
bool DHTEntryPointNameResolveCommand::execute()
{
if(e_->getRequestGroupMan()->downloadFinished() || e_->isHaltRequested()) {
return true;
}
#ifdef ENABLE_ASYNC_DNS
if(resolver_.isNull()) {
int family;
if(e_->getOption()->getAsBool(PREF_ENABLE_ASYNC_DNS6)) {
family = AF_UNSPEC;
} else {
family = AF_INET;
}
resolver_.reset(new AsyncNameResolver(family));
}
#endif // ENABLE_ASYNC_DNS
try {
#ifdef ENABLE_ASYNC_DNS
if(e_->getOption()->getAsBool(PREF_ASYNC_DNS)) {
while(!entryPoints_.empty()) {
std::string hostname = entryPoints_.front().first;
try {
2010-08-07 Tatsuhiro Tsujikawa <t-tujikawa@users.sourceforge.net> Added IPv6 DHT. Added --dht-entry-porint6, --dht-file-path6, --dht-listen-addr6 and --enable-dht6 option. IPv6 DHT is disabled by default. To use IPv6 DHT, you need to use --enable-dht6 and specify a global unicast address to --dht-listen-addr6. IPv6 DHT is highly experimental. * src/BtSetup.cc * src/DHTAutoSaveCommand.cc * src/DHTAutoSaveCommand.h * src/DHTConnectionImpl.cc * src/DHTConnectionImpl.h * src/DHTEntryPointNameResolveCommand.cc * src/DHTFindNodeReplyMessage.cc * src/DHTFindNodeReplyMessage.h * src/DHTGetPeersMessage.cc * src/DHTGetPeersReplyMessage.cc * src/DHTGetPeersReplyMessage.h * src/DHTMessageFactory.h * src/DHTMessageFactoryImpl.cc * src/DHTMessageFactoryImpl.h * src/DHTMessageTracker.cc * src/DHTRegistry.cc * src/DHTRegistry.h * src/DHTRoutingTableDeserializer.cc * src/DHTRoutingTableDeserializer.h * src/DHTRoutingTableSerializer.cc * src/DHTRoutingTableSerializer.h * src/DHTSetup.cc * src/DHTSetup.h * src/FtpConnection.cc * src/LpdMessageReceiver.cc * src/OptionHandlerFactory.cc * src/OptionHandlerImpl.h * src/PeerInteractionCommand.cc * src/RequestGroup.cc * src/SocketCore.cc * src/SocketCore.h * src/bittorrent_helper.cc * src/bittorrent_helper.h * src/prefs.cc * src/prefs.h * src/usage_text.h * test/DHTConnectionImplTest.cc * test/DHTFindNodeReplyMessageTest.cc * test/DHTGetPeersMessageTest.cc * test/DHTGetPeersReplyMessageTest.cc * test/DHTMessageFactoryImplTest.cc * test/DHTRoutingTableDeserializerTest.cc * test/DHTRoutingTableSerializerTest.cc * test/LpdMessageDispatcherTest.cc * test/MockDHTMessageFactory.h
2010-08-07 14:15:21 +00:00
if(util::isNumericHost(hostname)) {
std::pair<std::string, uint16_t> p
(hostname, entryPoints_.front().second);
resolvedEntryPoints_.push_back(p);
addPingTask(p);
} else if(resolveHostname(hostname, resolver_)) {
hostname = resolver_->getResolvedAddresses().front();
std::pair<std::string, uint16_t> p(hostname,
entryPoints_.front().second);
resolvedEntryPoints_.push_back(p);
addPingTask(p);
} else {
e_->addCommand(this);
return false;
}
} catch(RecoverableException& e) {
getLogger()->error(EX_EXCEPTION_CAUGHT, e);
}
resolver_->reset();
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<std::string> addrs;
res.resolve(addrs, hostname);
std::pair<std::string, uint16_t> p(addrs.front(),
entryPoints_.front().second);
resolvedEntryPoints_.push_back(p);
addPingTask(p);
} catch(RecoverableException& e) {
getLogger()->error(EX_EXCEPTION_CAUGHT, e);
}
entryPoints_.pop_front();
}
}
if(bootstrapEnabled_ && resolvedEntryPoints_.size()) {
taskQueue_->addPeriodicTask1(taskFactory_->createNodeLookupTask
(localNode_->getID()));
taskQueue_->addPeriodicTask1(taskFactory_->createBucketRefreshTask());
}
} catch(RecoverableException& e) {
getLogger()->error(EX_EXCEPTION_CAUGHT, e);
}
return true;
}
void DHTEntryPointNameResolveCommand::addPingTask
(const std::pair<std::string, uint16_t>& addr)
{
SharedHandle<DHTNode> 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,
const SharedHandle<AsyncNameResolver>& resolver)
{
switch(resolver->getStatus()) {
case AsyncNameResolver::STATUS_READY:
if(getLogger()->info()) {
getLogger()->info(MSG_RESOLVING_HOSTNAME,
util::itos(getCuid()).c_str(), hostname.c_str());
}
resolver->resolve(hostname);
setNameResolverCheck(resolver);
return false;
case AsyncNameResolver::STATUS_SUCCESS:
if(getLogger()->info()) {
getLogger()->info(MSG_NAME_RESOLUTION_COMPLETE,
util::itos(getCuid()).c_str(),
resolver->getHostname().c_str(),
resolver->getResolvedAddresses().front().c_str());
}
return true;
break;
case AsyncNameResolver::STATUS_ERROR:
2009-05-18 Tatsuhiro Tsujikawa <t-tujikawa@users.sourceforge.net> Added source filename(__FILE__) and line number(__LINE__) to exception message. * src/AbstractCommand.cc * src/AbstractDiskWriter.cc * src/AbstractProxyResponseCommand.cc * src/BDE.h * src/BtAllowedFastMessage.cc * src/BtHandshakeMessageValidator.h * src/BtHaveAllMessage.cc * src/BtHaveNoneMessage.cc * src/BtPieceMessage.cc * src/BtRejectMessage.cc * src/ChunkedDecoder.cc * src/CookieStorage.cc * src/DHTAnnouncePeerMessage.cc * src/DHTEntryPointNameResolveCommand.cc * src/DHTMessageFactoryImpl.cc * src/DHTMessageTracker.cc * src/DHTRoutingTableDeserializer.cc * src/DHTRoutingTableSerializer.cc * src/DHTSetup.cc * src/DHTTokenTracker.cc * src/DefaultBtAnnounce.cc * src/DefaultBtContext.cc * src/DefaultBtInteractive.cc * src/DefaultBtMessageFactory.cc * src/DefaultBtProgressInfoFile.cc * src/DefaultExtensionMessageFactory.cc * src/DlAbortEx.h * src/DlRetryEx.h * src/DownloadCommand.cc * src/DownloadEngineFactory.cc * src/DownloadFailureException.h * src/Exception.cc * src/Exception.h * src/ExpatMetalinkProcessor.cc * src/ExpatXmlRpcRequestProcessor.cc * src/FallocFileAllocationIterator.cc * src/FatalException.h * src/FtpConnection.cc * src/FtpFinishDownloadCommand.cc * src/FtpInitiateConnectionCommand.cc * src/FtpNegotiationCommand.cc * src/GZipDecoder.cc * src/HandshakeExtensionMessage.cc * src/HttpConnection.cc * src/HttpHeaderProcessor.cc * src/HttpInitiateConnectionCommand.cc * src/HttpResponse.cc * src/HttpResponseCommand.cc * src/HttpServer.cc * src/HttpSkipResponseCommand.cc * src/InitiateConnectionCommandFactory.cc * src/IteratableChunkChecksumValidator.cc * src/LibgcryptARC4Context.h * src/LibgcryptARC4Decryptor.h * src/LibgcryptARC4Encryptor.h * src/LibgcryptDHKeyExchange.h * src/LibgnutlsTLSContext.cc * src/LibsslARC4Context.h * src/LibsslARC4Decryptor.h * src/LibsslARC4Encryptor.h * src/LibsslDHKeyExchange.h * src/LibsslTLSContext.cc * src/MSEHandshake.cc * src/MessageDigestHelper.cc * src/MetalinkHelper.cc * src/MultiDiskAdaptor.cc * src/NameMatchOptionHandler.h * src/NameResolver.cc * src/Netrc.cc * src/NsCookieParser.cc * src/OptionHandlerException.cc * src/OptionHandlerException.h * src/OptionHandlerImpl.h * src/OptionParser.cc * src/ParameterizedStringParser.cc * src/PeerAbstractCommand.cc * src/PeerConnection.cc * src/PeerMessageUtil.cc * src/PeerReceiveHandshakeCommand.cc * src/Platform.cc * src/ReceiverMSEHandshakeCommand.cc * src/RecoverableException.h * src/RequestGroup.cc * src/SimpleLogger.cc * src/SocketCore.cc * src/Sqlite3MozCookieParser.cc * src/UTPexExtensionMessage.cc * src/Util.cc * src/XML2SAXMetalinkProcessor.cc * src/Xml2XmlRpcRequestProcessor.cc * src/XmlRpcMethodImpl.cc * src/bencode.cc * src/download_helper.cc * src/messageDigest.h * test/ExceptionTest.cc * test/TestUtil.cc
2009-05-18 15:07:15 +00:00
throw DL_ABORT_EX
(StringFormat(MSG_NAME_RESOLUTION_FAILED,
util::itos(getCuid()).c_str(),
hostname.c_str(),
resolver->getError().c_str()).str());
default:
return false;
}
}
void DHTEntryPointNameResolveCommand::setNameResolverCheck
(const SharedHandle<AsyncNameResolver>& resolver)
{
e_->addNameResolverCheck(resolver, this);
}
void DHTEntryPointNameResolveCommand::disableNameResolverCheck
(const SharedHandle<AsyncNameResolver>& resolver)
{
e_->deleteNameResolverCheck(resolver, this);
}
#endif // ENABLE_ASYNC_DNS
void DHTEntryPointNameResolveCommand::setBootstrapEnabled(bool f)
{
bootstrapEnabled_ = f;
}
void DHTEntryPointNameResolveCommand::setTaskQueue
(const SharedHandle<DHTTaskQueue>& taskQueue)
{
taskQueue_ = taskQueue;
}
void DHTEntryPointNameResolveCommand::setTaskFactory
(const SharedHandle<DHTTaskFactory>& taskFactory)
{
taskFactory_ = taskFactory;
}
void DHTEntryPointNameResolveCommand::setRoutingTable
(const SharedHandle<DHTRoutingTable>& routingTable)
{
routingTable_ = routingTable;
}
void DHTEntryPointNameResolveCommand::setLocalNode
(const SharedHandle<DHTNode>& localNode)
{
localNode_ = localNode;
}
} // namespace aria2