blob: dfc5c9860659749ef16b946aa2dc57456a322e1d [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 Pesavento79482ec2021-11-24 21:07:36 -050081 // Interest packet
Davide Pesaventoa2d4b142018-04-20 16:46:29 -040082 {tlv::CanBePrefix , "CanBePrefix"},
83 {tlv::MustBeFresh , "MustBeFresh"},
Davide Pesavento79482ec2021-11-24 21:07:36 -050084 {tlv::ForwardingHint , "ForwardingHint"},
Junxiao Shie5adbfd2015-06-18 14:42:06 -070085 {tlv::Nonce , "Nonce"},
86 {tlv::InterestLifetime , "InterestLifetime"},
Davide Pesaventoa2d4b142018-04-20 16:46:29 -040087 {tlv::HopLimit , "HopLimit"},
Davide Pesavento105cd9e2019-04-06 18:13:44 -040088 {tlv::ApplicationParameters , "ApplicationParameters"},
Davide Pesavento79482ec2021-11-24 21:07:36 -050089 {tlv::InterestSignatureInfo , "InterestSignatureInfo"},
90 {tlv::InterestSignatureValue , "InterestSignatureValue"},
91 // Data packet
Junxiao Shie5adbfd2015-06-18 14:42:06 -070092 {tlv::MetaInfo , "MetaInfo"},
93 {tlv::Content , "Content"},
94 {tlv::SignatureInfo , "SignatureInfo"},
95 {tlv::SignatureValue , "SignatureValue"},
96 {tlv::ContentType , "ContentType"},
97 {tlv::FreshnessPeriod , "FreshnessPeriod"},
98 {tlv::FinalBlockId , "FinalBlockId"},
Davide Pesavento79482ec2021-11-24 21:07:36 -050099 // (Interest)SignatureInfo elements
Junxiao Shie5adbfd2015-06-18 14:42:06 -0700100 {tlv::SignatureType , "SignatureType"},
101 {tlv::KeyLocator , "KeyLocator"},
102 {tlv::KeyDigest , "KeyDigest"},
Davide Pesavento79482ec2021-11-24 21:07:36 -0500103 {tlv::SignatureNonce , "SignatureNonce"},
104 {tlv::SignatureTime , "SignatureTime"},
105 {tlv::SignatureSeqNum , "SignatureSeqNum"},
Davide Pesavento105cd9e2019-04-06 18:13:44 -0400106 // Name components
Davide Pesavento79482ec2021-11-24 21:07:36 -0500107 {tlv::GenericNameComponent , "GenericNameComponent"},
108 {tlv::ImplicitSha256DigestComponent , "ImplicitSha256DigestComponent"},
109 {tlv::ParametersSha256DigestComponent , "ParametersSha256DigestComponent"},
110 {tlv::KeywordNameComponent , "KeywordNameComponent"},
111 {tlv::SegmentNameComponent , "SegmentNameComponent"},
112 {tlv::ByteOffsetNameComponent , "ByteOffsetNameComponent"},
113 {tlv::VersionNameComponent , "VersionNameComponent"},
114 {tlv::TimestampNameComponent , "TimestampNameComponent"},
115 {tlv::SequenceNumNameComponent , "SequenceNumNameComponent"},
Davide Pesaventoa2d4b142018-04-20 16:46:29 -0400116 // Deprecated elements
117 {tlv::Selectors , "Selectors"},
118 {tlv::MinSuffixComponents , "MinSuffixComponents"},
119 {tlv::MaxSuffixComponents , "MaxSuffixComponents"},
120 {tlv::PublisherPublicKeyLocator , "PublisherPublicKeyLocator"},
121 {tlv::Exclude , "Exclude"},
122 {tlv::ChildSelector , "ChildSelector"},
123 {tlv::Any , "Any"},
Junxiao Shie5adbfd2015-06-18 14:42:06 -0700124};
125
126void
Davide Pesaventoa420e972021-02-25 00:56:25 -0500127Dissector::printType(uint32_t type)
Junxiao Shie5adbfd2015-06-18 14:42:06 -0700128{
Davide Pesavento7ef5cf72021-01-29 00:20:24 -0500129 m_out << type << " (";
Junxiao Shie5adbfd2015-06-18 14:42:06 -0700130
Davide Pesaventoc0702702017-08-24 22:04:00 -0400131 auto it = TLV_DICT.find(type);
132 if (it != TLV_DICT.end()) {
Davide Pesavento7ef5cf72021-01-29 00:20:24 -0500133 m_out << it->second;
Junxiao Shie5adbfd2015-06-18 14:42:06 -0700134 }
135 else if (type < tlv::AppPrivateBlock1) {
Davide Pesavento7ef5cf72021-01-29 00:20:24 -0500136 m_out << "RESERVED_1";
Junxiao Shie5adbfd2015-06-18 14:42:06 -0700137 }
138 else if (tlv::AppPrivateBlock1 <= type && type < 253) {
Davide Pesavento7ef5cf72021-01-29 00:20:24 -0500139 m_out << "APP_TAG_1";
Junxiao Shie5adbfd2015-06-18 14:42:06 -0700140 }
141 else if (253 <= type && type < tlv::AppPrivateBlock2) {
Davide Pesavento7ef5cf72021-01-29 00:20:24 -0500142 m_out << "RESERVED_3";
Junxiao Shie5adbfd2015-06-18 14:42:06 -0700143 }
144 else {
Davide Pesavento7ef5cf72021-01-29 00:20:24 -0500145 m_out << "APP_TAG_3";
Junxiao Shie5adbfd2015-06-18 14:42:06 -0700146 }
Davide Pesaventoc0702702017-08-24 22:04:00 -0400147
Davide Pesavento7ef5cf72021-01-29 00:20:24 -0500148 m_out << ")";
Junxiao Shie5adbfd2015-06-18 14:42:06 -0700149}
150
Junxiao Shie5adbfd2015-06-18 14:42:06 -0700151void
Davide Pesaventoa420e972021-02-25 00:56:25 -0500152Dissector::printBlock(const Block& block)
Junxiao Shie5adbfd2015-06-18 14:42:06 -0700153{
Davide Pesavento7ef5cf72021-01-29 00:20:24 -0500154 printBranches();
155 printType(block.type());
156 m_out << " (size: " << block.value_size() << ")";
Junxiao Shie5adbfd2015-06-18 14:42:06 -0700157
158 try {
Davide Pesaventoa420e972021-02-25 00:56:25 -0500159 if (block.type() != tlv::SignatureValue &&
Davide Pesavento79482ec2021-11-24 21:07:36 -0500160 block.type() != tlv::InterestSignatureValue &&
Davide Pesaventoa420e972021-02-25 00:56:25 -0500161 (block.type() != tlv::Content || m_options.dissectContent)) {
Davide Pesavento3f588ca2021-02-24 15:48:33 -0500162 block.parse();
163 }
Junxiao Shie5adbfd2015-06-18 14:42:06 -0700164 }
Davide Pesaventoc0702702017-08-24 22:04:00 -0400165 catch (const tlv::Error&) {
Junxiao Shie5adbfd2015-06-18 14:42:06 -0700166 // pass (e.g., leaf block reached)
Junxiao Shie5adbfd2015-06-18 14:42:06 -0700167 }
168
Davide Pesavento7ef5cf72021-01-29 00:20:24 -0500169 const auto& elements = block.elements();
170 if (elements.empty()) {
171 m_out << " [[";
172 name::Component(block.value(), block.value_size()).toUri(m_out);
173 m_out << "]]";
Junxiao Shi78c78cc2015-06-19 15:53:53 -0700174 }
Davide Pesavento7ef5cf72021-01-29 00:20:24 -0500175 m_out << "\n";
Junxiao Shie5adbfd2015-06-18 14:42:06 -0700176
Davide Pesavento7ef5cf72021-01-29 00:20:24 -0500177 m_branches.push_back(true);
178 for (size_t i = 0; i < elements.size(); ++i) {
179 if (i == elements.size() - 1) {
180 // no more branches to draw at this level of the tree
181 m_branches.back() = false;
Junxiao Shie5adbfd2015-06-18 14:42:06 -0700182 }
Davide Pesavento7ef5cf72021-01-29 00:20:24 -0500183 printBlock(elements[i]);
Davide Pesavento9b1fd4b2021-01-26 22:16:02 -0500184 }
Davide Pesavento7ef5cf72021-01-29 00:20:24 -0500185 m_branches.pop_back();
Junxiao Shie5adbfd2015-06-18 14:42:06 -0700186}
187
Junxiao Shi78c78cc2015-06-19 15:53:53 -0700188} // namespace dissect
Junxiao Shie5adbfd2015-06-18 14:42:06 -0700189} // namespace ndn