blob: 4971d037b64a16880671ef7f4fab9e717675e520 [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 Pesaventoa0e6b602021-01-21 19:47:04 -05003 * Copyright (c) 2013-2021, 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 *
21 * @author Alexander Afanasyev <http://lasr.cs.ucla.edu/afanasyev/index.html>
22 */
23
Junxiao Shi78c78cc2015-06-19 15:53:53 -070024#include "ndn-dissect.hpp"
Junxiao Shie5adbfd2015-06-18 14:42:06 -070025
Junxiao Shi78c78cc2015-06-19 15:53:53 -070026#include <algorithm>
Davide Pesaventoa0e6b602021-01-21 19:47:04 -050027#include <map>
Junxiao Shie5adbfd2015-06-18 14:42:06 -070028
Junxiao Shi78c78cc2015-06-19 15:53:53 -070029#include <ndn-cxx/name-component.hpp>
Davide Pesaventoa2d4b142018-04-20 16:46:29 -040030#include <ndn-cxx/encoding/tlv.hpp>
Junxiao Shi78c78cc2015-06-19 15:53:53 -070031#include <ndn-cxx/util/indented-stream.hpp>
32
Junxiao Shie5adbfd2015-06-18 14:42:06 -070033namespace ndn {
Junxiao Shi78c78cc2015-06-19 15:53:53 -070034namespace dissect {
Junxiao Shie5adbfd2015-06-18 14:42:06 -070035
Davide Pesaventoa2d4b142018-04-20 16:46:29 -040036static const std::map<uint32_t, const char*> TLV_DICT = {
Junxiao Shie5adbfd2015-06-18 14:42:06 -070037 {tlv::Interest , "Interest"},
38 {tlv::Data , "Data"},
39 {tlv::Name , "Name"},
Davide Pesaventoa2d4b142018-04-20 16:46:29 -040040 {tlv::CanBePrefix , "CanBePrefix"},
41 {tlv::MustBeFresh , "MustBeFresh"},
42 //{tlv::ForwardingHint , "ForwardingHint"},
Junxiao Shie5adbfd2015-06-18 14:42:06 -070043 {tlv::Nonce , "Nonce"},
44 {tlv::InterestLifetime , "InterestLifetime"},
Davide Pesaventoa2d4b142018-04-20 16:46:29 -040045 {tlv::HopLimit , "HopLimit"},
Davide Pesavento105cd9e2019-04-06 18:13:44 -040046 {tlv::ApplicationParameters , "ApplicationParameters"},
Junxiao Shie5adbfd2015-06-18 14:42:06 -070047 {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 Pesavento105cd9e2019-04-06 18:13:44 -040057 // 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 Pesaventoa2d4b142018-04-20 16:46:29 -040067 // 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 Shie5adbfd2015-06-18 14:42:06 -070075};
76
77void
Junxiao Shi78c78cc2015-06-19 15:53:53 -070078NdnDissect::printType(std::ostream& os, uint32_t type)
Junxiao Shie5adbfd2015-06-18 14:42:06 -070079{
Junxiao Shi78c78cc2015-06-19 15:53:53 -070080 os << type << " (";
Junxiao Shie5adbfd2015-06-18 14:42:06 -070081
Davide Pesaventoc0702702017-08-24 22:04:00 -040082 auto it = TLV_DICT.find(type);
83 if (it != TLV_DICT.end()) {
84 os << it->second;
Junxiao Shie5adbfd2015-06-18 14:42:06 -070085 }
86 else if (type < tlv::AppPrivateBlock1) {
Junxiao Shi78c78cc2015-06-19 15:53:53 -070087 os << "RESERVED_1";
Junxiao Shie5adbfd2015-06-18 14:42:06 -070088 }
89 else if (tlv::AppPrivateBlock1 <= type && type < 253) {
Junxiao Shi78c78cc2015-06-19 15:53:53 -070090 os << "APP_TAG_1";
Junxiao Shie5adbfd2015-06-18 14:42:06 -070091 }
92 else if (253 <= type && type < tlv::AppPrivateBlock2) {
Junxiao Shi78c78cc2015-06-19 15:53:53 -070093 os << "RESERVED_3";
Junxiao Shie5adbfd2015-06-18 14:42:06 -070094 }
95 else {
Junxiao Shi78c78cc2015-06-19 15:53:53 -070096 os << "APP_TAG_3";
Junxiao Shie5adbfd2015-06-18 14:42:06 -070097 }
Davide Pesaventoc0702702017-08-24 22:04:00 -040098
Junxiao Shi78c78cc2015-06-19 15:53:53 -070099 os << ")";
Junxiao Shie5adbfd2015-06-18 14:42:06 -0700100}
101
Junxiao Shie5adbfd2015-06-18 14:42:06 -0700102void
Junxiao Shi78c78cc2015-06-19 15:53:53 -0700103NdnDissect::printBlock(std::ostream& os, const Block& block)
Junxiao Shie5adbfd2015-06-18 14:42:06 -0700104{
Davide Pesaventoa2d4b142018-04-20 16:46:29 -0400105 printType(os, block.type());
Junxiao Shi78c78cc2015-06-19 15:53:53 -0700106 os << " (size: " << block.value_size() << ")";
Junxiao Shie5adbfd2015-06-18 14:42:06 -0700107
108 try {
109 // if (block.type() != tlv::Content && block.type() != tlv::SignatureValue)
110 block.parse();
111 }
Davide Pesaventoc0702702017-08-24 22:04:00 -0400112 catch (const tlv::Error&) {
Junxiao Shie5adbfd2015-06-18 14:42:06 -0700113 // 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 Shi78c78cc2015-06-19 15:53:53 -0700118 if (block.elements().empty()) {
119 os << " [[";
120 name::Component(block.value(), block.value_size()).toUri(os);
Davide Pesavento9b1fd4b2021-01-26 22:16:02 -0500121 os << "]]";
Junxiao Shi78c78cc2015-06-19 15:53:53 -0700122 }
Davide Pesavento9b1fd4b2021-01-26 22:16:02 -0500123 os << "\n";
Junxiao Shie5adbfd2015-06-18 14:42:06 -0700124
Junxiao Shi78c78cc2015-06-19 15:53:53 -0700125 util::IndentedStream os2(os, " ");
126 std::for_each(block.elements_begin(), block.elements_end(),
Davide Pesaventoa2d4b142018-04-20 16:46:29 -0400127 [this, &os2] (const Block& element) { printBlock(os2, element); });
Junxiao Shie5adbfd2015-06-18 14:42:06 -0700128}
129
130void
Junxiao Shi78c78cc2015-06-19 15:53:53 -0700131NdnDissect::dissect(std::ostream& os, std::istream& is)
Junxiao Shie5adbfd2015-06-18 14:42:06 -0700132{
Davide Pesavento9b1fd4b2021-01-26 22:16:02 -0500133 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 Shie5adbfd2015-06-18 14:42:06 -0700139 }
Davide Pesavento9b1fd4b2021-01-26 22:16:02 -0500140 }
141 catch (const std::exception& e) {
142 std::cerr << "ERROR: " << e.what() << " at offset " << offset << "\n";
Junxiao Shie5adbfd2015-06-18 14:42:06 -0700143 }
Junxiao Shie5adbfd2015-06-18 14:42:06 -0700144}
145
Junxiao Shi78c78cc2015-06-19 15:53:53 -0700146} // namespace dissect
Junxiao Shie5adbfd2015-06-18 14:42:06 -0700147} // namespace ndn