PeerListenCommand.cc 4.1 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 "PeerListenCommand.h"
  36. #include <utility>
  37. #include <algorithm>
  38. #include "DownloadEngine.h"
  39. #include "Peer.h"
  40. #include "RequestGroupMan.h"
  41. #include "RecoverableException.h"
  42. #include "message.h"
  43. #include "ReceiverMSEHandshakeCommand.h"
  44. #include "Logger.h"
  45. #include "LogFactory.h"
  46. #include "SocketCore.h"
  47. #include "SimpleRandomizer.h"
  48. #include "util.h"
  49. #include "fmt.h"
  50. namespace aria2 {
  51. PeerListenCommand::PeerListenCommand(cuid_t cuid, DownloadEngine* e, int family)
  52. : Command(cuid), e_(e), family_(family)
  53. {
  54. }
  55. PeerListenCommand::~PeerListenCommand() {}
  56. bool PeerListenCommand::bindPort(uint16_t& port, SegList<int>& sgl)
  57. {
  58. socket_ = std::make_shared<SocketCore>();
  59. std::vector<uint16_t> ports;
  60. while (sgl.hasNext()) {
  61. ports.push_back(sgl.next());
  62. }
  63. std::shuffle(ports.begin(), ports.end(), *SimpleRandomizer::getInstance());
  64. const int ipv = (family_ == AF_INET) ? 4 : 6;
  65. for (std::vector<uint16_t>::const_iterator i = ports.begin(),
  66. eoi = ports.end();
  67. i != eoi; ++i) {
  68. port = *i;
  69. try {
  70. socket_->bind(nullptr, port, family_);
  71. socket_->beginListen();
  72. A2_LOG_NOTICE(
  73. fmt(_("IPv%d BitTorrent: listening on TCP port %u"), ipv, port));
  74. return true;
  75. }
  76. catch (RecoverableException& ex) {
  77. A2_LOG_ERROR_EX(
  78. fmt("IPv%d BitTorrent: failed to bind TCP port %u", ipv, port), ex);
  79. socket_->closeConnection();
  80. }
  81. }
  82. return false;
  83. }
  84. uint16_t PeerListenCommand::getPort() const
  85. {
  86. if (!socket_) {
  87. return 0;
  88. }
  89. return socket_->getAddrInfo().port;
  90. }
  91. bool PeerListenCommand::execute()
  92. {
  93. if (e_->isHaltRequested() || e_->getRequestGroupMan()->downloadFinished()) {
  94. return true;
  95. }
  96. for (int i = 0; i < 3 && socket_->isReadable(0); ++i) {
  97. std::shared_ptr<SocketCore> peerSocket;
  98. try {
  99. peerSocket = socket_->acceptConnection();
  100. peerSocket->applyIpDscp();
  101. auto endpoint = peerSocket->getPeerInfo();
  102. auto peer = std::make_shared<Peer>(endpoint.addr, endpoint.port, true);
  103. cuid_t cuid = e_->newCUID();
  104. e_->addCommand(
  105. make_unique<ReceiverMSEHandshakeCommand>(cuid, peer, e_, peerSocket));
  106. A2_LOG_DEBUG(fmt("Accepted the connection from %s:%u.",
  107. peer->getIPAddress().c_str(), peer->getPort()));
  108. A2_LOG_DEBUG(fmt(
  109. "Added CUID#%" PRId64 " to receive BitTorrent/MSE handshake.", cuid));
  110. }
  111. catch (RecoverableException& ex) {
  112. A2_LOG_DEBUG_EX(fmt(MSG_ACCEPT_FAILURE, getCuid()), ex);
  113. }
  114. }
  115. e_->addCommand(std::unique_ptr<Command>(this));
  116. return false;
  117. }
  118. } // namespace aria2