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:
@@ -320,6 +320,7 @@ public:
|
|||||||
private:
|
private:
|
||||||
void write_serialization(uint8_t* buffer, uint32_t total_sz);
|
void write_serialization(uint8_t* buffer, uint32_t total_sz);
|
||||||
void set_last_next_header(uint8_t value);
|
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 void write_header(const ext_header& header, Memory::OutputMemoryStream& stream);
|
||||||
static bool is_extension_header(uint8_t header_id);
|
static bool is_extension_header(uint8_t header_id);
|
||||||
|
|
||||||
@@ -345,7 +346,6 @@ private:
|
|||||||
|
|
||||||
ipv6_header header_;
|
ipv6_header header_;
|
||||||
headers_type ext_headers_;
|
headers_type ext_headers_;
|
||||||
uint32_t headers_size_;
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
17
src/ipv6.cpp
17
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*/)
|
IPv6::IPv6(address_type ip_dst, address_type ip_src, PDU* /*child*/)
|
||||||
: header_(), headers_size_(0) {
|
: header_() {
|
||||||
version(6);
|
version(6);
|
||||||
dst_addr(ip_dst);
|
dst_addr(ip_dst);
|
||||||
src_addr(ip_src);
|
src_addr(ip_src);
|
||||||
}
|
}
|
||||||
|
|
||||||
IPv6::IPv6(const uint8_t* buffer, uint32_t total_sz)
|
IPv6::IPv6(const uint8_t* buffer, uint32_t total_sz) {
|
||||||
: headers_size_(0) {
|
|
||||||
InputMemoryStream stream(buffer, total_sz);
|
InputMemoryStream stream(buffer, total_sz);
|
||||||
stream.read(header_);
|
stream.read(header_);
|
||||||
uint8_t current_header = header_.next_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 {
|
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 {
|
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) {
|
void IPv6::add_ext_header(const ext_header& header) {
|
||||||
ext_headers_.push_back(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 {
|
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) {
|
void IPv6::write_header(const ext_header& header, OutputMemoryStream& stream) {
|
||||||
const uint8_t length = header.length_field() / 8;
|
const uint8_t length = header.length_field() / 8;
|
||||||
stream.write(header.option());
|
stream.write(header.option());
|
||||||
|
|||||||
Reference in New Issue
Block a user