blob: 3c0ca5c21704b8593093441482d8bfe70e5bf089 [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 *
Davide Pesaventoa420e972021-02-25 00:56:25 -050021 * @author Alexander Afanasyev
22 * @author Davide Pesavento
Junxiao Shie5adbfd2015-06-18 14:42:06 -070023 */
24
Davide Pesaventoa420e972021-02-25 00:56:25 -050025#include "dissector.hpp"
Junxiao Shie5adbfd2015-06-18 14:42:06 -070026
Davide Pesaventoa0e6b602021-01-21 19:47:04 -050027#include <map>
Junxiao Shie5adbfd2015-06-18 14:42:06 -070028
Davide Pesaventoa2d4b142018-04-20 16:46:29 -040029#include <ndn-cxx/encoding/tlv.hpp>
Davide Pesavento7ef5cf72021-01-29 00:20:24 -050030#include <ndn-cxx/name-component.hpp>
Junxiao Shi78c78cc2015-06-19 15:53:53 -070031
Junxiao Shie5adbfd2015-06-18 14:42:06 -070032namespace ndn {
Junxiao Shi78c78cc2015-06-19 15:53:53 -070033namespace dissect {
Junxiao Shie5adbfd2015-06-18 14:42:06 -070034
Davide Pesaventoa420e972021-02-25 00:56:25 -050035Dissector::Dissector(std::istream& input, std::ostream& output, const Options& options)
36 : m_options(options)
37 , m_in(input)
Davide Pesavento7ef5cf72021-01-29 00:20:24 -050038 , m_out(output)
39{
40}
41
42void
Davide Pesaventoa420e972021-02-25 00:56:25 -050043Dissector::dissect()
Davide Pesavento7ef5cf72021-01-29 00:20:24 -050044{
45 size_t offset = 0;
46 try {
47 while (m_in.peek() != std::istream::traits_type::eof()) {
48 auto block = Block::fromStream(m_in);
49 printBlock(block);
50 offset += block.size();
51 }
52 }
53 catch (const std::exception& e) {
54 std::cerr << "ERROR: " << e.what() << " at offset " << offset << "\n";
55 }
56}
57
58// http://git.altlinux.org/people/legion/packages/kbd.git?p=kbd.git;a=blob;f=data/consolefonts/README.eurlatgr
59static const char GLYPH_VERTICAL[] = "\u2502 "; // "│ "
60static const char GLYPH_VERTICAL_AND_RIGHT[] = "\u251c\u2500"; // "├─"
61static const char GLYPH_UP_AND_RIGHT[] = "\u2514\u2500"; // "└─"
62static const char GLYPH_SPACE[] = " ";
63
64void
Davide Pesaventoa420e972021-02-25 00:56:25 -050065Dissector::printBranches()
Davide Pesavento7ef5cf72021-01-29 00:20:24 -050066{
67 for (size_t i = 0; i < m_branches.size(); ++i) {
68 if (i == m_branches.size() - 1) {
69 m_out << (m_branches[i] ? GLYPH_VERTICAL_AND_RIGHT : GLYPH_UP_AND_RIGHT);
70 }
71 else {
72 m_out << (m_branches[i] ? GLYPH_VERTICAL : GLYPH_SPACE);
73 }
74 }
75}
76
Davide Pesaventoa2d4b142018-04-20 16:46:29 -040077static const std::map<uint32_t, const char*> TLV_DICT = {
Junxiao Shie5adbfd2015-06-18 14:42:06 -070078 {tlv::Interest , "Interest"},
79 {tlv::Data , "Data"},
80 {tlv::Name , "Name"},
Davide Pesaventoa2d4b142018-04-20 16:46:29 -040081 {tlv::CanBePrefix , "CanBePrefix"},
82 {tlv::MustBeFresh , "MustBeFresh"},
83 //{tlv::ForwardingHint , "ForwardingHint"},
Junxiao Shie5adbfd2015-06-18 14:42:06 -070084 {tlv::Nonce , "Nonce"},
85 {tlv::InterestLifetime , "InterestLifetime"},
Davide Pesaventoa2d4b142018-04-20 16:46:29 -040086 {tlv::HopLimit , "HopLimit"},
Davide Pesavento105cd9e2019-04-06 18:13:44 -040087 {tlv::ApplicationParameters , "ApplicationParameters"},
Junxiao Shie5adbfd2015-06-18 14:42:06 -070088 {tlv::MetaInfo , "MetaInfo"},
89 {tlv::Content , "Content"},
90 {tlv::SignatureInfo , "SignatureInfo"},
91 {tlv::SignatureValue , "SignatureValue"},
92 {tlv::ContentType , "ContentType"},
93 {tlv::FreshnessPeriod , "FreshnessPeriod"},
94 {tlv::FinalBlockId , "FinalBlockId"},
95 {tlv::SignatureType , "SignatureType"},
96 {tlv::KeyLocator , "KeyLocator"},
97 {tlv::KeyDigest , "KeyDigest"},
Davide Pesavento105cd9e2019-04-06 18:13:44 -040098 // Name components
99 {tlv::GenericNameComponent , "GenericNameComponent"},
100 {tlv::ImplicitSha256DigestComponent , "ImplicitSha256DigestComponent"},
101 {tlv::ParametersSha256DigestComponent, "ParametersSha256DigestComponent"},
102 {tlv::KeywordNameComponent , "KeywordNameComponent"},
103 //{tlv::SegmentNameComponent , "SegmentNameComponent"},
104 //{tlv::ByteOffsetNameComponent , "ByteOffsetNameComponent"},
105 //{tlv::VersionNameComponent , "VersionNameComponent"},
106 //{tlv::TimestampNameComponent , "TimestampNameComponent"},
107 //{tlv::SequenceNumNameComponent , "SequenceNumNameComponent"},
Davide Pesaventoa2d4b142018-04-20 16:46:29 -0400108 // Deprecated elements
109 {tlv::Selectors , "Selectors"},
110 {tlv::MinSuffixComponents , "MinSuffixComponents"},
111 {tlv::MaxSuffixComponents , "MaxSuffixComponents"},
112 {tlv::PublisherPublicKeyLocator , "PublisherPublicKeyLocator"},
113 {tlv::Exclude , "Exclude"},
114 {tlv::ChildSelector , "ChildSelector"},
115 {tlv::Any , "Any"},
Junxiao Shie5adbfd2015-06-18 14:42:06 -0700116};
117
118void
Davide Pesaventoa420e972021-02-25 00:56:25 -0500119Dissector::printType(uint32_t type)
Junxiao Shie5adbfd2015-06-18 14:42:06 -0700120{
Davide Pesavento7ef5cf72021-01-29 00:20:24 -0500121 m_out << type << " (";
Junxiao Shie5adbfd2015-06-18 14:42:06 -0700122
Davide Pesaventoc0702702017-08-24 22:04:00 -0400123 auto it = TLV_DICT.find(type);
124 if (it != TLV_DICT.end()) {
Davide Pesavento7ef5cf72021-01-29 00:20:24 -0500125 m_out << it->second;
Junxiao Shie5adbfd2015-06-18 14:42:06 -0700126 }
127 else if (type < tlv::AppPrivateBlock1) {
Davide Pesavento7ef5cf72021-01-29 00:20:24 -0500128 m_out << "RESERVED_1";
Junxiao Shie5adbfd2015-06-18 14:42:06 -0700129 }
130 else if (tlv::AppPrivateBlock1 <= type && type < 253) {
Davide Pesavento7ef5cf72021-01-29 00:20:24 -0500131 m_out << "APP_TAG_1";
Junxiao Shie5adbfd2015-06-18 14:42:06 -0700132 }
133 else if (253 <= type && type < tlv::AppPrivateBlock2) {
Davide Pesavento7ef5cf72021-01-29 00:20:24 -0500134 m_out << "RESERVED_3";
Junxiao Shie5adbfd2015-06-18 14:42:06 -0700135 }
136 else {
Davide Pesavento7ef5cf72021-01-29 00:20:24 -0500137 m_out << "APP_TAG_3";
Junxiao Shie5adbfd2015-06-18 14:42:06 -0700138 }
Davide Pesaventoc0702702017-08-24 22:04:00 -0400139
Davide Pesavento7ef5cf72021-01-29 00:20:24 -0500140 m_out << ")";
Junxiao Shie5adbfd2015-06-18 14:42:06 -0700141}
142
Junxiao Shie5adbfd2015-06-18 14:42:06 -0700143void
Davide Pesaventoa420e972021-02-25 00:56:25 -0500144Dissector::printBlock(const Block& block)
Junxiao Shie5adbfd2015-06-18 14:42:06 -0700145{
Davide Pesavento7ef5cf72021-01-29 00:20:24 -0500146 printBranches();
147 printType(block.type());
148 m_out << " (size: " << block.value_size() << ")";
Junxiao Shie5adbfd2015-06-18 14:42:06 -0700149
150 try {
Davide Pesaventoa420e972021-02-25 00:56:25 -0500151 if (block.type() != tlv::SignatureValue &&
152 (block.type() != tlv::Content || m_options.dissectContent)) {
Davide Pesavento3f588ca2021-02-24 15:48:33 -0500153 block.parse();
154 }
Junxiao Shie5adbfd2015-06-18 14:42:06 -0700155 }
Davide Pesaventoc0702702017-08-24 22:04:00 -0400156 catch (const tlv::Error&) {
Junxiao Shie5adbfd2015-06-18 14:42:06 -0700157 // pass (e.g., leaf block reached)
Junxiao Shie5adbfd2015-06-18 14:42:06 -0700158 }
159
Davide Pesavento7ef5cf72021-01-29 00:20:24 -0500160 const auto& elements = block.elements();
161 if (elements.empty()) {
162 m_out << " [[";
163 name::Component(block.value(), block.value_size()).toUri(m_out);
164 m_out << "]]";
Junxiao Shi78c78cc2015-06-19 15:53:53 -0700165 }
Davide Pesavento7ef5cf72021-01-29 00:20:24 -0500166 m_out << "\n";
Junxiao Shie5adbfd2015-06-18 14:42:06 -0700167
Davide Pesavento7ef5cf72021-01-29 00:20:24 -0500168 m_branches.push_back(true);
169 for (size_t i = 0; i < elements.size(); ++i) {
170 if (i == elements.size() - 1) {
171 // no more branches to draw at this level of the tree
172 m_branches.back() = false;
Junxiao Shie5adbfd2015-06-18 14:42:06 -0700173 }
Davide Pesavento7ef5cf72021-01-29 00:20:24 -0500174 printBlock(elements[i]);
Davide Pesavento9b1fd4b2021-01-26 22:16:02 -0500175 }
Davide Pesavento7ef5cf72021-01-29 00:20:24 -0500176 m_branches.pop_back();
Junxiao Shie5adbfd2015-06-18 14:42:06 -0700177}
178
Junxiao Shi78c78cc2015-06-19 15:53:53 -0700179} // namespace dissect
Junxiao Shie5adbfd2015-06-18 14:42:06 -0700180} // namespace ndn