PeerListenCommand.cc 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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 "Socket.h"
  47. #include "SimpleRandomizer.h"
  48. #include "util.h"
  49. #include "fmt.h"
  50. namespace aria2 {
  51. unsigned int PeerListenCommand::numInstance_ = 0;
  52. PeerListenCommand* PeerListenCommand::instance_ = 0;
  53. PeerListenCommand* PeerListenCommand::instance6_ = 0;
  54. PeerListenCommand::PeerListenCommand
  55. (cuid_t cuid,
  56. DownloadEngine* e,
  57. int family)
  58. : Command(cuid),
  59. e_(e),
  60. family_(family),
  61. lowestSpeedLimit_(20*1024)
  62. {
  63. ++numInstance_;
  64. }
  65. PeerListenCommand::~PeerListenCommand()
  66. {
  67. --numInstance_;
  68. }
  69. bool PeerListenCommand::bindPort(uint16_t& port, IntSequence& seq)
  70. {
  71. socket_.reset(new SocketCore());
  72. std::vector<int32_t> randPorts = seq.flush();
  73. std::random_shuffle(randPorts.begin(), randPorts.end(),
  74. *SimpleRandomizer::getInstance().get());
  75. for(std::vector<int32_t>::const_iterator portItr = randPorts.begin(),
  76. eoi = randPorts.end(); portItr != eoi; ++portItr) {
  77. if(!(0 < (*portItr) && (*portItr) <= 65535)) {
  78. continue;
  79. }
  80. port = (*portItr);
  81. try {
  82. socket_->bind(A2STR::NIL, port, family_);
  83. socket_->beginListen();
  84. socket_->setNonBlockingMode();
  85. A2_LOG_NOTICE(fmt("IPv%d BitTorrent: listening to port %d",
  86. family_ == AF_INET?4:6, port));
  87. return true;
  88. } catch(RecoverableException& ex) {
  89. A2_LOG_ERROR_EX(fmt(MSG_BIND_FAILURE,
  90. getCuid(), port),
  91. ex);
  92. socket_->closeConnection();
  93. }
  94. }
  95. return false;
  96. }
  97. uint16_t PeerListenCommand::getPort() const
  98. {
  99. if(!socket_) {
  100. return 0;
  101. } else {
  102. std::pair<std::string, uint16_t> addr;
  103. socket_->getAddrInfo(addr);
  104. return addr.second;
  105. }
  106. }
  107. bool PeerListenCommand::execute() {
  108. if(e_->isHaltRequested() || e_->getRequestGroupMan()->downloadFinished()) {
  109. return true;
  110. }
  111. for(int i = 0; i < 3 && socket_->isReadable(0); ++i) {
  112. SocketHandle peerSocket;
  113. try {
  114. peerSocket.reset(socket_->acceptConnection());
  115. std::pair<std::string, uint16_t> peerInfo;
  116. peerSocket->getPeerInfo(peerInfo);
  117. peerSocket->setNonBlockingMode();
  118. SharedHandle<Peer> peer(new Peer(peerInfo.first, peerInfo.second, true));
  119. cuid_t cuid = e_->newCUID();
  120. Command* command =
  121. new ReceiverMSEHandshakeCommand(cuid, peer, e_, peerSocket);
  122. e_->addCommand(command);
  123. A2_LOG_DEBUG(fmt("Accepted the connection from %s:%u.",
  124. peer->getIPAddress().c_str(),
  125. peer->getPort()));
  126. A2_LOG_DEBUG(fmt("Added CUID#%lld to receive BitTorrent/MSE handshake.",
  127. cuid));
  128. } catch(RecoverableException& ex) {
  129. A2_LOG_DEBUG_EX(fmt(MSG_ACCEPT_FAILURE,
  130. getCuid()),
  131. ex);
  132. }
  133. }
  134. e_->addCommand(this);
  135. return false;
  136. }
  137. PeerListenCommand* PeerListenCommand::getInstance(DownloadEngine* e, int family)
  138. {
  139. if(family == AF_INET) {
  140. if(!instance_) {
  141. instance_ = new PeerListenCommand(e->newCUID(), e, family);
  142. }
  143. return instance_;
  144. } else if(family == AF_INET6) {
  145. if(!instance6_) {
  146. instance6_ = new PeerListenCommand(e->newCUID(), e, family);
  147. }
  148. return instance6_;
  149. } else {
  150. return 0;
  151. }
  152. }
  153. } // namespace aria2