key-locator.cpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
22 #include "key-locator.hpp"
24 #include "util/string-helper.hpp"
25 
26 namespace ndn {
27 
28 BOOST_CONCEPT_ASSERT((boost::EqualityComparable<KeyLocator>));
29 BOOST_CONCEPT_ASSERT((WireEncodable<KeyLocator>));
31 BOOST_CONCEPT_ASSERT((WireDecodable<KeyLocator>));
32 static_assert(std::is_base_of<tlv::Error, KeyLocator::Error>::value,
33  "KeyLocator::Error must inherit from tlv::Error");
34 
36  : m_type(KeyLocator_None)
37 {
38 }
39 
41 {
42  wireDecode(wire);
43 }
44 
46 {
47  setName(name);
48 }
49 
50 template<encoding::Tag TAG>
51 size_t
52 KeyLocator::wireEncode(EncodingImpl<TAG>& encoder) const
53 {
54  // KeyLocator ::= KEY-LOCATOR-TYPE TLV-LENGTH (Name | KeyDigest)
55  // KeyDigest ::= KEY-DIGEST-TYPE TLV-LENGTH BYTE+
56 
57  size_t totalLength = 0;
58 
59  switch (m_type) {
60  case KeyLocator_None:
61  break;
62  case KeyLocator_Name:
63  totalLength += m_name.wireEncode(encoder);
64  break;
66  totalLength += encoder.prependBlock(m_keyDigest);
67  break;
68  default:
69  BOOST_THROW_EXCEPTION(Error("Unsupported KeyLocator type"));
70  }
71 
72  totalLength += encoder.prependVarNumber(totalLength);
73  totalLength += encoder.prependVarNumber(tlv::KeyLocator);
74  return totalLength;
75 }
76 
77 template size_t
78 KeyLocator::wireEncode<encoding::EncoderTag>(EncodingImpl<encoding::EncoderTag>& encoder) const;
79 
80 template size_t
81 KeyLocator::wireEncode<encoding::EstimatorTag>(EncodingImpl<encoding::EstimatorTag>& encoder) const;
82 
83 const Block&
85 {
86  if (m_wire.hasWire())
87  return m_wire;
88 
89  EncodingEstimator estimator;
90  size_t estimatedSize = wireEncode(estimator);
91 
92  EncodingBuffer buffer(estimatedSize, 0);
93  wireEncode(buffer);
94 
95  m_wire = buffer.block();
96  return m_wire;
97 }
98 
99 void
101 {
102  if (wire.type() != tlv::KeyLocator)
103  BOOST_THROW_EXCEPTION(Error("Unexpected TLV type during KeyLocator decoding"));
104 
105  m_wire = wire;
106  m_wire.parse();
107 
108  if (m_wire.elements().empty()) {
109  m_type = KeyLocator_None;
110  return;
111  }
112 
113  switch (m_wire.elements_begin()->type()) {
114  case tlv::Name:
115  m_type = KeyLocator_Name;
116  m_name.wireDecode(*m_wire.elements_begin());
117  break;
118  case tlv::KeyDigest:
119  m_type = KeyLocator_KeyDigest;
120  m_keyDigest = *m_wire.elements_begin();
121  break;
122  default:
123  m_type = KeyLocator_Unknown;
124  break;
125  }
126 }
127 
128 KeyLocator&
130 {
131  m_wire.reset();
132  m_type = KeyLocator_None;
133  m_name.clear();
134  m_keyDigest.reset();
135  return *this;
136 }
137 
138 const Name&
140 {
141  if (m_type != KeyLocator_Name)
142  BOOST_THROW_EXCEPTION(Error("KeyLocator type is not Name"));
143 
144  return m_name;
145 }
146 
147 KeyLocator&
149 {
150  this->clear();
151  m_type = KeyLocator_Name;
152  m_name = name;
153  return *this;
154 }
155 
156 const Block&
158 {
159  if (m_type != KeyLocator_KeyDigest)
160  BOOST_THROW_EXCEPTION(Error("KeyLocator type is not KeyDigest"));
161 
162  return m_keyDigest;
163 }
164 
165 KeyLocator&
167 {
168  if (keyDigest.type() != tlv::KeyDigest)
169  BOOST_THROW_EXCEPTION(Error("expecting KeyDigest block"));
170 
171  this->clear();
172  m_type = KeyLocator_KeyDigest;
173  m_keyDigest = keyDigest;
174  return *this;
175 }
176 
177 KeyLocator&
179 {
180  // WARNING: ConstBufferPtr is shared_ptr<const Buffer>
181  // This function takes a constant reference of a shared pointer.
182  // It MUST NOT change the reference count of that shared pointer.
183 
184  return this->setKeyDigest(makeBinaryBlock(tlv::KeyDigest, keyDigest->get(), keyDigest->size()));
185 }
186 
187 bool
189 {
190  return wireEncode() == other.wireEncode();
191 }
192 
193 std::ostream&
194 operator<<(std::ostream& os, const KeyLocator& keyLocator)
195 {
196  switch (keyLocator.getType()) {
198  return os << "Name=" << keyLocator.getName();
199  }
201  const size_t MAX_DIGEST_OCTETS_TO_SHOW = 5;
202  const Block& digest = keyLocator.getKeyDigest();
203  os << "KeyDigest=" << toHex(digest.value(), digest.value_size()).substr(0, MAX_DIGEST_OCTETS_TO_SHOW * 2);
204  if (digest.value_size() > MAX_DIGEST_OCTETS_TO_SHOW) {
205  os << "...";
206  }
207  return os;
208  }
210  return os << "None";
211  }
213  return os << "Unknown";
214  }
215  }
216  return os << "Unknown";
217 }
218 
219 } // namespace ndn
size_t wireEncode(EncodingImpl< TAG > &encoder) const
prepend wire encoding
Definition: key-locator.cpp:52
Copyright (c) 2013-2016 Regents of the University of California.
Definition: common.hpp:74
EncodingImpl< EstimatorTag > EncodingEstimator
std::ostream & operator<<(std::ostream &os, const Data &data)
Definition: data.cpp:320
KeyLocator & setName(const Name &name)
set Name element
void wireDecode(const Block &wire)
decode from wire encoding
const element_container & elements() const
Get all subelements.
Definition: block.hpp:342
KeyLocator & setKeyDigest(const Block &keyDigest)
set KeyDigest element
size_t wireEncode(EncodingImpl< TAG > &encoder) const
Fast encoding or block size estimation.
Definition: name.cpp:122
Class representing a wire element of NDN-TLV packet format.
Definition: block.hpp:43
KeyLocator()
construct an empty KeyLocator
Definition: key-locator.cpp:35
indicates KeyLocator contains a Name
Definition: key-locator.hpp:49
const Name & getName() const
get Name element
element_const_iterator elements_begin() const
Definition: block.cpp:589
a concept check for TLV abstraction with .wireEncode method
Definition: concepts.hpp:56
const Block & getKeyDigest() const
get KeyDigest element
EncodingImpl< EncoderTag > EncodingBuffer
Type getType() const
Block makeBinaryBlock(uint32_t type, const uint8_t *value, size_t length)
Create a TLV block type type with value from a buffer value of size length.
indicates KeyLocator is empty (internal use only)
Definition: key-locator.hpp:46
std::string toHex(const uint8_t *buffer, size_t length, bool isUpperCase)
Return the hex representation of the bytes in array.
void reset()
Reset wire buffer of the element.
Definition: block.cpp:302
Name abstraction to represent an absolute name.
Definition: name.hpp:46
size_t value_size() const
Definition: block.cpp:529
indicates KeyLocator contains an unknown element
Definition: key-locator.hpp:55
void parse() const
Parse wire buffer into subblocks.
Definition: block.cpp:322
uint32_t type() const
Definition: block.hpp:324
KeyLocator & clear()
clear KeyLocator
const Block & wireEncode() const
Definition: key-locator.cpp:84
indicates KeyLocator contains a KeyDigest
Definition: key-locator.hpp:52
bool hasWire() const
Check if the Block has fully encoded wire.
Definition: block.cpp:471
const uint8_t * value() const
Definition: block.cpp:520
bool operator==(const KeyLocator &other) const
shared_ptr< const Buffer > ConstBufferPtr
Definition: buffer.hpp:33
void wireDecode(const Block &wire)
Definition: name.cpp:161
a concept check for TLV abstraction with .wireEncode method
Definition: concepts.hpp:40
a concept check for TLV abstraction with .wireDecode method and constructible from Block ...
Definition: concepts.hpp:76
void clear()
Clear all the components.
Definition: name.hpp:210