26 #include <boost/scope_exit.hpp> 34 BOOST_CONCEPT_ASSERT((boost::EqualityComparable<Interest>));
35 BOOST_CONCEPT_ASSERT((WireEncodable<Interest>));
36 BOOST_CONCEPT_ASSERT((WireEncodableWithEncodingBuffer<Interest>));
37 BOOST_CONCEPT_ASSERT((WireDecodable<Interest>));
38 static_assert(std::is_base_of<tlv::Error, Interest::Error>::value,
39 "Interest::Error must inherit from tlv::Error");
41 #ifdef NDN_CXX_HAVE_TESTS 42 bool Interest::s_errorIfCanBePrefixUnset =
true;
43 #endif // NDN_CXX_HAVE_TESTS 44 boost::logic::tribool Interest::s_defaultCanBePrefix = boost::logic::indeterminate;
48 , m_isCanBePrefixSet(false)
49 , m_interestLifetime(lifetime)
51 if (lifetime < time::milliseconds::zero()) {
52 BOOST_THROW_EXCEPTION(std::invalid_argument(
"InterestLifetime must be >= 0"));
55 if (!boost::logic::indeterminate(s_defaultCanBePrefix)) {
61 : m_isCanBePrefixSet(true)
68 template<encoding::Tag TAG>
72 static bool hasDefaultCanBePrefixWarning =
false;
73 if (!m_isCanBePrefixSet) {
74 if (!hasDefaultCanBePrefixWarning) {
75 std::cerr <<
"WARNING: Interest.CanBePrefix will be set to 0 in the near future. " 76 <<
"Please declare a preferred setting via Interest::setDefaultCanBePrefix.";
77 hasDefaultCanBePrefixWarning =
true;
79 #ifdef NDN_CXX_HAVE_TESTS 80 if (s_errorIfCanBePrefixUnset) {
81 BOOST_THROW_EXCEPTION(std::logic_error(
"Interest.CanBePrefix is unset"));
83 #endif // NDN_CXX_HAVE_TESTS 87 return encode03(encoder);
90 return encode02(encoder);
94 template<encoding::Tag TAG>
96 Interest::encode02(EncodingImpl<TAG>& encoder)
const 98 size_t totalLength = 0;
124 totalLength += encoder.prependByteArrayBlock(
tlv::Nonce, reinterpret_cast<uint8_t*>(&nonce),
sizeof(nonce));
134 totalLength += encoder.prependVarNumber(totalLength);
139 template<encoding::Tag TAG>
141 Interest::encode03(EncodingImpl<TAG>& encoder)
const 143 size_t totalLength = 0;
174 totalLength += encoder.prependByteArrayBlock(
tlv::Nonce, reinterpret_cast<uint8_t*>(&nonce),
sizeof(nonce));
194 totalLength += encoder.prependVarNumber(totalLength);
224 BOOST_THROW_EXCEPTION(
Error(
"expecting Interest element, got " +
to_string(m_wire.
type())));
234 m_isCanBePrefixSet =
true;
263 if (element->value_size() !=
sizeof(nonce)) {
264 BOOST_THROW_EXCEPTION(
Error(
"Nonce element is malformed"));
266 std::memcpy(&nonce, element->value(),
sizeof(nonce));
308 bool hasName =
false;
309 m_selectors =
Selectors().setMaxSuffixComponents(1);
313 m_parameters =
Block();
317 switch (element.type()) {
319 if (lastElement >= 1) {
320 BOOST_THROW_EXCEPTION(
Error(
"Name element is out of order"));
324 if (m_name.
empty()) {
325 BOOST_THROW_EXCEPTION(
Error(
"Name has zero name components"));
331 if (lastElement >= 2) {
332 BOOST_THROW_EXCEPTION(
Error(
"CanBePrefix element is out of order"));
334 if (element.value_size() != 0) {
335 BOOST_THROW_EXCEPTION(
Error(
"CanBePrefix element has non-zero TLV-LENGTH"));
342 if (lastElement >= 3) {
343 BOOST_THROW_EXCEPTION(
Error(
"MustBeFresh element is out of order"));
345 if (element.value_size() != 0) {
346 BOOST_THROW_EXCEPTION(
Error(
"MustBeFresh element has non-zero TLV-LENGTH"));
353 if (lastElement >= 4) {
354 BOOST_THROW_EXCEPTION(
Error(
"ForwardingHint element is out of order"));
361 if (lastElement >= 5) {
362 BOOST_THROW_EXCEPTION(
Error(
"Nonce element is out of order"));
365 if (element.value_size() !=
sizeof(nonce)) {
366 BOOST_THROW_EXCEPTION(
Error(
"Nonce element is malformed"));
368 std::memcpy(&nonce, element.value(),
sizeof(nonce));
374 if (lastElement >= 6) {
375 BOOST_THROW_EXCEPTION(
Error(
"InterestLifetime element is out of order"));
382 if (lastElement >= 7) {
385 if (element.value_size() != 1) {
386 BOOST_THROW_EXCEPTION(
Error(
"HopLimit element is malformed"));
393 if (lastElement >= 8) {
394 BOOST_THROW_EXCEPTION(
Error(
"Parameters element is out of order"));
396 m_parameters = element;
402 BOOST_THROW_EXCEPTION(
Error(
"unrecognized element of critical type " +
411 BOOST_THROW_EXCEPTION(
Error(
"Name element is missing"));
418 std::ostringstream os;
455 size_t interestNameLength = m_name.
size();
457 size_t fullNameLength = dataName.
size() + 1;
461 size_t minSuffixComponents = hasMinSuffixComponents ?
463 if (!(interestNameLength + minSuffixComponents <= fullNameLength))
468 if (hasMaxSuffixComponents &&
473 if (interestNameLength == fullNameLength) {
493 if (interestNameLength == fullNameLength - 1) {
521 if (!publisherPublicKeyLocator.
empty()) {
528 if (publisherPublicKeyLocator.
wireEncode() != *it) {
570 uint32_t newNonce = oldNonce;
571 while (newNonce == oldNonce)
580 if (lifetime < time::milliseconds::zero()) {
581 BOOST_THROW_EXCEPTION(std::invalid_argument(
"InterestLifetime must be >= 0"));
583 m_interestLifetime = lifetime;
591 m_forwardingHint = value;
600 m_parameters = parameters;
628 m_parameters =
Block();
638 bool wasCanBePrefixSetOnLhs = lhs.m_isCanBePrefixSet;
639 bool wasCanBePrefixSetOnRhs = rhs.m_isCanBePrefixSet;
640 lhs.m_isCanBePrefixSet =
true;
641 rhs.m_isCanBePrefixSet =
true;
642 BOOST_SCOPE_EXIT_ALL(&) {
643 lhs.m_isCanBePrefixSet = wasCanBePrefixSetOnLhs;
644 rhs.m_isCanBePrefixSet = wasCanBePrefixSetOnRhs;
679 os << delim <<
"ndn.Nonce=" << interest.
getNonce();
683 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
Copyright (c) 2013-2017 Regents of the University of California.
const element_container & elements() const
Get container of sub elements.
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.
const Block & getParameters() const
Selectors & setMustBeFresh(bool mustBeFresh)
size_t wireEncode(EncodingImpl< TAG > &encoder) const
Fast encoding or block size estimation.
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 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.
Represents a TLV element of NDN packet format.
Represents an Interest packet.
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 hasParameters() const
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
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.
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.
void reset()
Reset wire buffer of the element.
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
Get TLV-TYPE.
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.
bool hasWire() const
Check if the Block has fully encoded wire.
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 & unsetParameters()
Remove the Parameters element from this Interest.
Represents a Data packet.
bool getMustBeFresh() const
Check whether the MustBeFresh element is present.
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.
Interest & setParameters(const Block ¶meters)
Set parameters from a Block.
shared_ptr< const Buffer > ConstBufferPtr
bool hasNonce() const
Check if the Nonce element is present.