Skip to content

Commit

Permalink
Add cache flush bit to resource records. (#7181)
Browse files Browse the repository at this point in the history
* Add cache flush bit to resource records.

See https://datatracker.ietf.org/doc/html/rfc6762#section-10.2.
Currently, this flag is never set by the code. Just adding as
an option.

* review comments - tests and const function.
  • Loading branch information
cecille authored and pull[bot] committed Jul 3, 2021
1 parent a1e0ca3 commit 7ba3609
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 1 deletion.
4 changes: 4 additions & 0 deletions src/lib/mdns/minimal/core/Constants.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ enum class QType : uint16_t

/// Flag encoded in QCLASS requesting unicast answers
constexpr uint16_t kQClassUnicastAnswerFlag = 0x8000;
// Flag used to indicate receiver should flush cache rather than appending. Used for Response RR's.
// See https://datatracker.ietf.org/doc/html/rfc6762#section-10.2.
constexpr uint16_t kQClassResponseFlushBit = 0x8000;

enum class QClass : uint16_t
{
Expand All @@ -53,6 +56,7 @@ enum class QClass : uint16_t

// Unicast version for the class
IN_UNICAST = IN | kQClassUnicastAnswerFlag,
IN_FLUSH = IN | kQClassResponseFlushBit,
};

enum class ResourceType
Expand Down
10 changes: 9 additions & 1 deletion src/lib/mdns/minimal/records/ResourceRecord.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class ResourceRecord
ResourceRecord & operator=(const ResourceRecord & other) = default;

const FullQName & GetName() const { return mQName; }
QClass GetClass() const { return QClass::IN; }
QClass GetClass() const { return mCacheFlush ? QClass::IN_FLUSH : QClass::IN; }
QType GetType() const { return mType; }

uint32_t GetTtl() const { return mTtl; }
Expand All @@ -48,6 +48,13 @@ class ResourceRecord
return *this;
}

ResourceRecord & SetCacheFlush(bool set)
{
mCacheFlush = set;
return *this;
}
bool GetCacheFlush() const { return mCacheFlush; }

/// Append the given record to the underlying output.
/// Updates header item count on success, does NOT update header on failure.
bool Append(HeaderRef & hdr, ResourceType asType, chip::Encoding::BigEndian::BufferWriter & out) const;
Expand All @@ -62,6 +69,7 @@ class ResourceRecord
QType mType;
uint32_t mTtl = kDefaultTtl;
FullQName mQName;
bool mCacheFlush = false;
};

} // namespace Minimal
Expand Down
18 changes: 18 additions & 0 deletions src/lib/mdns/minimal/records/tests/TestResourceRecord.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -245,13 +245,31 @@ void RecordCount(nlTestSuite * inSuite, void * inContext)
NL_TEST_ASSERT(inSuite, header.GetAdditionalCount() == i + 1);
}
}
void CacheFlushBit(nlTestSuite * inSuite, void * inContext)
{
FakeResourceRecord record("somedata");
// No cache flush bit by default.
NL_TEST_ASSERT(inSuite, record.GetClass() == QClass::IN);
NL_TEST_ASSERT(inSuite, record.GetCacheFlush() == false);

// Check we can set flush bit and the class marker reflects that.
record.SetCacheFlush(true);
NL_TEST_ASSERT(inSuite, record.GetClass() == QClass::IN_FLUSH);
NL_TEST_ASSERT(inSuite, record.GetCacheFlush() == true);

// Check we can unset.
record.SetCacheFlush(false);
NL_TEST_ASSERT(inSuite, record.GetClass() == QClass::IN);
NL_TEST_ASSERT(inSuite, record.GetCacheFlush() == false);
}

const nlTest sTests[] = {
NL_TEST_DEF("CanWriteSimpleRecord", CanWriteSimpleRecord), //
NL_TEST_DEF("CanWriteMultipleRecords", CanWriteMultipleRecords), //
NL_TEST_DEF("RecordOrderIsEnforced", RecordOrderIsEnforced), //
NL_TEST_DEF("ErrorsOutOnSmallBuffers", ErrorsOutOnSmallBuffers), //
NL_TEST_DEF("RecordCount", RecordCount), //
NL_TEST_DEF("CacheFlushBit", CacheFlushBit), //
NL_TEST_SENTINEL() //
};

Expand Down

0 comments on commit 7ba3609

Please sign in to comment.