1
0
mirror of https://github.com/mfontanini/libtins synced 2026-01-27 20:24:26 +01:00

DNS, RadioTap and Dot1Q work when using PacketSender::send_recv.

This commit is contained in:
Matias Fontanini
2013-03-26 14:54:24 -03:00
parent 1cec099c0e
commit dfc0498b70
9 changed files with 95 additions and 9 deletions

View File

@@ -476,4 +476,11 @@ DNS::resources_type DNS::answers() const {
convert_resources(ans, res);
return res;
}
bool DNS::matches_response(uint8_t *ptr, uint32_t total_sz) {
if(total_sz < sizeof(dnshdr))
return false;
const dnshdr *hdr = (const dnshdr*)ptr;
return hdr->id == dns.id;
}
}

View File

@@ -68,8 +68,12 @@ void Dot1Q::cfi(small_uint<1> new_cfi) {
}
void Dot1Q::id(small_uint<12> new_id) {
#if TINS_IS_LITTLE_ENDIAN
_header.idL = new_id & 0xff;
_header.idH = new_id >> 8;
#else
_header.id = new_id;
#endif
}
void Dot1Q::payload_type(uint16_t new_type) {
@@ -103,4 +107,27 @@ void Dot1Q::write_serialization(uint8_t *buffer, uint32_t total_sz, const PDU *)
buffer += sizeof(_header) + inner_pdu()->size();
std::fill(buffer, buffer + trailer, 0);
}
#if TINS_IS_LITTLE_ENDIAN
uint16_t Dot1Q::get_id(const dot1q_hdr *hdr) {
return hdr->idL | (hdr->idH << 8);
}
#else
uint16_t Dot1Q::get_id(const dot1q_hdr *hdr) {
return hdr->id;
}
#endif
bool Dot1Q::matches_response(uint8_t *ptr, uint32_t total_sz) {
if(total_sz < sizeof(_header))
return false;
const dot1q_hdr *dot1q_ptr = (const dot1q_hdr*)ptr;
if(get_id(dot1q_ptr) == get_id(&_header)) {
ptr += sizeof(_header);
total_sz -= sizeof(_header);
return inner_pdu() ? inner_pdu()->matches_response(ptr, total_sz) : true;
}
return false;
}
}

View File

@@ -118,11 +118,15 @@ void Dot3::send(PacketSender &sender) {
bool Dot3::matches_response(uint8_t *ptr, uint32_t total_sz) {
if(total_sz < sizeof(ethhdr))
return false;
const size_t addr_sz = address_type::address_size;
const ethhdr *eth_ptr = (const ethhdr*)ptr;
if(!memcmp(eth_ptr->dst_mac, _eth.src_mac, sizeof(_eth.src_mac))) {
ptr += sizeof(ethhdr);
total_sz -= sizeof(ethhdr);
return inner_pdu() ? inner_pdu()->matches_response(ptr, total_sz) : true;
if(std::equal(_eth.src_mac, _eth.src_mac + addr_sz, eth_ptr->dst_mac)) {
if(std::equal(_eth.src_mac, _eth.src_mac + addr_sz, eth_ptr->dst_mac) || dst_addr() == BROADCAST)
{
ptr += sizeof(ethhdr);
total_sz -= sizeof(ethhdr);
return inner_pdu() ? inner_pdu()->matches_response(ptr, total_sz) : true;
}
}
return false;
}

View File

@@ -130,10 +130,13 @@ void EthernetII::send(PacketSender &sender) {
bool EthernetII::matches_response(uint8_t *ptr, uint32_t total_sz) {
if(total_sz < sizeof(ethhdr))
return false;
const size_t addr_sz = address_type::address_size;
const ethhdr *eth_ptr = (const ethhdr*)ptr;
if(!memcmp(eth_ptr->dst_mac, _eth.src_mac, address_type::address_size)) {
// chequear broadcast en destino original...
return (inner_pdu()) ? inner_pdu()->matches_response(ptr + sizeof(_eth), total_sz - sizeof(_eth)) : true;
if(std::equal(_eth.src_mac, _eth.src_mac + addr_sz, eth_ptr->dst_mac)) {
if(std::equal(_eth.src_mac, _eth.src_mac + addr_sz, eth_ptr->dst_mac) || dst_addr() == BROADCAST)
{
return (inner_pdu()) ? inner_pdu()->matches_response(ptr + sizeof(_eth), total_sz - sizeof(_eth)) : true;
}
}
return false;
}

View File

@@ -259,6 +259,18 @@ void RadioTap::send(PacketSender &sender) {
}
#endif
bool RadioTap::matches_response(uint8_t *ptr, uint32_t total_sz) {
if(sizeof(_radio) < total_sz)
return false;
const radiotap_hdr *radio_ptr = (const radiotap_hdr*)ptr;
if(radio_ptr->it_len <= total_sz) {
ptr += radio_ptr->it_len;
total_sz -= radio_ptr->it_len;
return inner_pdu() ? inner_pdu()->matches_response(ptr, total_sz) : true;
}
return false;
}
void RadioTap::write_serialization(uint8_t *buffer, uint32_t total_sz, const PDU *parent) {
uint32_t sz = header_size();
uint8_t *buffer_start = buffer;