InitiateConnectionCommand.cc 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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 "InitiateConnectionCommand.h"
  36. #include "Request.h"
  37. #include "DownloadEngine.h"
  38. #include "Option.h"
  39. #include "Logger.h"
  40. #include "LogFactory.h"
  41. #include "message.h"
  42. #include "prefs.h"
  43. #include "NameResolver.h"
  44. #include "SocketCore.h"
  45. #include "FileEntry.h"
  46. #include "RequestGroup.h"
  47. #include "Segment.h"
  48. #include "a2functional.h"
  49. #include "InitiateConnectionCommandFactory.h"
  50. #include "util.h"
  51. #include "RecoverableException.h"
  52. #include "fmt.h"
  53. #include "SocketRecvBuffer.h"
  54. #include "BackupIPv4ConnectCommand.h"
  55. #include "ConnectCommand.h"
  56. namespace aria2 {
  57. InitiateConnectionCommand::InitiateConnectionCommand
  58. (cuid_t cuid,
  59. const std::shared_ptr<Request>& req,
  60. const std::shared_ptr<FileEntry>& fileEntry,
  61. RequestGroup* requestGroup,
  62. DownloadEngine* e)
  63. : AbstractCommand(cuid, req, fileEntry, requestGroup, e)
  64. {
  65. setTimeout(getOption()->getAsInt(PREF_DNS_TIMEOUT));
  66. // give a chance to be executed in the next loop in DownloadEngine
  67. setStatus(Command::STATUS_ONESHOT_REALTIME);
  68. disableReadCheckSocket();
  69. disableWriteCheckSocket();
  70. }
  71. InitiateConnectionCommand::~InitiateConnectionCommand() {}
  72. bool InitiateConnectionCommand::executeInternal() {
  73. std::string hostname;
  74. uint16_t port;
  75. std::shared_ptr<Request> proxyRequest = createProxyRequest();
  76. if(!proxyRequest) {
  77. hostname = getRequest()->getHost();
  78. port = getRequest()->getPort();
  79. } else {
  80. hostname = proxyRequest->getHost();
  81. port = proxyRequest->getPort();
  82. }
  83. std::vector<std::string> addrs;
  84. std::string ipaddr = resolveHostname(addrs, hostname, port);
  85. if(ipaddr.empty()) {
  86. addCommandSelf();
  87. return false;
  88. }
  89. try {
  90. auto c = createNextCommand(hostname, ipaddr, port, addrs, proxyRequest);
  91. c->setStatus(Command::STATUS_ONESHOT_REALTIME);
  92. getDownloadEngine()->setNoWait(true);
  93. getDownloadEngine()->addCommand(std::move(c));
  94. return true;
  95. } catch(RecoverableException& ex) {
  96. // Catch exception and retry another address.
  97. // See also AbstractCommand::checkIfConnectionEstablished
  98. // TODO ipaddr might not be used if pooled socket was found.
  99. getDownloadEngine()->markBadIPAddress(hostname, ipaddr, port);
  100. if(!getDownloadEngine()->findCachedIPAddress(hostname, port).empty()) {
  101. A2_LOG_INFO_EX(EX_EXCEPTION_CAUGHT, ex);
  102. A2_LOG_INFO(fmt(MSG_CONNECT_FAILED_AND_RETRY,
  103. getCuid(),
  104. ipaddr.c_str(), port));
  105. auto command =
  106. InitiateConnectionCommandFactory::createInitiateConnectionCommand
  107. (getCuid(), getRequest(), getFileEntry(), getRequestGroup(),
  108. getDownloadEngine());
  109. getDownloadEngine()->setNoWait(true);
  110. getDownloadEngine()->addCommand(std::move(command));
  111. return true;
  112. }
  113. getDownloadEngine()->removeCachedIPAddress(hostname, port);
  114. throw;
  115. }
  116. }
  117. void InitiateConnectionCommand::setConnectedAddrInfo
  118. (const std::shared_ptr<Request>& req,
  119. const std::string& hostname,
  120. const std::shared_ptr<SocketCore>& socket)
  121. {
  122. std::pair<std::string, uint16_t> peerAddr;
  123. socket->getPeerInfo(peerAddr);
  124. req->setConnectedAddrInfo(hostname, peerAddr.first, peerAddr.second);
  125. }
  126. std::shared_ptr<BackupConnectInfo>
  127. InitiateConnectionCommand::createBackupIPv4ConnectCommand
  128. (const std::string& hostname, const std::string& ipaddr, uint16_t port,
  129. Command* mainCommand)
  130. {
  131. // Prepare IPv4 backup connection attempt in "Happy Eyeballs"
  132. // fashion.
  133. std::shared_ptr<BackupConnectInfo> info;
  134. char buf[sizeof(in6_addr)];
  135. if(inetPton(AF_INET6, ipaddr.c_str(), &buf) == -1) {
  136. return info;
  137. }
  138. A2_LOG_INFO("Searching IPv4 address for backup connection attempt");
  139. std::vector<std::string> addrs;
  140. getDownloadEngine()->findAllCachedIPAddresses(std::back_inserter(addrs),
  141. hostname, port);
  142. for(std::vector<std::string>::const_iterator i = addrs.begin(),
  143. eoi = addrs.end(); i != eoi; ++i) {
  144. if(inetPton(AF_INET, (*i).c_str(), &buf) == 0) {
  145. info = std::make_shared<BackupConnectInfo>();
  146. auto command = make_unique<BackupIPv4ConnectCommand>
  147. (getDownloadEngine()->newCUID(), *i, port, info, mainCommand,
  148. getRequestGroup(), getDownloadEngine());
  149. A2_LOG_INFO(fmt("Issue backup connection command CUID#%" PRId64
  150. ", addr=%s", command->getCuid(), (*i).c_str()));
  151. getDownloadEngine()->addCommand(std::move(command));
  152. return info;
  153. }
  154. }
  155. return info;
  156. }
  157. void InitiateConnectionCommand::setupBackupConnection
  158. (const std::string& hostname, const std::string& addr, uint16_t port,
  159. ConnectCommand* c)
  160. {
  161. std::shared_ptr<BackupConnectInfo> backupConnectInfo
  162. = createBackupIPv4ConnectCommand(hostname, addr, port, c);
  163. if(backupConnectInfo) {
  164. c->setBackupConnectInfo(backupConnectInfo);
  165. }
  166. }
  167. } // namespace aria2