Junxiao Shi | e5adbfd | 2015-06-18 14:42:06 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Davide Pesavento | c070270 | 2017-08-24 22:04:00 -0400 | [diff] [blame^] | 3 | * Copyright (c) 2014-2017, 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/>. |
| 18 | */ |
| 19 | /** |
Junxiao Shi | e5adbfd | 2015-06-18 14:42:06 -0700 | [diff] [blame] | 20 | * 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 Shi | 78c78cc | 2015-06-19 15:53:53 -0700 | [diff] [blame] | 41 | #include "ndn-dissect.hpp" |
Junxiao Shi | e5adbfd | 2015-06-18 14:42:06 -0700 | [diff] [blame] | 42 | |
Junxiao Shi | 78c78cc | 2015-06-19 15:53:53 -0700 | [diff] [blame] | 43 | #include <algorithm> |
Junxiao Shi | e5adbfd | 2015-06-18 14:42:06 -0700 | [diff] [blame] | 44 | |
Junxiao Shi | 78c78cc | 2015-06-19 15:53:53 -0700 | [diff] [blame] | 45 | #include <ndn-cxx/name-component.hpp> |
| 46 | #include <ndn-cxx/util/indented-stream.hpp> |
| 47 | |
Junxiao Shi | e5adbfd | 2015-06-18 14:42:06 -0700 | [diff] [blame] | 48 | namespace ndn { |
Junxiao Shi | 78c78cc | 2015-06-19 15:53:53 -0700 | [diff] [blame] | 49 | namespace dissect { |
Junxiao Shi | e5adbfd | 2015-06-18 14:42:06 -0700 | [diff] [blame] | 50 | |
Davide Pesavento | c070270 | 2017-08-24 22:04:00 -0400 | [diff] [blame^] | 51 | static const std::map<uint32_t, std::string> TLV_DICT = { |
Junxiao Shi | e5adbfd | 2015-06-18 14:42:06 -0700 | [diff] [blame] | 52 | {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 | |
| 79 | void |
Junxiao Shi | 78c78cc | 2015-06-19 15:53:53 -0700 | [diff] [blame] | 80 | NdnDissect::printType(std::ostream& os, uint32_t type) |
Junxiao Shi | e5adbfd | 2015-06-18 14:42:06 -0700 | [diff] [blame] | 81 | { |
Junxiao Shi | 78c78cc | 2015-06-19 15:53:53 -0700 | [diff] [blame] | 82 | os << type << " ("; |
Junxiao Shi | e5adbfd | 2015-06-18 14:42:06 -0700 | [diff] [blame] | 83 | |
Davide Pesavento | c070270 | 2017-08-24 22:04:00 -0400 | [diff] [blame^] | 84 | auto it = TLV_DICT.find(type); |
| 85 | if (it != TLV_DICT.end()) { |
| 86 | os << it->second; |
Junxiao Shi | e5adbfd | 2015-06-18 14:42:06 -0700 | [diff] [blame] | 87 | } |
| 88 | else if (type < tlv::AppPrivateBlock1) { |
Junxiao Shi | 78c78cc | 2015-06-19 15:53:53 -0700 | [diff] [blame] | 89 | os << "RESERVED_1"; |
Junxiao Shi | e5adbfd | 2015-06-18 14:42:06 -0700 | [diff] [blame] | 90 | } |
| 91 | else if (tlv::AppPrivateBlock1 <= type && type < 253) { |
Junxiao Shi | 78c78cc | 2015-06-19 15:53:53 -0700 | [diff] [blame] | 92 | os << "APP_TAG_1"; |
Junxiao Shi | e5adbfd | 2015-06-18 14:42:06 -0700 | [diff] [blame] | 93 | } |
| 94 | else if (253 <= type && type < tlv::AppPrivateBlock2) { |
Junxiao Shi | 78c78cc | 2015-06-19 15:53:53 -0700 | [diff] [blame] | 95 | os << "RESERVED_3"; |
Junxiao Shi | e5adbfd | 2015-06-18 14:42:06 -0700 | [diff] [blame] | 96 | } |
| 97 | else { |
Junxiao Shi | 78c78cc | 2015-06-19 15:53:53 -0700 | [diff] [blame] | 98 | os << "APP_TAG_3"; |
Junxiao Shi | e5adbfd | 2015-06-18 14:42:06 -0700 | [diff] [blame] | 99 | } |
Davide Pesavento | c070270 | 2017-08-24 22:04:00 -0400 | [diff] [blame^] | 100 | |
Junxiao Shi | 78c78cc | 2015-06-19 15:53:53 -0700 | [diff] [blame] | 101 | os << ")"; |
Junxiao Shi | e5adbfd | 2015-06-18 14:42:06 -0700 | [diff] [blame] | 102 | } |
| 103 | |
Junxiao Shi | e5adbfd | 2015-06-18 14:42:06 -0700 | [diff] [blame] | 104 | void |
Junxiao Shi | 78c78cc | 2015-06-19 15:53:53 -0700 | [diff] [blame] | 105 | NdnDissect::printBlock(std::ostream& os, const Block& block) |
Junxiao Shi | e5adbfd | 2015-06-18 14:42:06 -0700 | [diff] [blame] | 106 | { |
Junxiao Shi | 78c78cc | 2015-06-19 15:53:53 -0700 | [diff] [blame] | 107 | this->printType(os, block.type()); |
| 108 | os << " (size: " << block.value_size() << ")"; |
Junxiao Shi | e5adbfd | 2015-06-18 14:42:06 -0700 | [diff] [blame] | 109 | |
| 110 | try { |
| 111 | // if (block.type() != tlv::Content && block.type() != tlv::SignatureValue) |
| 112 | block.parse(); |
| 113 | } |
Davide Pesavento | c070270 | 2017-08-24 22:04:00 -0400 | [diff] [blame^] | 114 | catch (const tlv::Error&) { |
Junxiao Shi | e5adbfd | 2015-06-18 14:42:06 -0700 | [diff] [blame] | 115 | // 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 Shi | 78c78cc | 2015-06-19 15:53:53 -0700 | [diff] [blame] | 120 | if (block.elements().empty()) { |
| 121 | os << " [["; |
| 122 | name::Component(block.value(), block.value_size()).toUri(os); |
| 123 | os<< "]]"; |
| 124 | } |
| 125 | os << std::endl; |
Junxiao Shi | e5adbfd | 2015-06-18 14:42:06 -0700 | [diff] [blame] | 126 | |
Junxiao Shi | 78c78cc | 2015-06-19 15:53:53 -0700 | [diff] [blame] | 127 | 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 Shi | e5adbfd | 2015-06-18 14:42:06 -0700 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | void |
Junxiao Shi | 78c78cc | 2015-06-19 15:53:53 -0700 | [diff] [blame] | 135 | NdnDissect::dissect(std::ostream& os, std::istream& is) |
Junxiao Shi | e5adbfd | 2015-06-18 14:42:06 -0700 | [diff] [blame] | 136 | { |
| 137 | while (is.peek() != std::char_traits<char>::eof()) { |
| 138 | try { |
| 139 | Block block = Block::fromStream(is); |
Junxiao Shi | 78c78cc | 2015-06-19 15:53:53 -0700 | [diff] [blame] | 140 | this->printBlock(os, block); |
Junxiao Shi | e5adbfd | 2015-06-18 14:42:06 -0700 | [diff] [blame] | 141 | } |
Davide Pesavento | c070270 | 2017-08-24 22:04:00 -0400 | [diff] [blame^] | 142 | catch (const std::exception& e) { |
Junxiao Shi | e5adbfd | 2015-06-18 14:42:06 -0700 | [diff] [blame] | 143 | std::cerr << "ERROR: " << e.what() << std::endl; |
| 144 | } |
| 145 | } |
Junxiao Shi | e5adbfd | 2015-06-18 14:42:06 -0700 | [diff] [blame] | 146 | } |
| 147 | |
Junxiao Shi | 78c78cc | 2015-06-19 15:53:53 -0700 | [diff] [blame] | 148 | } // namespace dissect |
Junxiao Shi | e5adbfd | 2015-06-18 14:42:06 -0700 | [diff] [blame] | 149 | } // namespace ndn |