UTMetadataDataExtensionMessage.cc 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /* <!-- copyright */
  2. /*
  3. * aria2 - The high speed download utility
  4. *
  5. * Copyright (C) 2009 Tatsuhiro Tsujikawa
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. *
  21. * In addition, as a special exception, the copyright holders give
  22. * permission to link the code of portions of this program with the
  23. * OpenSSL library under certain conditions as described in each
  24. * individual source file, and distribute linked combinations
  25. * including the two.
  26. * You must obey the GNU General Public License in all respects
  27. * for all of the code used other than OpenSSL. If you modify
  28. * file(s) with this exception, you may extend this exception to your
  29. * version of the file(s), but you are not obligated to do so. If you
  30. * do not wish to do so, delete this exception statement from your
  31. * version. If you delete this exception statement from all source
  32. * files in the program, then also delete it here.
  33. */
  34. /* copyright --> */
  35. #include "UTMetadataDataExtensionMessage.h"
  36. #include "bencode2.h"
  37. #include "util.h"
  38. #include "a2functional.h"
  39. #include "DownloadContext.h"
  40. #include "UTMetadataRequestTracker.h"
  41. #include "PieceStorage.h"
  42. #include "BtConstants.h"
  43. #include "MessageDigest.h"
  44. #include "message_digest_helper.h"
  45. #include "bittorrent_helper.h"
  46. #include "DiskAdaptor.h"
  47. #include "Piece.h"
  48. #include "Logger.h"
  49. #include "LogFactory.h"
  50. #include "DlAbortEx.h"
  51. #include "fmt.h"
  52. namespace aria2 {
  53. UTMetadataDataExtensionMessage::UTMetadataDataExtensionMessage(
  54. uint8_t extensionMessageID)
  55. : UTMetadataExtensionMessage{extensionMessageID},
  56. totalSize_{0},
  57. dctx_{nullptr},
  58. pieceStorage_{nullptr},
  59. tracker_{nullptr}
  60. {
  61. }
  62. std::string UTMetadataDataExtensionMessage::getPayload()
  63. {
  64. Dict dict;
  65. dict.put("msg_type", Integer::g(1));
  66. dict.put("piece", Integer::g(getIndex()));
  67. dict.put("total_size", Integer::g(totalSize_));
  68. return bencode2::encode(&dict) + data_;
  69. }
  70. std::string UTMetadataDataExtensionMessage::toString() const
  71. {
  72. return fmt("ut_metadata data piece=%lu",
  73. static_cast<unsigned long>(getIndex()));
  74. }
  75. void UTMetadataDataExtensionMessage::doReceivedAction()
  76. {
  77. if (tracker_->tracks(getIndex())) {
  78. A2_LOG_DEBUG(fmt("ut_metadata index=%lu found in tracking list",
  79. static_cast<unsigned long>(getIndex())));
  80. tracker_->remove(getIndex());
  81. pieceStorage_->getDiskAdaptor()->writeData(
  82. reinterpret_cast<const unsigned char*>(data_.c_str()), data_.size(),
  83. getIndex() * METADATA_PIECE_SIZE);
  84. pieceStorage_->completePiece(pieceStorage_->getPiece(getIndex()));
  85. if (pieceStorage_->downloadFinished()) {
  86. std::string metadata = util::toString(pieceStorage_->getDiskAdaptor());
  87. unsigned char infoHash[INFO_HASH_LENGTH];
  88. message_digest::digest(infoHash, INFO_HASH_LENGTH,
  89. MessageDigest::sha1().get(), metadata.data(),
  90. metadata.size());
  91. if (memcmp(infoHash, bittorrent::getInfoHash(dctx_), INFO_HASH_LENGTH) ==
  92. 0) {
  93. A2_LOG_INFO("Got ut_metadata");
  94. }
  95. else {
  96. A2_LOG_INFO("Got wrong ut_metadata");
  97. for (size_t i = 0; i < dctx_->getNumPieces(); ++i) {
  98. pieceStorage_->markPieceMissing(i);
  99. }
  100. throw DL_ABORT_EX("Got wrong ut_metadata");
  101. }
  102. }
  103. }
  104. else {
  105. A2_LOG_DEBUG(fmt("ut_metadata index=%lu is not tracked",
  106. static_cast<unsigned long>(getIndex())));
  107. }
  108. }
  109. void UTMetadataDataExtensionMessage::setTotalSize(size_t totalSize)
  110. {
  111. totalSize_ = totalSize;
  112. }
  113. size_t UTMetadataDataExtensionMessage::getTotalSize() const
  114. {
  115. return totalSize_;
  116. }
  117. void UTMetadataDataExtensionMessage::setData(const std::string& data)
  118. {
  119. data_ = data;
  120. }
  121. const std::string& UTMetadataDataExtensionMessage::getData() const
  122. {
  123. return data_;
  124. }
  125. void UTMetadataDataExtensionMessage::setPieceStorage(PieceStorage* pieceStorage)
  126. {
  127. pieceStorage_ = pieceStorage;
  128. }
  129. void UTMetadataDataExtensionMessage::setUTMetadataRequestTracker(
  130. UTMetadataRequestTracker* tracker)
  131. {
  132. tracker_ = tracker;
  133. }
  134. void UTMetadataDataExtensionMessage::setDownloadContext(DownloadContext* dctx)
  135. {
  136. dctx_ = dctx;
  137. }
  138. } // namespace aria2