From 8f0a8bf572b9b8123121338a31462440bad65857 Mon Sep 17 00:00:00 2001 From: Mauro Sardara Date: Fri, 8 Jul 2022 16:10:13 +0000 Subject: feat: add interest manifest serialization/deserialization Also: add helpers for interest manifest Ref: HICN-738 Signed-off-by: Mauro Sardara Change-Id: Ia531605148e00ccbe446da0f4f2d8caae2b098be Signed-off-by: Mauro Sardara --- .../includes/hicn/transport/core/interest.h | 2 ++ libtransport/src/core/interest.cc | 31 +++++++++------------- libtransport/src/test/test_interest.cc | 7 +++++ 3 files changed, 22 insertions(+), 18 deletions(-) (limited to 'libtransport') diff --git a/libtransport/includes/hicn/transport/core/interest.h b/libtransport/includes/hicn/transport/core/interest.h index 23dc8f75e..716ac6b0a 100644 --- a/libtransport/includes/hicn/transport/core/interest.h +++ b/libtransport/includes/hicn/transport/core/interest.h @@ -92,6 +92,8 @@ class Interest void appendSuffix(std::uint32_t suffix); + void decodeSuffixes(); + void encodeSuffixes(); uint32_t *firstSuffix(); diff --git a/libtransport/src/core/interest.cc b/libtransport/src/core/interest.cc index 0851bfef6..777374b09 100644 --- a/libtransport/src/core/interest.cc +++ b/libtransport/src/core/interest.cc @@ -171,7 +171,7 @@ void Interest::encodeSuffixes() { (interest_manifest_header_t *)(writableData() + headerSize()); int_manifest_header->n_suffixes = (uint32_t)suffix_set_.size(); memset(int_manifest_header->request_bitmap, 0xFFFFFFFF, - BITMAP_SIZE * sizeof(u32)); + BITMAP_SIZE * sizeof(hicn_uword)); uint32_t *suffix = (uint32_t *)(int_manifest_header + 1); for (auto it = suffix_set_.begin(); it != suffix_set_.end(); it++, suffix++) { @@ -181,10 +181,21 @@ void Interest::encodeSuffixes() { std::size_t additional_length = sizeof(interest_manifest_header_t) + int_manifest_header->n_suffixes * sizeof(uint32_t); + + // Serialize interest manifest + interest_manifest_serialize(int_manifest_header); + append(additional_length); updateLength(); } +void Interest::decodeSuffixes() { + if (!hasManifest()) return; + + auto header = (interest_manifest_header_t *)(writableData() + headerSize()); + interest_manifest_deserialize(header); +} + uint32_t *Interest::firstSuffix() { if (!hasManifest()) { return nullptr; @@ -223,24 +234,8 @@ void Interest::setRequestBitmap(const uint32_t *request_bitmap) { bool Interest::isValid() { if (!hasManifest()) return true; - auto header = (interest_manifest_header_t *)(writableData() + headerSize()); - - if (header->n_suffixes == 0 || - header->n_suffixes > MAX_SUFFIXES_IN_MANIFEST) { - std::cerr << "Manifest with invalid number of suffixes " - << header->n_suffixes; - return false; - } - - uint32_t empty_bitmap[BITMAP_SIZE]; - memset(empty_bitmap, 0, sizeof(empty_bitmap)); - if (memcmp(empty_bitmap, header->request_bitmap, sizeof(empty_bitmap)) == 0) { - std::cerr << "Manifest with empty bitmap"; - return false; - } - - return true; + return interest_manifest_is_valid(header, payloadSize()); } } // end namespace core diff --git a/libtransport/src/test/test_interest.cc b/libtransport/src/test/test_interest.cc index 8d00a9c6d..ba63b6c93 100644 --- a/libtransport/src/test/test_interest.cc +++ b/libtransport/src/test/test_interest.cc @@ -255,7 +255,11 @@ TEST_F(InterestTest, AppendSuffixesEncodeAndIterate) { // Encode them in wire format interest.encodeSuffixes(); + // Decode suffixes from wire format + interest.decodeSuffixes(); + // Iterate over them. They should be in order and without repetitions + auto suffix = interest.firstSuffix(); auto n_suffixes = interest.numberOfSuffixes(); @@ -278,6 +282,9 @@ TEST_F(InterestTest, AppendSuffixesWithGaps) { interest.encodeSuffixes(); EXPECT_TRUE(interest.hasManifest()); + // Decode suffixes from wire format + interest.decodeSuffixes(); + // Check first suffix correctness auto suffix = interest.firstSuffix(); EXPECT_NE(suffix, nullptr); -- cgit 1.2.3-korg