1
0
mirror of https://github.com/mfontanini/libtins synced 2026-01-23 02:35:57 +01:00

Add ICMP extensions to ICMP PDU

The length field is still not being set
This commit is contained in:
Matias Fontanini
2015-12-17 20:42:57 -08:00
parent e3c382efa0
commit 0a16d8f462
5 changed files with 88 additions and 7 deletions

View File

@@ -140,9 +140,21 @@ uint32_t ICMP::header_size() const {
extra = sizeof(uint32_t) * 3;
else if(type() == ADDRESS_MASK_REQUEST || type() == ADDRESS_MASK_REPLY)
extra = sizeof(uint32_t);
return sizeof(icmphdr) + extra;
}
uint32_t ICMP::trailer_size() const {
uint32_t output = 0;
if (has_extensions()) {
output += extensions_.size();
if (inner_pdu()) {
output += 128 - std::min(inner_pdu()->size(), 128U);
}
}
return output;
}
void ICMP::set_echo_request(uint16_t id, uint16_t seq) {
type(ECHO_REQUEST);
this->id(id);
@@ -216,6 +228,20 @@ void ICMP::write_serialization(uint8_t *buffer, uint32_t total_sz, const PDU *)
uint32_t_buffer = address_mask();
memcpy(buffer + sizeof(icmphdr), &uint32_t_buffer, sizeof(uint32_t));
}
if (has_extensions()) {
uint8_t* extensions_ptr = buffer + sizeof(icmphdr);
if (inner_pdu()) {
uint32_t inner_pdu_size = inner_pdu()->size();
if (inner_pdu_size < 128) {
memset(buffer + sizeof(icmphdr) + inner_pdu_size, 0, 128 - inner_pdu_size);
inner_pdu_size = 128;
}
extensions_ptr += inner_pdu_size;
}
extensions_.serialize(extensions_ptr, total_sz - (extensions_ptr - buffer));
}
// checksum calc
_icmp.check = 0;
memcpy(buffer, &_icmp, sizeof(icmphdr));

View File

@@ -76,8 +76,8 @@ ICMPExtension::serialization_type ICMPExtension::serialize() const {
const uint32_t ICMPExtensionsStructure::BASE_HEADER_SIZE = sizeof(uint16_t) * 2;
ICMPExtensionsStructure::ICMPExtensionsStructure()
: version_and_reserved_(0x2000), checksum_(0) {
: version_and_reserved_(), checksum_(0) {
version(2);
}
ICMPExtensionsStructure::ICMPExtensionsStructure(const uint8_t* buffer, uint32_t total_sz) {
@@ -135,6 +135,10 @@ uint32_t ICMPExtensionsStructure::size() const {
return output;
}
void ICMPExtensionsStructure::add_extension(const ICMPExtension& extension) {
extensions_.push_back(extension);
}
void ICMPExtensionsStructure::serialize(uint8_t* buffer, uint32_t buffer_size) {
const uint32_t structure_size = size();
if (buffer_size < structure_size) {