From 16a99ef35becf469348f5e67a1d134926f4d3b93 Mon Sep 17 00:00:00 2001 From: Matias Fontanini Date: Wed, 20 Mar 2013 23:24:43 -0300 Subject: [PATCH] Added an overload of PacketSender::send that takes a NetworkInterface. --- include/packet_sender.h | 29 +++++++++++++++++++++++++++-- src/packet_sender.cpp | 26 ++++++++++++++++++++++++++ 2 files changed, 53 insertions(+), 2 deletions(-) diff --git a/include/packet_sender.h b/include/packet_sender.h index 7008e11..c24e122 100644 --- a/include/packet_sender.h +++ b/include/packet_sender.h @@ -130,6 +130,23 @@ namespace Tins { * \param pdu The PDU to be sent. */ void send(PDU &pdu); + + /** + * \brief Sends a PDU. + * + * \sa 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. + * + * \param pdu The PDU to be sent. + * \param iface The network interface to use. + */ + void send(PDU &pdu, const NetworkInterface &iface); /** * \brief Sends a PDU and waits for its response. @@ -212,9 +229,17 @@ namespace Tins { int find_type(SocketType type); int timeval_subtract (struct timeval *result, struct timeval *x, struct timeval *y); #ifndef WIN32 - bool ether_socket_initialized(const NetworkInterface& iface = NetworkInterface()) const; - int get_ether_socket(const NetworkInterface& iface = NetworkInterface()); + bool ether_socket_initialized(const NetworkInterface& iface = NetworkInterface()) const; + int get_ether_socket(const NetworkInterface& iface = NetworkInterface()); #endif + template + void send(PDU &pdu, const NetworkInterface &iface) { + T *actual_pdu = static_cast(&pdu); + NetworkInterface old_iface = actual_pdu->iface(); + actual_pdu->iface(iface); + send(*actual_pdu); + actual_pdu->iface(old_iface); + } PDU *recv_match_loop(int sock, PDU &pdu, struct sockaddr* link_addr, uint32_t addrlen); diff --git a/src/packet_sender.cpp b/src/packet_sender.cpp index a01e347..b966d30 100644 --- a/src/packet_sender.cpp +++ b/src/packet_sender.cpp @@ -59,6 +59,12 @@ #include "pdu.h" #include "macros.h" #include "network_interface.h" +// PDUs required by PacketSender::send(PDU&, NetworkInterface) +#include "ethernetII.h" +#include "radiotap.h" +#include "dot11.h" +#include "radiotap.h" +#include "ieee802_3.h" namespace Tins { @@ -213,6 +219,26 @@ void PacketSender::send(PDU &pdu) { pdu.send(*this); } +void PacketSender::send(PDU &pdu, const NetworkInterface &iface) { + PDU::PDUType type = pdu.pdu_type(); + switch(type) { + case PDU::ETHERNET_II: + send(pdu, iface); + break; + case PDU::DOT11: + send(pdu, iface); + break; + case PDU::RADIOTAP: + send(pdu, iface); + break; + case PDU::IEEE802_3: + send(pdu, iface); + break; + default: + send(pdu); + }; +} + PDU *PacketSender::send_recv(PDU &pdu) { try { pdu.send(*this);