ndn-embeds 0.1.0
Lightweight NDN protocol stack for embedded systems
Loading...
Searching...
No Matches
ndn::Data Class Reference

NDN Data packet. More...

#include <ndn/data.hpp>

Public Member Functions

 Data ()=default
 Default constructor.
 
 Data (const Name &name)
 Create a Data packet with a specified Name.
 
Encoding
Error encode (uint8_t *buf, size_t bufSize, size_t &encodedLen) const
 Encode the Data packet to TLV wire format.
 
Name field
const Namename () const
 Get the Name (const reference)
 
Namename ()
 Get the Name (reference)
 
DatasetName (const Name &name)
 Set the Name (supports method chaining)
 
Error setName (std::string_view uri)
 Set the Name from a URI string.
 
Content field
const uint8_t * content () const
 Get a pointer to the content data.
 
size_t contentSize () const
 Get the content size.
 
bool hasContent () const
 Check if content is set.
 
Error setContent (const uint8_t *data, size_t size)
 Set binary data as content.
 
Error setContent (std::string_view str)
 Set a string as content.
 
ContentType field
ContentType contentType () const
 Get the content type.
 
DatasetContentType (ContentType type)
 Set the content type.
 
bool isLink () const
 Check if this is a Link Object.
 
FreshnessPeriod field
std::optional< uint32_t > freshnessPeriod () const
 Get the FreshnessPeriod.
 
DatasetFreshnessPeriod (uint32_t periodMs)
 Set the FreshnessPeriod.
 
FinalBlockId field
std::optional< uint64_t > finalBlockId () const
 Get the FinalBlockId.
 
bool hasFinalBlockId () const
 Check if FinalBlockId is set.
 
DatasetFinalBlockId (uint64_t segmentNum)
 Set the FinalBlockId.
 
DataclearFinalBlockId ()
 Clear the FinalBlockId.
 
Signature fields
SignatureType signatureType () const
 Get the signature type.
 
DatasetSignatureType (SignatureType type)
 Set the signature type.
 
const NamekeyLocator () const
 Get the KeyLocator.
 
bool hasKeyLocator () const
 Check if KeyLocator is set.
 
DatasetKeyLocator (const Name &name)
 Set the KeyLocator.
 
DataclearKeyLocator ()
 Clear the KeyLocator.
 
const uint8_t * signatureValue () const
 Get a pointer to the signature value.
 
size_t signatureValueSize () const
 Get the size of the signature value.
 
bool hasSignature () const
 Check if a signature is set.
 
Error signWithDigestSha256 ()
 Sign with DigestSha256.
 
Error signWithHmac (const uint8_t *key, size_t keyLen)
 Sign with HMAC-SHA256.
 
bool verifyDigestSha256 () const
 Verify a DigestSha256 signature.
 
bool verifyHmac (const uint8_t *key, size_t keyLen) const
 Verify an HMAC-SHA256 signature.
 
Error signWithEcdsa (const uint8_t *privKey)
 Sign with ECDSA P-256.
 
bool verifyEcdsa (const uint8_t *pubKey) const
 Verify an ECDSA P-256 signature.
 

Static Public Member Functions

Decoding
static Result< DatafromWire (const uint8_t *buf, size_t len)
 Decode a Data packet from TLV wire format.
 

Detailed Description

NDN Data packet.

A Data packet contains the content returned by a producer in response to an Interest. Only the Name field is required; Content and FreshnessPeriod are optional.

// Create and configure a Data packet
ndn::Data data;
data.setName("/sensor/temperature");
data.setContent("25.5 C");
data.setFreshnessPeriod(10000); // Valid for 10 seconds
// Encode
uint8_t buf[256];
size_t len = 0;
if (data.encode(buf, sizeof(buf), len) == ndn::Error::Success) {
// Send buf over the network
}
// Decode
auto result = ndn::Data::fromWire(buf, len);
if (result.ok()) {
printf("Content: %.*s\n",
(int)result.value.contentSize(),
result.value.content());
}
NDN Data packet.
Definition data.hpp:49
Data & setFreshnessPeriod(uint32_t periodMs)
Set the FreshnessPeriod.
Definition data.cpp:39
Error setContent(const uint8_t *data, size_t size)
Set binary data as content.
Definition data.cpp:26
Error encode(uint8_t *buf, size_t bufSize, size_t &encodedLen) const
Encode the Data packet to TLV wire format.
Definition data.cpp:317
static Result< Data > fromWire(const uint8_t *buf, size_t len)
Decode a Data packet from TLV wire format.
Definition data.cpp:362
Data & setName(const Name &name)
Set the Name (supports method chaining)
Definition data.cpp:12

Definition at line 49 of file data.hpp.

Constructor & Destructor Documentation

◆ Data() [1/2]

ndn::Data::Data ( )
default

Default constructor.

Creates an empty Data packet. Name is empty, Content is empty, and FreshnessPeriod is unset.

◆ Data() [2/2]

ndn::Data::Data ( const Name name)
explicit

Create a Data packet with a specified Name.

Parameters
nameThe Data name

Definition at line 10 of file data.cpp.

Member Function Documentation

◆ clearFinalBlockId()

Data & ndn::Data::clearFinalBlockId ( )

Clear the FinalBlockId.

Returns
Reference to this Data

Definition at line 49 of file data.cpp.

◆ clearKeyLocator()

Data & ndn::Data::clearKeyLocator ( )

Clear the KeyLocator.

Returns
Reference to this Data

Definition at line 70 of file data.cpp.

◆ content()

const uint8_t * ndn::Data::content ( ) const
inline

Get a pointer to the content data.

Returns
Pointer to the content buffer

Definition at line 135 of file data.hpp.

Referenced by ndn::Certificate::fromData(), and ndn::Link::fromData().

◆ contentSize()

size_t ndn::Data::contentSize ( ) const
inline

Get the content size.

Returns
Content size in bytes

Definition at line 141 of file data.hpp.

Referenced by ndn::Certificate::fromData(), and ndn::Link::fromData().

◆ contentType()

ContentType ndn::Data::contentType ( ) const
inline

Get the content type.

Returns
Content type

Definition at line 177 of file data.hpp.

Referenced by ndn::Certificate::fromData(), and ndn::Link::fromData().

◆ encode()

Error ndn::Data::encode ( uint8_t *  buf,
size_t  bufSize,
size_t &  encodedLen 
) const

◆ finalBlockId()

std::optional< uint64_t > ndn::Data::finalBlockId ( ) const
inline

Get the FinalBlockId.

FinalBlockId indicates the last segment number in segmented transfer. The segment number is typically used as the last component of the Name.

Returns
FinalBlockId value if set, nullopt if unset

Definition at line 227 of file data.hpp.

◆ freshnessPeriod()

std::optional< uint32_t > ndn::Data::freshnessPeriod ( ) const
inline

Get the FreshnessPeriod.

FreshnessPeriod indicates the freshness duration of the Data (milliseconds). Within this period, cached responses from the Content Store are possible.

Returns
FreshnessPeriod (milliseconds) if set, nullopt if unset

Definition at line 205 of file data.hpp.

Referenced by ndn::ContentStore::insert().

◆ fromWire()

◆ hasContent()

bool ndn::Data::hasContent ( ) const
inline

Check if content is set.

Returns
true if content is present

Definition at line 147 of file data.hpp.

Referenced by ndn::Certificate::fromData(), and ndn::Link::fromData().

◆ hasFinalBlockId()

bool ndn::Data::hasFinalBlockId ( ) const
inline

Check if FinalBlockId is set.

Returns
true if FinalBlockId is present

Definition at line 233 of file data.hpp.

◆ hasKeyLocator()

bool ndn::Data::hasKeyLocator ( ) const
inline

Check if KeyLocator is set.

Returns
true if KeyLocator is present

Definition at line 283 of file data.hpp.

◆ hasSignature()

bool ndn::Data::hasSignature ( ) const
inline

Check if a signature is set.

Returns
true if a signature is present

Definition at line 314 of file data.hpp.

Referenced by ndn::Certificate::fromData().

◆ isLink()

bool ndn::Data::isLink ( ) const
inline

Check if this is a Link Object.

Returns
true if ContentType::Link

Definition at line 190 of file data.hpp.

◆ keyLocator()

const Name * ndn::Data::keyLocator ( ) const
inline

Get the KeyLocator.

Returns the reference information (Name) of the key used for signing. Required for RSA/ECDSA/Ed25519 signatures, prohibited for DigestSha256.

Returns
KeyLocator Name if set, nullptr if unset

Definition at line 277 of file data.hpp.

◆ name() [1/2]

Name & ndn::Data::name ( )
inline

Get the Name (reference)

Returns
Reference to the Name

Definition at line 110 of file data.hpp.

◆ name() [2/2]

const Name & ndn::Data::name ( ) const
inline

Get the Name (const reference)

Returns
Const reference to the Name

Definition at line 104 of file data.hpp.

Referenced by ndn::Certificate::fromData(), ndn::Link::fromData(), ndn::ContentStore::insert(), ndn::Forwarder::putData(), setKeyLocator(), and setName().

◆ setContent() [1/2]

Error ndn::Data::setContent ( const uint8_t *  data,
size_t  size 
)

Set binary data as content.

Parameters
dataPointer to the content data
sizeData size (bytes)
Returns
Error::Success on success, Error::BufferTooSmall if size exceeds DATA_MAX_CONTENT_SIZE

Definition at line 26 of file data.cpp.

References ndn::DATA_MAX_CONTENT_SIZE.

Referenced by setContent(), ndn::Certificate::toData(), and ndn::Link::toData().

◆ setContent() [2/2]

Error ndn::Data::setContent ( std::string_view  str)

Set a string as content.

Parameters
strContent string
Returns
Error::Success on success, Error::BufferTooSmall if size exceeds DATA_MAX_CONTENT_SIZE

Definition at line 35 of file data.cpp.

References setContent().

◆ setContentType()

Data & ndn::Data::setContentType ( ContentType  type)

Set the content type.

Parameters
typeContent type
Returns
Reference to this Data

Definition at line 54 of file data.cpp.

Referenced by ndn::Certificate::toData(), and ndn::Link::toData().

◆ setFinalBlockId()

Data & ndn::Data::setFinalBlockId ( uint64_t  segmentNum)

Set the FinalBlockId.

Sets the last segment number in segmented transfer.

Parameters
segmentNumLast segment number
Returns
Reference to this Data

Definition at line 44 of file data.cpp.

◆ setFreshnessPeriod()

Data & ndn::Data::setFreshnessPeriod ( uint32_t  periodMs)

Set the FreshnessPeriod.

Parameters
periodMsFreshness period (milliseconds)
Returns
Reference to this Data

Definition at line 39 of file data.cpp.

Referenced by ndn::Certificate::toData().

◆ setKeyLocator()

Data & ndn::Data::setKeyLocator ( const Name name)

Set the KeyLocator.

Parameters
nameName identifying the key
Returns
Reference to this Data

Definition at line 64 of file data.cpp.

References name().

◆ setName() [1/2]

Data & ndn::Data::setName ( const Name name)

Set the Name (supports method chaining)

Parameters
nameName to set
Returns
Reference to this Data

Definition at line 12 of file data.cpp.

References name().

Referenced by ndn::Certificate::toData(), and ndn::Link::toData().

◆ setName() [2/2]

Error ndn::Data::setName ( std::string_view  uri)

Set the Name from a URI string.

Parameters
uriURI string (e.g., "/sensor/temperature")
Returns
Error::Success on success, error code on parse failure

Definition at line 17 of file data.cpp.

References ndn::Name::fromUri().

◆ setSignatureType()

Data & ndn::Data::setSignatureType ( SignatureType  type)

Set the signature type.

Parameters
typeSignature type
Returns
Reference to this Data

Definition at line 59 of file data.cpp.

Referenced by ndn::Certificate::toData().

◆ signatureType()

SignatureType ndn::Data::signatureType ( ) const
inline

Get the signature type.

Returns
Signature type

Definition at line 260 of file data.hpp.

Referenced by ndn::Certificate::fromData().

◆ signatureValue()

const uint8_t * ndn::Data::signatureValue ( ) const
inline

Get a pointer to the signature value.

Returns
Pointer to the signature value buffer

Definition at line 302 of file data.hpp.

Referenced by ndn::Certificate::fromData().

◆ signatureValueSize()

size_t ndn::Data::signatureValueSize ( ) const
inline

Get the size of the signature value.

Returns
Size of the signature value in bytes

Definition at line 308 of file data.hpp.

Referenced by ndn::Certificate::fromData().

◆ signWithDigestSha256()

Error ndn::Data::signWithDigestSha256 ( )

Sign with DigestSha256.

Computes a SHA-256 digest from Name, MetaInfo, Content, and SignatureInfo, and stores it in SignatureValue. signatureType is set to DigestSha256.

Returns
Error::Success on success

Definition at line 169 of file data.cpp.

References ndn::PACKET_MAX_SIZE, and ndn::SHA256_DIGEST_SIZE.

◆ signWithEcdsa()

Error ndn::Data::signWithEcdsa ( const uint8_t *  privKey)

Sign with ECDSA P-256.

Computes an ECDSA signature using the specified private key and stores it in SignatureValue. signatureType is set to SignatureSha256WithEcdsa. It is recommended to set the KeyLocator beforehand.

Parameters
privKeyPrivate key (32 bytes)
Returns
Error::Success on success

Definition at line 272 of file data.cpp.

References ndn::PACKET_MAX_SIZE.

◆ signWithHmac()

Error ndn::Data::signWithHmac ( const uint8_t *  key,
size_t  keyLen 
)

Sign with HMAC-SHA256.

Computes HMAC-SHA256 using the specified key and stores it in SignatureValue. signatureType is set to SignatureHmacWithSha256.

Parameters
keyPointer to the key data
keyLenKey length (bytes)
Returns
Error::Success on success

Definition at line 190 of file data.cpp.

References ndn::HMAC_SHA256_SIZE, and ndn::PACKET_MAX_SIZE.

◆ verifyDigestSha256()

bool ndn::Data::verifyDigestSha256 ( ) const

Verify a DigestSha256 signature.

Returns
true if the signature is valid

Definition at line 215 of file data.cpp.

References ndn::PACKET_MAX_SIZE, and ndn::SHA256_DIGEST_SIZE.

◆ verifyEcdsa()

bool ndn::Data::verifyEcdsa ( const uint8_t *  pubKey) const

Verify an ECDSA P-256 signature.

Parameters
pubKeyPublic key (65 bytes, uncompressed form)
Returns
true if the signature is valid

Definition at line 293 of file data.cpp.

References ndn::ECDSA_P256_SIG_MAX_SIZE, and ndn::PACKET_MAX_SIZE.

◆ verifyHmac()

bool ndn::Data::verifyHmac ( const uint8_t *  key,
size_t  keyLen 
) const

Verify an HMAC-SHA256 signature.

Parameters
keyPointer to the key data
keyLenKey length (bytes)
Returns
true if the signature is valid

Definition at line 242 of file data.cpp.

References ndn::HMAC_SHA256_SIZE, and ndn::PACKET_MAX_SIZE.


The documentation for this class was generated from the following files: