From 6d7704c1b497341fd6dd3c27e3f64d0db062ccc2 Mon Sep 17 00:00:00 2001 From: Mauro Sardara Date: Mon, 4 Feb 2019 11:06:18 +0100 Subject: [HICN-11] Rework on transport protocols improving components modularity Change-Id: I6683ec5b494238dc93591c103d25275e89b9f267 Signed-off-by: Mauro Sardara --- .../src/hicn/transport/core/content_object.cc | 2 + .../src/hicn/transport/core/content_object.h | 4 +- .../src/hicn/transport/core/forwarder_interface.h | 5 + libtransport/src/hicn/transport/core/interest.cc | 13 +- libtransport/src/hicn/transport/core/interest.h | 4 +- libtransport/src/hicn/transport/core/manifest.h | 37 +-- .../hicn/transport/core/manifest_format_fixed.cc | 32 +-- .../hicn/transport/core/manifest_format_fixed.h | 3 +- .../transport/core/manifest_format_json_jsoncpp.cc | 244 ----------------- .../transport/core/manifest_format_json_jsoncpp.h | 162 ----------- .../manifest_format_json_libparc_deprecated.cc | 298 --------------------- .../core/manifest_format_json_libparc_deprecated.h | 152 ----------- .../src/hicn/transport/core/manifest_inline.h | 6 +- libtransport/src/hicn/transport/core/packet.cc | 26 +- libtransport/src/hicn/transport/core/packet.h | 15 +- .../hicn/transport/core/raw_socket_connector.cc | 2 +- .../src/hicn/transport/core/raw_socket_connector.h | 2 +- .../hicn/transport/core/tcp_socket_connector.cc | 26 +- .../src/hicn/transport/core/tcp_socket_connector.h | 2 + 19 files changed, 85 insertions(+), 950 deletions(-) delete mode 100644 libtransport/src/hicn/transport/core/manifest_format_json_jsoncpp.cc delete mode 100644 libtransport/src/hicn/transport/core/manifest_format_json_jsoncpp.h delete mode 100644 libtransport/src/hicn/transport/core/manifest_format_json_libparc_deprecated.cc delete mode 100644 libtransport/src/hicn/transport/core/manifest_format_json_libparc_deprecated.h (limited to 'libtransport/src/hicn/transport/core') diff --git a/libtransport/src/hicn/transport/core/content_object.cc b/libtransport/src/hicn/transport/core/content_object.cc index 4cd5623c4..c00535512 100644 --- a/libtransport/src/hicn/transport/core/content_object.cc +++ b/libtransport/src/hicn/transport/core/content_object.cc @@ -92,6 +92,8 @@ const Name &ContentObject::getName() const { return name_; } +Name &ContentObject::getWritableName() { return const_cast(getName()); } + ContentObject &ContentObject::setName(const Name &name) { if (hicn_data_set_name(format_, (hicn_header_t *)packet_start_, name.getStructReference()) < 0) { diff --git a/libtransport/src/hicn/transport/core/content_object.h b/libtransport/src/hicn/transport/core/content_object.h index c85259f20..8770e8cb4 100644 --- a/libtransport/src/hicn/transport/core/content_object.h +++ b/libtransport/src/hicn/transport/core/content_object.h @@ -46,7 +46,9 @@ class ContentObject : public Packet { ~ContentObject() override; - const Name &getName() const; + const Name &getName() const override; + + Name &getWritableName() override; ContentObject &setName(const Name &name); diff --git a/libtransport/src/hicn/transport/core/forwarder_interface.h b/libtransport/src/hicn/transport/core/forwarder_interface.h index de9f3b568..b3403cf5a 100644 --- a/libtransport/src/hicn/transport/core/forwarder_interface.h +++ b/libtransport/src/hicn/transport/core/forwarder_interface.h @@ -18,6 +18,8 @@ #include #include #include +#include +#include #include @@ -97,6 +99,9 @@ class ForwarderInterface { packet.setLocator(inet6_address_); } + // TRANSPORT_LOGI("Sending packet %s at %lu", + // packet.getName().toString().c_str(), + // utils::SteadyClock::now().time_since_epoch().count()); packet.setChecksum(); connector_.send(packet.data()); } diff --git a/libtransport/src/hicn/transport/core/interest.cc b/libtransport/src/hicn/transport/core/interest.cc index 4ea0c4419..49d452d19 100644 --- a/libtransport/src/hicn/transport/core/interest.cc +++ b/libtransport/src/hicn/transport/core/interest.cc @@ -79,16 +79,7 @@ const Name &Interest::getName() const { return name_; } -Name &Interest::getWritableName() { - if (!name_) { - if (hicn_interest_get_name(format_, (hicn_header_t *)packet_start_, - (hicn_name_t *)name_.getStructReference()) < 0) { - throw errors::MalformedPacketException(); - } - } - - return name_; -} +Name &Interest::getWritableName() { return const_cast(getName()); } Interest &Interest::setName(const Name &name) { if (hicn_interest_set_name(format_, (hicn_header_t *)packet_start_, @@ -148,4 +139,4 @@ void Interest::resetForHash() { } // end namespace core -} // end namespace transport +} // end namespace transport \ No newline at end of file diff --git a/libtransport/src/hicn/transport/core/interest.h b/libtransport/src/hicn/transport/core/interest.h index 75fcba8eb..95b45ac9a 100644 --- a/libtransport/src/hicn/transport/core/interest.h +++ b/libtransport/src/hicn/transport/core/interest.h @@ -45,9 +45,9 @@ class Interest ~Interest() override; - const Name &getName() const; + const Name &getName() const override; - Name &getWritableName(); + Name &getWritableName() override; Interest &setName(const Name &name); diff --git a/libtransport/src/hicn/transport/core/manifest.h b/libtransport/src/hicn/transport/core/manifest.h index 558a96804..9f7dc5984 100644 --- a/libtransport/src/hicn/transport/core/manifest.h +++ b/libtransport/src/hicn/transport/core/manifest.h @@ -38,39 +38,28 @@ class Manifest : public Base { using Encoder = typename FormatTraits::Encoder; using Decoder = typename FormatTraits::Decoder; - Manifest() - : packet_(new Base(HF_INET6_TCP_AH), nullptr), - encoder_(*packet_), - decoder_(*packet_) { + Manifest(std::size_t signature_size = 0) + : Base(HF_INET6_TCP_AH), + encoder_(*this, signature_size), + decoder_(*this) { Base::setPayloadType(PayloadType::MANIFEST); } - Manifest(const core::Name &name) - : packet_(new Base(name, HF_INET6_TCP_AH), nullptr), - encoder_(*packet_), - decoder_(*packet_) { - Base::setPayloadType(PayloadType::MANIFEST); - } - - Manifest(typename Base::Ptr &&base) - : packet_(std::move(base)), encoder_(*packet_), decoder_(*packet_) { + Manifest(const core::Name &name, std::size_t signature_size = 0) + : Base(name, HF_INET6_TCP_AH), + encoder_(*this, signature_size), + decoder_(*this) { Base::setPayloadType(PayloadType::MANIFEST); } template Manifest(T &&base) - : packet_(new Base(std::move(base)), nullptr), - encoder_(*packet_), - decoder_(*packet_) { + : Base(std::forward(base)), encoder_(*this), decoder_(*this) { Base::setPayloadType(PayloadType::MANIFEST); } virtual ~Manifest() = default; - bool operator==(const Manifest &other) { - return this->packet_ == other.packet_; - } - std::size_t estimateManifestSize(std::size_t additional_entries = 0) { return static_cast(*this).estimateManifestSizeImpl( additional_entries); @@ -142,15 +131,7 @@ class Manifest : public Base { return *this; } - void setSignatureSize(std::size_t size_bits) { - Packet::setSignatureSize(size_bits); - encoder_.update(); - } - - typename Base::Ptr &&getPacket() { return std::move(packet_); } - protected: - typename Base::Ptr packet_; ManifestType manifest_type_; HashAlgorithm hash_algorithm_; bool is_last_; diff --git a/libtransport/src/hicn/transport/core/manifest_format_fixed.cc b/libtransport/src/hicn/transport/core/manifest_format_fixed.cc index 6141ae311..73b33268c 100644 --- a/libtransport/src/hicn/transport/core/manifest_format_fixed.cc +++ b/libtransport/src/hicn/transport/core/manifest_format_fixed.cc @@ -15,7 +15,6 @@ #include #include -#include #include namespace transport { @@ -23,26 +22,34 @@ namespace transport { namespace core { // TODO use preallocated pool of membufs -FixedManifestEncoder::FixedManifestEncoder(Packet &packet) +FixedManifestEncoder::FixedManifestEncoder(Packet &packet, + std::size_t signature_size) : packet_(packet), - max_size_(Packet::default_mtu - packet_.headerSize()), + max_size_(Packet::default_mtu - packet_.headerSize() - signature_size), manifest_( utils::MemBuf::create(Packet::default_mtu - packet_.headerSize())), manifest_header_( reinterpret_cast(manifest_->writableData())), manifest_entries_(reinterpret_cast( manifest_->writableData() + sizeof(ManifestHeader))), - current_entry_(0) {} + current_entry_(0), + signature_size_(signature_size) { + *manifest_header_ = {0}; +} FixedManifestEncoder::~FixedManifestEncoder() {} FixedManifestEncoder &FixedManifestEncoder::encodeImpl() { + manifest_->append(sizeof(ManifestHeader) + + manifest_header_->number_of_entries * + sizeof(ManifestEntry)); packet_.appendPayload(std::move(manifest_)); return *this; } FixedManifestEncoder &FixedManifestEncoder::clearImpl() { - manifest_ = utils::MemBuf::create(Packet::default_mtu - packet_.headerSize()); + manifest_ = utils::MemBuf::create(Packet::default_mtu - packet_.headerSize() - + signature_size_); return *this; } @@ -84,7 +91,7 @@ FixedManifestEncoder &FixedManifestEncoder::addSuffixAndHashImpl( void FixedManifestEncoder::addSuffixHashBytes(uint32_t suffix, const uint8_t *hash, std::size_t length) { - manifest_entries_[current_entry_].suffix = utils::hton(suffix); + manifest_entries_[current_entry_].suffix = htonl(suffix); // std::copy(hash, hash + length, // manifest_entries_[current_entry_].hash); std::memcpy( @@ -119,18 +126,13 @@ std::size_t FixedManifestEncoder::estimateSerializedLengthImpl( } FixedManifestEncoder &FixedManifestEncoder::updateImpl() { - max_size_ = Packet::default_mtu - packet_.headerSize(); - manifest_header_ = reinterpret_cast( - const_cast(packet_.getPayload().data())); - manifest_entries_ = reinterpret_cast( - const_cast(packet_.getPayload().data()) + - sizeof(ManifestHeader)); + max_size_ = Packet::default_mtu - packet_.headerSize() - signature_size_; return *this; } FixedManifestEncoder &FixedManifestEncoder::setFinalBlockNumberImpl( std::uint32_t final_block_number) { - manifest_header_->final_block_number = utils::hton(final_block_number); + manifest_header_->final_block_number = htonl(final_block_number); return *this; } @@ -179,7 +181,7 @@ typename Fixed::SuffixList FixedManifestDecoder::getSuffixHashListImpl() { for (int i = 0; i < manifest_header_->number_of_entries; i++) { hash_list.insert(hash_list.end(), - std::make_pair(utils::ntoh(manifest_entries_[i].suffix), + std::make_pair(ntohl(manifest_entries_[i].suffix), reinterpret_cast( &manifest_entries_[i].hash[0]))); } @@ -213,7 +215,7 @@ std::size_t FixedManifestDecoder::estimateSerializedLengthImpl( } uint32_t FixedManifestDecoder::getFinalBlockNumberImpl() const { - return utils::ntoh(manifest_header_->final_block_number); + return ntohl(manifest_header_->final_block_number); } } // end namespace core diff --git a/libtransport/src/hicn/transport/core/manifest_format_fixed.h b/libtransport/src/hicn/transport/core/manifest_format_fixed.h index 82817ddfb..c9bc3f8e5 100644 --- a/libtransport/src/hicn/transport/core/manifest_format_fixed.h +++ b/libtransport/src/hicn/transport/core/manifest_format_fixed.h @@ -83,7 +83,7 @@ static const constexpr std::uint8_t manifest_version = 1; class FixedManifestEncoder : public ManifestEncoder { public: - FixedManifestEncoder(Packet &packet); + FixedManifestEncoder(Packet &packet, std::size_t signature_size = 0); ~FixedManifestEncoder(); @@ -126,6 +126,7 @@ class FixedManifestEncoder : public ManifestEncoder { ManifestHeader *manifest_header_; ManifestEntry *manifest_entries_; std::size_t current_entry_; + std::size_t signature_size_; }; class FixedManifestDecoder : public ManifestDecoder { diff --git a/libtransport/src/hicn/transport/core/manifest_format_json_jsoncpp.cc b/libtransport/src/hicn/transport/core/manifest_format_json_jsoncpp.cc deleted file mode 100644 index 0ffca39f6..000000000 --- a/libtransport/src/hicn/transport/core/manifest_format_json_jsoncpp.cc +++ /dev/null @@ -1,244 +0,0 @@ -/* - * Copyright (c) 2017-2019 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: - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include -#include - -#include - -namespace transport { - -namespace core { - -namespace { - -template -TRANSPORT_ALWAYS_INLINE void checkPointer(T *pointer) { - if (pointer == nullptr) { - throw errors::NullPointerException(); - } -} - -template -TRANSPORT_ALWAYS_INLINE void setValueToJson(Json::Value &root, EnumType value) { - root[JSONKey::key] = static_cast(value); -} - -template -TRANSPORT_ALWAYS_INLINE EnumType getValueFromJson(const Json::Value &root) { - return static_cast(root[JSONKey::key].asUInt()); -}; - -} // namespace - -JSONManifestEncoder::JSONManifestEncoder(Packet &packet) : packet_(packet) {} - -JSONManifestEncoder::~JSONManifestEncoder() {} - -TRANSPORT_ALWAYS_INLINE JSONManifestEncoder &JSONManifestEncoder::encodeImpl() { - Json::StreamWriterBuilder writer_builder; - Json::StreamWriter *fast_writer = writer_builder.newStreamWriter(); - - asio::streambuf strbuf; - strbuf.prepare(1500); - std::ostream stream(&strbuf); - fast_writer->write(root_, &stream); - - const uint8_t *buffer = asio::buffer_cast(strbuf.data()); - - packet_.setPayload(buffer, strbuf.size()); - - delete fast_writer; - - return *this; -} - -TRANSPORT_ALWAYS_INLINE JSONManifestEncoder &JSONManifestEncoder::clearImpl() { - root_.clear(); - return *this; -} - -TRANSPORT_ALWAYS_INLINE JSONManifestEncoder & -JSONManifestEncoder::setHashAlgorithmImpl(HashAlgorithm algorithm) { - setValueToJson(root_, algorithm); - return *this; -} - -JSONManifestEncoder &JSONManifestEncoder::setManifestTypeImpl( - ManifestType manifest_type) { - setValueToJson(root_, manifest_type); - return *this; -} - -JSONManifestEncoder &JSONManifestEncoder::setNextSegmentCalculationStrategyImpl( - NextSegmentCalculationStrategy strategy) { - setValueToJson(root_, strategy); - return *this; -} - -TRANSPORT_ALWAYS_INLINE JSONManifestEncoder & -JSONManifestEncoder::setBaseNameImpl(const core::Name &base_name) { - root_[JSONKey::key] = base_name.toString().c_str(); - return *this; -} - -TRANSPORT_ALWAYS_INLINE JSONManifestEncoder & -JSONManifestEncoder::addSuffixAndHashImpl(uint32_t suffix, - const utils::CryptoHash &hash) { - throw errors::NotImplementedException(); - // Json::Value value(Json::arrayValue); - // value.append(Json::Value(suffix)); - // value.append(Json::Value(Json::Value::UInt64 (hash))); - // root_[JSONKey::key].append(value); - - return *this; -} - -TRANSPORT_ALWAYS_INLINE JSONManifestEncoder & -JSONManifestEncoder::setIsFinalManifestImpl(bool is_last) { - root_[JSONKey::final_manifest] = is_last; - return *this; -} - -TRANSPORT_ALWAYS_INLINE JSONManifestEncoder & -JSONManifestEncoder::setVersionImpl(ManifestVersion version) { - setValueToJson(root_, version); - return *this; -} - -TRANSPORT_ALWAYS_INLINE JSONManifestEncoder & -JSONManifestEncoder::setSuffixHashListImpl( - const typename JSON::SuffixList &name_hash_list) { - throw errors::NotImplementedException(); - // for (auto &suffix : name_hash_list) { - // addSuffixAndHashImpl(suffix.first, suffix.second); - // } - // - // return *this; -} - -TRANSPORT_ALWAYS_INLINE std::size_t -JSONManifestEncoder::estimateSerializedLengthImpl( - std::size_t number_of_entries) { - Json::StreamWriterBuilder writer_builder; - Json::StreamWriter *fast_writer = writer_builder.newStreamWriter(); - - asio::streambuf strbuf; - strbuf.prepare(1500); - std::ostream stream(&strbuf); - fast_writer->write(root_, &stream); - - return strbuf.size(); -} - -TRANSPORT_ALWAYS_INLINE JSONManifestEncoder &JSONManifestEncoder::updateImpl() { - throw errors::NotImplementedException(); -} - -TRANSPORT_ALWAYS_INLINE JSONManifestEncoder & -JSONManifestEncoder::setFinalBlockNumberImpl(std::uint32_t final_block_number) { - throw errors::NotImplementedException(); -} - -TRANSPORT_ALWAYS_INLINE std::size_t -JSONManifestEncoder::getManifestHeaderSizeImpl() { - return 0; -} - -JSONManifestDecoder::JSONManifestDecoder(Packet &packet) : packet_(packet) {} - -JSONManifestDecoder::~JSONManifestDecoder() {} - -TRANSPORT_ALWAYS_INLINE void JSONManifestDecoder::decodeImpl() { - auto array = packet_.getPayload(); - auto payload = array.data(); - auto payload_size = array.length(); - - Json::CharReaderBuilder reader_builder; - Json::CharReader *reader = reader_builder.newCharReader(); - std::string errors; - - if (!reader->parse((char *)payload, (char *)payload + payload_size, &root_, - &errors)) { - TRANSPORT_LOGE("Error parsing manifest!"); - TRANSPORT_LOGE("%s", errors.c_str()); - - delete reader; - - throw errors::MalformedPacketException(); - } - - delete reader; -} - -TRANSPORT_ALWAYS_INLINE JSONManifestDecoder &JSONManifestDecoder::clearImpl() { - root_.clear(); - return *this; -} - -TRANSPORT_ALWAYS_INLINE ManifestType -JSONManifestDecoder::getManifestTypeImpl() const { - return getValueFromJson(root_); -} - -TRANSPORT_ALWAYS_INLINE HashAlgorithm -JSONManifestDecoder::getHashAlgorithmImpl() const { - return getValueFromJson(root_); -} - -TRANSPORT_ALWAYS_INLINE NextSegmentCalculationStrategy -JSONManifestDecoder::getNextSegmentCalculationStrategyImpl() const { - return getValueFromJson(root_); -} - -TRANSPORT_ALWAYS_INLINE typename JSON::SuffixList -JSONManifestDecoder::getSuffixHashListImpl() { - throw errors::NotImplementedException(); - // SuffixHashList hash_list; - // - // Json::Value &array = root_[JSONKey::key]; - // - // for (Json::Value::ArrayIndex i = 0; - // i != array.size(); - // i++) { - // hash_list[array[i][0].asUInt()] = array[i][1].asUInt64(); - // } - // - // return hash_list; -} - -TRANSPORT_ALWAYS_INLINE core::Name JSONManifestDecoder::getBaseNameImpl() - const { - return core::Name(root_[JSONKey::key].asCString()); -} - -TRANSPORT_ALWAYS_INLINE bool JSONManifestDecoder::getIsFinalManifestImpl() - const { - return root_[JSONKey::final_manifest].asBool(); -} - -TRANSPORT_ALWAYS_INLINE ManifestVersion -JSONManifestDecoder::getVersionImpl() const { - return getValueFromJson(root_); -} - -TRANSPORT_ALWAYS_INLINE uint32_t -JSONManifestDecoder::getFinalBlockNumberImpl() const { - return 0; -} - -} // end namespace core - -} // end namespace transport diff --git a/libtransport/src/hicn/transport/core/manifest_format_json_jsoncpp.h b/libtransport/src/hicn/transport/core/manifest_format_json_jsoncpp.h deleted file mode 100644 index 84d2ba29a..000000000 --- a/libtransport/src/hicn/transport/core/manifest_format_json_jsoncpp.h +++ /dev/null @@ -1,162 +0,0 @@ -/* - * Copyright (c) 2017-2019 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: - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#pragma once - -#include -#include - -#if defined(__APPLE__) || defined(__ANDROID__) -#include -#else -#include -#endif /* __APPLE__ || __ANDROID__*/ - -#include - -namespace transport { - -namespace core { - -class JSONManifestEncoder; -class JSONManifestDecoder; -class Packet; - -struct JSON { - using Encoder = JSONManifestEncoder; - using Decoder = JSONManifestDecoder; - using HashType = utils::CryptoHash; - using SuffixList = std::unordered_map; -}; - -template -struct JSONKey; - -template <> -struct JSONKey { - static const constexpr char *key = "manifest_version"; -}; - -template <> -struct JSONKey { - static const constexpr char *key = "hash_algorithm"; -}; - -template <> -struct JSONKey { - static const constexpr char *key = "manifest_type"; -}; - -template <> -struct JSONKey { - static const constexpr char *key = "next_segment_strategy"; -}; - -template <> -struct JSONKey { - static const constexpr char *key = "suffix_hash_list"; -}; - -template <> -struct JSONKey { - static const constexpr char *key = "base_name"; -}; - -template <> -struct JSONKey { - static const constexpr char *final_manifest = "final_manifest"; -}; - -class JSONManifestEncoder : public ManifestEncoder { - public: - JSONManifestEncoder(Packet &packet); - - ~JSONManifestEncoder() override; - - JSONManifestEncoder &encodeImpl(); - - JSONManifestEncoder &clearImpl(); - - JSONManifestEncoder &setManifestTypeImpl(ManifestType manifest_type); - - JSONManifestEncoder &setHashAlgorithmImpl(HashAlgorithm algorithm); - - JSONManifestEncoder &setNextSegmentCalculationStrategyImpl( - NextSegmentCalculationStrategy strategy); - - JSONManifestEncoder &setSuffixHashListImpl( - const typename JSON::SuffixList &name_hash_list); - - JSONManifestEncoder &setBaseNameImpl(const core::Name &base_name); - - JSONManifestEncoder &addSuffixAndHashImpl(uint32_t suffix, - const utils::CryptoHash &hash); - - JSONManifestEncoder &setIsFinalManifestImpl(bool is_last); - - JSONManifestEncoder &setVersionImpl(ManifestVersion version); - - std::size_t estimateSerializedLengthImpl(std::size_t number_of_entries); - - JSONManifestEncoder &updateImpl(); - - JSONManifestEncoder &setFinalBlockNumberImpl( - std::uint32_t final_block_number); - - static std::size_t getManifestHeaderSizeImpl(); - - private: - Packet &packet_; - Json::Value root_; -}; - -class JSONManifestDecoder : public ManifestDecoder { - public: - JSONManifestDecoder(Packet &packet); - - ~JSONManifestDecoder() override; - - void decodeImpl(); - - JSONManifestDecoder &clearImpl(); - - ManifestType getManifestTypeImpl() const; - - HashAlgorithm getHashAlgorithmImpl() const; - - uint32_t getFinalChunkImpl() const; - - NextSegmentCalculationStrategy getNextSegmentCalculationStrategyImpl() const; - - typename JSON::SuffixList getSuffixHashListImpl(); - - core::Name getBaseNameImpl() const; - - bool getIsFinalManifestImpl() const; - - std::size_t estimateSerializedLengthImpl(std::size_t number_of_entries) const; - - ManifestVersion getVersionImpl() const; - - uint32_t getFinalBlockNumberImpl() const; - - private: - Packet &packet_; - Json::Value root_; -}; - -} // namespace core - -} // namespace transport \ No newline at end of file diff --git a/libtransport/src/hicn/transport/core/manifest_format_json_libparc_deprecated.cc b/libtransport/src/hicn/transport/core/manifest_format_json_libparc_deprecated.cc deleted file mode 100644 index a3a47e62b..000000000 --- a/libtransport/src/hicn/transport/core/manifest_format_json_libparc_deprecated.cc +++ /dev/null @@ -1,298 +0,0 @@ -/* - * Copyright (c) 2017-2019 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: - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include -#include -#include -#include - -extern "C" { -#include -} - -namespace transport { - -namespace core { - -namespace { - -template -TRANSPORT_ALWAYS_INLINE void checkPointer(T *pointer) { - if (pointer == nullptr) { - throw errors::NullPointerException(); - } -} - -template -TRANSPORT_ALWAYS_INLINE void setValueToJson(PARCJSON *root, EnumType value) { - parcJSON_AddInteger(root, JSONKey::key, - static_cast(value)); -} - -template -TRANSPORT_ALWAYS_INLINE EnumType getValueFromJson(PARCJSON *root) { - checkPointer(root); - - PARCJSONValue *value = parcJSON_GetValueByName(root, JSONKey::key); - - EnumType ret = static_cast(parcJSONValue_GetInteger(value)); - // parcJSONValue_Release(&value); - - return ret; -}; - -} // namespace - -JSONManifestEncoder::JSONManifestEncoder() : root_(parcJSON_Create()) { - parcJSON_Acquire(root_); -} - -JSONManifestEncoder::~JSONManifestEncoder() { - if (root_) { - parcJSON_Release(&root_); - } -} - -TRANSPORT_ALWAYS_INLINE SONManifestEncoder &JSONManifestEncoder::encodeImpl( - Packet &packet) { - char *json_string = parcJSON_ToString(root_); - packet.setPayload(reinterpret_cast(json_string), - std::strlen(json_string)); - parcMemory_Deallocate(&json_string); - - return *this; -} - -TRANSPORT_ALWAYS_INLINE JSONManifestEncoder &JSONManifestEncoder::clearImpl() { - if (root_) { - parcJSON_Release(&root_); - } - - root_ = parcJSON_Create(); - - return *this; -} - -TRANSPORT_ALWAYS_INLINE JSONManifestEncoder & -JSONManifestEncoder::setHashAlgorithmImpl(HashAlgorithm algorithm) { - setValueToJson(root_, algorithm); - return *this; -} - -TRANSPORT_ALWAYS_INLINE JSONManifestEncoder & -JSONManifestEncoder::setManifestTypeImpl(ManifestType manifest_type) { - setValueToJson(root_, manifest_type); - return *this; -} - -TRANSPORT_ALWAYS_INLINE JSONManifestEncoder & -JSONManifestEncoder::setNextSegmentCalculationStrategyImpl( - NextSegmentCalculationStrategy strategy) { - setValueToJson(root_, strategy); - return *this; -} - -TRANSPORT_ALWAYS_INLINE JSONManifestEncoder & -JSONManifestEncoder::setBaseNameImpl(const core::Name &base_name) { - parcJSON_AddString(root_, JSONKey::key, - base_name.toString().c_str()); - return *this; -} - -TRANSPORT_ALWAYS_INLINE JSONManifestEncoder & -JSONManifestEncoder::addSuffixAndHashImpl(uint32_t suffix, - utils::CryptoHash &hash) { - throw errors::NotImplementedException(); - // PARCJSONValue *value = parcJSON_GetValueByName(root_, - // JSONKey::key); - // - // // Create the pair to store in the array. - // // It will be segment number + Hash of the segment - // PARCJSONArray * pair = parcJSONArray_Create(); - // - // PARCJSONValue *v = parcJSONValue_CreateFromInteger(suffix); - // parcJSONArray_AddValue(pair, v); - // parcJSONValue_Release(&v); - // - // v = parcJSONValue_CreateFromInteger(hash); - // parcJSONArray_AddValue(pair, v); - // parcJSONValue_Release(&v); - // - // if (value == nullptr /* || !parcJSONValue_IsArray(value) */) { - // // Create the array - // PARCJSONArray *array = parcJSONArray_Create(); - // parcJSON_AddArray(root_, - // JSONKey::key, - // array); - // parcJSONArray_Release(&array); - // - // value = parcJSON_GetValueByName(root_, JSONKey::key); - // } - // - // v = parcJSONValue_CreateFromJSONArray(pair); - // parcJSONArray_AddValue(parcJSONValue_GetArray(value), v); - // parcJSONValue_Release(&v); - // - // parcJSONArray_Release(&pair); - // // parcJSONValue_Release(&value); - - return *this; -} - -TRANSPORT_ALWAYS_INLINE JSONManifestEncoder & -JSONManifestEncoder::setIsFinalManifestImpl(bool is_last) { - parcJSON_AddBoolean(root_, JSONKey::final_manifest, is_last); - - return *this; -} - -TRANSPORT_ALWAYS_INLINE JSONManifestEncoder & -JSONManifestEncoder::setSuffixHashListImpl( - const SuffixHashList &name_hash_list) { - for (auto &suffix : name_hash_list) { - addSuffixAndHashImpl(suffix.first, suffix.second); - } - - return *this; -} - -TRANSPORT_ALWAYS_INLINE JSONManifestDecoder::JSONManifestDecoder() - : root_(nullptr) {} - -TRANSPORT_ALWAYS_INLINE JSONManifestDecoder::~JSONManifestDecoder() { - if (root_) { - parcJSON_Release(&root_); - } -} - -TRANSPORT_ALWAYS_INLINE void JSONManifestDecoder::decodeImpl( - const uint8_t *payload, std::size_t payload_size) { - PARCBuffer *b = parcBuffer_Wrap(const_cast(payload), payload_size, - 0, payload_size); - clearImpl(); - - root_ = parcJSON_ParseBuffer(b); - parcBuffer_Release(&b); - - char *str = parcJSON_ToString(root_); -} - -TRANSPORT_ALWAYS_INLINE JSONManifestDecoder &JSONManifestDecoder::clearImpl() { - if (root_) { - parcJSON_Release(&root_); - } - - return *this; -} - -TRANSPORT_ALWAYS_INLINE ManifestType -JSONManifestDecoder::getManifestTypeImpl() const { - return getValueFromJson(root_); -} - -TRANSPORT_ALWAYS_INLINE HashAlgorithm -JSONManifestDecoder::getHashAlgorithmImpl() const { - return getValueFromJson(root_); -} - -TRANSPORT_ALWAYS_INLINE NextSegmentCalculationStrategy -JSONManifestDecoder::getNextSegmentCalculationStrategyImpl() const { - return getValueFromJson(root_); -} - -TRANSPORT_ALWAYS_INLINE SuffixHashList -JSONManifestDecoder::getSuffixHashListImpl() { - throw errors::NotImplementedException(); - // SuffixHashList hash_list; - // - // char * str = parcJSON_ToString(root_); - // - // PARCJSONValue *value = parcJSON_GetValueByName(root_, - // JSONKey::key); - // - // if (value == nullptr || !parcJSONValue_IsArray(value)) { - // throw errors::RuntimeException("Manifest does not contain suffix-hash - // list"); - // } - // - // PARCJSONArray *array = parcJSONValue_GetArray(value); - // std::size_t array_size = parcJSONArray_GetLength(array); - // - // for (std::size_t i = 0; i < array_size; i++) { - // PARCJSONValue *v = parcJSONArray_GetValue(array, i); - // checkPointer(v); - // PARCJSONArray *a = parcJSONValue_GetArray(v); - // PARCJSONValue *_suffix = parcJSONArray_GetValue(a, 0); - // PARCJSONValue *_hash = parcJSONArray_GetValue(a, 1); - // - // uint32_t value1 = - // static_cast(parcJSONValue_GetInteger(_suffix)); uint64_t - // value2 = static_cast(parcJSONValue_GetInteger(_hash)); - // - // hash_list[static_cast(parcJSONValue_GetInteger(_suffix))] = - // static_cast(parcJSONValue_GetInteger(_hash)); - // - //// parcJSONValue_Release(&_hash); - //// parcJSONValue_Release(&_suffix); - //// parcJSONArray_Release(&a); - //// parcJSONValue_Release(&v); - // } - // - //// parcJSONArray_Release(&array); - //// parcJSONValue_Release(&value); - // - // char * str2 = parcJSON_ToString(root_); - // - // return hash_list; -} - -TRANSPORT_ALWAYS_INLINE core::Name JSONManifestDecoder::getBaseNameImpl() - const { - checkPointer(root_); - PARCJSONValue *value = - parcJSON_GetValueByName(root_, JSONKey::key); - - PARCBuffer *b = parcJSONValue_GetString(value); - char *string = parcBuffer_ToString(b); - - core::Name ret(string); - - // parcJSONValue_Release(&value); - parcMemory_Deallocate(&string); - - return ret; -} - -TRANSPORT_ALWAYS_INLINE bool JSONManifestDecoder::getIsFinalManifestImpl() { - checkPointer(root_); - PARCJSONValue *value = - parcJSON_GetValueByName(root_, JSONKey::final_manifest); - - bool ret = parcJSONValue_GetBoolean(value); - - // parcJSONValue_Release(&value); - - return ret; -} - -TRANSPORT_ALWAYS_INLINE std::size_t -JSONManifestDecoder::estimateSerializedLengthImpl( - std::size_t number_of_entries) { - return 0; -} - -} // end namespace core - -} // end namespace transport \ No newline at end of file diff --git a/libtransport/src/hicn/transport/core/manifest_format_json_libparc_deprecated.h b/libtransport/src/hicn/transport/core/manifest_format_json_libparc_deprecated.h deleted file mode 100644 index c89f4af7b..000000000 --- a/libtransport/src/hicn/transport/core/manifest_format_json_libparc_deprecated.h +++ /dev/null @@ -1,152 +0,0 @@ -/* - * Copyright (c) 2017-2019 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: - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#pragma once - -#include -#include - -extern "C" { -#include -} - -#include - -namespace transport { - -namespace core { - -class JSONManifestEncoder; -class JSONManifestDecoder; -class Packet; - -struct JSON { - using Encoder = JSONManifestEncoder; - using Decoder = JSONManifestDecoder; -}; - -template -struct JSONKey; - -template <> -struct JSONKey { - static const constexpr char *key = "hash_algorithm"; -}; - -template <> -struct JSONKey { - static const constexpr char *key = "manifest_type"; -}; - -template <> -struct JSONKey { - static const constexpr char *key = "next_segment_strategy"; -}; - -template <> -struct JSONKey { - static const constexpr char *key = "name_hash_list"; -}; - -template <> -struct JSONKey { - static const constexpr char *key = "suffix_hash_list"; -}; - -template <> -struct JSONKey { - static const constexpr char *key = "base_name"; -}; - -template <> -struct JSONKey { - static const constexpr char *final_manifest = "final_manifest"; -}; - -// template <> -// struct JSONKey { -// static const std::string key = "name_hash_list"; -//}; - -// namespace JSONManifestEncoding { -// static const std::string base_name = "base_name"; -// static const std::string final_chunk_number = "final_chunk_number"; -// static const std::string hash_algorithm = "hash_algorithm"; -// static const std::string manifest_type = "manifest_type"; -// static const std::string name_hash_list = "name_hash_list"; -// static const std::string next_segment_strategy = "next_segment_strategy"; -//} - -class JSONManifestEncoder : public ManifestEncoder { - public: - JSONManifestEncoder(); - - ~JSONManifestEncoder(); - - JSONManifestEncoder &encodeImpl(Packet &packet); - - JSONManifestEncoder &clearImpl(); - - JSONManifestEncoder &setManifestTypeImpl(ManifestType manifest_type); - - JSONManifestEncoder &setHashAlgorithmImpl(HashAlgorithm algorithm); - - JSONManifestEncoder &setNextSegmentCalculationStrategyImpl( - NextSegmentCalculationStrategy strategy); - - JSONManifestEncoder &setSuffixHashListImpl( - const SuffixHashList &name_hash_list); - - JSONManifestEncoder &setBaseNameImpl(const core::Name &base_name); - - JSONManifestEncoder &addSuffixAndHashImpl(uint32_t suffix, uint64_t hash); - - JSONManifestEncoder &setIsFinalManifestImpl(bool is_last); - - private: - PARCJSON *root_; -}; - -class JSONManifestDecoder : public ManifestDecoder { - public: - JSONManifestDecoder(); - - ~JSONManifestDecoder(); - - void decodeImpl(const uint8_t *payload, std::size_t payload_size); - - JSONManifestDecoder &clearImpl(); - - ManifestType getManifestTypeImpl() const; - - HashAlgorithm getHashAlgorithmImpl() const; - - uint32_t getFinalChunkImpl() const; - - NextSegmentCalculationStrategy getNextSegmentCalculationStrategyImpl() const; - - SuffixHashList getSuffixHashListImpl(); - - core::Name getBaseNameImpl() const; - - bool getIsFinalManifestImpl(); - - private: - PARCJSON *root_; -}; - -} // namespace core - -} // namespace transport \ No newline at end of file diff --git a/libtransport/src/hicn/transport/core/manifest_inline.h b/libtransport/src/hicn/transport/core/manifest_inline.h index 1f2c4edb4..60eb08433 100644 --- a/libtransport/src/hicn/transport/core/manifest_inline.h +++ b/libtransport/src/hicn/transport/core/manifest_inline.h @@ -37,7 +37,8 @@ class ManifestInline public: ManifestInline() : ManifestBase() {} - ManifestInline(const core::Name &name) : ManifestBase(name) {} + ManifestInline(const core::Name &name, std::size_t signature_size = 0) + : ManifestBase(name, signature_size) {} template ManifestInline(T &&base) : ManifestBase(std::forward(base)) {} @@ -47,8 +48,7 @@ class ManifestInline ManifestType type, HashAlgorithm algorithm, bool is_last, const Name &base_name, NextSegmentCalculationStrategy strategy, std::size_t signature_size) { - auto manifest = new ManifestInline(manifest_name); - manifest->setSignatureSize(signature_size); + auto manifest = new ManifestInline(manifest_name, signature_size); manifest->setVersion(version); manifest->setManifestType(type); manifest->setHashAlgorithm(algorithm); diff --git a/libtransport/src/hicn/transport/core/packet.cc b/libtransport/src/hicn/transport/core/packet.cc index b3e5526ab..ec99bb3f7 100644 --- a/libtransport/src/hicn/transport/core/packet.cc +++ b/libtransport/src/hicn/transport/core/packet.cc @@ -80,7 +80,7 @@ Packet::Packet(const uint8_t *buffer, std::size_t size) Packet::Packet(Packet &&other) : packet_(std::move(other.packet_)), - packet_start_(packet_->writableData()), + packet_start_(other.packet_start_), header_head_(other.header_head_), payload_head_(other.payload_head_), format_(other.format_) { @@ -269,7 +269,7 @@ void Packet::dump() const { std::cout << std::endl << "PAYLOAD -- Length: " << payloadSize() << std::endl; for (utils::MemBuf *current = payload_head_; current && current != header_head_; current = current->next()) { - std::cout << "First MemBuf Length: " << current->length() << std::endl; + std::cout << "MemBuf Length: " << current->length() << std::endl; hicn_packet_dump((uint8_t *)current->data(), current->length()); } } @@ -310,17 +310,6 @@ std::size_t Packet::getSignatureSize() const { return size_bytes; } -void Packet::setSignature(std::unique_ptr &&signature) { - // Check if packet already contains a signature - auto header = header_head_->next(); - while (header != payload_head_) { - header->unlink(); - header = header->next(); - } - - appendHeader(std::move(signature)); -} - void Packet::setSignatureTimestamp(const uint64_t ×tamp) { int ret = hicn_packet_set_signature_timestamp( format_, (hicn_header_t *)packet_start_, timestamp); @@ -397,12 +386,11 @@ utils::CryptoHash Packet::computeDigest(HashAlgorithm algorithm) const { const_cast(this)->resetForHash(); - std::size_t payload_len = getPayloadSizeFromBuffer(format_, packet_start_); - std::size_t header_length = getHeaderSizeFromFormat(format_); - std::size_t signature_size = _is_ah(format_) ? getSignatureSize() : 0; - - hasher.updateBytes(packet_start_, - payload_len + header_length + signature_size); + auto current = header_head_; + do { + hasher.updateBytes(current->data(), current->length()); + current = current->next(); + } while (current != header_head_); hicn_packet_copy_header(format_, &header_copy, (hicn_header_t *)packet_start_, false); diff --git a/libtransport/src/hicn/transport/core/packet.h b/libtransport/src/hicn/transport/core/packet.h index 49a558333..add830c1e 100644 --- a/libtransport/src/hicn/transport/core/packet.h +++ b/libtransport/src/hicn/transport/core/packet.h @@ -103,6 +103,10 @@ class Packet : public std::enable_shared_from_this { virtual void setLifetime(uint32_t lifetime); + virtual const Name &getName() const = 0; + + virtual Name &getWritableName() = 0; + virtual uint32_t getLifetime() const; Packet &appendPayload(const uint8_t *buffer, std::size_t length); @@ -129,12 +133,6 @@ class Packet : public std::enable_shared_from_this { virtual ip_address_t getLocator() const = 0; - void setSignatureSize(std::size_t size_bytes); - - std::size_t getSignatureSize() const; - - uint8_t *getSignature() const; - void setSignatureTimestamp(const uint64_t ×tamp); uint64_t getSignatureTimestamp() const; @@ -147,8 +145,6 @@ class Packet : public std::enable_shared_from_this { utils::KeyId getKeyId() const; - void setSignature(std::unique_ptr &&signature); - virtual utils::CryptoHash computeDigest(HashAlgorithm algorithm) const; void setChecksum(); @@ -180,6 +176,9 @@ class Packet : public std::enable_shared_from_this { private: virtual void resetForHash() = 0; + void setSignatureSize(std::size_t size_bytes); + std::size_t getSignatureSize() const; + uint8_t *getSignature() const; protected: Name name_; diff --git a/libtransport/src/hicn/transport/core/raw_socket_connector.cc b/libtransport/src/hicn/transport/core/raw_socket_connector.cc index fe16d2132..78241b2ea 100644 --- a/libtransport/src/hicn/transport/core/raw_socket_connector.cc +++ b/libtransport/src/hicn/transport/core/raw_socket_connector.cc @@ -181,7 +181,7 @@ void RawSocketConnector::doSendPacket() { } void RawSocketConnector::doRecvPacket() { - read_msg_ = std::move(getPacket()); + read_msg_ = getPacket(); socket_.async_receive( asio::buffer(read_msg_->writableData(), packet_size), [this](std::error_code ec, std::size_t bytes_transferred) mutable { diff --git a/libtransport/src/hicn/transport/core/raw_socket_connector.h b/libtransport/src/hicn/transport/core/raw_socket_connector.h index a54b55e7e..57a2bc067 100644 --- a/libtransport/src/hicn/transport/core/raw_socket_connector.h +++ b/libtransport/src/hicn/transport/core/raw_socket_connector.h @@ -19,11 +19,11 @@ #include #include -#include #include #include #include #include +#include #include namespace transport { diff --git a/libtransport/src/hicn/transport/core/tcp_socket_connector.cc b/libtransport/src/hicn/transport/core/tcp_socket_connector.cc index ade0f2611..4c5f90db3 100644 --- a/libtransport/src/hicn/transport/core/tcp_socket_connector.cc +++ b/libtransport/src/hicn/transport/core/tcp_socket_connector.cc @@ -111,8 +111,25 @@ void TcpSocketConnector::close() { } void TcpSocketConnector::doWrite() { - // TODO improve this piece of code for sending many buffers togethers - // if list contains more than one packet +#if 1 + auto array = std::vector(); + std::vector packet_store(packet_store_size); + uint8_t i = 0; + + utils::MemBuf *packet = nullptr; + const utils::MemBuf *current = nullptr; + // Send vectors of 32 packets + while (!output_buffer_.empty() && i++ < packet_store_size) { + packet_store[i] = output_buffer_.front(); + output_buffer_.pop_front(); + packet = packet_store[i].get(); + current = packet; + do { + array.push_back(asio::const_buffer(current->data(), current->length())); + current = current->next(); + } while (current != packet); + } +#else auto packet = output_buffer_.front().get(); auto array = std::vector(); @@ -121,12 +138,13 @@ void TcpSocketConnector::doWrite() { array.push_back(asio::const_buffer(current->data(), current->length())); current = current->next(); } while (current != packet); +#endif asio::async_write( socket_, std::move(array), - [this /*, packet*/](std::error_code ec, std::size_t length) { + [this, packet_store = std::move(packet_store)](std::error_code ec, + std::size_t length) { if (TRANSPORT_EXPECT_TRUE(!ec)) { - output_buffer_.pop_front(); if (!output_buffer_.empty()) { doWrite(); } diff --git a/libtransport/src/hicn/transport/core/tcp_socket_connector.h b/libtransport/src/hicn/transport/core/tcp_socket_connector.h index ca9d2b663..465eeb912 100644 --- a/libtransport/src/hicn/transport/core/tcp_socket_connector.h +++ b/libtransport/src/hicn/transport/core/tcp_socket_connector.h @@ -30,6 +30,8 @@ namespace core { using asio::ip::tcp; class TcpSocketConnector : public Connector { + static constexpr uint16_t packet_store_size = 32; + public: TcpSocketConnector(PacketReceivedCallback &&receive_callback, OnReconnect &&reconnect_callback, -- cgit 1.2.3-korg