|
@@ -47,6 +47,9 @@
|
|
|
#include "LogFactory.h"
|
|
#include "LogFactory.h"
|
|
|
#include "bittorrent_helper.h"
|
|
#include "bittorrent_helper.h"
|
|
|
#include "BtPieceMessage.h"
|
|
#include "BtPieceMessage.h"
|
|
|
|
|
+#include "util.h"
|
|
|
|
|
+#include "fmt.h"
|
|
|
|
|
+#include "DlAbortEx.h"
|
|
|
|
|
|
|
|
namespace aria2 {
|
|
namespace aria2 {
|
|
|
|
|
|
|
@@ -60,24 +63,40 @@ DefaultBtMessageReceiver::DefaultBtMessageReceiver():
|
|
|
SharedHandle<BtHandshakeMessage>
|
|
SharedHandle<BtHandshakeMessage>
|
|
|
DefaultBtMessageReceiver::receiveHandshake(bool quickReply)
|
|
DefaultBtMessageReceiver::receiveHandshake(bool quickReply)
|
|
|
{
|
|
{
|
|
|
|
|
+ A2_LOG_DEBUG
|
|
|
|
|
+ (fmt("Receiving handshake bufferLength=%lu",
|
|
|
|
|
+ static_cast<unsigned long>(peerConnection_->getBufferLength())));
|
|
|
unsigned char data[BtHandshakeMessage::MESSAGE_LENGTH];
|
|
unsigned char data[BtHandshakeMessage::MESSAGE_LENGTH];
|
|
|
size_t dataLength = BtHandshakeMessage::MESSAGE_LENGTH;
|
|
size_t dataLength = BtHandshakeMessage::MESSAGE_LENGTH;
|
|
|
- bool retval = peerConnection_->receiveHandshake(data, dataLength);
|
|
|
|
|
- // To handle tracker's NAT-checking feature
|
|
|
|
|
- if(!handshakeSent_ && quickReply && dataLength >= 48) {
|
|
|
|
|
|
|
+ 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){
|
|
|
handshakeSent_ = true;
|
|
handshakeSent_ = true;
|
|
|
// check info_hash
|
|
// check info_hash
|
|
|
- if(memcmp(bittorrent::getInfoHash(downloadContext_), &data[28],
|
|
|
|
|
|
|
+ if(memcmp(bittorrent::getInfoHash(downloadContext_),
|
|
|
|
|
+ peerConnection_->getBuffer()+28,
|
|
|
INFO_HASH_LENGTH) == 0) {
|
|
INFO_HASH_LENGTH) == 0) {
|
|
|
sendHandshake();
|
|
sendHandshake();
|
|
|
|
|
+ } 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();
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
- if(!retval) {
|
|
|
|
|
- return SharedHandle<BtHandshakeMessage>();
|
|
|
|
|
- }
|
|
|
|
|
- SharedHandle<BtHandshakeMessage> msg =
|
|
|
|
|
- messageFactory_->createHandshakeMessage(data, dataLength);
|
|
|
|
|
- msg->validate();
|
|
|
|
|
return msg;
|
|
return msg;
|
|
|
}
|
|
}
|
|
|
|
|
|