2006-03-21 14:12:51 +00:00
|
|
|
/* <!-- copyright */
|
|
|
|
/*
|
2006-09-21 15:31:24 +00:00
|
|
|
* aria2 - The high speed download utility
|
2006-03-21 14:12:51 +00:00
|
|
|
*
|
|
|
|
* Copyright (C) 2006 Tatsuhiro Tsujikawa
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
2006-09-21 15:31:24 +00:00
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*
|
|
|
|
* In addition, as a special exception, the copyright holders give
|
|
|
|
* permission to link the code of portions of this program with the
|
|
|
|
* OpenSSL library under certain conditions as described in each
|
|
|
|
* individual source file, and distribute linked combinations
|
|
|
|
* including the two.
|
|
|
|
* You must obey the GNU General Public License in all respects
|
|
|
|
* for all of the code used other than OpenSSL. If you modify
|
|
|
|
* file(s) with this exception, you may extend this exception to your
|
|
|
|
* version of the file(s), but you are not obligated to do so. If you
|
|
|
|
* do not wish to do so, delete this exception statement from your
|
|
|
|
* version. If you delete this exception statement from all source
|
|
|
|
* files in the program, then also delete it here.
|
2006-03-21 14:12:51 +00:00
|
|
|
*/
|
|
|
|
/* copyright --> */
|
|
|
|
#include "PeerListenCommand.h"
|
2008-11-03 07:49:13 +00:00
|
|
|
|
|
|
|
#include <utility>
|
|
|
|
|
2007-10-11 16:58:24 +00:00
|
|
|
#include "DownloadEngine.h"
|
|
|
|
#include "Peer.h"
|
|
|
|
#include "RequestGroupMan.h"
|
2007-05-20 13:51:52 +00:00
|
|
|
#include "RecoverableException.h"
|
2007-07-20 17:06:21 +00:00
|
|
|
#include "message.h"
|
2008-02-16 Tatsuhiro Tsujikawa <tujikawa at rednoah dot com>
Added Message Stream Encryption(MSE) support.
Currently, aria2 accepts incoming connections with Obfuscation Header
and legacy BitTorrent Header and establishes connections with
Obfuscation Header first and if failed then retry with legacy
BitTorrent header. If plain text and ARC4 is provided, aria2 always
choose ARC4. The new option to change the default behavior is planned.
For tracker extension, "supportcrypto=1" is added statically.
* src/PeerInitiateConnectionCommand.{h, cc}
* src/PeerConnection.{h, cc}
* src/HandleRegistry.h
* src/SocketCore.h
* src/PeerReceiveHandshakeCommand.{h, cc}
* src/BtRegistry.{h, cc}
* src/PeerListenCommand.cc
* src/InitiatorMSEHandshakeCommand.{h, cc}
* src/ReceiverMSEHandshakeCommand.{h, cc}
* src/MSEHandshake.{h, cc}
* src/ARC4Encryptor.h
* src/ARC4Decryptor.h
* src/LibgcryptARC4Encryptor.h
* src/LibgcryptARC4Decryptor.h
* src/LibgcryptARC4Context.h
* src/LibsslARC4Encryptor.h
* src/LibsslARC4Decryptor.h
* src/LibsslARC4Context.h
* src/DHKeyExchange.h
* src/LibgcryptDHKeyExchange.h
* src/LibsslDHKeyExchange.h
* src/DefaultBtAnnounce.cc: Just added "supportcrypto=1" parameter.
* test/DefaultBtAnnounceTest.cc
* test/ARC4Test.cc
* test/DHKeyExchangeTest.cc
Removed prepareForRetry() because it is not used.
* src/PeerAbstractCommand.{h, cc}
* src/PeerInteractionCommand.{h, cc}
* src/PeerInitiateConnectionCommand.{h, cc}
2008-02-17 15:56:47 +00:00
|
|
|
#include "ReceiverMSEHandshakeCommand.h"
|
2008-02-08 15:53:45 +00:00
|
|
|
#include "Logger.h"
|
|
|
|
#include "Socket.h"
|
|
|
|
|
|
|
|
namespace aria2 {
|
2006-03-21 14:12:51 +00:00
|
|
|
|
2008-03-09 12:24:01 +00:00
|
|
|
unsigned int PeerListenCommand::__numInstance = 0;
|
2007-11-04 12:26:12 +00:00
|
|
|
|
|
|
|
PeerListenCommand* PeerListenCommand::__instance = 0;
|
|
|
|
|
2007-10-11 16:58:24 +00:00
|
|
|
PeerListenCommand::PeerListenCommand(int32_t cuid, DownloadEngine* e):
|
|
|
|
Command(cuid),
|
|
|
|
e(e),
|
2007-11-04 12:26:12 +00:00
|
|
|
_lowestSpeedLimit(20*1024)
|
|
|
|
{
|
|
|
|
++__numInstance;
|
|
|
|
}
|
2006-03-21 14:12:51 +00:00
|
|
|
|
2007-11-04 12:26:12 +00:00
|
|
|
PeerListenCommand::~PeerListenCommand()
|
|
|
|
{
|
|
|
|
--__numInstance;
|
|
|
|
}
|
2006-03-21 14:12:51 +00:00
|
|
|
|
2008-03-09 14:09:17 +00:00
|
|
|
bool PeerListenCommand::bindPort(uint16_t& port, IntSequence& seq)
|
2007-11-04 12:26:12 +00:00
|
|
|
{
|
2008-04-20 00:50:22 +00:00
|
|
|
socket.reset(new SocketCore());
|
2007-11-21 16:14:40 +00:00
|
|
|
while(seq.hasNext()) {
|
2008-03-09 14:09:17 +00:00
|
|
|
int sport = seq.next();
|
|
|
|
if(!(0 < sport && sport <= UINT16_MAX)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
port = sport;
|
2006-03-21 14:12:51 +00:00
|
|
|
try {
|
2008-02-01 Tatsuhiro Tsujikawa <tujikawa at rednoah dot com>
Added DHT functionality, compatible with mainline.
DHT is disabled by default. To enable it, give --enable-dht to
aria2c.
You may need to specify entry point to DHT network using
--dht-entry-point. DHT uses UDP port to listen incoming message.
Use --dht-listen-port to specify port number. Make sure that
your
firewall configuration can pass through UDP traffic to the port.
The routing table is saved in $HOME/.aria2/dht.dat.
* src/DHT*
* src/BNode.{h, cc}
* src/PeerInteractionCommand.cc: enable DHT functionality for a
particular torrent.
* src/Data.cc: Rewritten ctor.
* src/OptionHandlerFactory.cc: Added --enable-dht,
--dht-listen-port,
--dht-entry-point.
* src/DefaultBtInteractive.cc: Send port message if dht is
enabled.
* src/RequestGroup.cc: Initialize DHT functionality. When
download
ends, remove BtContext from DHTPeerAnnounceStorage.
* src/BtPortMessage.{h, cc}: Rewritten.
* src/message.h
* src/OptionHandlerImpl.cc
* src/option_processing.cc: Added --enable-dht,
--dht-listen-port,
--dht-entry-point.
* src/Dictionary.{h, cc} (remove): New function.
* src/prefs.h
* src/DefaultBtMessageFactory.h
* src/BtHandshakeMessage.cc
* src/ActivePeerConnectionCommand.cc
* src/SocketCore.{h, cc}: Added datagram socket support.
* src/DefaultBtMessageFactory.cc
* src/BtSetup.cc: Add BtContext to DHTPeerAnnounceStorage here.
Create DHT commands.
* src/BtMessageFactory.h
* src/PeerMessageUtil.{h, cc}
2008-02-01 17:36:33 +00:00
|
|
|
socket->bind(port);
|
|
|
|
socket->beginListen();
|
2008-02-19 13:36:39 +00:00
|
|
|
socket->setNonBlockingMode();
|
2008-03-09 14:09:17 +00:00
|
|
|
logger->info(MSG_LISTENING_PORT, cuid, port);
|
|
|
|
return true;
|
2008-04-27 02:22:14 +00:00
|
|
|
} catch(RecoverableException& ex) {
|
2008-03-09 14:09:17 +00:00
|
|
|
logger->error(MSG_BIND_FAILURE, ex, cuid, port);
|
2006-07-19 17:07:45 +00:00
|
|
|
socket->closeConnection();
|
2006-03-21 14:12:51 +00:00
|
|
|
}
|
|
|
|
}
|
2008-03-09 14:09:17 +00:00
|
|
|
return false;
|
2006-03-21 14:12:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool PeerListenCommand::execute() {
|
2008-02-23 15:37:47 +00:00
|
|
|
if(e->isHaltRequested() || e->_requestGroupMan->downloadFinished()) {
|
2006-05-24 15:18:58 +00:00
|
|
|
return true;
|
|
|
|
}
|
2008-03-09 12:24:01 +00:00
|
|
|
for(int i = 0; i < 3 && socket->isReadable(0); i++) {
|
2006-07-19 17:07:45 +00:00
|
|
|
SocketHandle peerSocket;
|
|
|
|
try {
|
2008-04-20 00:50:22 +00:00
|
|
|
peerSocket.reset(socket->acceptConnection());
|
2008-03-09 12:24:01 +00:00
|
|
|
std::pair<std::string, uint16_t> peerInfo;
|
2006-07-19 17:07:45 +00:00
|
|
|
peerSocket->getPeerInfo(peerInfo);
|
2007-06-04 12:33:29 +00:00
|
|
|
|
2008-09-28 11:13:20 +00:00
|
|
|
peerSocket->setNonBlockingMode();
|
2008-02-16 Tatsuhiro Tsujikawa <tujikawa at rednoah dot com>
Added Message Stream Encryption(MSE) support.
Currently, aria2 accepts incoming connections with Obfuscation Header
and legacy BitTorrent Header and establishes connections with
Obfuscation Header first and if failed then retry with legacy
BitTorrent header. If plain text and ARC4 is provided, aria2 always
choose ARC4. The new option to change the default behavior is planned.
For tracker extension, "supportcrypto=1" is added statically.
* src/PeerInitiateConnectionCommand.{h, cc}
* src/PeerConnection.{h, cc}
* src/HandleRegistry.h
* src/SocketCore.h
* src/PeerReceiveHandshakeCommand.{h, cc}
* src/BtRegistry.{h, cc}
* src/PeerListenCommand.cc
* src/InitiatorMSEHandshakeCommand.{h, cc}
* src/ReceiverMSEHandshakeCommand.{h, cc}
* src/MSEHandshake.{h, cc}
* src/ARC4Encryptor.h
* src/ARC4Decryptor.h
* src/LibgcryptARC4Encryptor.h
* src/LibgcryptARC4Decryptor.h
* src/LibgcryptARC4Context.h
* src/LibsslARC4Encryptor.h
* src/LibsslARC4Decryptor.h
* src/LibsslARC4Context.h
* src/DHKeyExchange.h
* src/LibgcryptDHKeyExchange.h
* src/LibsslDHKeyExchange.h
* src/DefaultBtAnnounce.cc: Just added "supportcrypto=1" parameter.
* test/DefaultBtAnnounceTest.cc
* test/ARC4Test.cc
* test/DHKeyExchangeTest.cc
Removed prepareForRetry() because it is not used.
* src/PeerAbstractCommand.{h, cc}
* src/PeerInteractionCommand.{h, cc}
* src/PeerInitiateConnectionCommand.{h, cc}
2008-02-17 15:56:47 +00:00
|
|
|
|
2008-09-02 11:22:47 +00:00
|
|
|
PeerHandle peer(new Peer(peerInfo.first, peerInfo.second, true));
|
2008-11-03 07:49:13 +00:00
|
|
|
int32_t cuid = e->newCUID();
|
2008-02-16 Tatsuhiro Tsujikawa <tujikawa at rednoah dot com>
Added Message Stream Encryption(MSE) support.
Currently, aria2 accepts incoming connections with Obfuscation Header
and legacy BitTorrent Header and establishes connections with
Obfuscation Header first and if failed then retry with legacy
BitTorrent header. If plain text and ARC4 is provided, aria2 always
choose ARC4. The new option to change the default behavior is planned.
For tracker extension, "supportcrypto=1" is added statically.
* src/PeerInitiateConnectionCommand.{h, cc}
* src/PeerConnection.{h, cc}
* src/HandleRegistry.h
* src/SocketCore.h
* src/PeerReceiveHandshakeCommand.{h, cc}
* src/BtRegistry.{h, cc}
* src/PeerListenCommand.cc
* src/InitiatorMSEHandshakeCommand.{h, cc}
* src/ReceiverMSEHandshakeCommand.{h, cc}
* src/MSEHandshake.{h, cc}
* src/ARC4Encryptor.h
* src/ARC4Decryptor.h
* src/LibgcryptARC4Encryptor.h
* src/LibgcryptARC4Decryptor.h
* src/LibgcryptARC4Context.h
* src/LibsslARC4Encryptor.h
* src/LibsslARC4Decryptor.h
* src/LibsslARC4Context.h
* src/DHKeyExchange.h
* src/LibgcryptDHKeyExchange.h
* src/LibsslDHKeyExchange.h
* src/DefaultBtAnnounce.cc: Just added "supportcrypto=1" parameter.
* test/DefaultBtAnnounceTest.cc
* test/ARC4Test.cc
* test/DHKeyExchangeTest.cc
Removed prepareForRetry() because it is not used.
* src/PeerAbstractCommand.{h, cc}
* src/PeerInteractionCommand.{h, cc}
* src/PeerInitiateConnectionCommand.{h, cc}
2008-02-17 15:56:47 +00:00
|
|
|
Command* command =
|
|
|
|
new ReceiverMSEHandshakeCommand(cuid, peer, e, peerSocket);
|
2007-10-11 16:58:24 +00:00
|
|
|
e->commands.push_back(command);
|
2007-12-25 12:34:23 +00:00
|
|
|
logger->debug("Accepted the connection from %s:%u.",
|
2007-10-11 16:58:24 +00:00
|
|
|
peer->ipaddr.c_str(),
|
|
|
|
peer->port);
|
2008-02-16 Tatsuhiro Tsujikawa <tujikawa at rednoah dot com>
Added Message Stream Encryption(MSE) support.
Currently, aria2 accepts incoming connections with Obfuscation Header
and legacy BitTorrent Header and establishes connections with
Obfuscation Header first and if failed then retry with legacy
BitTorrent header. If plain text and ARC4 is provided, aria2 always
choose ARC4. The new option to change the default behavior is planned.
For tracker extension, "supportcrypto=1" is added statically.
* src/PeerInitiateConnectionCommand.{h, cc}
* src/PeerConnection.{h, cc}
* src/HandleRegistry.h
* src/SocketCore.h
* src/PeerReceiveHandshakeCommand.{h, cc}
* src/BtRegistry.{h, cc}
* src/PeerListenCommand.cc
* src/InitiatorMSEHandshakeCommand.{h, cc}
* src/ReceiverMSEHandshakeCommand.{h, cc}
* src/MSEHandshake.{h, cc}
* src/ARC4Encryptor.h
* src/ARC4Decryptor.h
* src/LibgcryptARC4Encryptor.h
* src/LibgcryptARC4Decryptor.h
* src/LibgcryptARC4Context.h
* src/LibsslARC4Encryptor.h
* src/LibsslARC4Decryptor.h
* src/LibsslARC4Context.h
* src/DHKeyExchange.h
* src/LibgcryptDHKeyExchange.h
* src/LibsslDHKeyExchange.h
* src/DefaultBtAnnounce.cc: Just added "supportcrypto=1" parameter.
* test/DefaultBtAnnounceTest.cc
* test/ARC4Test.cc
* test/DHKeyExchangeTest.cc
Removed prepareForRetry() because it is not used.
* src/PeerAbstractCommand.{h, cc}
* src/PeerInteractionCommand.{h, cc}
* src/PeerInitiateConnectionCommand.{h, cc}
2008-02-17 15:56:47 +00:00
|
|
|
logger->debug("Added CUID#%d to receive BitTorrent/MSE handshake.", cuid);
|
2008-04-27 02:22:14 +00:00
|
|
|
} catch(RecoverableException& ex) {
|
2007-07-20 17:06:21 +00:00
|
|
|
logger->debug(MSG_ACCEPT_FAILURE, ex, cuid);
|
2006-07-19 17:07:45 +00:00
|
|
|
}
|
2006-03-21 14:12:51 +00:00
|
|
|
}
|
2006-05-09 15:54:14 +00:00
|
|
|
e->commands.push_back(this);
|
2006-03-21 14:12:51 +00:00
|
|
|
return false;
|
|
|
|
}
|
2007-11-04 12:26:12 +00:00
|
|
|
|
|
|
|
PeerListenCommand* PeerListenCommand::getInstance(DownloadEngine* e)
|
|
|
|
{
|
|
|
|
if(__numInstance == 0) {
|
2008-11-03 07:49:13 +00:00
|
|
|
__instance = new PeerListenCommand(e->newCUID(), e);
|
2007-11-04 12:26:12 +00:00
|
|
|
}
|
|
|
|
return __instance;
|
|
|
|
}
|
2008-02-08 15:53:45 +00:00
|
|
|
|
|
|
|
} // namespace aria2
|