| /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| /* |
| * Copyright (c) 2013-2021, Regents of the University of California. |
| * |
| * This file is part of ndn-tools (Named Data Networking Essential Tools). |
| * See AUTHORS.md for complete list of ndn-tools authors and contributors. |
| * |
| * ndn-tools 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 3 of the License, or (at your option) any later version. |
| * |
| * ndn-tools 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 |
| * ndn-tools, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
| * |
| * See AUTHORS.md for complete list of ndn-cxx authors and contributors. |
| * |
| * @author Alexander Afanasyev <http://lasr.cs.ucla.edu/afanasyev/index.html> |
| */ |
| |
| #include "ndn-dissect.hpp" |
| |
| #include <algorithm> |
| #include <map> |
| |
| #include <ndn-cxx/name-component.hpp> |
| #include <ndn-cxx/encoding/tlv.hpp> |
| #include <ndn-cxx/util/indented-stream.hpp> |
| |
| namespace ndn { |
| namespace dissect { |
| |
| static const std::map<uint32_t, const char*> TLV_DICT = { |
| {tlv::Interest , "Interest"}, |
| {tlv::Data , "Data"}, |
| {tlv::Name , "Name"}, |
| {tlv::CanBePrefix , "CanBePrefix"}, |
| {tlv::MustBeFresh , "MustBeFresh"}, |
| //{tlv::ForwardingHint , "ForwardingHint"}, |
| {tlv::Nonce , "Nonce"}, |
| {tlv::InterestLifetime , "InterestLifetime"}, |
| {tlv::HopLimit , "HopLimit"}, |
| {tlv::ApplicationParameters , "ApplicationParameters"}, |
| {tlv::MetaInfo , "MetaInfo"}, |
| {tlv::Content , "Content"}, |
| {tlv::SignatureInfo , "SignatureInfo"}, |
| {tlv::SignatureValue , "SignatureValue"}, |
| {tlv::ContentType , "ContentType"}, |
| {tlv::FreshnessPeriod , "FreshnessPeriod"}, |
| {tlv::FinalBlockId , "FinalBlockId"}, |
| {tlv::SignatureType , "SignatureType"}, |
| {tlv::KeyLocator , "KeyLocator"}, |
| {tlv::KeyDigest , "KeyDigest"}, |
| // Name components |
| {tlv::GenericNameComponent , "GenericNameComponent"}, |
| {tlv::ImplicitSha256DigestComponent , "ImplicitSha256DigestComponent"}, |
| {tlv::ParametersSha256DigestComponent, "ParametersSha256DigestComponent"}, |
| {tlv::KeywordNameComponent , "KeywordNameComponent"}, |
| //{tlv::SegmentNameComponent , "SegmentNameComponent"}, |
| //{tlv::ByteOffsetNameComponent , "ByteOffsetNameComponent"}, |
| //{tlv::VersionNameComponent , "VersionNameComponent"}, |
| //{tlv::TimestampNameComponent , "TimestampNameComponent"}, |
| //{tlv::SequenceNumNameComponent , "SequenceNumNameComponent"}, |
| // Deprecated elements |
| {tlv::Selectors , "Selectors"}, |
| {tlv::MinSuffixComponents , "MinSuffixComponents"}, |
| {tlv::MaxSuffixComponents , "MaxSuffixComponents"}, |
| {tlv::PublisherPublicKeyLocator , "PublisherPublicKeyLocator"}, |
| {tlv::Exclude , "Exclude"}, |
| {tlv::ChildSelector , "ChildSelector"}, |
| {tlv::Any , "Any"}, |
| }; |
| |
| void |
| NdnDissect::printType(std::ostream& os, uint32_t type) |
| { |
| os << type << " ("; |
| |
| auto it = TLV_DICT.find(type); |
| if (it != TLV_DICT.end()) { |
| os << it->second; |
| } |
| else if (type < tlv::AppPrivateBlock1) { |
| os << "RESERVED_1"; |
| } |
| else if (tlv::AppPrivateBlock1 <= type && type < 253) { |
| os << "APP_TAG_1"; |
| } |
| else if (253 <= type && type < tlv::AppPrivateBlock2) { |
| os << "RESERVED_3"; |
| } |
| else { |
| os << "APP_TAG_3"; |
| } |
| |
| os << ")"; |
| } |
| |
| void |
| NdnDissect::printBlock(std::ostream& os, const Block& block) |
| { |
| printType(os, block.type()); |
| os << " (size: " << block.value_size() << ")"; |
| |
| try { |
| // if (block.type() != tlv::Content && block.type() != tlv::SignatureValue) |
| block.parse(); |
| } |
| catch (const tlv::Error&) { |
| // pass (e.g., leaf block reached) |
| |
| // @todo: Figure how to deterministically figure out that value is not recursive TLV block |
| } |
| |
| if (block.elements().empty()) { |
| os << " [["; |
| name::Component(block.value(), block.value_size()).toUri(os); |
| os << "]]"; |
| } |
| os << "\n"; |
| |
| util::IndentedStream os2(os, " "); |
| std::for_each(block.elements_begin(), block.elements_end(), |
| [this, &os2] (const Block& element) { printBlock(os2, element); }); |
| } |
| |
| void |
| NdnDissect::dissect(std::ostream& os, std::istream& is) |
| { |
| size_t offset = 0; |
| try { |
| while (is.peek() != std::istream::traits_type::eof()) { |
| auto block = Block::fromStream(is); |
| printBlock(os, block); |
| offset += block.size(); |
| } |
| } |
| catch (const std::exception& e) { |
| std::cerr << "ERROR: " << e.what() << " at offset " << offset << "\n"; |
| } |
| } |
| |
| } // namespace dissect |
| } // namespace ndn |