1
0
mirror of https://github.com/mfontanini/libtins synced 2026-01-23 02:35:57 +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

@@ -566,6 +566,15 @@ namespace Tins {
*/
resources_type answers() const;
/**
* \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);
/**
* \sa PDU::clone
*/

View File

@@ -92,7 +92,11 @@ public:
* \return The stored id field value.
*/
small_uint<12> id() const {
#if TINS_IS_LITTLE_ENDIAN
return _header.idL | (_header.idH << 8);
#else
return _header.id;
#endif
}
/**
@@ -141,6 +145,15 @@ public:
* \param new_type The new type field value.
*/
void payload_type(uint16_t new_type);
/**
* \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);
private:
void write_serialization(uint8_t *buffer, uint32_t total_sz, const PDU *parent);
@@ -159,6 +172,8 @@ private:
#endif
};
static uint16_t get_id(const dot1q_hdr *hdr);
dot1q_hdr _header;
};
}

View File

@@ -160,7 +160,8 @@ namespace Tins {
void send(PacketSender &sender);
#endif // WIN32
/** \brief Check wether ptr points to a valid response for this PDU.
/**
* \brief Check wether ptr points to a valid response for this PDU.
*
* \sa PDU::matches_response
* \param ptr The pointer to the buffer.

View File

@@ -306,6 +306,14 @@ namespace Tins {
*/
const NetworkInterface &iface() const { return _iface; }
/** \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 Returns the RadioTap frame's header length.
*

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,12 +118,16 @@ 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))) {
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,11 +130,14 @@ 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...
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;