diff options
Diffstat (limited to 'libtransport/includes/hicn/transport/core/packet.h')
-rw-r--r-- | libtransport/includes/hicn/transport/core/packet.h | 280 |
1 files changed, 104 insertions, 176 deletions
diff --git a/libtransport/includes/hicn/transport/core/packet.h b/libtransport/includes/hicn/transport/core/packet.h index 2efd7439d..31d856cd5 100644 --- a/libtransport/includes/hicn/transport/core/packet.h +++ b/libtransport/includes/hicn/transport/core/packet.h @@ -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: @@ -15,24 +15,31 @@ #pragma once +#include <hicn/transport/auth/crypto_hash.h> +#include <hicn/transport/auth/crypto_suite.h> +#include <hicn/transport/auth/key_id.h> #include <hicn/transport/core/name.h> #include <hicn/transport/core/payload_type.h> #include <hicn/transport/errors/malformed_packet_exception.h> #include <hicn/transport/portability/portability.h> -#include <hicn/transport/security/crypto_hasher.h> -#include <hicn/transport/security/crypto_suite.h> -#include <hicn/transport/security/key_id.h> #include <hicn/transport/utils/branch_prediction.h> #include <hicn/transport/utils/membuf.h> #include <hicn/transport/utils/object_pool.h> -namespace utils { -class Signer; -class Verifier; -} // namespace utils +extern "C" { +#ifndef _WIN32 +TRANSPORT_CLANG_DISABLE_WARNING("-Wextern-c-compat") +#endif +#include <hicn/packet.h> +} namespace transport { +namespace auth { +class Signer; +class Verifier; +} // namespace auth + namespace core { /* @@ -44,13 +51,17 @@ namespace core { * \_______________________________________| */ -class Packet : public std::enable_shared_from_this<Packet> { - friend class utils::Signer; - friend class utils::Verifier; +class Packet : public utils::MemBuf, + public std::enable_shared_from_this<Packet> { + friend class auth::Signer; + friend class auth::Verifier; public: + using Ptr = std::shared_ptr<Packet>; using MemBufPtr = std::shared_ptr<utils::MemBuf>; - using Format = hicn_format_t; + using Format = hicn_packet_format_t; + using Type = hicn_packet_type_t; + static constexpr size_t default_mtu = 1500; /** @@ -58,205 +69,122 @@ class Packet : public std::enable_shared_from_this<Packet> { * the eventual payload will be added by prepending the payload buffer * to the buffer chain whose the fist buffer is the header itself. */ - Packet(Format format = HF_UNSPEC); - - /** - * Create new IP packet using raw buffer. - */ - Packet(const uint8_t *buffer, std::size_t size); - Packet(MemBufPtr &&buffer); - - /* - * Enforce zero-copy lifestyle. - */ - Packet(const Packet &other) = delete; - Packet &operator=(const Packet &other) = delete; - - /* - * Move constructor. - */ + Packet(Type type, Format format, std::size_t additional_header_size = 0); + /* Copy buffer */ + Packet(CopyBufferOp, const uint8_t *buffer, std::size_t size); + /* Wrap buffer */ + Packet(WrapBufferOp, uint8_t *buffer, std::size_t length, std::size_t size); + /* Create new using pre-allocated buffer */ + Packet(CreateOp, Type type, uint8_t *buffer, std::size_t length, + std::size_t size, Format format, + std::size_t additional_header_size = 0); + + Packet(MemBuf &&buffer); Packet(Packet &&other); + Packet(const Packet &other); + + // Destructor + virtual ~Packet(); + // Operators + Packet &operator=(const Packet &other); friend bool operator==(const Packet &l_packet, const Packet &r_packet); - virtual ~Packet(); + // Cast to MemBuf + std::shared_ptr<utils::MemBuf> acquireMemBufReference(); - static std::size_t getHeaderSizeFromFormat(Format format, - std::size_t signature_size = 0) { - std::size_t header_length; - hicn_packet_get_header_length_from_format(format, &header_length); - int is_ah = _is_ah(format); - return is_ah * (header_length + signature_size) + (!is_ah) * header_length; - } - - static std::size_t getHeaderSizeFromBuffer(Format format, - const uint8_t *buffer); - - static std::size_t getPayloadSizeFromBuffer(Format format, - const uint8_t *buffer); - - static bool isInterest(const uint8_t *buffer); - - static Format getFormatFromBuffer(const uint8_t *buffer) { - Format format = HF_UNSPEC; - - if (TRANSPORT_EXPECT_FALSE( - hicn_packet_get_format((const hicn_header_t *)buffer, &format) < - 0)) { - throw errors::MalformedPacketException(); - } - - return format; - } - - TRANSPORT_ALWAYS_INLINE void replace(MemBufPtr &&buffer) { - packet_ = std::move(buffer); - packet_start_ = reinterpret_cast<hicn_header_t *>(packet_->writableData()); - header_head_ = packet_.get(); - payload_head_ = nullptr; - format_ = getFormatFromBuffer(reinterpret_cast<uint8_t *>(packet_start_)); - name_.clear(); - } + // Format + Format getFormat() const; + void setFormat(Packet::Format format); - std::size_t payloadSize() const; + void initialize(std::size_t additional_header_size = 0); + void analyze(); - std::size_t headerSize() const; + void initializeType(Packet::Type type); + hicn_packet_type_t getType() const; + void setType(Packet::Type type); - const std::shared_ptr<utils::MemBuf> acquireMemBufReference() const; + void setBuffer(); + // Name virtual const Name &getName() const = 0; - virtual Name &getWritableName() = 0; - virtual void setName(const Name &name) = 0; - virtual void setName(Name &&name) = 0; - + // Lifetime virtual void setLifetime(uint32_t lifetime) = 0; - virtual uint32_t getLifetime() const = 0; - Packet &appendPayload(const uint8_t *buffer, std::size_t length); - - Packet &appendPayload(std::unique_ptr<utils::MemBuf> &&payload); + // Locator + virtual void setLocator(const hicn_ip_address_t &locator) = 0; + virtual hicn_ip_address_t getLocator() const = 0; - Packet &appendHeader(std::unique_ptr<utils::MemBuf> &&header); - - Packet &appendHeader(const uint8_t *buffer, std::size_t length); + // Payload type + PayloadType getPayloadType() const; + Packet &setPayloadType(PayloadType payload_type); + // Payload std::unique_ptr<utils::MemBuf> getPayload() const; + Packet &appendPayload(std::unique_ptr<utils::MemBuf> &&payload); + Packet &appendPayload(const uint8_t *buffer, std::size_t length); - std::pair<const uint8_t *, std::size_t> getPayloadReference() const { - int signature_size = 0; + // Sizes + std::size_t headerSize() const; + std::size_t payloadSize() const; - if (_is_ah(format_)) { - signature_size = (uint32_t)getSignatureSize(); - } + // Digest + auth::CryptoHash computeDigest(auth::CryptoHashType algorithm) const; - auto header_size = getHeaderSizeFromFormat(format_, signature_size); - auto payload_length = payloadSize(); + bool isInterest(); - return std::make_pair(packet_->data() + header_size, payload_length); - } + // Reset packet + void reset(); + // Utils Packet &updateLength(std::size_t length = 0); - - PayloadType getPayloadType() const; - - Packet &setPayloadType(PayloadType payload_type); - - Format getFormat() const; - void dump() const; - virtual void setLocator(const ip_address_t &locator) = 0; - - virtual ip_address_t getLocator() const = 0; - - void setSignatureTimestamp(const uint64_t ×tamp); - - uint64_t getSignatureTimestamp() const; - - void setValidationAlgorithm(const utils::CryptoSuite &validation_algorithm); - - utils::CryptoSuite getValidationAlgorithm() const; - - void setKeyId(const utils::KeyId &key_id); - - utils::KeyId getKeyId() const; - - virtual utils::CryptoHash computeDigest( - utils::CryptoHashType algorithm) const; - - void setChecksum() { - uint16_t partial_csum = 0; - - for (utils::MemBuf *current = header_head_->next(); - current && current != header_head_; current = current->next()) { - if (partial_csum != 0) { - partial_csum = ~partial_csum; - } - partial_csum = csum(current->data(), current->length(), partial_csum); - } - if (hicn_packet_compute_header_checksum(format_, packet_start_, - partial_csum) < 0) { - throw errors::MalformedPacketException(); - } - } - + // TCP methods + void setChecksum(); bool checkIntegrity() const; - Packet &setSyn(); - Packet &resetSyn(); - bool testSyn() const; - Packet &setAck(); - Packet &resetAck(); - bool testAck() const; - Packet &setRst(); - Packet &resetRst(); - bool testRst() const; - Packet &setFin(); - Packet &resetFin(); - bool testFin() const; - Packet &resetFlags(); - std::string printFlags() const; - - Packet &setSrcPort(uint16_t srcPort); - Packet &setDstPort(uint16_t dstPort); - uint16_t getSrcPort() const; - uint16_t getDstPort() const; - - Packet &setTTL(uint8_t hops); - uint8_t getTTL() const; - - void separateHeaderPayload(); - void resetPayload(); + // Authentication Header methods + bool hasAH() const; + utils::MemBuf::Ptr getSignature() const; + std::size_t getSignatureFieldSize() const; + std::size_t getSignatureSize() const; + uint64_t getSignatureTimestamp() const; + auth::KeyId getKeyId() const; + auth::CryptoSuite getValidationAlgorithm() const; + void setSignature(const utils::MemBuf::Ptr &signature); + void setSignatureFieldSize(std::size_t size); + void setSignatureSize(std::size_t size); + void setSignatureTimestamp(const uint64_t ×tamp_ms); + void setKeyId(const auth::KeyId &key_id); + void setValidationAlgorithm(const auth::CryptoSuite &algo); + + void saveHeader(u8 *header, size_t *header_len); + void loadHeader(u8 *header, size_t header_len); + + // Static methods + static Format toAHFormat(const Format &format); + static Format getFormatFromBuffer(const uint8_t *buffer, std::size_t length); + static std::size_t getHeaderSizeFromFormat(Format format, + std::size_t signature_size = 0); + static std::size_t getHeaderSizeFromBuffer(const uint8_t *buffer, + size_t length); + static std::size_t getPayloadSizeFromBuffer(const uint8_t *buffer, + size_t length); + static void dump(uint8_t *buffer, std::size_t length); private: virtual void resetForHash() = 0; - void setSignatureSize(std::size_t size_bytes); - - std::size_t getSignatureSize() const { - size_t size_bytes; - int ret = - hicn_packet_get_signature_size(format_, packet_start_, &size_bytes); - - if (ret < 0) { - throw errors::RuntimeException("Packet without Authentication Header."); - } - - return size_bytes; - } - - uint8_t *getSignature() const; + void prependPayload(const uint8_t **buffer, std::size_t *size); protected: + hicn_packet_buffer_t pkbuf_; Name name_; - MemBufPtr packet_; - hicn_header_t *packet_start_; - utils::MemBuf *header_head_; - utils::MemBuf *payload_head_; - mutable Format format_; + mutable PayloadType payload_type_; static const core::Name base_name; }; |