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 | b3570c6 | 2022-02-19 19:19:00 -0500 | [diff] [blame^] | 3 | * Copyright (c) 2013-2022, 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 | a0e6b60 | 2021-01-21 19:47:04 -0500 | [diff] [blame] | 27 | #include <map> |
Junxiao Shi | e5adbfd | 2015-06-18 14:42:06 -0700 | [diff] [blame] | 28 | |
Davide Pesavento | a2d4b14 | 2018-04-20 16:46:29 -0400 | [diff] [blame] | 29 | #include <ndn-cxx/encoding/tlv.hpp> |
Davide Pesavento | 7ef5cf7 | 2021-01-29 00:20:24 -0500 | [diff] [blame] | 30 | #include <ndn-cxx/name-component.hpp> |
Junxiao Shi | 78c78cc | 2015-06-19 15:53:53 -0700 | [diff] [blame] | 31 | |
Davide Pesavento | b3570c6 | 2022-02-19 19:19:00 -0500 | [diff] [blame^] | 32 | namespace ndn::dissect { |
Junxiao Shi | e5adbfd | 2015-06-18 14:42:06 -0700 | [diff] [blame] | 33 | |
Davide Pesavento | a420e97 | 2021-02-25 00:56:25 -0500 | [diff] [blame] | 34 | Dissector::Dissector(std::istream& input, std::ostream& output, const Options& options) |
| 35 | : m_options(options) |
| 36 | , m_in(input) |
Davide Pesavento | 7ef5cf7 | 2021-01-29 00:20:24 -0500 | [diff] [blame] | 37 | , m_out(output) |
| 38 | { |
| 39 | } |
| 40 | |
| 41 | void |
Davide Pesavento | a420e97 | 2021-02-25 00:56:25 -0500 | [diff] [blame] | 42 | Dissector::dissect() |
Davide Pesavento | 7ef5cf7 | 2021-01-29 00:20:24 -0500 | [diff] [blame] | 43 | { |
| 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 |
| 58 | static const char GLYPH_VERTICAL[] = "\u2502 "; // "│ " |
| 59 | static const char GLYPH_VERTICAL_AND_RIGHT[] = "\u251c\u2500"; // "├─" |
| 60 | static const char GLYPH_UP_AND_RIGHT[] = "\u2514\u2500"; // "└─" |
| 61 | static const char GLYPH_SPACE[] = " "; |
| 62 | |
| 63 | void |
Davide Pesavento | a420e97 | 2021-02-25 00:56:25 -0500 | [diff] [blame] | 64 | Dissector::printBranches() |
Davide Pesavento | 7ef5cf7 | 2021-01-29 00:20:24 -0500 | [diff] [blame] | 65 | { |
| 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 Pesavento | b3570c6 | 2022-02-19 19:19:00 -0500 | [diff] [blame^] | 76 | static const std::map<uint32_t, std::string_view> TLV_DICT = { |
Junxiao Shi | e5adbfd | 2015-06-18 14:42:06 -0700 | [diff] [blame] | 77 | {tlv::Interest , "Interest"}, |
| 78 | {tlv::Data , "Data"}, |
| 79 | {tlv::Name , "Name"}, |
Davide Pesavento | 79482ec | 2021-11-24 21:07:36 -0500 | [diff] [blame] | 80 | // Interest packet |
Davide Pesavento | a2d4b14 | 2018-04-20 16:46:29 -0400 | [diff] [blame] | 81 | {tlv::CanBePrefix , "CanBePrefix"}, |
| 82 | {tlv::MustBeFresh , "MustBeFresh"}, |
Davide Pesavento | 79482ec | 2021-11-24 21:07:36 -0500 | [diff] [blame] | 83 | {tlv::ForwardingHint , "ForwardingHint"}, |
Junxiao Shi | e5adbfd | 2015-06-18 14:42:06 -0700 | [diff] [blame] | 84 | {tlv::Nonce , "Nonce"}, |
| 85 | {tlv::InterestLifetime , "InterestLifetime"}, |
Davide Pesavento | a2d4b14 | 2018-04-20 16:46:29 -0400 | [diff] [blame] | 86 | {tlv::HopLimit , "HopLimit"}, |
Davide Pesavento | 105cd9e | 2019-04-06 18:13:44 -0400 | [diff] [blame] | 87 | {tlv::ApplicationParameters , "ApplicationParameters"}, |
Davide Pesavento | 79482ec | 2021-11-24 21:07:36 -0500 | [diff] [blame] | 88 | {tlv::InterestSignatureInfo , "InterestSignatureInfo"}, |
| 89 | {tlv::InterestSignatureValue , "InterestSignatureValue"}, |
| 90 | // Data packet |
Junxiao Shi | e5adbfd | 2015-06-18 14:42:06 -0700 | [diff] [blame] | 91 | {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 Pesavento | 79482ec | 2021-11-24 21:07:36 -0500 | [diff] [blame] | 98 | // (Interest)SignatureInfo elements |
Junxiao Shi | e5adbfd | 2015-06-18 14:42:06 -0700 | [diff] [blame] | 99 | {tlv::SignatureType , "SignatureType"}, |
| 100 | {tlv::KeyLocator , "KeyLocator"}, |
| 101 | {tlv::KeyDigest , "KeyDigest"}, |
Davide Pesavento | 79482ec | 2021-11-24 21:07:36 -0500 | [diff] [blame] | 102 | {tlv::SignatureNonce , "SignatureNonce"}, |
| 103 | {tlv::SignatureTime , "SignatureTime"}, |
| 104 | {tlv::SignatureSeqNum , "SignatureSeqNum"}, |
Davide Pesavento | 105cd9e | 2019-04-06 18:13:44 -0400 | [diff] [blame] | 105 | // Name components |
Davide Pesavento | 79482ec | 2021-11-24 21:07:36 -0500 | [diff] [blame] | 106 | {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 Pesavento | a2d4b14 | 2018-04-20 16:46:29 -0400 | [diff] [blame] | 115 | // 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 Shi | e5adbfd | 2015-06-18 14:42:06 -0700 | [diff] [blame] | 123 | }; |
| 124 | |
| 125 | void |
Davide Pesavento | a420e97 | 2021-02-25 00:56:25 -0500 | [diff] [blame] | 126 | Dissector::printType(uint32_t type) |
Junxiao Shi | e5adbfd | 2015-06-18 14:42:06 -0700 | [diff] [blame] | 127 | { |
Davide Pesavento | 7ef5cf7 | 2021-01-29 00:20:24 -0500 | [diff] [blame] | 128 | m_out << type << " ("; |
Junxiao Shi | e5adbfd | 2015-06-18 14:42:06 -0700 | [diff] [blame] | 129 | |
Davide Pesavento | c070270 | 2017-08-24 22:04:00 -0400 | [diff] [blame] | 130 | auto it = TLV_DICT.find(type); |
| 131 | if (it != TLV_DICT.end()) { |
Davide Pesavento | 7ef5cf7 | 2021-01-29 00:20:24 -0500 | [diff] [blame] | 132 | m_out << it->second; |
Junxiao Shi | e5adbfd | 2015-06-18 14:42:06 -0700 | [diff] [blame] | 133 | } |
| 134 | else if (type < tlv::AppPrivateBlock1) { |
Davide Pesavento | 7ef5cf7 | 2021-01-29 00:20:24 -0500 | [diff] [blame] | 135 | m_out << "RESERVED_1"; |
Junxiao Shi | e5adbfd | 2015-06-18 14:42:06 -0700 | [diff] [blame] | 136 | } |
| 137 | else if (tlv::AppPrivateBlock1 <= type && type < 253) { |
Davide Pesavento | 7ef5cf7 | 2021-01-29 00:20:24 -0500 | [diff] [blame] | 138 | m_out << "APP_TAG_1"; |
Junxiao Shi | e5adbfd | 2015-06-18 14:42:06 -0700 | [diff] [blame] | 139 | } |
| 140 | else if (253 <= type && type < tlv::AppPrivateBlock2) { |
Davide Pesavento | 7ef5cf7 | 2021-01-29 00:20:24 -0500 | [diff] [blame] | 141 | m_out << "RESERVED_3"; |
Junxiao Shi | e5adbfd | 2015-06-18 14:42:06 -0700 | [diff] [blame] | 142 | } |
| 143 | else { |
Davide Pesavento | 7ef5cf7 | 2021-01-29 00:20:24 -0500 | [diff] [blame] | 144 | m_out << "APP_TAG_3"; |
Junxiao Shi | e5adbfd | 2015-06-18 14:42:06 -0700 | [diff] [blame] | 145 | } |
Davide Pesavento | c070270 | 2017-08-24 22:04:00 -0400 | [diff] [blame] | 146 | |
Davide Pesavento | 7ef5cf7 | 2021-01-29 00:20:24 -0500 | [diff] [blame] | 147 | m_out << ")"; |
Junxiao Shi | e5adbfd | 2015-06-18 14:42:06 -0700 | [diff] [blame] | 148 | } |
| 149 | |
Junxiao Shi | e5adbfd | 2015-06-18 14:42:06 -0700 | [diff] [blame] | 150 | void |
Davide Pesavento | a420e97 | 2021-02-25 00:56:25 -0500 | [diff] [blame] | 151 | Dissector::printBlock(const Block& block) |
Junxiao Shi | e5adbfd | 2015-06-18 14:42:06 -0700 | [diff] [blame] | 152 | { |
Davide Pesavento | 7ef5cf7 | 2021-01-29 00:20:24 -0500 | [diff] [blame] | 153 | printBranches(); |
| 154 | printType(block.type()); |
| 155 | m_out << " (size: " << block.value_size() << ")"; |
Junxiao Shi | e5adbfd | 2015-06-18 14:42:06 -0700 | [diff] [blame] | 156 | |
| 157 | try { |
Davide Pesavento | a420e97 | 2021-02-25 00:56:25 -0500 | [diff] [blame] | 158 | if (block.type() != tlv::SignatureValue && |
Davide Pesavento | 79482ec | 2021-11-24 21:07:36 -0500 | [diff] [blame] | 159 | block.type() != tlv::InterestSignatureValue && |
Davide Pesavento | a420e97 | 2021-02-25 00:56:25 -0500 | [diff] [blame] | 160 | (block.type() != tlv::Content || m_options.dissectContent)) { |
Davide Pesavento | 3f588ca | 2021-02-24 15:48:33 -0500 | [diff] [blame] | 161 | block.parse(); |
| 162 | } |
Junxiao Shi | e5adbfd | 2015-06-18 14:42:06 -0700 | [diff] [blame] | 163 | } |
Davide Pesavento | c070270 | 2017-08-24 22:04:00 -0400 | [diff] [blame] | 164 | catch (const tlv::Error&) { |
Junxiao Shi | e5adbfd | 2015-06-18 14:42:06 -0700 | [diff] [blame] | 165 | // pass (e.g., leaf block reached) |
Junxiao Shi | e5adbfd | 2015-06-18 14:42:06 -0700 | [diff] [blame] | 166 | } |
| 167 | |
Davide Pesavento | 7ef5cf7 | 2021-01-29 00:20:24 -0500 | [diff] [blame] | 168 | 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 Shi | 78c78cc | 2015-06-19 15:53:53 -0700 | [diff] [blame] | 173 | } |
Davide Pesavento | 7ef5cf7 | 2021-01-29 00:20:24 -0500 | [diff] [blame] | 174 | m_out << "\n"; |
Junxiao Shi | e5adbfd | 2015-06-18 14:42:06 -0700 | [diff] [blame] | 175 | |
Davide Pesavento | 7ef5cf7 | 2021-01-29 00:20:24 -0500 | [diff] [blame] | 176 | 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 Shi | e5adbfd | 2015-06-18 14:42:06 -0700 | [diff] [blame] | 181 | } |
Davide Pesavento | 7ef5cf7 | 2021-01-29 00:20:24 -0500 | [diff] [blame] | 182 | printBlock(elements[i]); |
Davide Pesavento | 9b1fd4b | 2021-01-26 22:16:02 -0500 | [diff] [blame] | 183 | } |
Davide Pesavento | 7ef5cf7 | 2021-01-29 00:20:24 -0500 | [diff] [blame] | 184 | m_branches.pop_back(); |
Junxiao Shi | e5adbfd | 2015-06-18 14:42:06 -0700 | [diff] [blame] | 185 | } |
| 186 | |
Davide Pesavento | b3570c6 | 2022-02-19 19:19:00 -0500 | [diff] [blame^] | 187 | } // namespace ndn::dissect |