From f0aaec98f3221d6b8cbdc594a977e8a079314511 Mon Sep 17 00:00:00 2001 From: Matias Fontanini Date: Sun, 14 May 2017 09:04:51 -0700 Subject: [PATCH] Calculate IPv6 headers size on demand --- include/tins/ipv6.h | 2 +- src/ipv6.cpp | 17 ++++++++++++----- 2 files changed, 13 insertions(+), 6 deletions(-) diff --git a/include/tins/ipv6.h b/include/tins/ipv6.h index 013581d..ba19222 100644 --- a/include/tins/ipv6.h +++ b/include/tins/ipv6.h @@ -320,6 +320,7 @@ public: private: void write_serialization(uint8_t* buffer, uint32_t total_sz); void set_last_next_header(uint8_t value); + uint32_t calculate_headers_size() const; static void write_header(const ext_header& header, Memory::OutputMemoryStream& stream); static bool is_extension_header(uint8_t header_id); @@ -345,7 +346,6 @@ private: ipv6_header header_; headers_type ext_headers_; - uint32_t headers_size_; }; } diff --git a/src/ipv6.cpp b/src/ipv6.cpp index 07170bf..03901fc 100644 --- a/src/ipv6.cpp +++ b/src/ipv6.cpp @@ -70,14 +70,13 @@ PDU::metadata IPv6::extract_metadata(const uint8_t *buffer, uint32_t total_sz) { } IPv6::IPv6(address_type ip_dst, address_type ip_src, PDU* /*child*/) -: header_(), headers_size_(0) { +: header_() { version(6); dst_addr(ip_dst); src_addr(ip_src); } -IPv6::IPv6(const uint8_t* buffer, uint32_t total_sz) -: headers_size_(0) { +IPv6::IPv6(const uint8_t* buffer, uint32_t total_sz) { InputMemoryStream stream(buffer, total_sz); stream.read(header_); uint8_t current_header = header_.next_header; @@ -208,7 +207,7 @@ void IPv6::dst_addr(const address_type& new_dst_addr) { } uint32_t IPv6::header_size() const { - return sizeof(header_) + headers_size_; + return sizeof(header_) + calculate_headers_size(); } bool IPv6::matches_response(const uint8_t* ptr, uint32_t total_sz) const { @@ -282,7 +281,6 @@ void IPv6::send(PacketSender& sender, const NetworkInterface &) { void IPv6::add_ext_header(const ext_header& header) { ext_headers_.push_back(header); - headers_size_ += static_cast(header.data_size() + sizeof(uint8_t) * 2); } const IPv6::ext_header* IPv6::search_header(ExtensionHeader id) const { @@ -307,6 +305,15 @@ void IPv6::set_last_next_header(uint8_t value) { } } +uint32_t IPv6::calculate_headers_size() const { + typedef headers_type::const_iterator const_iterator; + uint32_t output = 0; + for (const_iterator iter = ext_headers_.begin(); iter != ext_headers_.end(); ++iter) { + output += static_cast(iter->data_size() + sizeof(uint8_t) * 2); + } + return output; +} + void IPv6::write_header(const ext_header& header, OutputMemoryStream& stream) { const uint8_t length = header.length_field() / 8; stream.write(header.option());