NS-3 based Named Data Networking (NDN) simulator
ndnSIM 2.0: NDN, CCN, CCNx, content centric networks
API Documentation
data.cpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
22 #include "data.hpp"
24 #include "util/crypto.hpp"
25 
26 namespace ndn {
27 
28 BOOST_CONCEPT_ASSERT((boost::EqualityComparable<Data>));
29 BOOST_CONCEPT_ASSERT((WireEncodable<Data>));
30 BOOST_CONCEPT_ASSERT((WireEncodableWithEncodingBuffer<Data>));
31 BOOST_CONCEPT_ASSERT((WireDecodable<Data>));
32 static_assert(std::is_base_of<tlv::Error, Data::Error>::value,
33  "Data::Error must inherit from tlv::Error");
34 
36  : m_content(tlv::Content) // empty content
37 {
38 }
39 
41  : m_name(name)
42 {
43 }
44 
45 Data::Data(const Block& wire)
46 {
47  wireDecode(wire);
48 }
49 
50 template<encoding::Tag TAG>
51 size_t
52 Data::wireEncode(EncodingImpl<TAG>& encoder, bool unsignedPortion/* = false*/) const
53 {
54  size_t totalLength = 0;
55 
56  // Data ::= DATA-TLV TLV-LENGTH
57  // Name
58  // MetaInfo
59  // Content
60  // Signature
61 
62  // (reverse encoding)
63 
64  if (!unsignedPortion && !m_signature)
65  {
66  BOOST_THROW_EXCEPTION(Error("Requested wire format, but data packet has not been signed yet"));
67  }
68 
69  if (!unsignedPortion)
70  {
71  // SignatureValue
72  totalLength += encoder.prependBlock(m_signature.getValue());
73  }
74 
75  // SignatureInfo
76  totalLength += encoder.prependBlock(m_signature.getInfo());
77 
78  // Content
79  totalLength += encoder.prependBlock(getContent());
80 
81  // MetaInfo
82  totalLength += getMetaInfo().wireEncode(encoder);
83 
84  // Name
85  totalLength += getName().wireEncode(encoder);
86 
87  if (!unsignedPortion)
88  {
89  totalLength += encoder.prependVarNumber(totalLength);
90  totalLength += encoder.prependVarNumber(tlv::Data);
91  }
92  return totalLength;
93 }
94 
95 
96 template size_t
97 Data::wireEncode<encoding::EncoderTag>(EncodingImpl<encoding::EncoderTag>& encoder,
98  bool unsignedPortion) const;
99 
100 template size_t
101 Data::wireEncode<encoding::EstimatorTag>(EncodingImpl<encoding::EstimatorTag>& encoder,
102  bool unsignedPortion) const;
103 
104 
105 const Block&
106 Data::wireEncode(EncodingBuffer& encoder, const Block& signatureValue) const
107 {
108  size_t totalLength = encoder.size();
109  totalLength += encoder.appendBlock(signatureValue);
110 
111  encoder.prependVarNumber(totalLength);
112  encoder.prependVarNumber(tlv::Data);
113 
114  const_cast<Data*>(this)->wireDecode(encoder.block());
115  return m_wire;
116 }
117 
118 const Block&
120 {
121  if (m_wire.hasWire())
122  return m_wire;
123 
124  EncodingEstimator estimator;
125  size_t estimatedSize = wireEncode(estimator);
126 
127  EncodingBuffer buffer(estimatedSize, 0);
128  wireEncode(buffer);
129 
130  const_cast<Data*>(this)->wireDecode(buffer.block());
131  return m_wire;
132 }
133 
134 void
136 {
137  m_fullName.clear();
138  m_wire = wire;
139  m_wire.parse();
140 
141  // Data ::= DATA-TLV TLV-LENGTH
142  // Name
143  // MetaInfo
144  // Content
145  // Signature
146 
147  // Name
148  m_name.wireDecode(m_wire.get(tlv::Name));
149 
150  // MetaInfo
151  m_metaInfo.wireDecode(m_wire.get(tlv::MetaInfo));
152 
153  // Content
154  m_content = m_wire.get(tlv::Content);
155 
157  // Signature //
159 
160  // SignatureInfo
161  m_signature.setInfo(m_wire.get(tlv::SignatureInfo));
162 
163  // SignatureValue
165  if (val != m_wire.elements_end())
166  m_signature.setValue(*val);
167 }
168 
169 Data&
171 {
172  onChanged();
173  m_name = name;
174 
175  return *this;
176 }
177 
178 const Name&
180 {
181  if (m_fullName.empty()) {
182  if (!m_wire.hasWire()) {
183  BOOST_THROW_EXCEPTION(Error("Full name requested, but Data packet does not have wire format "
184  "(e.g., not signed)"));
185  }
186  m_fullName = m_name;
187  m_fullName.appendImplicitSha256Digest(crypto::sha256(m_wire.wire(), m_wire.size()));
188  }
189 
190  return m_fullName;
191 }
192 
193 Data&
194 Data::setMetaInfo(const MetaInfo& metaInfo)
195 {
196  onChanged();
197  m_metaInfo = metaInfo;
198 
199  return *this;
200 }
201 
202 Data&
203 Data::setContentType(uint32_t type)
204 {
205  onChanged();
206  m_metaInfo.setType(type);
207 
208  return *this;
209 }
210 
211 Data&
212 Data::setFreshnessPeriod(const time::milliseconds& freshnessPeriod)
213 {
214  onChanged();
215  m_metaInfo.setFreshnessPeriod(freshnessPeriod);
216 
217  return *this;
218 }
219 
220 Data&
222 {
223  onChanged();
224  m_metaInfo.setFinalBlockId(finalBlockId);
225 
226  return *this;
227 }
228 
229 const Block&
231 {
232  if (m_content.empty())
233  m_content = makeEmptyBlock(tlv::Content);
234 
235  if (!m_content.hasWire())
236  m_content.encode();
237  return m_content;
238 }
239 
240 Data&
241 Data::setContent(const uint8_t* content, size_t contentLength)
242 {
243  onChanged();
244 
245  m_content = makeBinaryBlock(tlv::Content, content, contentLength);
246 
247  return *this;
248 }
249 
250 Data&
251 Data::setContent(const ConstBufferPtr& contentValue)
252 {
253  onChanged();
254 
255  m_content = Block(tlv::Content, contentValue); // not a real wire encoding yet
256 
257  return *this;
258 }
259 
260 Data&
261 Data::setContent(const Block& content)
262 {
263  onChanged();
264 
265  if (content.type() == tlv::Content)
266  m_content = content;
267  else {
268  m_content = Block(tlv::Content, content);
269  }
270 
271  return *this;
272 }
273 
274 Data&
275 Data::setSignature(const Signature& signature)
276 {
277  onChanged();
278  m_signature = signature;
279 
280  return *this;
281 }
282 
283 Data&
285 {
286  onChanged();
287  m_signature.setValue(value);
288 
289  return *this;
290 }
291 
292 void
294 {
295  // The values have changed, so the wire format is invalidated
296 
297  // !!!Note!!! Signature is not invalidated and it is responsibility of
298  // the application to do proper re-signing if necessary
299 
300  m_wire.reset();
301  m_fullName.clear();
302 }
303 
304 bool
305 Data::operator==(const Data& other) const
306 {
307  return getName() == other.getName() &&
308  getMetaInfo() == other.getMetaInfo() &&
309  getContent() == other.getContent() &&
310  getSignature() == other.getSignature();
311 }
312 
313 bool
314 Data::operator!=(const Data& other) const
315 {
316  return !(*this == other);
317 }
318 
319 std::ostream&
320 operator<<(std::ostream& os, const Data& data)
321 {
322  os << "Name: " << data.getName() << "\n";
323  os << "MetaInfo: " << data.getMetaInfo() << "\n";
324  os << "Content: (size: " << data.getContent().value_size() << ")\n";
325  os << "Signature: (type: " << data.getSignature().getType() <<
326  ", value_length: "<< data.getSignature().getValue().value_size() << ")";
327  os << std::endl;
328 
329  return os;
330 }
331 
332 #ifdef NDN_LP_KEEP_LOCAL_CONTROL_HEADER
333 
334 // Permit deprecated usage for gcc only.
335 // clang allows deprecated usage in deprecated functions, so it doesn't need this directive.
336 #pragma GCC diagnostic push
337 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
338 
340 Data::getLocalControlHeader()
341 {
342  return lp::LocalControlHeaderFacade(*this);
343 }
344 
346 Data::getLocalControlHeader() const
347 {
348  return lp::LocalControlHeaderFacade(const_cast<Data&>(*this));
349 }
350 
351 uint64_t
352 Data::getIncomingFaceId() const
353 {
354  return getLocalControlHeader().getIncomingFaceId();
355 }
356 
357 Data&
358 Data::setIncomingFaceId(uint64_t incomingFaceId)
359 {
360  getLocalControlHeader().setIncomingFaceId(incomingFaceId);
361  return *this;
362 }
363 
365 Data::getCachingPolicy() const
366 {
367  return getLocalControlHeader().getCachingPolicy();
368 }
369 
370 Data&
371 Data::setCachingPolicy(lp::LocalControlHeaderFacade::CachingPolicy cachingPolicy)
372 {
373  getLocalControlHeader().setCachingPolicy(cachingPolicy);
374  return *this;
375 }
376 
377 #pragma GCC diagnostic pop
378 
379 #endif // NDN_LP_KEEP_LOCAL_CONTROL_HEADER
380 
381 } // namespace ndn
const Block & wireEncode() const
Encode to a wire format.
Definition: data.cpp:119
bool hasWire() const
Check if the Block has fully encoded wire.
Definition: block.cpp:471
Data & setContentType(uint32_t type)
Definition: data.cpp:203
Copyright (c) 2011-2015 Regents of the University of California.
void wireDecode(const Block &wire)
Decode from the wire format.
Definition: data.cpp:135
Block makeEmptyBlock(uint32_t type)
Create a TLV block type type containing no value (i.e., a boolean block)
const Block & getContent() const
Get content Block.
Definition: data.cpp:230
Data()
Create an empty Data object.
Definition: data.cpp:35
Data & setSignature(const Signature &signature)
Set the signature to a copy of the given signature.
Definition: data.cpp:275
EncodingImpl< EstimatorTag > EncodingEstimator
Data & setName(const Name &name)
Set name to a copy of the given Name.
Definition: data.cpp:170
size_t value_size() const
Definition: block.cpp:529
const Name & getName() const
Get name of the Data packet.
Definition: data.hpp:360
std::ostream & operator<<(std::ostream &os, const Data &data)
Definition: data.cpp:320
void setInfo(const Block &info)
Set SignatureInfo from a block.
Definition: signature.cpp:44
element_const_iterator elements_end() const
Definition: block.cpp:595
const uint8_t * wire() const
Definition: block.cpp:495
Data & setContent(const uint8_t *buffer, size_t bufferSize)
Set the content from the buffer (buffer will be copied)
Definition: data.cpp:241
void parse() const
Parse wire buffer into subblocks.
Definition: block.cpp:322
Class representing a wire element of NDN-TLV packet format.
Definition: block.hpp:43
bool operator!=(const Data &other) const
Definition: data.cpp:314
bool operator==(const Data &other) const
Definition: data.cpp:305
Data & setFreshnessPeriod(const time::milliseconds &freshnessPeriod)
Definition: data.cpp:212
void onChanged()
Clear the wire encoding.
Definition: data.cpp:293
MetaInfo & setFinalBlockId(const name::Component &finalBlockId)
Definition: meta-info.cpp:63
bool empty() const
Check if the Block is empty.
Definition: block.cpp:465
a concept check for TLV abstraction with .wireEncode method
Definition: concepts.hpp:50
size_t size() const
Definition: block.cpp:504
Data & setSignatureValue(const Block &value)
Definition: data.cpp:284
MetaInfo & setType(uint32_t type)
set ContentType
Definition: meta-info.cpp:47
const Block & get(uint32_t type) const
Get the first subelement of the requested type.
Definition: block.cpp:409
EncodingImpl< EncoderTag > EncodingBuffer
element_const_iterator find(uint32_t type) const
Definition: block.cpp:420
void setValue(const Block &value)
Get SignatureValue from a block.
Definition: signature.cpp:50
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.
const Block & getInfo() const
Get SignatureInfo in the wire format.
Definition: signature.hpp:70
const Block & getValue() const
Get SignatureValue in the wire format.
Definition: signature.hpp:96
An MetaInfo holds the meta info which is signed inside the data packet.
Definition: meta-info.hpp:56
const MetaInfo & getMetaInfo() const
Get MetaInfo block from Data packet.
Definition: data.hpp:366
element_container::const_iterator element_const_iterator
Definition: block.hpp:48
MetaInfo & setFreshnessPeriod(const time::milliseconds &freshnessPeriod)
Definition: meta-info.cpp:55
void reset()
Reset wire buffer of the element.
Definition: block.cpp:302
Name abstraction to represent an absolute name.
Definition: name.hpp:46
expose NDNLPv2 tags as LocalControlHeader API
Definition: tags.hpp:64
const Name & getFullName() const
Get full name of Data packet, including the implicit digest.
Definition: data.cpp:179
size_t wireEncode(EncodingImpl< TAG > &encoder) const
Definition: meta-info.cpp:130
Component holds a read-only name component value.
void encode()
Encode subblocks into wire buffer.
Definition: block.cpp:355
bool empty() const
Check if name is emtpy.
Definition: name.hpp:398
size_t wireEncode(EncodingImpl< TAG > &encoder) const
Fast encoding or block size estimation.
Definition: name.cpp:69
Data & setMetaInfo(const MetaInfo &metaInfo)
Set metaInfo to a copy of the given MetaInfo.
Definition: data.cpp:194
uint32_t getType() const
Get signature type.
Definition: signature.hpp:114
const Signature & getSignature() const
Definition: data.hpp:390
shared_ptr< const Buffer > ConstBufferPtr
Definition: buffer.hpp:33
ConstBufferPtr sha256(const uint8_t *data, size_t dataLength)
Compute the sha-256 digest of data.
Definition: crypto.cpp:51
Data & setFinalBlockId(const name::Component &finalBlockId)
Definition: data.cpp:221
void wireDecode(const Block &wire)
Definition: name.cpp:108
a concept check for TLV abstraction with .wireEncode method
Definition: concepts.hpp:34
represents a Data packet
Definition: data.hpp:39
a concept check for TLV abstraction with .wireDecode method and constructible from Block ...
Definition: concepts.hpp:70
void clear()
Clear all the components.
Definition: name.hpp:218
uint32_t type() const
Definition: block.hpp:346
A Signature is storage for the signature-related information (info and value) in a Data packet...
Definition: signature.hpp:33
void wireDecode(const Block &wire)
Definition: meta-info.cpp:192
Name & appendImplicitSha256Digest(const ConstBufferPtr &digest)
Append ImplicitSha256Digest.
Definition: name.cpp:260