nfd::face::PcapHelper Class Reference

Helper class for dealing with libpcap handles. More...

#include <daemon/face/pcap-helper.hpp>

+ Inheritance diagram for nfd::face::PcapHelper:
+ Collaboration diagram for nfd::face::PcapHelper:

Classes

class  Error
 

Public Member Functions

 PcapHelper (const std::string &interfaceName)
 Create a libpcap context for live packet capture on a network interface. More...
 
 ~PcapHelper () noexcept
 
void activate (int dlt)
 Start capturing packets. More...
 
void close () noexcept
 Stop capturing and close the handle. More...
 
int getFd () const
 Obtain a file descriptor that can be used in calls such as select(2) and poll(2). More...
 
std::string getLastError () const noexcept
 Get last error message. More...
 
size_t getNDropped () const
 Get the number of packets dropped by the kernel, as reported by libpcap. More...
 
 operator pcap_t * () const noexcept
 
std::tuple< span< const uint8_t >, std::string > readNextPacket () const noexcept
 Read the next packet captured on the interface. More...
 
void setPacketFilter (const char *filter) const
 Install a BPF filter on the receiving socket. More...
 

Detailed Description

Helper class for dealing with libpcap handles.

Definition at line 44 of file pcap-helper.hpp.

Constructor & Destructor Documentation

◆ PcapHelper()

nfd::face::PcapHelper::PcapHelper ( const std::string &  interfaceName)
explicit

Create a libpcap context for live packet capture on a network interface.

Exceptions
Erroron any error
See also
pcap_create(3pcap)

Definition at line 40 of file pcap-helper.cpp.

◆ ~PcapHelper()

nfd::face::PcapHelper::~PcapHelper ( )
noexcept

Definition at line 58 of file pcap-helper.cpp.

Member Function Documentation

◆ activate()

void nfd::face::PcapHelper::activate ( int  dlt)

Start capturing packets.

Parameters
dltThe link-layer header type to be used.
Exceptions
Erroron any error
See also
pcap_activate(3pcap), pcap_set_datalink(3pcap)

Definition at line 64 of file pcap-helper.cpp.

◆ close()

void nfd::face::PcapHelper::close ( )
noexcept

Stop capturing and close the handle.

See also
pcap_close(3pcap)

Definition at line 84 of file pcap-helper.cpp.

◆ getFd()

int nfd::face::PcapHelper::getFd ( ) const

Obtain a file descriptor that can be used in calls such as select(2) and poll(2).

Precondition
activate() has been called.
Returns
A selectable file descriptor. It is the caller's responsibility to close the fd.
Exceptions
Erroron any error
See also
pcap_get_selectable_fd(3pcap)

Definition at line 93 of file pcap-helper.cpp.

◆ getLastError()

std::string nfd::face::PcapHelper::getLastError ( ) const
noexcept

Get last error message.

Returns
Human-readable explanation of the last libpcap error.
Warning
The behavior is undefined if no error occurred.
See also
pcap_geterr(3pcap)

Definition at line 105 of file pcap-helper.cpp.

◆ getNDropped()

size_t nfd::face::PcapHelper::getNDropped ( ) const

Get the number of packets dropped by the kernel, as reported by libpcap.

Exceptions
Erroron any error
See also
pcap_stats(3pcap)

Definition at line 111 of file pcap-helper.cpp.

◆ operator pcap_t *()

nfd::face::PcapHelper::operator pcap_t * ( ) const
inlinenoexcept

Definition at line 128 of file pcap-helper.hpp.

◆ readNextPacket()

std::tuple< span< const uint8_t >, std::string > nfd::face::PcapHelper::readNextPacket ( ) const
noexcept

Read the next packet captured on the interface.

Returns
If successful, returns a tuple containing a read-only view of the received packet bytes (including the link-layer header) and a second element that must be ignored. On failure, returns a tuple containing an empty span and the reason for the failure.
Warning
The returned span is valid only until the next call to this function.
See also
pcap_next_ex(3pcap)

Definition at line 134 of file pcap-helper.cpp.

◆ setPacketFilter()

void nfd::face::PcapHelper::setPacketFilter ( const char *  filter) const

Install a BPF filter on the receiving socket.

Parameters
filterNull-terminated string containing the BPF program source.
Precondition
activate() has been called.
Exceptions
Erroron any error
See also
pcap_setfilter(3pcap), pcap-filter(7)

Definition at line 121 of file pcap-helper.cpp.