PcapPlusPlus  24.09
pcpp::IPFilter Class Reference

#include <PcapFilter.h>

Inheritance diagram for pcpp::IPFilter:
pcpp::IFilterWithDirection pcpp::GeneralFilter

Public Member Functions

 IPFilter (const std::string &ipAddress, Direction dir)
 
 IPFilter (const IPAddress &ipAddress, Direction dir)
 
 IPFilter (const std::string &ipAddress, Direction dir, const std::string &netmask)
 
 IPFilter (const IPAddress &ipAddress, Direction dir, const std::string &netmask)
 
 IPFilter (const std::string &ipAddress, Direction dir, int len)
 
 IPFilter (const IPAddress &ipAddress, Direction dir, int len)
 
 IPFilter (const IPNetwork &network, Direction dir)
 
void parseToString (std::string &result) override
 
void setNetwork (const IPNetwork &network)
 
void setAddr (const std::string &ipAddress)
 
void setAddr (const IPAddress &ipAddress)
 
void setMask (const std::string &netmask)
 
void clearMask ()
 
void setLen (const int len)
 
void clearLen ()
 
- Public Member Functions inherited from pcpp::IFilterWithDirection
void setDirection (Direction dir)
 
- Public Member Functions inherited from pcpp::GeneralFilter
bool matchPacketWithFilter (RawPacket *rawPacket)
 
virtual ~GeneralFilter ()
 

Detailed Description

A class for representing IPv4 or IPv6 address filter, equivalent to "net src x.x.x.x" or "net dst x.x.x.x"

For deeper understanding of the filter concept please refer to PcapFilter.h

Constructor & Destructor Documentation

◆ IPFilter() [1/7]

pcpp::IPFilter::IPFilter ( const std::string &  ipAddress,
Direction  dir 
)
inline

The basic constructor that creates the filter from an IP address string and direction (source or destination)

Parameters
[in]ipAddressThe IP address to build the filter with.
[in]dirThe address direction to filter (source or destination)
Exceptions
std::invalid_argumentThe provided address is not a valid IPv4 or IPv6 address.

◆ IPFilter() [2/7]

pcpp::IPFilter::IPFilter ( const IPAddress ipAddress,
Direction  dir 
)
inline

The basic constructor that creates the filter from an IP address and direction (source or destination)

Parameters
[in]ipAddressThe IP address to build the filter with.
[in]dirThe address direction to filter (source or destination)

◆ IPFilter() [3/7]

pcpp::IPFilter::IPFilter ( const std::string &  ipAddress,
Direction  dir,
const std::string &  netmask 
)
inline

A constructor that enable to filter only part of the address by using a mask (aka subnet). For example: "filter only IP addresses that matches the subnet 10.0.0.x"

Parameters
[in]ipAddressThe IP address to use. Only the part of the address that is not masked will be matched. For example: if the address is "1.2.3.4" and the mask is "255.255.255.0" than the part of the address that will be matched is "1.2.3.X".
[in]dirThe address direction to filter (source or destination)
[in]netmaskThe mask to use. The mask should be a valid IP address in either IPv4 dotted-decimal format (e.g., 255.255.255.0) or IPv6 colon-separated hexadecimal format (e.g., FFFF:FFFF:FFFF:FFFF::).
Exceptions
std::invalid_argumentThe provided address is not a valid IP address or the provided netmask string is invalid..

◆ IPFilter() [4/7]

pcpp::IPFilter::IPFilter ( const IPAddress ipAddress,
Direction  dir,
const std::string &  netmask 
)
inline

A constructor that enable to filter only part of the address by using a mask (aka subnet). For example: "filter only IP addresses that matches the subnet 10.0.0.x"

Parameters
[in]ipAddressThe IP address to use. Only the part of the address that is not masked will be matched. For example: if the address is "1.2.3.4" and the mask is "255.255.255.0" than the part of the address that will be matched is "1.2.3.X".
[in]dirThe address direction to filter (source or destination)
[in]netmaskThe mask to use. The mask should be a valid IP address in either IPv4 dotted-decimal format (e.g., 255.255.255.0) or IPv6 colon-separated hexadecimal format (e.g., FFFF:FFFF:FFFF:FFFF::).
Exceptions
std::invalid_argumentThe provided netmask string is invalid.

◆ IPFilter() [5/7]

pcpp::IPFilter::IPFilter ( const std::string &  ipAddress,
Direction  dir,
int  len 
)
inline

A constructor that enables to filter by a subnet. For example: "filter only IP addresses that matches the subnet 10.0.0.3/24" which means the part of the address that will be matched is "10.0.0.X"

Parameters
[in]ipAddressThe IP address to use. Only the part of the address that is not masked will be matched. For example: if the address is "1.2.3.4" and the subnet is "/24" than the part of the address that will be matched is "1.2.3.X".
[in]dirThe address direction to filter (source or destination)
[in]lenThe subnet to use (e.g "/24"). Acceptable subnet values are [0, 32] for IPv4 and [0, 128] for IPv6.
Exceptions
std::invalid_argumentThe provided address is not a valid IPv4 or IPv6 address or the provided length is out of acceptable range.

◆ IPFilter() [6/7]

pcpp::IPFilter::IPFilter ( const IPAddress ipAddress,
Direction  dir,
int  len 
)
inline

A constructor that enables to filter by a subnet. For example: "filter only IP addresses that matches the subnet 10.0.0.3/24" which means the part of the address that will be matched is "10.0.0.X"

Parameters
[in]ipAddressThe IP address to use. Only the part of the address that is not masked will be matched. For example: if the address is "1.2.3.4" and the subnet is "/24" than the part of the address that will be matched is "1.2.3.X".
[in]dirThe address direction to filter (source or destination)
[in]lenThe subnet to use (e.g "/24"). Acceptable subnet values are [0, 32] for IPv4 and [0, 128] for IPv6.
Exceptions
std::invalid_argumentThe provided length is out of acceptable range.

◆ IPFilter() [7/7]

pcpp::IPFilter::IPFilter ( const IPNetwork network,
Direction  dir 
)
inline

A constructor that enables to filter by a predefined network object.

Parameters
[in]networkThe network to use when filtering. IP address and subnet mask are taken from the network object.
[in]dirThe address direction to filter (source or destination)

Member Function Documentation

◆ clearLen()

void pcpp::IPFilter::clearLen ( )
inline

Clears the subnet mask length.

◆ clearMask()

void pcpp::IPFilter::clearMask ( )
inline

Clears the subnet mask.

◆ parseToString()

void pcpp::IPFilter::parseToString ( std::string &  result)
overridevirtual

A method that parses the class instance into BPF string format

Parameters
[out]resultAn empty string that the parsing will be written into. If the string isn't empty, its content will be overridden

Implements pcpp::GeneralFilter.

◆ setAddr() [1/2]

void pcpp::IPFilter::setAddr ( const IPAddress ipAddress)
inline

Set the IP address

Parameters
[in]ipAddressThe IP address to build the filter with.
Remarks
Alternating between IPv4 and IPv6 can have unintended consequences on the subnet mask. Setting an IPv4 address when the prefix length is over 32 make the new prefix length 32. Setting an IPv6 address will keep the current IPv4 prefix mask length.

◆ setAddr() [2/2]

void pcpp::IPFilter::setAddr ( const std::string &  ipAddress)
inline

Set the IP address

Parameters
[in]ipAddressThe IP address to build the filter with.
Exceptions
std::invalid_argumentThe provided string does not represent a valid IP address.

◆ setLen()

void pcpp::IPFilter::setLen ( const int  len)
inline

Set the subnet (IPv4) or prefix length (IPv6). Acceptable subnet values are [0, 32] for IPv4 and [0, 128] for IPv6.

Parameters
[in]lenThe subnet to use (e.g "/24")
Exceptions
std::invalid_argumentThe provided length is out of acceptable range.

◆ setMask()

void pcpp::IPFilter::setMask ( const std::string &  netmask)
inline

Set the subnet mask

Parameters
[in]netmaskThe mask to use. The mask should match the IP version and be in a valid format. Valid formats: IPv4 - (X.X.X.X) - 'X' - a number in the range of 0 and 255 (inclusive)): IPv6 - (YYYY:YYYY:YYYY:YYYY:YYYY:YYYY:YYYY:YYYY) - 'Y' - a hexadecimal digit [0 - 9, A - F]. Short form IPv6 formats are allowed.
Exceptions
std::invalid_argumentThe provided netmask is invalid or does not correspond to the current IP address version.

◆ setNetwork()

void pcpp::IPFilter::setNetwork ( const IPNetwork network)
inline

Set the network to build the filter with.

Parameters
[in]networkThe IP Network object to be used when building the filter.