libtins
4.0
|
Sends packets through a network interface. More...
#include <packet_sender.h>
Public Types | |
enum | SocketType { ETHER_SOCKET, IP_TCP_SOCKET, IP_UDP_SOCKET, IP_RAW_SOCKET, ARP_SOCKET, ICMP_SOCKET, IPV6_SOCKET, ICMPV6_SOCKET, SOCKETS_END } |
Public Member Functions | |
PacketSender (const NetworkInterface &iface=NetworkInterface(), uint32_t recv_timeout=DEFAULT_TIMEOUT, uint32_t usec=0) | |
Constructor for PacketSender objects. More... | |
PacketSender (PacketSender &&rhs) TINS_NOEXCEPT | |
Move constructor. More... | |
PacketSender & | operator= (PacketSender &&rhs) TINS_NOEXCEPT |
Move assignment operator. More... | |
~PacketSender () | |
PacketSender destructor. More... | |
void | open_l2_socket (const NetworkInterface &iface=NetworkInterface()) |
Opens a layer 2 socket. More... | |
void | open_l3_socket (SocketType type) |
Opens a layer 3 socket, using the corresponding protocol for the given flag. More... | |
void | close_socket (SocketType type, const NetworkInterface &iface=NetworkInterface()) |
Closes the socket associated with the given flag. More... | |
void | default_interface (const NetworkInterface &iface) |
Sets the default interface. More... | |
const NetworkInterface & | default_interface () const |
Gets the default interface. More... | |
void | send (PDU &pdu) |
Sends a PDU. More... | |
void | send (PDU &pdu, const NetworkInterface &iface) |
Sends a PDU. More... | |
PDU * | send_recv (PDU &pdu) |
Sends a PDU and waits for its response. More... | |
PDU * | send_recv (PDU &pdu, const NetworkInterface &iface) |
Sends a PDU and waits for its response. More... | |
PDU * | recv_l2 (PDU &pdu, struct sockaddr *link_addr, uint32_t len_addr, const NetworkInterface &iface=NetworkInterface()) |
Receives a layer 2 PDU response to a previously sent PDU. More... | |
void | send_l2 (PDU &pdu, struct sockaddr *link_addr, uint32_t len_addr, const NetworkInterface &iface=NetworkInterface()) |
Sends a level 2 PDU. More... | |
PDU * | recv_l3 (PDU &pdu, struct sockaddr *link_addr, uint32_t len_addr, SocketType type) |
Receives a layer 3 PDU response to a previously sent PDU. More... | |
void | send_l3 (PDU &pdu, struct sockaddr *link_addr, uint32_t len_addr, SocketType type) |
Sends a level 3 PDU. More... | |
Static Public Attributes | |
static const uint32_t | DEFAULT_TIMEOUT = 2 |
Sends packets through a network interface.
This class allows sending packets through a network interface. It can send basically two types of packets:
Sending packets can be done via PacketSender::send:
PacketSender also supports sending a packet and waiting for a response. This can be done by using PacketSender::send_recv.
This class opens sockets as it needs to, and closes them when the object is destructed.
Flags to indicate the socket type.
Tins::PacketSender::PacketSender | ( | const NetworkInterface & | iface = NetworkInterface() , |
uint32_t | recv_timeout = DEFAULT_TIMEOUT , |
||
uint32_t | usec = 0 |
||
) |
Constructor for PacketSender objects.
iface | The default interface in which to send the packets. |
recv_timeout | The timeout which will be used when receiving responses. |
|
inline |
Move constructor.
rhs | The sender to be moved. |
Tins::PacketSender::~PacketSender | ( | ) |
PacketSender destructor.
This gracefully closes all open sockets.
void Tins::PacketSender::close_socket | ( | SocketType | type, |
const NetworkInterface & | iface = NetworkInterface() |
||
) |
Closes the socket associated with the given flag.
If the provided type is invalid, meaning no such open socket exists, an invalid_socket_type exception is thrown.
If any socket close errors are encountered, a socket_close_error is thrown.
type | The type of the socket to be closed. |
void Tins::PacketSender::default_interface | ( | const NetworkInterface & | iface | ) |
Sets the default interface.
The interface will be used whenever PacketSender::send(PDU&) is called.
const NetworkInterface & Tins::PacketSender::default_interface | ( | ) | const |
Gets the default interface.
void Tins::PacketSender::open_l2_socket | ( | const NetworkInterface & | iface = NetworkInterface() | ) |
Opens a layer 2 socket.
If this operation fails, then a socket_open_error will be thrown.
void Tins::PacketSender::open_l3_socket | ( | SocketType | type | ) |
Opens a layer 3 socket, using the corresponding protocol for the given flag.
If this operation fails, then a socket_open_error will be thrown. If the provided socket type is not valid, an invalid_socket_type exception will be throw.
type | The type of socket which will be used to pick the protocol flag for this socket. |
|
inline |
Move assignment operator.
rhs | The sender to be moved. |
PDU * Tins::PacketSender::recv_l2 | ( | PDU & | pdu, |
struct sockaddr * | link_addr, | ||
uint32_t | len_addr, | ||
const NetworkInterface & | iface = NetworkInterface() |
||
) |
Receives a layer 2 PDU response to a previously sent PDU.
This method is used internally. You should just use PacketSender::send_recv.
This PacketSender will receive data from a raw socket, open using the corresponding flag, according to the given type of protocol, until a match for the given PDU is received.
pdu | The PDU which will try to match the responses. |
link_addr | The sockaddr struct which will be used to receive the PDU. |
len_addr | The sockaddr struct length. |
PDU * Tins::PacketSender::recv_l3 | ( | PDU & | pdu, |
struct sockaddr * | link_addr, | ||
uint32_t | len_addr, | ||
SocketType | type | ||
) |
Receives a layer 3 PDU response to a previously sent PDU.
This method is used internally. You should just use PacketSender::send_recv.
This PacketSender will receive data from a raw socket, open using the corresponding flag, according to the given type of protocol, until a match for the given PDU is received.
pdu | The PDU which will try to match the responses. |
link_addr | The sockaddr struct which will be used to receive the PDU. |
len_addr | The sockaddr struct length. |
type | The socket protocol type. |
void Tins::PacketSender::send | ( | PDU & | pdu | ) |
Sends a PDU.
This method opens the appropriate socket, if it's not open yet, and sends the PDU on the open socket.
If any send error occurs, then a socket_write_error is thrown.
If the PDU contains a link layer protocol, then default_interface is used.
pdu | The PDU to be sent. |
void Tins::PacketSender::send | ( | PDU & | pdu, |
const NetworkInterface & | iface | ||
) |
Sends a PDU.
This overload takes a NetworkInterface. The packet is sent through that interface if a link-layer PDU is present, otherwise this call is equivalent to send(PDU&).
The interface stored in the link layer PDU(if any), is restored after this method ends.
pdu | The PDU to be sent. |
iface | The network interface to use. |
void Tins::PacketSender::send_l2 | ( | PDU & | pdu, |
struct sockaddr * | link_addr, | ||
uint32_t | len_addr, | ||
const NetworkInterface & | iface = NetworkInterface() |
||
) |
Sends a level 2 PDU.
This method is used internally. You should just use PacketSender::send.
This method sends a layer 2 PDU, using a raw socket, open using the corresponding flag, according to the given type of protocol.
If any socket write error occurs, a socket_write_error is thrown.
void Tins::PacketSender::send_l3 | ( | PDU & | pdu, |
struct sockaddr * | link_addr, | ||
uint32_t | len_addr, | ||
SocketType | type | ||
) |
Sends a level 3 PDU.
This method is used internally. You should just use PacketSender::send.
This method sends a layer 3 PDU, using a raw socket, open using the corresponding flag, according to the given type of protocol.
If any socket write error occurs, a socket_write_error is thrown.
Sends a PDU and waits for its response.
This method is used to send PDUs and receive their response. The packet is sent, and then a response is awaited. PDU::matches_pdu is called on the packet sent in order to check whether a packet received is a response.
This will match every response to a packet. For example, if you send a TCP packet, any response matching the same IP addresses and ports will be taken as a response to it. This also happens for other protocols, such as ARP, ICMP, DHCP, DNS, IP, etc.
If you send a packet and get an ICMP response indicating an error (such as host unreachable, ttl exceeded, etc), that packet will be considered a response.
pdu | The PDU to send. |
PDU * Tins::PacketSender::send_recv | ( | PDU & | pdu, |
const NetworkInterface & | iface | ||
) |
Sends a PDU and waits for its response.
Sends a packet and receives a response. This overload takes a NetworkInterface.
pdu | The PDU to send. |
iface | The network interface in which to send and receive. |
|
static |
The default timeout for receive actions.