PcapPlusPlus  22.11
pcpp::NetworkUtils Class Reference

#include <NetworkUtils.h>

Public Member Functions

MacAddress getMacAddress (IPv4Address ipAddr, PcapLiveDevice *device, double &arpResponseTimeMS, MacAddress sourceMac=MacAddress::Zero, IPv4Address sourceIP=IPv4Address::Zero, int arpTimeout=-1) const
 
IPv4Address getIPv4Address (std::string hostname, PcapLiveDevice *device, double &dnsResponseTimeMS, uint32_t &dnsTTL, int dnsTimeout=-1, IPv4Address dnsServerIP=IPv4Address::Zero, IPv4Address gatewayIP=IPv4Address::Zero) const
 

Static Public Member Functions

static NetworkUtilsgetInstance ()
 

Static Public Attributes

static const int DefaultTimeout
 

Detailed Description

This class bundles several network utilities that are very common and useful. These utilities use Pcap++ and Packet++ packet crafting and processing capabilities. This class is a singleton and can be access by getInstance() only

Member Function Documentation

◆ getInstance()

static NetworkUtils& pcpp::NetworkUtils::getInstance ( )
inlinestatic

The access method to the singleton

Returns
The singleton instance of this class

◆ getIPv4Address()

IPv4Address pcpp::NetworkUtils::getIPv4Address ( std::string  hostname,
PcapLiveDevice device,
double &  dnsResponseTimeMS,
uint32_t &  dnsTTL,
int  dnsTimeout = -1,
IPv4Address  dnsServerIP = IPv4Address::Zero,
IPv4Address  gatewayIP = IPv4Address::Zero 
) const

Resolve an IPv4 address for a given hostname. Resolving is done in multiple phases: first resolving the LAN gateway MAC address (or default gateway if a gateway isn't provided) using ARP protocol (by using NetworkUtils::getMacAddress() ). Then a DNS request is sent to a DNS server (if specified) or to the LAN gateway (if DNS server is not specified). The DNS response is decoded and the IPv4 address is determined. In addition the method outputs the time it took the DNS response to arrive and the DNS TTL written on the DNS response. If DNS response doesn't contain an IPv4 address resolving an IPv4Address::Zero will be returned.

Parameters
[in]hostnameThe hostname to resolve
[in]deviceThe interface to send and receive packets on
[out]dnsResponseTimeMSWhen method returns successfully will contain the time it took to receive the DNS response (in milli-seconds)
[out]dnsTTLWhen method returns successfully will contain The DNS TTL written in the DNS response
[in]dnsTimeoutAn optional parameter to specify the timeout to wait for a DNS response. If not specified the default timeout is 5 sec
[in]dnsServerIPAn optional parameter to specify the DNS server IP to send the DNS request to. If not specified or specified with IPv4Address::Zero the DNS request will be sent to the default DNS server configured in the system
[in]gatewayIPAn optional parameter to specify the LAN gateway to send the DNS request through. If not specified or specified with IPv4Address::Zero the interface's default gateway will be used
Returns
The resolved IPv4 address or IPv4Address::Zero if something went wrong (in this case an error will be printed to log)

◆ getMacAddress()

MacAddress pcpp::NetworkUtils::getMacAddress ( IPv4Address  ipAddr,
PcapLiveDevice device,
double &  arpResponseTimeMS,
MacAddress  sourceMac = MacAddress::Zero,
IPv4Address  sourceIP = IPv4Address::Zero,
int  arpTimeout = -1 
) const

Resolve the MAC address for a given IPv4 address. It's done using the ARP protocol: send an ARP request and interpret the response

Parameters
[in]ipAddrThe IPv4 address to resolve MAC address to
[in]deviceThe interface to send and receive the ARP packets on
[out]arpResponseTimeMSAn output parameter that will contain the time in milliseconds that took the ARP response to arrive
[in]sourceMacAn optional parameter to set the source MAC address that will be sent with the ARP request if this parameter isn't set or set with MacAddress::Zero the MAC address of the interface will be used
[in]sourceIPAn optional parameter to set the source IPv4 address that will be sent with the ARP request if this parameter isn't set or set with IPv4Address::Zero the default IPv4 address of the interface will be used
[in]arpTimeoutAn optional parameter to set the timeout to wait for the ARP response to return. If this parameter isn't set or set with a number smaller than 0, a default timeout of 5 seconds will be set
Returns
The resolved MAC address or MacAddress::Zero if an error occurred or address could not be resolved. Errors will be printed to log

Member Data Documentation

◆ DefaultTimeout

const int pcpp::NetworkUtils::DefaultTimeout
static

Default timeout used for several utilities. Currently set to 5 seconds