blob: ee5088c083734979baaad51c03c61544a6407860 [file] [log] [blame]
Alexander Afanasyevc169a812014-05-20 20:37:29 -04001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Alexander Afanasyev303b3502014-01-07 12:02:59 -08002/**
Alexander Afanasyev73e30042015-09-17 17:09:51 -07003 * Copyright (c) 2013-2015 Regents of the University of California.
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07004 *
5 * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07006 *
Alexander Afanasyevc169a812014-05-20 20:37:29 -04007 * ndn-cxx library is free software: you can redistribute it and/or modify it under the
8 * terms of the GNU Lesser General Public License as published by the Free Software
9 * Foundation, either version 3 of the License, or (at your option) any later version.
10 *
11 * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY
12 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
13 * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
14 *
15 * You should have received copies of the GNU General Public License and GNU Lesser
16 * General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see
17 * <http://www.gnu.org/licenses/>.
18 *
19 * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -070020 *
21 * @author Alexander Afanasyev <http://lasr.cs.ucla.edu/afanasyev/index.html>
Alexander Afanasyev303b3502014-01-07 12:02:59 -080022 */
23
Alexander Afanasyev09c613f2014-01-29 00:23:58 -080024#include "face.hpp"
25#include "encoding/block.hpp"
Alexander Afanasyev303b3502014-01-07 12:02:59 -080026
27#include <iomanip>
28#include <fstream>
Alexander Afanasyev4cd8d732014-12-30 13:16:03 -080029#include <map>
Alexander Afanasyev303b3502014-01-07 12:02:59 -080030
Alexander Afanasyev6486d522014-10-23 14:14:11 -070031namespace ndn {
Alexander Afanasyev303b3502014-01-07 12:02:59 -080032
Alexander Afanasyev4cd8d732014-12-30 13:16:03 -080033std::map<uint32_t, std::string> TLV_DICT = {
34 {tlv::Interest , "Interest"},
35 {tlv::Data , "Data"},
36 {tlv::Name , "Name"},
37 {tlv::NameComponent , "NameComponent"},
38 {tlv::ImplicitSha256DigestComponent, "ImplicitSha256DigestComponent"},
39 {tlv::Selectors , "Selectors"},
40 {tlv::Nonce , "Nonce"},
Alexander Afanasyev4cd8d732014-12-30 13:16:03 -080041 {tlv::InterestLifetime , "InterestLifetime"},
42 {tlv::MinSuffixComponents , "MinSuffixComponents"},
43 {tlv::MaxSuffixComponents , "MaxSuffixComponents"},
44 {tlv::PublisherPublicKeyLocator , "PublisherPublicKeyLocator"},
45 {tlv::Exclude , "Exclude"},
46 {tlv::ChildSelector , "ChildSelector"},
47 {tlv::MustBeFresh , "MustBeFresh"},
48 {tlv::Any , "Any"},
49 {tlv::MetaInfo , "MetaInfo"},
50 {tlv::Content , "Content"},
51 {tlv::SignatureInfo , "SignatureInfo"},
52 {tlv::SignatureValue , "SignatureValue"},
53 {tlv::ContentType , "ContentType"},
54 {tlv::FreshnessPeriod , "FreshnessPeriod"},
55 {tlv::FinalBlockId , "FinalBlockId"},
56 {tlv::SignatureType , "SignatureType"},
57 {tlv::KeyLocator , "KeyLocator"},
58 {tlv::KeyDigest , "KeyDigest"},
Alexander Afanasyev303b3502014-01-07 12:02:59 -080059};
60
61void
62printTypeInfo(uint32_t type)
63{
64 std::cout << type << " (";
65
Alexander Afanasyev4cd8d732014-12-30 13:16:03 -080066 if (TLV_DICT.count(type) != 0) {
Alexander Afanasyev303b3502014-01-07 12:02:59 -080067 std::cout << TLV_DICT[type];
68 }
Alexander Afanasyev4cd8d732014-12-30 13:16:03 -080069 else if (type < tlv::AppPrivateBlock1) {
Alexander Afanasyev303b3502014-01-07 12:02:59 -080070 std::cout << "RESERVED_1";
71 }
Alexander Afanasyev6486d522014-10-23 14:14:11 -070072 else if (tlv::AppPrivateBlock1 <= type && type < 253) {
Alexander Afanasyev303b3502014-01-07 12:02:59 -080073 std::cout << "APP_TAG_1";
74 }
Alexander Afanasyev6486d522014-10-23 14:14:11 -070075 else if (253 <= type && type < tlv::AppPrivateBlock2) {
Alexander Afanasyev303b3502014-01-07 12:02:59 -080076 std::cout << "RESERVED_3";
77 }
78 else {
79 std::cout << "APP_TAG_3";
80 }
81 std::cout << ")";
82}
83
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -070084
Alexander Afanasyev303b3502014-01-07 12:02:59 -080085void
Alexander Afanasyev6486d522014-10-23 14:14:11 -070086BlockPrinter(const Block& block, const std::string& indent = "")
Alexander Afanasyev303b3502014-01-07 12:02:59 -080087{
88 std::cout << indent;
89 printTypeInfo(block.type());
90 std::cout << " (size: " << block.value_size() << ")";
91
92 try {
Alexander Afanasyev6486d522014-10-23 14:14:11 -070093 // if (block.type() != tlv::Content && block.type() != tlv::SignatureValue)
Alexander Afanasyev303b3502014-01-07 12:02:59 -080094 block.parse();
95 }
Alexander Afanasyev6486d522014-10-23 14:14:11 -070096 catch (tlv::Error& e) {
Alexander Afanasyev303b3502014-01-07 12:02:59 -080097 // pass (e.g., leaf block reached)
98
99 // @todo: Figure how to deterministically figure out that value is not recursive TLV block
100 }
101
Alexander Afanasyev29e5c3d2014-02-11 00:01:10 -0800102 if (block.elements().empty())
Alexander Afanasyev303b3502014-01-07 12:02:59 -0800103 {
104 std::cout << " [[";
Alexander Afanasyev6486d522014-10-23 14:14:11 -0700105 name::Component(block.value(), block.value_size()).toUri(std::cout);
Alexander Afanasyev303b3502014-01-07 12:02:59 -0800106 std::cout<< "]]";
107 }
108 std::cout << std::endl;
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -0700109
Alexander Afanasyev6486d522014-10-23 14:14:11 -0700110 for (Block::element_const_iterator i = block.elements_begin();
Yingdi Yu27b0e392014-05-05 16:27:02 -0700111 i != block.elements_end();
112 ++i)
Alexander Afanasyev303b3502014-01-07 12:02:59 -0800113 {
114 BlockPrinter(*i, indent+" ");
115 }
116}
117
118void
Alexander Afanasyev6486d522014-10-23 14:14:11 -0700119HexPrinter(const Block& block, const std::string& indent = "")
Alexander Afanasyev303b3502014-01-07 12:02:59 -0800120{
121 std::cout << indent;
Alexander Afanasyev6486d522014-10-23 14:14:11 -0700122 for (Buffer::const_iterator i = block.begin (); i != block.value_begin(); ++i)
Alexander Afanasyev303b3502014-01-07 12:02:59 -0800123 {
Yingdi Yu27b0e392014-05-05 16:27:02 -0700124 std::cout << "0x";
125 std::cout << std::noshowbase << std::hex << std::setw(2) <<
126 std::setfill('0') << static_cast<int>(*i);
Alexander Afanasyev303b3502014-01-07 12:02:59 -0800127 std::cout << ", ";
128 }
129 std::cout << "\n";
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -0700130
Alexander Afanasyev29e5c3d2014-02-11 00:01:10 -0800131 if (block.elements_size() == 0 && block.value_size() > 0)
Alexander Afanasyev303b3502014-01-07 12:02:59 -0800132 {
133 std::cout << indent << " ";
Alexander Afanasyev6486d522014-10-23 14:14:11 -0700134 for (Buffer::const_iterator i = block.value_begin (); i != block.value_end(); ++i)
Alexander Afanasyev303b3502014-01-07 12:02:59 -0800135 {
Yingdi Yu27b0e392014-05-05 16:27:02 -0700136 std::cout << "0x";
137 std::cout << std::noshowbase << std::hex << std::setw(2) <<
138 std::setfill('0') << static_cast<int>(*i);
Alexander Afanasyev303b3502014-01-07 12:02:59 -0800139 std::cout << ", ";
140 }
141 std::cout << "\n";
142 }
143 else
144 {
Alexander Afanasyev6486d522014-10-23 14:14:11 -0700145 for (Block::element_const_iterator i = block.elements_begin();
Yingdi Yu27b0e392014-05-05 16:27:02 -0700146 i != block.elements_end();
147 ++i)
Alexander Afanasyev303b3502014-01-07 12:02:59 -0800148 {
149 HexPrinter(*i, indent+" ");
150 }
151 }
152}
153
Yingdi Yu27b0e392014-05-05 16:27:02 -0700154void
155parseBlocksFromStream(std::istream& is)
156{
157 while (is.peek() != std::char_traits<char>::eof()) {
158 try {
Alexander Afanasyev6486d522014-10-23 14:14:11 -0700159 Block block = Block::fromStream(is);
Yingdi Yu27b0e392014-05-05 16:27:02 -0700160 BlockPrinter(block, "");
161 // HexPrinter(block, "");
162 }
163 catch (std::exception& e) {
164 std::cerr << "ERROR: " << e.what() << std::endl;
165 }
166 }
167
168}
169
Alexander Afanasyev6486d522014-10-23 14:14:11 -0700170} // namespace ndn
171
Alexander Afanasyev303b3502014-01-07 12:02:59 -0800172int main(int argc, const char *argv[])
173{
Junxiao Shif8f63da2015-10-07 22:28:56 +0000174 std::cerr << "tlvdump is deprecated. Use ndn-dissect program from ndn-tools repository.\n"
175 "See `man tlvdump` for details." << std::endl;
176
Alexander Afanasyev303b3502014-01-07 12:02:59 -0800177 if (argc == 1 ||
178 (argc == 2 && std::string(argv[1]) == "-"))
179 {
Alexander Afanasyev6486d522014-10-23 14:14:11 -0700180 ndn::parseBlocksFromStream(std::cin);
Alexander Afanasyev303b3502014-01-07 12:02:59 -0800181 }
182 else
183 {
184 std::ifstream file(argv[1]);
Alexander Afanasyev6486d522014-10-23 14:14:11 -0700185 ndn::parseBlocksFromStream(file);
Alexander Afanasyev303b3502014-01-07 12:02:59 -0800186 }
187
Alexander Afanasyev303b3502014-01-07 12:02:59 -0800188 return 0;
189}