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

Added an overload of PacketSender::send that takes a NetworkInterface.

This commit is contained in:
Matias Fontanini
2013-03-20 23:24:43 -03:00
parent 247273e086
commit 16a99ef35b
2 changed files with 53 additions and 2 deletions

View File

@@ -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<typename T>
void send(PDU &pdu, const NetworkInterface &iface) {
T *actual_pdu = static_cast<T*>(&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);

View File

@@ -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<Tins::EthernetII>(pdu, iface);
break;
case PDU::DOT11:
send<Tins::Dot11>(pdu, iface);
break;
case PDU::RADIOTAP:
send<Tins::RadioTap>(pdu, iface);
break;
case PDU::IEEE802_3:
send<Tins::IEEE802_3>(pdu, iface);
break;
default:
send(pdu);
};
}
PDU *PacketSender::send_recv(PDU &pdu) {
try {
pdu.send(*this);