PcapPlusPlus
22.11
|
#include <RawSocketDevice.h>
Public Types | |
enum | RecvPacketResult { RecvSuccess = 0, RecvTimeout = 1, RecvWouldBlock = 2, RecvError = 3 } |
Public Member Functions | |
~RawSocketDevice () | |
RecvPacketResult | receivePacket (RawPacket &rawPacket, bool blocking=true, int timeout=-1) |
int | receivePackets (RawPacketVector &packetVec, int timeout, int &failedRecv) |
bool | sendPacket (const RawPacket *rawPacket) |
int | sendPackets (const RawPacketVector &packetVec) |
virtual bool | open () |
virtual void | close () |
Public Member Functions inherited from pcpp::IDevice | |
bool | isOpened () |
A class that wraps the raw socket functionality. A raw socket is a network socket that allows direct sending and receiving of IP packets without any protocol-specific transport layer formatting (taken from Wikipedia: https://en.wikipedia.org/wiki/Network_socket#Raw_socket). This wrapper class enables creation of a raw socket, binding it to a network interface, and then receiving and sending packets on it. Current implementation supports only Windows and Linux because other platforms provide poor support for raw sockets making them practically unusable. There are also major differences between Linux and Windows in raw socket implementation, let's mention some of the:
More details about opening the raw socket, receiving and sending packets are explained in the corresponding class methods. Raw sockets are supported for both IPv4 and IPv6, so you can create and bind raw sockets to each of the two. Also, there is no limit on the number of sockets opened for a specific IP address or network interface, so you can create multiple instances of this class and bind all of them to the same interface and IP address.
An enum for reporting packet receive results
pcpp::RawSocketDevice::~RawSocketDevice | ( | ) |
A d'tor for this class. It closes the raw socket if not previously closed by calling close()
|
virtual |
Close the raw socket
Implements pcpp::IDevice.
|
virtual |
Open the device by creating a raw socket and binding it to the network interface specified in the c'tor
Implements pcpp::IDevice.
RecvPacketResult pcpp::RawSocketDevice::receivePacket | ( | RawPacket & | rawPacket, |
bool | blocking = true , |
||
int | timeout = -1 |
||
) |
Receive a packet on the raw socket. This method has several modes of operation:
There is a slight difference on this method's behavior between Windows and Linux around how packets are received. On Linux the received packet contains all layers starting from the L2 (Ethernet). However on Windows raw socket are integrated in L3 level so the received packet contains only L3 (IP) layer and up.
[out] | rawPacket | An empty packet instance where the received packet data will be written to |
[in] | blocking | Indicates whether to run in blocking or non-blocking mode. Default value is blocking |
[in] | timeout | When in blocking mode, specifies the timeout [in seconds] to wait for a packet. If timeout expired and no packets were captured the method will return RawSocketDevice::RecvTimeout. Zero or negative values mean no timeout. The default value is no timeout |
int pcpp::RawSocketDevice::receivePackets | ( | RawPacketVector & | packetVec, |
int | timeout, | ||
int & | failedRecv | ||
) |
Receive packets into a packet vector for a certain amount of time. This method starts a timer and invokes the receivePacket() method in blocking mode repeatedly until the timeout expires. All packets received successfully are put into a packet vector
[out] | packetVec | The packet vector to add the received packet to |
[in] | timeout | Timeout in seconds to receive packets on the raw socket |
[out] | failedRecv | Number of receive attempts that failed |
bool pcpp::RawSocketDevice::sendPacket | ( | const RawPacket * | rawPacket | ) |
Send an Ethernet packet to the network. L2 protocols other than Ethernet are not supported in raw sockets. The entire packet is sent as is, including the original Ethernet and IP data. This method is only supported in Linux as Windows doesn't allow sending packets from raw sockets. Using it from other platforms will also return "false" with a corresponding error log message
[in] | rawPacket | The packet to send |
int pcpp::RawSocketDevice::sendPackets | ( | const RawPacketVector & | packetVec | ) |
Send a set of Ethernet packets to the network. L2 protocols other than Ethernet are not supported by raw sockets. The entire packet is sent as is, including the original Ethernet and IP data. This method is only supported in Linux as Windows doesn't allow sending packets from raw sockets. Using it from other platforms will return "false" with an appropriate error log message
[in] | packetVec | The set of packets to send |