PcapPlusPlus  Next
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
pcpp::internal::PcapHandle Class Reference

A wrapper class for pcap_t* which is the libpcap packet capture descriptor. This class is used to manage the lifecycle of the pcap_t* object. More...

#include <PcapDevice.h>

Public Member Functions

constexpr PcapHandle () noexcept=default
 Creates an empty handle.
 
 PcapHandle (pcap_t *pcapDescriptor) noexcept
 Creates a handle from the provided pcap descriptor. More...
 
bool isValid () const noexcept
 
pcap_t * get () const noexcept
 
pcap_t * release () noexcept
 Releases ownership of the handle and returns the pcap descriptor. More...
 
void reset (pcap_t *pcapDescriptor=nullptr) noexcept
 Replaces the managed handle with the provided one. More...
 
char const * getLastError () const noexcept
 Helper function to retrieve a view of the last error string for this handle. More...
 
bool setFilter (std::string const &filter)
 Sets a filter on the handle. Only packets that match the filter will be captured by the handle. More...
 
bool clearFilter ()
 Clears the filter currently set on the handle. More...
 
 operator bool () const noexcept
 

Detailed Description

A wrapper class for pcap_t* which is the libpcap packet capture descriptor. This class is used to manage the lifecycle of the pcap_t* object.

Constructor & Destructor Documentation

◆ PcapHandle()

pcpp::internal::PcapHandle::PcapHandle ( pcap_t *  pcapDescriptor)
explicitnoexcept

Creates a handle from the provided pcap descriptor.

Parameters
pcapDescriptorThe pcap descriptor to wrap.

Member Function Documentation

◆ clearFilter()

bool pcpp::internal::PcapHandle::clearFilter ( )

Clears the filter currently set on the handle.

Returns
True if the filter was removed successfully or if no filter was set, false otherwise.

◆ get()

pcap_t* pcpp::internal::PcapHandle::get ( ) const
inlinenoexcept
Returns
The underlying pcap descriptor.

◆ getLastError()

char const* pcpp::internal::PcapHandle::getLastError ( ) const
noexcept

Helper function to retrieve a view of the last error string for this handle.

Returns
A null-terminated view of the last error string.
Remarks
The returned view is only valid until the next call to a pcap function.

◆ isValid()

bool pcpp::internal::PcapHandle::isValid ( ) const
inlinenoexcept
Returns
True if the handle is not null, false otherwise.

◆ operator bool()

pcpp::internal::PcapHandle::operator bool ( ) const
inlineexplicitnoexcept
Returns
True if the handle is not null, false otherwise.

◆ release()

pcap_t* pcpp::internal::PcapHandle::release ( )
noexcept

Releases ownership of the handle and returns the pcap descriptor.

Returns
The pcap descriptor or nullptr if no handle is owned.

◆ reset()

void pcpp::internal::PcapHandle::reset ( pcap_t *  pcapDescriptor = nullptr)
noexcept

Replaces the managed handle with the provided one.

Parameters
pcapDescriptorA new pcap descriptor to manage.
Remarks
If the handle contains a non-null descriptor it will be closed.

◆ setFilter()

bool pcpp::internal::PcapHandle::setFilter ( std::string const &  filter)

Sets a filter on the handle. Only packets that match the filter will be captured by the handle.

The filter uses Berkeley Packet Filter (BPF) syntax (http://biot.com/capstats/bpf.html).

Parameters
[in]filterThe filter to set in Berkeley Packet Filter (BPF) syntax.
Returns
True if the filter was set successfully, false otherwise.