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 
172  const Block&
173  getContent() const noexcept
174  {
175  return m_content;
176  }
177 
186  Data&
187  setContent(const Block& block);
188 
194  Data&
195  setContent(span<const uint8_t> value);
196 
204  [[deprecated("use the overload that takes a span<>")]]
205  Data&
206  setContent(const uint8_t* value, size_t length);
207 
213  Data&
214  setContent(ConstBufferPtr value);
215 
221  Data&
222  unsetContent();
223 
226  const SignatureInfo&
227  getSignatureInfo() const noexcept
228  {
229  return m_signatureInfo;
230  }
231 
241  Data&
243 
246  const Block&
247  getSignatureValue() const noexcept
248  {
249  return m_signatureValue;
250  }
251 
261  Data&
263 
268  InputBuffers
269  extractSignedRanges() const;
270 
271 public: // MetaInfo fields
272  uint32_t
274  {
275  return m_metaInfo.getType();
276  }
277 
278  Data&
279  setContentType(uint32_t type);
280 
283  {
284  return m_metaInfo.getFreshnessPeriod();
285  }
286 
287  Data&
288  setFreshnessPeriod(time::milliseconds freshnessPeriod);
289 
290  const optional<name::Component>&
292  {
293  return m_metaInfo.getFinalBlock();
294  }
295 
296  Data&
297  setFinalBlock(optional<name::Component> finalBlockId);
298 
299 public: // SignatureInfo fields
303  int32_t
304  getSignatureType() const noexcept
305  {
306  return m_signatureInfo.getSignatureType();
307  }
308 
311  optional<KeyLocator>
312  getKeyLocator() const noexcept
313  {
314  return m_signatureInfo.hasKeyLocator() ? make_optional(m_signatureInfo.getKeyLocator()) : nullopt;
315  }
316 
317 protected:
321  void
322  resetWire();
323 
324 private:
325  Name m_name;
326  MetaInfo m_metaInfo;
327  Block m_content;
328  SignatureInfo m_signatureInfo;
329  Block m_signatureValue;
330 
331  mutable Block m_wire;
332  mutable Name m_fullName; // cached FullName computed from m_wire
333 };
334 
335 #ifndef DOXYGEN
336 extern template size_t
337 Data::wireEncode<encoding::EncoderTag>(EncodingBuffer&, bool) const;
338 
339 extern template size_t
340 Data::wireEncode<encoding::EstimatorTag>(EncodingEstimator&, bool) const;
341 #endif
342 
343 std::ostream&
344 operator<<(std::ostream& os, const Data& data);
345 
346 bool
347 operator==(const Data& lhs, const Data& rhs);
348 
349 inline bool
350 operator!=(const Data& lhs, const Data& rhs)
351 {
352  return !(lhs == rhs);
353 }
354 
355 } // namespace ndn
356 
357 #endif // NDN_CXX_DATA_HPP
Represents a TLV element of the NDN packet format.
Definition: block.hpp:45
bool hasWire() const noexcept
Check if the Block contains a fully encoded wire representation.
Definition: block.hpp:241
bool isValid() const noexcept
Check if the Block is valid.
Definition: block.hpp:212
Represents a Data packet.
Definition: data.hpp:38
int32_t getSignatureType() const noexcept
Get SignatureType.
Definition: data.hpp:304
void wireDecode(const Block &wire)
Decode from wire.
Definition: data.cpp:125
InputBuffers extractSignedRanges() const
Extract ranges of Data covered by the signature.
Definition: data.cpp:322
Data & setContent(const Block &block)
Set Content from a Block.
Definition: data.cpp:246
Data & setSignatureValue(ConstBufferPtr value)
Set SignatureValue.
Definition: data.cpp:310
Data(const Name &name=Name())
Construct an unsigned Data packet with given name and empty Content.
Definition: data.cpp:34
const MetaInfo & getMetaInfo() const noexcept
Get MetaInfo.
Definition: data.hpp:141
bool hasWire() const noexcept
Check if this instance has cached wire encoding.
Definition: data.hpp:111
void resetWire()
Clear wire encoding and cached FullName.
Definition: data.cpp:221
optional< KeyLocator > getKeyLocator() const noexcept
Get KeyLocator.
Definition: data.hpp:312
Data & setFreshnessPeriod(time::milliseconds freshnessPeriod)
Definition: data.cpp:346
const Block & wireEncode() const
Encode into a Block.
Definition: data.cpp:109
const Name & getFullName() const
Get full name including implicit digest.
Definition: data.cpp:207
Data & setFinalBlock(optional< name::Component > finalBlockId)
Definition: data.cpp:356
bool hasContent() const noexcept
Return whether this Data has a Content element.
Definition: data.hpp:156
const SignatureInfo & getSignatureInfo() const noexcept
Get SignatureInfo.
Definition: data.hpp:227
const Name & getName() const noexcept
Get name.
Definition: data.hpp:127
const Block & getSignatureValue() const noexcept
Get SignatureValue.
Definition: data.hpp:247
Data & setSignatureInfo(const SignatureInfo &info)
Set SignatureInfo.
Definition: data.cpp:302
time::milliseconds getFreshnessPeriod() const
Definition: data.hpp:282
const optional< name::Component > & getFinalBlock() const
Definition: data.hpp:291
uint32_t getContentType() const
Definition: data.hpp:273
Data & setName(const Name &name)
Set name.
Definition: data.cpp:228
const Block & getContent() const noexcept
Get the Content element.
Definition: data.hpp:173
Data & setContentType(uint32_t type)
Definition: data.cpp:336
Data & setMetaInfo(const MetaInfo &metaInfo)
Set MetaInfo.
Definition: data.cpp:238
Data & unsetContent()
Remove the Content element.
Definition: data.cpp:294
A MetaInfo holds the meta info which is signed inside the data packet.
Definition: meta-info.hpp:59
const optional< name::Component > & getFinalBlock() const
return FinalBlockId
Definition: meta-info.hpp:121
time::milliseconds getFreshnessPeriod() const
return FreshnessPeriod
Definition: meta-info.hpp:107
uint32_t getType() const
return ContentType
Definition: meta-info.hpp:91
Represents an absolute name.
Definition: name.hpp:46
base class to allow simple management of packet tags
Definition: packet-base.hpp:32
Represents a SignatureInfo or InterestSignatureInfo TLV element.
int32_t getSignatureType() const noexcept
Get SignatureType.
bool hasKeyLocator() const noexcept
Check if KeyLocator is present.
const KeyLocator & getKeyLocator() const
Get KeyLocator.
represents an error in TLV encoding or decoding
Definition: tlv.hpp:53
Error(const char *expectedType, uint32_t actualType)
Definition: tlv.cpp:27
EncodingImpl< EstimatorTag > EncodingEstimator
EncodingImpl< EncoderTag > EncodingBuffer
boost::chrono::milliseconds milliseconds
Definition: time.hpp:48
@ Name
Definition: tlv.hpp:67
@ Data
Definition: tlv.hpp:66
Definition: data.cpp:25
bool operator!=(const Data &lhs, const Data &rhs)
Definition: data.hpp:350
shared_ptr< const Buffer > ConstBufferPtr
Definition: buffer.hpp:139
bool operator==(const Data &lhs, const Data &rhs)
Definition: data.cpp:366
std::ostream & operator<<(std::ostream &os, const Data &data)
Definition: data.cpp:376
SignatureInfo info