1
0
mirror of https://github.com/mfontanini/libtins synced 2026-01-28 12:44:25 +01:00

Fixed issue related with EthernetII protocol.

The Ethernet II protocol forces a minimum length of 60 bytes. It is necessary to add a trailer for padding of null-bytes when the inner_pdu's size does not meet the requirement.

Also EthernetII packets received were being incorrectly parsed due to the existance of this padding.

The issue has been solved and several tests were added. All tests successfully pass.
This commit is contained in:
Santiago Alessandri
2013-07-12 20:06:55 -03:00
parent 3b349471ea
commit 02d3a14083
8 changed files with 76 additions and 3 deletions

View File

@@ -82,6 +82,7 @@ EthernetII::EthernetII(const uint8_t *buffer, uint32_t total_sz)
)
);
}
}
void EthernetII::dst_addr(const address_type &new_dst_addr) {
@@ -97,9 +98,19 @@ void EthernetII::payload_type(uint16_t new_payload_type) {
}
uint32_t EthernetII::header_size() const {
return sizeof(ethhdr);
}
uint32_t EthernetII::trailer_size() const {
int32_t padding = 60 - sizeof(ethhdr); // EthernetII min size is 60, padding is sometimes needed
if (inner_pdu()) {
padding -= inner_pdu()->size();
padding = std::max(0, padding);
}
return padding;
}
#ifndef WIN32
void EthernetII::send(PacketSender &sender, const NetworkInterface &iface) {
if(!iface)
@@ -140,7 +151,7 @@ bool EthernetII::matches_response(const uint8_t *ptr, uint32_t total_sz) const {
void EthernetII::write_serialization(uint8_t *buffer, uint32_t total_sz, const PDU *parent) {
#ifdef TINS_DEBUG
assert(total_sz >= header_size());
assert(total_sz >= header_size() + trailer_size());
#endif
/* Inner type defaults to IP */
@@ -151,6 +162,14 @@ void EthernetII::write_serialization(uint8_t *buffer, uint32_t total_sz, const P
payload_type(static_cast<uint16_t>(flag));
}
memcpy(buffer, &_eth, sizeof(ethhdr));
uint32_t trailer = trailer_size();
if (trailer) {
uint32_t trailer_offset = header_size();
if (inner_pdu())
trailer_offset += inner_pdu()->size();
memset(buffer + trailer_offset, 0, trailer);
}
}
#ifndef WIN32