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>
|
2013-06-21 16:10:38 +00:00
|
|
|
#include <memory>
|
2010-03-04 16:24:03 +00:00
|
|
|
|
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
|
|
|
|
2012-06-25 12:29:48 +00:00
|
|
|
// The maximum length of buffer. If the message length (including 4
|
|
|
|
// bytes length and payload length) is larger than this value, it is
|
2009-11-20 15:42:25 +00:00
|
|
|
// dropped.
|
2012-06-25 12:29:48 +00:00
|
|
|
#define MAX_BUFFER_CAPACITY (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_;
|
2013-06-21 16:10:38 +00:00
|
|
|
std::shared_ptr<Peer> peer_;
|
|
|
|
std::shared_ptr<SocketCore> socket_;
|
2006-03-21 14:12:51 +00:00
|
|
|
|
2012-06-25 12:29:48 +00:00
|
|
|
int msgState_;
|
|
|
|
// The capacity of the buffer resbuf_
|
|
|
|
size_t bufferCapacity_;
|
|
|
|
// The internal buffer of incoming handshakes and messages
|
2010-06-21 13:51:56 +00:00
|
|
|
unsigned char* resbuf_;
|
2012-06-25 12:29:48 +00:00
|
|
|
// The number of bytes written in resbuf_
|
2010-06-21 13:51:56 +00:00
|
|
|
size_t resbufLength_;
|
2012-06-25 12:29:48 +00:00
|
|
|
// The length of message (not handshake) currently receiving
|
|
|
|
uint32_t currentPayloadLength_;
|
|
|
|
// The number of bytes processed in resbuf_
|
|
|
|
size_t resbufOffset_;
|
|
|
|
// The offset in resbuf_ where the 4 bytes message length begins
|
|
|
|
size_t msgOffset_;
|
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_;
|
2013-07-04 12:13:14 +00:00
|
|
|
std::unique_ptr<ARC4Encryptor> encryptor_;
|
|
|
|
std::unique_ptr<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,
|
2013-06-21 16:10:38 +00:00
|
|
|
const std::shared_ptr<Peer>& peer,
|
|
|
|
const std::shared_ptr<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.
|
2013-01-11 05:20:34 +00:00
|
|
|
void pushBytes(unsigned char* data, size_t len,
|
2013-07-01 12:59:54 +00:00
|
|
|
std::unique_ptr<ProgressUpdate> progressUpdate =
|
|
|
|
std::unique_ptr<ProgressUpdate>{});
|
2010-03-04 16:24:03 +00:00
|
|
|
|
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
|
|
|
|
2013-07-04 12:13:14 +00:00
|
|
|
void enableEncryption(std::unique_ptr<ARC4Encryptor> encryptor,
|
|
|
|
std::unique_ptr<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;
|
2012-09-28 14:27:46 +00:00
|
|
|
|
2013-01-11 09:46:36 +00:00
|
|
|
size_t getBufferEntrySize() const;
|
|
|
|
|
2008-09-28 10:55:17 +00:00
|
|
|
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_;
|
|
|
|
}
|
|
|
|
|
2012-06-25 12:29:48 +00:00
|
|
|
// Returns the pointer to the message in wire format. This method
|
|
|
|
// must be called after receiveMessage() returned true.
|
|
|
|
const unsigned char* getMsgPayloadBuffer() const;
|
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);
|
|
|
|
|
2012-06-25 12:29:48 +00:00
|
|
|
size_t getBufferCapacity()
|
2012-02-08 16:51:42 +00:00
|
|
|
{
|
2012-06-25 12:29:48 +00:00
|
|
|
return bufferCapacity_;
|
2012-02-08 16:51:42 +00:00
|
|
|
}
|
2006-03-21 14:12:51 +00:00
|
|
|
};
|
|
|
|
|
2008-02-08 15:53:45 +00:00
|
|
|
} // namespace aria2
|
|
|
|
|
2010-10-31 07:23:53 +00:00
|
|
|
#endif // D_PEER_CONNECTION_H
|