1
0
mirror of https://github.com/mfontanini/libtins synced 2026-01-23 02:35:57 +01:00

Calculate IPv6 headers size on demand

This commit is contained in:
Matias Fontanini
2017-05-14 09:04:51 -07:00
parent 348371e43c
commit f0aaec98f3
2 changed files with 13 additions and 6 deletions

View File

@@ -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_;
};
}

View File

@@ -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<uint32_t>(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<uint32_t>(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());