InitiateConnectionCommand.cc 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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. namespace aria2 {
  55. InitiateConnectionCommand::InitiateConnectionCommand
  56. (cuid_t cuid,
  57. const SharedHandle<Request>& req,
  58. const SharedHandle<FileEntry>& fileEntry,
  59. RequestGroup* requestGroup,
  60. DownloadEngine* e)
  61. : AbstractCommand(cuid, req, fileEntry, requestGroup, e)
  62. {
  63. setTimeout(getOption()->getAsInt(PREF_DNS_TIMEOUT));
  64. // give a chance to be executed in the next loop in DownloadEngine
  65. setStatus(Command::STATUS_ONESHOT_REALTIME);
  66. disableReadCheckSocket();
  67. disableWriteCheckSocket();
  68. }
  69. InitiateConnectionCommand::~InitiateConnectionCommand() {}
  70. bool InitiateConnectionCommand::executeInternal() {
  71. std::string hostname;
  72. uint16_t port;
  73. SharedHandle<Request> proxyRequest = createProxyRequest();
  74. if(!proxyRequest) {
  75. hostname = getRequest()->getHost();
  76. port = getRequest()->getPort();
  77. } else {
  78. hostname = proxyRequest->getHost();
  79. port = proxyRequest->getPort();
  80. }
  81. std::vector<std::string> addrs;
  82. std::string ipaddr = resolveHostname(addrs, hostname, port);
  83. if(ipaddr.empty()) {
  84. getDownloadEngine()->addCommand(this);
  85. return false;
  86. }
  87. try {
  88. Command* command = createNextCommand(hostname, ipaddr, port,
  89. addrs, proxyRequest);
  90. getDownloadEngine()->addCommand(command);
  91. return true;
  92. } catch(RecoverableException& ex) {
  93. // Catch exception and retry another address.
  94. // See also AbstractCommand::checkIfConnectionEstablished
  95. // TODO ipaddr might not be used if pooled socket was found.
  96. getDownloadEngine()->markBadIPAddress(hostname, ipaddr, port);
  97. if(!getDownloadEngine()->findCachedIPAddress(hostname, port).empty()) {
  98. A2_LOG_INFO_EX(EX_EXCEPTION_CAUGHT, ex);
  99. A2_LOG_INFO(fmt(MSG_CONNECT_FAILED_AND_RETRY,
  100. getCuid(),
  101. ipaddr.c_str(), port));
  102. Command* command =
  103. InitiateConnectionCommandFactory::createInitiateConnectionCommand
  104. (getCuid(), getRequest(), getFileEntry(), getRequestGroup(),
  105. getDownloadEngine());
  106. getDownloadEngine()->setNoWait(true);
  107. getDownloadEngine()->addCommand(command);
  108. return true;
  109. }
  110. getDownloadEngine()->removeCachedIPAddress(hostname, port);
  111. throw;
  112. }
  113. }
  114. void InitiateConnectionCommand::setConnectedAddrInfo
  115. (const SharedHandle<Request>& req,
  116. const std::string& hostname,
  117. const SharedHandle<SocketCore>& socket)
  118. {
  119. std::pair<std::string, uint16_t> peerAddr;
  120. socket->getPeerInfo(peerAddr);
  121. req->setConnectedAddrInfo(hostname, peerAddr.first, peerAddr.second);
  122. }
  123. } // namespace aria2