PeerListenCommand.cc 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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 "Socket.h"
  46. #include "SimpleRandomizer.h"
  47. #include "FileEntry.h"
  48. #include "util.h"
  49. #include "ServerStatMan.h"
  50. #include "FileAllocationEntry.h"
  51. #include "CheckIntegrityEntry.h"
  52. namespace aria2 {
  53. unsigned int PeerListenCommand::numInstance_ = 0;
  54. PeerListenCommand* PeerListenCommand::instance_ = 0;
  55. PeerListenCommand* PeerListenCommand::instance6_ = 0;
  56. PeerListenCommand::PeerListenCommand
  57. (cuid_t cuid, DownloadEngine* e, 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. getLogger()->notice("IPv%d BitTorrent: listening to port %d",
  86. family_ == AF_INET?4:6, port);
  87. return true;
  88. } catch(RecoverableException& ex) {
  89. getLogger()->error(MSG_BIND_FAILURE, ex,
  90. util::itos(getCuid()).c_str(), port);
  91. socket_->closeConnection();
  92. }
  93. }
  94. return false;
  95. }
  96. uint16_t PeerListenCommand::getPort() const
  97. {
  98. if(socket_.isNull()) {
  99. return 0;
  100. } else {
  101. std::pair<std::string, uint16_t> addr;
  102. socket_->getAddrInfo(addr);
  103. return addr.second;
  104. }
  105. }
  106. bool PeerListenCommand::execute() {
  107. if(e_->isHaltRequested() || e_->getRequestGroupMan()->downloadFinished()) {
  108. return true;
  109. }
  110. for(int i = 0; i < 3 && socket_->isReadable(0); ++i) {
  111. SocketHandle peerSocket;
  112. try {
  113. peerSocket.reset(socket_->acceptConnection());
  114. std::pair<std::string, uint16_t> peerInfo;
  115. peerSocket->getPeerInfo(peerInfo);
  116. peerSocket->setNonBlockingMode();
  117. SharedHandle<Peer> peer(new Peer(peerInfo.first, peerInfo.second, true));
  118. cuid_t cuid = e_->newCUID();
  119. Command* command =
  120. new ReceiverMSEHandshakeCommand(cuid, peer, e_, peerSocket);
  121. e_->addCommand(command);
  122. if(getLogger()->debug()) {
  123. getLogger()->debug("Accepted the connection from %s:%u.",
  124. peer->getIPAddress().c_str(),
  125. peer->getPort());
  126. getLogger()->debug("Added CUID#%s to receive BitTorrent/MSE handshake.",
  127. util::itos(cuid).c_str());
  128. }
  129. } catch(RecoverableException& ex) {
  130. getLogger()->debug(MSG_ACCEPT_FAILURE, ex, util::itos(getCuid()).c_str());
  131. }
  132. }
  133. e_->addCommand(this);
  134. return false;
  135. }
  136. PeerListenCommand* PeerListenCommand::getInstance(DownloadEngine* e, int family)
  137. {
  138. if(family == AF_INET) {
  139. if(!instance_) {
  140. instance_ = new PeerListenCommand(e->newCUID(), e, family);
  141. }
  142. return instance_;
  143. } else if(family == AF_INET6) {
  144. if(!instance6_) {
  145. instance6_ = new PeerListenCommand(e->newCUID(), e, family);
  146. }
  147. return instance6_;
  148. } else {
  149. return 0;
  150. }
  151. }
  152. } // namespace aria2