2006-02-17 13:35:04 +00:00
|
|
|
/* <!-- copyright */
|
|
|
|
/*
|
2006-09-21 15:31:24 +00:00
|
|
|
* aria2 - The high speed download utility
|
2006-02-17 13:35:04 +00:00
|
|
|
*
|
|
|
|
* 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
|
2010-01-05 16:01:46 +00:00
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
2006-09-21 15:31:24 +00:00
|
|
|
*
|
|
|
|
* 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.
|
2006-02-17 13:35:04 +00:00
|
|
|
*/
|
|
|
|
/* copyright --> */
|
|
|
|
#include "DownloadEngine.h"
|
2008-11-03 06:49:02 +00:00
|
|
|
|
2013-03-04 13:32:51 +00:00
|
|
|
#include <signal.h>
|
2008-11-03 06:49:02 +00:00
|
|
|
|
|
|
|
#include <cstring>
|
|
|
|
#include <cerrno>
|
|
|
|
#include <algorithm>
|
|
|
|
#include <numeric>
|
2013-06-23 12:55:52 +00:00
|
|
|
#include <iterator>
|
2008-11-03 06:49:02 +00:00
|
|
|
|
2007-10-11 16:58:24 +00:00
|
|
|
#include "StatCalc.h"
|
|
|
|
#include "RequestGroup.h"
|
|
|
|
#include "RequestGroupMan.h"
|
2008-02-08 15:53:45 +00:00
|
|
|
#include "DownloadResult.h"
|
|
|
|
#include "StatCalc.h"
|
2006-04-17 16:17:20 +00:00
|
|
|
#include "LogFactory.h"
|
2008-02-08 15:53:45 +00:00
|
|
|
#include "Logger.h"
|
2012-09-28 14:27:46 +00:00
|
|
|
#include "SocketCore.h"
|
2009-10-22 15:35:33 +00:00
|
|
|
#include "util.h"
|
2008-05-11 09:36:39 +00:00
|
|
|
#include "a2functional.h"
|
2008-05-31 10:31:07 +00:00
|
|
|
#include "DlAbortEx.h"
|
2008-08-04 17:06:47 +00:00
|
|
|
#include "ServerStatMan.h"
|
2008-09-01 13:46:03 +00:00
|
|
|
#include "CookieStorage.h"
|
2008-09-24 17:01:57 +00:00
|
|
|
#include "A2STR.h"
|
2008-11-03 10:06:25 +00:00
|
|
|
#include "AuthConfigFactory.h"
|
|
|
|
#include "AuthConfig.h"
|
2008-11-05 12:30:22 +00:00
|
|
|
#include "Request.h"
|
2009-01-15 15:23:16 +00:00
|
|
|
#include "EventPoll.h"
|
|
|
|
#include "Command.h"
|
2009-01-24 09:00:36 +00:00
|
|
|
#include "FileAllocationEntry.h"
|
2009-01-24 10:40:42 +00:00
|
|
|
#include "CheckIntegrityEntry.h"
|
2008-11-03 06:49:02 +00:00
|
|
|
#include "BtProgressInfoFile.h"
|
2009-06-28 10:37:15 +00:00
|
|
|
#include "DownloadContext.h"
|
2010-11-20 08:21:36 +00:00
|
|
|
#include "fmt.h"
|
2011-08-09 16:17:28 +00:00
|
|
|
#include "wallclock.h"
|
2009-06-28 10:37:15 +00:00
|
|
|
#ifdef ENABLE_BITTORRENT
|
2015-12-27 09:39:47 +00:00
|
|
|
#include "BtRegistry.h"
|
2009-06-28 10:37:15 +00:00
|
|
|
#endif // ENABLE_BITTORRENT
|
2013-05-11 02:04:47 +00:00
|
|
|
#ifdef ENABLE_WEBSOCKET
|
2015-12-27 09:39:47 +00:00
|
|
|
#include "WebSocketSessionMan.h"
|
2013-05-11 02:04:47 +00:00
|
|
|
#endif // ENABLE_WEBSOCKET
|
2014-04-19 17:10:06 +00:00
|
|
|
#include "Option.h"
|
|
|
|
#include "util_security.h"
|
|
|
|
|
2008-02-08 15:53:45 +00:00
|
|
|
namespace aria2 {
|
2006-02-17 13:35:04 +00:00
|
|
|
|
2010-03-06 08:29:53 +00:00
|
|
|
namespace global {
|
|
|
|
|
2007-12-07 13:33:59 +00:00
|
|
|
// 0 ... running
|
|
|
|
// 1 ... stop signal detected
|
|
|
|
// 2 ... stop signal processed by DownloadEngine
|
|
|
|
// 3 ... 2nd stop signal(force shutdown) detected
|
|
|
|
// 4 ... 2nd stop signal processed by DownloadEngine
|
2013-08-17 21:04:15 +00:00
|
|
|
// 5 ... main loop exited
|
2007-12-07 13:33:59 +00:00
|
|
|
volatile sig_atomic_t globalHaltRequested = 0;
|
2007-10-11 16:58:24 +00:00
|
|
|
|
2010-03-06 08:29:53 +00:00
|
|
|
} // namespace global
|
|
|
|
|
2015-06-07 17:03:31 +00:00
|
|
|
namespace {
|
2015-06-21 06:17:00 +00:00
|
|
|
constexpr auto DEFAULT_REFRESH_INTERVAL = 1_s;
|
2015-06-07 17:03:31 +00:00
|
|
|
} // namespace
|
|
|
|
|
2013-07-06 10:39:16 +00:00
|
|
|
DownloadEngine::DownloadEngine(std::unique_ptr<EventPoll> eventPoll)
|
2015-12-27 09:39:47 +00:00
|
|
|
: eventPoll_(std::move(eventPoll)),
|
|
|
|
haltRequested_(0),
|
|
|
|
noWait_(true),
|
|
|
|
refreshInterval_(DEFAULT_REFRESH_INTERVAL),
|
|
|
|
lastRefresh_(Timer::zero()),
|
|
|
|
cookieStorage_(make_unique<CookieStorage>()),
|
2009-07-25 14:07:46 +00:00
|
|
|
#ifdef ENABLE_BITTORRENT
|
2015-12-27 09:39:47 +00:00
|
|
|
btRegistry_(make_unique<BtRegistry>()),
|
2009-07-25 14:07:46 +00:00
|
|
|
#endif // ENABLE_BITTORRENT
|
2011-02-17 16:04:11 +00:00
|
|
|
#ifdef HAVE_ARES_ADDR_NODE
|
2015-12-27 09:39:47 +00:00
|
|
|
asyncDNSServers_(nullptr),
|
2011-02-17 16:04:11 +00:00
|
|
|
#endif // HAVE_ARES_ADDR_NODE
|
2015-12-27 09:39:47 +00:00
|
|
|
dnsCache_(make_unique<DNSCache>()),
|
|
|
|
option_(nullptr)
|
2010-01-17 11:55:22 +00:00
|
|
|
{
|
|
|
|
unsigned char sessionId[20];
|
|
|
|
util::generateRandomKey(sessionId);
|
2015-12-27 09:39:47 +00:00
|
|
|
sessionId_.assign(&sessionId[0], &sessionId[sizeof(sessionId)]);
|
2010-01-17 11:55:22 +00:00
|
|
|
}
|
2007-10-11 16:58:24 +00:00
|
|
|
|
2013-06-23 12:55:52 +00:00
|
|
|
DownloadEngine::~DownloadEngine()
|
|
|
|
{
|
2011-02-17 16:04:11 +00:00
|
|
|
#ifdef HAVE_ARES_ADDR_NODE
|
2013-08-20 16:57:17 +00:00
|
|
|
setAsyncDNSServers(nullptr);
|
2011-02-17 16:04:11 +00:00
|
|
|
#endif // HAVE_ARES_ADDR_NODE
|
2006-05-09 15:54:14 +00:00
|
|
|
}
|
|
|
|
|
2010-10-30 16:02:15 +00:00
|
|
|
namespace {
|
2013-06-23 12:55:52 +00:00
|
|
|
void executeCommand(std::deque<std::unique_ptr<Command>>& commands,
|
2010-10-30 16:02:15 +00:00
|
|
|
Command::STATUS statusFilter)
|
2007-03-16 14:21:29 +00:00
|
|
|
{
|
2008-03-09 12:24:01 +00:00
|
|
|
size_t max = commands.size();
|
2015-12-27 09:39:47 +00:00
|
|
|
for (size_t i = 0; i < max; ++i) {
|
2013-08-19 14:40:28 +00:00
|
|
|
auto com = std::move(commands.front());
|
2007-03-16 14:21:29 +00:00
|
|
|
commands.pop_front();
|
2013-08-21 04:06:10 +00:00
|
|
|
if (!com->statusMatch(statusFilter)) {
|
2008-05-31 10:31:07 +00:00
|
|
|
com->clearIOEvents();
|
2013-06-23 12:55:52 +00:00
|
|
|
commands.push_back(std::move(com));
|
2013-08-21 04:06:10 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
com->transitStatus();
|
|
|
|
if (com->execute()) {
|
|
|
|
com.reset();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
com->clearIOEvents();
|
|
|
|
com.release();
|
2008-05-31 10:31:07 +00:00
|
|
|
}
|
2007-03-16 14:21:29 +00:00
|
|
|
}
|
|
|
|
}
|
2010-10-30 16:02:15 +00:00
|
|
|
} // namespace
|
2007-03-16 14:21:29 +00:00
|
|
|
|
2013-08-19 14:54:54 +00:00
|
|
|
namespace {
|
2013-08-21 04:06:10 +00:00
|
|
|
class GlobalHaltRequestedFinalizer {
|
2013-08-19 14:54:54 +00:00
|
|
|
public:
|
2013-08-21 04:06:10 +00:00
|
|
|
GlobalHaltRequestedFinalizer() {}
|
2015-12-27 09:39:47 +00:00
|
|
|
~GlobalHaltRequestedFinalizer() { global::globalHaltRequested = 5; }
|
2013-08-19 14:54:54 +00:00
|
|
|
};
|
|
|
|
} // namespace
|
|
|
|
|
2013-04-25 12:46:31 +00:00
|
|
|
int DownloadEngine::run(bool oneshot)
|
2009-01-15 15:23:16 +00:00
|
|
|
{
|
2013-08-21 04:06:10 +00:00
|
|
|
GlobalHaltRequestedFinalizer ghrf;
|
2015-12-27 09:39:47 +00:00
|
|
|
while (!commands_.empty() || !routineCommands_.empty()) {
|
|
|
|
if (!commands_.empty()) {
|
2013-04-25 12:46:31 +00:00
|
|
|
waitData();
|
|
|
|
}
|
|
|
|
noWait_ = false;
|
2011-08-09 16:17:28 +00:00
|
|
|
global::wallclock().reset();
|
2011-02-07 15:16:29 +00:00
|
|
|
calculateStatistics();
|
2015-12-27 09:39:47 +00:00
|
|
|
if (lastRefresh_.difference(global::wallclock()) + A2_DELTA_MILLIS >=
|
|
|
|
refreshInterval_) {
|
2010-06-21 13:51:56 +00:00
|
|
|
refreshInterval_ = DEFAULT_REFRESH_INTERVAL;
|
2013-04-26 14:55:51 +00:00
|
|
|
lastRefresh_ = global::wallclock();
|
2010-06-21 13:51:56 +00:00
|
|
|
executeCommand(commands_, Command::STATUS_ALL);
|
2015-12-27 09:39:47 +00:00
|
|
|
}
|
|
|
|
else {
|
2010-06-21 13:51:56 +00:00
|
|
|
executeCommand(commands_, Command::STATUS_ACTIVE);
|
2006-02-17 13:35:04 +00:00
|
|
|
}
|
2010-06-21 13:51:56 +00:00
|
|
|
executeCommand(routineCommands_, Command::STATUS_ALL);
|
2006-03-24 11:59:18 +00:00
|
|
|
afterEachIteration();
|
2015-12-27 09:39:47 +00:00
|
|
|
if (!noWait_ && oneshot) {
|
2013-04-25 12:46:31 +00:00
|
|
|
return 1;
|
2006-02-21 12:27:17 +00:00
|
|
|
}
|
2006-02-17 13:35:04 +00:00
|
|
|
}
|
2013-04-26 16:23:54 +00:00
|
|
|
onEndOfRun();
|
2013-04-25 12:46:31 +00:00
|
|
|
return 0;
|
2006-03-02 02:54:49 +00:00
|
|
|
}
|
|
|
|
|
2008-05-31 10:31:07 +00:00
|
|
|
void DownloadEngine::waitData()
|
|
|
|
{
|
2006-08-07 16:05:00 +00:00
|
|
|
struct timeval tv;
|
2015-12-27 09:39:47 +00:00
|
|
|
if (noWait_) {
|
2009-01-15 15:23:16 +00:00
|
|
|
tv.tv_sec = tv.tv_usec = 0;
|
2015-12-27 09:39:47 +00:00
|
|
|
}
|
|
|
|
else {
|
2015-06-07 17:03:31 +00:00
|
|
|
auto t =
|
|
|
|
std::chrono::duration_cast<std::chrono::microseconds>(refreshInterval_);
|
|
|
|
tv.tv_sec = t.count() / 1000000;
|
|
|
|
tv.tv_usec = t.count() % 1000000;
|
2008-05-31 10:31:07 +00:00
|
|
|
}
|
2010-06-21 13:51:56 +00:00
|
|
|
eventPoll_->poll(tv);
|
2008-05-31 10:31:07 +00:00
|
|
|
}
|
|
|
|
|
2015-12-27 09:39:47 +00:00
|
|
|
bool DownloadEngine::addSocketForReadCheck(
|
|
|
|
const std::shared_ptr<SocketCore>& socket, Command* command)
|
2008-05-31 10:31:07 +00:00
|
|
|
{
|
2010-06-21 13:51:56 +00:00
|
|
|
return eventPoll_->addEvents(socket->getSockfd(), command,
|
2010-01-05 16:01:46 +00:00
|
|
|
EventPoll::EVENT_READ);
|
2006-02-17 13:35:04 +00:00
|
|
|
}
|
|
|
|
|
2015-12-27 09:39:47 +00:00
|
|
|
bool DownloadEngine::deleteSocketForReadCheck(
|
|
|
|
const std::shared_ptr<SocketCore>& socket, Command* command)
|
2008-05-31 10:31:07 +00:00
|
|
|
{
|
2010-06-21 13:51:56 +00:00
|
|
|
return eventPoll_->deleteEvents(socket->getSockfd(), command,
|
2010-01-05 16:01:46 +00:00
|
|
|
EventPoll::EVENT_READ);
|
2006-02-17 13:35:04 +00:00
|
|
|
}
|
|
|
|
|
2015-12-27 09:39:47 +00:00
|
|
|
bool DownloadEngine::addSocketForWriteCheck(
|
|
|
|
const std::shared_ptr<SocketCore>& socket, Command* command)
|
2008-05-31 10:31:07 +00:00
|
|
|
{
|
2010-06-21 13:51:56 +00:00
|
|
|
return eventPoll_->addEvents(socket->getSockfd(), command,
|
2010-01-05 16:01:46 +00:00
|
|
|
EventPoll::EVENT_WRITE);
|
2006-02-17 13:35:04 +00:00
|
|
|
}
|
|
|
|
|
2015-12-27 09:39:47 +00:00
|
|
|
bool DownloadEngine::deleteSocketForWriteCheck(
|
|
|
|
const std::shared_ptr<SocketCore>& socket, Command* command)
|
2008-05-31 10:31:07 +00:00
|
|
|
{
|
2010-06-21 13:51:56 +00:00
|
|
|
return eventPoll_->deleteEvents(socket->getSockfd(), command,
|
2010-01-05 16:01:46 +00:00
|
|
|
EventPoll::EVENT_WRITE);
|
2006-02-17 13:35:04 +00:00
|
|
|
}
|
2006-08-11 12:29:55 +00:00
|
|
|
|
2007-10-11 16:58:24 +00:00
|
|
|
void DownloadEngine::calculateStatistics()
|
|
|
|
{
|
2015-12-27 09:39:47 +00:00
|
|
|
if (statCalc_) {
|
2010-06-21 13:51:56 +00:00
|
|
|
statCalc_->calculateStat(this);
|
2007-10-11 16:58:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void DownloadEngine::onEndOfRun()
|
|
|
|
{
|
2010-12-04 09:24:38 +00:00
|
|
|
requestGroupMan_->removeStoppedGroup(this);
|
2010-06-21 13:51:56 +00:00
|
|
|
requestGroupMan_->closeFile();
|
|
|
|
requestGroupMan_->save();
|
2007-10-11 16:58:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void DownloadEngine::afterEachIteration()
|
|
|
|
{
|
2015-12-27 09:39:47 +00:00
|
|
|
if (global::globalHaltRequested == 1) {
|
2010-11-20 08:21:36 +00:00
|
|
|
A2_LOG_NOTICE(_("Shutdown sequence commencing..."
|
|
|
|
" Press Ctrl-C again for emergency shutdown."));
|
2008-02-11 05:07:08 +00:00
|
|
|
requestHalt();
|
2010-03-06 08:29:53 +00:00
|
|
|
global::globalHaltRequested = 2;
|
2008-11-11 16:18:23 +00:00
|
|
|
setNoWait(true);
|
2015-06-07 17:03:31 +00:00
|
|
|
setRefreshInterval(std::chrono::milliseconds(0));
|
2013-08-21 04:06:10 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-12-27 09:39:47 +00:00
|
|
|
if (global::globalHaltRequested == 3) {
|
2010-11-20 08:21:36 +00:00
|
|
|
A2_LOG_NOTICE(_("Emergency shutdown sequence commencing..."));
|
2010-06-03 11:49:48 +00:00
|
|
|
requestForceHalt();
|
2010-03-06 08:29:53 +00:00
|
|
|
global::globalHaltRequested = 4;
|
2008-11-11 16:18:23 +00:00
|
|
|
setNoWait(true);
|
2015-06-07 17:03:31 +00:00
|
|
|
setRefreshInterval(std::chrono::milliseconds(0));
|
2013-08-21 04:06:10 +00:00
|
|
|
return;
|
2007-10-11 16:58:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-02-11 05:07:08 +00:00
|
|
|
void DownloadEngine::requestHalt()
|
|
|
|
{
|
2013-02-24 15:56:49 +00:00
|
|
|
haltRequested_ = std::max(haltRequested_, 1);
|
2010-06-21 13:51:56 +00:00
|
|
|
requestGroupMan_->halt();
|
2008-02-11 05:07:08 +00:00
|
|
|
}
|
|
|
|
|
2010-04-02 14:23:59 +00:00
|
|
|
void DownloadEngine::requestForceHalt()
|
|
|
|
{
|
2013-02-24 15:56:49 +00:00
|
|
|
haltRequested_ = std::max(haltRequested_, 2);
|
2010-06-21 13:51:56 +00:00
|
|
|
requestGroupMan_->forceHalt();
|
2010-04-02 14:23:59 +00:00
|
|
|
}
|
|
|
|
|
2013-07-06 10:26:30 +00:00
|
|
|
void DownloadEngine::setStatCalc(std::unique_ptr<StatCalc> statCalc)
|
2007-10-11 16:58:24 +00:00
|
|
|
{
|
2013-07-06 10:26:30 +00:00
|
|
|
statCalc_ = std::move(statCalc);
|
2007-10-11 16:58:24 +00:00
|
|
|
}
|
|
|
|
|
2006-08-21 13:18:51 +00:00
|
|
|
#ifdef ENABLE_ASYNC_DNS
|
2015-12-27 09:39:47 +00:00
|
|
|
bool DownloadEngine::addNameResolverCheck(
|
|
|
|
const std::shared_ptr<AsyncNameResolver>& resolver, Command* command)
|
2008-05-08 11:18:36 +00:00
|
|
|
{
|
2010-06-21 13:51:56 +00:00
|
|
|
return eventPoll_->addNameResolver(resolver, command);
|
2006-08-11 12:29:55 +00:00
|
|
|
}
|
|
|
|
|
2015-12-27 09:39:47 +00:00
|
|
|
bool DownloadEngine::deleteNameResolverCheck(
|
|
|
|
const std::shared_ptr<AsyncNameResolver>& resolver, Command* command)
|
2008-05-08 11:18:36 +00:00
|
|
|
{
|
2010-06-21 13:51:56 +00:00
|
|
|
return eventPoll_->deleteNameResolver(resolver, command);
|
2006-08-11 12:29:55 +00:00
|
|
|
}
|
2006-08-21 13:18:51 +00:00
|
|
|
#endif // ENABLE_ASYNC_DNS
|
2008-02-08 15:53:45 +00:00
|
|
|
|
2015-12-27 09:39:47 +00:00
|
|
|
void DownloadEngine::setNoWait(bool b) { noWait_ = b; }
|
2008-04-20 05:42:15 +00:00
|
|
|
|
2013-06-23 12:55:52 +00:00
|
|
|
void DownloadEngine::addRoutineCommand(std::unique_ptr<Command> command)
|
2008-04-20 05:42:15 +00:00
|
|
|
{
|
2013-06-23 12:55:52 +00:00
|
|
|
routineCommands_.push_back(std::move(command));
|
2008-04-20 05:42:15 +00:00
|
|
|
}
|
|
|
|
|
2010-05-21 13:54:50 +00:00
|
|
|
void DownloadEngine::poolSocket(const std::string& key,
|
2010-01-05 16:01:46 +00:00
|
|
|
const SocketPoolEntry& entry)
|
2008-04-22 08:52:47 +00:00
|
|
|
{
|
2010-11-20 08:21:36 +00:00
|
|
|
A2_LOG_INFO(fmt("Pool socket for %s", key.c_str()));
|
2010-05-21 13:54:50 +00:00
|
|
|
std::multimap<std::string, SocketPoolEntry>::value_type p(key, entry);
|
2010-06-21 13:51:56 +00:00
|
|
|
socketPool_.insert(p);
|
2015-11-11 13:31:00 +00:00
|
|
|
}
|
2008-09-24 17:01:57 +00:00
|
|
|
|
2015-11-11 13:31:00 +00:00
|
|
|
void DownloadEngine::evictSocketPool()
|
|
|
|
{
|
|
|
|
if (socketPool_.empty()) {
|
2013-08-21 04:06:10 +00:00
|
|
|
return;
|
|
|
|
}
|
2015-11-11 13:31:00 +00:00
|
|
|
|
2013-08-21 04:06:10 +00:00
|
|
|
std::multimap<std::string, SocketPoolEntry> newPool;
|
|
|
|
A2_LOG_DEBUG("Scaning SocketPool and erasing timed out entry.");
|
2015-11-11 13:31:00 +00:00
|
|
|
for (auto& elem : socketPool_) {
|
|
|
|
if (!elem.second.isTimeout()) {
|
2013-08-21 04:06:10 +00:00
|
|
|
newPool.insert(elem);
|
2008-09-24 17:01:57 +00:00
|
|
|
}
|
|
|
|
}
|
2015-12-27 09:39:47 +00:00
|
|
|
A2_LOG_DEBUG(
|
|
|
|
fmt("%lu entries removed.",
|
|
|
|
static_cast<unsigned long>(socketPool_.size() - newPool.size())));
|
2015-11-11 13:31:00 +00:00
|
|
|
socketPool_ = std::move(newPool);
|
2008-09-24 17:01:57 +00:00
|
|
|
}
|
|
|
|
|
2010-10-30 16:02:15 +00:00
|
|
|
namespace {
|
2015-12-27 09:39:47 +00:00
|
|
|
std::string createSockPoolKey(const std::string& host, uint16_t port,
|
|
|
|
const std::string& username,
|
|
|
|
const std::string& proxyhost, uint16_t proxyport)
|
2010-05-21 12:22:04 +00:00
|
|
|
{
|
|
|
|
std::string key;
|
2015-12-27 09:39:47 +00:00
|
|
|
if (!username.empty()) {
|
2010-05-21 13:54:50 +00:00
|
|
|
key += util::percentEncode(username);
|
2011-11-12 12:27:24 +00:00
|
|
|
key += "@";
|
2010-05-21 13:54:50 +00:00
|
|
|
}
|
2011-11-12 12:31:14 +00:00
|
|
|
key += fmt("%s(%u)", host.c_str(), port);
|
2015-12-27 09:39:47 +00:00
|
|
|
if (!proxyhost.empty()) {
|
2011-11-12 12:31:14 +00:00
|
|
|
key += fmt("/%s(%u)", proxyhost.c_str(), proxyport);
|
2010-05-21 13:54:50 +00:00
|
|
|
}
|
2010-05-21 12:22:04 +00:00
|
|
|
return key;
|
|
|
|
}
|
2010-10-30 16:02:15 +00:00
|
|
|
} // namespace
|
2010-05-21 12:22:04 +00:00
|
|
|
|
2015-12-27 09:39:47 +00:00
|
|
|
void DownloadEngine::poolSocket(const std::string& ipaddr, uint16_t port,
|
|
|
|
const std::string& username,
|
|
|
|
const std::string& proxyhost,
|
|
|
|
uint16_t proxyport,
|
|
|
|
const std::shared_ptr<SocketCore>& sock,
|
|
|
|
const std::string& options,
|
|
|
|
std::chrono::seconds timeout)
|
2008-09-24 17:01:57 +00:00
|
|
|
{
|
2015-06-07 17:03:31 +00:00
|
|
|
SocketPoolEntry e(sock, options, std::move(timeout));
|
2015-12-27 09:39:47 +00:00
|
|
|
poolSocket(createSockPoolKey(ipaddr, port, username, proxyhost, proxyport),
|
|
|
|
e);
|
2008-09-24 17:01:57 +00:00
|
|
|
}
|
|
|
|
|
2015-12-27 09:39:47 +00:00
|
|
|
void DownloadEngine::poolSocket(const std::string& ipaddr, uint16_t port,
|
|
|
|
const std::string& proxyhost,
|
|
|
|
uint16_t proxyport,
|
|
|
|
const std::shared_ptr<SocketCore>& sock,
|
|
|
|
std::chrono::seconds timeout)
|
2008-09-24 17:01:57 +00:00
|
|
|
{
|
2015-06-07 17:03:31 +00:00
|
|
|
SocketPoolEntry e(sock, std::move(timeout));
|
2015-12-27 09:39:47 +00:00
|
|
|
poolSocket(createSockPoolKey(ipaddr, port, A2STR::NIL, proxyhost, proxyport),
|
|
|
|
e);
|
2008-09-24 17:01:57 +00:00
|
|
|
}
|
|
|
|
|
2012-09-02 08:35:50 +00:00
|
|
|
namespace {
|
|
|
|
bool getPeerInfo(std::pair<std::string, uint16_t>& res,
|
2013-06-21 16:10:38 +00:00
|
|
|
const std::shared_ptr<SocketCore>& socket)
|
2012-09-02 08:35:50 +00:00
|
|
|
{
|
|
|
|
try {
|
|
|
|
socket->getPeerInfo(res);
|
|
|
|
return true;
|
2015-12-27 09:39:47 +00:00
|
|
|
}
|
|
|
|
catch (RecoverableException& e) {
|
2012-09-02 08:35:50 +00:00
|
|
|
// socket->getPeerInfo() can fail if the socket has been
|
|
|
|
// disconnected.
|
|
|
|
A2_LOG_INFO_EX("Getting peer info failed. Pooling socket canceled.", e);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
2013-06-21 16:10:38 +00:00
|
|
|
void DownloadEngine::poolSocket(const std::shared_ptr<Request>& request,
|
|
|
|
const std::shared_ptr<Request>& proxyRequest,
|
|
|
|
const std::shared_ptr<SocketCore>& socket,
|
2015-06-07 17:03:31 +00:00
|
|
|
std::chrono::seconds timeout)
|
2008-11-05 12:30:22 +00:00
|
|
|
{
|
2015-12-27 09:39:47 +00:00
|
|
|
if (proxyRequest) {
|
2010-05-21 13:54:50 +00:00
|
|
|
// If proxy is defined, then pool socket with its hostname.
|
2015-12-27 09:39:47 +00:00
|
|
|
poolSocket(request->getHost(), request->getPort(), proxyRequest->getHost(),
|
|
|
|
proxyRequest->getPort(), socket, std::move(timeout));
|
2013-08-21 04:06:10 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::pair<std::string, uint16_t> peerInfo;
|
2015-12-27 09:39:47 +00:00
|
|
|
if (getPeerInfo(peerInfo, socket)) {
|
|
|
|
poolSocket(peerInfo.first, peerInfo.second, A2STR::NIL, 0, socket,
|
|
|
|
std::move(timeout));
|
2008-11-05 12:30:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-27 09:39:47 +00:00
|
|
|
void DownloadEngine::poolSocket(const std::shared_ptr<Request>& request,
|
|
|
|
const std::string& username,
|
|
|
|
const std::shared_ptr<Request>& proxyRequest,
|
|
|
|
const std::shared_ptr<SocketCore>& socket,
|
|
|
|
const std::string& options,
|
|
|
|
std::chrono::seconds timeout)
|
2008-11-05 12:30:22 +00:00
|
|
|
{
|
2015-12-27 09:39:47 +00:00
|
|
|
if (proxyRequest) {
|
2010-05-21 13:54:50 +00:00
|
|
|
// If proxy is defined, then pool socket with its hostname.
|
|
|
|
poolSocket(request->getHost(), request->getPort(), username,
|
2015-12-27 09:39:47 +00:00
|
|
|
proxyRequest->getHost(), proxyRequest->getPort(), socket,
|
|
|
|
options, std::move(timeout));
|
2013-08-21 04:06:10 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::pair<std::string, uint16_t> peerInfo;
|
2015-12-27 09:39:47 +00:00
|
|
|
if (getPeerInfo(peerInfo, socket)) {
|
|
|
|
poolSocket(peerInfo.first, peerInfo.second, username, A2STR::NIL, 0, socket,
|
|
|
|
options, std::move(timeout));
|
2008-11-05 12:30:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-09-24 17:01:57 +00:00
|
|
|
std::multimap<std::string, DownloadEngine::SocketPoolEntry>::iterator
|
2010-05-21 13:54:50 +00:00
|
|
|
DownloadEngine::findSocketPoolEntry(const std::string& key)
|
2008-09-24 17:01:57 +00:00
|
|
|
{
|
|
|
|
std::pair<std::multimap<std::string, SocketPoolEntry>::iterator,
|
2015-12-27 09:39:47 +00:00
|
|
|
std::multimap<std::string, SocketPoolEntry>::iterator> range =
|
|
|
|
socketPool_.equal_range(key);
|
|
|
|
for (auto i = range.first, eoi = range.second; i != eoi; ++i) {
|
2008-09-24 17:01:57 +00:00
|
|
|
const SocketPoolEntry& e = (*i).second;
|
2010-10-19 14:02:47 +00:00
|
|
|
// We assume that if socket is readable it means peer shutdowns
|
|
|
|
// connection and the socket will receive EOF. So skip it.
|
2015-12-27 09:39:47 +00:00
|
|
|
if (!e.isTimeout() && !e.getSocket()->isReadable(0)) {
|
2010-11-20 08:21:36 +00:00
|
|
|
A2_LOG_INFO(fmt("Found socket for %s", key.c_str()));
|
2008-09-24 17:01:57 +00:00
|
|
|
return i;
|
|
|
|
}
|
|
|
|
}
|
2010-06-21 13:51:56 +00:00
|
|
|
return socketPool_.end();
|
2008-04-22 08:52:47 +00:00
|
|
|
}
|
|
|
|
|
2013-06-21 16:10:38 +00:00
|
|
|
std::shared_ptr<SocketCore>
|
2015-12-27 09:39:47 +00:00
|
|
|
DownloadEngine::popPooledSocket(const std::string& ipaddr, uint16_t port,
|
|
|
|
const std::string& proxyhost,
|
|
|
|
uint16_t proxyport)
|
2008-04-22 08:52:47 +00:00
|
|
|
{
|
2013-06-21 16:10:38 +00:00
|
|
|
std::shared_ptr<SocketCore> s;
|
2015-12-27 09:39:47 +00:00
|
|
|
auto i = findSocketPoolEntry(
|
|
|
|
createSockPoolKey(ipaddr, port, A2STR::NIL, proxyhost, proxyport));
|
|
|
|
if (i != socketPool_.end()) {
|
2008-09-24 17:01:57 +00:00
|
|
|
s = (*i).second.getSocket();
|
2010-06-21 13:51:56 +00:00
|
|
|
socketPool_.erase(i);
|
2008-09-24 17:01:57 +00:00
|
|
|
}
|
|
|
|
return s;
|
|
|
|
}
|
2008-05-08 13:18:25 +00:00
|
|
|
|
2013-06-21 16:10:38 +00:00
|
|
|
std::shared_ptr<SocketCore>
|
2015-12-27 09:39:47 +00:00
|
|
|
DownloadEngine::popPooledSocket(std::string& options, const std::string& ipaddr,
|
|
|
|
uint16_t port, const std::string& username,
|
|
|
|
const std::string& proxyhost,
|
|
|
|
uint16_t proxyport)
|
2008-09-24 17:01:57 +00:00
|
|
|
{
|
2013-06-21 16:10:38 +00:00
|
|
|
std::shared_ptr<SocketCore> s;
|
2015-12-27 09:39:47 +00:00
|
|
|
auto i = findSocketPoolEntry(
|
|
|
|
createSockPoolKey(ipaddr, port, username, proxyhost, proxyport));
|
|
|
|
if (i != socketPool_.end()) {
|
2008-09-24 17:01:57 +00:00
|
|
|
s = (*i).second.getSocket();
|
|
|
|
options = (*i).second.getOptions();
|
2010-06-21 13:51:56 +00:00
|
|
|
socketPool_.erase(i);
|
2008-09-24 17:01:57 +00:00
|
|
|
}
|
2012-09-02 08:40:11 +00:00
|
|
|
return s;
|
2008-09-24 17:01:57 +00:00
|
|
|
}
|
|
|
|
|
2013-06-21 16:10:38 +00:00
|
|
|
std::shared_ptr<SocketCore>
|
2015-12-27 09:39:47 +00:00
|
|
|
DownloadEngine::popPooledSocket(const std::vector<std::string>& ipaddrs,
|
|
|
|
uint16_t port)
|
2008-09-24 17:01:57 +00:00
|
|
|
{
|
2013-06-21 16:10:38 +00:00
|
|
|
std::shared_ptr<SocketCore> s;
|
2015-12-27 09:39:47 +00:00
|
|
|
for (const auto& ipaddr : ipaddrs) {
|
2013-08-21 03:06:53 +00:00
|
|
|
s = popPooledSocket(ipaddr, port, A2STR::NIL, 0);
|
2015-12-27 09:39:47 +00:00
|
|
|
if (s) {
|
2008-05-08 13:18:25 +00:00
|
|
|
break;
|
|
|
|
}
|
2008-04-22 08:52:47 +00:00
|
|
|
}
|
2008-05-08 13:18:25 +00:00
|
|
|
return s;
|
2008-04-22 08:52:47 +00:00
|
|
|
}
|
|
|
|
|
2013-06-21 16:10:38 +00:00
|
|
|
std::shared_ptr<SocketCore>
|
2015-12-27 09:39:47 +00:00
|
|
|
DownloadEngine::popPooledSocket(std::string& options,
|
|
|
|
const std::vector<std::string>& ipaddrs,
|
|
|
|
uint16_t port, const std::string& username)
|
2008-05-08 11:18:36 +00:00
|
|
|
{
|
2013-06-21 16:10:38 +00:00
|
|
|
std::shared_ptr<SocketCore> s;
|
2015-12-27 09:39:47 +00:00
|
|
|
for (const auto& ipaddr : ipaddrs) {
|
2013-08-21 03:06:53 +00:00
|
|
|
s = popPooledSocket(options, ipaddr, port, username, A2STR::NIL, 0);
|
2015-12-27 09:39:47 +00:00
|
|
|
if (s) {
|
2008-09-24 17:01:57 +00:00
|
|
|
break;
|
2008-05-08 11:18:36 +00:00
|
|
|
}
|
|
|
|
}
|
2008-09-24 17:01:57 +00:00
|
|
|
return s;
|
2008-05-08 11:18:36 +00:00
|
|
|
}
|
|
|
|
|
2015-12-27 09:39:47 +00:00
|
|
|
DownloadEngine::SocketPoolEntry::SocketPoolEntry(
|
|
|
|
const std::shared_ptr<SocketCore>& socket, const std::string& options,
|
|
|
|
std::chrono::seconds timeout)
|
|
|
|
: socket_(socket), options_(options), timeout_(std::move(timeout))
|
|
|
|
{
|
|
|
|
}
|
2008-05-08 13:18:25 +00:00
|
|
|
|
2015-12-27 09:39:47 +00:00
|
|
|
DownloadEngine::SocketPoolEntry::SocketPoolEntry(
|
|
|
|
const std::shared_ptr<SocketCore>& socket, std::chrono::seconds timeout)
|
|
|
|
: socket_(socket), timeout_(std::move(timeout))
|
|
|
|
{
|
|
|
|
}
|
2010-05-21 12:22:04 +00:00
|
|
|
|
2008-05-08 13:18:25 +00:00
|
|
|
DownloadEngine::SocketPoolEntry::~SocketPoolEntry() {}
|
|
|
|
|
|
|
|
bool DownloadEngine::SocketPoolEntry::isTimeout() const
|
|
|
|
{
|
2011-08-09 16:17:28 +00:00
|
|
|
return registeredTime_.difference(global::wallclock()) >= timeout_;
|
2008-05-08 13:18:25 +00:00
|
|
|
}
|
|
|
|
|
2015-12-27 09:39:47 +00:00
|
|
|
cuid_t DownloadEngine::newCUID() { return cuidCounter_.newID(); }
|
2008-11-03 07:49:13 +00:00
|
|
|
|
2015-12-27 09:39:47 +00:00
|
|
|
const std::string&
|
|
|
|
DownloadEngine::findCachedIPAddress(const std::string& hostname,
|
|
|
|
uint16_t port) const
|
2008-11-03 08:18:58 +00:00
|
|
|
{
|
2010-06-21 13:51:56 +00:00
|
|
|
return dnsCache_->find(hostname, port);
|
2008-11-03 08:18:58 +00:00
|
|
|
}
|
|
|
|
|
2015-12-27 09:39:47 +00:00
|
|
|
void DownloadEngine::cacheIPAddress(const std::string& hostname,
|
|
|
|
const std::string& ipaddr, uint16_t port)
|
2008-11-03 08:18:58 +00:00
|
|
|
{
|
2010-06-21 13:51:56 +00:00
|
|
|
dnsCache_->put(hostname, ipaddr, port);
|
2009-07-02 15:18:13 +00:00
|
|
|
}
|
|
|
|
|
2015-12-27 09:39:47 +00:00
|
|
|
void DownloadEngine::markBadIPAddress(const std::string& hostname,
|
|
|
|
const std::string& ipaddr, uint16_t port)
|
2009-07-02 15:18:13 +00:00
|
|
|
{
|
2010-06-21 13:51:56 +00:00
|
|
|
dnsCache_->markBad(hostname, ipaddr, port);
|
2009-07-02 15:18:13 +00:00
|
|
|
}
|
|
|
|
|
2015-12-27 09:39:47 +00:00
|
|
|
void DownloadEngine::removeCachedIPAddress(const std::string& hostname,
|
|
|
|
uint16_t port)
|
2009-07-02 15:18:13 +00:00
|
|
|
{
|
2010-06-21 13:51:56 +00:00
|
|
|
dnsCache_->remove(hostname, port);
|
2008-11-03 08:18:58 +00:00
|
|
|
}
|
|
|
|
|
2015-12-27 09:39:47 +00:00
|
|
|
void DownloadEngine::setAuthConfigFactory(
|
|
|
|
std::unique_ptr<AuthConfigFactory> factory)
|
2008-11-03 10:06:25 +00:00
|
|
|
{
|
2013-06-25 13:06:29 +00:00
|
|
|
authConfigFactory_ = std::move(factory);
|
|
|
|
}
|
|
|
|
|
2013-06-26 14:56:43 +00:00
|
|
|
const std::unique_ptr<AuthConfigFactory>&
|
|
|
|
DownloadEngine::getAuthConfigFactory() const
|
2013-06-25 13:06:29 +00:00
|
|
|
{
|
2013-06-26 14:56:43 +00:00
|
|
|
return authConfigFactory_;
|
2008-11-03 10:06:25 +00:00
|
|
|
}
|
|
|
|
|
2013-06-26 14:56:43 +00:00
|
|
|
const std::unique_ptr<CookieStorage>& DownloadEngine::getCookieStorage() const
|
2013-06-25 14:01:00 +00:00
|
|
|
{
|
2013-06-26 14:56:43 +00:00
|
|
|
return cookieStorage_;
|
2013-06-25 14:01:00 +00:00
|
|
|
}
|
|
|
|
|
2015-06-07 17:03:31 +00:00
|
|
|
void DownloadEngine::setRefreshInterval(std::chrono::milliseconds interval)
|
2008-11-11 16:05:42 +00:00
|
|
|
{
|
2015-06-07 17:03:31 +00:00
|
|
|
refreshInterval_ = std::move(interval);
|
2008-11-11 16:05:42 +00:00
|
|
|
}
|
|
|
|
|
2015-12-27 09:39:47 +00:00
|
|
|
void DownloadEngine::addCommand(std::vector<std::unique_ptr<Command>> commands)
|
2010-11-14 07:17:55 +00:00
|
|
|
{
|
2013-06-23 12:55:52 +00:00
|
|
|
commands_.insert(commands_.end(),
|
|
|
|
std::make_move_iterator(std::begin(commands)),
|
|
|
|
std::make_move_iterator(std::end(commands)));
|
2010-11-14 07:17:55 +00:00
|
|
|
}
|
|
|
|
|
2013-06-23 12:55:52 +00:00
|
|
|
void DownloadEngine::addCommand(std::unique_ptr<Command> command)
|
2010-11-14 07:17:55 +00:00
|
|
|
{
|
2013-06-23 12:55:52 +00:00
|
|
|
commands_.push_back(std::move(command));
|
2010-11-14 07:17:55 +00:00
|
|
|
}
|
|
|
|
|
2013-07-05 15:59:43 +00:00
|
|
|
void DownloadEngine::setRequestGroupMan(std::unique_ptr<RequestGroupMan> rgman)
|
2010-11-14 07:17:55 +00:00
|
|
|
{
|
2013-07-05 15:59:43 +00:00
|
|
|
requestGroupMan_ = std::move(rgman);
|
2010-11-14 07:17:55 +00:00
|
|
|
}
|
|
|
|
|
2015-12-27 09:39:47 +00:00
|
|
|
void DownloadEngine::setFileAllocationMan(
|
|
|
|
std::unique_ptr<FileAllocationMan> faman)
|
2010-11-14 07:17:55 +00:00
|
|
|
{
|
2013-07-05 15:30:30 +00:00
|
|
|
fileAllocationMan_ = std::move(faman);
|
2010-11-14 07:17:55 +00:00
|
|
|
}
|
|
|
|
|
2015-12-27 09:39:47 +00:00
|
|
|
void DownloadEngine::setCheckIntegrityMan(
|
|
|
|
std::unique_ptr<CheckIntegrityMan> ciman)
|
2010-11-14 07:17:55 +00:00
|
|
|
{
|
2013-07-05 15:30:30 +00:00
|
|
|
checkIntegrityMan_ = std::move(ciman);
|
2010-11-14 07:17:55 +00:00
|
|
|
}
|
|
|
|
|
2011-02-17 16:04:11 +00:00
|
|
|
#ifdef HAVE_ARES_ADDR_NODE
|
2011-02-08 14:05:40 +00:00
|
|
|
void DownloadEngine::setAsyncDNSServers(ares_addr_node* asyncDNSServers)
|
|
|
|
{
|
|
|
|
ares_addr_node* node = asyncDNSServers_;
|
2015-12-27 09:39:47 +00:00
|
|
|
while (node) {
|
2011-02-08 14:05:40 +00:00
|
|
|
ares_addr_node* next = node->next;
|
|
|
|
delete node;
|
|
|
|
node = next;
|
|
|
|
}
|
|
|
|
asyncDNSServers_ = asyncDNSServers;
|
|
|
|
}
|
2011-02-17 16:04:11 +00:00
|
|
|
#endif // HAVE_ARES_ADDR_NODE
|
2011-02-08 14:05:40 +00:00
|
|
|
|
2013-05-11 02:04:47 +00:00
|
|
|
#ifdef ENABLE_WEBSOCKET
|
2015-12-27 09:39:47 +00:00
|
|
|
void DownloadEngine::setWebSocketSessionMan(
|
|
|
|
std::unique_ptr<rpc::WebSocketSessionMan> wsman)
|
2013-05-11 02:04:47 +00:00
|
|
|
{
|
2013-07-06 09:33:59 +00:00
|
|
|
webSocketSessionMan_ = std::move(wsman);
|
2013-05-11 02:04:47 +00:00
|
|
|
}
|
|
|
|
#endif // ENABLE_WEBSOCKET
|
|
|
|
|
2014-04-19 17:10:06 +00:00
|
|
|
bool DownloadEngine::validateToken(const std::string& token)
|
|
|
|
{
|
|
|
|
using namespace util::security;
|
|
|
|
|
|
|
|
if (!option_->defined(PREF_RPC_SECRET)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-07-15 06:15:46 +00:00
|
|
|
if (!tokenHMAC_) {
|
2014-04-19 17:10:06 +00:00
|
|
|
tokenHMAC_ = HMAC::createRandom();
|
|
|
|
if (!tokenHMAC_) {
|
|
|
|
A2_LOG_ERROR("Failed to create HMAC");
|
|
|
|
return false;
|
|
|
|
}
|
2015-12-27 09:39:47 +00:00
|
|
|
tokenExpected_ = make_unique<HMACResult>(
|
|
|
|
tokenHMAC_->getResult(option_->get(PREF_RPC_SECRET)));
|
2014-04-19 17:10:06 +00:00
|
|
|
}
|
|
|
|
|
2014-07-15 06:15:46 +00:00
|
|
|
return *tokenExpected_ == tokenHMAC_->getResult(token);
|
2014-04-19 17:10:06 +00:00
|
|
|
}
|
|
|
|
|
2008-02-08 15:53:45 +00:00
|
|
|
} // namespace aria2
|