blob: c072c69f9b59506ec56767960cddf6e3f2a64bec [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 Pesavento105cd9e2019-04-06 18:13:44 -04003 * Copyright (c) 2013-2019, 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>
Junxiao Shie5adbfd2015-06-18 14:42:06 -070027
Junxiao Shi78c78cc2015-06-19 15:53:53 -070028#include <ndn-cxx/name-component.hpp>
Davide Pesaventoa2d4b142018-04-20 16:46:29 -040029#include <ndn-cxx/encoding/tlv.hpp>
Junxiao Shi78c78cc2015-06-19 15:53:53 -070030#include <ndn-cxx/util/indented-stream.hpp>
31
Junxiao Shie5adbfd2015-06-18 14:42:06 -070032namespace ndn {
Junxiao Shi78c78cc2015-06-19 15:53:53 -070033namespace dissect {
Junxiao Shie5adbfd2015-06-18 14:42:06 -070034
Davide Pesaventoa2d4b142018-04-20 16:46:29 -040035static const std::map<uint32_t, const char*> TLV_DICT = {
Junxiao Shie5adbfd2015-06-18 14:42:06 -070036 {tlv::Interest , "Interest"},
37 {tlv::Data , "Data"},
38 {tlv::Name , "Name"},
Davide Pesaventoa2d4b142018-04-20 16:46:29 -040039 {tlv::CanBePrefix , "CanBePrefix"},
40 {tlv::MustBeFresh , "MustBeFresh"},
41 //{tlv::ForwardingHint , "ForwardingHint"},
Junxiao Shie5adbfd2015-06-18 14:42:06 -070042 {tlv::Nonce , "Nonce"},
43 {tlv::InterestLifetime , "InterestLifetime"},
Davide Pesaventoa2d4b142018-04-20 16:46:29 -040044 {tlv::HopLimit , "HopLimit"},
Davide Pesavento105cd9e2019-04-06 18:13:44 -040045 {tlv::ApplicationParameters , "ApplicationParameters"},
Junxiao Shie5adbfd2015-06-18 14:42:06 -070046 {tlv::MetaInfo , "MetaInfo"},
47 {tlv::Content , "Content"},
48 {tlv::SignatureInfo , "SignatureInfo"},
49 {tlv::SignatureValue , "SignatureValue"},
50 {tlv::ContentType , "ContentType"},
51 {tlv::FreshnessPeriod , "FreshnessPeriod"},
52 {tlv::FinalBlockId , "FinalBlockId"},
53 {tlv::SignatureType , "SignatureType"},
54 {tlv::KeyLocator , "KeyLocator"},
55 {tlv::KeyDigest , "KeyDigest"},
Davide Pesavento105cd9e2019-04-06 18:13:44 -040056 // Name components
57 {tlv::GenericNameComponent , "GenericNameComponent"},
58 {tlv::ImplicitSha256DigestComponent , "ImplicitSha256DigestComponent"},
59 {tlv::ParametersSha256DigestComponent, "ParametersSha256DigestComponent"},
60 {tlv::KeywordNameComponent , "KeywordNameComponent"},
61 //{tlv::SegmentNameComponent , "SegmentNameComponent"},
62 //{tlv::ByteOffsetNameComponent , "ByteOffsetNameComponent"},
63 //{tlv::VersionNameComponent , "VersionNameComponent"},
64 //{tlv::TimestampNameComponent , "TimestampNameComponent"},
65 //{tlv::SequenceNumNameComponent , "SequenceNumNameComponent"},
Davide Pesaventoa2d4b142018-04-20 16:46:29 -040066 // Deprecated elements
67 {tlv::Selectors , "Selectors"},
68 {tlv::MinSuffixComponents , "MinSuffixComponents"},
69 {tlv::MaxSuffixComponents , "MaxSuffixComponents"},
70 {tlv::PublisherPublicKeyLocator , "PublisherPublicKeyLocator"},
71 {tlv::Exclude , "Exclude"},
72 {tlv::ChildSelector , "ChildSelector"},
73 {tlv::Any , "Any"},
Junxiao Shie5adbfd2015-06-18 14:42:06 -070074};
75
76void
Junxiao Shi78c78cc2015-06-19 15:53:53 -070077NdnDissect::printType(std::ostream& os, uint32_t type)
Junxiao Shie5adbfd2015-06-18 14:42:06 -070078{
Junxiao Shi78c78cc2015-06-19 15:53:53 -070079 os << type << " (";
Junxiao Shie5adbfd2015-06-18 14:42:06 -070080
Davide Pesaventoc0702702017-08-24 22:04:00 -040081 auto it = TLV_DICT.find(type);
82 if (it != TLV_DICT.end()) {
83 os << it->second;
Junxiao Shie5adbfd2015-06-18 14:42:06 -070084 }
85 else if (type < tlv::AppPrivateBlock1) {
Junxiao Shi78c78cc2015-06-19 15:53:53 -070086 os << "RESERVED_1";
Junxiao Shie5adbfd2015-06-18 14:42:06 -070087 }
88 else if (tlv::AppPrivateBlock1 <= type && type < 253) {
Junxiao Shi78c78cc2015-06-19 15:53:53 -070089 os << "APP_TAG_1";
Junxiao Shie5adbfd2015-06-18 14:42:06 -070090 }
91 else if (253 <= type && type < tlv::AppPrivateBlock2) {
Junxiao Shi78c78cc2015-06-19 15:53:53 -070092 os << "RESERVED_3";
Junxiao Shie5adbfd2015-06-18 14:42:06 -070093 }
94 else {
Junxiao Shi78c78cc2015-06-19 15:53:53 -070095 os << "APP_TAG_3";
Junxiao Shie5adbfd2015-06-18 14:42:06 -070096 }
Davide Pesaventoc0702702017-08-24 22:04:00 -040097
Junxiao Shi78c78cc2015-06-19 15:53:53 -070098 os << ")";
Junxiao Shie5adbfd2015-06-18 14:42:06 -070099}
100
Junxiao Shie5adbfd2015-06-18 14:42:06 -0700101void
Junxiao Shi78c78cc2015-06-19 15:53:53 -0700102NdnDissect::printBlock(std::ostream& os, const Block& block)
Junxiao Shie5adbfd2015-06-18 14:42:06 -0700103{
Davide Pesaventoa2d4b142018-04-20 16:46:29 -0400104 printType(os, block.type());
Junxiao Shi78c78cc2015-06-19 15:53:53 -0700105 os << " (size: " << block.value_size() << ")";
Junxiao Shie5adbfd2015-06-18 14:42:06 -0700106
107 try {
108 // if (block.type() != tlv::Content && block.type() != tlv::SignatureValue)
109 block.parse();
110 }
Davide Pesaventoc0702702017-08-24 22:04:00 -0400111 catch (const tlv::Error&) {
Junxiao Shie5adbfd2015-06-18 14:42:06 -0700112 // pass (e.g., leaf block reached)
113
114 // @todo: Figure how to deterministically figure out that value is not recursive TLV block
115 }
116
Junxiao Shi78c78cc2015-06-19 15:53:53 -0700117 if (block.elements().empty()) {
118 os << " [[";
119 name::Component(block.value(), block.value_size()).toUri(os);
120 os<< "]]";
121 }
122 os << std::endl;
Junxiao Shie5adbfd2015-06-18 14:42:06 -0700123
Junxiao Shi78c78cc2015-06-19 15:53:53 -0700124 util::IndentedStream os2(os, " ");
125 std::for_each(block.elements_begin(), block.elements_end(),
Davide Pesaventoa2d4b142018-04-20 16:46:29 -0400126 [this, &os2] (const Block& element) { printBlock(os2, element); });
Junxiao Shie5adbfd2015-06-18 14:42:06 -0700127}
128
129void
Junxiao Shi78c78cc2015-06-19 15:53:53 -0700130NdnDissect::dissect(std::ostream& os, std::istream& is)
Junxiao Shie5adbfd2015-06-18 14:42:06 -0700131{
132 while (is.peek() != std::char_traits<char>::eof()) {
133 try {
Davide Pesaventoa2d4b142018-04-20 16:46:29 -0400134 printBlock(os, Block::fromStream(is));
Junxiao Shie5adbfd2015-06-18 14:42:06 -0700135 }
Davide Pesaventoc0702702017-08-24 22:04:00 -0400136 catch (const std::exception& e) {
Junxiao Shie5adbfd2015-06-18 14:42:06 -0700137 std::cerr << "ERROR: " << e.what() << std::endl;
138 }
139 }
Junxiao Shie5adbfd2015-06-18 14:42:06 -0700140}
141
Junxiao Shi78c78cc2015-06-19 15:53:53 -0700142} // namespace dissect
Junxiao Shie5adbfd2015-06-18 14:42:06 -0700143} // namespace ndn