ndn-embeds 0.1.0
Lightweight NDN protocol stack for embedded systems
Loading...
Searching...
No Matches
interest.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
20class TlvEncoder;
21class TlvDecoder;
22
50class Interest {
51public:
58 Interest() = default;
59
64 explicit Interest(const Name& name);
65
79 static Result<Interest> fromWire(const uint8_t* buf, size_t len);
94 Error encode(uint8_t* buf, size_t bufSize, size_t& encodedLen) const;
105 const Name& name() const { return name_; }
106
111 Name& name() { return name_; }
112
118 Interest& setName(const Name& name);
119
125 Error setName(std::string_view uri);
139 std::optional<uint32_t> nonce() const { return nonce_; }
140
146 Interest& setNonce(uint32_t nonce);
147
167 uint32_t lifetime() const { return lifetime_; }
168
174 Interest& setLifetime(uint32_t lifetimeMs);
189 std::optional<uint8_t> hopLimit() const { return hopLimit_; }
190
196 Interest& setHopLimit(uint8_t limit);
197
220 bool canBePrefix() const { return canBePrefix_; }
221
242 bool mustBeFresh() const { return mustBeFresh_; }
243
266
272 Error addForwardingHint(std::string_view uri);
273
278 size_t forwardingHintCount() const { return fwHintCount_; }
279
285 const Name* forwardingHint(size_t index) const;
286
291
296 bool hasForwardingHint() const { return fwHintCount_ > 0; }
307 const uint8_t* applicationParameters() const {
308 return appParamsLen_ > 0 ? appParams_ : nullptr;
309 }
310
315 size_t applicationParametersSize() const { return appParamsLen_; }
316
326 Interest& setApplicationParameters(const uint8_t* params, size_t len);
337 bool isSigned() const { return signatureSize_ > 0; }
338
343 SignatureType signatureType() const { return signatureType_; }
344
352 const uint8_t* signatureNonce() const { return hasSignatureNonce_ ? signatureNonce_ : nullptr; }
353
359 std::optional<uint64_t> signatureTime() const { return signatureTime_; }
360
369 std::optional<uint64_t> signatureSeqNum() const { return signatureSeqNum_; }
370
376 Interest& setSignatureSeqNum(uint64_t seqNum);
377
385 const Name* keyLocator() const { return hasKeyLocator_ ? &keyLocator_ : nullptr; }
386
391 bool hasKeyLocator() const { return hasKeyLocator_; }
392
399
405
410 const uint8_t* signatureValue() const { return signatureValue_.data(); }
411
416 size_t signatureValueSize() const { return signatureSize_; }
417
427
438 Error signWithHmac(const uint8_t* key, size_t keyLen);
439
445 bool verifyDigestSha256() const;
446
454 bool verifyHmac(const uint8_t* key, size_t keyLen) const;
455
466 Error signWithEcdsa(const uint8_t* privKey);
467
474 bool verifyEcdsa(const uint8_t* pubKey) const;
477private:
488 Error encodeSignedPortion(uint8_t* buf, size_t bufSize, size_t& encodedLen) const;
489
493 void generateSignatureNonce();
494
500 Error encodeInterestSignatureInfo(TlvEncoder& encoder) const;
501
509 static Error parseSignatureInfo(TlvDecoder& decoder, size_t elemLen, Interest& interest);
510
512 static constexpr size_t APP_PARAMS_MAX_SIZE = 200;
513
515 static constexpr size_t SIGNATURE_NONCE_SIZE = 8;
516
518 static constexpr size_t FW_HINT_MAX_COUNT = LINK_MAX_DELEGATIONS;
519
520 Name name_;
521 std::optional<uint32_t> nonce_;
522 uint32_t lifetime_ = INTEREST_DEFAULT_LIFETIME_MS;
523 std::optional<uint8_t> hopLimit_;
524 bool canBePrefix_ = false;
525 bool mustBeFresh_ = false;
526 std::array<Name, FW_HINT_MAX_COUNT> fwHints_{};
527 size_t fwHintCount_ = 0;
528 uint8_t appParams_[APP_PARAMS_MAX_SIZE] = {};
529 size_t appParamsLen_ = 0;
530
531 // Signature-related fields
532 SignatureType signatureType_ = SignatureType::DigestSha256;
533 uint8_t signatureNonce_[SIGNATURE_NONCE_SIZE] = {};
534 bool hasSignatureNonce_ = false;
535 std::optional<uint64_t> signatureTime_;
536 std::optional<uint64_t> signatureSeqNum_;
537 Name keyLocator_;
538 bool hasKeyLocator_ = false;
539 std::array<uint8_t, SIGNATURE_MAX_SIZE> signatureValue_{};
540 size_t signatureSize_ = 0;
541};
542
543} // namespace ndn
NDN Interest packet.
Definition interest.hpp:50
Error signWithDigestSha256()
Sign with DigestSha256.
Definition interest.cpp:196
const uint8_t * signatureNonce() const
Get the signature nonce.
Definition interest.hpp:352
bool verifyHmac(const uint8_t *key, size_t keyLen) const
Verify an HMAC-SHA256 signature.
Definition interest.cpp:273
uint32_t lifetime() const
Get the InterestLifetime.
Definition interest.hpp:167
Interest & setLifetime(uint32_t lifetimeMs)
Set the InterestLifetime.
Definition interest.cpp:37
static Result< Interest > fromWire(const uint8_t *buf, size_t len)
Decode an Interest from TLV wire format.
Definition interest.cpp:580
Interest & setCanBePrefix(bool canBePrefix)
Set the CanBePrefix flag.
Definition interest.cpp:54
bool mustBeFresh() const
Get the MustBeFresh flag.
Definition interest.hpp:242
std::optional< uint8_t > hopLimit() const
Get the HopLimit.
Definition interest.hpp:189
const uint8_t * signatureValue() const
Get a pointer to the signature value.
Definition interest.hpp:410
Error signWithEcdsa(const uint8_t *privKey)
Sign with ECDSA P-256.
Definition interest.cpp:303
Interest & setApplicationParameters(const uint8_t *params, size_t len)
Set ApplicationParameters.
Definition interest.cpp:91
Error addForwardingHint(const Name &name)
Add a ForwardingHint.
Definition interest.cpp:64
Interest & setSignatureSeqNum(uint64_t seqNum)
Set the signature sequence number.
Definition interest.cpp:98
bool canBePrefix() const
Get the CanBePrefix flag.
Definition interest.hpp:220
bool isSigned() const
Check if the Interest is signed.
Definition interest.hpp:337
Interest()=default
Default constructor.
Interest & setMustBeFresh(bool mustBeFresh)
Set the MustBeFresh flag.
Definition interest.cpp:59
Error encode(uint8_t *buf, size_t bufSize, size_t &encodedLen) const
Encode the Interest to TLV wire format.
Definition interest.cpp:350
Name & name()
Get the Name (reference)
Definition interest.hpp:111
const Name * forwardingHint(size_t index) const
Get a ForwardingHint by index.
Definition interest.cpp:80
SignatureType signatureType() const
Get the signature type.
Definition interest.hpp:343
const Name * keyLocator() const
Get the KeyLocator.
Definition interest.hpp:385
size_t signatureValueSize() const
Get the size of the signature value.
Definition interest.hpp:416
size_t applicationParametersSize() const
Get the size of ApplicationParameters.
Definition interest.hpp:315
bool verifyEcdsa(const uint8_t *pubKey) const
Verify an ECDSA P-256 signature.
Definition interest.cpp:326
void clearForwardingHints()
Clear all ForwardingHints.
Definition interest.cpp:87
Interest & setKeyLocator(const Name &name)
Set the KeyLocator.
Definition interest.cpp:103
Interest & setNonce(uint32_t nonce)
Set the Nonce.
Definition interest.cpp:27
bool hasForwardingHint() const
Check if ForwardingHints are set.
Definition interest.hpp:296
Interest & generateNonce()
Generate and set a random Nonce.
Definition interest.cpp:32
const uint8_t * applicationParameters() const
Get the ApplicationParameters.
Definition interest.hpp:307
bool verifyDigestSha256() const
Verify a DigestSha256 signature.
Definition interest.cpp:246
bool hasKeyLocator() const
Check if KeyLocator is set.
Definition interest.hpp:391
Interest & clearKeyLocator()
Clear the KeyLocator.
Definition interest.cpp:109
Interest & setName(const Name &name)
Set the Name (supports method chaining)
Definition interest.cpp:13
std::optional< uint64_t > signatureTime() const
Get the signature time.
Definition interest.hpp:359
std::optional< uint32_t > nonce() const
Get the Nonce.
Definition interest.hpp:139
size_t forwardingHintCount() const
Get the number of ForwardingHints.
Definition interest.hpp:278
Interest & setHopLimit(uint8_t limit)
Set the HopLimit.
Definition interest.cpp:42
Error signWithHmac(const uint8_t *key, size_t keyLen)
Sign with HMAC-SHA256.
Definition interest.cpp:219
const Name & name() const
Get the Name (const reference)
Definition interest.hpp:105
std::optional< uint64_t > signatureSeqNum() const
Get the signature sequence number.
Definition interest.hpp:369
Interest & decrementHopLimit()
Decrement the HopLimit by 1.
Definition interest.cpp:47
NDN Name class.
Definition name.hpp:64
TLV decoder.
Definition tlv.hpp:235
TLV encoder.
Definition tlv.hpp:116
Common definitions for the NDN protocol stack.
constexpr uint32_t INTEREST_DEFAULT_LIFETIME_MS
Default Interest lifetime (milliseconds)
Definition common.hpp:125
constexpr size_t LINK_MAX_DELEGATIONS
Maximum number of Names in a Link.
Definition common.hpp:89
Error
Error codes.
Definition common.hpp:24
NDN Name class.
constexpr uint32_t Name
Name.
Definition tlv.hpp:34
NDN signature types and constants.
SignatureType
Signature type.
Definition signature.hpp:22
Result type template.
Definition common.hpp:147