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
|
2010-01-05 16:01:46 +00:00
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
2006-09-21 15:31:24 +00:00
|
|
|
*
|
|
|
|
* 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 --> */
|
2010-10-31 07:23:53 +00:00
|
|
|
#ifndef D_PEER_CONNECTION_H
|
|
|
|
#define D_PEER_CONNECTION_H
|
2006-03-21 14:12:51 +00:00
|
|
|
|
2006-05-26 15:28:19 +00:00
|
|
|
#include "common.h"
|
2010-03-04 16:24:03 +00:00
|
|
|
|
|
|
|
#include <unistd.h>
|
|
|
|
|
2008-02-08 15:53:45 +00:00
|
|
|
#include "SharedHandle.h"
|
2008-09-13 16:32:47 +00:00
|
|
|
#include "SocketBuffer.h"
|
2010-03-20 14:30:36 +00:00
|
|
|
#include "Command.h"
|
2008-02-08 15:53:45 +00:00
|
|
|
|
|
|
|
namespace aria2 {
|
|
|
|
|
2010-08-04 15:00:38 +00:00
|
|
|
class Peer;
|
2008-02-08 15:53:45 +00:00
|
|
|
class SocketCore;
|
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
|
|
|
class ARC4Encryptor;
|
2006-03-21 14:12:51 +00:00
|
|
|
|
2009-11-20 15:42:25 +00:00
|
|
|
// The maximum length of payload. Messages beyond that length are
|
|
|
|
// dropped.
|
|
|
|
#define MAX_PAYLOAD_LEN (16*1024+128)
|
2006-03-21 14:12:51 +00:00
|
|
|
|
|
|
|
class PeerConnection {
|
|
|
|
private:
|
2010-06-21 13:51:56 +00:00
|
|
|
cuid_t cuid_;
|
2010-08-04 15:00:38 +00:00
|
|
|
SharedHandle<Peer> peer_;
|
2010-06-21 13:51:56 +00:00
|
|
|
SharedHandle<SocketCore> socket_;
|
2006-03-21 14:12:51 +00:00
|
|
|
|
2012-02-08 16:51:42 +00:00
|
|
|
// Maximum payload length
|
|
|
|
size_t maxPayloadLength_;
|
2010-06-21 13:51:56 +00:00
|
|
|
unsigned char* resbuf_;
|
|
|
|
size_t resbufLength_;
|
|
|
|
size_t currentPayloadLength_;
|
|
|
|
unsigned char lenbuf_[4];
|
|
|
|
size_t lenbufLength_;
|
2006-03-21 14:12:51 +00:00
|
|
|
|
2010-06-21 13:51:56 +00:00
|
|
|
SocketBuffer socketBuffer_;
|
2008-09-13 16:32:47 +00:00
|
|
|
|
2010-06-21 13:51:56 +00:00
|
|
|
bool encryptionEnabled_;
|
|
|
|
SharedHandle<ARC4Encryptor> encryptor_;
|
2011-11-07 13:24:47 +00:00
|
|
|
SharedHandle<ARC4Encryptor> decryptor_;
|
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
|
|
|
|
2010-06-21 13:51:56 +00:00
|
|
|
bool prevPeek_;
|
2008-06-09 14:55:12 +00:00
|
|
|
|
2008-03-09 12:24:01 +00:00
|
|
|
void readData(unsigned char* data, size_t& length, bool encryption);
|
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-13 16:32:47 +00:00
|
|
|
ssize_t sendData(const unsigned char* data, size_t length, bool encryption);
|
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
|
|
|
|
2006-03-21 14:12:51 +00:00
|
|
|
public:
|
2010-08-04 15:00:38 +00:00
|
|
|
PeerConnection
|
|
|
|
(cuid_t cuid,
|
|
|
|
const SharedHandle<Peer>& peer,
|
|
|
|
const SharedHandle<SocketCore>& socket);
|
2008-02-08 15:53:45 +00:00
|
|
|
|
2006-03-21 14:12:51 +00:00
|
|
|
~PeerConnection();
|
2006-12-24 06:25:21 +00:00
|
|
|
|
2010-03-04 16:27:42 +00:00
|
|
|
// Pushes data into send buffer. After this call, this object gets
|
|
|
|
// ownership of data, so caller must not delete or alter it.
|
2010-03-04 16:24:03 +00:00
|
|
|
void pushBytes(unsigned char* data, size_t len);
|
|
|
|
|
|
|
|
void pushStr(const std::string& data);
|
|
|
|
|
2008-03-09 12:24:01 +00:00
|
|
|
bool receiveMessage(unsigned char* data, size_t& dataLength);
|
2006-03-21 14:12:51 +00:00
|
|
|
|
2006-06-24 16:09:14 +00:00
|
|
|
/**
|
|
|
|
* Returns true if a handshake message is fully received, otherwise returns
|
|
|
|
* false.
|
|
|
|
* In both cases, 'msg' is filled with received bytes and the filled length
|
|
|
|
* is assigned to 'length'.
|
|
|
|
*/
|
2010-06-12 12:29:11 +00:00
|
|
|
bool receiveHandshake
|
|
|
|
(unsigned char* data, size_t& dataLength, bool peek = false);
|
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
|
|
|
|
|
|
|
void enableEncryption(const SharedHandle<ARC4Encryptor>& encryptor,
|
2011-11-07 13:24:47 +00:00
|
|
|
const SharedHandle<ARC4Encryptor>& decryptor);
|
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
|
|
|
|
|
|
|
void presetBuffer(const unsigned char* data, size_t length);
|
2008-09-28 10:55:17 +00:00
|
|
|
|
|
|
|
bool sendBufferIsEmpty() const;
|
|
|
|
|
|
|
|
ssize_t sendPendingData();
|
2010-03-04 16:24:03 +00:00
|
|
|
|
|
|
|
const unsigned char* getBuffer() const
|
|
|
|
{
|
2010-06-21 13:51:56 +00:00
|
|
|
return resbuf_;
|
2010-03-04 16:24:03 +00:00
|
|
|
}
|
|
|
|
|
2011-01-08 08:32:16 +00:00
|
|
|
size_t getBufferLength() const
|
|
|
|
{
|
|
|
|
return resbufLength_;
|
|
|
|
}
|
|
|
|
|
2010-11-14 07:17:55 +00:00
|
|
|
unsigned char* detachBuffer();
|
2012-02-08 16:51:42 +00:00
|
|
|
|
|
|
|
// Reserves buffer at least minSize. Reallocate memory if current
|
|
|
|
// buffer length < minSize
|
|
|
|
void reserveBuffer(size_t minSize);
|
|
|
|
|
|
|
|
size_t getMaxPayloadLength()
|
|
|
|
{
|
|
|
|
return maxPayloadLength_;
|
|
|
|
}
|
2006-03-21 14:12:51 +00:00
|
|
|
};
|
|
|
|
|
2006-12-24 06:25:21 +00:00
|
|
|
typedef SharedHandle<PeerConnection> PeerConnectionHandle;
|
2008-02-08 15:53:45 +00:00
|
|
|
|
|
|
|
} // namespace aria2
|
|
|
|
|
2010-10-31 07:23:53 +00:00
|
|
|
#endif // D_PEER_CONNECTION_H
|