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

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

◆ ~PcapHelper()

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

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

References close().

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

References getLastError().

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

◆ close()

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

Stop capturing and close the handle.

See also
pcap_close(3pcap)

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

Referenced by nfd::face::EthernetTransport::doClose(), 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 83 of file pcap-helper.cpp.

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

◆ getLastError()

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

Referenced by activate(), 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 101 of file pcap-helper.cpp.

References getLastError().

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

References getLastError(), and PCAP_NETMASK_UNKNOWN.

Referenced by nfd::face::MulticastEthernetTransport::MulticastEthernetTransport(), and nfd::face::UnicastEthernetTransport::UnicastEthernetTransport().

◆ readNextPacket()

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

References getLastError().

◆ operator pcap_t *()

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

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


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