blob: c08a40d5ea2a8374dfd74787983a8e3233ef6e4c [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 Pesavento5748e822024-01-26 18:40:22 -05003 * Copyright (c) 2013-2024, 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 Pesaventoa2d4b142018-04-20 16:46:29 -040027#include <ndn-cxx/encoding/tlv.hpp>
Davide Pesaventod66fb492022-11-30 19:52:35 -050028#include <ndn-cxx/encoding/tlv-security.hpp>
Davide Pesavento242d5062022-03-11 16:34:23 -050029#include <ndn-cxx/util/string-helper.hpp>
Junxiao Shi78c78cc2015-06-19 15:53:53 -070030
Davide Pesavento5748e822024-01-26 18:40:22 -050031#include <iostream>
32#include <map>
33
Davide Pesaventob3570c62022-02-19 19:19:00 -050034namespace ndn::dissect {
Junxiao Shie5adbfd2015-06-18 14:42:06 -070035
Davide Pesaventoa420e972021-02-25 00:56:25 -050036Dissector::Dissector(std::istream& input, std::ostream& output, const Options& options)
37 : m_options(options)
38 , m_in(input)
Davide Pesavento7ef5cf72021-01-29 00:20:24 -050039 , m_out(output)
40{
41}
42
43void
Davide Pesaventoa420e972021-02-25 00:56:25 -050044Dissector::dissect()
Davide Pesavento7ef5cf72021-01-29 00:20:24 -050045{
46 size_t offset = 0;
47 try {
48 while (m_in.peek() != std::istream::traits_type::eof()) {
49 auto block = Block::fromStream(m_in);
50 printBlock(block);
51 offset += block.size();
52 }
53 }
54 catch (const std::exception& e) {
55 std::cerr << "ERROR: " << e.what() << " at offset " << offset << "\n";
56 }
57}
58
59// http://git.altlinux.org/people/legion/packages/kbd.git?p=kbd.git;a=blob;f=data/consolefonts/README.eurlatgr
60static const char GLYPH_VERTICAL[] = "\u2502 "; // "│ "
61static const char GLYPH_VERTICAL_AND_RIGHT[] = "\u251c\u2500"; // "├─"
62static const char GLYPH_UP_AND_RIGHT[] = "\u2514\u2500"; // "└─"
63static const char GLYPH_SPACE[] = " ";
64
65void
Davide Pesaventoa420e972021-02-25 00:56:25 -050066Dissector::printBranches()
Davide Pesavento7ef5cf72021-01-29 00:20:24 -050067{
68 for (size_t i = 0; i < m_branches.size(); ++i) {
69 if (i == m_branches.size() - 1) {
70 m_out << (m_branches[i] ? GLYPH_VERTICAL_AND_RIGHT : GLYPH_UP_AND_RIGHT);
71 }
72 else {
73 m_out << (m_branches[i] ? GLYPH_VERTICAL : GLYPH_SPACE);
74 }
75 }
76}
77
Davide Pesaventodb9613e2023-01-20 20:52:21 -050078// https://docs.named-data.net/NDN-packet-spec/current/types.html
Davide Pesaventob3570c62022-02-19 19:19:00 -050079static const std::map<uint32_t, std::string_view> TLV_DICT = {
Davide Pesaventod66fb492022-11-30 19:52:35 -050080 {tlv::Interest , "Interest"},
81 {tlv::Data , "Data"},
82 {tlv::Name , "Name"},
Davide Pesavento105cd9e2019-04-06 18:13:44 -040083 // Name components
Davide Pesavento79482ec2021-11-24 21:07:36 -050084 {tlv::GenericNameComponent , "GenericNameComponent"},
85 {tlv::ImplicitSha256DigestComponent , "ImplicitSha256DigestComponent"},
86 {tlv::ParametersSha256DigestComponent , "ParametersSha256DigestComponent"},
87 {tlv::KeywordNameComponent , "KeywordNameComponent"},
88 {tlv::SegmentNameComponent , "SegmentNameComponent"},
89 {tlv::ByteOffsetNameComponent , "ByteOffsetNameComponent"},
90 {tlv::VersionNameComponent , "VersionNameComponent"},
91 {tlv::TimestampNameComponent , "TimestampNameComponent"},
92 {tlv::SequenceNumNameComponent , "SequenceNumNameComponent"},
Davide Pesaventod66fb492022-11-30 19:52:35 -050093 // Interest packet
94 {tlv::CanBePrefix , "CanBePrefix"},
95 {tlv::MustBeFresh , "MustBeFresh"},
96 {tlv::ForwardingHint , "ForwardingHint"},
97 {tlv::Nonce , "Nonce"},
98 {tlv::InterestLifetime , "InterestLifetime"},
99 {tlv::HopLimit , "HopLimit"},
100 {tlv::ApplicationParameters , "ApplicationParameters"},
101 {tlv::InterestSignatureInfo , "InterestSignatureInfo"},
102 {tlv::InterestSignatureValue , "InterestSignatureValue"},
103 // Data packet
104 {tlv::MetaInfo , "MetaInfo"},
105 {tlv::Content , "Content"},
106 {tlv::SignatureInfo , "SignatureInfo"},
107 {tlv::SignatureValue , "SignatureValue"},
108 {tlv::ContentType , "ContentType"},
109 {tlv::FreshnessPeriod , "FreshnessPeriod"},
110 {tlv::FinalBlockId , "FinalBlockId"},
111 // (Interest)SignatureInfo
112 {tlv::SignatureType , "SignatureType"},
113 {tlv::KeyLocator , "KeyLocator"},
114 {tlv::KeyDigest , "KeyDigest"},
115 {tlv::SignatureNonce , "SignatureNonce"},
116 {tlv::SignatureTime , "SignatureTime"},
117 {tlv::SignatureSeqNum , "SignatureSeqNum"},
118 // Certificate
119 {tlv::ValidityPeriod , "ValidityPeriod"},
120 {tlv::NotBefore , "NotBefore"},
121 {tlv::NotAfter , "NotAfter"},
122 {tlv::AdditionalDescription , "AdditionalDescription"},
123 {tlv::DescriptionEntry , "DescriptionEntry"},
124 {tlv::DescriptionKey , "DescriptionKey"},
125 {tlv::DescriptionValue , "DescriptionValue"},
126 // SafeBag
127 {tlv::security::SafeBag , "SafeBag"},
128 {tlv::security::EncryptedKey , "EncryptedKey"},
Junxiao Shie5adbfd2015-06-18 14:42:06 -0700129};
130
131void
Davide Pesaventoa420e972021-02-25 00:56:25 -0500132Dissector::printType(uint32_t type)
Junxiao Shie5adbfd2015-06-18 14:42:06 -0700133{
Davide Pesavento7ef5cf72021-01-29 00:20:24 -0500134 m_out << type << " (";
Junxiao Shie5adbfd2015-06-18 14:42:06 -0700135
Davide Pesaventoc0702702017-08-24 22:04:00 -0400136 auto it = TLV_DICT.find(type);
137 if (it != TLV_DICT.end()) {
Davide Pesavento7ef5cf72021-01-29 00:20:24 -0500138 m_out << it->second;
Junxiao Shie5adbfd2015-06-18 14:42:06 -0700139 }
Davide Pesaventod66fb492022-11-30 19:52:35 -0500140 else if ((type >= tlv::AppPrivateBlock1 && type <= 252) ||
141 type >= tlv::AppPrivateBlock2) {
142 m_out << "UNKNOWN_APP";
Junxiao Shie5adbfd2015-06-18 14:42:06 -0700143 }
144 else {
Davide Pesaventod66fb492022-11-30 19:52:35 -0500145 m_out << "RESERVED";
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 << " [[";
Davide Pesavento0df0e1c2023-04-25 15:50:05 -0400172 escape(m_out, {reinterpret_cast<const char*>(block.value()), block.value_size()});
Davide Pesavento7ef5cf72021-01-29 00:20:24 -0500173 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
Davide Pesaventob3570c62022-02-19 19:19:00 -0500188} // namespace ndn::dissect