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 //
293 
294 Data&
295 Data::setIncomingFaceId(uint64_t incomingFaceId)
296 {
297  getLocalControlHeader().setIncomingFaceId(incomingFaceId);
298  // ! do not reset Data's wire !
299 
300  return *this;
301 }
302 
303 Data&
305 {
306  getLocalControlHeader().setCachingPolicy(cachingPolicy);
307  // ! do not reset Data's wire !
308 
309  return *this;
310 }
311 
312 void
314 {
315  // The values have changed, so the wire format is invalidated
316 
317  // !!!Note!!! Signature is not invalidated and it is responsibility of
318  // the application to do proper re-signing if necessary
319 
320  m_wire.reset();
321  m_fullName.clear();
322 }
323 
324 bool
325 Data::operator==(const Data& other) const
326 {
327  return getName() == other.getName() &&
328  getMetaInfo() == other.getMetaInfo() &&
329  getContent() == other.getContent() &&
330  getSignature() == other.getSignature();
331 }
332 
333 bool
334 Data::operator!=(const Data& other) const
335 {
336  return !(*this == other);
337 }
338 
339 std::ostream&
340 operator<<(std::ostream& os, const Data& data)
341 {
342  os << "Name: " << data.getName() << "\n";
343  os << "MetaInfo: " << data.getMetaInfo() << "\n";
344  os << "Content: (size: " << data.getContent().value_size() << ")\n";
345  os << "Signature: (type: " << data.getSignature().getType() <<
346  ", value_length: "<< data.getSignature().getValue().value_size() << ")";
347  os << std::endl;
348 
349  return os;
350 }
351 
352 } // namespace ndn
void setCachingPolicy(CachingPolicy cachingPolicy)
bool empty() const
Check if the Block is empty.
Definition: block.cpp:465
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)
size_t wireEncode(EncodingImpl< TAG > &encoder) const
Definition: meta-info.cpp:130
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
element_const_iterator find(uint32_t type) const
Definition: block.cpp:420
Data & setName(const Name &name)
Set name to a copy of the given Name.
Definition: data.cpp:170
std::ostream & operator<<(std::ostream &os, const Data &data)
Definition: data.cpp:340
void setInfo(const Block &info)
Set SignatureInfo from a block.
Definition: signature.cpp:44
Data & setContent(const uint8_t *buffer, size_t bufferSize)
Set the content from the buffer (buffer will be copied)
Definition: data.cpp:241
size_t wireEncode(EncodingImpl< TAG > &encoder) const
Fast encoding or block size estimation.
Definition: name.cpp:69
Class representing a wire element of NDN-TLV packet format.
Definition: block.hpp:43
const Block & getValue() const
Get SignatureValue in the wire format.
Definition: signature.hpp:96
const MetaInfo & getMetaInfo() const
Get MetaInfo block from Data packet.
Definition: data.hpp:349
bool operator!=(const Data &other) const
Definition: data.cpp:334
Data & setFreshnessPeriod(const time::milliseconds &freshnessPeriod)
Definition: data.cpp:212
const Name & getName() const
Get name of the Data packet.
Definition: data.hpp:343
element_const_iterator elements_end() const
Definition: block.cpp:595
size_t size() const
Definition: block.cpp:504
void onChanged()
Clear the wire encoding.
Definition: data.cpp:313
uint32_t getType() const
Get signature type.
Definition: signature.hpp:114
MetaInfo & setFinalBlockId(const name::Component &finalBlockId)
Definition: meta-info.cpp:63
a concept check for TLV abstraction with .wireEncode method
Definition: concepts.hpp:50
Data & setSignatureValue(const Block &value)
Definition: data.cpp:284
MetaInfo & setType(uint32_t type)
set ContentType
Definition: meta-info.cpp:47
EncodingImpl< EncoderTag > EncodingBuffer
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.
An MetaInfo holds the meta info which is signed inside the data packet.
Definition: meta-info.hpp:56
const Block & get(uint32_t type) const
Get the first subelement of the requested type.
Definition: block.cpp:409
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
size_t value_size() const
Definition: block.cpp:529
const Block & wireEncode() const
Encode to a wire format.
Definition: data.cpp:119
void parse() const
Parse wire buffer into subblocks.
Definition: block.cpp:322
uint32_t type() const
Definition: block.hpp:346
const Signature & getSignature() const
Definition: data.hpp:373
Component holds a read-only name component value.
const Block & getInfo() const
Get SignatureInfo in the wire format.
Definition: signature.hpp:70
void encode()
Encode subblocks into wire buffer.
Definition: block.cpp:355
const Block & getContent() const
Get content Block.
Definition: data.cpp:230
bool operator==(const Data &other) const
Definition: data.cpp:325
const uint8_t * wire() const
Definition: block.cpp:495
bool empty() const
Check if name is emtpy.
Definition: name.hpp:398
bool hasWire() const
Check if the Block has fully encoded wire.
Definition: block.cpp:471
void setIncomingFaceId(uint64_t incomingFaceId)
Data & setMetaInfo(const MetaInfo &metaInfo)
Set metaInfo to a copy of the given MetaInfo.
Definition: data.cpp:194
Data & setIncomingFaceId(uint64_t incomingFaceId)
Definition: data.cpp:295
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
const Name & getFullName() const
Get full name of Data packet, including the implicit digest.
Definition: data.cpp:179
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
nfd::LocalControlHeader & getLocalControlHeader()
Definition: data.hpp:379
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
Data & setCachingPolicy(nfd::LocalControlHeader::CachingPolicy cachingPolicy)
Definition: data.cpp:304
Name & appendImplicitSha256Digest(const ConstBufferPtr &digest)
Append ImplicitSha256Digest.
Definition: name.cpp:260