blob: 90aa71c464d9c946b2e45d65f653bbd1e0b688f9 [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 Afanasyevc169a812014-05-20 20:37:29 -04003 * Copyright (c) 2013-2014 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"},
41 {tlv::Scope , "Scope"},
42 {tlv::InterestLifetime , "InterestLifetime"},
43 {tlv::MinSuffixComponents , "MinSuffixComponents"},
44 {tlv::MaxSuffixComponents , "MaxSuffixComponents"},
45 {tlv::PublisherPublicKeyLocator , "PublisherPublicKeyLocator"},
46 {tlv::Exclude , "Exclude"},
47 {tlv::ChildSelector , "ChildSelector"},
48 {tlv::MustBeFresh , "MustBeFresh"},
49 {tlv::Any , "Any"},
50 {tlv::MetaInfo , "MetaInfo"},
51 {tlv::Content , "Content"},
52 {tlv::SignatureInfo , "SignatureInfo"},
53 {tlv::SignatureValue , "SignatureValue"},
54 {tlv::ContentType , "ContentType"},
55 {tlv::FreshnessPeriod , "FreshnessPeriod"},
56 {tlv::FinalBlockId , "FinalBlockId"},
57 {tlv::SignatureType , "SignatureType"},
58 {tlv::KeyLocator , "KeyLocator"},
59 {tlv::KeyDigest , "KeyDigest"},
Alexander Afanasyev303b3502014-01-07 12:02:59 -080060};
61
62void
63printTypeInfo(uint32_t type)
64{
65 std::cout << type << " (";
66
Alexander Afanasyev4cd8d732014-12-30 13:16:03 -080067 if (TLV_DICT.count(type) != 0) {
Alexander Afanasyev303b3502014-01-07 12:02:59 -080068 std::cout << TLV_DICT[type];
69 }
Alexander Afanasyev4cd8d732014-12-30 13:16:03 -080070 else if (type < tlv::AppPrivateBlock1) {
Alexander Afanasyev303b3502014-01-07 12:02:59 -080071 std::cout << "RESERVED_1";
72 }
Alexander Afanasyev6486d522014-10-23 14:14:11 -070073 else if (tlv::AppPrivateBlock1 <= type && type < 253) {
Alexander Afanasyev303b3502014-01-07 12:02:59 -080074 std::cout << "APP_TAG_1";
75 }
Alexander Afanasyev6486d522014-10-23 14:14:11 -070076 else if (253 <= type && type < tlv::AppPrivateBlock2) {
Alexander Afanasyev303b3502014-01-07 12:02:59 -080077 std::cout << "RESERVED_3";
78 }
79 else {
80 std::cout << "APP_TAG_3";
81 }
82 std::cout << ")";
83}
84
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -070085
Alexander Afanasyev303b3502014-01-07 12:02:59 -080086void
Alexander Afanasyev6486d522014-10-23 14:14:11 -070087BlockPrinter(const Block& block, const std::string& indent = "")
Alexander Afanasyev303b3502014-01-07 12:02:59 -080088{
89 std::cout << indent;
90 printTypeInfo(block.type());
91 std::cout << " (size: " << block.value_size() << ")";
92
93 try {
Alexander Afanasyev6486d522014-10-23 14:14:11 -070094 // if (block.type() != tlv::Content && block.type() != tlv::SignatureValue)
Alexander Afanasyev303b3502014-01-07 12:02:59 -080095 block.parse();
96 }
Alexander Afanasyev6486d522014-10-23 14:14:11 -070097 catch (tlv::Error& e) {
Alexander Afanasyev303b3502014-01-07 12:02:59 -080098 // pass (e.g., leaf block reached)
99
100 // @todo: Figure how to deterministically figure out that value is not recursive TLV block
101 }
102
Alexander Afanasyev29e5c3d2014-02-11 00:01:10 -0800103 if (block.elements().empty())
Alexander Afanasyev303b3502014-01-07 12:02:59 -0800104 {
105 std::cout << " [[";
Alexander Afanasyev6486d522014-10-23 14:14:11 -0700106 name::Component(block.value(), block.value_size()).toUri(std::cout);
Alexander Afanasyev303b3502014-01-07 12:02:59 -0800107 std::cout<< "]]";
108 }
109 std::cout << std::endl;
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -0700110
Alexander Afanasyev6486d522014-10-23 14:14:11 -0700111 for (Block::element_const_iterator i = block.elements_begin();
Yingdi Yu27b0e392014-05-05 16:27:02 -0700112 i != block.elements_end();
113 ++i)
Alexander Afanasyev303b3502014-01-07 12:02:59 -0800114 {
115 BlockPrinter(*i, indent+" ");
116 }
117}
118
119void
Alexander Afanasyev6486d522014-10-23 14:14:11 -0700120HexPrinter(const Block& block, const std::string& indent = "")
Alexander Afanasyev303b3502014-01-07 12:02:59 -0800121{
122 std::cout << indent;
Alexander Afanasyev6486d522014-10-23 14:14:11 -0700123 for (Buffer::const_iterator i = block.begin (); i != block.value_begin(); ++i)
Alexander Afanasyev303b3502014-01-07 12:02:59 -0800124 {
Yingdi Yu27b0e392014-05-05 16:27:02 -0700125 std::cout << "0x";
126 std::cout << std::noshowbase << std::hex << std::setw(2) <<
127 std::setfill('0') << static_cast<int>(*i);
Alexander Afanasyev303b3502014-01-07 12:02:59 -0800128 std::cout << ", ";
129 }
130 std::cout << "\n";
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -0700131
Alexander Afanasyev29e5c3d2014-02-11 00:01:10 -0800132 if (block.elements_size() == 0 && block.value_size() > 0)
Alexander Afanasyev303b3502014-01-07 12:02:59 -0800133 {
134 std::cout << indent << " ";
Alexander Afanasyev6486d522014-10-23 14:14:11 -0700135 for (Buffer::const_iterator i = block.value_begin (); i != block.value_end(); ++i)
Alexander Afanasyev303b3502014-01-07 12:02:59 -0800136 {
Yingdi Yu27b0e392014-05-05 16:27:02 -0700137 std::cout << "0x";
138 std::cout << std::noshowbase << std::hex << std::setw(2) <<
139 std::setfill('0') << static_cast<int>(*i);
Alexander Afanasyev303b3502014-01-07 12:02:59 -0800140 std::cout << ", ";
141 }
142 std::cout << "\n";
143 }
144 else
145 {
Alexander Afanasyev6486d522014-10-23 14:14:11 -0700146 for (Block::element_const_iterator i = block.elements_begin();
Yingdi Yu27b0e392014-05-05 16:27:02 -0700147 i != block.elements_end();
148 ++i)
Alexander Afanasyev303b3502014-01-07 12:02:59 -0800149 {
150 HexPrinter(*i, indent+" ");
151 }
152 }
153}
154
Yingdi Yu27b0e392014-05-05 16:27:02 -0700155void
156parseBlocksFromStream(std::istream& is)
157{
158 while (is.peek() != std::char_traits<char>::eof()) {
159 try {
Alexander Afanasyev6486d522014-10-23 14:14:11 -0700160 Block block = Block::fromStream(is);
Yingdi Yu27b0e392014-05-05 16:27:02 -0700161 BlockPrinter(block, "");
162 // HexPrinter(block, "");
163 }
164 catch (std::exception& e) {
165 std::cerr << "ERROR: " << e.what() << std::endl;
166 }
167 }
168
169}
170
Alexander Afanasyev6486d522014-10-23 14:14:11 -0700171} // namespace ndn
172
Alexander Afanasyev303b3502014-01-07 12:02:59 -0800173int main(int argc, const char *argv[])
174{
Alexander Afanasyev303b3502014-01-07 12:02:59 -0800175 if (argc == 1 ||
176 (argc == 2 && std::string(argv[1]) == "-"))
177 {
Alexander Afanasyev6486d522014-10-23 14:14:11 -0700178 ndn::parseBlocksFromStream(std::cin);
Alexander Afanasyev303b3502014-01-07 12:02:59 -0800179 }
180 else
181 {
182 std::ifstream file(argv[1]);
Alexander Afanasyev6486d522014-10-23 14:14:11 -0700183 ndn::parseBlocksFromStream(file);
Alexander Afanasyev303b3502014-01-07 12:02:59 -0800184 }
185
Alexander Afanasyev303b3502014-01-07 12:02:59 -0800186 return 0;
187}