blob: c612352e0c50020b366c6d6e1e0f6e8fd993e6f4 [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 Pesaventob3570c62022-02-19 19:19:00 -05003 * Copyright (c) 2013-2022, 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
Davide Pesaventob3570c62022-02-19 19:19:00 -050032namespace ndn::dissect {
Junxiao Shie5adbfd2015-06-18 14:42:06 -070033
Davide Pesaventoa420e972021-02-25 00:56:25 -050034Dissector::Dissector(std::istream& input, std::ostream& output, const Options& options)
35 : m_options(options)
36 , m_in(input)
Davide Pesavento7ef5cf72021-01-29 00:20:24 -050037 , m_out(output)
38{
39}
40
41void
Davide Pesaventoa420e972021-02-25 00:56:25 -050042Dissector::dissect()
Davide Pesavento7ef5cf72021-01-29 00:20:24 -050043{
44 size_t offset = 0;
45 try {
46 while (m_in.peek() != std::istream::traits_type::eof()) {
47 auto block = Block::fromStream(m_in);
48 printBlock(block);
49 offset += block.size();
50 }
51 }
52 catch (const std::exception& e) {
53 std::cerr << "ERROR: " << e.what() << " at offset " << offset << "\n";
54 }
55}
56
57// http://git.altlinux.org/people/legion/packages/kbd.git?p=kbd.git;a=blob;f=data/consolefonts/README.eurlatgr
58static const char GLYPH_VERTICAL[] = "\u2502 "; // "│ "
59static const char GLYPH_VERTICAL_AND_RIGHT[] = "\u251c\u2500"; // "├─"
60static const char GLYPH_UP_AND_RIGHT[] = "\u2514\u2500"; // "└─"
61static const char GLYPH_SPACE[] = " ";
62
63void
Davide Pesaventoa420e972021-02-25 00:56:25 -050064Dissector::printBranches()
Davide Pesavento7ef5cf72021-01-29 00:20:24 -050065{
66 for (size_t i = 0; i < m_branches.size(); ++i) {
67 if (i == m_branches.size() - 1) {
68 m_out << (m_branches[i] ? GLYPH_VERTICAL_AND_RIGHT : GLYPH_UP_AND_RIGHT);
69 }
70 else {
71 m_out << (m_branches[i] ? GLYPH_VERTICAL : GLYPH_SPACE);
72 }
73 }
74}
75
Davide Pesaventob3570c62022-02-19 19:19:00 -050076static const std::map<uint32_t, std::string_view> TLV_DICT = {
Junxiao Shie5adbfd2015-06-18 14:42:06 -070077 {tlv::Interest , "Interest"},
78 {tlv::Data , "Data"},
79 {tlv::Name , "Name"},
Davide Pesavento79482ec2021-11-24 21:07:36 -050080 // Interest packet
Davide Pesaventoa2d4b142018-04-20 16:46:29 -040081 {tlv::CanBePrefix , "CanBePrefix"},
82 {tlv::MustBeFresh , "MustBeFresh"},
Davide Pesavento79482ec2021-11-24 21:07:36 -050083 {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"},
Davide Pesavento79482ec2021-11-24 21:07:36 -050088 {tlv::InterestSignatureInfo , "InterestSignatureInfo"},
89 {tlv::InterestSignatureValue , "InterestSignatureValue"},
90 // Data packet
Junxiao Shie5adbfd2015-06-18 14:42:06 -070091 {tlv::MetaInfo , "MetaInfo"},
92 {tlv::Content , "Content"},
93 {tlv::SignatureInfo , "SignatureInfo"},
94 {tlv::SignatureValue , "SignatureValue"},
95 {tlv::ContentType , "ContentType"},
96 {tlv::FreshnessPeriod , "FreshnessPeriod"},
97 {tlv::FinalBlockId , "FinalBlockId"},
Davide Pesavento79482ec2021-11-24 21:07:36 -050098 // (Interest)SignatureInfo elements
Junxiao Shie5adbfd2015-06-18 14:42:06 -070099 {tlv::SignatureType , "SignatureType"},
100 {tlv::KeyLocator , "KeyLocator"},
101 {tlv::KeyDigest , "KeyDigest"},
Davide Pesavento79482ec2021-11-24 21:07:36 -0500102 {tlv::SignatureNonce , "SignatureNonce"},
103 {tlv::SignatureTime , "SignatureTime"},
104 {tlv::SignatureSeqNum , "SignatureSeqNum"},
Davide Pesavento105cd9e2019-04-06 18:13:44 -0400105 // Name components
Davide Pesavento79482ec2021-11-24 21:07:36 -0500106 {tlv::GenericNameComponent , "GenericNameComponent"},
107 {tlv::ImplicitSha256DigestComponent , "ImplicitSha256DigestComponent"},
108 {tlv::ParametersSha256DigestComponent , "ParametersSha256DigestComponent"},
109 {tlv::KeywordNameComponent , "KeywordNameComponent"},
110 {tlv::SegmentNameComponent , "SegmentNameComponent"},
111 {tlv::ByteOffsetNameComponent , "ByteOffsetNameComponent"},
112 {tlv::VersionNameComponent , "VersionNameComponent"},
113 {tlv::TimestampNameComponent , "TimestampNameComponent"},
114 {tlv::SequenceNumNameComponent , "SequenceNumNameComponent"},
Davide Pesaventoa2d4b142018-04-20 16:46:29 -0400115 // Deprecated elements
116 {tlv::Selectors , "Selectors"},
117 {tlv::MinSuffixComponents , "MinSuffixComponents"},
118 {tlv::MaxSuffixComponents , "MaxSuffixComponents"},
119 {tlv::PublisherPublicKeyLocator , "PublisherPublicKeyLocator"},
120 {tlv::Exclude , "Exclude"},
121 {tlv::ChildSelector , "ChildSelector"},
122 {tlv::Any , "Any"},
Junxiao Shie5adbfd2015-06-18 14:42:06 -0700123};
124
125void
Davide Pesaventoa420e972021-02-25 00:56:25 -0500126Dissector::printType(uint32_t type)
Junxiao Shie5adbfd2015-06-18 14:42:06 -0700127{
Davide Pesavento7ef5cf72021-01-29 00:20:24 -0500128 m_out << type << " (";
Junxiao Shie5adbfd2015-06-18 14:42:06 -0700129
Davide Pesaventoc0702702017-08-24 22:04:00 -0400130 auto it = TLV_DICT.find(type);
131 if (it != TLV_DICT.end()) {
Davide Pesavento7ef5cf72021-01-29 00:20:24 -0500132 m_out << it->second;
Junxiao Shie5adbfd2015-06-18 14:42:06 -0700133 }
134 else if (type < tlv::AppPrivateBlock1) {
Davide Pesavento7ef5cf72021-01-29 00:20:24 -0500135 m_out << "RESERVED_1";
Junxiao Shie5adbfd2015-06-18 14:42:06 -0700136 }
137 else if (tlv::AppPrivateBlock1 <= type && type < 253) {
Davide Pesavento7ef5cf72021-01-29 00:20:24 -0500138 m_out << "APP_TAG_1";
Junxiao Shie5adbfd2015-06-18 14:42:06 -0700139 }
140 else if (253 <= type && type < tlv::AppPrivateBlock2) {
Davide Pesavento7ef5cf72021-01-29 00:20:24 -0500141 m_out << "RESERVED_3";
Junxiao Shie5adbfd2015-06-18 14:42:06 -0700142 }
143 else {
Davide Pesavento7ef5cf72021-01-29 00:20:24 -0500144 m_out << "APP_TAG_3";
Junxiao Shie5adbfd2015-06-18 14:42:06 -0700145 }
Davide Pesaventoc0702702017-08-24 22:04:00 -0400146
Davide Pesavento7ef5cf72021-01-29 00:20:24 -0500147 m_out << ")";
Junxiao Shie5adbfd2015-06-18 14:42:06 -0700148}
149
Junxiao Shie5adbfd2015-06-18 14:42:06 -0700150void
Davide Pesaventoa420e972021-02-25 00:56:25 -0500151Dissector::printBlock(const Block& block)
Junxiao Shie5adbfd2015-06-18 14:42:06 -0700152{
Davide Pesavento7ef5cf72021-01-29 00:20:24 -0500153 printBranches();
154 printType(block.type());
155 m_out << " (size: " << block.value_size() << ")";
Junxiao Shie5adbfd2015-06-18 14:42:06 -0700156
157 try {
Davide Pesaventoa420e972021-02-25 00:56:25 -0500158 if (block.type() != tlv::SignatureValue &&
Davide Pesavento79482ec2021-11-24 21:07:36 -0500159 block.type() != tlv::InterestSignatureValue &&
Davide Pesaventoa420e972021-02-25 00:56:25 -0500160 (block.type() != tlv::Content || m_options.dissectContent)) {
Davide Pesavento3f588ca2021-02-24 15:48:33 -0500161 block.parse();
162 }
Junxiao Shie5adbfd2015-06-18 14:42:06 -0700163 }
Davide Pesaventoc0702702017-08-24 22:04:00 -0400164 catch (const tlv::Error&) {
Junxiao Shie5adbfd2015-06-18 14:42:06 -0700165 // pass (e.g., leaf block reached)
Junxiao Shie5adbfd2015-06-18 14:42:06 -0700166 }
167
Davide Pesavento7ef5cf72021-01-29 00:20:24 -0500168 const auto& elements = block.elements();
169 if (elements.empty()) {
170 m_out << " [[";
171 name::Component(block.value(), block.value_size()).toUri(m_out);
172 m_out << "]]";
Junxiao Shi78c78cc2015-06-19 15:53:53 -0700173 }
Davide Pesavento7ef5cf72021-01-29 00:20:24 -0500174 m_out << "\n";
Junxiao Shie5adbfd2015-06-18 14:42:06 -0700175
Davide Pesavento7ef5cf72021-01-29 00:20:24 -0500176 m_branches.push_back(true);
177 for (size_t i = 0; i < elements.size(); ++i) {
178 if (i == elements.size() - 1) {
179 // no more branches to draw at this level of the tree
180 m_branches.back() = false;
Junxiao Shie5adbfd2015-06-18 14:42:06 -0700181 }
Davide Pesavento7ef5cf72021-01-29 00:20:24 -0500182 printBlock(elements[i]);
Davide Pesavento9b1fd4b2021-01-26 22:16:02 -0500183 }
Davide Pesavento7ef5cf72021-01-29 00:20:24 -0500184 m_branches.pop_back();
Junxiao Shie5adbfd2015-06-18 14:42:06 -0700185}
186
Davide Pesaventob3570c62022-02-19 19:19:00 -0500187} // namespace ndn::dissect