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 "PeerConnection.h"
|
|
|
|
#include "message.h"
|
|
|
|
#include "DlAbortEx.h"
|
|
|
|
#include "PeerMessageUtil.h"
|
|
|
|
#include "Util.h"
|
2006-04-17 16:17:20 +00:00
|
|
|
#include "LogFactory.h"
|
2006-12-24 06:25:21 +00:00
|
|
|
#include "BtHandshakeMessage.h"
|
2007-07-23 13:04:48 +00:00
|
|
|
#include "a2netcompat.h"
|
2006-03-21 14:12:51 +00:00
|
|
|
|
2006-12-24 06:25:21 +00:00
|
|
|
PeerConnection::PeerConnection(int32_t cuid,
|
2006-07-19 17:07:45 +00:00
|
|
|
const SocketHandle& socket,
|
2006-05-18 17:08:29 +00:00
|
|
|
const Option* op)
|
|
|
|
:cuid(cuid),
|
|
|
|
socket(socket),
|
|
|
|
option(op),
|
2007-01-28 14:18:35 +00:00
|
|
|
logger(LogFactory::getInstance()),
|
2006-05-18 17:08:29 +00:00
|
|
|
resbufLength(0),
|
|
|
|
currentPayloadLength(0),
|
2007-01-28 14:18:35 +00:00
|
|
|
lenbufLength(0)
|
|
|
|
{
|
2007-07-20 17:06:21 +00:00
|
|
|
//logger->debug("PeerConnection::instantiated");
|
2006-04-17 16:17:20 +00:00
|
|
|
}
|
2006-03-21 14:12:51 +00:00
|
|
|
|
2007-01-28 14:18:35 +00:00
|
|
|
PeerConnection::~PeerConnection()
|
|
|
|
{
|
2007-07-20 17:06:21 +00:00
|
|
|
//logger->debug("PeerConnection::deleted");
|
2007-01-28 14:18:35 +00:00
|
|
|
}
|
2006-03-21 14:12:51 +00:00
|
|
|
|
2007-01-25 16:47:29 +00:00
|
|
|
int32_t PeerConnection::sendMessage(const unsigned char* data, int32_t dataLength) {
|
|
|
|
int32_t writtenLength = 0;
|
2006-03-21 14:12:51 +00:00
|
|
|
if(socket->isWritable(0)) {
|
2006-12-24 06:25:21 +00:00
|
|
|
// TODO fix this
|
|
|
|
socket->writeData((const char*)data, dataLength);
|
|
|
|
writtenLength += dataLength;
|
2006-03-21 14:12:51 +00:00
|
|
|
}
|
|
|
|
return writtenLength;
|
|
|
|
}
|
|
|
|
|
2007-01-25 16:47:29 +00:00
|
|
|
bool PeerConnection::receiveMessage(unsigned char* data, int32_t& dataLength) {
|
2006-03-21 14:12:51 +00:00
|
|
|
if(!socket->isReadable(0)) {
|
2006-05-09 15:54:14 +00:00
|
|
|
return false;
|
2006-03-21 14:12:51 +00:00
|
|
|
}
|
|
|
|
if(resbufLength == 0 && lenbufLength != 4) {
|
|
|
|
// read payload size, 4-byte integer
|
2007-01-25 16:47:29 +00:00
|
|
|
int32_t remain = 4-lenbufLength;
|
|
|
|
int32_t temp = remain;
|
2006-12-24 06:25:21 +00:00
|
|
|
// TODO fix this
|
2007-08-15 15:11:01 +00:00
|
|
|
socket->readData((char*)lenbuf+lenbufLength, temp);
|
2006-03-21 14:12:51 +00:00
|
|
|
if(temp == 0) {
|
|
|
|
// we got EOF
|
|
|
|
throw new DlAbortEx(EX_EOF_FROM_PEER);
|
|
|
|
}
|
|
|
|
if(remain != temp) {
|
|
|
|
// still 4-temp bytes to go
|
|
|
|
lenbufLength += temp;
|
2006-05-09 15:54:14 +00:00
|
|
|
return false;
|
2006-03-21 14:12:51 +00:00
|
|
|
}
|
|
|
|
//payloadLen = ntohl(nPayloadLen);
|
2007-01-25 16:47:29 +00:00
|
|
|
int32_t payloadLength = ntohl(*((int32_t*)lenbuf));
|
2006-07-03 14:19:23 +00:00
|
|
|
if(payloadLength > MAX_PAYLOAD_LEN || payloadLength < 0) {
|
2007-07-20 17:06:21 +00:00
|
|
|
throw new DlAbortEx(EX_TOO_LONG_PAYLOAD,
|
2006-03-21 14:12:51 +00:00
|
|
|
payloadLength);
|
|
|
|
}
|
|
|
|
currentPayloadLength = payloadLength;
|
|
|
|
}
|
2007-01-11 16:32:31 +00:00
|
|
|
if(!socket->isReadable(0)) {
|
|
|
|
return false;
|
|
|
|
}
|
2006-03-21 14:12:51 +00:00
|
|
|
// we have currentPayloadLen-resbufLen bytes to read
|
2007-01-25 16:47:29 +00:00
|
|
|
int32_t remaining = currentPayloadLength-resbufLength;
|
2006-03-21 14:12:51 +00:00
|
|
|
if(remaining > 0) {
|
2007-08-15 15:11:01 +00:00
|
|
|
socket->readData((char*)resbuf+resbufLength, remaining);
|
2006-03-21 14:12:51 +00:00
|
|
|
if(remaining == 0) {
|
|
|
|
// we got EOF
|
|
|
|
throw new DlAbortEx(EX_EOF_FROM_PEER);
|
|
|
|
}
|
|
|
|
resbufLength += remaining;
|
|
|
|
if(currentPayloadLength != resbufLength) {
|
2006-05-09 15:54:14 +00:00
|
|
|
return false;
|
2006-03-21 14:12:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// we got whole payload.
|
|
|
|
resbufLength = 0;
|
|
|
|
lenbufLength = 0;
|
2006-05-09 15:54:14 +00:00
|
|
|
|
2006-12-24 06:25:21 +00:00
|
|
|
memcpy(data, resbuf, currentPayloadLength);
|
|
|
|
dataLength = currentPayloadLength;
|
2006-05-09 15:54:14 +00:00
|
|
|
return true;
|
2006-03-21 14:12:51 +00:00
|
|
|
}
|
|
|
|
|
2007-10-11 16:58:24 +00:00
|
|
|
bool PeerConnection::receiveHandshake(unsigned char* data, int32_t& dataLength,
|
|
|
|
bool peek) {
|
|
|
|
int32_t remain = BtHandshakeMessage::MESSAGE_LENGTH-resbufLength;
|
|
|
|
if(remain != 0 && !socket->isReadable(0)) {
|
2006-12-24 06:25:21 +00:00
|
|
|
dataLength = 0;
|
2006-05-09 15:54:14 +00:00
|
|
|
return false;
|
2006-03-21 14:12:51 +00:00
|
|
|
}
|
2007-01-25 16:47:29 +00:00
|
|
|
int32_t temp = remain;
|
2007-10-11 16:58:24 +00:00
|
|
|
if(remain != 0) {
|
|
|
|
socket->readData((char*)resbuf+resbufLength, temp);
|
|
|
|
if(temp == 0) {
|
|
|
|
// we got EOF
|
|
|
|
throw new DlAbortEx(EX_EOF_FROM_PEER);
|
|
|
|
}
|
2006-03-21 14:12:51 +00:00
|
|
|
}
|
2006-06-22 15:26:18 +00:00
|
|
|
bool retval;
|
2006-03-21 14:12:51 +00:00
|
|
|
if(remain != temp) {
|
2006-06-22 15:26:18 +00:00
|
|
|
retval = false;
|
|
|
|
} else {
|
|
|
|
retval = true;
|
2006-03-21 14:12:51 +00:00
|
|
|
}
|
2006-06-22 15:26:18 +00:00
|
|
|
resbufLength += temp;
|
2007-10-11 16:58:24 +00:00
|
|
|
|
2007-01-25 16:47:29 +00:00
|
|
|
int32_t writeLength = resbufLength > dataLength ? dataLength : resbufLength;
|
2006-12-24 06:25:21 +00:00
|
|
|
memcpy(data, resbuf, writeLength);
|
|
|
|
dataLength = writeLength;
|
2007-10-11 16:58:24 +00:00
|
|
|
if(retval && !peek) {
|
2006-06-22 15:26:18 +00:00
|
|
|
resbufLength = 0;
|
|
|
|
}
|
|
|
|
return retval;
|
2006-03-21 14:12:51 +00:00
|
|
|
}
|