26 #include <boost/scope_exit.hpp> 28 #ifdef NDN_CXX_HAVE_STACKTRACE 29 #include <boost/stacktrace/stacktrace.hpp> 38 BOOST_CONCEPT_ASSERT((boost::EqualityComparable<Interest>));
39 BOOST_CONCEPT_ASSERT((WireEncodable<Interest>));
40 BOOST_CONCEPT_ASSERT((WireEncodableWithEncodingBuffer<Interest>));
41 BOOST_CONCEPT_ASSERT((WireDecodable<Interest>));
42 static_assert(std::is_base_of<tlv::Error, Interest::Error>::value,
43 "Interest::Error must inherit from tlv::Error");
45 #ifdef NDN_CXX_HAVE_TESTS 46 bool Interest::s_errorIfCanBePrefixUnset =
true;
47 #endif // NDN_CXX_HAVE_TESTS 48 boost::logic::tribool Interest::s_defaultCanBePrefix = boost::logic::indeterminate;
52 , m_isCanBePrefixSet(false)
53 , m_interestLifetime(lifetime)
55 if (lifetime < 0_ms) {
56 NDN_THROW(std::invalid_argument(
"InterestLifetime must be >= 0"));
59 if (!boost::logic::indeterminate(s_defaultCanBePrefix)) {
65 : m_isCanBePrefixSet(true)
75 static bool didWarn =
false;
78 std::cerr <<
"WARNING: Interest.CanBePrefix will be set to false in the near future. " 79 <<
"Please declare a preferred setting via Interest::setDefaultCanBePrefix.\n";
80 #ifdef NDN_CXX_HAVE_STACKTRACE 81 if (std::getenv(
"NDN_CXX_VERBOSE_CANBEPREFIX_UNSET_WARNING") !=
nullptr) {
82 std::cerr << boost::stacktrace::stacktrace(2, 64);
88 template<encoding::Tag TAG>
92 if (!m_isCanBePrefixSet) {
94 #ifdef NDN_CXX_HAVE_TESTS 95 if (s_errorIfCanBePrefixUnset) {
96 NDN_THROW(std::logic_error(
"Interest.CanBePrefix is unset"));
98 #endif // NDN_CXX_HAVE_TESTS 102 return encode03(encoder);
105 return encode02(encoder);
109 template<encoding::Tag TAG>
111 Interest::encode02(EncodingImpl<TAG>& encoder)
const 113 size_t totalLength = 0;
138 totalLength += encoder.prependByteArrayBlock(
tlv::Nonce, reinterpret_cast<uint8_t*>(&nonce),
sizeof(nonce));
148 totalLength += encoder.prependVarNumber(totalLength);
153 template<encoding::Tag TAG>
155 Interest::encode03(EncodingImpl<TAG>& encoder)
const 157 size_t totalLength = 0;
187 totalLength += encoder.prependByteArrayBlock(
tlv::Nonce, reinterpret_cast<uint8_t*>(&nonce),
sizeof(nonce));
207 totalLength += encoder.prependVarNumber(totalLength);
247 m_isCanBePrefixSet =
true;
276 if (element->value_size() !=
sizeof(nonce)) {
279 std::memcpy(&nonce, element->value(),
sizeof(nonce));
302 m_forwardingHint = {};
326 if (m_name.
empty()) {
331 m_selectors =
Selectors().setMaxSuffixComponents(1);
334 m_forwardingHint = {};
337 for (++element; element != m_wire.
elements_end(); ++element) {
338 switch (element->type()) {
340 if (lastElement >= 2) {
343 if (element->value_size() != 0) {
351 if (lastElement >= 3) {
354 if (element->value_size() != 0) {
362 if (lastElement >= 4) {
370 if (lastElement >= 5) {
374 if (element->value_size() !=
sizeof(nonce)) {
377 std::memcpy(&nonce, element->value(),
sizeof(nonce));
383 if (lastElement >= 6) {
391 if (lastElement >= 7) {
394 if (element->value_size() != 1) {
402 if (lastElement >= 8) {
405 m_parameters = *element;
422 std::ostringstream os;
459 size_t interestNameLength = m_name.
size();
461 size_t fullNameLength = dataName.
size() + 1;
465 if (!(interestNameLength + minSuffixComponents <= fullNameLength))
470 if (hasMaxSuffixComponents &&
475 if (interestNameLength == fullNameLength) {
495 if (interestNameLength == fullNameLength - 1) {
523 if (!publisherPublicKeyLocator.
empty()) {
530 if (publisherPublicKeyLocator.
wireEncode() != *it) {
572 uint32_t newNonce = oldNonce;
573 while (newNonce == oldNonce)
582 if (lifetime < 0_ms) {
583 NDN_THROW(std::invalid_argument(
"InterestLifetime must be >= 0"));
585 m_interestLifetime = lifetime;
593 m_forwardingHint = value;
601 if (parameters.
empty()) {
605 m_parameters = parameters;
617 if (buffer ==
nullptr && bufferSize != 0) {
618 NDN_THROW(std::invalid_argument(
"ApplicationParameters buffer cannot be nullptr"));
628 if (buffer ==
nullptr) {
629 NDN_THROW(std::invalid_argument(
"ApplicationParameters buffer cannot be nullptr"));
649 bool wasCanBePrefixSetOnLhs = lhs.m_isCanBePrefixSet;
650 bool wasCanBePrefixSetOnRhs = rhs.m_isCanBePrefixSet;
651 lhs.m_isCanBePrefixSet =
true;
652 rhs.m_isCanBePrefixSet =
true;
653 BOOST_SCOPE_EXIT_ALL(&) {
654 lhs.m_isCanBePrefixSet = wasCanBePrefixSetOnLhs;
655 rhs.m_isCanBePrefixSet = wasCanBePrefixSetOnRhs;
689 os << delim <<
"ndn.Nonce=" << interest.
getNonce();
693 os << delim <<
"ndn.Exclude=" << interest.
getExclude();
void wireDecode(const Block &wire)
Decode the input from wire format.
size_t wireEncode(EncodingImpl< TAG > &encoder) const
prepend wire encoding
int getMinSuffixComponents() const
int getMaxSuffixComponents() const
const Name & getName() const
bool matchesName(const Name &name) const
Check if Interest, including selectors, matches the given name.
size_t prependNonNegativeIntegerBlock(EncodingImpl< TAG > &encoder, uint32_t type, uint64_t value)
Prepend a TLV element containing a non-negative integer.
Selectors & setMustBeFresh(bool mustBeFresh)
size_t wireEncode(EncodingImpl< TAG > &encoder) const
Fast encoding or block size estimation.
const Block & getApplicationParameters() const
constexpr bool isCriticalType(uint32_t type)
Determine whether a TLV-TYPE is "critical" for evolvability purpose.
element_const_iterator find(uint32_t type) const
Find the first sub-element of the specified TLV-TYPE.
void refreshNonce()
Change nonce value.
std::ostream & operator<<(std::ostream &os, const Data &data)
Interest(const Name &name=Name(), time::milliseconds lifetime=DEFAULT_INTEREST_LIFETIME)
Construct an Interest with given name and lifetime.
element_container::const_iterator element_const_iterator
bool hasSelectors() const
Check if Interest has any selector present.
const Signature & getSignature() const
Get Signature.
const int DEFAULT_CHILD_SELECTOR
const Block & wireEncode() const
Encode to a Block.
size_t wireEncode(EncodingImpl< TAG > &encoder) const
Fast encoding or block size estimation.
Interest & setApplicationParameters(const Block ¶meters)
Set ApplicationParameters from a Block.
Represents a TLV element of NDN packet format.
Represents an Interest packet.
bool hasWire() const noexcept
Check if the Block contains a fully encoded wire representation.
bool empty() const noexcept
Check if the Block is empty.
std::string toUri() const
Return a URI-like string that represents the Interest.
int getChildSelector() const
uint64_t readNonNegativeInteger(const Block &block)
Read a non-negative integer from a TLV element.
uint32_t getNonce() const
Get nonce value.
Selectors & setMaxSuffixComponents(int maxSuffixComponents)
bool getCanBePrefix() const
Check whether the CanBePrefix element is present.
uint32_t generateWord32()
Generate a non-cryptographically-secure random integer in the range [0, 2^32)
bool isExcluded(const name::Component &comp) const
Check if name component is excluded.
void wireDecode(const Block &block, bool wantSort=true)
decode a DelegationList
const Selectors & getSelectors() const
void reset() noexcept
Reset the Block to a default-constructed state.
size_t prependEmptyBlock(EncodingImpl< TAG > &encoder, uint32_t type)
Prepend an empty TLV element.
Block makeBinaryBlock(uint32_t type, const uint8_t *value, size_t length)
Create a TLV block copying TLV-VALUE from raw buffer.
static void warnOnceCanBePrefixUnset()
Interest & setNonce(uint32_t nonce)
Set nonce value.
size_t wireEncode(EncodingImpl< TAG > &encoder) const
Prepend wire encoding to encoder.
const Exclude & getExclude() const
#define NDN_CXX_DEFINE_WIRE_ENCODE_INSTANTIATIONS(ClassName)
friend bool operator==(const Interest &lhs, const Interest &rhs)
size_t size() const
Get number of components.
Represents an absolute name.
bool isPrefixOf(const Name &other) const
Check if this name is a prefix of another name.
bool matchesData(const Data &data) const
Check if Interest can be satisfied by data.
void parse() const
Parse TLV-VALUE into sub-elements.
uint32_t type() const
Return the TLV-TYPE of the Block.
void wireDecode(const Block &wire)
Decode from wire in NDN Packet Format v0.2 or v0.3.
bool matchesInterest(const Interest &other) const
Check if Interest matches other interest.
const DelegationList & getForwardingHint() const
const Name & getName() const
Get name.
const Block & getInfo() const
Get SignatureInfo as wire format.
bool empty() const
Check if name is empty.
represents a list of Delegations
Interest & setForwardingHint(const DelegationList &value)
Interest & setInterestLifetime(time::milliseconds lifetime)
Set Interest's lifetime.
const KeyLocator & getPublisherPublicKeyLocator() const
const time::milliseconds DEFAULT_INTEREST_LIFETIME
default value for InterestLifetime
std::string to_string(const V &v)
element_const_iterator elements_end() const
Equivalent to elements().end()
time::milliseconds getInterestLifetime() const
const Name & getFullName() const
Get full name including implicit digest.
element_const_iterator elements_begin() const
Equivalent to elements().begin()
void wireDecode(const Block &wire)
Decode name from wire encoding.
Interest & unsetApplicationParameters()
Remove the ApplicationParameters element from this Interest.
Represents a Data packet.
bool getMustBeFresh() const
Check whether the MustBeFresh element is present.
bool hasApplicationParameters() const
const Component & get(ssize_t i) const
Get the component at the given index.
EncodingImpl< EncoderTag > EncodingBuffer
size_t wireEncode(EncodingImpl< TAG > &encoder, uint32_t type=tlv::ForwardingHint) const
encode into wire format
EncodingImpl< EstimatorTag > EncodingEstimator
bool isImplicitSha256Digest() const
Check if the component is ImplicitSha256DigestComponent.
Holds SignatureInfo and SignatureValue in a Data packet.
Interest & setCanBePrefix(bool canBePrefix)
Add or remove CanBePrefix element.
shared_ptr< const Buffer > ConstBufferPtr
bool hasNonce() const
Check if the Nonce element is present.