ndn-embeds 0.1.0
Lightweight NDN protocol stack for embedded systems
Loading...
Searching...
No Matches
data.hpp
Go to the documentation of this file.
1
12#pragma once
13
14#include "ndn/common.hpp"
15#include "ndn/name.hpp"
16#include "ndn/signature.hpp"
17
18namespace ndn {
19
49class Data {
50public:
57 Data() = default;
58
63 explicit Data(const Name& name);
64
78 static Result<Data> fromWire(const uint8_t* buf, size_t len);
93 Error encode(uint8_t* buf, size_t bufSize, size_t& encodedLen) const;
104 const Name& name() const { return name_; }
105
110 Name& name() { return name_; }
111
117 Data& setName(const Name& name);
118
124 Error setName(std::string_view uri);
135 const uint8_t* content() const { return content_.data(); }
136
141 size_t contentSize() const { return contentSize_; }
142
147 bool hasContent() const { return contentSize_ > 0; }
148
157 Error setContent(const uint8_t* data, size_t size);
158
166 Error setContent(std::string_view str);
177 ContentType contentType() const { return contentType_; }
178
185
190 bool isLink() const { return contentType_ == ContentType::Link; }
205 std::optional<uint32_t> freshnessPeriod() const { return freshnessPeriod_; }
206
212 Data& setFreshnessPeriod(uint32_t periodMs);
227 std::optional<uint64_t> finalBlockId() const { return finalBlockId_; }
228
233 bool hasFinalBlockId() const { return finalBlockId_.has_value(); }
234
243 Data& setFinalBlockId(uint64_t segmentNum);
244
260 SignatureType signatureType() const { return signatureType_; }
261
268
277 const Name* keyLocator() const { return hasKeyLocator_ ? &keyLocator_ : nullptr; }
278
283 bool hasKeyLocator() const { return hasKeyLocator_; }
284
290 Data& setKeyLocator(const Name& name);
291
297
302 const uint8_t* signatureValue() const { return signatureValue_.data(); }
303
308 size_t signatureValueSize() const { return signatureSize_; }
309
314 bool hasSignature() const { return signatureSize_ > 0; }
315
325
336 Error signWithHmac(const uint8_t* key, size_t keyLen);
337
343 bool verifyDigestSha256() const;
344
352 bool verifyHmac(const uint8_t* key, size_t keyLen) const;
353
364 Error signWithEcdsa(const uint8_t* privKey);
365
372 bool verifyEcdsa(const uint8_t* pubKey) const;
375private:
386 Error encodeSignedPortion(uint8_t* buf, size_t bufSize, size_t& encodedLen) const;
387
388 Name name_;
389 std::array<uint8_t, DATA_MAX_CONTENT_SIZE> content_{};
390 size_t contentSize_ = 0;
391 ContentType contentType_ = ContentType::Blob;
392 std::optional<uint32_t> freshnessPeriod_;
393 std::optional<uint64_t> finalBlockId_;
394 SignatureType signatureType_ = SignatureType::DigestSha256;
395 Name keyLocator_;
396 bool hasKeyLocator_ = false;
397 std::array<uint8_t, SIGNATURE_MAX_SIZE> signatureValue_{};
398 size_t signatureSize_ = 0;
399};
400
401} // namespace ndn
NDN Data packet.
Definition data.hpp:49
Data & setFreshnessPeriod(uint32_t periodMs)
Set the FreshnessPeriod.
Definition data.cpp:39
Data & setKeyLocator(const Name &name)
Set the KeyLocator.
Definition data.cpp:64
Data & clearKeyLocator()
Clear the KeyLocator.
Definition data.cpp:70
Error signWithEcdsa(const uint8_t *privKey)
Sign with ECDSA P-256.
Definition data.cpp:272
bool isLink() const
Check if this is a Link Object.
Definition data.hpp:190
const Name & name() const
Get the Name (const reference)
Definition data.hpp:104
bool hasContent() const
Check if content is set.
Definition data.hpp:147
Error signWithDigestSha256()
Sign with DigestSha256.
Definition data.cpp:169
size_t contentSize() const
Get the content size.
Definition data.hpp:141
bool hasFinalBlockId() const
Check if FinalBlockId is set.
Definition data.hpp:233
Data & setContentType(ContentType type)
Set the content type.
Definition data.cpp:54
Data & setFinalBlockId(uint64_t segmentNum)
Set the FinalBlockId.
Definition data.cpp:44
bool verifyHmac(const uint8_t *key, size_t keyLen) const
Verify an HMAC-SHA256 signature.
Definition data.cpp:242
const uint8_t * content() const
Get a pointer to the content data.
Definition data.hpp:135
const uint8_t * signatureValue() const
Get a pointer to the signature value.
Definition data.hpp:302
bool hasKeyLocator() const
Check if KeyLocator is set.
Definition data.hpp:283
Name & name()
Get the Name (reference)
Definition data.hpp:110
Error setContent(const uint8_t *data, size_t size)
Set binary data as content.
Definition data.cpp:26
std::optional< uint64_t > finalBlockId() const
Get the FinalBlockId.
Definition data.hpp:227
const Name * keyLocator() const
Get the KeyLocator.
Definition data.hpp:277
bool verifyDigestSha256() const
Verify a DigestSha256 signature.
Definition data.cpp:215
std::optional< uint32_t > freshnessPeriod() const
Get the FreshnessPeriod.
Definition data.hpp:205
Error encode(uint8_t *buf, size_t bufSize, size_t &encodedLen) const
Encode the Data packet to TLV wire format.
Definition data.cpp:317
Data & clearFinalBlockId()
Clear the FinalBlockId.
Definition data.cpp:49
Data & setSignatureType(SignatureType type)
Set the signature type.
Definition data.cpp:59
size_t signatureValueSize() const
Get the size of the signature value.
Definition data.hpp:308
SignatureType signatureType() const
Get the signature type.
Definition data.hpp:260
bool verifyEcdsa(const uint8_t *pubKey) const
Verify an ECDSA P-256 signature.
Definition data.cpp:293
ContentType contentType() const
Get the content type.
Definition data.hpp:177
bool hasSignature() const
Check if a signature is set.
Definition data.hpp:314
static Result< Data > fromWire(const uint8_t *buf, size_t len)
Decode a Data packet from TLV wire format.
Definition data.cpp:362
Data()=default
Default constructor.
Data & setName(const Name &name)
Set the Name (supports method chaining)
Definition data.cpp:12
Error signWithHmac(const uint8_t *key, size_t keyLen)
Sign with HMAC-SHA256.
Definition data.cpp:190
NDN Name class.
Definition name.hpp:64
Common definitions for the NDN protocol stack.
ContentType
Content type.
Definition common.hpp:81
Error
Error codes.
Definition common.hpp:24
NDN Name class.
NDN signature types and constants.
SignatureType
Signature type.
Definition signature.hpp:22
Result type template.
Definition common.hpp:147