From 0f3441ccf622175ab038de3fe2c1e68844e80680 Mon Sep 17 00:00:00 2001 From: Matias Fontanini Date: Tue, 26 May 2015 21:33:13 -0700 Subject: [PATCH] Add PacketWriter::write overload that takes a Packet. --- include/tins/packet_writer.h | 15 +++++++++++++++ include/tins/pdu_option.h | 1 + src/packet_writer.cpp | 22 +++++++++++++++++----- 3 files changed, 33 insertions(+), 5 deletions(-) diff --git a/include/tins/packet_writer.h b/include/tins/packet_writer.h index 43e986b..2664e6a 100644 --- a/include/tins/packet_writer.h +++ b/include/tins/packet_writer.h @@ -37,8 +37,11 @@ #include "utils.h" #include "cxxstd.h" +struct timeval; + namespace Tins { class PDU; +class Packet; /** * \class PacketWriter @@ -167,8 +170,19 @@ public: /** * \brief Writes a PDU to this file. + * \param pdu The PDU to be written. */ void write(PDU &pdu); + + /** + * \brief Writes a Packet to this file. + * + * The timestamp used on the entry for this packet will be the Timestamp + * object associated with this packet. + * + * \param packet The packet to be written. + */ + void write(Packet &packet); /** * \brief Writes a PDU to this file. @@ -200,6 +214,7 @@ private: PacketWriter& operator=(const PacketWriter&); void init(const std::string& file_name, int link_type); + void write(PDU& pdu, const struct timeval& tv); pcap_t *handle; pcap_dumper_t *dumper; diff --git a/include/tins/pdu_option.h b/include/tins/pdu_option.h index b71a479..d131568 100644 --- a/include/tins/pdu_option.h +++ b/include/tins/pdu_option.h @@ -35,6 +35,7 @@ #include #include #include +#include #include #include "exceptions.h" #include "endianness.h" diff --git a/src/packet_writer.cpp b/src/packet_writer.cpp index 032165c..d8ac055 100644 --- a/src/packet_writer.cpp +++ b/src/packet_writer.cpp @@ -32,6 +32,7 @@ #endif #include #include "packet_writer.h" +#include "packet.h" #include "pdu.h" namespace Tins { @@ -47,16 +48,27 @@ PacketWriter::~PacketWriter() { } void PacketWriter::write(PDU &pdu) { - PDU::serialization_type buffer = pdu.serialize(); - timeval tm; + timeval tv; #ifndef _WIN32 - gettimeofday(&tm, 0); + gettimeofday(&tv, 0); #else // fixme - tm = timeval(); + tv = timeval(); #endif + write(pdu, tv); +} + +void PacketWriter::write(Packet &packet) { + timeval tv; + tv.tv_sec = packet.timestamp().seconds(); + tv.tv_usec = packet.timestamp().microseconds(); + write(*packet.pdu(), tv); +} + +void PacketWriter::write(PDU& pdu, const struct timeval& tv) { + PDU::serialization_type buffer = pdu.serialize(); struct pcap_pkthdr header = { - tm, + tv, static_cast(buffer.size()), static_cast(buffer.size()) };