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 | a0e6b60 | 2021-01-21 19:47:04 -0500 | [diff] [blame] | 3 | * Copyright (c) 2013-2021, 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 | * |
| 21 | * @author Alexander Afanasyev <http://lasr.cs.ucla.edu/afanasyev/index.html> |
| 22 | */ |
| 23 | |
Junxiao Shi | 78c78cc | 2015-06-19 15:53:53 -0700 | [diff] [blame] | 24 | #include "ndn-dissect.hpp" |
Junxiao Shi | e5adbfd | 2015-06-18 14:42:06 -0700 | [diff] [blame] | 25 | |
Junxiao Shi | 78c78cc | 2015-06-19 15:53:53 -0700 | [diff] [blame] | 26 | #include <algorithm> |
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 | |
Junxiao Shi | 78c78cc | 2015-06-19 15:53:53 -0700 | [diff] [blame] | 29 | #include <ndn-cxx/name-component.hpp> |
Davide Pesavento | a2d4b14 | 2018-04-20 16:46:29 -0400 | [diff] [blame] | 30 | #include <ndn-cxx/encoding/tlv.hpp> |
Junxiao Shi | 78c78cc | 2015-06-19 15:53:53 -0700 | [diff] [blame] | 31 | #include <ndn-cxx/util/indented-stream.hpp> |
| 32 | |
Junxiao Shi | e5adbfd | 2015-06-18 14:42:06 -0700 | [diff] [blame] | 33 | namespace ndn { |
Junxiao Shi | 78c78cc | 2015-06-19 15:53:53 -0700 | [diff] [blame] | 34 | namespace dissect { |
Junxiao Shi | e5adbfd | 2015-06-18 14:42:06 -0700 | [diff] [blame] | 35 | |
Davide Pesavento | a2d4b14 | 2018-04-20 16:46:29 -0400 | [diff] [blame] | 36 | static const std::map<uint32_t, const char*> TLV_DICT = { |
Junxiao Shi | e5adbfd | 2015-06-18 14:42:06 -0700 | [diff] [blame] | 37 | {tlv::Interest , "Interest"}, |
| 38 | {tlv::Data , "Data"}, |
| 39 | {tlv::Name , "Name"}, |
Davide Pesavento | a2d4b14 | 2018-04-20 16:46:29 -0400 | [diff] [blame] | 40 | {tlv::CanBePrefix , "CanBePrefix"}, |
| 41 | {tlv::MustBeFresh , "MustBeFresh"}, |
| 42 | //{tlv::ForwardingHint , "ForwardingHint"}, |
Junxiao Shi | e5adbfd | 2015-06-18 14:42:06 -0700 | [diff] [blame] | 43 | {tlv::Nonce , "Nonce"}, |
| 44 | {tlv::InterestLifetime , "InterestLifetime"}, |
Davide Pesavento | a2d4b14 | 2018-04-20 16:46:29 -0400 | [diff] [blame] | 45 | {tlv::HopLimit , "HopLimit"}, |
Davide Pesavento | 105cd9e | 2019-04-06 18:13:44 -0400 | [diff] [blame] | 46 | {tlv::ApplicationParameters , "ApplicationParameters"}, |
Junxiao Shi | e5adbfd | 2015-06-18 14:42:06 -0700 | [diff] [blame] | 47 | {tlv::MetaInfo , "MetaInfo"}, |
| 48 | {tlv::Content , "Content"}, |
| 49 | {tlv::SignatureInfo , "SignatureInfo"}, |
| 50 | {tlv::SignatureValue , "SignatureValue"}, |
| 51 | {tlv::ContentType , "ContentType"}, |
| 52 | {tlv::FreshnessPeriod , "FreshnessPeriod"}, |
| 53 | {tlv::FinalBlockId , "FinalBlockId"}, |
| 54 | {tlv::SignatureType , "SignatureType"}, |
| 55 | {tlv::KeyLocator , "KeyLocator"}, |
| 56 | {tlv::KeyDigest , "KeyDigest"}, |
Davide Pesavento | 105cd9e | 2019-04-06 18:13:44 -0400 | [diff] [blame] | 57 | // Name components |
| 58 | {tlv::GenericNameComponent , "GenericNameComponent"}, |
| 59 | {tlv::ImplicitSha256DigestComponent , "ImplicitSha256DigestComponent"}, |
| 60 | {tlv::ParametersSha256DigestComponent, "ParametersSha256DigestComponent"}, |
| 61 | {tlv::KeywordNameComponent , "KeywordNameComponent"}, |
| 62 | //{tlv::SegmentNameComponent , "SegmentNameComponent"}, |
| 63 | //{tlv::ByteOffsetNameComponent , "ByteOffsetNameComponent"}, |
| 64 | //{tlv::VersionNameComponent , "VersionNameComponent"}, |
| 65 | //{tlv::TimestampNameComponent , "TimestampNameComponent"}, |
| 66 | //{tlv::SequenceNumNameComponent , "SequenceNumNameComponent"}, |
Davide Pesavento | a2d4b14 | 2018-04-20 16:46:29 -0400 | [diff] [blame] | 67 | // Deprecated elements |
| 68 | {tlv::Selectors , "Selectors"}, |
| 69 | {tlv::MinSuffixComponents , "MinSuffixComponents"}, |
| 70 | {tlv::MaxSuffixComponents , "MaxSuffixComponents"}, |
| 71 | {tlv::PublisherPublicKeyLocator , "PublisherPublicKeyLocator"}, |
| 72 | {tlv::Exclude , "Exclude"}, |
| 73 | {tlv::ChildSelector , "ChildSelector"}, |
| 74 | {tlv::Any , "Any"}, |
Junxiao Shi | e5adbfd | 2015-06-18 14:42:06 -0700 | [diff] [blame] | 75 | }; |
| 76 | |
| 77 | void |
Junxiao Shi | 78c78cc | 2015-06-19 15:53:53 -0700 | [diff] [blame] | 78 | NdnDissect::printType(std::ostream& os, uint32_t type) |
Junxiao Shi | e5adbfd | 2015-06-18 14:42:06 -0700 | [diff] [blame] | 79 | { |
Junxiao Shi | 78c78cc | 2015-06-19 15:53:53 -0700 | [diff] [blame] | 80 | os << type << " ("; |
Junxiao Shi | e5adbfd | 2015-06-18 14:42:06 -0700 | [diff] [blame] | 81 | |
Davide Pesavento | c070270 | 2017-08-24 22:04:00 -0400 | [diff] [blame] | 82 | auto it = TLV_DICT.find(type); |
| 83 | if (it != TLV_DICT.end()) { |
| 84 | os << it->second; |
Junxiao Shi | e5adbfd | 2015-06-18 14:42:06 -0700 | [diff] [blame] | 85 | } |
| 86 | else if (type < tlv::AppPrivateBlock1) { |
Junxiao Shi | 78c78cc | 2015-06-19 15:53:53 -0700 | [diff] [blame] | 87 | os << "RESERVED_1"; |
Junxiao Shi | e5adbfd | 2015-06-18 14:42:06 -0700 | [diff] [blame] | 88 | } |
| 89 | else if (tlv::AppPrivateBlock1 <= type && type < 253) { |
Junxiao Shi | 78c78cc | 2015-06-19 15:53:53 -0700 | [diff] [blame] | 90 | os << "APP_TAG_1"; |
Junxiao Shi | e5adbfd | 2015-06-18 14:42:06 -0700 | [diff] [blame] | 91 | } |
| 92 | else if (253 <= type && type < tlv::AppPrivateBlock2) { |
Junxiao Shi | 78c78cc | 2015-06-19 15:53:53 -0700 | [diff] [blame] | 93 | os << "RESERVED_3"; |
Junxiao Shi | e5adbfd | 2015-06-18 14:42:06 -0700 | [diff] [blame] | 94 | } |
| 95 | else { |
Junxiao Shi | 78c78cc | 2015-06-19 15:53:53 -0700 | [diff] [blame] | 96 | os << "APP_TAG_3"; |
Junxiao Shi | e5adbfd | 2015-06-18 14:42:06 -0700 | [diff] [blame] | 97 | } |
Davide Pesavento | c070270 | 2017-08-24 22:04:00 -0400 | [diff] [blame] | 98 | |
Junxiao Shi | 78c78cc | 2015-06-19 15:53:53 -0700 | [diff] [blame] | 99 | os << ")"; |
Junxiao Shi | e5adbfd | 2015-06-18 14:42:06 -0700 | [diff] [blame] | 100 | } |
| 101 | |
Junxiao Shi | e5adbfd | 2015-06-18 14:42:06 -0700 | [diff] [blame] | 102 | void |
Junxiao Shi | 78c78cc | 2015-06-19 15:53:53 -0700 | [diff] [blame] | 103 | NdnDissect::printBlock(std::ostream& os, const Block& block) |
Junxiao Shi | e5adbfd | 2015-06-18 14:42:06 -0700 | [diff] [blame] | 104 | { |
Davide Pesavento | a2d4b14 | 2018-04-20 16:46:29 -0400 | [diff] [blame] | 105 | printType(os, block.type()); |
Junxiao Shi | 78c78cc | 2015-06-19 15:53:53 -0700 | [diff] [blame] | 106 | os << " (size: " << block.value_size() << ")"; |
Junxiao Shi | e5adbfd | 2015-06-18 14:42:06 -0700 | [diff] [blame] | 107 | |
| 108 | try { |
| 109 | // if (block.type() != tlv::Content && block.type() != tlv::SignatureValue) |
| 110 | block.parse(); |
| 111 | } |
Davide Pesavento | c070270 | 2017-08-24 22:04:00 -0400 | [diff] [blame] | 112 | catch (const tlv::Error&) { |
Junxiao Shi | e5adbfd | 2015-06-18 14:42:06 -0700 | [diff] [blame] | 113 | // pass (e.g., leaf block reached) |
| 114 | |
| 115 | // @todo: Figure how to deterministically figure out that value is not recursive TLV block |
| 116 | } |
| 117 | |
Junxiao Shi | 78c78cc | 2015-06-19 15:53:53 -0700 | [diff] [blame] | 118 | if (block.elements().empty()) { |
| 119 | os << " [["; |
| 120 | name::Component(block.value(), block.value_size()).toUri(os); |
Davide Pesavento | 9b1fd4b | 2021-01-26 22:16:02 -0500 | [diff] [blame^] | 121 | os << "]]"; |
Junxiao Shi | 78c78cc | 2015-06-19 15:53:53 -0700 | [diff] [blame] | 122 | } |
Davide Pesavento | 9b1fd4b | 2021-01-26 22:16:02 -0500 | [diff] [blame^] | 123 | os << "\n"; |
Junxiao Shi | e5adbfd | 2015-06-18 14:42:06 -0700 | [diff] [blame] | 124 | |
Junxiao Shi | 78c78cc | 2015-06-19 15:53:53 -0700 | [diff] [blame] | 125 | util::IndentedStream os2(os, " "); |
| 126 | std::for_each(block.elements_begin(), block.elements_end(), |
Davide Pesavento | a2d4b14 | 2018-04-20 16:46:29 -0400 | [diff] [blame] | 127 | [this, &os2] (const Block& element) { printBlock(os2, element); }); |
Junxiao Shi | e5adbfd | 2015-06-18 14:42:06 -0700 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | void |
Junxiao Shi | 78c78cc | 2015-06-19 15:53:53 -0700 | [diff] [blame] | 131 | NdnDissect::dissect(std::ostream& os, std::istream& is) |
Junxiao Shi | e5adbfd | 2015-06-18 14:42:06 -0700 | [diff] [blame] | 132 | { |
Davide Pesavento | 9b1fd4b | 2021-01-26 22:16:02 -0500 | [diff] [blame^] | 133 | size_t offset = 0; |
| 134 | try { |
| 135 | while (is.peek() != std::istream::traits_type::eof()) { |
| 136 | auto block = Block::fromStream(is); |
| 137 | printBlock(os, block); |
| 138 | offset += block.size(); |
Junxiao Shi | e5adbfd | 2015-06-18 14:42:06 -0700 | [diff] [blame] | 139 | } |
Davide Pesavento | 9b1fd4b | 2021-01-26 22:16:02 -0500 | [diff] [blame^] | 140 | } |
| 141 | catch (const std::exception& e) { |
| 142 | std::cerr << "ERROR: " << e.what() << " at offset " << offset << "\n"; |
Junxiao Shi | e5adbfd | 2015-06-18 14:42:06 -0700 | [diff] [blame] | 143 | } |
Junxiao Shi | e5adbfd | 2015-06-18 14:42:06 -0700 | [diff] [blame] | 144 | } |
| 145 | |
Junxiao Shi | 78c78cc | 2015-06-19 15:53:53 -0700 | [diff] [blame] | 146 | } // namespace dissect |
Junxiao Shi | e5adbfd | 2015-06-18 14:42:06 -0700 | [diff] [blame] | 147 | } // namespace ndn |