NS-3 based Named Data Networking (NDN) simulator
ndnSIM 2.5: NDN, CCN, CCNx, content centric networks
API Documentation
data.hpp
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 #ifndef NDN_CXX_DATA_HPP
23 #define NDN_CXX_DATA_HPP
24 
27 #include "ndn-cxx/meta-info.hpp"
28 #include "ndn-cxx/name.hpp"
31 
32 namespace ndn {
33 
37 class Data : public PacketBase, public std::enable_shared_from_this<Data>
38 {
39 public:
40  class Error : public tlv::Error
41  {
42  public:
43  using tlv::Error::Error;
44  };
45 
51  explicit
52  Data(const Name& name = Name());
53 
60  explicit
61  Data(const Block& wire);
62 
72  template<encoding::Tag TAG>
73  size_t
74  wireEncode(EncodingImpl<TAG>& encoder, bool wantUnsignedPortionOnly = false) const;
75 
94  const Block&
95  wireEncode(EncodingBuffer& encoder, span<const uint8_t> signature) const;
96 
100  const Block&
101  wireEncode() const;
102 
105  void
106  wireDecode(const Block& wire);
107 
110  bool
111  hasWire() const noexcept
112  {
113  return m_wire.hasWire();
114  }
115 
120  const Name&
121  getFullName() const;
122 
123 public: // Data fields
126  const Name&
127  getName() const noexcept
128  {
129  return m_name;
130  }
131 
135  Data&
136  setName(const Name& name);
137 
140  const MetaInfo&
141  getMetaInfo() const noexcept
142  {
143  return m_metaInfo;
144  }
145 
149  Data&
150  setMetaInfo(const MetaInfo& metaInfo);
151 
155  bool
156  hasContent() const noexcept
157  {
158  return m_content.isValid();
159  }
160 
174  const Block&
175  getContent() const noexcept
176  {
177  return m_content;
178  }
179 
188  Data&
189  setContent(const Block& block);
190 
196  Data&
197  setContent(span<const uint8_t> value);
198 
206  [[deprecated("use the overload that takes a span<>")]]
207  Data&
208  setContent(const uint8_t* value, size_t length);
209 
215  Data&
216  setContent(ConstBufferPtr value);
217 
223  Data&
224  unsetContent();
225 
228  const SignatureInfo&
229  getSignatureInfo() const noexcept
230  {
231  return m_signatureInfo;
232  }
233 
243  Data&
245 
248  const Block&
249  getSignatureValue() const noexcept
250  {
251  return m_signatureValue;
252  }
253 
263  Data&
265 
270  InputBuffers
271  extractSignedRanges() const;
272 
273 public: // MetaInfo fields
274  uint32_t
276  {
277  return m_metaInfo.getType();
278  }
279 
280  Data&
281  setContentType(uint32_t type);
282 
285  {
286  return m_metaInfo.getFreshnessPeriod();
287  }
288 
289  Data&
290  setFreshnessPeriod(time::milliseconds freshnessPeriod);
291 
292  const optional<name::Component>&
294  {
295  return m_metaInfo.getFinalBlock();
296  }
297 
298  Data&
299  setFinalBlock(optional<name::Component> finalBlockId);
300 
301 public: // SignatureInfo fields
305  int32_t
306  getSignatureType() const noexcept
307  {
308  return m_signatureInfo.getSignatureType();
309  }
310 
313  optional<KeyLocator>
314  getKeyLocator() const noexcept
315  {
316  return m_signatureInfo.hasKeyLocator() ? make_optional(m_signatureInfo.getKeyLocator()) : nullopt;
317  }
318 
319 protected:
323  void
324  resetWire();
325 
326 private:
327  Name m_name;
328  MetaInfo m_metaInfo;
329  Block m_content;
330  SignatureInfo m_signatureInfo;
331  Block m_signatureValue;
332 
333  mutable Block m_wire;
334  mutable Name m_fullName; // cached FullName computed from m_wire
335 };
336 
337 #ifndef DOXYGEN
338 extern template size_t
339 Data::wireEncode<encoding::EncoderTag>(EncodingBuffer&, bool) const;
340 
341 extern template size_t
342 Data::wireEncode<encoding::EstimatorTag>(EncodingEstimator&, bool) const;
343 #endif
344 
345 std::ostream&
346 operator<<(std::ostream& os, const Data& data);
347 
348 bool
349 operator==(const Data& lhs, const Data& rhs);
350 
351 inline bool
352 operator!=(const Data& lhs, const Data& rhs)
353 {
354  return !(lhs == rhs);
355 }
356 
357 } // namespace ndn
358 
359 #endif // NDN_CXX_DATA_HPP
const Block & wireEncode() const
Encode into a Block.
Definition: data.cpp:109
Data & setContentType(uint32_t type)
Definition: data.cpp:336
Copyright (c) 2011-2015 Regents of the University of California.
Represents a SignatureInfo or InterestSignatureInfo TLV element.
void wireDecode(const Block &wire)
Decode from wire.
Definition: data.cpp:125
Data & setSignatureValue(ConstBufferPtr value)
Set SignatureValue.
Definition: data.cpp:310
Data & setName(const Name &name)
Set name.
Definition: data.cpp:228
std::ostream & operator<<(std::ostream &os, const Data &data)
Definition: data.cpp:376
Data(const Name &name=Name())
Construct an unsigned Data packet with given name and empty Content.
Definition: data.cpp:34
Data & setContent(const Block &block)
Set Content from a Block.
Definition: data.cpp:246
optional< T > make_optional(T const &value)
SignatureInfo info
void resetWire()
Clear wire encoding and cached FullName.
Definition: data.cpp:221
Represents a TLV element of the NDN packet format.
Definition: block.hpp:44
optional< KeyLocator > getKeyLocator() const noexcept
Get KeyLocator.
Definition: data.hpp:314
bool operator!=(const Data &lhs, const Data &rhs)
Definition: data.hpp:352
const Block & getSignatureValue() const noexcept
Get SignatureValue.
Definition: data.hpp:249
bool hasWire() const noexcept
Check if this instance has cached wire encoding.
Definition: data.hpp:111
int32_t getSignatureType() const noexcept
Get SignatureType.
Definition: data.hpp:306
Data & setSignatureInfo(const SignatureInfo &info)
Set SignatureInfo.
Definition: data.cpp:302
InputBuffers extractSignedRanges() const
Extract ranges of Data covered by the signature.
Definition: data.cpp:322
A MetaInfo holds the meta info which is signed inside the data packet.
Definition: meta-info.hpp:58
base class to allow simple management of packet tags
Definition: packet-base.hpp:31
const Name & getName() const noexcept
Get name.
Definition: data.hpp:127
Represents an absolute name.
Definition: name.hpp:41
const MetaInfo & getMetaInfo() const noexcept
Get MetaInfo.
Definition: data.hpp:141
const Name & getFullName() const
Get full name including implicit digest.
Definition: data.cpp:207
Data & setFreshnessPeriod(time::milliseconds freshnessPeriod)
Definition: data.cpp:346
Data & setFinalBlock(optional< name::Component > finalBlockId)
Definition: data.cpp:356
const Block & getContent() const noexcept
Get the Content element.
Definition: data.hpp:175
Data & unsetContent()
Remove the Content element.
Definition: data.cpp:294
bool operator==(const Data &lhs, const Data &rhs)
Definition: data.cpp:366
Data & setMetaInfo(const MetaInfo &metaInfo)
Set MetaInfo.
Definition: data.cpp:238
time::milliseconds getFreshnessPeriod() const
Definition: data.hpp:284
Represents a Data packet.
Definition: data.hpp:37
bool hasContent() const noexcept
Return whether this Data has a Content element.
Definition: data.hpp:156
uint32_t getContentType() const
Definition: data.hpp:275
const optional< name::Component > & getFinalBlock() const
Definition: data.hpp:293
EncodingImpl< EncoderTag > EncodingBuffer
const nullopt_t nullopt((nullopt_t::init()))
Error(const char *expectedType, uint32_t actualType)
Definition: tlv.cpp:27
represents an error in TLV encoding or decoding
Definition: tlv.hpp:52
EncodingImpl< EstimatorTag > EncodingEstimator
const SignatureInfo & getSignatureInfo() const noexcept
Get SignatureInfo.
Definition: data.hpp:229
shared_ptr< const Buffer > ConstBufferPtr
Definition: buffer.hpp:139
boost::chrono::milliseconds milliseconds
Definition: time.hpp:48