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 
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 
52  explicit
53  Data(const Name& name = Name());
54 
61  explicit
62  Data(const Block& wire);
63 
73  template<encoding::Tag TAG>
74  size_t
75  wireEncode(EncodingImpl<TAG>& encoder, bool wantUnsignedPortionOnly = false) const;
76 
95  const Block&
96  wireEncode(EncodingBuffer& encoder, span<const uint8_t> signature) const;
97 
101  const Block&
102  wireEncode() const;
103 
106  void
107  wireDecode(const Block& wire);
108 
111  bool
112  hasWire() const noexcept
113  {
114  return m_wire.hasWire();
115  }
116 
121  const Name&
122  getFullName() const;
123 
124 public: // Data fields
128  const Name&
129  getName() const noexcept
130  {
131  return m_name;
132  }
133 
138  Data&
139  setName(const Name& name);
140 
144  const MetaInfo&
145  getMetaInfo() const noexcept
146  {
147  return m_metaInfo;
148  }
149 
154  Data&
155  setMetaInfo(const MetaInfo& metaInfo);
156 
160  bool
161  hasContent() const noexcept
162  {
163  return m_content.isValid();
164  }
165 
179  const Block&
180  getContent() const noexcept
181  {
182  return m_content;
183  }
184 
193  Data&
194  setContent(const Block& block);
195 
201  Data&
202  setContent(span<const uint8_t> value);
203 
209  Data&
210  setContent(ConstBufferPtr value);
211 
217  Data&
218  unsetContent();
219 
223  const SignatureInfo&
224  getSignatureInfo() const noexcept
225  {
226  return m_signatureInfo;
227  }
228 
238  Data&
240 
244  const Block&
245  getSignatureValue() const noexcept
246  {
247  return m_signatureValue;
248  }
249 
260  Data&
261  setSignatureValue(span<const uint8_t> value);
262 
273  Data&
275 
280  InputBuffers
281  extractSignedRanges() const;
282 
283 public: // MetaInfo fields
284  uint32_t
286  {
287  return m_metaInfo.getType();
288  }
289 
290  Data&
291  setContentType(uint32_t type);
292 
295  {
296  return m_metaInfo.getFreshnessPeriod();
297  }
298 
299  Data&
300  setFreshnessPeriod(time::milliseconds freshnessPeriod);
301 
302  const optional<name::Component>&
304  {
305  return m_metaInfo.getFinalBlock();
306  }
307 
308  Data&
309  setFinalBlock(optional<name::Component> finalBlockId);
310 
311 public: // SignatureInfo fields
316  int32_t
317  getSignatureType() const noexcept
318  {
319  return m_signatureInfo.getSignatureType();
320  }
321 
325  optional<KeyLocator>
326  getKeyLocator() const noexcept
327  {
328  return m_signatureInfo.hasKeyLocator() ? make_optional(m_signatureInfo.getKeyLocator()) : nullopt;
329  }
330 
331 protected:
335  void
336  resetWire();
337 
338 private:
339  Name m_name;
340  MetaInfo m_metaInfo;
341  Block m_content;
342  SignatureInfo m_signatureInfo;
343  Block m_signatureValue;
344 
345  mutable Block m_wire;
346  mutable Name m_fullName; // cached FullName computed from m_wire
347 };
348 
349 #ifndef DOXYGEN
350 extern template size_t
351 Data::wireEncode<encoding::EncoderTag>(EncodingBuffer&, bool) const;
352 
353 extern template size_t
354 Data::wireEncode<encoding::EstimatorTag>(EncodingEstimator&, bool) const;
355 #endif
356 
357 std::ostream&
358 operator<<(std::ostream& os, const Data& data);
359 
360 bool
361 operator==(const Data& lhs, const Data& rhs);
362 
363 inline bool
364 operator!=(const Data& lhs, const Data& rhs)
365 {
366  return !(lhs == rhs);
367 }
368 
369 } // namespace ndn
370 
371 #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:221
bool isValid() const noexcept
Check if the Block is valid.
Definition: block.hpp:192
Represents a Data packet.
Definition: data.hpp:39
int32_t getSignatureType() const noexcept
Get the SignatureType.
Definition: data.hpp:317
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:320
Data & setContent(const Block &block)
Set Content from a Block.
Definition: data.cpp:246
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 the MetaInfo element.
Definition: data.hpp:145
bool hasWire() const noexcept
Check if this instance has cached wire encoding.
Definition: data.hpp:112
void resetWire()
Clear wire encoding and cached FullName.
Definition: data.cpp:221
optional< KeyLocator > getKeyLocator() const noexcept
Get the KeyLocator element.
Definition: data.hpp:326
Data & setFreshnessPeriod(time::milliseconds freshnessPeriod)
Definition: data.cpp:344
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:354
bool hasContent() const noexcept
Return whether this Data has a Content element.
Definition: data.hpp:161
Data & setSignatureValue(span< const uint8_t > value)
Set SignatureValue by copying from a contiguous sequence of bytes.
Definition: data.cpp:300
const SignatureInfo & getSignatureInfo() const noexcept
Get the SignatureInfo element.
Definition: data.hpp:224
const Name & getName() const noexcept
Get the data name.
Definition: data.hpp:129
const Block & getSignatureValue() const noexcept
Get the SignatureValue element.
Definition: data.hpp:245
Data & setSignatureInfo(const SignatureInfo &info)
Set the SignatureInfo element.
Definition: data.cpp:292
time::milliseconds getFreshnessPeriod() const
Definition: data.hpp:294
const optional< name::Component > & getFinalBlock() const
Definition: data.hpp:303
uint32_t getContentType() const
Definition: data.hpp:285
Data & setName(const Name &name)
Set the data name.
Definition: data.cpp:228
const Block & getContent() const noexcept
Get the Content element.
Definition: data.hpp:180
Data & setContentType(uint32_t type)
Definition: data.cpp:334
Data & setMetaInfo(const MetaInfo &metaInfo)
Set the MetaInfo element.
Definition: data.cpp:238
Data & unsetContent()
Remove the Content element.
Definition: data.cpp:284
A MetaInfo holds the meta info which is signed inside the Data packet.
Definition: meta-info.hpp:62
const optional< name::Component > & getFinalBlock() const
Return the value of FinalBlockId.
Definition: meta-info.hpp:124
time::milliseconds getFreshnessPeriod() const
Return the value of FreshnessPeriod.
Definition: meta-info.hpp:110
uint32_t getType() const
Return the value of ContentType.
Definition: meta-info.hpp:94
Represents an absolute name.
Definition: name.hpp:44
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:27
EncodingImpl< EstimatorTag > EncodingEstimator
EncodingImpl< EncoderTag > EncodingBuffer
boost::chrono::milliseconds milliseconds
Definition: time.hpp:48
@ 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:364
shared_ptr< const Buffer > ConstBufferPtr
Definition: buffer.hpp:139
bool operator==(const Data &lhs, const Data &rhs)
Definition: data.cpp:364
std::ostream & operator<<(std::ostream &os, const Data &data)
Definition: data.cpp:374
SignatureInfo info