libtins 4.5
Loading...
Searching...
No Matches
Tins::PacketSender Class Reference

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.
 
 PacketSender (PacketSender &&rhs) TINS_NOEXCEPT
 Move constructor.
 
PacketSenderoperator= (PacketSender &&rhs) TINS_NOEXCEPT
 Move assignment operator.
 
 ~PacketSender ()
 PacketSender destructor.
 
void open_l2_socket (const NetworkInterface &iface=NetworkInterface())
 Opens a layer 2 socket.
 
void open_l3_socket (SocketType type)
 Opens a layer 3 socket, using the corresponding protocol for the given flag.
 
void close_socket (SocketType type, const NetworkInterface &iface=NetworkInterface())
 Closes the socket associated with the given flag.
 
void default_interface (const NetworkInterface &iface)
 Sets the default interface.
 
const NetworkInterfacedefault_interface () const
 Gets the default interface.
 
void send (PDU &pdu)
 Sends a PDU.
 
void send (PDU &pdu, const NetworkInterface &iface)
 Sends a PDU.
 
PDUsend_recv (PDU &pdu)
 Sends a PDU and waits for its response.
 
PDUsend_recv (PDU &pdu, const NetworkInterface &iface)
 Sends a PDU and waits for its response.
 
PDUrecv_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.
 
void send_l2 (PDU &pdu, struct sockaddr *link_addr, uint32_t len_addr, const NetworkInterface &iface=NetworkInterface())
 Sends a level 2 PDU.
 
PDUrecv_l3 (PDU &pdu, struct sockaddr *link_addr, uint32_t len_addr, SocketType type)
 Receives a layer 3 PDU response to a previously sent PDU.
 
void send_l3 (PDU &pdu, struct sockaddr *link_addr, uint32_t len_addr, SocketType type)
 Sends a level 3 PDU.
 

Static Public Attributes

static const uint32_t DEFAULT_TIMEOUT = 2
 

Detailed Description

Sends packets through a network interface.

This class allows sending packets through a network interface. It can send basically two types of packets:

  • Those that contain a link layer PDU (EthernetII, SLL, etc). These will be serialized and sent through an interface that has to be specified. This can be done by providing it when you call PacketSender::send, or set a default one using PacketSender::default_interface.
  • Those that don't contain a link layer PDU. In this case, the kernel will be responsible for picking the appropriate network interface based on the destination address.
    • Exception: RFC2553 requires IPv6 link-scope address have a interface defined.
    Note for Windows users:
    Sending layer 3 PDUs (without a link layer protocol) is very restricted on Windows (link). Therefore it's recommended you always send packets which contain link layer PDUs. This will use Winpcap's pcap_sendpacket to inject the packets.
    Sending packets can be done via PacketSender::send:
// Construct a packet which uses an EthernetII link layer.
EthernetII pkt1 = ...;
// Construct a packet sender, which we'll use to send packets.
PacketSender sender;
// Send it through interface eth0
sender.send(pkt1, "eth0");
// Set the default interface to eth0
sender.default_interface("eth0");
// This is now equivalent to the previous send.
sender.send(pkt1);
// Construct a packet which has no link layer protocol.
IP ip = IP("192.168.0.1") / TCP(22, 928);
// Here the kernel will figure out which interface to use and it will
// append the appropriate link layer protocol PDU. It will also perform
// the necessary ARP lookups in order to use the destination host's
// hardware address.
//
// libtins will find which is the appropriate source IP address to use.
// This will be done by the kernel as well, but it's required when
// calculating checksums.
sender.send(ip);
Represents an Ethernet II PDU.
Definition ethernetII.h:46
Class that represents an IP PDU.
Definition ip.h:63
Sends packets through a network interface.
Definition packet_sender.h:118
void default_interface(const NetworkInterface &iface)
Sets the default interface.
Definition packet_sender.cpp:143
void send(PDU &pdu)
Sends a PDU.
Definition packet_sender.cpp:320
Represents a TCP PDU.
Definition tcp.h:76

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.

See also
PacketSender::send
PacketSender::send_recv

Member Enumeration Documentation

◆ SocketType

Flags to indicate the socket type.

Constructor & Destructor Documentation

◆ PacketSender() [1/2]

Tins::PacketSender::PacketSender ( const NetworkInterface & iface = NetworkInterface(),
uint32_t recv_timeout = DEFAULT_TIMEOUT,
uint32_t usec = 0 )

Constructor for PacketSender objects.

Parameters
ifaceThe default interface in which to send the packets.
recv_timeoutThe timeout which will be used when receiving responses.

◆ PacketSender() [2/2]

Tins::PacketSender::PacketSender ( PacketSender && rhs)
inline

Move constructor.

Parameters
rhsThe sender to be moved.

◆ ~PacketSender()

Tins::PacketSender::~PacketSender ( )

PacketSender destructor.

This gracefully closes all open sockets.

Member Function Documentation

◆ close_socket()

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.

Parameters
typeThe type of the socket to be closed.

◆ default_interface() [1/2]

const NetworkInterface & Tins::PacketSender::default_interface ( ) const

Gets the default interface.

See also
PacketSender::default_interface

◆ default_interface() [2/2]

void Tins::PacketSender::default_interface ( const NetworkInterface & iface)

Sets the default interface.

The interface will be used whenever PacketSender::send(PDU&) is called.

◆ open_l2_socket()

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.

◆ open_l3_socket()

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.

Parameters
typeThe type of socket which will be used to pick the protocol flag for this socket.

◆ operator=()

PacketSender & Tins::PacketSender::operator= ( PacketSender && rhs)
inline

Move assignment operator.

Parameters
rhsThe sender to be moved.

◆ recv_l2()

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.

Parameters
pduThe PDU which will try to match the responses.
link_addrThe sockaddr struct which will be used to receive the PDU.
len_addrThe sockaddr struct length.
Returns
Returns the response PDU. If no response is received, then 0 is returned.

◆ recv_l3()

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.

Parameters
pduThe PDU which will try to match the responses.
link_addrThe sockaddr struct which will be used to receive the PDU.
len_addrThe sockaddr struct length.
typeThe socket protocol type.
Returns
Returns the response PDU. If no response is received, then 0 is returned.

◆ send() [1/2]

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.

See also
PacketSender::default_interface
Parameters
pduThe PDU to be sent.

◆ send() [2/2]

void Tins::PacketSender::send ( PDU & pdu,
const NetworkInterface & iface )

Sends a PDU.

See also
PacketSender::send

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.

Parameters
pduThe PDU to be sent.
ifaceThe network interface to use.

◆ send_l2()

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.

Parameters
pduThe PDU to send.
link_addrThe sockaddr struct which will be used to send the PDU.
len_addrThe sockaddr struct length.

◆ send_l3()

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.

Parameters
pduThe PDU to send.
link_addrThe sockaddr struct which will be used to send the PDU.
len_addrThe sockaddr struct length.
typeThe socket protocol type.

◆ send_recv() [1/2]

PDU * Tins::PacketSender::send_recv ( PDU & pdu)

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.

Parameters
pduThe PDU to send.
Returns
Returns the response PDU, 0 if not response was received.

◆ send_recv() [2/2]

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.

See also
PacketSender::send_recv(PDU&);
Parameters
pduThe PDU to send.
ifaceThe network interface in which to send and receive.
Returns
Returns the response PDU, 0 if not response was received.

Member Data Documentation

◆ DEFAULT_TIMEOUT

const uint32_t Tins::PacketSender::DEFAULT_TIMEOUT = 2
static

The default timeout for receive actions.


The documentation for this class was generated from the following files: