mirror of https://github.com/aria2/aria2
2010-06-12 Tatsuhiro Tsujikawa <t-tujikawa@users.sourceforge.net>
Renamed member variables. * src/PeerConnection.cc * src/PeerConnection.hpull/1/head
parent
26faa70b3b
commit
b3b955b0c4
|
@ -1,3 +1,9 @@
|
||||||
|
2010-06-12 Tatsuhiro Tsujikawa <t-tujikawa@users.sourceforge.net>
|
||||||
|
|
||||||
|
Renamed member variables.
|
||||||
|
* src/PeerConnection.cc
|
||||||
|
* src/PeerConnection.h
|
||||||
|
|
||||||
2010-06-12 Tatsuhiro Tsujikawa <t-tujikawa@users.sourceforge.net>
|
2010-06-12 Tatsuhiro Tsujikawa <t-tujikawa@users.sourceforge.net>
|
||||||
|
|
||||||
Renamed member variables.
|
Renamed member variables.
|
||||||
|
|
|
@ -54,13 +54,13 @@ namespace aria2 {
|
||||||
|
|
||||||
PeerConnection::PeerConnection(cuid_t cuid, const SocketHandle& socket)
|
PeerConnection::PeerConnection(cuid_t cuid, const SocketHandle& socket)
|
||||||
|
|
||||||
:cuid(cuid),
|
:_cuid(cuid),
|
||||||
socket(socket),
|
_socket(socket),
|
||||||
logger(LogFactory::getInstance()),
|
_logger(LogFactory::getInstance()),
|
||||||
resbuf(new unsigned char[MAX_PAYLOAD_LEN]),
|
_resbuf(new unsigned char[MAX_PAYLOAD_LEN]),
|
||||||
resbufLength(0),
|
_resbufLength(0),
|
||||||
currentPayloadLength(0),
|
_currentPayloadLength(0),
|
||||||
lenbufLength(0),
|
_lenbufLength(0),
|
||||||
_socketBuffer(socket),
|
_socketBuffer(socket),
|
||||||
_encryptionEnabled(false),
|
_encryptionEnabled(false),
|
||||||
_prevPeek(false)
|
_prevPeek(false)
|
||||||
|
@ -68,7 +68,7 @@ PeerConnection::PeerConnection(cuid_t cuid, const SocketHandle& socket)
|
||||||
|
|
||||||
PeerConnection::~PeerConnection()
|
PeerConnection::~PeerConnection()
|
||||||
{
|
{
|
||||||
delete [] resbuf;
|
delete [] _resbuf;
|
||||||
}
|
}
|
||||||
|
|
||||||
void PeerConnection::pushStr(const std::string& data)
|
void PeerConnection::pushStr(const std::string& data)
|
||||||
|
@ -108,141 +108,143 @@ void PeerConnection::pushBytes(unsigned char* data, size_t len)
|
||||||
}
|
}
|
||||||
|
|
||||||
bool PeerConnection::receiveMessage(unsigned char* data, size_t& dataLength) {
|
bool PeerConnection::receiveMessage(unsigned char* data, size_t& dataLength) {
|
||||||
if(resbufLength == 0 && 4 > lenbufLength) {
|
if(_resbufLength == 0 && 4 > _lenbufLength) {
|
||||||
if(!socket->isReadable(0)) {
|
if(!_socket->isReadable(0)) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
// read payload size, 32bit unsigned integer
|
// read payload size, 32bit unsigned integer
|
||||||
size_t remaining = 4-lenbufLength;
|
size_t remaining = 4-_lenbufLength;
|
||||||
size_t temp = remaining;
|
size_t temp = remaining;
|
||||||
readData(lenbuf+lenbufLength, remaining, _encryptionEnabled);
|
readData(_lenbuf+_lenbufLength, remaining, _encryptionEnabled);
|
||||||
if(remaining == 0) {
|
if(remaining == 0) {
|
||||||
if(socket->wantRead() || socket->wantWrite()) {
|
if(_socket->wantRead() || _socket->wantWrite()) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
// we got EOF
|
// we got EOF
|
||||||
if(logger->debug()) {
|
if(_logger->debug()) {
|
||||||
logger->debug("CUID#%s - In PeerConnection::receiveMessage(),"
|
_logger->debug("CUID#%s - In PeerConnection::receiveMessage(),"
|
||||||
" remain=%lu",
|
" remain=%lu",
|
||||||
util::itos(cuid).c_str(),
|
util::itos(_cuid).c_str(),
|
||||||
static_cast<unsigned long>(temp));
|
static_cast<unsigned long>(temp));
|
||||||
}
|
}
|
||||||
throw DL_ABORT_EX(EX_EOF_FROM_PEER);
|
throw DL_ABORT_EX(EX_EOF_FROM_PEER);
|
||||||
}
|
}
|
||||||
lenbufLength += remaining;
|
_lenbufLength += remaining;
|
||||||
if(4 > lenbufLength) {
|
if(4 > _lenbufLength) {
|
||||||
// still 4-lenbufLength bytes to go
|
// still 4-_lenbufLength bytes to go
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
uint32_t payloadLength;
|
uint32_t payloadLength;
|
||||||
memcpy(&payloadLength, lenbuf, sizeof(payloadLength));
|
memcpy(&payloadLength, _lenbuf, sizeof(payloadLength));
|
||||||
payloadLength = ntohl(payloadLength);
|
payloadLength = ntohl(payloadLength);
|
||||||
if(payloadLength > MAX_PAYLOAD_LEN) {
|
if(payloadLength > MAX_PAYLOAD_LEN) {
|
||||||
throw DL_ABORT_EX(StringFormat(EX_TOO_LONG_PAYLOAD, payloadLength).str());
|
throw DL_ABORT_EX(StringFormat(EX_TOO_LONG_PAYLOAD, payloadLength).str());
|
||||||
}
|
}
|
||||||
currentPayloadLength = payloadLength;
|
_currentPayloadLength = payloadLength;
|
||||||
}
|
}
|
||||||
if(!socket->isReadable(0)) {
|
if(!_socket->isReadable(0)) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
// we have currentPayloadLen-resbufLen bytes to read
|
// we have currentPayloadLen-resbufLen bytes to read
|
||||||
size_t remaining = currentPayloadLength-resbufLength;
|
size_t remaining = _currentPayloadLength-_resbufLength;
|
||||||
size_t temp = remaining;
|
size_t temp = remaining;
|
||||||
if(remaining > 0) {
|
if(remaining > 0) {
|
||||||
readData(resbuf+resbufLength, remaining, _encryptionEnabled);
|
readData(_resbuf+_resbufLength, remaining, _encryptionEnabled);
|
||||||
if(remaining == 0) {
|
if(remaining == 0) {
|
||||||
if(socket->wantRead() || socket->wantWrite()) {
|
if(_socket->wantRead() || _socket->wantWrite()) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
// we got EOF
|
// we got EOF
|
||||||
if(logger->debug()) {
|
if(_logger->debug()) {
|
||||||
logger->debug("CUID#%s - In PeerConnection::receiveMessage(),"
|
_logger->debug("CUID#%s - In PeerConnection::receiveMessage(),"
|
||||||
" payloadlen=%lu, remaining=%lu",
|
" payloadlen=%lu, remaining=%lu",
|
||||||
util::itos(cuid).c_str(),
|
util::itos(_cuid).c_str(),
|
||||||
static_cast<unsigned long>(currentPayloadLength),
|
static_cast<unsigned long>(_currentPayloadLength),
|
||||||
static_cast<unsigned long>(temp));
|
static_cast<unsigned long>(temp));
|
||||||
}
|
}
|
||||||
throw DL_ABORT_EX(EX_EOF_FROM_PEER);
|
throw DL_ABORT_EX(EX_EOF_FROM_PEER);
|
||||||
}
|
}
|
||||||
resbufLength += remaining;
|
_resbufLength += remaining;
|
||||||
if(currentPayloadLength > resbufLength) {
|
if(_currentPayloadLength > _resbufLength) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// we got whole payload.
|
// we got whole payload.
|
||||||
resbufLength = 0;
|
_resbufLength = 0;
|
||||||
lenbufLength = 0;
|
_lenbufLength = 0;
|
||||||
if(data) {
|
if(data) {
|
||||||
memcpy(data, resbuf, currentPayloadLength);
|
memcpy(data, _resbuf, _currentPayloadLength);
|
||||||
}
|
}
|
||||||
dataLength = currentPayloadLength;
|
dataLength = _currentPayloadLength;
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool PeerConnection::receiveHandshake(unsigned char* data, size_t& dataLength,
|
bool PeerConnection::receiveHandshake(unsigned char* data, size_t& dataLength,
|
||||||
bool peek) {
|
bool peek) {
|
||||||
assert(BtHandshakeMessage::MESSAGE_LENGTH >= resbufLength);
|
assert(BtHandshakeMessage::MESSAGE_LENGTH >= _resbufLength);
|
||||||
bool retval = true;
|
bool retval = true;
|
||||||
if(_prevPeek && !peek && resbufLength) {
|
if(_prevPeek && !peek && _resbufLength) {
|
||||||
// We have data in previous peek.
|
// We have data in previous peek.
|
||||||
// There is a chance that socket is readable because of EOF, for example,
|
// There is a chance that socket is readable because of EOF, for example,
|
||||||
// official bttrack shutdowns socket after sending first 48 bytes of
|
// official bttrack shutdowns socket after sending first 48 bytes of
|
||||||
// handshake in its NAT checking.
|
// handshake in its NAT checking.
|
||||||
// So if there are data in resbuf, return it without checking socket
|
// So if there are data in _resbuf, return it without checking socket
|
||||||
// status.
|
// status.
|
||||||
_prevPeek = false;
|
_prevPeek = false;
|
||||||
retval = BtHandshakeMessage::MESSAGE_LENGTH <= resbufLength;
|
retval = BtHandshakeMessage::MESSAGE_LENGTH <= _resbufLength;
|
||||||
} else {
|
} else {
|
||||||
_prevPeek = peek;
|
_prevPeek = peek;
|
||||||
size_t remaining = BtHandshakeMessage::MESSAGE_LENGTH-resbufLength;
|
size_t remaining = BtHandshakeMessage::MESSAGE_LENGTH-_resbufLength;
|
||||||
if(remaining > 0 && !socket->isReadable(0)) {
|
if(remaining > 0 && !_socket->isReadable(0)) {
|
||||||
dataLength = 0;
|
dataLength = 0;
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
if(remaining > 0) {
|
if(remaining > 0) {
|
||||||
size_t temp = remaining;
|
size_t temp = remaining;
|
||||||
readData(resbuf+resbufLength, remaining, _encryptionEnabled);
|
readData(_resbuf+_resbufLength, remaining, _encryptionEnabled);
|
||||||
if(remaining == 0) {
|
if(remaining == 0) {
|
||||||
if(socket->wantRead() || socket->wantWrite()) {
|
if(_socket->wantRead() || _socket->wantWrite()) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
// we got EOF
|
// we got EOF
|
||||||
if(logger->debug()) {
|
if(_logger->debug()) {
|
||||||
logger->debug
|
_logger->debug
|
||||||
("CUID#%s - In PeerConnection::receiveHandshake(), remain=%lu",
|
("CUID#%s - In PeerConnection::receiveHandshake(), remain=%lu",
|
||||||
util::itos(cuid).c_str(), static_cast<unsigned long>(temp));
|
util::itos(_cuid).c_str(), static_cast<unsigned long>(temp));
|
||||||
}
|
}
|
||||||
throw DL_ABORT_EX(EX_EOF_FROM_PEER);
|
throw DL_ABORT_EX(EX_EOF_FROM_PEER);
|
||||||
}
|
}
|
||||||
resbufLength += remaining;
|
_resbufLength += remaining;
|
||||||
if(BtHandshakeMessage::MESSAGE_LENGTH > resbufLength) {
|
if(BtHandshakeMessage::MESSAGE_LENGTH > _resbufLength) {
|
||||||
retval = false;
|
retval = false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
size_t writeLength = std::min(resbufLength, dataLength);
|
size_t writeLength = std::min(_resbufLength, dataLength);
|
||||||
memcpy(data, resbuf, writeLength);
|
memcpy(data, _resbuf, writeLength);
|
||||||
dataLength = writeLength;
|
dataLength = writeLength;
|
||||||
if(retval && !peek) {
|
if(retval && !peek) {
|
||||||
resbufLength = 0;
|
_resbufLength = 0;
|
||||||
}
|
}
|
||||||
return retval;
|
return retval;
|
||||||
}
|
}
|
||||||
|
|
||||||
void PeerConnection::readData(unsigned char* data, size_t& length, bool encryption)
|
void PeerConnection::readData
|
||||||
|
(unsigned char* data, size_t& length, bool encryption)
|
||||||
{
|
{
|
||||||
if(encryption) {
|
if(encryption) {
|
||||||
unsigned char temp[MAX_PAYLOAD_LEN];
|
unsigned char temp[MAX_PAYLOAD_LEN];
|
||||||
assert(MAX_PAYLOAD_LEN >= length);
|
assert(MAX_PAYLOAD_LEN >= length);
|
||||||
socket->readData(temp, length);
|
_socket->readData(temp, length);
|
||||||
_decryptor->decrypt(data, length, temp, length);
|
_decryptor->decrypt(data, length, temp, length);
|
||||||
} else {
|
} else {
|
||||||
socket->readData(data, length);
|
_socket->readData(data, length);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void PeerConnection::enableEncryption(const SharedHandle<ARC4Encryptor>& encryptor,
|
void PeerConnection::enableEncryption
|
||||||
const SharedHandle<ARC4Decryptor>& decryptor)
|
(const SharedHandle<ARC4Encryptor>& encryptor,
|
||||||
|
const SharedHandle<ARC4Decryptor>& decryptor)
|
||||||
{
|
{
|
||||||
_encryptor = encryptor;
|
_encryptor = encryptor;
|
||||||
_decryptor = decryptor;
|
_decryptor = decryptor;
|
||||||
|
@ -253,8 +255,8 @@ void PeerConnection::enableEncryption(const SharedHandle<ARC4Encryptor>& encrypt
|
||||||
void PeerConnection::presetBuffer(const unsigned char* data, size_t length)
|
void PeerConnection::presetBuffer(const unsigned char* data, size_t length)
|
||||||
{
|
{
|
||||||
size_t nwrite = std::min((size_t)MAX_PAYLOAD_LEN, length);
|
size_t nwrite = std::min((size_t)MAX_PAYLOAD_LEN, length);
|
||||||
memcpy(resbuf, data, nwrite);
|
memcpy(_resbuf, data, nwrite);
|
||||||
resbufLength = length;
|
_resbufLength = length;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool PeerConnection::sendBufferIsEmpty() const
|
bool PeerConnection::sendBufferIsEmpty() const
|
||||||
|
@ -265,8 +267,8 @@ bool PeerConnection::sendBufferIsEmpty() const
|
||||||
ssize_t PeerConnection::sendPendingData()
|
ssize_t PeerConnection::sendPendingData()
|
||||||
{
|
{
|
||||||
ssize_t writtenLength = _socketBuffer.send();
|
ssize_t writtenLength = _socketBuffer.send();
|
||||||
if(logger->debug()) {
|
if(_logger->debug()) {
|
||||||
logger->debug("sent %d byte(s).", writtenLength);
|
_logger->debug("sent %d byte(s).", writtenLength);
|
||||||
}
|
}
|
||||||
return writtenLength;
|
return writtenLength;
|
||||||
}
|
}
|
||||||
|
|
|
@ -56,15 +56,15 @@ class ARC4Decryptor;
|
||||||
|
|
||||||
class PeerConnection {
|
class PeerConnection {
|
||||||
private:
|
private:
|
||||||
cuid_t cuid;
|
cuid_t _cuid;
|
||||||
SharedHandle<SocketCore> socket;
|
SharedHandle<SocketCore> _socket;
|
||||||
Logger* logger;
|
Logger* _logger;
|
||||||
|
|
||||||
unsigned char* resbuf;
|
unsigned char* _resbuf;
|
||||||
size_t resbufLength;
|
size_t _resbufLength;
|
||||||
size_t currentPayloadLength;
|
size_t _currentPayloadLength;
|
||||||
unsigned char lenbuf[4];
|
unsigned char _lenbuf[4];
|
||||||
size_t lenbufLength;
|
size_t _lenbufLength;
|
||||||
|
|
||||||
SocketBuffer _socketBuffer;
|
SocketBuffer _socketBuffer;
|
||||||
|
|
||||||
|
@ -97,7 +97,8 @@ public:
|
||||||
* In both cases, 'msg' is filled with received bytes and the filled length
|
* In both cases, 'msg' is filled with received bytes and the filled length
|
||||||
* is assigned to 'length'.
|
* is assigned to 'length'.
|
||||||
*/
|
*/
|
||||||
bool receiveHandshake(unsigned char* data, size_t& dataLength, bool peek = false);
|
bool receiveHandshake
|
||||||
|
(unsigned char* data, size_t& dataLength, bool peek = false);
|
||||||
|
|
||||||
void enableEncryption(const SharedHandle<ARC4Encryptor>& encryptor,
|
void enableEncryption(const SharedHandle<ARC4Encryptor>& encryptor,
|
||||||
const SharedHandle<ARC4Decryptor>& decryptor);
|
const SharedHandle<ARC4Decryptor>& decryptor);
|
||||||
|
@ -110,13 +111,13 @@ public:
|
||||||
|
|
||||||
const unsigned char* getBuffer() const
|
const unsigned char* getBuffer() const
|
||||||
{
|
{
|
||||||
return resbuf;
|
return _resbuf;
|
||||||
}
|
}
|
||||||
|
|
||||||
unsigned char* detachBuffer()
|
unsigned char* detachBuffer()
|
||||||
{
|
{
|
||||||
unsigned char* detachbuf = resbuf;
|
unsigned char* detachbuf = _resbuf;
|
||||||
resbuf = new unsigned char[MAX_PAYLOAD_LEN];
|
_resbuf = new unsigned char[MAX_PAYLOAD_LEN];
|
||||||
return detachbuf;
|
return detachbuf;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
Loading…
Reference in New Issue