Junxiao Shi | e5adbfd | 2015-06-18 14:42:06 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Junxiao Shi | 78c78cc | 2015-06-19 15:53:53 -0700 | [diff] [blame^] | 3 | * 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 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 | #include <map> |
| 45 | |
Junxiao Shi | 78c78cc | 2015-06-19 15:53:53 -0700 | [diff] [blame^] | 46 | #include <ndn-cxx/name-component.hpp> |
| 47 | #include <ndn-cxx/util/indented-stream.hpp> |
| 48 | |
Junxiao Shi | e5adbfd | 2015-06-18 14:42:06 -0700 | [diff] [blame] | 49 | namespace ndn { |
Junxiao Shi | 78c78cc | 2015-06-19 15:53:53 -0700 | [diff] [blame^] | 50 | namespace dissect { |
Junxiao Shi | e5adbfd | 2015-06-18 14:42:06 -0700 | [diff] [blame] | 51 | |
| 52 | std::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 | |
| 80 | void |
Junxiao Shi | 78c78cc | 2015-06-19 15:53:53 -0700 | [diff] [blame^] | 81 | NdnDissect::printType(std::ostream& os, uint32_t type) |
Junxiao Shi | e5adbfd | 2015-06-18 14:42:06 -0700 | [diff] [blame] | 82 | { |
Junxiao Shi | 78c78cc | 2015-06-19 15:53:53 -0700 | [diff] [blame^] | 83 | os << type << " ("; |
Junxiao Shi | e5adbfd | 2015-06-18 14:42:06 -0700 | [diff] [blame] | 84 | |
| 85 | if (TLV_DICT.count(type) != 0) { |
Junxiao Shi | 78c78cc | 2015-06-19 15:53:53 -0700 | [diff] [blame^] | 86 | os << TLV_DICT[type]; |
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 | } |
Junxiao Shi | 78c78cc | 2015-06-19 15:53:53 -0700 | [diff] [blame^] | 100 | os << ")"; |
Junxiao Shi | e5adbfd | 2015-06-18 14:42:06 -0700 | [diff] [blame] | 101 | } |
| 102 | |
Junxiao Shi | e5adbfd | 2015-06-18 14:42:06 -0700 | [diff] [blame] | 103 | void |
Junxiao Shi | 78c78cc | 2015-06-19 15:53:53 -0700 | [diff] [blame^] | 104 | NdnDissect::printBlock(std::ostream& os, const Block& block) |
Junxiao Shi | e5adbfd | 2015-06-18 14:42:06 -0700 | [diff] [blame] | 105 | { |
Junxiao Shi | 78c78cc | 2015-06-19 15:53:53 -0700 | [diff] [blame^] | 106 | this->printType(os, block.type()); |
| 107 | os << " (size: " << block.value_size() << ")"; |
Junxiao Shi | e5adbfd | 2015-06-18 14:42:06 -0700 | [diff] [blame] | 108 | |
| 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 Shi | 78c78cc | 2015-06-19 15:53:53 -0700 | [diff] [blame^] | 119 | if (block.elements().empty()) { |
| 120 | os << " [["; |
| 121 | name::Component(block.value(), block.value_size()).toUri(os); |
| 122 | os<< "]]"; |
| 123 | } |
| 124 | os << std::endl; |
Junxiao Shi | e5adbfd | 2015-06-18 14:42:06 -0700 | [diff] [blame] | 125 | |
Junxiao Shi | 78c78cc | 2015-06-19 15:53:53 -0700 | [diff] [blame^] | 126 | 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 Shi | e5adbfd | 2015-06-18 14:42:06 -0700 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | void |
Junxiao Shi | 78c78cc | 2015-06-19 15:53:53 -0700 | [diff] [blame^] | 134 | NdnDissect::dissect(std::ostream& os, std::istream& is) |
Junxiao Shi | e5adbfd | 2015-06-18 14:42:06 -0700 | [diff] [blame] | 135 | { |
| 136 | while (is.peek() != std::char_traits<char>::eof()) { |
| 137 | try { |
| 138 | Block block = Block::fromStream(is); |
Junxiao Shi | 78c78cc | 2015-06-19 15:53:53 -0700 | [diff] [blame^] | 139 | this->printBlock(os, block); |
Junxiao Shi | e5adbfd | 2015-06-18 14:42:06 -0700 | [diff] [blame] | 140 | } |
| 141 | catch (std::exception& e) { |
| 142 | std::cerr << "ERROR: " << e.what() << std::endl; |
| 143 | } |
| 144 | } |
Junxiao Shi | e5adbfd | 2015-06-18 14:42:06 -0700 | [diff] [blame] | 145 | } |
| 146 | |
Junxiao Shi | 78c78cc | 2015-06-19 15:53:53 -0700 | [diff] [blame^] | 147 | } // namespace dissect |
Junxiao Shi | e5adbfd | 2015-06-18 14:42:06 -0700 | [diff] [blame] | 148 | } // namespace ndn |