diff --git a/include/utils.h b/include/utils.h index 73aa754..dc7866f 100644 --- a/include/utils.h +++ b/include/utils.h @@ -80,13 +80,14 @@ namespace Tins { /** \brief Resolves the hardware address for a given ip. * + * \param iface The interface in which the packet will be sent. * \param ip The ip to resolve, in integer format. * \param buffer The buffer in which the host's hardware address will be stored. - * \param sender The sender to use to send and receive the ARP requests.(optional) + * \param sender The sender to use to send and receive the ARP requests. * \return Returns true if the hardware address was resolved successfully, * false otherwise. */ - bool resolve_hwaddr(uint32_t ip, uint8_t *buffer, PacketSender *sender = 0); + bool resolve_hwaddr(const std::string &iface, uint32_t ip, uint8_t *buffer, PacketSender *sender); /** \brief List all network interfaces. * diff --git a/src/arp.cpp b/src/arp.cpp index a57b403..a54839e 100644 --- a/src/arp.cpp +++ b/src/arp.cpp @@ -59,9 +59,9 @@ Tins::PDU* Tins::ARP::make_arp_request(const std::string& iface, return eth; } -Tins::PDU* Tins::ARP::make_arp_reply(const std::string& iface, - const std::string& target, - const std::string& sender, +Tins::PDU* Tins::ARP::make_arp_reply(const string& iface, + const string& target, + const string& sender, const uint8_t* hw_tgt, const uint8_t* hw_snd) { @@ -70,7 +70,7 @@ Tins::PDU* Tins::ARP::make_arp_reply(const std::string& iface, return make_arp_reply(iface, target_ip, sender_ip, hw_tgt, hw_snd); } -Tins::PDU* Tins::ARP::make_arp_reply(const std::string& iface, +Tins::PDU* Tins::ARP::make_arp_reply(const string& iface, uint32_t target, uint32_t sender, const uint8_t* hw_tgt, diff --git a/src/utils.cpp b/src/utils.cpp index 7522a99..b1142c8 100644 --- a/src/utils.cpp +++ b/src/utils.cpp @@ -30,6 +30,8 @@ #include #endif #include "utils.h" +#include "pdu.h" +#include "arp.h" using namespace std; @@ -155,8 +157,23 @@ uint32_t Tins::Utils::resolve_ip(const string &to_resolve) { return ((struct in_addr**)data->h_addr_list)[0]->s_addr; } -bool Tins::Utils::resolve_hwaddr(uint32_t ip, uint8_t *buffer, PacketSender *sender) { - return false; +bool Tins::Utils::resolve_hwaddr(const string &iface, uint32_t ip, uint8_t *buffer, PacketSender *sender) { + uint32_t my_ip; + uint8_t my_hw[6]; + if(!interface_ip(iface, my_ip) || !interface_hwaddr(iface, my_hw)) + return false; + PDU *packet = ARP::make_arp_request(iface, ip, my_ip, my_hw); + PDU *response = sender->send_recv(packet); + delete packet; + if(response) { + ARP *arp_resp = dynamic_cast(response->inner_pdu()); + if(arp_resp) + memcpy(buffer, arp_resp->sender_hw_addr(), 6); + delete response; + return arp_resp; + } + else + return false; } set Tins::Utils::network_interfaces() { @@ -179,10 +196,8 @@ bool Tins::Utils::interface_hwaddr(const string &iface, uint8_t *buffer) { } bool Tins::Utils::interface_id(const string &iface, uint32_t &id) { - id = if_nametoindex(iface.c_str()); return (((int32_t)id) != -1); - } uint32_t Tins::Utils::crc32(uint8_t* data, uint32_t data_size) {