DownloadEngine.cc 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649
  1. /* <!-- copyright */
  2. /*
  3. * aria2 - The high speed download utility
  4. *
  5. * Copyright (C) 2006 Tatsuhiro Tsujikawa
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. *
  21. * In addition, as a special exception, the copyright holders give
  22. * permission to link the code of portions of this program with the
  23. * OpenSSL library under certain conditions as described in each
  24. * individual source file, and distribute linked combinations
  25. * including the two.
  26. * You must obey the GNU General Public License in all respects
  27. * for all of the code used other than OpenSSL. If you modify
  28. * file(s) with this exception, you may extend this exception to your
  29. * version of the file(s), but you are not obligated to do so. If you
  30. * do not wish to do so, delete this exception statement from your
  31. * version. If you delete this exception statement from all source
  32. * files in the program, then also delete it here.
  33. */
  34. /* copyright --> */
  35. #include "DownloadEngine.h"
  36. #include <signal.h>
  37. #include <cstring>
  38. #include <cerrno>
  39. #include <algorithm>
  40. #include <numeric>
  41. #include <iterator>
  42. #include "StatCalc.h"
  43. #include "RequestGroup.h"
  44. #include "RequestGroupMan.h"
  45. #include "DownloadResult.h"
  46. #include "StatCalc.h"
  47. #include "LogFactory.h"
  48. #include "Logger.h"
  49. #include "SocketCore.h"
  50. #include "util.h"
  51. #include "a2functional.h"
  52. #include "DlAbortEx.h"
  53. #include "ServerStatMan.h"
  54. #include "CookieStorage.h"
  55. #include "A2STR.h"
  56. #include "AuthConfigFactory.h"
  57. #include "AuthConfig.h"
  58. #include "Request.h"
  59. #include "EventPoll.h"
  60. #include "Command.h"
  61. #include "FileAllocationEntry.h"
  62. #include "CheckIntegrityEntry.h"
  63. #include "BtProgressInfoFile.h"
  64. #include "DownloadContext.h"
  65. #include "fmt.h"
  66. #include "wallclock.h"
  67. #ifdef ENABLE_BITTORRENT
  68. #include "BtRegistry.h"
  69. #endif // ENABLE_BITTORRENT
  70. #ifdef ENABLE_WEBSOCKET
  71. #include "WebSocketSessionMan.h"
  72. #endif // ENABLE_WEBSOCKET
  73. #include "Option.h"
  74. #include "util_security.h"
  75. namespace aria2 {
  76. namespace global {
  77. // 0 ... running
  78. // 1 ... stop signal detected
  79. // 2 ... stop signal processed by DownloadEngine
  80. // 3 ... 2nd stop signal(force shutdown) detected
  81. // 4 ... 2nd stop signal processed by DownloadEngine
  82. // 5 ... main loop exited
  83. volatile sig_atomic_t globalHaltRequested = 0;
  84. } // namespace global
  85. namespace {
  86. constexpr auto DEFAULT_REFRESH_INTERVAL = 1_s;
  87. } // namespace
  88. DownloadEngine::DownloadEngine(std::unique_ptr<EventPoll> eventPoll)
  89. : eventPoll_(std::move(eventPoll)),
  90. haltRequested_(0),
  91. noWait_(true),
  92. refreshInterval_(DEFAULT_REFRESH_INTERVAL),
  93. lastRefresh_(Timer::zero()),
  94. cookieStorage_(make_unique<CookieStorage>()),
  95. #ifdef ENABLE_BITTORRENT
  96. btRegistry_(make_unique<BtRegistry>()),
  97. #endif // ENABLE_BITTORRENT
  98. #ifdef HAVE_ARES_ADDR_NODE
  99. asyncDNSServers_(nullptr),
  100. #endif // HAVE_ARES_ADDR_NODE
  101. dnsCache_(make_unique<DNSCache>()),
  102. option_(nullptr)
  103. {
  104. unsigned char sessionId[20];
  105. util::generateRandomKey(sessionId);
  106. sessionId_.assign(&sessionId[0], &sessionId[sizeof(sessionId)]);
  107. }
  108. DownloadEngine::~DownloadEngine()
  109. {
  110. #ifdef HAVE_ARES_ADDR_NODE
  111. setAsyncDNSServers(nullptr);
  112. #endif // HAVE_ARES_ADDR_NODE
  113. }
  114. namespace {
  115. void executeCommand(std::deque<std::unique_ptr<Command>>& commands,
  116. Command::STATUS statusFilter)
  117. {
  118. size_t max = commands.size();
  119. for (size_t i = 0; i < max; ++i) {
  120. auto com = std::move(commands.front());
  121. commands.pop_front();
  122. if (!com->statusMatch(statusFilter)) {
  123. com->clearIOEvents();
  124. commands.push_back(std::move(com));
  125. continue;
  126. }
  127. com->transitStatus();
  128. if (com->execute()) {
  129. com.reset();
  130. }
  131. else {
  132. com->clearIOEvents();
  133. com.release();
  134. }
  135. }
  136. }
  137. } // namespace
  138. namespace {
  139. class GlobalHaltRequestedFinalizer {
  140. public:
  141. GlobalHaltRequestedFinalizer() = default;
  142. ~GlobalHaltRequestedFinalizer() { global::globalHaltRequested = 5; }
  143. };
  144. } // namespace
  145. int DownloadEngine::run(bool oneshot)
  146. {
  147. GlobalHaltRequestedFinalizer ghrf;
  148. while (!commands_.empty() || !routineCommands_.empty()) {
  149. if (!commands_.empty()) {
  150. waitData();
  151. }
  152. noWait_ = false;
  153. global::wallclock().reset();
  154. calculateStatistics();
  155. if (lastRefresh_.difference(global::wallclock()) + A2_DELTA_MILLIS >=
  156. refreshInterval_) {
  157. refreshInterval_ = DEFAULT_REFRESH_INTERVAL;
  158. lastRefresh_ = global::wallclock();
  159. executeCommand(commands_, Command::STATUS_ALL);
  160. }
  161. else {
  162. executeCommand(commands_, Command::STATUS_ACTIVE);
  163. }
  164. executeCommand(routineCommands_, Command::STATUS_ALL);
  165. afterEachIteration();
  166. if (!noWait_ && oneshot) {
  167. return 1;
  168. }
  169. }
  170. onEndOfRun();
  171. return 0;
  172. }
  173. void DownloadEngine::waitData()
  174. {
  175. struct timeval tv;
  176. if (noWait_) {
  177. tv.tv_sec = tv.tv_usec = 0;
  178. }
  179. else {
  180. auto t =
  181. std::chrono::duration_cast<std::chrono::microseconds>(refreshInterval_);
  182. tv.tv_sec = t.count() / 1000000;
  183. tv.tv_usec = t.count() % 1000000;
  184. }
  185. eventPoll_->poll(tv);
  186. }
  187. bool DownloadEngine::addSocketForReadCheck(
  188. const std::shared_ptr<SocketCore>& socket, Command* command)
  189. {
  190. return eventPoll_->addEvents(socket->getSockfd(), command,
  191. EventPoll::EVENT_READ);
  192. }
  193. bool DownloadEngine::deleteSocketForReadCheck(
  194. const std::shared_ptr<SocketCore>& socket, Command* command)
  195. {
  196. return eventPoll_->deleteEvents(socket->getSockfd(), command,
  197. EventPoll::EVENT_READ);
  198. }
  199. bool DownloadEngine::addSocketForWriteCheck(
  200. const std::shared_ptr<SocketCore>& socket, Command* command)
  201. {
  202. return eventPoll_->addEvents(socket->getSockfd(), command,
  203. EventPoll::EVENT_WRITE);
  204. }
  205. bool DownloadEngine::deleteSocketForWriteCheck(
  206. const std::shared_ptr<SocketCore>& socket, Command* command)
  207. {
  208. return eventPoll_->deleteEvents(socket->getSockfd(), command,
  209. EventPoll::EVENT_WRITE);
  210. }
  211. void DownloadEngine::calculateStatistics()
  212. {
  213. if (statCalc_) {
  214. statCalc_->calculateStat(this);
  215. }
  216. }
  217. void DownloadEngine::onEndOfRun()
  218. {
  219. requestGroupMan_->removeStoppedGroup(this);
  220. requestGroupMan_->closeFile();
  221. requestGroupMan_->save();
  222. }
  223. void DownloadEngine::afterEachIteration()
  224. {
  225. if (global::globalHaltRequested == 1) {
  226. A2_LOG_NOTICE(_("Shutdown sequence commencing..."
  227. " Press Ctrl-C again for emergency shutdown."));
  228. requestHalt();
  229. global::globalHaltRequested = 2;
  230. setNoWait(true);
  231. setRefreshInterval(std::chrono::milliseconds(0));
  232. return;
  233. }
  234. if (global::globalHaltRequested == 3) {
  235. A2_LOG_NOTICE(_("Emergency shutdown sequence commencing..."));
  236. requestForceHalt();
  237. global::globalHaltRequested = 4;
  238. setNoWait(true);
  239. setRefreshInterval(std::chrono::milliseconds(0));
  240. return;
  241. }
  242. }
  243. void DownloadEngine::requestHalt()
  244. {
  245. haltRequested_ = std::max(haltRequested_, 1);
  246. requestGroupMan_->halt();
  247. }
  248. void DownloadEngine::requestForceHalt()
  249. {
  250. haltRequested_ = std::max(haltRequested_, 2);
  251. requestGroupMan_->forceHalt();
  252. }
  253. void DownloadEngine::setStatCalc(std::unique_ptr<StatCalc> statCalc)
  254. {
  255. statCalc_ = std::move(statCalc);
  256. }
  257. #ifdef ENABLE_ASYNC_DNS
  258. bool DownloadEngine::addNameResolverCheck(
  259. const std::shared_ptr<AsyncNameResolver>& resolver, Command* command)
  260. {
  261. return eventPoll_->addNameResolver(resolver, command);
  262. }
  263. bool DownloadEngine::deleteNameResolverCheck(
  264. const std::shared_ptr<AsyncNameResolver>& resolver, Command* command)
  265. {
  266. return eventPoll_->deleteNameResolver(resolver, command);
  267. }
  268. #endif // ENABLE_ASYNC_DNS
  269. void DownloadEngine::setNoWait(bool b) { noWait_ = b; }
  270. void DownloadEngine::addRoutineCommand(std::unique_ptr<Command> command)
  271. {
  272. routineCommands_.push_back(std::move(command));
  273. }
  274. void DownloadEngine::poolSocket(const std::string& key,
  275. const SocketPoolEntry& entry)
  276. {
  277. A2_LOG_INFO(fmt("Pool socket for %s", key.c_str()));
  278. std::multimap<std::string, SocketPoolEntry>::value_type p(key, entry);
  279. socketPool_.insert(p);
  280. }
  281. void DownloadEngine::evictSocketPool()
  282. {
  283. if (socketPool_.empty()) {
  284. return;
  285. }
  286. std::multimap<std::string, SocketPoolEntry> newPool;
  287. A2_LOG_DEBUG("Scaning SocketPool and erasing timed out entry.");
  288. for (auto& elem : socketPool_) {
  289. if (!elem.second.isTimeout()) {
  290. newPool.insert(elem);
  291. }
  292. }
  293. A2_LOG_DEBUG(
  294. fmt("%lu entries removed.",
  295. static_cast<unsigned long>(socketPool_.size() - newPool.size())));
  296. socketPool_ = std::move(newPool);
  297. }
  298. namespace {
  299. std::string createSockPoolKey(const std::string& host, uint16_t port,
  300. const std::string& username,
  301. const std::string& proxyhost, uint16_t proxyport)
  302. {
  303. std::string key;
  304. if (!username.empty()) {
  305. key += util::percentEncode(username);
  306. key += "@";
  307. }
  308. key += fmt("%s(%u)", host.c_str(), port);
  309. if (!proxyhost.empty()) {
  310. key += fmt("/%s(%u)", proxyhost.c_str(), proxyport);
  311. }
  312. return key;
  313. }
  314. } // namespace
  315. void DownloadEngine::poolSocket(const std::string& ipaddr, uint16_t port,
  316. const std::string& username,
  317. const std::string& proxyhost,
  318. uint16_t proxyport,
  319. const std::shared_ptr<SocketCore>& sock,
  320. const std::string& options,
  321. std::chrono::seconds timeout)
  322. {
  323. SocketPoolEntry e(sock, options, std::move(timeout));
  324. poolSocket(createSockPoolKey(ipaddr, port, username, proxyhost, proxyport),
  325. e);
  326. }
  327. void DownloadEngine::poolSocket(const std::string& ipaddr, uint16_t port,
  328. const std::string& proxyhost,
  329. uint16_t proxyport,
  330. const std::shared_ptr<SocketCore>& sock,
  331. std::chrono::seconds timeout)
  332. {
  333. SocketPoolEntry e(sock, std::move(timeout));
  334. poolSocket(createSockPoolKey(ipaddr, port, A2STR::NIL, proxyhost, proxyport),
  335. e);
  336. }
  337. namespace {
  338. bool getPeerInfo(Endpoint& res, const std::shared_ptr<SocketCore>& socket)
  339. {
  340. try {
  341. res = socket->getPeerInfo();
  342. return true;
  343. }
  344. catch (RecoverableException& e) {
  345. // socket->getPeerInfo() can fail if the socket has been
  346. // disconnected.
  347. A2_LOG_INFO_EX("Getting peer info failed. Pooling socket canceled.", e);
  348. return false;
  349. }
  350. }
  351. } // namespace
  352. void DownloadEngine::poolSocket(const std::shared_ptr<Request>& request,
  353. const std::shared_ptr<Request>& proxyRequest,
  354. const std::shared_ptr<SocketCore>& socket,
  355. std::chrono::seconds timeout)
  356. {
  357. if (proxyRequest) {
  358. // If proxy is defined, then pool socket with its hostname.
  359. poolSocket(request->getHost(), request->getPort(), proxyRequest->getHost(),
  360. proxyRequest->getPort(), socket, std::move(timeout));
  361. return;
  362. }
  363. Endpoint peerInfo;
  364. if (getPeerInfo(peerInfo, socket)) {
  365. poolSocket(peerInfo.addr, peerInfo.port, A2STR::NIL, 0, socket,
  366. std::move(timeout));
  367. }
  368. }
  369. void DownloadEngine::poolSocket(const std::shared_ptr<Request>& request,
  370. const std::string& username,
  371. const std::shared_ptr<Request>& proxyRequest,
  372. const std::shared_ptr<SocketCore>& socket,
  373. const std::string& options,
  374. std::chrono::seconds timeout)
  375. {
  376. if (proxyRequest) {
  377. // If proxy is defined, then pool socket with its hostname.
  378. poolSocket(request->getHost(), request->getPort(), username,
  379. proxyRequest->getHost(), proxyRequest->getPort(), socket,
  380. options, std::move(timeout));
  381. return;
  382. }
  383. Endpoint peerInfo;
  384. if (getPeerInfo(peerInfo, socket)) {
  385. poolSocket(peerInfo.addr, peerInfo.port, username, A2STR::NIL, 0, socket,
  386. options, std::move(timeout));
  387. }
  388. }
  389. std::multimap<std::string, DownloadEngine::SocketPoolEntry>::iterator
  390. DownloadEngine::findSocketPoolEntry(const std::string& key)
  391. {
  392. std::pair<std::multimap<std::string, SocketPoolEntry>::iterator,
  393. std::multimap<std::string, SocketPoolEntry>::iterator> range =
  394. socketPool_.equal_range(key);
  395. for (auto i = range.first, eoi = range.second; i != eoi; ++i) {
  396. const SocketPoolEntry& e = (*i).second;
  397. // We assume that if socket is readable it means peer shutdowns
  398. // connection and the socket will receive EOF. So skip it.
  399. if (!e.isTimeout() && !e.getSocket()->isReadable(0)) {
  400. A2_LOG_INFO(fmt("Found socket for %s", key.c_str()));
  401. return i;
  402. }
  403. }
  404. return socketPool_.end();
  405. }
  406. std::shared_ptr<SocketCore>
  407. DownloadEngine::popPooledSocket(const std::string& ipaddr, uint16_t port,
  408. const std::string& proxyhost,
  409. uint16_t proxyport)
  410. {
  411. std::shared_ptr<SocketCore> s;
  412. auto i = findSocketPoolEntry(
  413. createSockPoolKey(ipaddr, port, A2STR::NIL, proxyhost, proxyport));
  414. if (i != socketPool_.end()) {
  415. s = (*i).second.getSocket();
  416. socketPool_.erase(i);
  417. }
  418. return s;
  419. }
  420. std::shared_ptr<SocketCore>
  421. DownloadEngine::popPooledSocket(std::string& options, const std::string& ipaddr,
  422. uint16_t port, const std::string& username,
  423. const std::string& proxyhost,
  424. uint16_t proxyport)
  425. {
  426. std::shared_ptr<SocketCore> s;
  427. auto i = findSocketPoolEntry(
  428. createSockPoolKey(ipaddr, port, username, proxyhost, proxyport));
  429. if (i != socketPool_.end()) {
  430. s = (*i).second.getSocket();
  431. options = (*i).second.getOptions();
  432. socketPool_.erase(i);
  433. }
  434. return s;
  435. }
  436. std::shared_ptr<SocketCore>
  437. DownloadEngine::popPooledSocket(const std::vector<std::string>& ipaddrs,
  438. uint16_t port)
  439. {
  440. std::shared_ptr<SocketCore> s;
  441. for (const auto& ipaddr : ipaddrs) {
  442. s = popPooledSocket(ipaddr, port, A2STR::NIL, 0);
  443. if (s) {
  444. break;
  445. }
  446. }
  447. return s;
  448. }
  449. std::shared_ptr<SocketCore>
  450. DownloadEngine::popPooledSocket(std::string& options,
  451. const std::vector<std::string>& ipaddrs,
  452. uint16_t port, const std::string& username)
  453. {
  454. std::shared_ptr<SocketCore> s;
  455. for (const auto& ipaddr : ipaddrs) {
  456. s = popPooledSocket(options, ipaddr, port, username, A2STR::NIL, 0);
  457. if (s) {
  458. break;
  459. }
  460. }
  461. return s;
  462. }
  463. DownloadEngine::SocketPoolEntry::SocketPoolEntry(
  464. const std::shared_ptr<SocketCore>& socket, const std::string& options,
  465. std::chrono::seconds timeout)
  466. : socket_(socket), options_(options), timeout_(std::move(timeout))
  467. {
  468. }
  469. DownloadEngine::SocketPoolEntry::SocketPoolEntry(
  470. const std::shared_ptr<SocketCore>& socket, std::chrono::seconds timeout)
  471. : socket_(socket), timeout_(std::move(timeout))
  472. {
  473. }
  474. DownloadEngine::SocketPoolEntry::~SocketPoolEntry() = default;
  475. bool DownloadEngine::SocketPoolEntry::isTimeout() const
  476. {
  477. return registeredTime_.difference(global::wallclock()) >= timeout_;
  478. }
  479. cuid_t DownloadEngine::newCUID() { return cuidCounter_.newID(); }
  480. const std::string&
  481. DownloadEngine::findCachedIPAddress(const std::string& hostname,
  482. uint16_t port) const
  483. {
  484. return dnsCache_->find(hostname, port);
  485. }
  486. void DownloadEngine::cacheIPAddress(const std::string& hostname,
  487. const std::string& ipaddr, uint16_t port)
  488. {
  489. dnsCache_->put(hostname, ipaddr, port);
  490. }
  491. void DownloadEngine::markBadIPAddress(const std::string& hostname,
  492. const std::string& ipaddr, uint16_t port)
  493. {
  494. dnsCache_->markBad(hostname, ipaddr, port);
  495. }
  496. void DownloadEngine::removeCachedIPAddress(const std::string& hostname,
  497. uint16_t port)
  498. {
  499. dnsCache_->remove(hostname, port);
  500. }
  501. void DownloadEngine::setAuthConfigFactory(
  502. std::unique_ptr<AuthConfigFactory> factory)
  503. {
  504. authConfigFactory_ = std::move(factory);
  505. }
  506. const std::unique_ptr<AuthConfigFactory>&
  507. DownloadEngine::getAuthConfigFactory() const
  508. {
  509. return authConfigFactory_;
  510. }
  511. const std::unique_ptr<CookieStorage>& DownloadEngine::getCookieStorage() const
  512. {
  513. return cookieStorage_;
  514. }
  515. void DownloadEngine::setRefreshInterval(std::chrono::milliseconds interval)
  516. {
  517. refreshInterval_ = std::move(interval);
  518. }
  519. void DownloadEngine::addCommand(std::vector<std::unique_ptr<Command>> commands)
  520. {
  521. commands_.insert(commands_.end(),
  522. std::make_move_iterator(std::begin(commands)),
  523. std::make_move_iterator(std::end(commands)));
  524. }
  525. void DownloadEngine::addCommand(std::unique_ptr<Command> command)
  526. {
  527. commands_.push_back(std::move(command));
  528. }
  529. void DownloadEngine::setRequestGroupMan(std::unique_ptr<RequestGroupMan> rgman)
  530. {
  531. requestGroupMan_ = std::move(rgman);
  532. }
  533. void DownloadEngine::setFileAllocationMan(
  534. std::unique_ptr<FileAllocationMan> faman)
  535. {
  536. fileAllocationMan_ = std::move(faman);
  537. }
  538. void DownloadEngine::setCheckIntegrityMan(
  539. std::unique_ptr<CheckIntegrityMan> ciman)
  540. {
  541. checkIntegrityMan_ = std::move(ciman);
  542. }
  543. #ifdef HAVE_ARES_ADDR_NODE
  544. void DownloadEngine::setAsyncDNSServers(ares_addr_node* asyncDNSServers)
  545. {
  546. ares_addr_node* node = asyncDNSServers_;
  547. while (node) {
  548. ares_addr_node* next = node->next;
  549. delete node;
  550. node = next;
  551. }
  552. asyncDNSServers_ = asyncDNSServers;
  553. }
  554. #endif // HAVE_ARES_ADDR_NODE
  555. #ifdef ENABLE_WEBSOCKET
  556. void DownloadEngine::setWebSocketSessionMan(
  557. std::unique_ptr<rpc::WebSocketSessionMan> wsman)
  558. {
  559. webSocketSessionMan_ = std::move(wsman);
  560. }
  561. #endif // ENABLE_WEBSOCKET
  562. bool DownloadEngine::validateToken(const std::string& token)
  563. {
  564. using namespace util::security;
  565. if (!option_->defined(PREF_RPC_SECRET)) {
  566. return true;
  567. }
  568. if (!tokenHMAC_) {
  569. tokenHMAC_ = HMAC::createRandom();
  570. if (!tokenHMAC_) {
  571. A2_LOG_ERROR("Failed to create HMAC");
  572. return false;
  573. }
  574. tokenExpected_ = make_unique<HMACResult>(
  575. tokenHMAC_->getResult(option_->get(PREF_RPC_SECRET)));
  576. }
  577. return *tokenExpected_ == tokenHMAC_->getResult(token);
  578. }
  579. } // namespace aria2