libtins  4.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Pages
Public Types | Public Member Functions | List of all members
Tins::PacketWriter Class Reference

Writes PDUs to a pcap format file. More...

#include <packet_writer.h>

Public Types

enum  LinkType {
  RADIOTAP = DLT_IEEE802_11_RADIO, DOT11 = DLT_IEEE802_11, ETH2 = DLT_EN10MB, DOT3 = DLT_EN10MB,
  SLL = DLT_LINUX_SLL
}
 The type of PDUs that will be written to this file (deprecated). More...
 

Public Member Functions

template<typename T >
 PacketWriter (const std::string &file_name, const DataLinkType< T > &lt)
 Constructs a PacketWriter. More...
 
 PacketWriter (const std::string &file_name, LinkType lt)
 Constructs a PacketWriter. More...
 
 PacketWriter (PacketWriter &&rhs) TINS_NOEXCEPT
 Move constructor. More...
 
PacketWriteroperator= (PacketWriter &&rhs) TINS_NOEXCEPT
 Move assignment operator. More...
 
 ~PacketWriter ()
 Destructor. More...
 
void write (PDU &pdu)
 Writes a PDU to this file. More...
 
void write (Packet &packet)
 Writes a Packet to this file. More...
 
template<typename T >
void write (T &pdu)
 Writes a PDU to this file. More...
 
template<typename ForwardIterator >
void write (ForwardIterator start, ForwardIterator end)
 Writes all the PDUs in the range [start, end) More...
 

Detailed Description

Writes PDUs to a pcap format file.

This class can be used to write packets into a pcap format file. It supports both writing packets one by one, or writing all packets in a range (provided by iterators), so you can use it to dump all packets in a vector.

Since you might use both PDU objects and pointers to them, both the PacketWriter::write overload that takes a single object or the one that takes an iterator range accept a PDU reference type as well as any type that can be dereferenced until a PDU type is found. This means you can use both raw and smart pointers.

For example:

// Differents types holding PDUs
EthernetII object;
std::shared_ptr<PDU> smart_ptr = ...;
std::vector<std::shared_ptr<PDU>> vt = ....;
// The writer we'll use
PacketWriter writer("/tmp/file.pcap", DataLinkType<EthernetII>());
// Now write all of them
writer.write(object);
writer.write(smart_ptr);
writer.write(vt.begin(), vt.end());

Member Enumeration Documentation

The type of PDUs that will be written to this file (deprecated).

Deprecated:
Use DataLinkType instead of this enum.

This flag should match the type of the lowest layer PDU to be written.

Constructor & Destructor Documentation

template<typename T >
Tins::PacketWriter::PacketWriter ( const std::string &  file_name,
const DataLinkType< T > &  lt 
)
inline

Constructs a PacketWriter.

This method takes a DataLinkType, which indicates the link layer protocol that will be used on the packets to write.

For example, you can write packets that contain an EthernetII link layer type by doing:

// Construct a PacketWriter
PacketWriter writer("/tmp/test.pcap", DataLinkType<EthernetII>());
// Write some packet
writer.write(packet);
Parameters
file_nameThe file in which to store the written PDUs.
ltA DataLinkType that represents the link layer protocol to use.
See Also
PcapIdentifier.
Tins::PacketWriter::PacketWriter ( const std::string &  file_name,
LinkType  lt 
)

Constructs a PacketWriter.

Deprecated:
Use the PacketWriter(const std::string&, const DataLinkType<T>&) constructor.
Parameters
file_nameThe file in which to store the written PDUs.
ltThe link type which will be written to this file.
See Also
LinkType.
Tins::PacketWriter::PacketWriter ( PacketWriter &&  rhs)
inline

Move constructor.

Note that calling PacketWriter::write on an previously moved object will lead to undefined behaviour.

Parameters
rhsThe PacketWriter to be moved.
Tins::PacketWriter::~PacketWriter ( )

Destructor.

Gracefully closes the output file.

Member Function Documentation

PacketWriter& Tins::PacketWriter::operator= ( PacketWriter &&  rhs)
inline

Move assignment operator.

Note that calling PacketWriter::write on an previously moved object will lead to undefined behaviour.

Parameters
rhsThe PacketWriter to be moved.
void Tins::PacketWriter::write ( PDU pdu)

Writes a PDU to this file.

Parameters
pduThe PDU to be written.
void Tins::PacketWriter::write ( Packet packet)

Writes a Packet to this file.

The timestamp used on the entry for this packet will be the Timestamp object associated with this packet.

Parameters
packetThe packet to be written.
template<typename T >
void Tins::PacketWriter::write ( T &  pdu)
inline

Writes a PDU to this file.

The template parameter T must at some point yield a PDU& after applying operator* one or more than one time. This accepts both raw and smart pointers.

template<typename ForwardIterator >
void Tins::PacketWriter::write ( ForwardIterator  start,
ForwardIterator  end 
)
inline

Writes all the PDUs in the range [start, end)

Parameters
startA forward iterator pointing to the first PDU to be written.
endA forward iterator pointing to one past the last PDU in the range.

The documentation for this class was generated from the following files: