1
0
mirror of https://github.com/mfontanini/libtins synced 2026-01-23 02:35:57 +01:00

Link layer PDUs no longer contain a NetworkInterface.

This commit is contained in:
Matias Fontanini
2013-04-21 18:50:08 -03:00
parent 6f04329fbe
commit 97f049580b
27 changed files with 599 additions and 649 deletions

View File

@@ -43,23 +43,18 @@ using namespace std;
using namespace Tins;
int do_arp_spoofing(NetworkInterface iface, IPv4Address gw, IPv4Address victim,
void do_arp_spoofing(NetworkInterface iface, IPv4Address gw, IPv4Address victim,
const NetworkInterface::Info &info)
{
PacketSender sender;
EthernetII::address_type gw_hw, victim_hw;
// Resolves gateway's hardware address.
if(!Utils::resolve_hwaddr(iface, gw, &gw_hw, sender)) {
cout << "Could not resolve gateway's ip address.\n";
return 5;
}
gw_hw = Utils::resolve_hwaddr(iface, gw, sender);
// Resolves victim's hardware address.
if(!Utils::resolve_hwaddr(iface, victim, &victim_hw, sender)) {
cout << "Could not resolve victim's ip address.\n";
return 6;
}
victim_hw = Utils::resolve_hwaddr(iface, victim, sender);
// Print out the hw addresses we're using.
cout << " Using gateway hw address: " << gw_hw << "\n";
cout << " Using victim hw address: " << victim_hw << "\n";
@@ -77,12 +72,12 @@ int do_arp_spoofing(NetworkInterface iface, IPv4Address gw, IPv4Address victim,
* We include our hw address as the source address
* in ethernet layer, to avoid possible packet dropping
* performed by any routers. */
EthernetII to_gw(iface, gw_hw, info.hw_addr, gw_arp);
EthernetII to_victim(iface, victim_hw, info.hw_addr, victim_arp);
EthernetII to_gw(gw_hw, info.hw_addr, gw_arp);
EthernetII to_victim(victim_hw, info.hw_addr, victim_arp);
while(true) {
// Just send them once every 5 seconds.
sender.send(to_gw);
sender.send(to_victim);
sender.send(to_gw, iface);
sender.send(to_victim, iface);
sleep(5);
}
}
@@ -116,7 +111,7 @@ int main(int argc, char *argv[]) {
return 3;
}
try {
return do_arp_spoofing(iface, gw, victim, info);
do_arp_spoofing(iface, gw, victim, info);
}
catch(std::runtime_error &ex) {
std::cout << "Runtime error: " << ex.what() << std::endl;

View File

@@ -97,8 +97,8 @@ void send_syns(const NetworkInterface &iface, IPv4Address dest_ip, const vector<
// Pretend we're the scanned host...
ip.src_addr(dest_ip);
// We use an ethernet pdu, otherwise the kernel will drop it.
EthernetII eth(iface, info.hw_addr, info.hw_addr, ip.clone());
sender.send(eth);
EthernetII eth(info.hw_addr, info.hw_addr, ip.clone());
sender.send(eth, iface);
}
void *thread_proc(void *param) {

View File

@@ -236,34 +236,32 @@ namespace Tins {
PDUType pdu_type() const { return PDU::ARP; }
/**
* \brief Creates an ARP Request within a Layer 2 PDU using uint32_t for target and sender.
* \brief Creates an ARP Request within an EthernetII PDU.
*
* 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.
* sent.
*
* \param iface string with the interface from where to send the ARP.
* \param target IPv4Address with the target's IP.
* \param sender IPv4Address with the sender's IP.
* \param hw_snd uint8_t array of 6 bytes containing the sender's hardware address.
* \return Returns a EthernetII containing the ARP Request.
*/
static EthernetII make_arp_request(const NetworkInterface& iface, ipaddress_type target,
static EthernetII make_arp_request(ipaddress_type target,
ipaddress_type sender, const hwaddress_type &hw_snd = hwaddress_type());
/**
* \brief Creates an ARP Reply within a Layer 2 PDU using uint32_t for target and sender.
* \brief Creates an ARP Reply within an EthernetII PDU.
*
* 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.
* sent.
*
* \param iface string with the interface from where to send the ARP.
* \param target IPv4Address with the target's IP.
* \param sender IPv4Address 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 an EthetnetII containing the ARP Replay.
*/
static EthernetII make_arp_reply(const NetworkInterface& iface, ipaddress_type target,
static EthernetII make_arp_reply(ipaddress_type target,
ipaddress_type sender, const hwaddress_type &hw_tgt = hwaddress_type(),
const hwaddress_type &hw_snd = hwaddress_type());

View File

@@ -285,14 +285,6 @@ namespace Tins {
*/
address_type addr1() const { return _header.addr1; }
/**
* \brief Getter for the network interface.
*
* \return const NetworkInterface& containing the network
* interface in which this PDU will be sent.
*/
const NetworkInterface &iface() const { return _iface; }
/**
* \brief Setter for the protocol version.
*
@@ -377,14 +369,6 @@ namespace Tins {
*/
void addr1(const address_type &new_addr1);
/**
* \brief Setter for the network interface.
*
* \param new_iface The network interface in which this PDU
* will be sent.
*/
void iface(const NetworkInterface &new_iface);
/* Virtual methods */
/**
* \brief Returns the 802.11 frame's header length.
@@ -398,7 +382,7 @@ namespace Tins {
/**
* \sa PDU::send()
*/
void send(PacketSender &sender);
void send(PacketSender &sender, const NetworkInterface &iface);
#endif // WIN32
/**
@@ -520,7 +504,6 @@ namespace Tins {
ieee80211_header _header;
NetworkInterface _iface;
uint32_t _options_size;
std::list<option> _options;
};

View File

@@ -35,7 +35,6 @@
#include "pdu.h"
#include "endianness.h"
#include "hw_address.h"
#include "network_interface.h"
namespace Tins {
@@ -65,13 +64,11 @@ namespace Tins {
* Constructor that builds an Dot3 PDU taking the interface name,
* destination's and source's MAC.
*
* \param iface string containing the interface's name from where to send the packet.
* \param dst_hw_addr The destination hardware address.
* \param src_hw_addr The source hardware address.
* \param child The PDU which will be set as the inner PDU.
*/
Dot3(const NetworkInterface& iface = NetworkInterface(),
const address_type &dst_hw_addr = address_type(),
Dot3(const address_type &dst_hw_addr = address_type(),
const address_type &src_hw_addr = address_type(),
PDU* child = 0);
@@ -102,13 +99,6 @@ namespace Tins {
*/
address_type src_addr() const { return _eth.src_mac; }
/**
* \brief Getter for the interface.
*
* \return The network interface.
*/
const NetworkInterface &iface() const { return this->_iface; }
/**
* \brief Getter for the length field.
* \return The length field value.
@@ -131,13 +121,6 @@ namespace Tins {
*/
void src_addr(const address_type &new_src_mac);
/**
* \brief Setter for the interface.
*
* \param new_iface The interface in which to send this PDU.
*/
void iface(const NetworkInterface &new_iface);
/**
* \brief Setter for the length field.
*
@@ -158,7 +141,7 @@ namespace Tins {
/**
* \sa PDU::send()
*/
void send(PacketSender &sender);
void send(PacketSender &sender, const NetworkInterface &iface);
#endif // WIN32
/**
@@ -172,12 +155,9 @@ namespace Tins {
#ifndef WIN32
/**
* \brief Receives a matching response for this packet.
*
* \sa PDU::recv_response
* \param sender The packet sender which will receive the packet.
*/
PDU *recv_response(PacketSender &sender);
PDU *recv_response(PacketSender &sender, const NetworkInterface &iface);
#endif // WIN32
/**
@@ -220,7 +200,6 @@ namespace Tins {
ethhdr _eth;
NetworkInterface _iface;
};
}

View File

@@ -35,7 +35,6 @@
#include "pdu.h"
#include "endianness.h"
#include "hw_address.h"
#include "network_interface.h"
namespace Tins {
@@ -60,18 +59,13 @@ namespace Tins {
static const address_type BROADCAST;
/**
* \brief Constructor for creating an ethernet PDU
* \brief Constructs an ethernet II PDU.
*
* Constructor that builds an ethernet PDU taking the interface name,
* destination's and source's MAC.
*
* \param iface string containing the interface's name from where to send the packet.
* \param dst_hw_addr address_type containing the destination's MAC(optional).
* \param src_hw_addr address_type containing the source's MAC(optional).
* \param child PDU* with the PDU contained by the ethernet PDU (optional).
*/
EthernetII(const NetworkInterface& iface = NetworkInterface(),
const address_type &dst_hw_addr = address_type(),
EthernetII(const address_type &dst_hw_addr = address_type(),
const address_type &src_hw_addr = address_type(),
PDU* child = 0);
@@ -106,13 +100,6 @@ namespace Tins {
*/
address_type src_addr() const { return _eth.src_mac; }
/**
* \brief Getter for the interface.
*
* \return Returns the interface in which this PDU will be sent.
*/
const NetworkInterface &iface() const { return _iface; }
/**
* \brief Getter for the payload_type
* \return The payload type.
@@ -135,13 +122,6 @@ namespace Tins {
*/
void src_addr(const address_type &new_src_addr);
/**
* \brief Setter for the interface.
*
* \param new_iface the interface to be set.
*/
void iface(const NetworkInterface& new_iface);
/**
* \brief Setter for the payload type.
*
@@ -163,7 +143,7 @@ namespace Tins {
/**
* \sa PDU::send()
*/
void send(PacketSender &sender);
void send(PacketSender &sender, const NetworkInterface &iface);
#endif // WIN32
/**
@@ -176,12 +156,12 @@ namespace Tins {
bool matches_response(uint8_t *ptr, uint32_t total_sz);
#ifndef WIN32
/** \brief Receives a matching response for this packet.
/**
* \brief Receives a matching response for this packet.
*
* \sa PDU::recv_response
* \param sender The packet sender which will receive the packet.
*/
PDU *recv_response(PacketSender &sender);
PDU *recv_response(PacketSender &sender, const NetworkInterface &iface);
#endif // WIN32
/**
@@ -223,7 +203,6 @@ namespace Tins {
void write_serialization(uint8_t *buffer, uint32_t total_sz, const PDU *parent);
ethhdr _eth;
NetworkInterface _iface;
};
}

View File

@@ -72,6 +72,20 @@ public:
return "PDU not found";
}
};
/**
* \brief Exception thrown when PDU::send requires a valid interface,
* but an invalid is used.
*/
class invalid_interface : public std::runtime_error {
public:
invalid_interface()
: std::runtime_error(std::string()) { }
const char* what() const throw() {
return "Invalid interface";
}
};
}
#endif // TINS_EXCEPTIONS_H

View File

@@ -572,7 +572,7 @@ namespace Tins {
/**
* \sa PDU::send()
*/
void send(PacketSender &sender);
void send(PacketSender &sender, const NetworkInterface &);
/**
* \brief Check wether ptr points to a valid response for this PDU.
@@ -589,7 +589,7 @@ namespace Tins {
* \sa PDU::recv_response
* \param sender The packet sender which will receive the packet.
*/
PDU *recv_response(PacketSender &sender);
PDU *recv_response(PacketSender &sender, const NetworkInterface &);
/**
* \brief Getter for the PDU's type.

View File

@@ -276,7 +276,7 @@ public:
/**
* \sa PDU::send()
*/
void send(PacketSender &sender);
void send(PacketSender &sender, const NetworkInterface &);
#endif
/**

View File

@@ -32,7 +32,6 @@
#include "pdu.h"
#include "macros.h"
#include "network_interface.h"
namespace Tins {
class Loopback : public PDU {
@@ -55,10 +54,9 @@ public:
* The NetworkInterface object will only be used in *BSD, where
* Null/Loopback PDUs can actually be sent.
*
* \param iface The network interface in which to send this PDU.
* \param inner_pdu The inner pdu to be set.
*/
Loopback(const NetworkInterface &iface, PDU *inner_pdu = 0);
Loopback(PDU *inner_pdu = 0);
/**
* \brief Construct a Loopback object from a buffer and adds
@@ -98,18 +96,6 @@ public:
*/
PDUType pdu_type() const { return PDU::IP; }
/**
* \brief Getter for the interface member.
*/
const NetworkInterface &iface() const { return _iface; }
/**
* \brief Setter for the interface member.
*
* \param new_iface The new interface to be set.
*/
void iface(const NetworkInterface &new_iface);
/**
* \sa PDU::clone
*/
@@ -121,13 +107,12 @@ public:
/**
* \sa PDU::send()
*/
void send(PacketSender &sender);
void send(PacketSender &sender, const NetworkInterface &iface);
#endif // BSD
private:
void write_serialization(uint8_t *buffer, uint32_t total_sz, const PDU *parent);
uint32_t _family;
NetworkInterface _iface;
};
}

View File

@@ -163,6 +163,20 @@ namespace Tins {
*/
PDU *send_recv(PDU &pdu);
/**
* \brief Sends a PDU and waits for its response.
*
* This method is used to send PDUs and receive their response.
* It opens the required socket(if it's not open yet). This can be used
* to expect responses for ICMP, ARP, and such packets that are normally
* answered by the host that receives the packet.
*
* \param pdu The PDU to send.
* \param iface The network interface in which to send and receive.
* \return Returns the response PDU, 0 if not response was received.
*/
PDU *send_recv(PDU &pdu, const NetworkInterface &iface);
#ifndef WIN32
/**
* \brief Receives a layer 2 PDU response to a previously sent PDU.
@@ -236,11 +250,7 @@ namespace Tins {
#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);
static_cast<T&>(pdu).send(*this, iface);
}
PDU *recv_match_loop(int sock, PDU &pdu, struct sockaddr* link_addr, uint32_t addrlen);

View File

@@ -42,6 +42,7 @@
namespace Tins {
class PacketSender;
class NetworkInterface;
/**
* The type used to store several PDU option values.
@@ -286,25 +287,37 @@ namespace Tins {
*/
virtual PDU *clone() const = 0;
/** \brief Send the stack of PDUs through a PacketSender.
/**
* \brief Send the stack of PDUs through a PacketSender.
*
* This method will be called only for the PDU on the bottom of the stack,
* therefore it should only implement this method if it can be sent.
*
* PacketSender implements specific methods to send packets which start
* on every valid TCP/IP stack layer; this should only be a proxy for
* those methods.
*
* If this PDU does not represent a link layer protocol, then
* the interface argument will be ignored.
*
* \param sender The PacketSender which will send the packet.
* \param iface The network interface in which this packet will
* be sent.
*/
virtual void send(PacketSender &sender);
virtual void send(PacketSender &sender, const NetworkInterface &iface);
/** \brief Receives a matching response for this packet.
/**
* \brief Receives a matching response for this packet.
*
* This method should act as a proxy for PacketSender::recv_lX methods.
*
* \param sender The packet sender which will receive the packet.
* \param iface The interface in which to expect the response.
*/
virtual PDU *recv_response(PacketSender &sender);
virtual PDU *recv_response(PacketSender &sender, const NetworkInterface &iface);
/** \brief Check wether ptr points to a valid response for this PDU.
/**
* \brief Check wether ptr points to a valid response for this PDU.
*
* This method must check wether the buffer pointed by ptr is a valid
* response for this PDU. If it is valid, then it might want to propagate

View File

@@ -95,15 +95,15 @@ public:
/**
* Forwards the call to the cached PDU. \sa PDU::send.
*/
void send(PacketSender &sender) {
cached.send(sender);
void send(PacketSender &sender, const NetworkInterface &iface) {
cached.send(sender, iface);
}
/**
* Forwards the call to the cached PDU. \sa PDU::recv_responde.
*/
PDU *recv_response(PacketSender &sender) {
return cached.recv_response(sender);
PDU *recv_response(PacketSender &sender, const NetworkInterface &iface) {
return cached.recv_response(sender, iface);
}
/**

View File

@@ -33,7 +33,6 @@
#include "macros.h"
#include "pdu.h"
#include "endianness.h"
#include "network_interface.h"
namespace Tins {
class PacketSender;
@@ -109,11 +108,9 @@ namespace Tins {
/**
* \brief Creates an instance of RadioTap.
* \param iface The interface in which to send this PDU.
* \param child The child PDU.(optional)
*/
RadioTap(const NetworkInterface &iface = NetworkInterface(),
PDU *child = 0);
RadioTap(PDU *child = 0);
/**
* \brief Constructs a RadioTap object from a buffer and adds all
@@ -133,7 +130,7 @@ namespace Tins {
/**
* \sa PDU::send()
*/
void send(PacketSender &sender);
void send(PacketSender &sender, const NetworkInterface &iface);
#endif
/**
@@ -203,13 +200,6 @@ namespace Tins {
*/
void rx_flags(uint16_t new_rx_flag);
/**
* \brief Setter for the interface.
*
* \param new_iface the interface to be set.
*/
void iface(const NetworkInterface& new_iface);
/* Getters */
/**
@@ -303,13 +293,6 @@ namespace Tins {
return (PresentFlags)*(uint32_t*)(&_radio.it_len + 1);
}
/**
* \brief Getter for the interface.
*
* \return Returns the interface in which this PDU will be sent.
*/
const NetworkInterface &iface() const { return _iface; }
/** \brief Check wether ptr points to a valid response for this PDU.
*
* \sa PDU::matches_response
@@ -403,7 +386,6 @@ namespace Tins {
radiotap_hdr _radio;
NetworkInterface _iface;
// present fields...
uint64_t _tsft;
uint32_t _channel_type;

View File

@@ -123,8 +123,8 @@ PDU *ARP::clone_packet(const uint8_t *ptr, uint32_t total_sz) {
return new ARP(ptr, total_sz);
}
EthernetII ARP::make_arp_request(const NetworkInterface& iface,
ipaddress_type target, ipaddress_type sender, const hwaddress_type &hw_snd)
EthernetII ARP::make_arp_request(ipaddress_type target, ipaddress_type sender,
const hwaddress_type &hw_snd)
{
/* Create ARP packet and set its attributes */
ARP* arp = new ARP();
@@ -134,18 +134,17 @@ EthernetII ARP::make_arp_request(const NetworkInterface& iface,
arp->opcode(REQUEST);
/* Create the EthernetII PDU with the ARP PDU as its inner PDU */
return EthernetII(iface, EthernetII::BROADCAST, hw_snd, arp);
return EthernetII(EthernetII::BROADCAST, hw_snd, arp);
}
EthernetII ARP::make_arp_reply(const NetworkInterface& iface,
ipaddress_type target, ipaddress_type sender, const hwaddress_type &hw_tgt,
const hwaddress_type &hw_snd)
EthernetII ARP::make_arp_reply(ipaddress_type target, ipaddress_type sender,
const hwaddress_type &hw_tgt, const hwaddress_type &hw_snd)
{
/* Create ARP packet and set its attributes */
ARP* arp = new ARP(target, sender, hw_tgt, hw_snd);
arp->opcode(REPLY);
/* Create the EthernetII PDU with the ARP PDU as its inner PDU */
return EthernetII(iface, hw_tgt, hw_snd, arp);
return EthernetII(hw_tgt, hw_snd, arp);
}
}

View File

@@ -180,19 +180,15 @@ void Dot11::addr1(const address_type &new_addr1) {
std::copy(new_addr1.begin(), new_addr1.end(), _header.addr1);
}
void Dot11::iface(const NetworkInterface &new_iface) {
this->_iface = new_iface;
}
uint32_t Dot11::header_size() const {
uint32_t sz = sizeof(ieee80211_header) + _options_size;
return sz;
}
#ifndef WIN32
void Dot11::send(PacketSender &sender) {
if(!_iface)
throw std::runtime_error("Interface has not been set");
void Dot11::send(PacketSender &sender, const NetworkInterface &iface) {
if(!iface)
throw invalid_interface();
#ifndef BSD
sockaddr_ll addr;
@@ -202,11 +198,11 @@ void Dot11::send(PacketSender &sender) {
addr.sll_family = Endian::host_to_be<uint16_t>(PF_PACKET);
addr.sll_protocol = Endian::host_to_be<uint16_t>(ETH_P_ALL);
addr.sll_halen = 6;
addr.sll_ifindex = _iface.id();
addr.sll_ifindex = iface.id();
memcpy(&(addr.sll_addr), _header.addr1, 6);
sender.send_l2(*this, (struct sockaddr*)&addr, (uint32_t)sizeof(addr));
#else
sender.send_l2(*this, 0, 0, _iface);
sender.send_l2(*this, 0, 0, iface);
#endif
}
#endif // WIN32

View File

@@ -49,15 +49,13 @@
namespace Tins {
const Dot3::address_type Dot3::BROADCAST("ff:ff:ff:ff:ff:ff");
Dot3::Dot3(const NetworkInterface& iface,
const address_type &dst_hw_addr, const address_type &src_hw_addr,
Dot3::Dot3(const address_type &dst_hw_addr, const address_type &src_hw_addr,
PDU* child)
: PDU(child)
{
memset(&_eth, 0, sizeof(ethhdr));
this->dst_addr(dst_hw_addr);
this->src_addr(src_hw_addr);
this->iface(iface);
this->_eth.length = 0;
}
@@ -81,10 +79,6 @@ void Dot3::src_addr(const address_type &new_src_mac) {
std::copy(new_src_mac.begin(), new_src_mac.end(), _eth.src_mac);
}
void Dot3::iface(const NetworkInterface &new_iface) {
_iface = new_iface;
}
void Dot3::length(uint16_t new_length) {
this->_eth.length = Endian::host_to_be(new_length);
}
@@ -94,9 +88,9 @@ uint32_t Dot3::header_size() const {
}
#ifndef WIN32
void Dot3::send(PacketSender &sender) {
if(!_iface)
throw std::runtime_error("Interface has not been set");
void Dot3::send(PacketSender &sender, const NetworkInterface &iface) {
if(!iface)
throw invalid_interface();
#ifndef BSD
struct sockaddr_ll addr;
@@ -106,12 +100,12 @@ void Dot3::send(PacketSender &sender) {
addr.sll_family = Endian::host_to_be<uint16_t>(PF_PACKET);
addr.sll_protocol = Endian::host_to_be<uint16_t>(ETH_P_ALL);
addr.sll_halen = address_type::address_size;
addr.sll_ifindex = _iface.id();
addr.sll_ifindex = iface.id();
memcpy(&(addr.sll_addr), _eth.dst_mac, sizeof(_eth.dst_mac));
sender.send_l2(*this, (struct sockaddr*)&addr, (uint32_t)sizeof(addr));
#else
sender.send_l2(*this, 0, 0, _iface);
sender.send_l2(*this, 0, 0, iface);
#endif
}
#endif // WIN32
@@ -147,9 +141,9 @@ void Dot3::write_serialization(uint8_t *buffer, uint32_t total_sz, const PDU *pa
}
#ifndef WIN32
PDU *Dot3::recv_response(PacketSender &sender) {
if(!_iface)
throw std::runtime_error("Interface has not been set");
PDU *Dot3::recv_response(PacketSender &sender, const NetworkInterface &iface) {
if(!iface)
throw invalid_interface();
#ifndef BSD
struct sockaddr_ll addr;
memset(&addr, 0, sizeof(struct sockaddr_ll));
@@ -157,12 +151,12 @@ PDU *Dot3::recv_response(PacketSender &sender) {
addr.sll_family = Endian::host_to_be<uint16_t>(PF_PACKET);
addr.sll_protocol = Endian::host_to_be<uint16_t>(ETH_P_802_3);
addr.sll_halen = address_type::address_size;
addr.sll_ifindex = _iface.id();
addr.sll_ifindex = iface.id();
memcpy(&(addr.sll_addr), _eth.dst_mac, sizeof(_eth.dst_mac));
return sender.recv_l2(*this, (struct sockaddr*)&addr, (uint32_t)sizeof(addr));
#else
return sender.recv_l2(*this, 0, 0, _iface);
return sender.recv_l2(*this, 0, 0, iface);
#endif
}
#endif // WIN32

View File

@@ -54,15 +54,13 @@
namespace Tins {
const EthernetII::address_type EthernetII::BROADCAST("ff:ff:ff:ff:ff:ff");
EthernetII::EthernetII(const NetworkInterface& iface,
const address_type &dst_hw_addr, const address_type &src_hw_addr,
PDU* child)
EthernetII::EthernetII(const address_type &dst_hw_addr,
const address_type &src_hw_addr, PDU* child)
: PDU(child)
{
memset(&_eth, 0, sizeof(ethhdr));
dst_addr(dst_hw_addr);
src_addr(src_hw_addr);
this->iface(iface);
_eth.payload_type = 0;
}
@@ -93,10 +91,6 @@ void EthernetII::src_addr(const address_type &new_src_addr) {
new_src_addr.copy(_eth.src_mac);
}
void EthernetII::iface(const NetworkInterface& new_iface) {
_iface = new_iface;
}
void EthernetII::payload_type(uint16_t new_payload_type) {
this->_eth.payload_type = Endian::host_to_be(new_payload_type);
}
@@ -106,9 +100,9 @@ uint32_t EthernetII::header_size() const {
}
#ifndef WIN32
void EthernetII::send(PacketSender &sender) {
if(!_iface)
throw std::runtime_error("Interface has not been set");
void EthernetII::send(PacketSender &sender, const NetworkInterface &iface) {
if(!iface)
throw invalid_interface();
#ifndef BSD
struct sockaddr_ll addr;
@@ -118,12 +112,12 @@ void EthernetII::send(PacketSender &sender) {
addr.sll_family = Endian::host_to_be<uint16_t>(PF_PACKET);
addr.sll_protocol = Endian::host_to_be<uint16_t>(ETH_P_ALL);
addr.sll_halen = address_type::address_size;
addr.sll_ifindex = _iface.id();
addr.sll_ifindex = iface.id();
memcpy(&(addr.sll_addr), _eth.dst_mac, address_type::address_size);
sender.send_l2(*this, (struct sockaddr*)&addr, (uint32_t)sizeof(addr));
#else
sender.send_l2(*this, 0, 0, _iface);
sender.send_l2(*this, 0, 0, iface);
#endif
}
#endif // WIN32
@@ -158,7 +152,7 @@ void EthernetII::write_serialization(uint8_t *buffer, uint32_t total_sz, const P
}
#ifndef WIN32
PDU *EthernetII::recv_response(PacketSender &sender) {
PDU *EthernetII::recv_response(PacketSender &sender, const NetworkInterface &iface) {
#ifndef BSD
struct sockaddr_ll addr;
memset(&addr, 0, sizeof(struct sockaddr_ll));
@@ -166,12 +160,12 @@ PDU *EthernetII::recv_response(PacketSender &sender) {
addr.sll_family = Endian::host_to_be<uint16_t>(PF_PACKET);
addr.sll_protocol = Endian::host_to_be<uint16_t>(ETH_P_ALL);
addr.sll_halen = address_type::address_size;
addr.sll_ifindex = _iface.id();
addr.sll_ifindex = iface.id();
memcpy(&(addr.sll_addr), _eth.dst_mac, address_type::address_size);
return sender.recv_l2(*this, (struct sockaddr*)&addr, (uint32_t)sizeof(addr));
#else
return sender.recv_l2(*this, 0, 0, _iface);
return sender.recv_l2(*this, 0, 0, iface);
#endif
}
#endif // WIN32

View File

@@ -349,7 +349,7 @@ PacketSender::SocketType pdu_type_to_sender_type(PDU::PDUType type) {
}
}
void IP::send(PacketSender& sender) {
void IP::send(PacketSender& sender, const NetworkInterface &) {
sockaddr_in link_addr;
PacketSender::SocketType type = PacketSender::IP_RAW_SOCKET;
link_addr.sin_family = AF_INET;
@@ -361,7 +361,7 @@ void IP::send(PacketSender& sender) {
sender.send_l3(*this, (struct sockaddr*)&link_addr, sizeof(link_addr), type);
}
PDU *IP::recv_response(PacketSender &sender) {
PDU *IP::recv_response(PacketSender &sender, const NetworkInterface &) {
sockaddr_in link_addr;
PacketSender::SocketType type = PacketSender::IP_RAW_SOCKET;
std::memset(&link_addr, 0, sizeof(link_addr));

View File

@@ -226,7 +226,7 @@ void IPv6::write_serialization(uint8_t *buffer, uint32_t total_sz, const PDU *pa
}
#ifndef BSD
void IPv6::send(PacketSender &sender) {
void IPv6::send(PacketSender &sender, const NetworkInterface &) {
struct sockaddr_in6 link_addr;
PacketSender::SocketType type = PacketSender::IPV6_SOCKET;
link_addr.sin6_family = AF_INET6;

View File

@@ -57,8 +57,8 @@ Loopback::Loopback()
}
Loopback::Loopback(const NetworkInterface &iface, PDU *inner_pdu)
: PDU(inner_pdu), _family(), _iface(iface)
Loopback::Loopback(PDU *inner_pdu)
: PDU(inner_pdu), _family()
{
}
@@ -91,10 +91,6 @@ void Loopback::family(uint32_t family_id) {
_family = family_id;
}
void Loopback::iface(const NetworkInterface &new_iface) {
_iface = new_iface;
}
uint32_t Loopback::header_size() const {
return sizeof(_family);
}
@@ -109,11 +105,11 @@ void Loopback::write_serialization(uint8_t *buffer, uint32_t total_sz, const PDU
*reinterpret_cast<uint32_t*>(buffer) = _family;
}
#ifdef BSD
void Loopback::send(PacketSender &sender) {
if(!_iface)
throw std::runtime_error("Interface has not been set");
void Loopback::send(PacketSender &sender, const NetworkInterface &iface) {
if(!iface)
throw invalid_interface();
sender.send_l2(*this, 0, 0, _iface);
sender.send_l2(*this, 0, 0, iface);
}
#endif // WIN32
}

View File

@@ -219,7 +219,7 @@ void PacketSender::close_socket(SocketType type, const NetworkInterface &iface)
}
void PacketSender::send(PDU &pdu) {
pdu.send(*this);
pdu.send(*this, NetworkInterface());
}
void PacketSender::send(PDU &pdu, const NetworkInterface &iface) {
@@ -243,14 +243,19 @@ void PacketSender::send(PDU &pdu, const NetworkInterface &iface) {
}
PDU *PacketSender::send_recv(PDU &pdu) {
return send_recv(pdu, NetworkInterface());
}
PDU *PacketSender::send_recv(PDU &pdu, const NetworkInterface &iface) {
try {
pdu.send(*this);
pdu.send(*this, iface);
}
catch(std::runtime_error&) {
return 0;
}
return pdu.recv_response(*this);
return pdu.recv_response(*this, iface);
}
#ifndef WIN32
void PacketSender::send_l2(PDU &pdu, struct sockaddr* link_addr,
uint32_t len_addr, const NetworkInterface &iface) {

View File

@@ -79,11 +79,11 @@ uint32_t PDU::size() const {
return sz;
}
void PDU::send(PacketSender &) {
void PDU::send(PacketSender &, const NetworkInterface &) {
}
PDU *PDU::recv_response(PacketSender &) {
PDU *PDU::recv_response(PacketSender &, const NetworkInterface &) {
return 0;
}

View File

@@ -59,8 +59,8 @@ void read_field(const uint8_t* &buffer, uint32_t &total_sz, T& field) {
total_sz -= sizeof(field);
}
RadioTap::RadioTap(const NetworkInterface &iface, PDU *child)
: PDU(child), _iface(iface)
RadioTap::RadioTap(PDU *child)
: PDU(child)
{
std::memset(&_radio, 0, sizeof(_radio));
init();
@@ -198,10 +198,6 @@ void RadioTap::rx_flags(uint16_t new_rx_flag) {
_radio.rx_flags = 1;
}
void RadioTap::iface(const NetworkInterface& new_iface) {
_iface = new_iface;
}
uint32_t RadioTap::header_size() const {
uint32_t total_bytes = 0;
if(_radio.tsft)
@@ -240,9 +236,9 @@ uint32_t RadioTap::trailer_size() const {
}
#ifndef WIN32
void RadioTap::send(PacketSender &sender) {
if(!_iface)
throw std::runtime_error("Interface has not been set");
void RadioTap::send(PacketSender &sender, const NetworkInterface &iface) {
if(!iface)
throw invalid_interface();
#ifndef BSD
struct sockaddr_ll addr;
@@ -252,7 +248,7 @@ void RadioTap::send(PacketSender &sender) {
addr.sll_family = Endian::host_to_be<uint16_t>(PF_PACKET);
addr.sll_protocol = Endian::host_to_be<uint16_t>(ETH_P_ALL);
addr.sll_halen = 6;
addr.sll_ifindex = _iface.id();
addr.sll_ifindex = iface.id();
Tins::Dot11 *wlan = dynamic_cast<Tins::Dot11*>(inner_pdu());
if(wlan) {
@@ -262,7 +258,7 @@ void RadioTap::send(PacketSender &sender) {
sender.send_l2(*this, (struct sockaddr*)&addr, (uint32_t)sizeof(addr));
#else
sender.send_l2(*this, 0, 0, _iface);
sender.send_l2(*this, 0, 0, iface);
#endif
}
#endif

View File

@@ -115,8 +115,8 @@ bool resolve_hwaddr(const NetworkInterface &iface, IPv4Address ip,
{
IPv4Address my_ip;
NetworkInterface::Info info(iface.addresses());
EthernetII packet = ARP::make_arp_request(iface, ip, info.ip_addr, info.hw_addr);
Internals::smart_ptr<PDU>::type response(sender.send_recv(packet));
EthernetII packet = ARP::make_arp_request(ip, info.ip_addr, info.hw_addr);
Internals::smart_ptr<PDU>::type response(sender.send_recv(packet, iface));
if(response.get()) {
ARP *arp_resp = response->find_pdu<ARP>();
if(arp_resp)
@@ -131,8 +131,8 @@ HWAddress<6> resolve_hwaddr(const NetworkInterface &iface, IPv4Address ip, Packe
{
IPv4Address my_ip;
NetworkInterface::Info info(iface.addresses());
EthernetII packet = ARP::make_arp_request(iface, ip, info.ip_addr, info.hw_addr);
Internals::smart_ptr<PDU>::type response(sender.send_recv(packet));
EthernetII packet = ARP::make_arp_request(ip, info.ip_addr, info.hw_addr);
Internals::smart_ptr<PDU>::type response(sender.send_recv(packet, iface));
if(response.get()) {
const ARP *arp_resp = response->find_pdu<ARP>();
if(arp_resp)

File diff suppressed because it is too large Load Diff

View File

@@ -5,7 +5,6 @@
#include "macros.h"
#include "ipv6.h"
#include "ip.h"
#include "network_interface.h"
using namespace Tins;
@@ -17,7 +16,6 @@ public:
static address_type src_addr;
static address_type dst_addr;
static address_type empty_addr;
static const NetworkInterface iface;
static const uint16_t p_type;
void test_equals(const EthernetII &eth1, const EthernetII &eth2);
@@ -42,25 +40,17 @@ address_type EthernetIITest::dst_addr("aa:bb:cc:dd:ee:ff");
address_type EthernetIITest::empty_addr;
#ifdef BSD
const NetworkInterface EthernetIITest::iface("lo0");
#else
const NetworkInterface EthernetIITest::iface("lo");
#endif
const uint16_t EthernetIITest::p_type = 0xd0ab;
void EthernetIITest::test_equals(const EthernetII &eth1, const EthernetII &eth2) {
EXPECT_EQ(eth1.dst_addr(), eth2.dst_addr());
EXPECT_EQ(eth1.src_addr(), eth2.src_addr());
EXPECT_EQ(eth1.payload_type(), eth2.payload_type());
EXPECT_EQ(eth1.iface(), eth2.iface());
EXPECT_EQ((bool)eth1.inner_pdu(), (bool)eth2.inner_pdu());
}
TEST_F(EthernetIITest, DefaultConstructor) {
EthernetII eth(iface);
EXPECT_EQ(eth.iface(), iface);
EthernetII eth;
EXPECT_EQ(eth.dst_addr(), empty_addr);
EXPECT_EQ(eth.src_addr(), empty_addr);
EXPECT_EQ(eth.payload_type(), 0);
@@ -107,24 +97,17 @@ TEST_F(EthernetIITest, PayloadType) {
ASSERT_EQ(eth.payload_type(), p_type);
}
TEST_F(EthernetIITest, Interface) {
EthernetII eth;
eth.iface(iface);
ASSERT_EQ(eth.iface(), iface);
}
TEST_F(EthernetIITest, CompleteConstructor) {
EthernetII* eth2 = new EthernetII();
EthernetII eth(iface, dst_addr, src_addr, eth2);
EthernetII eth(dst_addr, src_addr, eth2);
EXPECT_EQ(eth.dst_addr(), dst_addr);
EXPECT_EQ(eth.src_addr(), src_addr);
EXPECT_TRUE(eth.inner_pdu() == eth2);
EXPECT_EQ(eth.payload_type(), 0);
EXPECT_EQ(eth.iface(), iface);
}
TEST_F(EthernetIITest, Serialize) {
EthernetII eth(iface, dst_addr, src_addr);
EthernetII eth(dst_addr, src_addr);
eth.payload_type(p_type);
PDU::serialization_type serialized = eth.serialize();
ASSERT_EQ(serialized.size(), sizeof(expected_packet));