From 8f85a6e557556b6723cf218fe1eec8b743c7b59b Mon Sep 17 00:00:00 2001 From: Matias Fontanini Date: Tue, 12 Dec 2017 10:29:57 -0300 Subject: [PATCH 1/3] Append padding to IPv6 options Relates to #270 --- include/tins/ipv6.h | 1 + src/ipv6.cpp | 9 +++++++++ tests/src/ipv6_test.cpp | 6 ++++++ 3 files changed, 16 insertions(+) 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/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()); +} From f44b253a42506763f1fe066503f0802e4862274b Mon Sep 17 00:00:00 2001 From: Matias Fontanini Date: Thu, 14 Dec 2017 14:42:46 -0300 Subject: [PATCH 2/3] Fix memory leak in PDU's move assignment operator Fixes #272 --- include/tins/pdu.h | 3 ++- tests/src/pdu_test.cpp | 8 ++++++++ 2 files changed, 10 insertions(+), 1 deletion(-) 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/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; From f7fc5fae1d2046c47ca92b9485f632e58961e29d Mon Sep 17 00:00:00 2001 From: Matias Fontanini Date: Sun, 3 Dec 2017 16:04:42 -0800 Subject: [PATCH 3/3] Update CHANGES.md file using v4.0 changes --- CHANGES.md | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) diff --git a/CHANGES.md b/CHANGES.md index cfe92a0..0923de5 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -1,3 +1,55 @@ +##### v4.0 - Mon Dec 4 00:04:30 UTC 2017 + +- Add parent PDU to each PDU. + +- Removed parent PDU parameter on `PDU::write_serialization`. + +- Split `utils.h` into multiple files under the `utils` directory. + +- Split `internals.h` into multiple files under the `detail` directory. + +- Improve compilation times by removing useless include directives. + +- Refactor `PDUOption` conversions so that heavy headers are not included in source file. + +- Use `std::vector` instead of `std::list` in `TCP`, `IP`, `IPv6`, `DHCP`, `DHCPv6`, `DNS`, `LLC`, `Dot11` and `PPPoE`. + +- Improve performance on `IP`, `IPv6` and `TCP` by compiting option sizes during serialization. + +- Minor performance improvements in `DNS`. + +- Fix `IPv6` next header handling. Now each one contains its own type and the next type is only set during serialization for ease of use. + +- Refactor `RadioTap` parsing and serialization using a generic parser/writer. + +- Add `BaseSniffer::set_pcap_sniffing_method` to specify whether `pcap_loop` or `pcap_dispatch` should be used when sniffing. + +- Use `IFF_POINTOPOINT` on BSD when getting broadcast address for an interface. + +- Added cipher and akm suites from 802.11-2016. + +- Add IPv6 layer parsing on `Loopback` packets. + +- Allow serializing `Loopback` on Windows. + +- Use the right flag on `Loopback` for `IPv6`. + +- Use the first fragment as a base when reassembling `IP` packets in `IPv4Reassembler`. + +- Restructure CMake files removing useless `CMakeLists.txt` in `include` paths. + +- Add getter/setter for "more data" field in `Dot11Base`. + +- Implemented matching for ND protocol related ICMPv6 messages. + +- Ensure TCP::OptionTypes has 8-bit range. + +- Add header files into CMake sources so IDE can pick them up. + +- Add MPLS "experimental" field. + +- Fix dhcpv6::duid_type constructor from duid_ll. + ##### v3.5 - Sat Apr 1 09:11:58 PDT 2017 - Added Utils::route6_entries