/* */ #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" namespace aria2 { InitiateConnectionCommand::InitiateConnectionCommand (int cuid, const RequestHandle& req, RequestGroup* requestGroup, DownloadEngine* e): AbstractCommand(cuid, req, requestGroup, e) { setTimeout(e->option->getAsInt(PREF_DNS_TIMEOUT)); setStatus(Command::STATUS_ONESHOT_REALTIME); disableReadCheckSocket(); disableWriteCheckSocket(); } InitiateConnectionCommand::~InitiateConnectionCommand() {} bool InitiateConnectionCommand::executeInternal() { std::string hostname; SharedHandle proxyRequest = createProxyRequest(); if(proxyRequest.isNull()) { hostname = req->getHost(); } else { hostname = proxyRequest->getHost(); } std::deque addrs; std::string ipaddr = e->findCachedIPAddress(hostname); if(ipaddr.empty()) { #ifdef ENABLE_ASYNC_DNS if(e->option->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(), addrs.front().c_str()); e->cacheIPAddress(hostname, addrs.front()); } else { logger->info(MSG_DNS_CACHE_HIT, cuid, hostname.c_str(), ipaddr.c_str()); addrs.push_back(ipaddr); } Command* command = createNextCommand(addrs, proxyRequest); e->commands.push_back(command); return true; } } // namespace aria2