blob: f37195e7ed2394349834b878fa869b6f14188f7b [file] [log] [blame]
Junxiao Shie5adbfd2015-06-18 14:42:06 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Junxiao Shi78c78cc2015-06-19 15:53:53 -07003 * Copyright (c) 2014-2015, Regents of the University of California.
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/>.
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#include <map>
45
Junxiao Shi78c78cc2015-06-19 15:53:53 -070046#include <ndn-cxx/name-component.hpp>
47#include <ndn-cxx/util/indented-stream.hpp>
48
Junxiao Shie5adbfd2015-06-18 14:42:06 -070049namespace ndn {
Junxiao Shi78c78cc2015-06-19 15:53:53 -070050namespace dissect {
Junxiao Shie5adbfd2015-06-18 14:42:06 -070051
52std::map<uint32_t, std::string> TLV_DICT = {
53 {tlv::Interest , "Interest"},
54 {tlv::Data , "Data"},
55 {tlv::Name , "Name"},
56 {tlv::NameComponent , "NameComponent"},
57 {tlv::ImplicitSha256DigestComponent, "ImplicitSha256DigestComponent"},
58 {tlv::Selectors , "Selectors"},
59 {tlv::Nonce , "Nonce"},
60 {tlv::InterestLifetime , "InterestLifetime"},
61 {tlv::MinSuffixComponents , "MinSuffixComponents"},
62 {tlv::MaxSuffixComponents , "MaxSuffixComponents"},
63 {tlv::PublisherPublicKeyLocator , "PublisherPublicKeyLocator"},
64 {tlv::Exclude , "Exclude"},
65 {tlv::ChildSelector , "ChildSelector"},
66 {tlv::MustBeFresh , "MustBeFresh"},
67 {tlv::Any , "Any"},
68 {tlv::MetaInfo , "MetaInfo"},
69 {tlv::Content , "Content"},
70 {tlv::SignatureInfo , "SignatureInfo"},
71 {tlv::SignatureValue , "SignatureValue"},
72 {tlv::ContentType , "ContentType"},
73 {tlv::FreshnessPeriod , "FreshnessPeriod"},
74 {tlv::FinalBlockId , "FinalBlockId"},
75 {tlv::SignatureType , "SignatureType"},
76 {tlv::KeyLocator , "KeyLocator"},
77 {tlv::KeyDigest , "KeyDigest"},
78};
79
80void
Junxiao Shi78c78cc2015-06-19 15:53:53 -070081NdnDissect::printType(std::ostream& os, uint32_t type)
Junxiao Shie5adbfd2015-06-18 14:42:06 -070082{
Junxiao Shi78c78cc2015-06-19 15:53:53 -070083 os << type << " (";
Junxiao Shie5adbfd2015-06-18 14:42:06 -070084
85 if (TLV_DICT.count(type) != 0) {
Junxiao Shi78c78cc2015-06-19 15:53:53 -070086 os << TLV_DICT[type];
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 }
Junxiao Shi78c78cc2015-06-19 15:53:53 -0700100 os << ")";
Junxiao Shie5adbfd2015-06-18 14:42:06 -0700101}
102
Junxiao Shie5adbfd2015-06-18 14:42:06 -0700103void
Junxiao Shi78c78cc2015-06-19 15:53:53 -0700104NdnDissect::printBlock(std::ostream& os, const Block& block)
Junxiao Shie5adbfd2015-06-18 14:42:06 -0700105{
Junxiao Shi78c78cc2015-06-19 15:53:53 -0700106 this->printType(os, block.type());
107 os << " (size: " << block.value_size() << ")";
Junxiao Shie5adbfd2015-06-18 14:42:06 -0700108
109 try {
110 // if (block.type() != tlv::Content && block.type() != tlv::SignatureValue)
111 block.parse();
112 }
113 catch (tlv::Error& e) {
114 // pass (e.g., leaf block reached)
115
116 // @todo: Figure how to deterministically figure out that value is not recursive TLV block
117 }
118
Junxiao Shi78c78cc2015-06-19 15:53:53 -0700119 if (block.elements().empty()) {
120 os << " [[";
121 name::Component(block.value(), block.value_size()).toUri(os);
122 os<< "]]";
123 }
124 os << std::endl;
Junxiao Shie5adbfd2015-06-18 14:42:06 -0700125
Junxiao Shi78c78cc2015-06-19 15:53:53 -0700126 util::IndentedStream os2(os, " ");
127 std::for_each(block.elements_begin(), block.elements_end(),
128 [this, &os2] (const Block& element) {
129 this->printBlock(os2, element);
130 });
Junxiao Shie5adbfd2015-06-18 14:42:06 -0700131}
132
133void
Junxiao Shi78c78cc2015-06-19 15:53:53 -0700134NdnDissect::dissect(std::ostream& os, std::istream& is)
Junxiao Shie5adbfd2015-06-18 14:42:06 -0700135{
136 while (is.peek() != std::char_traits<char>::eof()) {
137 try {
138 Block block = Block::fromStream(is);
Junxiao Shi78c78cc2015-06-19 15:53:53 -0700139 this->printBlock(os, block);
Junxiao Shie5adbfd2015-06-18 14:42:06 -0700140 }
141 catch (std::exception& e) {
142 std::cerr << "ERROR: " << e.what() << std::endl;
143 }
144 }
Junxiao Shie5adbfd2015-06-18 14:42:06 -0700145}
146
Junxiao Shi78c78cc2015-06-19 15:53:53 -0700147} // namespace dissect
Junxiao Shie5adbfd2015-06-18 14:42:06 -0700148} // namespace ndn