#include <PcapLive.h>
Public Types | |
enum | Direction { IN = PCAP_D_IN, OUT = PCAP_D_OUT, IN_OUT = PCAP_D_INOUT } |
Type of direction the packets will be captured. More... | |
Public Member Functions | |
std::string const & | get_device () const |
Gets the name of the device. | |
Direction | get_direction () const |
Gets the direction of the capture. | |
int | get_fd () const |
Gets the file descriptor number from which captured packets are read. | |
int | get_selectable_fd () const |
Gets the selectable file descriptor number. | |
Stats const & | get_stats () |
Gets packet statistics. | |
int | get_timeout () const |
Gets the timeout, in miliseconds. | |
void | inject (Packet const &packet) |
Sends a raw packet through the network interface. | |
bool | is_nonblock () |
Gets the current non-blocking state. | |
bool | is_promiscuous () const |
Gets the promiscuous state of the interface. | |
PcapLive (std::string const &device, unsigned int snaplen=MAX_SNAPLEN, bool promisc=true, unsigned int timeout_ms=0) | |
Creates a PcapLive object. | |
void | set_direction (Direction direction) |
Specifies a direction that packets will be captured. | |
void | set_nonblock (bool set=true) |
Puts the PcapLive into non-blocking mode, or takes it out of non-blocking mode. | |
virtual | ~PcapLive () |
Destroys the PcapLive object. | |
Static Public Attributes | |
static unsigned int const | MAX_SNAPLEN = 65535 |
Default snap length for PcapLive(). | |
Friends | |
PcapLive & | operator<< (PcapLive &pcap, Packet const &packet) |
An alias for inject(). |
pcappp::PcapLive::PcapLive | ( | std::string const & | device, | |
unsigned int | snaplen = MAX_SNAPLEN , |
|||
bool | promisc = true , |
|||
unsigned int | timeout_ms = 0 | |||
) |
Creates a PcapLive object.
device | A string identifying the network device to open (e.g. "eth0"). On Linux systems with 2.2 or later kernels, a device string of "any" or "" (empty) can be used to capture packets from all interfaces | |
snaplen | The maximum number of bytes to capture. If this value is less than the size of a packet that is captured, only the first snaplen bytes of that packet will be provided as packet data. The default value of PcapLive::MAX_SNAPLEN should be sufficient, on most if not all networks, to capture all the data available from the packet | |
promisc | Whether the interface is to be put into promiscuous mode. (Note that even if this parameter is false, the interface could well be in promiscuous mode for some other reason.) For now, this doesn’t work on the "any" device; if an argument of "any" or empty is supplied, the promisc parameter is ignored | |
timeout_ms | The read timeout, in miliseconds. It is used to arrange that the read not necessarily returns immediately when a packet is seen, but that it waits for some amount of time to allow more packets to arrive and to read multiple packets from the OS kernel in one operation. Not all platforms support a read timeout; on platforms that don’t, the read timeout is ignored. A timeout of zero, on platforms that support a read timeout, will cause a read to wait forever to allow enough packets to arrive, with no timeout |
PcapError |
pcappp::PcapLive::~PcapLive | ( | ) | [virtual] |
Destroys the PcapLive object.
std::string const& pcappp::PcapLive::get_device | ( | ) | const [inline] |
Gets the name of the device.
Direction pcappp::PcapLive::get_direction | ( | ) | const [inline] |
Gets the direction of the capture.
int pcappp::PcapLive::get_fd | ( | ) | const |
Gets the file descriptor number from which captured packets are read.
int pcappp::PcapLive::get_selectable_fd | ( | ) | const |
Gets the selectable file descriptor number.
Stats const & pcappp::PcapLive::get_stats | ( | ) |
int pcappp::PcapLive::get_timeout | ( | ) | const [inline] |
Gets the timeout, in miliseconds.
void pcappp::PcapLive::inject | ( | Packet const & | packet | ) |
Sends a raw packet through the network interface.
Note that, even if you successfully open the network interface, you might not have permission to send packets on it, or it might not support sending packets; as the PcapLive constructor doesn’t have a flag to indicate whether to open for capturing, sending, or capturing and sending, you cannot request a PcapLive that supports sending and be notified at construction time whether sending will be possible.
Note also that some devices might not support sending packets.
On some platforms, the link-layer header of the packet that’s sent might not be the same as the link-layer header of the packet supplied to inject(), as the source link-layer address, if the header contains such an address, might be changed to be the address assigned to the interface on which the packet it sent, if the platform doesn’t support sending completely raw and unchanged packets. Even worse, some drivers on some platforms might change the link-layer type field to whatever value libpcap used when attaching to the device, even on platforms that do nominally support sending completely raw and unchanged packets.
packet | The Packet to be sent |
PcapError |
bool pcappp::PcapLive::is_nonblock | ( | ) |
Gets the current non-blocking state.
PcapError |
bool pcappp::PcapLive::is_promiscuous | ( | ) | const [inline] |
Gets the promiscuous state of the interface.
void pcappp::PcapLive::set_direction | ( | Direction | direction | ) |
Specifies a direction that packets will be captured.
In some platforms changing the direction of the capture might not be supported. In some other platforms the direction cannot be set to PcapLive::OUT. In both cases a PcapError exception is thrown.
The default setting is PcapLive::IN_OUT
PcapError |
void pcappp::PcapLive::set_nonblock | ( | bool | set = true |
) |
Puts the PcapLive into non-blocking mode, or takes it out of non-blocking mode.
In non-blocking mode, an attempt to read from the network interface with dispatch() will, if no packets are currently available to be read, return immediately rather than blocking waiting for packets to arrive. Note that loop() and next() will not work in non-blocking mode.
set | Whether the PcapLive is to be put into non-blocking mode |
PcapError |
unsigned int const pcappp::PcapLive::MAX_SNAPLEN = 65535 [static] |
Default snap length for PcapLive().