2006-12-24 06:25:21 +00:00
|
|
|
/* <!-- copyright */
|
|
|
|
/*
|
|
|
|
* aria2 - The high speed download utility
|
|
|
|
*
|
|
|
|
* 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-12-24 06:25:21 +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.
|
|
|
|
*/
|
|
|
|
/* copyright --> */
|
|
|
|
#include "DefaultBtMessageReceiver.h"
|
2008-11-03 06:49:02 +00:00
|
|
|
|
|
|
|
#include <cstring>
|
|
|
|
|
2006-12-24 06:25:21 +00:00
|
|
|
#include "BtHandshakeMessage.h"
|
2007-07-20 17:06:21 +00:00
|
|
|
#include "message.h"
|
2009-06-28 10:37:15 +00:00
|
|
|
#include "DownloadContext.h"
|
2008-02-08 15:53:45 +00:00
|
|
|
#include "Peer.h"
|
|
|
|
#include "PeerConnection.h"
|
|
|
|
#include "BtMessageDispatcher.h"
|
|
|
|
#include "BtMessageFactory.h"
|
|
|
|
#include "Logger.h"
|
|
|
|
#include "LogFactory.h"
|
2009-06-28 10:37:15 +00:00
|
|
|
#include "bittorrent_helper.h"
|
2010-03-04 16:24:03 +00:00
|
|
|
#include "BtPieceMessage.h"
|
2011-01-09 14:57:07 +00:00
|
|
|
#include "util.h"
|
|
|
|
#include "fmt.h"
|
|
|
|
#include "DlAbortEx.h"
|
2008-02-08 15:53:45 +00:00
|
|
|
|
|
|
|
namespace aria2 {
|
|
|
|
|
|
|
|
DefaultBtMessageReceiver::DefaultBtMessageReceiver():
|
2010-11-12 12:48:48 +00:00
|
|
|
handshakeSent_(false),
|
|
|
|
peerConnection_(0),
|
|
|
|
dispatcher_(0),
|
|
|
|
messageFactory_(0)
|
2010-06-12 08:31:36 +00:00
|
|
|
{}
|
2006-12-24 06:25:21 +00:00
|
|
|
|
2008-04-20 00:50:22 +00:00
|
|
|
SharedHandle<BtHandshakeMessage>
|
|
|
|
DefaultBtMessageReceiver::receiveHandshake(bool quickReply)
|
|
|
|
{
|
2011-01-09 14:57:07 +00:00
|
|
|
A2_LOG_DEBUG
|
|
|
|
(fmt("Receiving handshake bufferLength=%lu",
|
|
|
|
static_cast<unsigned long>(peerConnection_->getBufferLength())));
|
2006-12-24 06:25:21 +00:00
|
|
|
unsigned char data[BtHandshakeMessage::MESSAGE_LENGTH];
|
2008-03-09 12:24:01 +00:00
|
|
|
size_t dataLength = BtHandshakeMessage::MESSAGE_LENGTH;
|
2011-01-09 14:57:07 +00:00
|
|
|
SharedHandle<BtHandshakeMessage> msg;
|
|
|
|
if(handshakeSent_ || !quickReply || peerConnection_->getBufferLength() < 48) {
|
|
|
|
if(peerConnection_->receiveHandshake(data, dataLength)) {
|
|
|
|
msg = messageFactory_->createHandshakeMessage(data, dataLength);
|
|
|
|
msg->validate();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Handle tracker's NAT-checking feature
|
|
|
|
if(!handshakeSent_ && quickReply && peerConnection_->getBufferLength() >= 48){
|
2010-06-21 13:51:56 +00:00
|
|
|
handshakeSent_ = true;
|
2006-12-24 06:25:21 +00:00
|
|
|
// check info_hash
|
2011-01-09 14:57:07 +00:00
|
|
|
if(memcmp(bittorrent::getInfoHash(downloadContext_),
|
|
|
|
peerConnection_->getBuffer()+28,
|
2010-01-05 16:01:46 +00:00
|
|
|
INFO_HASH_LENGTH) == 0) {
|
2006-12-24 06:25:21 +00:00
|
|
|
sendHandshake();
|
2011-01-09 14:57:07 +00:00
|
|
|
} else {
|
|
|
|
throw DL_ABORT_EX
|
|
|
|
(fmt("Bad Info Hash %s",
|
|
|
|
util::toHex(peerConnection_->getBuffer()+28,
|
|
|
|
INFO_HASH_LENGTH).c_str()));
|
|
|
|
}
|
|
|
|
if(!msg &&
|
|
|
|
peerConnection_->getBufferLength() ==
|
|
|
|
BtHandshakeMessage::MESSAGE_LENGTH &&
|
|
|
|
peerConnection_->receiveHandshake(data, dataLength)) {
|
|
|
|
msg = messageFactory_->createHandshakeMessage(data, dataLength);
|
|
|
|
msg->validate();
|
2006-12-24 06:25:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return msg;
|
|
|
|
}
|
|
|
|
|
2008-04-20 00:50:22 +00:00
|
|
|
SharedHandle<BtHandshakeMessage>
|
|
|
|
DefaultBtMessageReceiver::receiveAndSendHandshake()
|
|
|
|
{
|
2006-12-24 06:25:21 +00:00
|
|
|
return receiveHandshake(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
void DefaultBtMessageReceiver::sendHandshake() {
|
2008-04-20 00:50:22 +00:00
|
|
|
SharedHandle<BtMessage> msg =
|
2010-06-21 13:51:56 +00:00
|
|
|
messageFactory_->createHandshakeMessage
|
|
|
|
(bittorrent::getInfoHash(downloadContext_), bittorrent::getStaticPeerId());
|
|
|
|
dispatcher_->addMessageToQueue(msg);
|
|
|
|
dispatcher_->sendMessages();
|
2006-12-24 06:25:21 +00:00
|
|
|
}
|
|
|
|
|
2012-09-28 14:27:46 +00:00
|
|
|
SharedHandle<BtMessage> DefaultBtMessageReceiver::receiveMessage() {
|
2008-03-09 12:24:01 +00:00
|
|
|
size_t dataLength = 0;
|
2010-03-04 16:24:03 +00:00
|
|
|
// Give 0 to PeerConnection::receiveMessage() to prevent memcpy.
|
2010-06-21 13:51:56 +00:00
|
|
|
if(!peerConnection_->receiveMessage(0, dataLength)) {
|
2008-04-20 00:50:22 +00:00
|
|
|
return SharedHandle<BtMessage>();
|
2006-12-24 06:25:21 +00:00
|
|
|
}
|
2012-09-28 14:27:46 +00:00
|
|
|
SharedHandle<BtMessage> msg =
|
2012-06-25 12:29:48 +00:00
|
|
|
messageFactory_->createBtMessage(peerConnection_->getMsgPayloadBuffer(),
|
|
|
|
dataLength);
|
2010-02-28 12:30:11 +00:00
|
|
|
msg->validate();
|
2010-03-04 16:24:03 +00:00
|
|
|
if(msg->getId() == BtPieceMessage::ID) {
|
|
|
|
SharedHandle<BtPieceMessage> piecemsg =
|
2010-06-13 11:19:21 +00:00
|
|
|
static_pointer_cast<BtPieceMessage>(msg);
|
2012-06-25 12:29:48 +00:00
|
|
|
piecemsg->setMsgPayload(peerConnection_->getMsgPayloadBuffer());
|
2010-03-04 16:24:03 +00:00
|
|
|
}
|
2010-02-28 12:30:11 +00:00
|
|
|
return msg;
|
2006-12-24 06:25:21 +00:00
|
|
|
}
|
|
|
|
|
2009-06-28 10:37:15 +00:00
|
|
|
void DefaultBtMessageReceiver::setDownloadContext
|
|
|
|
(const SharedHandle<DownloadContext>& downloadContext)
|
2008-02-08 15:53:45 +00:00
|
|
|
{
|
2010-06-21 13:51:56 +00:00
|
|
|
downloadContext_ = downloadContext;
|
2008-02-08 15:53:45 +00:00
|
|
|
}
|
|
|
|
|
2010-11-12 12:48:48 +00:00
|
|
|
void DefaultBtMessageReceiver::setPeerConnection(PeerConnection* peerConnection)
|
2008-02-08 15:53:45 +00:00
|
|
|
{
|
2010-06-21 13:51:56 +00:00
|
|
|
peerConnection_ = peerConnection;
|
2008-02-08 15:53:45 +00:00
|
|
|
}
|
|
|
|
|
2010-11-12 12:48:48 +00:00
|
|
|
void DefaultBtMessageReceiver::setDispatcher(BtMessageDispatcher* dispatcher)
|
2008-02-08 15:53:45 +00:00
|
|
|
{
|
2010-06-21 13:51:56 +00:00
|
|
|
dispatcher_ = dispatcher;
|
2008-02-08 15:53:45 +00:00
|
|
|
}
|
|
|
|
|
2010-11-12 12:48:48 +00:00
|
|
|
void DefaultBtMessageReceiver::setBtMessageFactory(BtMessageFactory* factory)
|
2008-02-08 15:53:45 +00:00
|
|
|
{
|
2010-06-21 13:51:56 +00:00
|
|
|
messageFactory_ = factory;
|
2008-02-08 15:53:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace aria2
|