Junxiao Shi | e5adbfd | 2015-06-18 14:42:06 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Davide Pesavento | a2d4b14 | 2018-04-20 16:46:29 -0400 | [diff] [blame] | 2 | /* |
Davide Pesavento | 5748e82 | 2024-01-26 18:40:22 -0500 | [diff] [blame] | 3 | * Copyright (c) 2013-2024, Regents of the University of California. |
Junxiao Shi | 78c78cc | 2015-06-19 15:53:53 -0700 | [diff] [blame] | 4 | * |
| 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 Shi | e5adbfd | 2015-06-18 14:42:06 -0700 | [diff] [blame] | 18 | * |
| 19 | * See AUTHORS.md for complete list of ndn-cxx authors and contributors. |
| 20 | * |
Davide Pesavento | a420e97 | 2021-02-25 00:56:25 -0500 | [diff] [blame] | 21 | * @author Alexander Afanasyev |
| 22 | * @author Davide Pesavento |
Junxiao Shi | e5adbfd | 2015-06-18 14:42:06 -0700 | [diff] [blame] | 23 | */ |
| 24 | |
Davide Pesavento | a420e97 | 2021-02-25 00:56:25 -0500 | [diff] [blame] | 25 | #include "dissector.hpp" |
Junxiao Shi | e5adbfd | 2015-06-18 14:42:06 -0700 | [diff] [blame] | 26 | |
Davide Pesavento | a2d4b14 | 2018-04-20 16:46:29 -0400 | [diff] [blame] | 27 | #include <ndn-cxx/encoding/tlv.hpp> |
Davide Pesavento | d66fb49 | 2022-11-30 19:52:35 -0500 | [diff] [blame] | 28 | #include <ndn-cxx/encoding/tlv-security.hpp> |
Davide Pesavento | 242d506 | 2022-03-11 16:34:23 -0500 | [diff] [blame] | 29 | #include <ndn-cxx/util/string-helper.hpp> |
Junxiao Shi | 78c78cc | 2015-06-19 15:53:53 -0700 | [diff] [blame] | 30 | |
Davide Pesavento | 5748e82 | 2024-01-26 18:40:22 -0500 | [diff] [blame] | 31 | #include <iostream> |
| 32 | #include <map> |
| 33 | |
Davide Pesavento | b3570c6 | 2022-02-19 19:19:00 -0500 | [diff] [blame] | 34 | namespace ndn::dissect { |
Junxiao Shi | e5adbfd | 2015-06-18 14:42:06 -0700 | [diff] [blame] | 35 | |
Davide Pesavento | a420e97 | 2021-02-25 00:56:25 -0500 | [diff] [blame] | 36 | Dissector::Dissector(std::istream& input, std::ostream& output, const Options& options) |
| 37 | : m_options(options) |
| 38 | , m_in(input) |
Davide Pesavento | 7ef5cf7 | 2021-01-29 00:20:24 -0500 | [diff] [blame] | 39 | , m_out(output) |
| 40 | { |
| 41 | } |
| 42 | |
| 43 | void |
Davide Pesavento | a420e97 | 2021-02-25 00:56:25 -0500 | [diff] [blame] | 44 | Dissector::dissect() |
Davide Pesavento | 7ef5cf7 | 2021-01-29 00:20:24 -0500 | [diff] [blame] | 45 | { |
| 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 |
| 60 | static const char GLYPH_VERTICAL[] = "\u2502 "; // "│ " |
| 61 | static const char GLYPH_VERTICAL_AND_RIGHT[] = "\u251c\u2500"; // "├─" |
| 62 | static const char GLYPH_UP_AND_RIGHT[] = "\u2514\u2500"; // "└─" |
| 63 | static const char GLYPH_SPACE[] = " "; |
| 64 | |
| 65 | void |
Davide Pesavento | a420e97 | 2021-02-25 00:56:25 -0500 | [diff] [blame] | 66 | Dissector::printBranches() |
Davide Pesavento | 7ef5cf7 | 2021-01-29 00:20:24 -0500 | [diff] [blame] | 67 | { |
| 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 Pesavento | db9613e | 2023-01-20 20:52:21 -0500 | [diff] [blame] | 78 | // https://docs.named-data.net/NDN-packet-spec/current/types.html |
Davide Pesavento | b3570c6 | 2022-02-19 19:19:00 -0500 | [diff] [blame] | 79 | static const std::map<uint32_t, std::string_view> TLV_DICT = { |
Davide Pesavento | d66fb49 | 2022-11-30 19:52:35 -0500 | [diff] [blame] | 80 | {tlv::Interest , "Interest"}, |
| 81 | {tlv::Data , "Data"}, |
| 82 | {tlv::Name , "Name"}, |
Davide Pesavento | 105cd9e | 2019-04-06 18:13:44 -0400 | [diff] [blame] | 83 | // Name components |
Davide Pesavento | 79482ec | 2021-11-24 21:07:36 -0500 | [diff] [blame] | 84 | {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 Pesavento | d66fb49 | 2022-11-30 19:52:35 -0500 | [diff] [blame] | 93 | // 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 Shi | e5adbfd | 2015-06-18 14:42:06 -0700 | [diff] [blame] | 129 | }; |
| 130 | |
| 131 | void |
Davide Pesavento | a420e97 | 2021-02-25 00:56:25 -0500 | [diff] [blame] | 132 | Dissector::printType(uint32_t type) |
Junxiao Shi | e5adbfd | 2015-06-18 14:42:06 -0700 | [diff] [blame] | 133 | { |
Davide Pesavento | 7ef5cf7 | 2021-01-29 00:20:24 -0500 | [diff] [blame] | 134 | m_out << type << " ("; |
Junxiao Shi | e5adbfd | 2015-06-18 14:42:06 -0700 | [diff] [blame] | 135 | |
Davide Pesavento | c070270 | 2017-08-24 22:04:00 -0400 | [diff] [blame] | 136 | auto it = TLV_DICT.find(type); |
| 137 | if (it != TLV_DICT.end()) { |
Davide Pesavento | 7ef5cf7 | 2021-01-29 00:20:24 -0500 | [diff] [blame] | 138 | m_out << it->second; |
Junxiao Shi | e5adbfd | 2015-06-18 14:42:06 -0700 | [diff] [blame] | 139 | } |
Davide Pesavento | d66fb49 | 2022-11-30 19:52:35 -0500 | [diff] [blame] | 140 | else if ((type >= tlv::AppPrivateBlock1 && type <= 252) || |
| 141 | type >= tlv::AppPrivateBlock2) { |
| 142 | m_out << "UNKNOWN_APP"; |
Junxiao Shi | e5adbfd | 2015-06-18 14:42:06 -0700 | [diff] [blame] | 143 | } |
| 144 | else { |
Davide Pesavento | d66fb49 | 2022-11-30 19:52:35 -0500 | [diff] [blame] | 145 | m_out << "RESERVED"; |
Junxiao Shi | e5adbfd | 2015-06-18 14:42:06 -0700 | [diff] [blame] | 146 | } |
Davide Pesavento | c070270 | 2017-08-24 22:04:00 -0400 | [diff] [blame] | 147 | |
Davide Pesavento | 7ef5cf7 | 2021-01-29 00:20:24 -0500 | [diff] [blame] | 148 | m_out << ")"; |
Junxiao Shi | e5adbfd | 2015-06-18 14:42:06 -0700 | [diff] [blame] | 149 | } |
| 150 | |
Junxiao Shi | e5adbfd | 2015-06-18 14:42:06 -0700 | [diff] [blame] | 151 | void |
Davide Pesavento | a420e97 | 2021-02-25 00:56:25 -0500 | [diff] [blame] | 152 | Dissector::printBlock(const Block& block) |
Junxiao Shi | e5adbfd | 2015-06-18 14:42:06 -0700 | [diff] [blame] | 153 | { |
Davide Pesavento | 7ef5cf7 | 2021-01-29 00:20:24 -0500 | [diff] [blame] | 154 | printBranches(); |
| 155 | printType(block.type()); |
| 156 | m_out << " (size: " << block.value_size() << ")"; |
Junxiao Shi | e5adbfd | 2015-06-18 14:42:06 -0700 | [diff] [blame] | 157 | |
| 158 | try { |
Davide Pesavento | a420e97 | 2021-02-25 00:56:25 -0500 | [diff] [blame] | 159 | if (block.type() != tlv::SignatureValue && |
Davide Pesavento | 79482ec | 2021-11-24 21:07:36 -0500 | [diff] [blame] | 160 | block.type() != tlv::InterestSignatureValue && |
Davide Pesavento | a420e97 | 2021-02-25 00:56:25 -0500 | [diff] [blame] | 161 | (block.type() != tlv::Content || m_options.dissectContent)) { |
Davide Pesavento | 3f588ca | 2021-02-24 15:48:33 -0500 | [diff] [blame] | 162 | block.parse(); |
| 163 | } |
Junxiao Shi | e5adbfd | 2015-06-18 14:42:06 -0700 | [diff] [blame] | 164 | } |
Davide Pesavento | c070270 | 2017-08-24 22:04:00 -0400 | [diff] [blame] | 165 | catch (const tlv::Error&) { |
Junxiao Shi | e5adbfd | 2015-06-18 14:42:06 -0700 | [diff] [blame] | 166 | // pass (e.g., leaf block reached) |
Junxiao Shi | e5adbfd | 2015-06-18 14:42:06 -0700 | [diff] [blame] | 167 | } |
| 168 | |
Davide Pesavento | 7ef5cf7 | 2021-01-29 00:20:24 -0500 | [diff] [blame] | 169 | const auto& elements = block.elements(); |
| 170 | if (elements.empty()) { |
| 171 | m_out << " [["; |
Davide Pesavento | 0df0e1c | 2023-04-25 15:50:05 -0400 | [diff] [blame] | 172 | escape(m_out, {reinterpret_cast<const char*>(block.value()), block.value_size()}); |
Davide Pesavento | 7ef5cf7 | 2021-01-29 00:20:24 -0500 | [diff] [blame] | 173 | m_out << "]]"; |
Junxiao Shi | 78c78cc | 2015-06-19 15:53:53 -0700 | [diff] [blame] | 174 | } |
Davide Pesavento | 7ef5cf7 | 2021-01-29 00:20:24 -0500 | [diff] [blame] | 175 | m_out << "\n"; |
Junxiao Shi | e5adbfd | 2015-06-18 14:42:06 -0700 | [diff] [blame] | 176 | |
Davide Pesavento | 7ef5cf7 | 2021-01-29 00:20:24 -0500 | [diff] [blame] | 177 | 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 Shi | e5adbfd | 2015-06-18 14:42:06 -0700 | [diff] [blame] | 182 | } |
Davide Pesavento | 7ef5cf7 | 2021-01-29 00:20:24 -0500 | [diff] [blame] | 183 | printBlock(elements[i]); |
Davide Pesavento | 9b1fd4b | 2021-01-26 22:16:02 -0500 | [diff] [blame] | 184 | } |
Davide Pesavento | 7ef5cf7 | 2021-01-29 00:20:24 -0500 | [diff] [blame] | 185 | m_branches.pop_back(); |
Junxiao Shi | e5adbfd | 2015-06-18 14:42:06 -0700 | [diff] [blame] | 186 | } |
| 187 | |
Davide Pesavento | b3570c6 | 2022-02-19 19:19:00 -0500 | [diff] [blame] | 188 | } // namespace ndn::dissect |