diff options
Diffstat (limited to 'libtransport/src/core/interest.cc')
-rw-r--r-- | libtransport/src/core/interest.cc | 205 |
1 files changed, 154 insertions, 51 deletions
diff --git a/libtransport/src/core/interest.cc b/libtransport/src/core/interest.cc index 9ee662615..c3eb7c379 100644 --- a/libtransport/src/core/interest.cc +++ b/libtransport/src/core/interest.cc @@ -1,5 +1,5 @@ /* - * Copyright (c) 2017-2019 Cisco and/or its affiliates. + * Copyright (c) 2021-2022 Cisco and/or its affiliates. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at: @@ -21,7 +21,9 @@ extern "C" { #ifndef _WIN32 TRANSPORT_CLANG_DISABLE_WARNING("-Wextern-c-compat") #endif +#include <hicn/base.h> #include <hicn/hicn.h> +#include <hicn/interest_manifest.h> } #include <cstring> @@ -31,36 +33,32 @@ namespace transport { namespace core { -Interest::Interest(const Name &interest_name, Packet::Format format) - : Packet(format) { - if (hicn_interest_set_name(format_, packet_start_, - interest_name.getConstStructReference()) < 0) { +Interest::Interest(const Name &interest_name, Packet::Format format, + std::size_t additional_header_size) + : Packet(HICN_PACKET_TYPE_INTEREST, format, additional_header_size) { + if (hicn_interest_set_name(&pkbuf_, + &interest_name.getConstStructReference()) < 0) { throw errors::MalformedPacketException(); } - if (hicn_interest_get_name(format_, packet_start_, - name_.getStructReference()) < 0) { + if (hicn_interest_get_name(&pkbuf_, &name_.getStructReference()) < 0) { throw errors::MalformedPacketException(); } } +Interest::Interest(hicn_packet_format_t format, + std::size_t additional_header_size) + : Interest( #ifdef __ANDROID__ -Interest::Interest(hicn_format_t format) : Interest(Name("0::0|0"), format) {} + Name("0::0|0"), #else -Interest::Interest(hicn_format_t format) : Interest(base_name, format) {} + base_name, #endif - -Interest::Interest(const uint8_t *buffer, std::size_t size) - : Packet(buffer, size) { - if (hicn_interest_get_name(format_, packet_start_, - name_.getStructReference()) < 0) { - throw errors::MalformedPacketException(); - } + format, additional_header_size) { } -Interest::Interest(MemBufPtr &&buffer) : Packet(std::move(buffer)) { - if (hicn_interest_get_name(format_, packet_start_, - name_.getStructReference()) < 0) { +Interest::Interest(MemBuf &&buffer) : Packet(std::move(buffer)) { + if (hicn_interest_get_name(&pkbuf_, &name_.getStructReference()) < 0) { throw errors::MalformedPacketException(); } } @@ -70,15 +68,21 @@ Interest::Interest(Interest &&other_interest) name_ = std::move(other_interest.name_); } -Interest::~Interest() {} +Interest::Interest(const Interest &other_interest) : Packet(other_interest) { + name_ = other_interest.name_; +} + +Interest &Interest::operator=(const Interest &other) { + return (Interest &)Packet::operator=(other); +} + +Interest::~Interest() = default; const Name &Interest::getName() const { - if (!name_) { - if (hicn_interest_get_name(format_, packet_start_, - (hicn_name_t *)name_.getConstStructReference()) < - 0) { - throw errors::MalformedPacketException(); - } + if (!name_ && + hicn_interest_get_name( + &pkbuf_, (hicn_name_t *)&name_.getConstStructReference()) < 0) { + throw errors::MalformedPacketException(); } return name_; @@ -87,41 +91,27 @@ const Name &Interest::getName() const { Name &Interest::getWritableName() { return const_cast<Name &>(getName()); } void Interest::setName(const Name &name) { - if (hicn_interest_set_name(format_, packet_start_, - name.getConstStructReference()) < 0) { - throw errors::RuntimeException("Error setting interest name."); - } - - if (hicn_interest_get_name(format_, packet_start_, - name_.getStructReference()) < 0) { - throw errors::MalformedPacketException(); - } -} - -void Interest::setName(Name &&name) { - if (hicn_interest_set_name(format_, packet_start_, - name.getStructReference()) < 0) { + if (hicn_interest_set_name(&pkbuf_, &name.getConstStructReference()) < 0) { throw errors::RuntimeException("Error setting interest name."); } - if (hicn_interest_get_name(format_, packet_start_, - name_.getStructReference()) < 0) { + if (hicn_interest_get_name(&pkbuf_, &name_.getStructReference()) < 0) { throw errors::MalformedPacketException(); } } -void Interest::setLocator(const ip_address_t &ip_address) { - if (hicn_interest_set_locator(format_, packet_start_, &ip_address) < 0) { +void Interest::setLocator(const hicn_ip_address_t &ip_address) { + if (hicn_interest_set_locator(&pkbuf_, &ip_address) < 0) { throw errors::RuntimeException("Error setting interest locator."); } return; } -ip_address_t Interest::getLocator() const { - ip_address_t ip; +hicn_ip_address_t Interest::getLocator() const { + hicn_ip_address_t ip; - if (hicn_interest_get_locator(format_, packet_start_, &ip) < 0) { + if (hicn_interest_get_locator(&pkbuf_, &ip) < 0) { throw errors::RuntimeException("Error getting interest locator."); } @@ -129,7 +119,7 @@ ip_address_t Interest::getLocator() const { } void Interest::setLifetime(uint32_t lifetime) { - if (hicn_interest_set_lifetime(packet_start_, lifetime) < 0) { + if (hicn_interest_set_lifetime(&pkbuf_, lifetime) < 0) { throw errors::MalformedPacketException(); } } @@ -137,7 +127,7 @@ void Interest::setLifetime(uint32_t lifetime) { uint32_t Interest::getLifetime() const { uint32_t lifetime = 0; - if (hicn_interest_get_lifetime(packet_start_, &lifetime) < 0) { + if (hicn_interest_get_lifetime(&pkbuf_, &lifetime) < 0) { throw errors::MalformedPacketException(); } @@ -145,11 +135,124 @@ uint32_t Interest::getLifetime() const { } void Interest::resetForHash() { - if (hicn_interest_reset_for_hash( - format_, reinterpret_cast<hicn_header_t *>(packet_start_)) < 0) { + if (hicn_interest_reset_for_hash(&pkbuf_) < 0) { throw errors::RuntimeException( "Error resetting interest fields for hash computation."); } + + // Reset request bitmap in manifest + if (hasManifest()) { + auto int_manifest_header = + (interest_manifest_header_t *)(writableData() + headerSize()); + memset(int_manifest_header->request_bitmap, 0, BITMAP_SIZE * sizeof(u32)); + } +} + +bool Interest::hasManifest() const { + return (getPayloadType() == PayloadType::MANIFEST); +} + +void Interest::appendSuffix(std::uint32_t suffix) { + if (TRANSPORT_EXPECT_FALSE(suffix_set_.empty())) { + setPayloadType(PayloadType::MANIFEST); + } + + suffix_set_.emplace(suffix); +} + +void Interest::encodeSuffixes() { + if (!hasManifest()) { + return; + } + + // We assume interest does not hold signature for the moment. + auto int_manifest_header = + (interest_manifest_header_t *)(writableData() + headerSize()); + + interest_manifest_init(int_manifest_header, name_.getSuffix()); + for (auto it = suffix_set_.begin(); it != suffix_set_.end(); it++) { + interest_manifest_add_suffix(int_manifest_header, *it); + } + + std::size_t additional_length = + sizeof(interest_manifest_header_t) + + int_manifest_header->n_suffixes * sizeof(uint32_t); + + append(additional_length); + updateLength(); +} + +void Interest::serializeSuffixes() { + if (!hasManifest()) { + return; + } + + // We assume interest does not hold signature for the moment. + auto int_manifest_header = + (interest_manifest_header_t *)(writableData() + headerSize()); + // Serialize interest manifest + interest_manifest_serialize(int_manifest_header); +} + +void Interest::deserializeSuffixes() { + if (!hasManifest()) { + return; + } + + // We assume interest does not hold signature for the moment. + auto int_manifest_header = + (interest_manifest_header_t *)(writableData() + headerSize()); + // Serialize interest manifest + interest_manifest_deserialize(int_manifest_header); +} + +uint32_t *Interest::firstSuffix() { + if (!hasManifest()) { + return nullptr; + } + + auto ret = (interest_manifest_header_t *)(writableData() + headerSize()); + ret += 1; + + return (uint32_t *)ret; +} + +uint32_t Interest::numberOfSuffixes() { + if (!hasManifest()) { + return 0; + } + + auto header = (interest_manifest_header_t *)(writableData() + headerSize()); + + return header->n_suffixes; +} + +hicn_uword *Interest::getRequestBitmap() { + if (!hasManifest()) return nullptr; + + auto header = (interest_manifest_header_t *)(writableData() + headerSize()); + return header->request_bitmap; +} + +interest_manifest_header_t *Interest::getIntManifestHeader() { + if (!hasManifest()) return nullptr; + + auto header = (interest_manifest_header_t *)(writableData() + headerSize()); + return header; +}; + +void Interest::setRequestBitmap(const uint32_t *request_bitmap) { + if (!hasManifest()) return; + + auto header = (interest_manifest_header_t *)(writableData() + headerSize()); + memcpy(header->request_bitmap, request_bitmap, + BITMAP_SIZE * sizeof(uint32_t)); +} + +bool Interest::isValid() { + if (!hasManifest()) return true; + auto header = (interest_manifest_header_t *)(writableData() + headerSize()); + return interest_manifest_is_valid(header, payloadSize()); } } // end namespace core |