From 2a8101eef322f61776a9949ee0d6d2b0be170a5f Mon Sep 17 00:00:00 2001 From: Jens Keim Date: Tue, 15 Jan 2019 12:52:02 +0100 Subject: [PATCH 1/2] add advertised_size method --- include/tins/ip.h | 4 ++++ include/tins/pdu.h | 6 ++++++ src/pdu.cpp | 8 ++++++++ 3 files changed, 18 insertions(+) diff --git a/include/tins/ip.h b/include/tins/ip.h index 6d608ac..07e23e3 100644 --- a/include/tins/ip.h +++ b/include/tins/ip.h @@ -281,6 +281,10 @@ public: /* Getters */ + uint32_t advertised_size() const { + return static_cast(tot_len()); + } + /** * \brief Getter for the header length field. * diff --git a/include/tins/pdu.h b/include/tins/pdu.h index b2165c1..212a973 100644 --- a/include/tins/pdu.h +++ b/include/tins/pdu.h @@ -281,6 +281,12 @@ public: */ uint32_t size() const; + /** \brief The whole chain of PDU's advertised size, including this one. + * + * Returns the sum of this and all children PDU's advertised size. + */ + virtual uint32_t advertised_size() const; + /** * \brief Getter for the inner PDU. * \return The current inner PDU. Might be a null pointer. diff --git a/src/pdu.cpp b/src/pdu.cpp index 8328ece..832d6ea 100644 --- a/src/pdu.cpp +++ b/src/pdu.cpp @@ -85,6 +85,14 @@ uint32_t PDU::size() const { return sz; } +uint32_t PDU::advertised_size() const { + uint32_t result = header_size() + trailer_size(); + if (inner_pdu_) { + result += inner_pdu()->advertised_size(); + } + return result; +} + void PDU::send(PacketSender &, const NetworkInterface &) { } From dc702f4fd61c1828f87509a09bf5609568ed89fc Mon Sep 17 00:00:00 2001 From: Jens Keim Date: Wed, 16 Jan 2019 11:51:23 +0100 Subject: [PATCH 2/2] use advertised_size to determine frame length --- src/packet_writer.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/packet_writer.cpp b/src/packet_writer.cpp index 613f070..7e79a10 100644 --- a/src/packet_writer.cpp +++ b/src/packet_writer.cpp @@ -70,12 +70,12 @@ void PacketWriter::write(Packet& packet) { } void PacketWriter::write(PDU& pdu, const struct timeval& tv) { - PDU::serialization_type buffer = pdu.serialize(); struct pcap_pkthdr header; memset(&header, 0, sizeof(header)); header.ts = tv; + header.len = static_cast(pdu.advertised_size()); + PDU::serialization_type buffer = pdu.serialize(); header.caplen = static_cast(buffer.size()); - header.len = static_cast(buffer.size()); pcap_dump((u_char*)dumper_, &header, &buffer[0]); }