PeerListenCommand.cc 4.4 KB

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