libtins  4.0
Public Types | Public Member Functions | Static Public Attributes | List of all members
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. More...
 
 PacketSender (PacketSender &&rhs) TINS_NOEXCEPT
 Move constructor. More...
 
PacketSenderoperator= (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 NetworkInterfacedefault_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...
 
PDUsend_recv (PDU &pdu)
 Sends a PDU and waits for its response. More...
 
PDUsend_recv (PDU &pdu, const NetworkInterface &iface)
 Sends a PDU and waits for its response. More...
 
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. More...
 
void send_l2 (PDU &pdu, struct sockaddr *link_addr, uint32_t len_addr, const NetworkInterface &iface=NetworkInterface())
 Sends a level 2 PDU. More...
 
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. 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
 

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:

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);

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

Flags to indicate the socket type.

Constructor & Destructor Documentation

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.
Tins::PacketSender::PacketSender ( PacketSender &&  rhs)
inline

Move constructor.

Parameters
rhsThe sender to be moved.
Tins::PacketSender::~PacketSender ( )

PacketSender destructor.

This gracefully closes all open sockets.

Member Function Documentation

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.
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.

See also
PacketSender::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.

Parameters
typeThe type of socket which will be used to pick the protocol flag for this socket.
PacketSender& Tins::PacketSender::operator= ( PacketSender &&  rhs)
inline

Move assignment operator.

Parameters
rhsThe 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.

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.
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.
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.
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.
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.
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.
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.
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

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: