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-2024 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 
38 class Data : public PacketBase, public std::enable_shared_from_this<Data>
39 {
40 public:
41  class Error : public tlv::Error
42  {
43  public:
44  using tlv::Error::Error;
45  };
46 
54  explicit
55  Data(const Name& name = Name());
56 
65  explicit
66  Data(const Block& wire);
67 
77  template<encoding::Tag TAG>
78  size_t
79  wireEncode(EncodingImpl<TAG>& encoder, bool wantUnsignedPortionOnly = false) const;
80 
99  const Block&
100  wireEncode(EncodingBuffer& encoder, span<const uint8_t> signature) const;
101 
106  const Block&
107  wireEncode() const;
108 
112  void
113  wireDecode(const Block& wire);
114 
118  bool
119  hasWire() const noexcept
120  {
121  return m_wire.hasWire();
122  }
123 
129  const Name&
130  getFullName() const;
131 
132 public: // Data fields
136  const Name&
137  getName() const noexcept
138  {
139  return m_name;
140  }
141 
146  Data&
147  setName(const Name& name);
148 
152  const MetaInfo&
153  getMetaInfo() const noexcept
154  {
155  return m_metaInfo;
156  }
157 
162  Data&
163  setMetaInfo(const MetaInfo& metaInfo);
164 
168  bool
169  hasContent() const noexcept
170  {
171  return m_content.isValid();
172  }
173 
187  const Block&
188  getContent() const noexcept
189  {
190  return m_content;
191  }
192 
201  Data&
202  setContent(const Block& block);
203 
209  Data&
210  setContent(span<const uint8_t> value);
211 
217  Data&
218  setContent(std::string_view value);
219 
225  Data&
226  setContent(ConstBufferPtr value);
227 
228  Data&
229  setContent(std::nullptr_t) = delete;
230 
236  Data&
237  unsetContent();
238 
242  const SignatureInfo&
243  getSignatureInfo() const noexcept
244  {
245  return m_signatureInfo;
246  }
247 
258  Data&
260 
264  const Block&
265  getSignatureValue() const noexcept
266  {
267  return m_signatureValue;
268  }
269 
280  Data&
281  setSignatureValue(span<const uint8_t> value);
282 
293  Data&
295 
296  Data&
297  setSignatureValue(std::nullptr_t) = delete;
298 
304  [[nodiscard]] InputBuffers
305  extractSignedRanges() const;
306 
307 public: // MetaInfo fields
311  uint32_t
312  getContentType() const noexcept
313  {
314  return m_metaInfo.getType();
315  }
316 
320  Data&
321  setContentType(uint32_t type);
322 
327  getFreshnessPeriod() const noexcept
328  {
329  return m_metaInfo.getFreshnessPeriod();
330  }
331 
335  Data&
336  setFreshnessPeriod(time::milliseconds freshnessPeriod);
337 
341  const std::optional<name::Component>&
342  getFinalBlock() const noexcept
343  {
344  return m_metaInfo.getFinalBlock();
345  }
346 
350  Data&
351  setFinalBlock(std::optional<name::Component> finalBlockId);
352 
353 public: // SignatureInfo fields
357  int32_t
358  getSignatureType() const noexcept
359  {
360  return m_signatureInfo.getSignatureType();
361  }
362 
366  std::optional<KeyLocator>
367  getKeyLocator() const noexcept
368  {
369  if (m_signatureInfo.hasKeyLocator()) {
370  return m_signatureInfo.getKeyLocator();
371  }
372  return std::nullopt;
373  }
374 
375 protected:
380  void
381  resetWire();
382 
383 private:
384  Name m_name;
385  MetaInfo m_metaInfo;
386  Block m_content;
387  SignatureInfo m_signatureInfo;
388  Block m_signatureValue;
389 
390  mutable Block m_wire;
391  mutable Name m_fullName; // cached FullName computed from m_wire
392 };
393 
394 #ifndef DOXYGEN
395 extern template size_t
396 Data::wireEncode<encoding::EncoderTag>(EncodingBuffer&, bool) const;
397 
398 extern template size_t
399 Data::wireEncode<encoding::EstimatorTag>(EncodingEstimator&, bool) const;
400 #endif
401 
402 std::ostream&
403 operator<<(std::ostream& os, const Data& data);
404 
405 bool
406 operator==(const Data& lhs, const Data& rhs);
407 
408 inline bool
409 operator!=(const Data& lhs, const Data& rhs)
410 {
411  return !(lhs == rhs);
412 }
413 
414 } // namespace ndn
415 
416 #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:205
bool isValid() const noexcept
Check if the Block is valid.
Definition: block.hpp:193
Represents a Data packet.
Definition: data.hpp:39
int32_t getSignatureType() const noexcept
Get the SignatureType.
Definition: data.hpp:358
void wireDecode(const Block &wire)
Decode from wire.
Definition: data.cpp:118
InputBuffers extractSignedRanges() const
Extract ranges of Data covered by the signature.
Definition: data.cpp:323
Data & setContent(const Block &block)
Set Content from a Block.
Definition: data.cpp:239
Data(const Name &name=Name())
Construct an unsigned Data packet with given name and empty Content.
Definition: data.cpp:27
const MetaInfo & getMetaInfo() const noexcept
Get the MetaInfo element.
Definition: data.hpp:153
time::milliseconds getFreshnessPeriod() const noexcept
Return the value of FreshnessPeriod.
Definition: data.hpp:327
bool hasWire() const noexcept
Check if this instance has cached wire encoding.
Definition: data.hpp:119
void resetWire()
Clear wire encoding and cached FullName.
Definition: data.cpp:214
Data & setSignatureValue(std::nullptr_t)=delete
Data & setFreshnessPeriod(time::milliseconds freshnessPeriod)
Set the FreshnessPeriod.
Definition: data.cpp:347
const Block & wireEncode() const
Encode into a Block.
Definition: data.cpp:102
const Name & getFullName() const
Get the full name (including implicit digest).
Definition: data.cpp:200
const std::optional< name::Component > & getFinalBlock() const noexcept
Return the value of FinalBlockId.
Definition: data.hpp:342
bool hasContent() const noexcept
Return whether this Data has a Content element.
Definition: data.hpp:169
Data & setFinalBlock(std::optional< name::Component > finalBlockId)
Set the FinalBlockId.
Definition: data.cpp:357
Data & setSignatureValue(span< const uint8_t > value)
Set SignatureValue by copying from a contiguous sequence of bytes.
Definition: data.cpp:303
uint32_t getContentType() const noexcept
Return the value of ContentType.
Definition: data.hpp:312
const SignatureInfo & getSignatureInfo() const noexcept
Get the SignatureInfo element.
Definition: data.hpp:243
const Name & getName() const noexcept
Get the Data name.
Definition: data.hpp:137
const Block & getSignatureValue() const noexcept
Get the SignatureValue element.
Definition: data.hpp:265
Data & setSignatureInfo(const SignatureInfo &info)
Set the SignatureInfo element.
Definition: data.cpp:295
std::optional< KeyLocator > getKeyLocator() const noexcept
Get the KeyLocator element.
Definition: data.hpp:367
Data & setName(const Name &name)
Set the Data name.
Definition: data.cpp:221
Data & setContent(std::nullptr_t)=delete
const Block & getContent() const noexcept
Get the Content element.
Definition: data.hpp:188
Data & setContentType(uint32_t type)
Set the ContentType.
Definition: data.cpp:337
Data & setMetaInfo(const MetaInfo &metaInfo)
Set the MetaInfo element.
Definition: data.cpp:231
Data & unsetContent()
Remove the Content element.
Definition: data.cpp:285
A MetaInfo holds the meta info which is signed inside the Data packet.
Definition: meta-info.hpp:62
uint32_t getType() const noexcept
Return the value of ContentType.
Definition: meta-info.hpp:95
const std::optional< name::Component > & getFinalBlock() const noexcept
Return the value of FinalBlockId.
Definition: meta-info.hpp:128
time::milliseconds getFreshnessPeriod() const noexcept
Return the value of FreshnessPeriod.
Definition: meta-info.cpp:46
Represents an absolute name.
Definition: name.hpp:45
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 the SignatureType.
bool hasKeyLocator() const noexcept
Check if KeyLocator is present.
const KeyLocator & getKeyLocator() const
Get the KeyLocator element.
Represents an error in TLV encoding or decoding.
Definition: tlv.hpp:54
Error(const char *expectedType, uint32_t actualType)
Definition: tlv.cpp:28
EncodingImpl< EstimatorTag > EncodingEstimator
EncodingImpl< EncoderTag > EncodingBuffer
::boost::chrono::milliseconds milliseconds
Definition: time.hpp:52
@ Name
Definition: tlv.hpp:71
@ Data
Definition: tlv.hpp:69
Definition: data.cpp:25
bool operator!=(const Data &lhs, const Data &rhs)
Definition: data.hpp:409
bool operator==(const Data &lhs, const Data &rhs)
Definition: data.cpp:367
std::ostream & operator<<(std::ostream &os, const Data &data)
Definition: data.cpp:377
std::shared_ptr< const Buffer > ConstBufferPtr
Definition: buffer.hpp:140
SignatureInfo info