mirror of
https://github.com/mfontanini/libtins
synced 2026-01-27 04:11:35 +01:00
Added PacketSender::recv_l2. Ethernet packets can now expect a response packet. ARP response is working now.
This commit is contained in:
@@ -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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user