Junxiao Shi | e5adbfd | 2015-06-18 14:42:06 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Davide Pesavento | a2d4b14 | 2018-04-20 16:46:29 -0400 | [diff] [blame] | 2 | /* |
Davide Pesavento | a0e6b60 | 2021-01-21 19:47:04 -0500 | [diff] [blame] | 3 | * Copyright (c) 2013-2021, Regents of the University of California. |
Junxiao Shi | 78c78cc | 2015-06-19 15:53:53 -0700 | [diff] [blame] | 4 | * |
| 5 | * This file is part of ndn-tools (Named Data Networking Essential Tools). |
| 6 | * See AUTHORS.md for complete list of ndn-tools authors and contributors. |
| 7 | * |
| 8 | * ndn-tools is free software: you can redistribute it and/or modify it under the terms |
| 9 | * of the GNU General Public License as published by the Free Software Foundation, |
| 10 | * either version 3 of the License, or (at your option) any later version. |
| 11 | * |
| 12 | * ndn-tools is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
| 13 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
| 14 | * PURPOSE. See the GNU General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License along with |
| 17 | * ndn-tools, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
Junxiao Shi | e5adbfd | 2015-06-18 14:42:06 -0700 | [diff] [blame] | 18 | * |
| 19 | * See AUTHORS.md for complete list of ndn-cxx authors and contributors. |
| 20 | * |
| 21 | * @author Alexander Afanasyev <http://lasr.cs.ucla.edu/afanasyev/index.html> |
| 22 | */ |
| 23 | |
Junxiao Shi | 78c78cc | 2015-06-19 15:53:53 -0700 | [diff] [blame] | 24 | #include "ndn-dissect.hpp" |
Junxiao Shi | e5adbfd | 2015-06-18 14:42:06 -0700 | [diff] [blame] | 25 | |
Davide Pesavento | a0e6b60 | 2021-01-21 19:47:04 -0500 | [diff] [blame] | 26 | #include <map> |
Junxiao Shi | e5adbfd | 2015-06-18 14:42:06 -0700 | [diff] [blame] | 27 | |
Davide Pesavento | a2d4b14 | 2018-04-20 16:46:29 -0400 | [diff] [blame] | 28 | #include <ndn-cxx/encoding/tlv.hpp> |
Davide Pesavento | 7ef5cf7 | 2021-01-29 00:20:24 -0500 | [diff] [blame] | 29 | #include <ndn-cxx/name-component.hpp> |
Junxiao Shi | 78c78cc | 2015-06-19 15:53:53 -0700 | [diff] [blame] | 30 | |
Junxiao Shi | e5adbfd | 2015-06-18 14:42:06 -0700 | [diff] [blame] | 31 | namespace ndn { |
Junxiao Shi | 78c78cc | 2015-06-19 15:53:53 -0700 | [diff] [blame] | 32 | namespace dissect { |
Junxiao Shi | e5adbfd | 2015-06-18 14:42:06 -0700 | [diff] [blame] | 33 | |
Davide Pesavento | 7ef5cf7 | 2021-01-29 00:20:24 -0500 | [diff] [blame] | 34 | NdnDissect::NdnDissect(std::istream& input, std::ostream& output) |
| 35 | : m_in(input) |
| 36 | , m_out(output) |
| 37 | { |
| 38 | } |
| 39 | |
| 40 | void |
| 41 | NdnDissect::dissect() |
| 42 | { |
| 43 | size_t offset = 0; |
| 44 | try { |
| 45 | while (m_in.peek() != std::istream::traits_type::eof()) { |
| 46 | auto block = Block::fromStream(m_in); |
| 47 | printBlock(block); |
| 48 | offset += block.size(); |
| 49 | } |
| 50 | } |
| 51 | catch (const std::exception& e) { |
| 52 | std::cerr << "ERROR: " << e.what() << " at offset " << offset << "\n"; |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | // http://git.altlinux.org/people/legion/packages/kbd.git?p=kbd.git;a=blob;f=data/consolefonts/README.eurlatgr |
| 57 | static const char GLYPH_VERTICAL[] = "\u2502 "; // "│ " |
| 58 | static const char GLYPH_VERTICAL_AND_RIGHT[] = "\u251c\u2500"; // "├─" |
| 59 | static const char GLYPH_UP_AND_RIGHT[] = "\u2514\u2500"; // "└─" |
| 60 | static const char GLYPH_SPACE[] = " "; |
| 61 | |
| 62 | void |
| 63 | NdnDissect::printBranches() |
| 64 | { |
| 65 | for (size_t i = 0; i < m_branches.size(); ++i) { |
| 66 | if (i == m_branches.size() - 1) { |
| 67 | m_out << (m_branches[i] ? GLYPH_VERTICAL_AND_RIGHT : GLYPH_UP_AND_RIGHT); |
| 68 | } |
| 69 | else { |
| 70 | m_out << (m_branches[i] ? GLYPH_VERTICAL : GLYPH_SPACE); |
| 71 | } |
| 72 | } |
| 73 | } |
| 74 | |
Davide Pesavento | a2d4b14 | 2018-04-20 16:46:29 -0400 | [diff] [blame] | 75 | static const std::map<uint32_t, const char*> TLV_DICT = { |
Junxiao Shi | e5adbfd | 2015-06-18 14:42:06 -0700 | [diff] [blame] | 76 | {tlv::Interest , "Interest"}, |
| 77 | {tlv::Data , "Data"}, |
| 78 | {tlv::Name , "Name"}, |
Davide Pesavento | a2d4b14 | 2018-04-20 16:46:29 -0400 | [diff] [blame] | 79 | {tlv::CanBePrefix , "CanBePrefix"}, |
| 80 | {tlv::MustBeFresh , "MustBeFresh"}, |
| 81 | //{tlv::ForwardingHint , "ForwardingHint"}, |
Junxiao Shi | e5adbfd | 2015-06-18 14:42:06 -0700 | [diff] [blame] | 82 | {tlv::Nonce , "Nonce"}, |
| 83 | {tlv::InterestLifetime , "InterestLifetime"}, |
Davide Pesavento | a2d4b14 | 2018-04-20 16:46:29 -0400 | [diff] [blame] | 84 | {tlv::HopLimit , "HopLimit"}, |
Davide Pesavento | 105cd9e | 2019-04-06 18:13:44 -0400 | [diff] [blame] | 85 | {tlv::ApplicationParameters , "ApplicationParameters"}, |
Junxiao Shi | e5adbfd | 2015-06-18 14:42:06 -0700 | [diff] [blame] | 86 | {tlv::MetaInfo , "MetaInfo"}, |
| 87 | {tlv::Content , "Content"}, |
| 88 | {tlv::SignatureInfo , "SignatureInfo"}, |
| 89 | {tlv::SignatureValue , "SignatureValue"}, |
| 90 | {tlv::ContentType , "ContentType"}, |
| 91 | {tlv::FreshnessPeriod , "FreshnessPeriod"}, |
| 92 | {tlv::FinalBlockId , "FinalBlockId"}, |
| 93 | {tlv::SignatureType , "SignatureType"}, |
| 94 | {tlv::KeyLocator , "KeyLocator"}, |
| 95 | {tlv::KeyDigest , "KeyDigest"}, |
Davide Pesavento | 105cd9e | 2019-04-06 18:13:44 -0400 | [diff] [blame] | 96 | // Name components |
| 97 | {tlv::GenericNameComponent , "GenericNameComponent"}, |
| 98 | {tlv::ImplicitSha256DigestComponent , "ImplicitSha256DigestComponent"}, |
| 99 | {tlv::ParametersSha256DigestComponent, "ParametersSha256DigestComponent"}, |
| 100 | {tlv::KeywordNameComponent , "KeywordNameComponent"}, |
| 101 | //{tlv::SegmentNameComponent , "SegmentNameComponent"}, |
| 102 | //{tlv::ByteOffsetNameComponent , "ByteOffsetNameComponent"}, |
| 103 | //{tlv::VersionNameComponent , "VersionNameComponent"}, |
| 104 | //{tlv::TimestampNameComponent , "TimestampNameComponent"}, |
| 105 | //{tlv::SequenceNumNameComponent , "SequenceNumNameComponent"}, |
Davide Pesavento | a2d4b14 | 2018-04-20 16:46:29 -0400 | [diff] [blame] | 106 | // Deprecated elements |
| 107 | {tlv::Selectors , "Selectors"}, |
| 108 | {tlv::MinSuffixComponents , "MinSuffixComponents"}, |
| 109 | {tlv::MaxSuffixComponents , "MaxSuffixComponents"}, |
| 110 | {tlv::PublisherPublicKeyLocator , "PublisherPublicKeyLocator"}, |
| 111 | {tlv::Exclude , "Exclude"}, |
| 112 | {tlv::ChildSelector , "ChildSelector"}, |
| 113 | {tlv::Any , "Any"}, |
Junxiao Shi | e5adbfd | 2015-06-18 14:42:06 -0700 | [diff] [blame] | 114 | }; |
| 115 | |
| 116 | void |
Davide Pesavento | 7ef5cf7 | 2021-01-29 00:20:24 -0500 | [diff] [blame] | 117 | NdnDissect::printType(uint32_t type) |
Junxiao Shi | e5adbfd | 2015-06-18 14:42:06 -0700 | [diff] [blame] | 118 | { |
Davide Pesavento | 7ef5cf7 | 2021-01-29 00:20:24 -0500 | [diff] [blame] | 119 | m_out << type << " ("; |
Junxiao Shi | e5adbfd | 2015-06-18 14:42:06 -0700 | [diff] [blame] | 120 | |
Davide Pesavento | c070270 | 2017-08-24 22:04:00 -0400 | [diff] [blame] | 121 | auto it = TLV_DICT.find(type); |
| 122 | if (it != TLV_DICT.end()) { |
Davide Pesavento | 7ef5cf7 | 2021-01-29 00:20:24 -0500 | [diff] [blame] | 123 | m_out << it->second; |
Junxiao Shi | e5adbfd | 2015-06-18 14:42:06 -0700 | [diff] [blame] | 124 | } |
| 125 | else if (type < tlv::AppPrivateBlock1) { |
Davide Pesavento | 7ef5cf7 | 2021-01-29 00:20:24 -0500 | [diff] [blame] | 126 | m_out << "RESERVED_1"; |
Junxiao Shi | e5adbfd | 2015-06-18 14:42:06 -0700 | [diff] [blame] | 127 | } |
| 128 | else if (tlv::AppPrivateBlock1 <= type && type < 253) { |
Davide Pesavento | 7ef5cf7 | 2021-01-29 00:20:24 -0500 | [diff] [blame] | 129 | m_out << "APP_TAG_1"; |
Junxiao Shi | e5adbfd | 2015-06-18 14:42:06 -0700 | [diff] [blame] | 130 | } |
| 131 | else if (253 <= type && type < tlv::AppPrivateBlock2) { |
Davide Pesavento | 7ef5cf7 | 2021-01-29 00:20:24 -0500 | [diff] [blame] | 132 | m_out << "RESERVED_3"; |
Junxiao Shi | e5adbfd | 2015-06-18 14:42:06 -0700 | [diff] [blame] | 133 | } |
| 134 | else { |
Davide Pesavento | 7ef5cf7 | 2021-01-29 00:20:24 -0500 | [diff] [blame] | 135 | m_out << "APP_TAG_3"; |
Junxiao Shi | e5adbfd | 2015-06-18 14:42:06 -0700 | [diff] [blame] | 136 | } |
Davide Pesavento | c070270 | 2017-08-24 22:04:00 -0400 | [diff] [blame] | 137 | |
Davide Pesavento | 7ef5cf7 | 2021-01-29 00:20:24 -0500 | [diff] [blame] | 138 | m_out << ")"; |
Junxiao Shi | e5adbfd | 2015-06-18 14:42:06 -0700 | [diff] [blame] | 139 | } |
| 140 | |
Junxiao Shi | e5adbfd | 2015-06-18 14:42:06 -0700 | [diff] [blame] | 141 | void |
Davide Pesavento | 7ef5cf7 | 2021-01-29 00:20:24 -0500 | [diff] [blame] | 142 | NdnDissect::printBlock(const Block& block) |
Junxiao Shi | e5adbfd | 2015-06-18 14:42:06 -0700 | [diff] [blame] | 143 | { |
Davide Pesavento | 7ef5cf7 | 2021-01-29 00:20:24 -0500 | [diff] [blame] | 144 | printBranches(); |
| 145 | printType(block.type()); |
| 146 | m_out << " (size: " << block.value_size() << ")"; |
Junxiao Shi | e5adbfd | 2015-06-18 14:42:06 -0700 | [diff] [blame] | 147 | |
| 148 | try { |
Davide Pesavento | 3f588ca | 2021-02-24 15:48:33 -0500 | [diff] [blame^] | 149 | if (block.type() != tlv::SignatureValue) { |
| 150 | block.parse(); |
| 151 | } |
Junxiao Shi | e5adbfd | 2015-06-18 14:42:06 -0700 | [diff] [blame] | 152 | } |
Davide Pesavento | c070270 | 2017-08-24 22:04:00 -0400 | [diff] [blame] | 153 | catch (const tlv::Error&) { |
Junxiao Shi | e5adbfd | 2015-06-18 14:42:06 -0700 | [diff] [blame] | 154 | // pass (e.g., leaf block reached) |
Junxiao Shi | e5adbfd | 2015-06-18 14:42:06 -0700 | [diff] [blame] | 155 | } |
| 156 | |
Davide Pesavento | 7ef5cf7 | 2021-01-29 00:20:24 -0500 | [diff] [blame] | 157 | const auto& elements = block.elements(); |
| 158 | if (elements.empty()) { |
| 159 | m_out << " [["; |
| 160 | name::Component(block.value(), block.value_size()).toUri(m_out); |
| 161 | m_out << "]]"; |
Junxiao Shi | 78c78cc | 2015-06-19 15:53:53 -0700 | [diff] [blame] | 162 | } |
Davide Pesavento | 7ef5cf7 | 2021-01-29 00:20:24 -0500 | [diff] [blame] | 163 | m_out << "\n"; |
Junxiao Shi | e5adbfd | 2015-06-18 14:42:06 -0700 | [diff] [blame] | 164 | |
Davide Pesavento | 7ef5cf7 | 2021-01-29 00:20:24 -0500 | [diff] [blame] | 165 | m_branches.push_back(true); |
| 166 | for (size_t i = 0; i < elements.size(); ++i) { |
| 167 | if (i == elements.size() - 1) { |
| 168 | // no more branches to draw at this level of the tree |
| 169 | m_branches.back() = false; |
Junxiao Shi | e5adbfd | 2015-06-18 14:42:06 -0700 | [diff] [blame] | 170 | } |
Davide Pesavento | 7ef5cf7 | 2021-01-29 00:20:24 -0500 | [diff] [blame] | 171 | printBlock(elements[i]); |
Davide Pesavento | 9b1fd4b | 2021-01-26 22:16:02 -0500 | [diff] [blame] | 172 | } |
Davide Pesavento | 7ef5cf7 | 2021-01-29 00:20:24 -0500 | [diff] [blame] | 173 | m_branches.pop_back(); |
Junxiao Shi | e5adbfd | 2015-06-18 14:42:06 -0700 | [diff] [blame] | 174 | } |
| 175 | |
Junxiao Shi | 78c78cc | 2015-06-19 15:53:53 -0700 | [diff] [blame] | 176 | } // namespace dissect |
Junxiao Shi | e5adbfd | 2015-06-18 14:42:06 -0700 | [diff] [blame] | 177 | } // namespace ndn |