2009-11-20 15:42:25 +00:00
|
|
|
/* <!-- copyright */
|
|
|
|
/*
|
|
|
|
* aria2 - The high speed download utility
|
|
|
|
*
|
|
|
|
* Copyright (C) 2009 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
|
2009-11-20 15:42:25 +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 "UTMetadataDataExtensionMessage.h"
|
2010-06-19 17:54:54 +00:00
|
|
|
#include "bencode2.h"
|
2009-11-20 15:42:25 +00:00
|
|
|
#include "util.h"
|
|
|
|
#include "a2functional.h"
|
2009-11-22 15:31:47 +00:00
|
|
|
#include "DownloadContext.h"
|
|
|
|
#include "UTMetadataRequestTracker.h"
|
|
|
|
#include "PieceStorage.h"
|
|
|
|
#include "BtConstants.h"
|
2010-11-11 02:56:24 +00:00
|
|
|
#include "MessageDigest.h"
|
2009-11-22 15:31:47 +00:00
|
|
|
#include "MessageDigestHelper.h"
|
|
|
|
#include "bittorrent_helper.h"
|
|
|
|
#include "DiskAdaptor.h"
|
|
|
|
#include "Piece.h"
|
|
|
|
#include "LogFactory.h"
|
2010-11-11 02:56:24 +00:00
|
|
|
#include "DlAbortEx.h"
|
2009-11-20 15:42:25 +00:00
|
|
|
|
|
|
|
namespace aria2 {
|
|
|
|
|
|
|
|
UTMetadataDataExtensionMessage::UTMetadataDataExtensionMessage
|
2009-11-22 15:31:47 +00:00
|
|
|
(uint8_t extensionMessageID):UTMetadataExtensionMessage(extensionMessageID),
|
2010-06-21 13:51:56 +00:00
|
|
|
logger_(LogFactory::getInstance()) {}
|
2009-11-20 15:42:25 +00:00
|
|
|
|
2009-11-28 12:32:07 +00:00
|
|
|
std::string UTMetadataDataExtensionMessage::getPayload()
|
2009-11-20 15:42:25 +00:00
|
|
|
{
|
2010-06-19 17:54:54 +00:00
|
|
|
Dict dict;
|
|
|
|
dict.put("msg_type", Integer::g(1));
|
|
|
|
dict.put("piece", Integer::g(getIndex()));
|
2010-06-21 13:51:56 +00:00
|
|
|
dict.put("total_size", Integer::g(totalSize_));
|
|
|
|
return bencode2::encode(&dict)+data_;
|
2009-11-20 15:42:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string UTMetadataDataExtensionMessage::toString() const
|
|
|
|
{
|
2010-06-12 14:11:46 +00:00
|
|
|
return strconcat("ut_metadata data piece=", util::uitos(getIndex()));
|
2009-11-20 15:42:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void UTMetadataDataExtensionMessage::doReceivedAction()
|
|
|
|
{
|
2010-06-21 13:51:56 +00:00
|
|
|
if(tracker_->tracks(getIndex())) {
|
|
|
|
if(logger_->debug()) {
|
|
|
|
logger_->debug("ut_metadata index=%lu found in tracking list",
|
2010-06-12 14:11:46 +00:00
|
|
|
static_cast<unsigned long>(getIndex()));
|
2010-02-09 12:20:20 +00:00
|
|
|
}
|
2010-06-21 13:51:56 +00:00
|
|
|
tracker_->remove(getIndex());
|
|
|
|
pieceStorage_->getDiskAdaptor()->writeData
|
|
|
|
(reinterpret_cast<const unsigned char*>(data_.c_str()), data_.size(),
|
2010-06-12 14:11:46 +00:00
|
|
|
getIndex()*METADATA_PIECE_SIZE);
|
2010-06-21 13:51:56 +00:00
|
|
|
pieceStorage_->completePiece(pieceStorage_->getPiece(getIndex()));
|
|
|
|
if(pieceStorage_->downloadFinished()) {
|
|
|
|
std::string metadata = util::toString(pieceStorage_->getDiskAdaptor());
|
2009-11-22 15:31:47 +00:00
|
|
|
unsigned char infoHash[INFO_HASH_LENGTH];
|
|
|
|
MessageDigestHelper::digest(infoHash, INFO_HASH_LENGTH,
|
2010-11-11 02:56:24 +00:00
|
|
|
MessageDigest::sha1(),
|
2010-01-05 16:01:46 +00:00
|
|
|
metadata.data(), metadata.size());
|
2010-06-21 13:51:56 +00:00
|
|
|
if(memcmp(infoHash, bittorrent::getInfoHash(dctx_),
|
2010-06-18 14:47:09 +00:00
|
|
|
INFO_HASH_LENGTH) == 0) {
|
2010-06-21 13:51:56 +00:00
|
|
|
logger_->info("Got ut_metadata");
|
2009-11-22 15:31:47 +00:00
|
|
|
} else {
|
2010-06-21 13:51:56 +00:00
|
|
|
logger_->info("Got wrong ut_metadata");
|
|
|
|
for(size_t i = 0; i < dctx_->getNumPieces(); ++i) {
|
|
|
|
pieceStorage_->markPieceMissing(i);
|
2010-01-05 16:01:46 +00:00
|
|
|
}
|
2010-08-29 12:40:56 +00:00
|
|
|
throw DL_ABORT_EX("Got wrong ut_metadata");
|
2009-11-22 15:31:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2010-06-21 13:51:56 +00:00
|
|
|
if(logger_->debug()) {
|
|
|
|
logger_->debug("ut_metadata index=%lu is not tracked",
|
2010-06-12 14:11:46 +00:00
|
|
|
static_cast<unsigned long>(getIndex()));
|
2010-02-09 12:20:20 +00:00
|
|
|
}
|
2009-11-22 15:31:47 +00:00
|
|
|
}
|
2009-11-20 15:42:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace aria2
|