blob: cf012d85147eb0f0dc27a2ffc067cb833ffbbe9e [file] [log] [blame]
Junxiao Shie5adbfd2015-06-18 14:42:06 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Davide Pesaventoa2d4b142018-04-20 16:46:29 -04002/*
Davide Pesaventoa0e6b602021-01-21 19:47:04 -05003 * Copyright (c) 2013-2021, Regents of the University of California.
Junxiao Shi78c78cc2015-06-19 15:53:53 -07004 *
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 Shie5adbfd2015-06-18 14:42:06 -070018 *
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 Shi78c78cc2015-06-19 15:53:53 -070024#include "ndn-dissect.hpp"
Junxiao Shie5adbfd2015-06-18 14:42:06 -070025
Davide Pesaventoa0e6b602021-01-21 19:47:04 -050026#include <map>
Junxiao Shie5adbfd2015-06-18 14:42:06 -070027
Davide Pesaventoa2d4b142018-04-20 16:46:29 -040028#include <ndn-cxx/encoding/tlv.hpp>
Davide Pesavento7ef5cf72021-01-29 00:20:24 -050029#include <ndn-cxx/name-component.hpp>
Junxiao Shi78c78cc2015-06-19 15:53:53 -070030
Junxiao Shie5adbfd2015-06-18 14:42:06 -070031namespace ndn {
Junxiao Shi78c78cc2015-06-19 15:53:53 -070032namespace dissect {
Junxiao Shie5adbfd2015-06-18 14:42:06 -070033
Davide Pesavento7ef5cf72021-01-29 00:20:24 -050034NdnDissect::NdnDissect(std::istream& input, std::ostream& output)
35 : m_in(input)
36 , m_out(output)
37{
38}
39
40void
41NdnDissect::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
57static const char GLYPH_VERTICAL[] = "\u2502 "; // "│ "
58static const char GLYPH_VERTICAL_AND_RIGHT[] = "\u251c\u2500"; // "├─"
59static const char GLYPH_UP_AND_RIGHT[] = "\u2514\u2500"; // "└─"
60static const char GLYPH_SPACE[] = " ";
61
62void
63NdnDissect::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 Pesaventoa2d4b142018-04-20 16:46:29 -040075static const std::map<uint32_t, const char*> TLV_DICT = {
Junxiao Shie5adbfd2015-06-18 14:42:06 -070076 {tlv::Interest , "Interest"},
77 {tlv::Data , "Data"},
78 {tlv::Name , "Name"},
Davide Pesaventoa2d4b142018-04-20 16:46:29 -040079 {tlv::CanBePrefix , "CanBePrefix"},
80 {tlv::MustBeFresh , "MustBeFresh"},
81 //{tlv::ForwardingHint , "ForwardingHint"},
Junxiao Shie5adbfd2015-06-18 14:42:06 -070082 {tlv::Nonce , "Nonce"},
83 {tlv::InterestLifetime , "InterestLifetime"},
Davide Pesaventoa2d4b142018-04-20 16:46:29 -040084 {tlv::HopLimit , "HopLimit"},
Davide Pesavento105cd9e2019-04-06 18:13:44 -040085 {tlv::ApplicationParameters , "ApplicationParameters"},
Junxiao Shie5adbfd2015-06-18 14:42:06 -070086 {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 Pesavento105cd9e2019-04-06 18:13:44 -040096 // 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 Pesaventoa2d4b142018-04-20 16:46:29 -0400106 // 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 Shie5adbfd2015-06-18 14:42:06 -0700114};
115
116void
Davide Pesavento7ef5cf72021-01-29 00:20:24 -0500117NdnDissect::printType(uint32_t type)
Junxiao Shie5adbfd2015-06-18 14:42:06 -0700118{
Davide Pesavento7ef5cf72021-01-29 00:20:24 -0500119 m_out << type << " (";
Junxiao Shie5adbfd2015-06-18 14:42:06 -0700120
Davide Pesaventoc0702702017-08-24 22:04:00 -0400121 auto it = TLV_DICT.find(type);
122 if (it != TLV_DICT.end()) {
Davide Pesavento7ef5cf72021-01-29 00:20:24 -0500123 m_out << it->second;
Junxiao Shie5adbfd2015-06-18 14:42:06 -0700124 }
125 else if (type < tlv::AppPrivateBlock1) {
Davide Pesavento7ef5cf72021-01-29 00:20:24 -0500126 m_out << "RESERVED_1";
Junxiao Shie5adbfd2015-06-18 14:42:06 -0700127 }
128 else if (tlv::AppPrivateBlock1 <= type && type < 253) {
Davide Pesavento7ef5cf72021-01-29 00:20:24 -0500129 m_out << "APP_TAG_1";
Junxiao Shie5adbfd2015-06-18 14:42:06 -0700130 }
131 else if (253 <= type && type < tlv::AppPrivateBlock2) {
Davide Pesavento7ef5cf72021-01-29 00:20:24 -0500132 m_out << "RESERVED_3";
Junxiao Shie5adbfd2015-06-18 14:42:06 -0700133 }
134 else {
Davide Pesavento7ef5cf72021-01-29 00:20:24 -0500135 m_out << "APP_TAG_3";
Junxiao Shie5adbfd2015-06-18 14:42:06 -0700136 }
Davide Pesaventoc0702702017-08-24 22:04:00 -0400137
Davide Pesavento7ef5cf72021-01-29 00:20:24 -0500138 m_out << ")";
Junxiao Shie5adbfd2015-06-18 14:42:06 -0700139}
140
Junxiao Shie5adbfd2015-06-18 14:42:06 -0700141void
Davide Pesavento7ef5cf72021-01-29 00:20:24 -0500142NdnDissect::printBlock(const Block& block)
Junxiao Shie5adbfd2015-06-18 14:42:06 -0700143{
Davide Pesavento7ef5cf72021-01-29 00:20:24 -0500144 printBranches();
145 printType(block.type());
146 m_out << " (size: " << block.value_size() << ")";
Junxiao Shie5adbfd2015-06-18 14:42:06 -0700147
148 try {
149 // if (block.type() != tlv::Content && block.type() != tlv::SignatureValue)
150 block.parse();
151 }
Davide Pesaventoc0702702017-08-24 22:04:00 -0400152 catch (const tlv::Error&) {
Junxiao Shie5adbfd2015-06-18 14:42:06 -0700153 // pass (e.g., leaf block reached)
154
155 // @todo: Figure how to deterministically figure out that value is not recursive TLV block
156 }
157
Davide Pesavento7ef5cf72021-01-29 00:20:24 -0500158 const auto& elements = block.elements();
159 if (elements.empty()) {
160 m_out << " [[";
161 name::Component(block.value(), block.value_size()).toUri(m_out);
162 m_out << "]]";
Junxiao Shi78c78cc2015-06-19 15:53:53 -0700163 }
Davide Pesavento7ef5cf72021-01-29 00:20:24 -0500164 m_out << "\n";
Junxiao Shie5adbfd2015-06-18 14:42:06 -0700165
Davide Pesavento7ef5cf72021-01-29 00:20:24 -0500166 m_branches.push_back(true);
167 for (size_t i = 0; i < elements.size(); ++i) {
168 if (i == elements.size() - 1) {
169 // no more branches to draw at this level of the tree
170 m_branches.back() = false;
Junxiao Shie5adbfd2015-06-18 14:42:06 -0700171 }
Davide Pesavento7ef5cf72021-01-29 00:20:24 -0500172 printBlock(elements[i]);
Davide Pesavento9b1fd4b2021-01-26 22:16:02 -0500173 }
Davide Pesavento7ef5cf72021-01-29 00:20:24 -0500174 m_branches.pop_back();
Junxiao Shie5adbfd2015-06-18 14:42:06 -0700175}
176
Junxiao Shi78c78cc2015-06-19 15:53:53 -0700177} // namespace dissect
Junxiao Shie5adbfd2015-06-18 14:42:06 -0700178} // namespace ndn