mirror of
https://github.com/mfontanini/libtins
synced 2026-01-23 02:35:57 +01:00
Added PacketSender::recv_l2. Ethernet packets can now expect a response packet. ARP response is working now.
This commit is contained in:
@@ -181,8 +181,25 @@ namespace Tins {
|
||||
|
||||
void set_arp_request(const std::string &ip_dst, const std::string &ip_src, const std::string &hw_src = "");
|
||||
|
||||
/** \brief Check wether ptr points to a valid response for this PDU.
|
||||
*
|
||||
* \sa PDU::matches_response
|
||||
* \param ptr The pointer to the buffer.
|
||||
* \param total_sz The size of the buffer.
|
||||
*/
|
||||
bool matches_response(uint8_t *ptr, uint32_t total_sz);
|
||||
|
||||
uint32_t header_size() const;
|
||||
|
||||
/** \brief Clones this pdu, filling the corresponding header with data
|
||||
* extracted from a buffer.
|
||||
*
|
||||
* \param ptr The pointer to the from from which the data will be extracted.
|
||||
* \param total_sz The size of the buffer.
|
||||
* \return The cloned PDU.
|
||||
* \sa PDU::clone_packet
|
||||
*/
|
||||
PDU *clone_packet(uint8_t *ptr, uint32_t total_sz);
|
||||
private:
|
||||
struct arphdr {
|
||||
uint16_t ar_hrd; /* format of hardware address */
|
||||
@@ -197,6 +214,12 @@ namespace Tins {
|
||||
uint32_t ar_tip; /* target IP address */
|
||||
} __attribute__((__packed__));
|
||||
|
||||
/** \brief Creates an instance of ARP using an arphdr pointer.
|
||||
*
|
||||
* \param arp_ptr The pointer to the arphdr.
|
||||
*/
|
||||
ARP(arphdr *arp_ptr);
|
||||
|
||||
void write_serialization(uint8_t *buffer, uint32_t total_sz, const PDU *parent);
|
||||
|
||||
arphdr _arp;
|
||||
|
||||
@@ -41,12 +41,12 @@ namespace Tins {
|
||||
* Constructor that builds an ethernet PDU taking the destination's
|
||||
* and source's MAC.
|
||||
*
|
||||
* \param iface string containing the interface's name from where to send the packet.
|
||||
* \param mac_dst uint8_t array of 6 bytes containing the destination's MAC.
|
||||
* \param mac_src uint8_t array of 6 bytes containing the source's MAC.
|
||||
* \param iface string containing the interface's name from where to send the packet.
|
||||
* \param child PDU* with the PDU contained by the ethernet PDU (optional).
|
||||
*/
|
||||
EthernetII(const uint8_t* mac_dst, const uint8_t* mac_src, const std::string& iface, PDU* child = 0) throw (std::runtime_error);
|
||||
EthernetII(const std::string& iface, const uint8_t* mac_dst, const uint8_t* mac_src, PDU* child = 0) throw (std::runtime_error);
|
||||
|
||||
/**
|
||||
* \brief Constructor for creating an ethernet PDU
|
||||
@@ -59,7 +59,7 @@ namespace Tins {
|
||||
* \param iface_index uint32_t containing the interface's index from where to send the packet.
|
||||
* \param child PDU* with the PDU contained by the ethernet PDU (optional).
|
||||
*/
|
||||
EthernetII(const uint8_t* mac_dst, const uint8_t* mac_src, const uint32_t iface_index, PDU* child = 0);
|
||||
EthernetII(const uint32_t iface_index, const uint8_t* mac_dst = 0, const uint8_t* mac_src = 0, PDU* child = 0);
|
||||
|
||||
/* Getters */
|
||||
/**
|
||||
@@ -67,14 +67,14 @@ namespace Tins {
|
||||
*
|
||||
* \return Returns the destination's mac address as a constant uint8_t pointer.
|
||||
*/
|
||||
inline const uint8_t* dst_mac() const { return this->header.dst_mac; }
|
||||
inline const uint8_t* dst_mac() const { return _eth.dst_mac; }
|
||||
|
||||
/**
|
||||
* \brief Getter for the source's mac address.
|
||||
*
|
||||
* \return Returns the source's mac address as a constant uint8_t pointer.
|
||||
*/
|
||||
inline const uint8_t* src_mac() const { return this->header.src_mac; }
|
||||
inline const uint8_t* src_mac() const { return _eth.src_mac; }
|
||||
|
||||
/**
|
||||
* \brief Getter for the interface.
|
||||
@@ -125,20 +125,50 @@ namespace Tins {
|
||||
* \sa PDU::send()
|
||||
*/
|
||||
bool send(PacketSender* sender);
|
||||
|
||||
/** \brief Check wether ptr points to a valid response for this PDU.
|
||||
*
|
||||
* \sa PDU::matches_response
|
||||
* \param ptr The pointer to the buffer.
|
||||
* \param total_sz The size of the buffer.
|
||||
*/
|
||||
bool matches_response(uint8_t *ptr, uint32_t total_sz);
|
||||
|
||||
/** \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);
|
||||
|
||||
PDUType pdu_type() const { return PDU::ETHERNET_II; }
|
||||
|
||||
/** \brief Clones this pdu, filling the corresponding header with data
|
||||
* extracted from a buffer.
|
||||
*
|
||||
* \param ptr The pointer to the from from which the data will be extracted.
|
||||
* \param total_sz The size of the buffer.
|
||||
* \return The cloned PDU.
|
||||
* \sa PDU::clone_packet
|
||||
*/
|
||||
PDU *clone_packet(uint8_t *ptr, uint32_t total_sz);
|
||||
private:
|
||||
/**
|
||||
* Struct that represents the Ethernet II header
|
||||
*/
|
||||
struct ethernet_header {
|
||||
struct ethhdr {
|
||||
uint8_t dst_mac[6];
|
||||
uint8_t src_mac[6];
|
||||
uint16_t payload_type;
|
||||
} __attribute__((__packed__));
|
||||
|
||||
ethernet_header header;
|
||||
/** \brief Creates an instance of EthernetII using an ethhdr pointer.
|
||||
*
|
||||
* \param eth_ptr The pointer to the ethhdr.
|
||||
*/
|
||||
EthernetII(ethhdr *eth_ptr);
|
||||
|
||||
ethhdr _eth;
|
||||
uint32_t _iface_index;
|
||||
|
||||
void write_serialization(uint8_t *buffer, uint32_t total_sz, const PDU *parent);
|
||||
|
||||
@@ -71,6 +71,8 @@ namespace Tins {
|
||||
|
||||
PDU *send_recv(PDU *pdu);
|
||||
|
||||
PDU *recv_l2(PDU *pdu, struct sockaddr *link_addr, uint32_t len_link_addr);
|
||||
|
||||
bool send_l2(PDU *pdu, struct sockaddr* link_addr, uint32_t len_link_addr);
|
||||
|
||||
PDU *recv_l3(PDU *pdu, struct sockaddr *link_addr, uint32_t len_link_addr, SocketType type);
|
||||
|
||||
@@ -148,6 +148,13 @@ namespace Tins {
|
||||
*/
|
||||
virtual PDUType pdu_type() const = 0;
|
||||
|
||||
/** \brief Clones this pdu, filling the corresponding header with data
|
||||
* extracted from a buffer.
|
||||
*
|
||||
* \param ptr The pointer to the from from which the data will be extracted.
|
||||
* \param total_sz The size of the buffer.
|
||||
* \return The cloned PDU.
|
||||
*/
|
||||
virtual PDU *clone_packet(uint8_t *ptr, uint32_t total_sz) { return 0; }
|
||||
protected:
|
||||
/* Serialize this PDU storing the result in buffer. */
|
||||
|
||||
@@ -66,7 +66,7 @@ namespace Tins {
|
||||
* The input buffer must be at least 6 bytes long.
|
||||
* \param array The input buffer.
|
||||
*/
|
||||
std::string hwaddr_to_string(uint8_t *array);
|
||||
std::string hwaddr_to_string(const uint8_t *array);
|
||||
|
||||
/** \brief Resolves a domain name and returns its corresponding ip address.
|
||||
*
|
||||
|
||||
30
src/arp.cpp
30
src/arp.cpp
@@ -24,6 +24,7 @@
|
||||
#include <cassert>
|
||||
#include <netinet/in.h>
|
||||
#include "arp.h"
|
||||
#include "rawpdu.h"
|
||||
#include "utils.h"
|
||||
|
||||
|
||||
@@ -37,6 +38,10 @@ Tins::ARP::ARP() : PDU(0x0608) {
|
||||
_arp.ar_pln = 4;
|
||||
}
|
||||
|
||||
Tins::ARP::ARP(arphdr *arp_ptr) : PDU(0x0608) {
|
||||
memcpy(&_arp, arp_ptr, sizeof(arphdr));
|
||||
}
|
||||
|
||||
void Tins::ARP::sender_hw_addr(uint8_t* new_snd_hw_addr) {
|
||||
memcpy(this->_arp.ar_sha, new_snd_hw_addr, 6); //Should this use hardware address' length?
|
||||
}
|
||||
@@ -88,3 +93,28 @@ void Tins::ARP::write_serialization(uint8_t *buffer, uint32_t total_sz, const PD
|
||||
memcpy(buffer, &_arp, sizeof(arphdr));
|
||||
}
|
||||
|
||||
bool Tins::ARP::matches_response(uint8_t *ptr, uint32_t total_sz) {
|
||||
if(total_sz < sizeof(arphdr))
|
||||
return false;
|
||||
arphdr *arp_ptr = (arphdr*)ptr;
|
||||
return arp_ptr->ar_sip == _arp.ar_tip && arp_ptr->ar_tip == _arp.ar_sip;
|
||||
}
|
||||
|
||||
Tins::PDU *Tins::ARP::clone_packet(uint8_t *ptr, uint32_t total_sz) {
|
||||
if(total_sz < sizeof(arphdr))
|
||||
return 0;
|
||||
arphdr *arp_ptr = (arphdr*)ptr;
|
||||
PDU *child = 0, *cloned;
|
||||
if(total_sz > sizeof(arphdr)) {
|
||||
if(inner_pdu()) {
|
||||
child = inner_pdu()->clone_packet(ptr + sizeof(arphdr), total_sz - sizeof(arphdr));
|
||||
if(!child)
|
||||
return 0;
|
||||
}
|
||||
else
|
||||
child = new RawPDU(ptr + sizeof(arphdr), total_sz - sizeof(arphdr));
|
||||
}
|
||||
cloned = new ARP(arp_ptr);
|
||||
cloned->inner_pdu(child);
|
||||
return cloned;
|
||||
}
|
||||
|
||||
@@ -27,32 +27,41 @@
|
||||
#include <netpacket/packet.h>
|
||||
#include <netinet/in.h>
|
||||
#endif
|
||||
|
||||
#include "ethernetII.h"
|
||||
#include "rawpdu.h"
|
||||
#include "utils.h"
|
||||
|
||||
Tins::EthernetII::EthernetII(const uint8_t* mac_dst, const uint8_t* mac_src, const std::string& iface, PDU* child) throw (std::runtime_error) : PDU(ETHERTYPE_IP, child) {
|
||||
|
||||
this->dst_mac(mac_dst);
|
||||
this->src_mac(mac_src);
|
||||
Tins::EthernetII::EthernetII(const std::string& iface, const uint8_t* mac_dst, const uint8_t* mac_src, PDU* child) throw (std::runtime_error) : PDU(ETHERTYPE_IP, child) {
|
||||
memset(&_eth, 0, sizeof(ethhdr));
|
||||
if(mac_dst)
|
||||
this->dst_mac(mac_dst);
|
||||
if(mac_src)
|
||||
this->src_mac(mac_src);
|
||||
this->iface(iface);
|
||||
this->header.payload_type = 0;
|
||||
this->_eth.payload_type = 0;
|
||||
|
||||
}
|
||||
|
||||
Tins::EthernetII::EthernetII(const uint8_t* mac_dst, const uint8_t* mac_src, uint32_t iface_index, PDU* child) : PDU(ETHERTYPE_IP, child) {
|
||||
this->dst_mac(mac_dst);
|
||||
this->src_mac(mac_src);
|
||||
Tins::EthernetII::EthernetII(uint32_t iface_index, const uint8_t* mac_dst, const uint8_t* mac_src, PDU* child) : PDU(ETHERTYPE_IP, child) {
|
||||
memset(&_eth, 0, sizeof(ethhdr));
|
||||
if(mac_dst)
|
||||
this->dst_mac(mac_dst);
|
||||
if(mac_src)
|
||||
this->src_mac(mac_src);
|
||||
this->iface(iface_index);
|
||||
this->header.payload_type = 0;
|
||||
this->_eth.payload_type = 0;
|
||||
}
|
||||
|
||||
Tins::EthernetII::EthernetII(ethhdr *eth_ptr) : PDU(ETHERTYPE_IP) {
|
||||
memcpy(&_eth, eth_ptr, sizeof(ethhdr));
|
||||
}
|
||||
|
||||
void Tins::EthernetII::dst_mac(const uint8_t* new_dst_mac) {
|
||||
memcpy(this->header.dst_mac, new_dst_mac, 6);
|
||||
memcpy(this->_eth.dst_mac, new_dst_mac, 6);
|
||||
}
|
||||
|
||||
void Tins::EthernetII::src_mac(const uint8_t* new_src_mac) {
|
||||
memcpy(this->header.src_mac, new_src_mac, 6);
|
||||
memcpy(this->_eth.src_mac, new_src_mac, 6);
|
||||
}
|
||||
|
||||
void Tins::EthernetII::iface(uint32_t new_iface_index) {
|
||||
@@ -66,11 +75,10 @@ void Tins::EthernetII::iface(const std::string& new_iface) throw (std::runtime_e
|
||||
}
|
||||
|
||||
uint32_t Tins::EthernetII::header_size() const {
|
||||
return sizeof(ethernet_header);
|
||||
return sizeof(ethhdr);
|
||||
}
|
||||
|
||||
bool Tins::EthernetII::send(PacketSender* sender) {
|
||||
|
||||
struct sockaddr_ll addr;
|
||||
|
||||
memset(&addr, 0, sizeof(struct sockaddr_ll));
|
||||
@@ -79,10 +87,20 @@ bool Tins::EthernetII::send(PacketSender* sender) {
|
||||
addr.sll_protocol = Utils::net_to_host_s(ETH_P_ALL);
|
||||
addr.sll_halen = 6;
|
||||
addr.sll_ifindex = this->_iface_index;
|
||||
memcpy(&(addr.sll_addr), this->header.dst_mac, 6);
|
||||
memcpy(&(addr.sll_addr), this->_eth.dst_mac, 6);
|
||||
|
||||
return sender->send_l2(this, (struct sockaddr*)&addr, (uint32_t)sizeof(addr));
|
||||
}
|
||||
|
||||
bool Tins::EthernetII::matches_response(uint8_t *ptr, uint32_t total_sz) {
|
||||
if(total_sz < sizeof(ethhdr))
|
||||
return false;
|
||||
ethhdr *eth_ptr = (ethhdr*)ptr;
|
||||
if(!memcmp(eth_ptr->dst_mac, _eth.src_mac, 6)) {
|
||||
// chequear broadcast en destino original...
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
void Tins::EthernetII::write_serialization(uint8_t *buffer, uint32_t total_sz, const PDU *parent) {
|
||||
@@ -91,7 +109,7 @@ void Tins::EthernetII::write_serialization(uint8_t *buffer, uint32_t total_sz, c
|
||||
|
||||
/* Inner type defaults to IP */
|
||||
|
||||
if ((this->header.payload_type == 0) && this->inner_pdu()) {
|
||||
if ((this->_eth.payload_type == 0) && this->inner_pdu()) {
|
||||
uint16_t type = ETHERTYPE_IP;
|
||||
switch (this->inner_pdu()->pdu_type()) {
|
||||
case PDU::IP:
|
||||
@@ -103,10 +121,43 @@ void Tins::EthernetII::write_serialization(uint8_t *buffer, uint32_t total_sz, c
|
||||
default:
|
||||
type = 0;
|
||||
}
|
||||
this->header.payload_type = Utils::net_to_host_s(type);
|
||||
this->_eth.payload_type = Utils::net_to_host_s(type);
|
||||
}
|
||||
|
||||
|
||||
memcpy(buffer, &this->header, sizeof(ethernet_header));
|
||||
memcpy(buffer, &this->_eth, sizeof(ethhdr));
|
||||
|
||||
}
|
||||
|
||||
Tins::PDU *Tins::EthernetII::recv_response(PacketSender *sender) {
|
||||
struct sockaddr_ll addr;
|
||||
|
||||
memset(&addr, 0, sizeof(struct sockaddr_ll));
|
||||
|
||||
addr.sll_family = Utils::net_to_host_s(PF_PACKET);
|
||||
addr.sll_protocol = Utils::net_to_host_s(ETH_P_ALL);
|
||||
addr.sll_halen = 6;
|
||||
addr.sll_ifindex = this->_iface_index;
|
||||
memcpy(&(addr.sll_addr), this->_eth.dst_mac, 6);
|
||||
|
||||
return sender->recv_l2(this, (struct sockaddr*)&addr, (uint32_t)sizeof(addr));
|
||||
}
|
||||
|
||||
Tins::PDU *Tins::EthernetII::clone_packet(uint8_t *ptr, uint32_t total_sz) {
|
||||
if(total_sz < sizeof(ethhdr))
|
||||
return 0;
|
||||
ethhdr *eth_ptr = (ethhdr*)ptr;
|
||||
PDU *child = 0, *cloned;
|
||||
if(total_sz > sizeof(ethhdr)) {
|
||||
if(inner_pdu()) {
|
||||
child = inner_pdu()->clone_packet(ptr + sizeof(ethhdr), total_sz - sizeof(ethhdr));
|
||||
if(!child)
|
||||
return 0;
|
||||
}
|
||||
else
|
||||
child = new RawPDU(ptr + sizeof(ethhdr), total_sz - sizeof(ethhdr));
|
||||
}
|
||||
cloned = new EthernetII(eth_ptr);
|
||||
cloned->inner_pdu(child);
|
||||
return cloned;
|
||||
}
|
||||
|
||||
@@ -29,7 +29,6 @@
|
||||
#include <netdb.h>
|
||||
#endif
|
||||
#include <assert.h>
|
||||
#include <iostream> //borrar
|
||||
#include <errno.h>
|
||||
#include <string.h>
|
||||
#include "packetsender.h"
|
||||
@@ -103,10 +102,29 @@ bool Tins::PacketSender::send_l2(PDU *pdu, struct sockaddr* link_addr, uint32_t
|
||||
uint8_t *buffer = pdu->serialize(sz);
|
||||
bool ret_val = (sendto(sock, buffer, sz, 0, link_addr, len_link_addr) != -1);
|
||||
delete[] buffer;
|
||||
|
||||
|
||||
return ret_val;
|
||||
}
|
||||
|
||||
Tins::PDU *Tins::PacketSender::recv_l2(PDU *pdu, struct sockaddr *link_addr, uint32_t len_link_addr) {
|
||||
if(!open_l2_socket())
|
||||
return 0;
|
||||
uint8_t buffer[2048];
|
||||
int sock = _sockets[ETHER_SOCKET];
|
||||
bool done = false;
|
||||
socklen_t addrlen = len_link_addr;
|
||||
|
||||
while(!done) {
|
||||
ssize_t size = recvfrom(sock, buffer, 2048, 0, link_addr, &addrlen);
|
||||
if(size == -1)
|
||||
return 0;
|
||||
if(pdu->matches_response(buffer, size)) {
|
||||
return pdu->clone_packet(buffer, size);
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
Tins::PDU *Tins::PacketSender::recv_l3(PDU *pdu, struct sockaddr* link_addr, uint32_t len_link_addr, SocketType type) {
|
||||
if(!open_l3_socket(type))
|
||||
return 0;
|
||||
|
||||
@@ -135,7 +135,7 @@ bool Tins::Utils::hwaddr_to_byte(const std::string &hw_addr, uint8_t *array) {
|
||||
return true;
|
||||
}
|
||||
|
||||
string Tins::Utils::hwaddr_to_string(uint8_t *array) {
|
||||
string Tins::Utils::hwaddr_to_string(const uint8_t *array) {
|
||||
ostringstream oss;
|
||||
oss << hex;
|
||||
for(unsigned i(0); i < 6; ++i) {
|
||||
|
||||
Reference in New Issue
Block a user