InitiateConnectionCommand.cc 4.5 KB

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