diff --git a/include/arp.h b/include/arp.h index 67113cb..396267a 100644 --- a/include/arp.h +++ b/include/arp.h @@ -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; diff --git a/include/ethernetII.h b/include/ethernetII.h index c25dd47..ca84954 100644 --- a/include/ethernetII.h +++ b/include/ethernetII.h @@ -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; diff --git a/src/arp.cpp b/src/arp.cpp index 8ab6606..a57b403 100644 --- a/src/arp.cpp +++ b/src/arp.cpp @@ -19,17 +19,76 @@ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ - +#include #include #include #include #include "arp.h" +#include "ethernetII.h" #include "rawpdu.h" #include "utils.h" using namespace std; +Tins::PDU* Tins::ARP::make_arp_request(const std::string& iface, + const std::string& target, + const std::string& sender, + const uint8_t* hw_snd) { + uint32_t target_ip = Tins::Utils::resolve_ip(target); + uint32_t sender_ip = Tins::Utils::resolve_ip(sender); + return make_arp_request(iface, target_ip, sender_ip, hw_snd); +} + +Tins::PDU* Tins::ARP::make_arp_request(const std::string& iface, + uint32_t target, + uint32_t sender, + const uint8_t* hw_snd) { + + /* Create ARP packet and set its attributes */ + ARP* arp = new ARP(); + arp->_arp.ar_tip = target; + arp->_arp.ar_sip = sender; + if (hw_snd) { + memcpy(arp->_arp.ar_sha, hw_snd, 6); + } + arp->_arp.ar_op = Tins::ARP::REQUEST; + + /* Create the EthernetII PDU with the ARP PDU as its inner PDU */ + EthernetII* eth = new EthernetII(iface, Tins::EthernetII::BROADCAST, hw_snd, arp); + return eth; +} + +Tins::PDU* Tins::ARP::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) { + + uint32_t target_ip = Tins::Utils::resolve_ip(target); + uint32_t sender_ip = Tins::Utils::resolve_ip(sender); + return make_arp_reply(iface, target_ip, sender_ip, hw_tgt, hw_snd); +} + +Tins::PDU* Tins::ARP::make_arp_reply(const std::string& iface, + uint32_t target, + uint32_t sender, + const uint8_t* hw_tgt, + const uint8_t* hw_snd) { + + /* Create ARP packet and set its attributes */ + ARP* arp = new ARP(); + arp->_arp.ar_tip = target; + arp->_arp.ar_sip = sender; + memcpy(arp->_arp.ar_sha, hw_snd, 6); + memcpy(arp->_arp.ar_tha, hw_tgt, 6); + arp->_arp.ar_op = Tins::ARP::REPLY; + + /* Create the EthernetII PDU with the ARP PDU as its inner PDU */ + EthernetII* eth = new EthernetII(iface, hw_tgt, hw_snd, arp); + return eth; +} + Tins::ARP::ARP() : PDU(0x0608) { std::memset(&_arp, 0, sizeof(arphdr)); _arp.ar_hrd = 0x0100; @@ -78,10 +137,26 @@ void Tins::ARP::opcode(Flags new_opcode) { this->_arp.ar_op = new_opcode; } -void Tins::ARP::set_arp_request(const string &ip_dst, const string &ip_src, const string &hw_src) { - _arp.ar_tip = Utils::resolve_ip(ip_dst); - _arp.ar_sip = Utils::resolve_ip(ip_src); - _arp.ar_op = REQUEST; +void Tins::ARP::set_arp_request(const std::string& ip_tgt, const std::string& ip_snd, const uint8_t* hw_snd) { + this->_arp.ar_tip = Utils::resolve_ip(ip_tgt); + this->_arp.ar_sip = Utils::resolve_ip(ip_snd); + if (hw_snd) { + memcpy(this->_arp.ar_sha, hw_snd, 6); + } + this->_arp.ar_op = REQUEST; +} + +void Tins::ARP::set_arp_reply(const std::string& ip_tgt, + const std::string& ip_snd, + const uint8_t* hw_tgt, + const uint8_t* hw_snd) { + + this->_arp.ar_tip = Utils::resolve_ip(ip_tgt); + this->_arp.ar_sip = Utils::resolve_ip(ip_snd); + memcpy(this->_arp.ar_tha, hw_tgt, 6); + memcpy(this->_arp.ar_sha, hw_snd, 6); + this->_arp.ar_op = REPLY; + } uint32_t Tins::ARP::header_size() const { diff --git a/src/ethernetII.cpp b/src/ethernetII.cpp index 29f9712..ed18439 100644 --- a/src/ethernetII.cpp +++ b/src/ethernetII.cpp @@ -31,6 +31,8 @@ #include "rawpdu.h" #include "utils.h" +const uint8_t* Tins::EthernetII::BROADCAST = (const uint8_t*)"\xff\xff\xff\xff\xff\xff"; + Tins::EthernetII::EthernetII(const std::string& iface, const uint8_t* mac_dst, const uint8_t* mac_src, PDU* child) throw (std::runtime_error) : PDU(ETHERTYPE_IP, child) { memset(&_eth, 0, sizeof(ethhdr)); if(mac_dst)