Junxiao Shi | e5adbfd | 2015-06-18 14:42:06 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Davide Pesavento | a2d4b14 | 2018-04-20 16:46:29 -0400 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (c) 2013-2018, Regents of the University of California. |
Junxiao Shi | 78c78cc | 2015-06-19 15:53:53 -0700 | [diff] [blame] | 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/>. |
Junxiao Shi | e5adbfd | 2015-06-18 14:42:06 -0700 | [diff] [blame] | 18 | * |
| 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 Shi | 78c78cc | 2015-06-19 15:53:53 -0700 | [diff] [blame] | 24 | #include "ndn-dissect.hpp" |
Junxiao Shi | e5adbfd | 2015-06-18 14:42:06 -0700 | [diff] [blame] | 25 | |
Junxiao Shi | 78c78cc | 2015-06-19 15:53:53 -0700 | [diff] [blame] | 26 | #include <algorithm> |
Junxiao Shi | e5adbfd | 2015-06-18 14:42:06 -0700 | [diff] [blame] | 27 | |
Junxiao Shi | 78c78cc | 2015-06-19 15:53:53 -0700 | [diff] [blame] | 28 | #include <ndn-cxx/name-component.hpp> |
Davide Pesavento | a2d4b14 | 2018-04-20 16:46:29 -0400 | [diff] [blame] | 29 | #include <ndn-cxx/encoding/tlv.hpp> |
Junxiao Shi | 78c78cc | 2015-06-19 15:53:53 -0700 | [diff] [blame] | 30 | #include <ndn-cxx/util/indented-stream.hpp> |
| 31 | |
Junxiao Shi | e5adbfd | 2015-06-18 14:42:06 -0700 | [diff] [blame] | 32 | namespace ndn { |
Junxiao Shi | 78c78cc | 2015-06-19 15:53:53 -0700 | [diff] [blame] | 33 | namespace dissect { |
Junxiao Shi | e5adbfd | 2015-06-18 14:42:06 -0700 | [diff] [blame] | 34 | |
Davide Pesavento | a2d4b14 | 2018-04-20 16:46:29 -0400 | [diff] [blame] | 35 | static const std::map<uint32_t, const char*> TLV_DICT = { |
Junxiao Shi | e5adbfd | 2015-06-18 14:42:06 -0700 | [diff] [blame] | 36 | {tlv::Interest , "Interest"}, |
| 37 | {tlv::Data , "Data"}, |
| 38 | {tlv::Name , "Name"}, |
Davide Pesavento | a2d4b14 | 2018-04-20 16:46:29 -0400 | [diff] [blame] | 39 | {tlv::GenericNameComponent , "GenericNameComponent"}, |
Junxiao Shi | e5adbfd | 2015-06-18 14:42:06 -0700 | [diff] [blame] | 40 | {tlv::ImplicitSha256DigestComponent, "ImplicitSha256DigestComponent"}, |
Davide Pesavento | a2d4b14 | 2018-04-20 16:46:29 -0400 | [diff] [blame] | 41 | {tlv::CanBePrefix , "CanBePrefix"}, |
| 42 | {tlv::MustBeFresh , "MustBeFresh"}, |
| 43 | //{tlv::ForwardingHint , "ForwardingHint"}, |
Junxiao Shi | e5adbfd | 2015-06-18 14:42:06 -0700 | [diff] [blame] | 44 | {tlv::Nonce , "Nonce"}, |
| 45 | {tlv::InterestLifetime , "InterestLifetime"}, |
Davide Pesavento | a2d4b14 | 2018-04-20 16:46:29 -0400 | [diff] [blame] | 46 | {tlv::HopLimit , "HopLimit"}, |
| 47 | {tlv::Parameters , "Parameters"}, |
Junxiao Shi | e5adbfd | 2015-06-18 14:42:06 -0700 | [diff] [blame] | 48 | {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 Pesavento | a2d4b14 | 2018-04-20 16:46:29 -0400 | [diff] [blame] | 58 | // 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 Shi | e5adbfd | 2015-06-18 14:42:06 -0700 | [diff] [blame] | 66 | }; |
| 67 | |
| 68 | void |
Junxiao Shi | 78c78cc | 2015-06-19 15:53:53 -0700 | [diff] [blame] | 69 | NdnDissect::printType(std::ostream& os, uint32_t type) |
Junxiao Shi | e5adbfd | 2015-06-18 14:42:06 -0700 | [diff] [blame] | 70 | { |
Junxiao Shi | 78c78cc | 2015-06-19 15:53:53 -0700 | [diff] [blame] | 71 | os << type << " ("; |
Junxiao Shi | e5adbfd | 2015-06-18 14:42:06 -0700 | [diff] [blame] | 72 | |
Davide Pesavento | c070270 | 2017-08-24 22:04:00 -0400 | [diff] [blame] | 73 | auto it = TLV_DICT.find(type); |
| 74 | if (it != TLV_DICT.end()) { |
| 75 | os << it->second; |
Junxiao Shi | e5adbfd | 2015-06-18 14:42:06 -0700 | [diff] [blame] | 76 | } |
| 77 | else if (type < tlv::AppPrivateBlock1) { |
Junxiao Shi | 78c78cc | 2015-06-19 15:53:53 -0700 | [diff] [blame] | 78 | os << "RESERVED_1"; |
Junxiao Shi | e5adbfd | 2015-06-18 14:42:06 -0700 | [diff] [blame] | 79 | } |
| 80 | else if (tlv::AppPrivateBlock1 <= type && type < 253) { |
Junxiao Shi | 78c78cc | 2015-06-19 15:53:53 -0700 | [diff] [blame] | 81 | os << "APP_TAG_1"; |
Junxiao Shi | e5adbfd | 2015-06-18 14:42:06 -0700 | [diff] [blame] | 82 | } |
| 83 | else if (253 <= type && type < tlv::AppPrivateBlock2) { |
Junxiao Shi | 78c78cc | 2015-06-19 15:53:53 -0700 | [diff] [blame] | 84 | os << "RESERVED_3"; |
Junxiao Shi | e5adbfd | 2015-06-18 14:42:06 -0700 | [diff] [blame] | 85 | } |
| 86 | else { |
Junxiao Shi | 78c78cc | 2015-06-19 15:53:53 -0700 | [diff] [blame] | 87 | os << "APP_TAG_3"; |
Junxiao Shi | e5adbfd | 2015-06-18 14:42:06 -0700 | [diff] [blame] | 88 | } |
Davide Pesavento | c070270 | 2017-08-24 22:04:00 -0400 | [diff] [blame] | 89 | |
Junxiao Shi | 78c78cc | 2015-06-19 15:53:53 -0700 | [diff] [blame] | 90 | os << ")"; |
Junxiao Shi | e5adbfd | 2015-06-18 14:42:06 -0700 | [diff] [blame] | 91 | } |
| 92 | |
Junxiao Shi | e5adbfd | 2015-06-18 14:42:06 -0700 | [diff] [blame] | 93 | void |
Junxiao Shi | 78c78cc | 2015-06-19 15:53:53 -0700 | [diff] [blame] | 94 | NdnDissect::printBlock(std::ostream& os, const Block& block) |
Junxiao Shi | e5adbfd | 2015-06-18 14:42:06 -0700 | [diff] [blame] | 95 | { |
Davide Pesavento | a2d4b14 | 2018-04-20 16:46:29 -0400 | [diff] [blame] | 96 | printType(os, block.type()); |
Junxiao Shi | 78c78cc | 2015-06-19 15:53:53 -0700 | [diff] [blame] | 97 | os << " (size: " << block.value_size() << ")"; |
Junxiao Shi | e5adbfd | 2015-06-18 14:42:06 -0700 | [diff] [blame] | 98 | |
| 99 | try { |
| 100 | // if (block.type() != tlv::Content && block.type() != tlv::SignatureValue) |
| 101 | block.parse(); |
| 102 | } |
Davide Pesavento | c070270 | 2017-08-24 22:04:00 -0400 | [diff] [blame] | 103 | catch (const tlv::Error&) { |
Junxiao Shi | e5adbfd | 2015-06-18 14:42:06 -0700 | [diff] [blame] | 104 | // 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 Shi | 78c78cc | 2015-06-19 15:53:53 -0700 | [diff] [blame] | 109 | if (block.elements().empty()) { |
| 110 | os << " [["; |
| 111 | name::Component(block.value(), block.value_size()).toUri(os); |
| 112 | os<< "]]"; |
| 113 | } |
| 114 | os << std::endl; |
Junxiao Shi | e5adbfd | 2015-06-18 14:42:06 -0700 | [diff] [blame] | 115 | |
Junxiao Shi | 78c78cc | 2015-06-19 15:53:53 -0700 | [diff] [blame] | 116 | util::IndentedStream os2(os, " "); |
| 117 | std::for_each(block.elements_begin(), block.elements_end(), |
Davide Pesavento | a2d4b14 | 2018-04-20 16:46:29 -0400 | [diff] [blame] | 118 | [this, &os2] (const Block& element) { printBlock(os2, element); }); |
Junxiao Shi | e5adbfd | 2015-06-18 14:42:06 -0700 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | void |
Junxiao Shi | 78c78cc | 2015-06-19 15:53:53 -0700 | [diff] [blame] | 122 | NdnDissect::dissect(std::ostream& os, std::istream& is) |
Junxiao Shi | e5adbfd | 2015-06-18 14:42:06 -0700 | [diff] [blame] | 123 | { |
| 124 | while (is.peek() != std::char_traits<char>::eof()) { |
| 125 | try { |
Davide Pesavento | a2d4b14 | 2018-04-20 16:46:29 -0400 | [diff] [blame] | 126 | printBlock(os, Block::fromStream(is)); |
Junxiao Shi | e5adbfd | 2015-06-18 14:42:06 -0700 | [diff] [blame] | 127 | } |
Davide Pesavento | c070270 | 2017-08-24 22:04:00 -0400 | [diff] [blame] | 128 | catch (const std::exception& e) { |
Junxiao Shi | e5adbfd | 2015-06-18 14:42:06 -0700 | [diff] [blame] | 129 | std::cerr << "ERROR: " << e.what() << std::endl; |
| 130 | } |
| 131 | } |
Junxiao Shi | e5adbfd | 2015-06-18 14:42:06 -0700 | [diff] [blame] | 132 | } |
| 133 | |
Junxiao Shi | 78c78cc | 2015-06-19 15:53:53 -0700 | [diff] [blame] | 134 | } // namespace dissect |
Junxiao Shi | e5adbfd | 2015-06-18 14:42:06 -0700 | [diff] [blame] | 135 | } // namespace ndn |