PeerListenCommand.cc 4.5 KB

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