diff --git a/include/tins/ipv6.h b/include/tins/ipv6.h index 2e2283a..0fbaaa7 100644 --- a/include/tins/ipv6.h +++ b/include/tins/ipv6.h @@ -356,6 +356,7 @@ private: 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 { diff --git a/include/tins/pdu.h b/include/tins/pdu.h index 6f6d5c0..62dc0f8 100644 --- a/include/tins/pdu.h +++ b/include/tins/pdu.h @@ -244,8 +244,9 @@ public: * \param rhs The PDU to be moved. */ PDU& operator=(PDU &&rhs) TINS_NOEXCEPT { + delete inner_pdu_; + inner_pdu_ = 0; std::swap(inner_pdu_, rhs.inner_pdu_); - rhs.inner_pdu_ = 0; if (inner_pdu_) { inner_pdu_->parent_pdu(this); } diff --git a/src/ipv6.cpp b/src/ipv6.cpp index c81b843..02967f3 100644 --- a/src/ipv6.cpp +++ b/src/ipv6.cpp @@ -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; } @@ -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(iter->data_size() + sizeof(uint8_t) * 2); + output += get_padding_size(*iter); + } return output; } @@ -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 diff --git a/tests/src/ipv6_test.cpp b/tests/src/ipv6_test.cpp index 029a161..e317456 100644 --- a/tests/src/ipv6_test.cpp +++ b/tests/src/ipv6_test.cpp @@ -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()); +} diff --git a/tests/src/pdu_test.cpp b/tests/src/pdu_test.cpp index 483dc3e..faee7e6 100644 --- a/tests/src/pdu_test.cpp +++ b/tests/src/pdu_test.cpp @@ -83,6 +83,14 @@ TEST_F(PDUTest, OperatorConcatOnPacket) { EXPECT_TRUE(std::equal(raw->payload().begin(), raw->payload().end(), raw_payload.begin())); } +#if TINS_IS_CXX11 +TEST_F(PDUTest, MoveAssignment) { + IP packet = IP("192.168.0.1") / TCP(22, 52); + packet = IP("1.2.3.4"); + EXPECT_TRUE(packet.inner_pdu() == 0); +} +#endif // TINS_IS_CXX11 + TEST_F(PDUTest, TinsCast) { PDU* null_pdu = 0; TCP tcp;