diff options
Diffstat (limited to 'libtransport/includes/hicn/transport/core/packet.h')
-rw-r--r-- | libtransport/includes/hicn/transport/core/packet.h | 143 |
1 files changed, 64 insertions, 79 deletions
diff --git a/libtransport/includes/hicn/transport/core/packet.h b/libtransport/includes/hicn/transport/core/packet.h index 91f957964..68daea841 100644 --- a/libtransport/includes/hicn/transport/core/packet.h +++ b/libtransport/includes/hicn/transport/core/packet.h @@ -19,21 +19,15 @@ #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/auth/crypto_hasher.h> +#include <hicn/transport/auth/crypto_suite.h> +#include <hicn/transport/auth/key_id.h> #include <hicn/transport/utils/branch_prediction.h> #include <hicn/transport/utils/log.h> #include <hicn/transport/utils/membuf.h> #include <hicn/transport/utils/object_pool.h> -namespace utils { -class Signer; -class Verifier; -} // namespace utils - namespace transport { - namespace core { /* @@ -45,11 +39,13 @@ 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; static constexpr size_t default_mtu = 1500; @@ -59,24 +55,29 @@ 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); + Packet(Format format = HF_INET6_TCP, std::size_t additional_header_size = 0); /** * 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; + /* 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, uint8_t *buffer, std::size_t length, std::size_t size, + Format format = HF_INET6_TCP, std::size_t additional_header_size = 0); + /* Move MemBuf */ + Packet(MemBuf &&buffer); + + Packet(Packet &&other); /* - * Move constructor. + * Copy constructor and assignemnt operators. */ - Packet(Packet &&other); + Packet(const Packet &other); + Packet &operator=(const Packet &other); friend bool operator==(const Packet &l_packet, const Packet &r_packet); @@ -98,36 +99,35 @@ class Packet : public std::enable_shared_from_this<Packet> { static bool isInterest(const uint8_t *buffer); + bool isInterest(); + static Format getFormatFromBuffer(const uint8_t *buffer, std::size_t length) { Format format = HF_UNSPEC; - - if (TRANSPORT_EXPECT_FALSE( - hicn_packet_get_format((const hicn_header_t *)buffer, &format) < - 0)) { - TRANSPORT_LOGE( - "Error while getting format from packet buffer. Packet will be " - "discarded."); - hicn_packet_dump(buffer, length); - } - + hicn_packet_get_format((const hicn_header_t *)buffer, &format); 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_), - packet_->length()); + void reset() { + clear(); + packet_start_ = reinterpret_cast<hicn_header_t *>(writableData()); + header_offset_ = 0; + format_ = HF_UNSPEC; + payload_type_ = PayloadType::UNSPECIFIED; name_.clear(); + + if (isChained()) { + separateChain(next(), prev()); + } } + void setFormat(Packet::Format format = HF_INET6_TCP, + std::size_t additional_header_size = 0); + std::size_t payloadSize() const; std::size_t headerSize() const; - const std::shared_ptr<utils::MemBuf> acquireMemBufReference() const; + std::shared_ptr<utils::MemBuf> acquireMemBufReference(); virtual const Name &getName() const = 0; @@ -145,25 +145,8 @@ class Packet : public std::enable_shared_from_this<Packet> { Packet &appendPayload(std::unique_ptr<utils::MemBuf> &&payload); - Packet &appendHeader(std::unique_ptr<utils::MemBuf> &&header); - - Packet &appendHeader(const uint8_t *buffer, std::size_t length); - std::unique_ptr<utils::MemBuf> getPayload() const; - std::pair<const uint8_t *, std::size_t> getPayloadReference() const { - int signature_size = 0; - - if (_is_ah(format_)) { - signature_size = (uint32_t)getSignatureSize(); - } - - auto header_size = getHeaderSizeFromFormat(format_, signature_size); - auto payload_length = payloadSize(); - - return std::make_pair(packet_->data() + header_size, payload_length); - } - Packet &updateLength(std::size_t length = 0); PayloadType getPayloadType() const; @@ -174,35 +157,38 @@ class Packet : public std::enable_shared_from_this<Packet> { void dump() const; + static void dump(uint8_t *buffer, std::size_t length); + virtual void setLocator(const ip_address_t &locator) = 0; virtual ip_address_t getLocator() const = 0; - void setSignatureTimestamp(const uint64_t ×tamp); + /** + * @brief Set signature timestamp, in milliseconds. + */ + void setSignatureTimestamp(const uint64_t ×tamp_milliseconds); uint64_t getSignatureTimestamp() const; - void setValidationAlgorithm(const utils::CryptoSuite &validation_algorithm); + void setValidationAlgorithm(const auth::CryptoSuite &validation_algorithm); - utils::CryptoSuite getValidationAlgorithm() const; + auth::CryptoSuite getValidationAlgorithm() const; - void setKeyId(const utils::KeyId &key_id); + void setKeyId(const auth::KeyId &key_id); - utils::KeyId getKeyId() const; + auth::KeyId getKeyId() const; - virtual utils::CryptoHash computeDigest( - utils::CryptoHashType algorithm) const; + virtual auth::CryptoHash computeDigest(auth::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); + uint16_t partial_csum = + csum(data() + HICN_V6_TCP_HDRLEN, length() - HICN_V6_TCP_HDRLEN, 0); + + for (utils::MemBuf *current = next(); current != this; + current = current->next()) { + partial_csum = csum(current->data(), current->length(), ~partial_csum); } + if (hicn_packet_compute_header_checksum(format_, packet_start_, partial_csum) < 0) { throw errors::MalformedPacketException(); @@ -234,12 +220,12 @@ class Packet : public std::enable_shared_from_this<Packet> { Packet &setTTL(uint8_t hops); uint8_t getTTL() const; - void separateHeaderPayload(); - void resetPayload(); - private: virtual void resetForHash() = 0; void setSignatureSize(std::size_t size_bytes); + void prependPayload(const uint8_t **buffer, std::size_t *size); + + bool authenticationHeader() const { return _is_ah(format_); } std::size_t getSignatureSize() const { size_t size_bytes; @@ -256,12 +242,11 @@ class Packet : public std::enable_shared_from_this<Packet> { uint8_t *getSignature() const; protected: - Name name_; - MemBufPtr packet_; hicn_header_t *packet_start_; - utils::MemBuf *header_head_; - utils::MemBuf *payload_head_; + std::size_t header_offset_; mutable Format format_; + Name name_; + mutable PayloadType payload_type_; static const core::Name base_name; }; |