blob: 6cd86592ce96d517c6230cf9766170a24a759d70 [file] [log] [blame]
Junxiao Shie5adbfd2015-06-18 14:42:06 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Davide Pesaventoc0702702017-08-24 22:04:00 -04003 * Copyright (c) 2014-2017, 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/>.
18 */
19/**
Junxiao Shie5adbfd2015-06-18 14:42:06 -070020 * Copyright (c) 2013-2014 Regents of the University of California.
21 *
22 * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
23 *
24 * ndn-cxx library is free software: you can redistribute it and/or modify it under the
25 * terms of the GNU Lesser General Public License as published by the Free Software
26 * Foundation, either version 3 of the License, or (at your option) any later version.
27 *
28 * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY
29 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
30 * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
31 *
32 * You should have received copies of the GNU General Public License and GNU Lesser
33 * General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see
34 * <http://www.gnu.org/licenses/>.
35 *
36 * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
37 *
38 * @author Alexander Afanasyev <http://lasr.cs.ucla.edu/afanasyev/index.html>
39 */
40
Junxiao Shi78c78cc2015-06-19 15:53:53 -070041#include "ndn-dissect.hpp"
Junxiao Shie5adbfd2015-06-18 14:42:06 -070042
Junxiao Shi78c78cc2015-06-19 15:53:53 -070043#include <algorithm>
Junxiao Shie5adbfd2015-06-18 14:42:06 -070044
Junxiao Shi78c78cc2015-06-19 15:53:53 -070045#include <ndn-cxx/name-component.hpp>
46#include <ndn-cxx/util/indented-stream.hpp>
47
Junxiao Shie5adbfd2015-06-18 14:42:06 -070048namespace ndn {
Junxiao Shi78c78cc2015-06-19 15:53:53 -070049namespace dissect {
Junxiao Shie5adbfd2015-06-18 14:42:06 -070050
Davide Pesaventoc0702702017-08-24 22:04:00 -040051static const std::map<uint32_t, std::string> TLV_DICT = {
Junxiao Shie5adbfd2015-06-18 14:42:06 -070052 {tlv::Interest , "Interest"},
53 {tlv::Data , "Data"},
54 {tlv::Name , "Name"},
55 {tlv::NameComponent , "NameComponent"},
56 {tlv::ImplicitSha256DigestComponent, "ImplicitSha256DigestComponent"},
57 {tlv::Selectors , "Selectors"},
58 {tlv::Nonce , "Nonce"},
59 {tlv::InterestLifetime , "InterestLifetime"},
60 {tlv::MinSuffixComponents , "MinSuffixComponents"},
61 {tlv::MaxSuffixComponents , "MaxSuffixComponents"},
62 {tlv::PublisherPublicKeyLocator , "PublisherPublicKeyLocator"},
63 {tlv::Exclude , "Exclude"},
64 {tlv::ChildSelector , "ChildSelector"},
65 {tlv::MustBeFresh , "MustBeFresh"},
66 {tlv::Any , "Any"},
67 {tlv::MetaInfo , "MetaInfo"},
68 {tlv::Content , "Content"},
69 {tlv::SignatureInfo , "SignatureInfo"},
70 {tlv::SignatureValue , "SignatureValue"},
71 {tlv::ContentType , "ContentType"},
72 {tlv::FreshnessPeriod , "FreshnessPeriod"},
73 {tlv::FinalBlockId , "FinalBlockId"},
74 {tlv::SignatureType , "SignatureType"},
75 {tlv::KeyLocator , "KeyLocator"},
76 {tlv::KeyDigest , "KeyDigest"},
77};
78
79void
Junxiao Shi78c78cc2015-06-19 15:53:53 -070080NdnDissect::printType(std::ostream& os, uint32_t type)
Junxiao Shie5adbfd2015-06-18 14:42:06 -070081{
Junxiao Shi78c78cc2015-06-19 15:53:53 -070082 os << type << " (";
Junxiao Shie5adbfd2015-06-18 14:42:06 -070083
Davide Pesaventoc0702702017-08-24 22:04:00 -040084 auto it = TLV_DICT.find(type);
85 if (it != TLV_DICT.end()) {
86 os << it->second;
Junxiao Shie5adbfd2015-06-18 14:42:06 -070087 }
88 else if (type < tlv::AppPrivateBlock1) {
Junxiao Shi78c78cc2015-06-19 15:53:53 -070089 os << "RESERVED_1";
Junxiao Shie5adbfd2015-06-18 14:42:06 -070090 }
91 else if (tlv::AppPrivateBlock1 <= type && type < 253) {
Junxiao Shi78c78cc2015-06-19 15:53:53 -070092 os << "APP_TAG_1";
Junxiao Shie5adbfd2015-06-18 14:42:06 -070093 }
94 else if (253 <= type && type < tlv::AppPrivateBlock2) {
Junxiao Shi78c78cc2015-06-19 15:53:53 -070095 os << "RESERVED_3";
Junxiao Shie5adbfd2015-06-18 14:42:06 -070096 }
97 else {
Junxiao Shi78c78cc2015-06-19 15:53:53 -070098 os << "APP_TAG_3";
Junxiao Shie5adbfd2015-06-18 14:42:06 -070099 }
Davide Pesaventoc0702702017-08-24 22:04:00 -0400100
Junxiao Shi78c78cc2015-06-19 15:53:53 -0700101 os << ")";
Junxiao Shie5adbfd2015-06-18 14:42:06 -0700102}
103
Junxiao Shie5adbfd2015-06-18 14:42:06 -0700104void
Junxiao Shi78c78cc2015-06-19 15:53:53 -0700105NdnDissect::printBlock(std::ostream& os, const Block& block)
Junxiao Shie5adbfd2015-06-18 14:42:06 -0700106{
Junxiao Shi78c78cc2015-06-19 15:53:53 -0700107 this->printType(os, block.type());
108 os << " (size: " << block.value_size() << ")";
Junxiao Shie5adbfd2015-06-18 14:42:06 -0700109
110 try {
111 // if (block.type() != tlv::Content && block.type() != tlv::SignatureValue)
112 block.parse();
113 }
Davide Pesaventoc0702702017-08-24 22:04:00 -0400114 catch (const tlv::Error&) {
Junxiao Shie5adbfd2015-06-18 14:42:06 -0700115 // pass (e.g., leaf block reached)
116
117 // @todo: Figure how to deterministically figure out that value is not recursive TLV block
118 }
119
Junxiao Shi78c78cc2015-06-19 15:53:53 -0700120 if (block.elements().empty()) {
121 os << " [[";
122 name::Component(block.value(), block.value_size()).toUri(os);
123 os<< "]]";
124 }
125 os << std::endl;
Junxiao Shie5adbfd2015-06-18 14:42:06 -0700126
Junxiao Shi78c78cc2015-06-19 15:53:53 -0700127 util::IndentedStream os2(os, " ");
128 std::for_each(block.elements_begin(), block.elements_end(),
129 [this, &os2] (const Block& element) {
130 this->printBlock(os2, element);
131 });
Junxiao Shie5adbfd2015-06-18 14:42:06 -0700132}
133
134void
Junxiao Shi78c78cc2015-06-19 15:53:53 -0700135NdnDissect::dissect(std::ostream& os, std::istream& is)
Junxiao Shie5adbfd2015-06-18 14:42:06 -0700136{
137 while (is.peek() != std::char_traits<char>::eof()) {
138 try {
139 Block block = Block::fromStream(is);
Junxiao Shi78c78cc2015-06-19 15:53:53 -0700140 this->printBlock(os, block);
Junxiao Shie5adbfd2015-06-18 14:42:06 -0700141 }
Davide Pesaventoc0702702017-08-24 22:04:00 -0400142 catch (const std::exception& e) {
Junxiao Shie5adbfd2015-06-18 14:42:06 -0700143 std::cerr << "ERROR: " << e.what() << std::endl;
144 }
145 }
Junxiao Shie5adbfd2015-06-18 14:42:06 -0700146}
147
Junxiao Shi78c78cc2015-06-19 15:53:53 -0700148} // namespace dissect
Junxiao Shie5adbfd2015-06-18 14:42:06 -0700149} // namespace ndn