1
0
mirror of https://github.com/mfontanini/libtins synced 2026-01-23 02:35:57 +01:00

Add PacketWriter::write overload that takes a Packet.

This commit is contained in:
Matias Fontanini
2015-05-26 21:33:13 -07:00
parent 9c25f635eb
commit 0f3441ccf6
3 changed files with 33 additions and 5 deletions

View File

@@ -37,8 +37,11 @@
#include "utils.h" #include "utils.h"
#include "cxxstd.h" #include "cxxstd.h"
struct timeval;
namespace Tins { namespace Tins {
class PDU; class PDU;
class Packet;
/** /**
* \class PacketWriter * \class PacketWriter
@@ -167,8 +170,19 @@ public:
/** /**
* \brief Writes a PDU to this file. * \brief Writes a PDU to this file.
* \param pdu The PDU to be written.
*/ */
void write(PDU &pdu); 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. * \brief Writes a PDU to this file.
@@ -200,6 +214,7 @@ private:
PacketWriter& operator=(const PacketWriter&); PacketWriter& operator=(const PacketWriter&);
void init(const std::string& file_name, int link_type); void init(const std::string& file_name, int link_type);
void write(PDU& pdu, const struct timeval& tv);
pcap_t *handle; pcap_t *handle;
pcap_dumper_t *dumper; pcap_dumper_t *dumper;

View File

@@ -35,6 +35,7 @@
#include <cstring> #include <cstring>
#include <algorithm> #include <algorithm>
#include <string> #include <string>
#include <limits>
#include <stdint.h> #include <stdint.h>
#include "exceptions.h" #include "exceptions.h"
#include "endianness.h" #include "endianness.h"

View File

@@ -32,6 +32,7 @@
#endif #endif
#include <stdexcept> #include <stdexcept>
#include "packet_writer.h" #include "packet_writer.h"
#include "packet.h"
#include "pdu.h" #include "pdu.h"
namespace Tins { namespace Tins {
@@ -47,16 +48,27 @@ PacketWriter::~PacketWriter() {
} }
void PacketWriter::write(PDU &pdu) { void PacketWriter::write(PDU &pdu) {
PDU::serialization_type buffer = pdu.serialize(); timeval tv;
timeval tm;
#ifndef _WIN32 #ifndef _WIN32
gettimeofday(&tm, 0); gettimeofday(&tv, 0);
#else #else
// fixme // fixme
tm = timeval(); tv = timeval();
#endif #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 = { struct pcap_pkthdr header = {
tm, tv,
static_cast<bpf_u_int32>(buffer.size()), static_cast<bpf_u_int32>(buffer.size()),
static_cast<bpf_u_int32>(buffer.size()) static_cast<bpf_u_int32>(buffer.size())
}; };