Skip to content

Commit

Permalink
Append padding to IPv6 options
Browse files Browse the repository at this point in the history
Relates to mfontanini#270
  • Loading branch information
mfontanini committed Dec 12, 2017
1 parent a3dd057 commit 8f85a6e
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 0 deletions.
1 change: 1 addition & 0 deletions include/tins/ipv6.h
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,7 @@ class TINS_API IPv6 : public PDU {
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);
static uint32_t get_padding_size(const ext_header& header);

TINS_BEGIN_PACK
struct ipv6_header {
Expand Down
9 changes: 9 additions & 0 deletions src/ipv6.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,11 @@ bool IPv6::is_extension_header(uint8_t header_id) {
|| header_id == MOBILITY || header_id == NO_NEXT_HEADER;
}

uint32_t IPv6::get_padding_size(const ext_header& header) {
const uint32_t padding = (header.data_size() + sizeof(uint8_t) * 2) % 8;
return padding == 0 ? 0 : (8 - padding);
}

void IPv6::version(small_uint<4> new_version) {
header_.version = new_version;
}
Expand Down Expand Up @@ -337,6 +342,8 @@ uint32_t IPv6::calculate_headers_size() const {
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);
output += get_padding_size(*iter);

}
return output;
}
Expand All @@ -346,6 +353,8 @@ void IPv6::write_header(const ext_header& header, OutputMemoryStream& stream) {
stream.write(header.option());
stream.write(length);
stream.write(header.data_ptr(), header.data_size());
// Append padding
stream.fill(get_padding_size(header), 0);
}

} // Tins
6 changes: 6 additions & 0 deletions tests/src/ipv6_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -360,3 +360,9 @@ TEST_F(IPv6Test, OptionAddition) {
EXPECT_TRUE(ipv6.search_header(IPv6::ROUTING) != 0);
EXPECT_TRUE(ipv6.search_header(IPv6::AUTHENTICATION) != 0);
}

TEST_F(IPv6Test, HopByHopPadding) {
IPv6 ipv6_header;
ipv6_header.add_header(IPv6::ExtensionHeader::HOP_BY_HOP);
EXPECT_EQ(48UL, ipv6_header.serialize().size());
}

0 comments on commit 8f85a6e

Please sign in to comment.