/* */ #include "InitiateConnectionCommand.h" #include "Request.h" #include "DownloadEngine.h" #include "Option.h" #include "Logger.h" #include "message.h" #include "prefs.h" #include "NameResolver.h" #include "DNSCache.h" #include "SocketCore.h" #include "FileEntry.h" #include "RequestGroup.h" #include "DownloadContext.h" #include "Segment.h" #include "a2functional.h" namespace aria2 { InitiateConnectionCommand::InitiateConnectionCommand (int cuid, const RequestHandle& req, const SharedHandle& fileEntry, RequestGroup* requestGroup, DownloadEngine* e): AbstractCommand(cuid, req, fileEntry, requestGroup, e) { setTimeout(getOption()->getAsInt(PREF_DNS_TIMEOUT)); // give a chance to be executed in the next loop in DownloadEngine setStatus(Command::STATUS_ONESHOT_REALTIME); disableReadCheckSocket(); disableWriteCheckSocket(); } InitiateConnectionCommand::~InitiateConnectionCommand() {} bool InitiateConnectionCommand::executeInternal() { std::string hostname; uint16_t port; SharedHandle proxyRequest = createProxyRequest(); if(proxyRequest.isNull()) { hostname = req->getHost(); port = req->getPort(); } else { hostname = proxyRequest->getHost(); port = proxyRequest->getPort(); } std::deque addrs; std::string ipaddr = e->findCachedIPAddress(hostname, port); if(ipaddr.empty()) { #ifdef ENABLE_ASYNC_DNS if(getOption()->getAsBool(PREF_ASYNC_DNS)) { if(!isAsyncNameResolverInitialized()) { initAsyncNameResolver(hostname); } if(asyncResolveHostname()) { addrs = getResolvedAddresses(); } else { e->commands.push_back(this); return false; } } else #endif // ENABLE_ASYNC_DNS { NameResolver res; res.setSocktype(SOCK_STREAM); res.resolve(addrs, hostname); } logger->info(MSG_NAME_RESOLUTION_COMPLETE, cuid, hostname.c_str(), strjoin(addrs.begin(), addrs.end(), ",").c_str()); for(std::deque::const_iterator i = addrs.begin(); i != addrs.end(); ++i) { e->cacheIPAddress(hostname, *i, port); } ipaddr = e->findCachedIPAddress(hostname, port); } else { logger->info(MSG_DNS_CACHE_HIT, cuid, hostname.c_str(), ipaddr.c_str()); addrs.push_back(ipaddr); } Command* command = createNextCommand(hostname, ipaddr, port, addrs, proxyRequest); e->commands.push_back(command); return true; } } // namespace aria2