pcap.h

Class

class PcapReader

Class for dealing with reading pcap files

Subclassed by ouster::sensor_utils::IndexedPcapReader

Public Functions

explicit PcapReader(const std::string &file)
Parameters:

file[in] A filepath of the pcap to read

PcapReader(PcapReader &&other)

Move construct from one PcapReader to a new PcapReader.

Parameters:

other[in] The other PcapReader to move from.

PcapReader &operator=(PcapReader &&other)

Assign move resources from one PcapReader to another.

Parameters:

other[in] The other PcapReader to move from.

virtual ~PcapReader()

Destructor for cleaning up after PcapReader.

size_t next_packet()

Advances to the next packet and returns the size of that packet. Will also populate data and info for next_packet(), current_data(), current_length(), and current_info()

Returns:

The size of the packet payload

const uint8_t *current_data() const

Return the current packets data. To advance to a new packet please use next_packet() To get the size of the data use current_length()

Returns:

A pointer to a byte array containing the packet data

size_t current_length() const

Return the current packets data size. To advance to a new packet please use next_packet()

Returns:

The size of the byte array

const packet_info &current_info() const

Return the current packets info. To advance to a new packet please use next_packet()

Returns:

A packet_info object on the current packet

int64_t file_size() const
Returns:

The size of the PCAP file in bytes

void reset()

Return the read position to the start of the PCAP file

void seek(uint64_t offset)

Seek to the position in the file represented by the number of bytes from the beginning of the file.

Parameters:

offset[in] The position to seek to in bytes, starting from the beginning of the file.

Pre:

offset must be the offset of a PCAP record header. If any other value is provided, subsequent packet reads from this PcapReader will be invalid until PcapReader::reset is called.

class PcapWriter

Class for dealing with writing udp pcap files

Public Types

enum PacketEncapsulation

Enum to describe the current encapsulation for a pcap file

Values:

enumerator NULL_LOOPBACK

Null Loopback Encapsulation.

enumerator ETHERNET

Ethernet II Encapsulation.

enumerator SLL

Linux Cooked Capture Encapsulation.

Public Functions

PcapWriter(const std::string &file, PacketEncapsulation encap, uint16_t frag_size)
Parameters:
  • file[in] The file path to write the pcap to

  • encap[in] The encapsulation to use for the pcap

  • frag_size[in] The fragmentation size to use (Currently broken)

PcapWriter(PcapWriter &&other)

Move construct from one PcapWriter to a new PcapWriter.

Parameters:

other[in] The other PcapWriter to move from.

PcapWriter &operator=(PcapWriter &&other)

Assign move resources from one PcapWriter to another.

Parameters:

other[in] The other PcapWriter to move from.

virtual ~PcapWriter()

Destructor for cleaning up after PcapWriter.

void write_packet(const uint8_t *buf, size_t buf_size, const std::string &src_ip, const std::string &dst_ip, uint16_t src_port, uint16_t dst_port, packet_info::ts timestamp)

Write a packet using a buffer to the pcap

Note

The timestamp parameter does not affect the order of packets being recorded, it is strictly recorded FIFO.

Parameters:
  • buf[in] The buffer to write

  • buf_size[in] The size of the buffer to write

  • src_ip[in] The source ip address to use for the packet

  • dst_ip[in] The destination ip address to use for the packet

  • src_port[in] The source port number to use for the packet

  • dst_port[in] The destination port number to use for the packet

  • timestamp[in] The timestamp of the packet to record

void write_packet(const uint8_t *buf, size_t buf_size, const packet_info &info)

Write a packet using a buffer to the pcap

Note

The timestamp parameter in info does not affect the order of packets being recorded, it is strictly recorded FIFO.

Parameters:
  • buf[in] The buffer to write

  • buf_size[in] The size of the buffer to write

  • info[in] The packet info object to use for the recording parameters

void flush()

Write all pending data to the pcap file

void close()

Flushes and cleans up all memory in use by the pap writer

Structs

struct packet_info

Public Members

std::string dst_ip

The destination IP.

std::string src_ip

The source IP.

int dst_port

The destination port.

int src_port

The source port.

size_t payload_size

The size of the packet payload.

size_t packet_size

The size of the full packet.

ts timestamp

The packet capture timestamp.

int fragments_in_packet

Number of fragments in the packet.

int ip_version

The ip version, 4 or 6.

int encapsulation_protocol

PCAP encapsulation type.

uint64_t file_offset

Where the packet is in the pcap.

int network_protocol

IANA protocol number. Always 17 (UDP)