spirosmastorakis | a6057f5 | 2016-01-28 13:34:41 -0800 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
| 3 | * Copyright (c) 2016 Regents of the University of California. |
| 4 | * |
| 5 | * This file is part of the nTorrent codebase. |
| 6 | * |
| 7 | * nTorrent is free software: you can redistribute it and/or modify it under the |
| 8 | * terms of the GNU Lesser General Public License as published by the Free Software |
| 9 | * Foundation, either version 3 of the License, or (at your option) any later version. |
| 10 | * |
| 11 | * nTorrent is distributed in the hope that it will be useful, but WITHOUT ANY |
| 12 | * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A |
| 13 | * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. |
| 14 | * |
| 15 | * You should have received copies of the GNU General Public License and GNU Lesser |
| 16 | * General Public License along with nTorrent, e.g., in COPYING.md file. If not, see |
| 17 | * <http://www.gnu.org/licenses/>. |
| 18 | * |
| 19 | * See AUTHORS for complete list of nTorrent authors and contributors. |
| 20 | */ |
| 21 | |
| 22 | #include "torrent-file.hpp" |
| 23 | |
spirosmastorakis | f5d1b6c | 2016-02-25 12:49:56 -0800 | [diff] [blame^] | 24 | #include <ndn-cxx/security/key-chain.hpp> |
| 25 | #include <ndn-cxx/security/signing-helpers.hpp> |
| 26 | |
spirosmastorakis | a6057f5 | 2016-01-28 13:34:41 -0800 | [diff] [blame] | 27 | #include <algorithm> |
| 28 | |
| 29 | #include <boost/range/adaptors.hpp> |
spirosmastorakis | f5d1b6c | 2016-02-25 12:49:56 -0800 | [diff] [blame^] | 30 | #include <boost/filesystem.hpp> |
| 31 | #include <boost/filesystem/fstream.hpp> |
| 32 | |
| 33 | namespace fs = boost::filesystem; |
spirosmastorakis | a6057f5 | 2016-01-28 13:34:41 -0800 | [diff] [blame] | 34 | |
| 35 | namespace ndn { |
| 36 | |
| 37 | namespace ntorrent { |
| 38 | |
| 39 | BOOST_CONCEPT_ASSERT((WireEncodable<TorrentFile>)); |
| 40 | BOOST_CONCEPT_ASSERT((WireDecodable<TorrentFile>)); |
| 41 | static_assert(std::is_base_of<Data::Error, TorrentFile::Error>::value, |
| 42 | "TorrentFile::Error should inherit from Data::Error"); |
| 43 | |
| 44 | TorrentFile::TorrentFile(const Name& torrentFileName, |
| 45 | const Name& torrentFilePtr, |
| 46 | const Name& commonPrefix, |
| 47 | const std::vector<ndn::Name>& catalog) |
| 48 | : Data(torrentFileName) |
| 49 | , m_commonPrefix(commonPrefix) |
| 50 | , m_torrentFilePtr(torrentFilePtr) |
| 51 | , m_catalog(catalog) |
| 52 | { |
| 53 | } |
| 54 | |
| 55 | TorrentFile::TorrentFile(const Name& torrentFileName, |
| 56 | const Name& commonPrefix, |
| 57 | const std::vector<ndn::Name>& catalog) |
| 58 | : Data(torrentFileName) |
| 59 | , m_commonPrefix(commonPrefix) |
| 60 | , m_catalog(catalog) |
| 61 | { |
| 62 | } |
| 63 | |
| 64 | TorrentFile::TorrentFile(const Block& block) |
| 65 | { |
| 66 | this->wireDecode(block); |
| 67 | } |
| 68 | |
spirosmastorakis | a6057f5 | 2016-01-28 13:34:41 -0800 | [diff] [blame] | 69 | void |
| 70 | TorrentFile::createSuffixCatalog() |
| 71 | { |
| 72 | for (auto i = m_catalog.begin() ; i != m_catalog.end(); ++i) { |
| 73 | m_suffixCatalog.push_back((*i).getSubName(m_commonPrefix.size())); |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | shared_ptr<Name> |
| 78 | TorrentFile::getTorrentFilePtr() const |
| 79 | { |
| 80 | if (this->hasTorrentFilePtr()) { |
| 81 | return make_shared<Name>(m_torrentFilePtr); |
| 82 | } |
| 83 | return nullptr; |
| 84 | } |
| 85 | |
| 86 | void |
| 87 | TorrentFile::constructLongNames() |
| 88 | { |
| 89 | for (auto i = m_suffixCatalog.begin(); i != m_suffixCatalog.end(); ++i) { |
| 90 | Name commonPrefix = m_commonPrefix; |
| 91 | m_catalog.push_back(commonPrefix.append((*i))); |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | template<encoding::Tag TAG> |
| 96 | size_t |
| 97 | TorrentFile::encodeContent(EncodingImpl<TAG>& encoder) const |
| 98 | { |
| 99 | // TorrentFileContent ::= CONTENT-TYPE TLV-LENGTH |
| 100 | // Suffix+ |
| 101 | // CommonPrefix |
| 102 | // TorrentFilePtr? |
| 103 | |
| 104 | // Suffix ::= NAME-TYPE TLV-LENGTH |
| 105 | // Name |
| 106 | |
| 107 | // CommonPrefix ::= NAME-TYPE TLV-LENGTH |
| 108 | // Name |
| 109 | |
| 110 | // TorrentFilePtr ::= NAME-TYPE TLV-LENGTH |
| 111 | // Name |
| 112 | |
| 113 | size_t totalLength = 0; |
| 114 | for (const auto& name : m_suffixCatalog | boost::adaptors::reversed) { |
| 115 | size_t fileManifestSuffixLength = 0; |
| 116 | fileManifestSuffixLength += name.wireEncode(encoder); |
| 117 | totalLength += fileManifestSuffixLength; |
| 118 | } |
| 119 | totalLength += m_commonPrefix.wireEncode(encoder); |
| 120 | if (!m_torrentFilePtr.empty()) { |
| 121 | size_t torrentFilePtrLength = 0; |
| 122 | torrentFilePtrLength += m_torrentFilePtr.wireEncode(encoder); |
| 123 | totalLength += torrentFilePtrLength; |
| 124 | } |
| 125 | |
| 126 | totalLength += encoder.prependVarNumber(totalLength); |
| 127 | totalLength += encoder.prependVarNumber(tlv::Content); |
| 128 | return totalLength; |
| 129 | } |
| 130 | |
| 131 | bool |
| 132 | TorrentFile::erase(const Name& name) |
| 133 | { |
| 134 | auto found = std::find(m_catalog.begin(), m_catalog.end(), name); |
| 135 | if (found != m_catalog.end()) { |
| 136 | m_catalog.erase(found); |
| 137 | return true; |
| 138 | } |
| 139 | return false; |
| 140 | } |
| 141 | |
| 142 | void |
| 143 | TorrentFile::encodeContent() |
| 144 | { |
| 145 | onChanged(); |
| 146 | |
| 147 | EncodingEstimator estimator; |
| 148 | size_t estimatedSize = encodeContent(estimator); |
| 149 | |
| 150 | EncodingBuffer buffer(estimatedSize, 0); |
| 151 | encodeContent(buffer); |
| 152 | |
| 153 | setContentType(tlv::ContentType_Blob); |
| 154 | setContent(buffer.block()); |
| 155 | } |
| 156 | |
| 157 | void |
| 158 | TorrentFile::decodeContent() |
| 159 | { |
| 160 | // TorrentFileContent ::= CONTENT-TYPE TLV-LENGTH |
| 161 | // Suffix+ |
| 162 | // CommonPrefix |
| 163 | // TorrentFilePtr? |
| 164 | |
| 165 | // Suffix ::= NAME-TYPE TLV-LENGTH |
| 166 | // Name |
| 167 | |
| 168 | // CommonPrefix ::= NAME-TYPE TLV-LENGTH |
| 169 | // Name |
| 170 | |
| 171 | // TorrentFilePtr ::= NAME-TYPE TLV-LENGTH |
| 172 | // Name |
| 173 | |
| 174 | if (getContentType() != tlv::ContentType_Blob) { |
| 175 | BOOST_THROW_EXCEPTION(Error("Expected Content Type Blob")); |
| 176 | } |
| 177 | |
| 178 | const Block& content = Data::getContent(); |
| 179 | content.parse(); |
| 180 | |
| 181 | // Check whether there is a TorrentFilePtr |
| 182 | auto element = content.elements_begin(); |
| 183 | if (content.elements_end() == element) { |
spirosmastorakis | f5d1b6c | 2016-02-25 12:49:56 -0800 | [diff] [blame^] | 184 | BOOST_THROW_EXCEPTION(Error("Torrent-file with empty content")); |
spirosmastorakis | a6057f5 | 2016-01-28 13:34:41 -0800 | [diff] [blame] | 185 | } |
| 186 | element->parse(); |
| 187 | Name name(*element); |
| 188 | if (name.empty()) |
spirosmastorakis | f5d1b6c | 2016-02-25 12:49:56 -0800 | [diff] [blame^] | 189 | BOOST_THROW_EXCEPTION(Error("Empty name included in the torrent-file")); |
spirosmastorakis | a6057f5 | 2016-01-28 13:34:41 -0800 | [diff] [blame] | 190 | |
spirosmastorakis | f5d1b6c | 2016-02-25 12:49:56 -0800 | [diff] [blame^] | 191 | if (name.get(name.size() - 3) == name::Component("torrent-file")) { |
spirosmastorakis | a6057f5 | 2016-01-28 13:34:41 -0800 | [diff] [blame] | 192 | m_torrentFilePtr = name; |
| 193 | ++element; |
| 194 | m_commonPrefix = Name(*element); |
| 195 | if (m_commonPrefix.empty()) { |
| 196 | BOOST_THROW_EXCEPTION(Error("Common prefix cannot be empty")); |
| 197 | } |
| 198 | } |
| 199 | else { |
| 200 | m_commonPrefix = name; |
| 201 | } |
| 202 | element++; |
| 203 | for (; element != content.elements_end(); ++element) { |
| 204 | element->parse(); |
| 205 | Name fileManifestSuffix(*element); |
| 206 | if (fileManifestSuffix.empty()) |
spirosmastorakis | f5d1b6c | 2016-02-25 12:49:56 -0800 | [diff] [blame^] | 207 | BOOST_THROW_EXCEPTION(Error("Empty manifest file name included in the torrent-file")); |
spirosmastorakis | a6057f5 | 2016-01-28 13:34:41 -0800 | [diff] [blame] | 208 | this->insertToSuffixCatalog(fileManifestSuffix); |
| 209 | } |
| 210 | if (m_suffixCatalog.size() == 0) { |
spirosmastorakis | f5d1b6c | 2016-02-25 12:49:56 -0800 | [diff] [blame^] | 211 | BOOST_THROW_EXCEPTION(Error("Torrent-file with empty catalog of file manifest names")); |
spirosmastorakis | a6057f5 | 2016-01-28 13:34:41 -0800 | [diff] [blame] | 212 | } |
| 213 | } |
| 214 | |
| 215 | void |
| 216 | TorrentFile::wireDecode(const Block& wire) |
| 217 | { |
| 218 | m_catalog.clear(); |
| 219 | m_suffixCatalog.clear(); |
| 220 | Data::wireDecode(wire); |
| 221 | this->decodeContent(); |
| 222 | this->constructLongNames(); |
| 223 | } |
| 224 | |
spirosmastorakis | 6d4300f | 2016-02-29 20:18:43 -0800 | [diff] [blame] | 225 | void |
| 226 | TorrentFile::finalize() |
spirosmastorakis | a6057f5 | 2016-01-28 13:34:41 -0800 | [diff] [blame] | 227 | { |
| 228 | this->createSuffixCatalog(); |
| 229 | this->encodeContent(); |
| 230 | m_suffixCatalog.clear(); |
spirosmastorakis | a6057f5 | 2016-01-28 13:34:41 -0800 | [diff] [blame] | 231 | } |
| 232 | |
spirosmastorakis | f5d1b6c | 2016-02-25 12:49:56 -0800 | [diff] [blame^] | 233 | std::pair<std::vector<TorrentFile>, |
| 234 | std::vector<std::pair<std::vector<FileManifest>, |
| 235 | std::vector<Data>>>> |
| 236 | TorrentFile::generate(const std::string& directoryPath, |
| 237 | size_t namesPerSegment, |
| 238 | size_t subManifestSize, |
| 239 | size_t dataPacketSize, |
| 240 | bool returnData) |
| 241 | { |
| 242 | BOOST_ASSERT(0 < namesPerSegment); |
| 243 | |
| 244 | std::vector<TorrentFile> torrentSegments; |
| 245 | |
| 246 | fs::path path(directoryPath); |
| 247 | if (!fs::exists(path)) { |
| 248 | BOOST_THROW_EXCEPTION(Error(directoryPath + ": no such directory.")); |
| 249 | } |
| 250 | |
| 251 | Name directoryPathName(directoryPath); |
| 252 | fs::recursive_directory_iterator directoryPtr(fs::system_complete(directoryPath).string()); |
| 253 | |
| 254 | Name commonPrefix("/NTORRENT" + |
| 255 | directoryPathName.getSubName(directoryPathName.size() - 1).toUri()); |
| 256 | |
| 257 | Name torrentName(commonPrefix.toUri() + "/torrent-file"); |
| 258 | TorrentFile currentTorrentFile(torrentName, commonPrefix, {}); |
| 259 | std::vector<std::pair<std::vector<FileManifest>, std::vector<Data>>> manifestPairs; |
| 260 | |
| 261 | size_t manifestFileCounter = 0u; |
| 262 | for (fs::recursive_directory_iterator i = directoryPtr; |
| 263 | i != fs::recursive_directory_iterator(); |
| 264 | ++i) { |
| 265 | Name manifestPrefix("/NTORRENT" + |
| 266 | directoryPathName.getSubName(directoryPathName.size() - 1).toUri()); |
| 267 | std::pair<std::vector<FileManifest>, std::vector<Data>> currentManifestPair = |
| 268 | FileManifest::generate((*i).path().string(), |
| 269 | manifestPrefix, subManifestSize, |
| 270 | dataPacketSize, returnData); |
| 271 | |
| 272 | if (manifestFileCounter != 0 && 0 == manifestFileCounter % namesPerSegment) { |
| 273 | torrentSegments.push_back(currentTorrentFile); |
| 274 | Name currentTorrentName = torrentName; |
| 275 | currentTorrentName.appendSequenceNumber(static_cast<int>(manifestFileCounter)); |
| 276 | currentTorrentFile = TorrentFile(currentTorrentName, commonPrefix, {}); |
| 277 | } |
| 278 | currentTorrentFile.insert(currentManifestPair.first[0].getName()); |
| 279 | currentManifestPair.first.shrink_to_fit(); |
| 280 | currentManifestPair.second.shrink_to_fit(); |
| 281 | manifestPairs.push_back(currentManifestPair); |
| 282 | ++manifestFileCounter; |
| 283 | } |
| 284 | |
| 285 | // Sign and append the last torrent-file |
| 286 | security::KeyChain keyChain; |
| 287 | keyChain.sign(currentTorrentFile, signingWithSha256()); |
| 288 | torrentSegments.push_back(currentTorrentFile); |
| 289 | |
| 290 | for (auto it = torrentSegments.rbegin() + 1; it != torrentSegments.rend(); ++it) { |
| 291 | auto next = it - 1; |
| 292 | it->setTorrentFilePtr(next->getFullName()); |
| 293 | keyChain.sign(*it, signingWithSha256()); |
| 294 | } |
| 295 | |
| 296 | torrentSegments.shrink_to_fit(); |
| 297 | manifestPairs.shrink_to_fit(); |
| 298 | return std::make_pair(torrentSegments, manifestPairs); |
| 299 | } |
| 300 | |
spirosmastorakis | a6057f5 | 2016-01-28 13:34:41 -0800 | [diff] [blame] | 301 | } // namespace ntorrent |
| 302 | |
| 303 | } // namespace ndn |