blob: 90d31608811996ee75e53527e85c88b9d0b70560 [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/*
3 * Copyright (c) 2013-2018, 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::GenericNameComponent , "GenericNameComponent"},
Junxiao Shie5adbfd2015-06-18 14:42:06 -070040 {tlv::ImplicitSha256DigestComponent, "ImplicitSha256DigestComponent"},
Davide Pesaventoa2d4b142018-04-20 16:46:29 -040041 {tlv::CanBePrefix , "CanBePrefix"},
42 {tlv::MustBeFresh , "MustBeFresh"},
43 //{tlv::ForwardingHint , "ForwardingHint"},
Junxiao Shie5adbfd2015-06-18 14:42:06 -070044 {tlv::Nonce , "Nonce"},
45 {tlv::InterestLifetime , "InterestLifetime"},
Davide Pesaventoa2d4b142018-04-20 16:46:29 -040046 {tlv::HopLimit , "HopLimit"},
47 {tlv::Parameters , "Parameters"},
Junxiao Shie5adbfd2015-06-18 14:42:06 -070048 {tlv::MetaInfo , "MetaInfo"},
49 {tlv::Content , "Content"},
50 {tlv::SignatureInfo , "SignatureInfo"},
51 {tlv::SignatureValue , "SignatureValue"},
52 {tlv::ContentType , "ContentType"},
53 {tlv::FreshnessPeriod , "FreshnessPeriod"},
54 {tlv::FinalBlockId , "FinalBlockId"},
55 {tlv::SignatureType , "SignatureType"},
56 {tlv::KeyLocator , "KeyLocator"},
57 {tlv::KeyDigest , "KeyDigest"},
Davide Pesaventoa2d4b142018-04-20 16:46:29 -040058 // Deprecated elements
59 {tlv::Selectors , "Selectors"},
60 {tlv::MinSuffixComponents , "MinSuffixComponents"},
61 {tlv::MaxSuffixComponents , "MaxSuffixComponents"},
62 {tlv::PublisherPublicKeyLocator , "PublisherPublicKeyLocator"},
63 {tlv::Exclude , "Exclude"},
64 {tlv::ChildSelector , "ChildSelector"},
65 {tlv::Any , "Any"},
Junxiao Shie5adbfd2015-06-18 14:42:06 -070066};
67
68void
Junxiao Shi78c78cc2015-06-19 15:53:53 -070069NdnDissect::printType(std::ostream& os, uint32_t type)
Junxiao Shie5adbfd2015-06-18 14:42:06 -070070{
Junxiao Shi78c78cc2015-06-19 15:53:53 -070071 os << type << " (";
Junxiao Shie5adbfd2015-06-18 14:42:06 -070072
Davide Pesaventoc0702702017-08-24 22:04:00 -040073 auto it = TLV_DICT.find(type);
74 if (it != TLV_DICT.end()) {
75 os << it->second;
Junxiao Shie5adbfd2015-06-18 14:42:06 -070076 }
77 else if (type < tlv::AppPrivateBlock1) {
Junxiao Shi78c78cc2015-06-19 15:53:53 -070078 os << "RESERVED_1";
Junxiao Shie5adbfd2015-06-18 14:42:06 -070079 }
80 else if (tlv::AppPrivateBlock1 <= type && type < 253) {
Junxiao Shi78c78cc2015-06-19 15:53:53 -070081 os << "APP_TAG_1";
Junxiao Shie5adbfd2015-06-18 14:42:06 -070082 }
83 else if (253 <= type && type < tlv::AppPrivateBlock2) {
Junxiao Shi78c78cc2015-06-19 15:53:53 -070084 os << "RESERVED_3";
Junxiao Shie5adbfd2015-06-18 14:42:06 -070085 }
86 else {
Junxiao Shi78c78cc2015-06-19 15:53:53 -070087 os << "APP_TAG_3";
Junxiao Shie5adbfd2015-06-18 14:42:06 -070088 }
Davide Pesaventoc0702702017-08-24 22:04:00 -040089
Junxiao Shi78c78cc2015-06-19 15:53:53 -070090 os << ")";
Junxiao Shie5adbfd2015-06-18 14:42:06 -070091}
92
Junxiao Shie5adbfd2015-06-18 14:42:06 -070093void
Junxiao Shi78c78cc2015-06-19 15:53:53 -070094NdnDissect::printBlock(std::ostream& os, const Block& block)
Junxiao Shie5adbfd2015-06-18 14:42:06 -070095{
Davide Pesaventoa2d4b142018-04-20 16:46:29 -040096 printType(os, block.type());
Junxiao Shi78c78cc2015-06-19 15:53:53 -070097 os << " (size: " << block.value_size() << ")";
Junxiao Shie5adbfd2015-06-18 14:42:06 -070098
99 try {
100 // if (block.type() != tlv::Content && block.type() != tlv::SignatureValue)
101 block.parse();
102 }
Davide Pesaventoc0702702017-08-24 22:04:00 -0400103 catch (const tlv::Error&) {
Junxiao Shie5adbfd2015-06-18 14:42:06 -0700104 // pass (e.g., leaf block reached)
105
106 // @todo: Figure how to deterministically figure out that value is not recursive TLV block
107 }
108
Junxiao Shi78c78cc2015-06-19 15:53:53 -0700109 if (block.elements().empty()) {
110 os << " [[";
111 name::Component(block.value(), block.value_size()).toUri(os);
112 os<< "]]";
113 }
114 os << std::endl;
Junxiao Shie5adbfd2015-06-18 14:42:06 -0700115
Junxiao Shi78c78cc2015-06-19 15:53:53 -0700116 util::IndentedStream os2(os, " ");
117 std::for_each(block.elements_begin(), block.elements_end(),
Davide Pesaventoa2d4b142018-04-20 16:46:29 -0400118 [this, &os2] (const Block& element) { printBlock(os2, element); });
Junxiao Shie5adbfd2015-06-18 14:42:06 -0700119}
120
121void
Junxiao Shi78c78cc2015-06-19 15:53:53 -0700122NdnDissect::dissect(std::ostream& os, std::istream& is)
Junxiao Shie5adbfd2015-06-18 14:42:06 -0700123{
124 while (is.peek() != std::char_traits<char>::eof()) {
125 try {
Davide Pesaventoa2d4b142018-04-20 16:46:29 -0400126 printBlock(os, Block::fromStream(is));
Junxiao Shie5adbfd2015-06-18 14:42:06 -0700127 }
Davide Pesaventoc0702702017-08-24 22:04:00 -0400128 catch (const std::exception& e) {
Junxiao Shie5adbfd2015-06-18 14:42:06 -0700129 std::cerr << "ERROR: " << e.what() << std::endl;
130 }
131 }
Junxiao Shie5adbfd2015-06-18 14:42:06 -0700132}
133
Junxiao Shi78c78cc2015-06-19 15:53:53 -0700134} // namespace dissect
Junxiao Shie5adbfd2015-06-18 14:42:06 -0700135} // namespace ndn