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 ()
 
void activate (int dlt)
 Start capturing packets. More...
 
void close ()
 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
 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
 
std::tuple< const uint8_t *, size_t, std::string > readNextPacket () const
 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 45 of file pcap-helper.hpp.

Constructor & Destructor Documentation

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 39 of file pcap-helper.cpp.

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

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

Member Function Documentation

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.

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

Stop capturing and close the handle.

See also
pcap_close(3pcap)

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

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 87 of file pcap-helper.cpp.

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

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 99 of file pcap-helper.cpp.

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 105 of file pcap-helper.cpp.

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

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

std::tuple< const uint8_t *, size_t, std::string > nfd::face::PcapHelper::readNextPacket ( ) const

Read the next packet captured on the interface.

Returns
If successful, returns a tuple containing a pointer to the received packet (including the link-layer header) and the size of the packet; the third element must be ignored. On failure, returns a tuple containing nullptr, 0, and the reason for the failure.
Warning
The returned pointer must not be freed by the caller, and is valid only until the next call to this function.
See also
pcap_next_ex(3pcap)

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

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 115 of file pcap-helper.cpp.