NS-3 based Named Data Networking (NDN) simulator
ndnSIM 2.5: NDN, CCN, CCNx, content centric networks
API Documentation
nfd::face::PcapHelper Class Reference

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

#include <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 () 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...
 
void setPacketFilter (const char *filter) const
 Install a BPF filter on the receiving socket. More...
 
std::tuple< span< const uint8_t >, std::string > readNextPacket () const noexcept
 Read the next packet captured on the interface. More...
 
 operator pcap_t * () const noexcept
 

Detailed Description

Helper class for dealing with libpcap handles.

Definition at line 45 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 41 of file pcap-helper.cpp.

References ndn::ethernet::HDR_LEN, ndn::MAX_NDN_PACKET_SIZE, and NDN_THROW.

Referenced by nfd::face::PcapHelper::Error::Error().

◆ ~PcapHelper()

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

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

References close().

Referenced by nfd::face::PcapHelper::Error::Error().

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

References getLastError(), NDN_THROW, and nfd::PrivilegeHelper::runElevated().

Referenced by nfd::face::PcapHelper::Error::Error(), nfd::face::EthernetTransport::EthernetTransport(), and nfd::face::EthernetChannel::listen().

◆ close()

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

Stop capturing and close the handle.

See also
pcap_close(3pcap)

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

Referenced by nfd::face::EthernetTransport::doClose(), nfd::face::PcapHelper::Error::Error(), and ~PcapHelper().

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

References NDN_THROW.

Referenced by nfd::face::PcapHelper::Error::Error(), nfd::face::EthernetTransport::EthernetTransport(), and nfd::face::EthernetChannel::listen().

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

Referenced by activate(), nfd::face::EthernetTransport::doClose(), nfd::face::PcapHelper::Error::Error(), getNDropped(), readNextPacket(), and setPacketFilter().

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

References getLastError(), and NDN_THROW.

Referenced by nfd::face::EthernetTransport::doClose(), nfd::face::PcapHelper::Error::Error(), and nfd::face::EthernetChannel::listen().

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

References getLastError(), NDN_THROW, and PCAP_NETMASK_UNKNOWN.

Referenced by nfd::face::PcapHelper::Error::Error(), nfd::face::EthernetChannel::listen(), nfd::face::MulticastEthernetTransport::MulticastEthernetTransport(), and nfd::face::UnicastEthernetTransport::UnicastEthernetTransport().

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

References getLastError().

Referenced by nfd::face::EthernetTransport::doClose(), nfd::face::PcapHelper::Error::Error(), and nfd::face::EthernetChannel::listen().

◆ operator pcap_t *()

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

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


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