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-2022 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 += prependBlock(encoder, 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);
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
#define NDN_CXX_UNREACHABLE
Definition: backports.hpp:72
Represents a TLV element of the NDN packet format.
Definition: block.hpp:45
uint32_t type() const
Return the TLV-TYPE of the Block.
Definition: block.hpp:285
bool hasWire() const noexcept
Check if the Block contains a fully encoded wire representation.
Definition: block.hpp:241
element_const_iterator elements_begin() const
Equivalent to elements().begin()
Definition: block.hpp:410
void reset() noexcept
Reset the Block to a default-constructed state.
Definition: block.cpp:265
const element_container & elements() const
Get container of sub-elements.
Definition: block.hpp:402
void parse() const
Parse TLV-VALUE into sub-elements.
Definition: block.cpp:341
const Block & getKeyDigest() const
Get nested KeyDigest element.
const Name & getName() const
Get nested Name element.
KeyLocator()
Construct an empty KeyLocator.
const Block & wireEncode() const
Definition: key-locator.cpp:74
uint32_t getType() const
void wireDecode(const Block &wire)
Decode from wire encoding.
Definition: key-locator.cpp:90
KeyLocator & setKeyDigest(const Block &keyDigest)
Set nested KeyDigest element (whole TLV).
KeyLocator & clear()
Reset KeyLocator to its default-constructed state.
KeyLocator & setName(const Name &name)
Set nested Name element.
Represents an absolute name.
Definition: name.hpp:46
#define NDN_CXX_DEFINE_WIRE_ENCODE_INSTANTIATIONS(ClassName)
#define NDN_THROW(e)
Definition: exception.hpp:61
EncodingImpl< EstimatorTag > EncodingEstimator
Block makeBinaryBlock(uint32_t type, span< const uint8_t > value)
Create a TLV block copying the TLV-VALUE from a byte range.
EncodingImpl< EncoderTag > EncodingBuffer
size_t prependBlock(EncodingImpl< TAG > &encoder, const Block &block)
Prepend a TLV element.
std::string to_string(const errinfo_stacktrace &x)
Definition: exception.cpp:31
@ Name
Definition: tlv.hpp:67
@ KeyDigest
Definition: tlv.hpp:89
@ Invalid
Definition: tlv.hpp:64
@ KeyLocator
Definition: tlv.hpp:88
Definition: data.cpp:25
void printHex(std::ostream &os, uint64_t num, bool wantUpperCase)
Output the hex representation of num to the output stream os.
shared_ptr< const Buffer > ConstBufferPtr
Definition: buffer.hpp:139
const size_t MAX_KEY_DIGEST_OCTETS_TO_SHOW
Definition: key-locator.cpp:36
constexpr detail::make_overload_t overload
Definition: overload.hpp:108
std::ostream & operator<<(std::ostream &os, const Data &data)
Definition: data.cpp:376