NS-3 based Named Data Networking (NDN) simulator
ndnSIM 2.5: NDN, CCN, CCNx, content centric networks
API Documentation
key-locator.cpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2013-2019 Regents of the University of California.
4  *
5  * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
6  *
7  * 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.
20  */
21 
22 #include "ndn-cxx/key-locator.hpp"
26 
27 namespace ndn {
28 
29 BOOST_CONCEPT_ASSERT((boost::EqualityComparable<KeyLocator>));
30 BOOST_CONCEPT_ASSERT((WireEncodable<KeyLocator>));
31 BOOST_CONCEPT_ASSERT((WireEncodableWithEncodingBuffer<KeyLocator>));
32 BOOST_CONCEPT_ASSERT((WireDecodable<KeyLocator>));
33 static_assert(std::is_base_of<tlv::Error, KeyLocator::Error>::value,
34  "KeyLocator::Error must inherit from tlv::Error");
35 
37 
38 KeyLocator::KeyLocator() = default;
39 
41 {
42  wireDecode(wire);
43 }
44 
46  : m_locator(name)
47 {
48 }
49 
50 template<encoding::Tag TAG>
51 size_t
53 {
54  // KeyLocator = KEY-LOCATOR-TYPE TLV-LENGTH (Name / KeyDigest)
55  // KeyDigest = KEY-DIGEST-TYPE TLV-LENGTH *OCTET
56 
57  size_t totalLength = 0;
58 
59  auto visitor = overload(
60  [] (monostate) {}, // nothing to encode, TLV-VALUE is empty
61  [&] (const Name& name) { totalLength += name.wireEncode(encoder); },
62  [&] (const Block& digest) { totalLength += encoder.prependBlock(digest); },
63  [] (uint32_t type) { NDN_THROW(Error("Unsupported KeyLocator type " + to_string(type))); });
64  visit(visitor, m_locator);
65 
66  totalLength += encoder.prependVarNumber(totalLength);
67  totalLength += encoder.prependVarNumber(tlv::KeyLocator);
68  return totalLength;
69 }
70 
72 
73 const Block&
75 {
76  if (m_wire.hasWire())
77  return m_wire;
78 
79  EncodingEstimator estimator;
80  size_t estimatedSize = wireEncode(estimator);
81 
82  EncodingBuffer buffer(estimatedSize, 0);
83  wireEncode(buffer);
84 
85  m_wire = buffer.block();
86  return m_wire;
87 }
88 
89 void
91 {
92  if (wire.type() != tlv::KeyLocator)
93  NDN_THROW(Error("KeyLocator", wire.type()));
94 
95  clear();
96  m_wire = wire;
97  m_wire.parse();
98 
99  auto element = m_wire.elements_begin();
100  if (element == m_wire.elements().end()) {
101  return;
102  }
103 
104  switch (element->type()) {
105  case tlv::Name:
106  m_locator.emplace<Name>(*element);
107  break;
108  case tlv::KeyDigest:
109  m_locator.emplace<Block>(*element);
110  break;
111  default:
112  m_locator = element->type();
113  break;
114  }
115 }
116 
117 uint32_t
119 {
120  switch (m_locator.index()) {
121  case 0:
122  return tlv::Invalid;
123  case 1:
124  return tlv::Name;
125  case 2:
126  return tlv::KeyDigest;
127  case 3:
128  return get<uint32_t>(m_locator);
129  default:
131  }
132 }
133 
134 KeyLocator&
136 {
137  m_locator = monostate{};
138  m_wire.reset();
139  return *this;
140 }
141 
142 const Name&
144 {
145  try {
146  return get<Name>(m_locator);
147  }
148  catch (const bad_variant_access&) {
149  NDN_THROW(Error("KeyLocator does not contain a Name"));
150  }
151 }
152 
153 KeyLocator&
155 {
156  m_locator = name;
157  m_wire.reset();
158  return *this;
159 }
160 
161 const Block&
163 {
164  try {
165  return get<Block>(m_locator);
166  }
167  catch (const bad_variant_access&) {
168  NDN_THROW(Error("KeyLocator does not contain a KeyDigest"));
169  }
170 }
171 
172 KeyLocator&
174 {
175  if (keyDigest.type() != tlv::KeyDigest) {
176  NDN_THROW(std::invalid_argument("Invalid KeyDigest block of type " + to_string(keyDigest.type())));
177  }
178  m_locator = keyDigest;
179  m_wire.reset();
180  return *this;
181 }
182 
183 KeyLocator&
185 {
186  BOOST_ASSERT(keyDigest != nullptr);
187  m_locator = makeBinaryBlock(tlv::KeyDigest, keyDigest->data(), keyDigest->size());
188  m_wire.reset();
189  return *this;
190 }
191 
192 std::ostream&
193 operator<<(std::ostream& os, const KeyLocator& keyLocator)
194 {
195  auto visitor = overload(
196  [&] (monostate) {
197  os << "None";
198  },
199  [&] (const Name& name) {
200  os << "Name=" << name;
201  },
202  [&] (const Block& digest) {
203  os << "KeyDigest=";
204  printHex(os, digest.value(), std::min(digest.value_size(), MAX_KEY_DIGEST_OCTETS_TO_SHOW));
205  if (digest.value_size() > MAX_KEY_DIGEST_OCTETS_TO_SHOW) {
206  os << "...";
207  }
208  },
209  [&] (uint32_t type) {
210  os << "Unknown(" << type << ")";
211  });
212  visit(visitor, keyLocator.m_locator);
213  return os;
214 }
215 
216 } // namespace ndn
ndn::Block::elements
const element_container & elements() const
Get container of sub-elements.
Definition: block.hpp:391
ndn::KeyLocator::clear
KeyLocator & clear()
Reset KeyLocator to its default-constructed state.
Definition: key-locator.cpp:135
string-helper.hpp
ndn::KeyLocator::wireEncode
const Block & wireEncode() const
Definition: key-locator.cpp:74
ndn::Block::elements_begin
element_const_iterator elements_begin() const
Equivalent to elements().begin()
Definition: block.hpp:399
ndn::tlv::KeyDigest
@ KeyDigest
Definition: tlv.hpp:87
ndn::KeyLocator::getKeyDigest
const Block & getKeyDigest() const
Get nested KeyDigest element.
Definition: key-locator.cpp:162
ndn::KeyLocator::getType
uint32_t getType() const
Definition: key-locator.cpp:118
NDN_CXX_UNREACHABLE
#define NDN_CXX_UNREACHABLE
Definition: backports.hpp:72
ndn::Block::reset
void reset() noexcept
Reset the Block to a default-constructed state.
Definition: block.cpp:250
ndn::KeyLocator::setKeyDigest
KeyLocator & setKeyDigest(const Block &keyDigest)
Set nested KeyDigest element (whole TLV).
Definition: key-locator.cpp:173
ndn::WireDecodable
a concept check for TLV abstraction with .wireDecode method and constructible from Block
Definition: concepts.hpp:81
ndn::encoding::EncodingEstimator
EncodingImpl< EstimatorTag > EncodingEstimator
Definition: encoding-buffer-fwd.hpp:39
ndn::Name
Represents an absolute name.
Definition: name.hpp:44
ndn::KeyLocator::KeyLocator
KeyLocator()
Construct an empty KeyLocator.
ndn::KeyLocator::setName
KeyLocator & setName(const Name &name)
Set nested Name element.
Definition: key-locator.cpp:154
ndn::Block::type
uint32_t type() const
Return the TLV-TYPE of the Block.
Definition: block.hpp:274
ndn::KeyLocator::wireDecode
void wireDecode(const Block &wire)
Decode from wire encoding.
Definition: key-locator.cpp:90
ndn::WireEncodable
a concept check for TLV abstraction with .wireEncode method
Definition: concepts.hpp:45
nonstd::variants::visit
R visit(const Visitor &v, V1 const &arg1)
Definition: variant.hpp:2194
ndn::overload
constexpr detail::make_overload_t overload
Definition: overload.hpp:108
ndn::printHex
void printHex(std::ostream &os, uint64_t num, bool wantUpperCase)
Output the hex representation of num to the output stream os.
Definition: string-helper.cpp:42
NDN_THROW
#define NDN_THROW(e)
Definition: exception.hpp:61
ndn::KeyLocator::Error
Definition: key-locator.hpp:33
ndn::tlv::Name
@ Name
Definition: tlv.hpp:67
ndn::Block::parse
void parse() const
Parse TLV-VALUE into sub-elements.
Definition: block.cpp:325
ndn::NDN_CXX_DEFINE_WIRE_ENCODE_INSTANTIATIONS
NDN_CXX_DEFINE_WIRE_ENCODE_INSTANTIATIONS(Interest)
ndn::encoding::EncodingImpl
Definition: encoding-buffer-fwd.hpp:36
block-helpers.hpp
ndn::tlv::KeyLocator
@ KeyLocator
Definition: tlv.hpp:86
ndn::Block
Represents a TLV element of NDN packet format.
Definition: block.hpp:43
ndn::to_string
std::string to_string(const T &val)
Definition: backports.hpp:102
ndn::operator<<
std::ostream & operator<<(std::ostream &os, const Data &data)
Definition: data.cpp:322
ndn::name
Definition: name-component-types.hpp:33
ndn::Block::hasWire
bool hasWire() const noexcept
Check if the Block contains a fully encoded wire representation.
Definition: block.hpp:230
ndn::KeyLocator::getName
const Name & getName() const
Get nested Name element.
Definition: key-locator.cpp:143
ndn::MAX_KEY_DIGEST_OCTETS_TO_SHOW
const size_t MAX_KEY_DIGEST_OCTETS_TO_SHOW
Definition: key-locator.cpp:36
overload.hpp
ndn::tlv::Invalid
@ Invalid
Definition: tlv.hpp:64
ndn::KeyLocator
Definition: key-locator.hpp:30
key-locator.hpp
ndn::encoding::makeBinaryBlock
Block makeBinaryBlock(uint32_t type, const uint8_t *value, size_t length)
Create a TLV block copying TLV-VALUE from raw buffer.
Definition: block-helpers.cpp:181
ndn::ConstBufferPtr
shared_ptr< const Buffer > ConstBufferPtr
Definition: buffer.hpp:126
ndn::WireEncodableWithEncodingBuffer
a concept check for TLV abstraction with .wireEncode method
Definition: concepts.hpp:61
ndn
Copyright (c) 2011-2015 Regents of the University of California.
Definition: ndn-strategy-choice-helper.hpp:34
ndn::encoding::EncodingBuffer
EncodingImpl< EncoderTag > EncodingBuffer
Definition: encoding-buffer-fwd.hpp:38