From 9a4c847e6b2becf4bae126a64df7eb0006e1e83c Mon Sep 17 00:00:00 2001 From: Santiago Alessandri Date: Wed, 17 Aug 2011 10:11:41 -0300 Subject: [PATCH] Fixed endianess in ARP PDU. Getters and Setters use little endian --- include/arp.h | 13 +++++++------ src/arp.cpp | 14 +++++++------- 2 files changed, 14 insertions(+), 13 deletions(-) diff --git a/include/arp.h b/include/arp.h index 7b12be6..cb22ca6 100644 --- a/include/arp.h +++ b/include/arp.h @@ -26,6 +26,7 @@ #include #include "pdu.h" +#include "utils.h" namespace Tins { @@ -45,7 +46,7 @@ namespace Tins { /** * \brief Default constructor for ARP PDU objects. - * + * * ARP requests and replies can be constructed easily using * ARP::make_arp_request/reply static functions. */ @@ -85,14 +86,14 @@ namespace Tins { * * \return Returns the hardware address' format in an uint16_t. */ - inline uint16_t hw_addr_format() { return this->_arp.ar_hrd; } + inline uint16_t hw_addr_format() { return Utils::net_to_host_s(this->_arp.ar_hrd); } /** * \brief Getter for the protocol address format. * * \return Returns the protocol address' format in an uint16_t. */ - inline uint16_t prot_addr_format() { return this->_arp.ar_pro; } + inline uint16_t prot_addr_format() { return Utils::net_to_host_s(this->_arp.ar_pro); } /** * \brief Getter for the hardware address length. @@ -118,7 +119,7 @@ namespace Tins { /** \brief Getter for the header size. * \return Returns the ARP header size. * \sa PDU::header_size - */ + */ uint32_t header_size() const; /* Setters */ @@ -184,7 +185,7 @@ namespace Tins { * \param new_opcode Flag enum value of the ARP opcode to set. */ void opcode(Flags new_opcode); - + /** * \brief Getter for the PDU's type. * \sa PDU::pdu_type @@ -273,7 +274,7 @@ namespace Tins { * \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); - + /** \brief Check wether ptr points to a valid response for this PDU. * * \sa PDU::matches_response diff --git a/src/arp.cpp b/src/arp.cpp index 8f9aa01..012696a 100644 --- a/src/arp.cpp +++ b/src/arp.cpp @@ -52,7 +52,7 @@ Tins::PDU* Tins::ARP::make_arp_request(const std::string& iface, if (hw_snd) { memcpy(arp->_arp.ar_sha, hw_snd, 6); } - arp->_arp.ar_op = Utils::net_to_host_s(REQUEST); + arp->opcode(REQUEST); /* Create the EthernetII PDU with the ARP PDU as its inner PDU */ EthernetII* eth = new EthernetII(iface, Tins::EthernetII::BROADCAST, hw_snd, arp); @@ -82,7 +82,7 @@ Tins::PDU* Tins::ARP::make_arp_reply(const string& iface, 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 = Utils::net_to_host_s(REPLY); + arp->opcode(REPLY); /* Create the EthernetII PDU with the ARP PDU as its inner PDU */ EthernetII* eth = new EthernetII(iface, hw_tgt, hw_snd, arp); @@ -118,11 +118,11 @@ void Tins::ARP::target_ip_addr(uint32_t new_tgt_ip_addr) { } void Tins::ARP::hw_addr_format(uint16_t new_hw_addr_fmt) { - this->_arp.ar_hrd = new_hw_addr_fmt; + this->_arp.ar_hrd = Utils::net_to_host_s(new_hw_addr_fmt); } void Tins::ARP::prot_addr_format(uint16_t new_prot_addr_fmt) { - this->_arp.ar_pro = new_prot_addr_fmt; + this->_arp.ar_pro = Utils::net_to_host_s(new_prot_addr_fmt); } void Tins::ARP::hw_addr_length(uint8_t new_hw_addr_len) { @@ -134,7 +134,7 @@ void Tins::ARP::prot_addr_length(uint8_t new_prot_addr_len) { } void Tins::ARP::opcode(Flags new_opcode) { - this->_arp.ar_op = new_opcode; + this->_arp.ar_op = Utils::net_to_host_s(new_opcode); } void Tins::ARP::set_arp_request(const std::string& ip_tgt, const std::string& ip_snd, const uint8_t* hw_snd) { @@ -142,7 +142,7 @@ void Tins::ARP::set_arp_request(const std::string& ip_tgt, const std::string& ip this->_arp.ar_sip = Utils::resolve_ip(ip_snd); if (hw_snd) memcpy(this->_arp.ar_sha, hw_snd, 6); - this->_arp.ar_op = Utils::net_to_host_s(REQUEST); + this->opcode(REQUEST); } void Tins::ARP::set_arp_reply(const std::string& ip_tgt, @@ -154,7 +154,7 @@ void Tins::ARP::set_arp_reply(const std::string& 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 = Utils::net_to_host_s(REPLY); + this->opcode(REPLY); }