aboutsummaryrefslogtreecommitdiffstats
path: root/libtransport/includes/hicn/transport/core/packet.h
diff options
context:
space:
mode:
Diffstat (limited to 'libtransport/includes/hicn/transport/core/packet.h')
-rw-r--r--libtransport/includes/hicn/transport/core/packet.h248
1 files changed, 79 insertions, 169 deletions
diff --git a/libtransport/includes/hicn/transport/core/packet.h b/libtransport/includes/hicn/transport/core/packet.h
index 269a1571a..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:
@@ -26,15 +26,18 @@
#include <hicn/transport/utils/membuf.h>
#include <hicn/transport/utils/object_pool.h>
+extern "C" {
+#ifndef _WIN32
+TRANSPORT_CLANG_DISABLE_WARNING("-Wextern-c-compat")
+#endif
+#include <hicn/packet.h>
+}
+
namespace transport {
namespace auth {
class Signer;
-class AsymmetricSigner;
-class SymmetricSigner;
class Verifier;
-class AsymmetricVerifier;
-class SymmetricVerifier;
} // namespace auth
namespace core {
@@ -51,16 +54,14 @@ namespace core {
class Packet : public utils::MemBuf,
public std::enable_shared_from_this<Packet> {
friend class auth::Signer;
- friend class auth::SymmetricSigner;
- friend class auth::AsymmetricSigner;
friend class auth::Verifier;
- friend class auth::AsymmetricVerifier;
- friend class auth::SymmetricVerifier;
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;
/**
@@ -68,211 +69,120 @@ class Packet : public utils::MemBuf,
* 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_INET6_TCP, std::size_t additional_header_size = 0);
-
- /**
- * Create new IP packet using raw buffer.
- */
-
+ 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, 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(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);
-
- /*
- * Copy constructor and assignemnt operators.
- */
Packet(const Packet &other);
- Packet &operator=(const Packet &other);
-
- friend bool operator==(const Packet &l_packet, const Packet &r_packet);
+ // Destructor
virtual ~Packet();
- 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);
-
- bool isInterest();
-
- static Format getFormatFromBuffer(const uint8_t *buffer, std::size_t length) {
- Format format = HF_UNSPEC;
- hicn_packet_get_format((const hicn_header_t *)buffer, &format);
- return format;
- }
-
- void reset() {
- clear();
- packet_start_ = reinterpret_cast<hicn_header_t *>(writableData());
- header_offset_ = 0;
- format_ = HF_UNSPEC;
- payload_type_ = PayloadType::UNSPECIFIED;
- name_.clear();
+ // Operators
+ Packet &operator=(const Packet &other);
+ friend bool operator==(const Packet &l_packet, const Packet &r_packet);
- if (isChained()) {
- separateChain(next(), prev());
- }
- }
+ // Cast to MemBuf
+ std::shared_ptr<utils::MemBuf> acquireMemBufReference();
- void setFormat(Packet::Format format = HF_INET6_TCP,
- std::size_t additional_header_size = 0);
+ // 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);
- std::shared_ptr<utils::MemBuf> acquireMemBufReference();
+ void setBuffer();
+ // Name
virtual const Name &getName() const = 0;
-
virtual Name &getWritableName() = 0;
-
virtual void setName(const 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);
+ // Locator
+ virtual void setLocator(const hicn_ip_address_t &locator) = 0;
+ virtual hicn_ip_address_t getLocator() const = 0;
- Packet &appendPayload(std::unique_ptr<utils::MemBuf> &&payload);
+ // 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);
- Packet &updateLength(std::size_t length = 0);
+ // Sizes
+ std::size_t headerSize() const;
+ std::size_t payloadSize() const;
- PayloadType getPayloadType() const;
+ // Digest
+ auth::CryptoHash computeDigest(auth::CryptoHashType algorithm) const;
- Packet &setPayloadType(PayloadType payload_type);
+ bool isInterest();
- Format getFormat() const;
+ // Reset packet
+ void reset();
+ // Utils
+ Packet &updateLength(std::size_t length = 0);
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;
-
- /**
- * @brief Set signature timestamp, in milliseconds.
- */
- void setSignatureTimestamp(const uint64_t &timestamp_milliseconds);
+ // TCP methods
+ void setChecksum();
+ bool checkIntegrity() const;
+ // 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;
-
- void setValidationAlgorithm(const auth::CryptoSuite &validation_algorithm);
-
+ 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 &timestamp_ms);
void setKeyId(const auth::KeyId &key_id);
+ void setValidationAlgorithm(const auth::CryptoSuite &algo);
- auth::KeyId getKeyId() const;
-
- virtual auth::CryptoHash computeDigest(auth::CryptoHashType algorithm) const;
-
- void setChecksum() {
- 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();
- }
- }
+ void saveHeader(u8 *header, size_t *header_len);
+ void loadHeader(u8 *header, size_t header_len);
- 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;
+ // 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);
- void setSignatureSizeGap(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;
- 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;
- }
-
- std::size_t getSignatureSizeGap() const {
- uint8_t size_bytes;
- int ret =
- hicn_packet_get_signature_gap(format_, packet_start_, &size_bytes);
-
- if (ret < 0) {
- throw errors::RuntimeException("Packet without Authentication Header.");
- }
-
- return (size_t)size_bytes;
- }
-
- std::size_t getSignatureSizeReal() const {
- return getSignatureSize() - getSignatureSizeGap();
- }
-
- uint8_t *getSignature() const;
-
protected:
- hicn_header_t *packet_start_;
- std::size_t header_offset_;
- mutable Format format_;
+ hicn_packet_buffer_t pkbuf_;
Name name_;
mutable PayloadType payload_type_;
static const core::Name base_name;