1
0
mirror of https://github.com/mfontanini/libtins synced 2026-01-29 21:14:28 +01:00

Added wrappers to build ARP requests and replies. Defined BROADCAST in EthernetII

This commit is contained in:
Santiago Alessandri
2011-08-16 13:21:13 -03:00
parent 7bf8bd6ee7
commit cb647d1642
4 changed files with 199 additions and 34 deletions

View File

@@ -177,12 +177,8 @@ namespace Tins {
*/
void opcode(Flags new_opcode);
PDUType pdu_type() const { return PDU::ARP; }
void set_arp_request(const std::string &ip_dst, const std::string &ip_src, const std::string &hw_src = "");
/** \brief Check wether ptr points to a valid response for this PDU.
*
*
* \sa PDU::matches_response
* \param ptr The pointer to the buffer.
* \param total_sz The size of the buffer.
@@ -193,13 +189,99 @@ namespace Tins {
/** \brief Clones this pdu, filling the corresponding header with data
* extracted from a buffer.
*
*
* \param ptr The pointer to the from from which the data will be extracted.
* \param total_sz The size of the buffer.
* \return The cloned PDU.
* \sa PDU::clone_packet
*/
PDU *clone_packet(uint8_t *ptr, uint32_t total_sz);
PDUType pdu_type() const { return PDU::ARP; }
/**
* \brief Creates an ARP Request within a Layer 2 PDU using strings for target and sender.
*
* Creates an ARP Request PDU and embeds it within a Layer 2 PDU ready to be
* sent. The target and sender's protocol address are given using strings.
*
* \param iface string with the interface from where to send the ARP.
* \param target string with the target's IP or hostname.
* \param sender string with the sender's IP or hostname.
* \param hw_snd uint8_t array of 6 bytes containing the sender's hardware address.
* \return Returns a PDU* to the new Layer 2 PDU containing the ARP Request.
*/
static PDU* make_arp_request(const std::string& iface, const std::string &target, const std::string &sender, const uint8_t* hw_snd = 0);
/**
* \brief Creates an ARP Request within a Layer 2 PDU using uint32_t for target and sender.
*
* Creates an ARP Request PDU and embeds it within a Layer 2 PDU ready to be
* sent. The target and sender's protocol address are given using uint32_t.
*
* \param iface string with the interface from where to send the ARP.
* \param target uint32_t with the target's IP.
* \param sender uint32_t with the sender's IP.
* \param hw_snd uint8_t array of 6 bytes containing the sender's hardware address.
* \return Returns a PDU* to the new Layer 2 PDU containing the ARP Request.
*/
static PDU* make_arp_request(const std::string& iface, uint32_t target, uint32_t sender, const uint8_t* hw_snd = 0);
/**
* \brief Creates an ARP Reply within a Layer 2 PDU using strings for target and sender.
*
* Creates an ARP Reply PDU and embeds it within a Layer 2 PDU ready to be
* sent. The target and sender's protocol address are given using strings.
*
* \param iface string with the interface from where to send the ARP.
* \param target string with the target's IP or hostname.
* \param sender string with the sender's IP or hostname.
* \param hw_tgt uint8_t array of 6 bytes containing the target's hardware address.
* \param hw_snd uint8_t array of 6 bytes containing the sender's hardware address.
* \return Returns a PDU* to the new Layer 2 PDU containing the ARP Replay.
*/
static PDU* make_arp_reply(const std::string& iface, const std::string &target, const std::string &sender, const uint8_t* hw_tgt, const uint8_t* hw_snd);
/**
* \brief Creates an ARP Reply within a Layer 2 PDU using uint32_t for target and sender.
*
* Creates an ARP Reply PDU and embeds it within a Layer 2 PDU ready to be
* sent. The target and sender's protocol address are given using uint32_t.
*
* \param iface string with the interface from where to send the ARP.
* \param target uint32_t with the target's IP.
* \param sender uint32_t with the sender's IP.
* \param hw_tgt uint8_t array of 6 bytes containing the target's hardware address.
* \param hw_snd uint8_t array of 6 bytes containing the sender's hardware address.
* \return Returns a PDU* to the new Layer 2 PDU containing the ARP Replay.
*/
static PDU* make_arp_reply(const std::string& iface, uint32_t target, uint32_t sender, const uint8_t* hw_tgt, const uint8_t* hw_snd);
/**
* \brief Converts the current ARP PDU to an ARP Request.
*
* Calling this method sets the values of the target ARP PDU to become
* an ARP Request PDU.
*
* \param ip_tgt string with the target's IP or hostname.
* \param ip_snd string with the sender's IP or hostname.
* \param hw_snd uint8_t array of 6 bytes containing the sender's hardware address(optional).
*/
void set_arp_request(const std::string& ip_tgt, const std::string& ip_snd, const uint8_t* hw_snd = 0);
/**
* \brief Converts the current ARP PDU to an ARP Reply.
*
* Calling this method sets the values of the target ARP PDU to become
* an ARP Reply PDU.
*
* \param ip_tgt string with the target's IP or hostname.
* \param ip_snd string with the sender's IP or hostname.
* \param hw_tgt uint8_t array of 6 bytes containing the target's hardware address.
* \param hw_snd uint8_t array of 6 bytes containing the sender's hardware address.
*/
void set_arp_reply(const std::string& ip_tgt, const std::string& ip_snd, const uint8_t* hw_tgt, const uint8_t* hw_snd);
private:
struct arphdr {
uint16_t ar_hrd; /* format of hardware address */
@@ -215,11 +297,11 @@ namespace Tins {
} __attribute__((__packed__));
/** \brief Creates an instance of ARP using an arphdr pointer.
*
*
* \param arp_ptr The pointer to the arphdr.
*/
ARP(arphdr *arp_ptr);
void write_serialization(uint8_t *buffer, uint32_t total_sz, const PDU *parent);
arphdr _arp;

View File

@@ -35,31 +35,37 @@ namespace Tins {
class EthernetII : public PDU {
public:
/**
* \brief Constructor for creating an ethernet PDU
*
* Constructor that builds an ethernet PDU taking the destination's
* and source's MAC.
*
* \param iface string containing the interface's name from where to send the packet.
* \param mac_dst uint8_t array of 6 bytes containing the destination's MAC.
* \param mac_src uint8_t array of 6 bytes containing the source's MAC.
* \param child PDU* with the PDU contained by the ethernet PDU (optional).
*/
EthernetII(const std::string& iface, const uint8_t* mac_dst, const uint8_t* mac_src, PDU* child = 0) throw (std::runtime_error);
static const uint8_t* BROADCAST;
/**
* \brief Constructor for creating an ethernet PDU
*
* Constructor that builds an ethernet PDU taking the destination's
* and source's MAC.
* Constructor that builds an ethernet PDU taking the interface name,
* destination's and source's MAC.
*
* \param mac_dst uint8_t array of 6 bytes containing the destination's MAC.
* \param mac_src uint8_t array of 6 bytes containing the source's MAC.
* \param iface_index uint32_t containing the interface's index from where to send the packet.
* \param iface string containing the interface's name from where to send the packet.
* \param mac_dst uint8_t array of 6 bytes containing the destination's MAC(optional).
* \param mac_src uint8_t array of 6 bytes containing the source's MAC(optional).
* \param child PDU* with the PDU contained by the ethernet PDU (optional).
*/
EthernetII(const uint32_t iface_index, const uint8_t* mac_dst = 0, const uint8_t* mac_src = 0, PDU* child = 0);
EthernetII(const std::string& iface, const uint8_t* mac_dst = 0, const uint8_t* mac_src = 0, PDU* child = 0) throw (std::runtime_error);
/**
* \brief Constructor for creating an ethernet PDU
*
* Constructor that builds an ethernet PDU taking the interface index,
* destination's and source's MAC.
*
* \param iface_index const uint32_t with the interface's index from where to send the packet.
* \param mac_dst uint8_t array of 6 bytes containing the destination's MAC(optional).
* \param mac_src uint8_t array of 6 bytes containing the source's MAC(optional).
* \param child PDU* with the PDU contained by the ethernet PDU (optional).
*/
EthernetII(uint32_t iface_index, const uint8_t* mac_dst = 0, const uint8_t* mac_src = 0, PDU* child = 0);
/* Getters */
/**
@@ -125,15 +131,15 @@ namespace Tins {
* \sa PDU::send()
*/
bool send(PacketSender* sender);
/** \brief Check wether ptr points to a valid response for this PDU.
*
*
* \sa PDU::matches_response
* \param ptr The pointer to the buffer.
* \param total_sz The size of the buffer.
*/
bool matches_response(uint8_t *ptr, uint32_t total_sz);
/** \brief Receives a matching response for this packet.
*
* \sa PDU::recv_response
@@ -145,7 +151,7 @@ namespace Tins {
/** \brief Clones this pdu, filling the corresponding header with data
* extracted from a buffer.
*
*
* \param ptr The pointer to the from from which the data will be extracted.
* \param total_sz The size of the buffer.
* \return The cloned PDU.
@@ -163,11 +169,11 @@ namespace Tins {
} __attribute__((__packed__));
/** \brief Creates an instance of EthernetII using an ethhdr pointer.
*
*
* \param eth_ptr The pointer to the ethhdr.
*/
EthernetII(ethhdr *eth_ptr);
ethhdr _eth;
uint32_t _iface_index;