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/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]); } 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 &) { }