mirror of
https://github.com/mfontanini/libtins
synced 2026-01-23 02:35:57 +01:00
Fixed ICMPv6 checksum calculation.
This commit is contained in:
@@ -198,6 +198,7 @@ void ICMPv6::write_serialization(uint8_t *buffer, uint32_t total_sz, const PDU *
|
||||
assert(total_sz >= header_size());
|
||||
#endif
|
||||
icmp6hdr* ptr_header = (icmp6hdr*)buffer;
|
||||
_header.cksum = 0;
|
||||
std::memcpy(buffer, &_header, sizeof(_header));
|
||||
buffer += sizeof(_header);
|
||||
total_sz -= sizeof(_header);
|
||||
@@ -224,19 +225,18 @@ void ICMPv6::write_serialization(uint8_t *buffer, uint32_t total_sz, const PDU *
|
||||
#endif
|
||||
buffer = write_option(*it, buffer);
|
||||
}
|
||||
if(!_header.cksum) {
|
||||
const Tins::IPv6 *ipv6 = tins_cast<const Tins::IPv6*>(parent);
|
||||
if(ipv6) {
|
||||
uint32_t checksum = Utils::pseudoheader_checksum(
|
||||
ipv6->src_addr(),
|
||||
ipv6->dst_addr(),
|
||||
size(),
|
||||
Constants::IP::PROTO_ICMPV6
|
||||
) + Utils::do_checksum((uint8_t*)ptr_header, buffer);
|
||||
while (checksum >> 16)
|
||||
checksum = (checksum & 0xffff) + (checksum >> 16);
|
||||
ptr_header->cksum = Endian::host_to_be<uint16_t>(~checksum);
|
||||
}
|
||||
const Tins::IPv6 *ipv6 = tins_cast<const Tins::IPv6*>(parent);
|
||||
if(ipv6) {
|
||||
uint32_t checksum = Utils::pseudoheader_checksum(
|
||||
ipv6->src_addr(),
|
||||
ipv6->dst_addr(),
|
||||
size(),
|
||||
Constants::IP::PROTO_ICMPV6
|
||||
) + Utils::do_checksum((uint8_t*)ptr_header, buffer);
|
||||
while (checksum >> 16)
|
||||
checksum = (checksum & 0xffff) + (checksum >> 16);
|
||||
this->checksum(~checksum);
|
||||
ptr_header->cksum = _header.cksum;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -184,12 +184,16 @@ uint32_t pseudoheader_checksum(IPv4Address source_ip, IPv4Address dest_ip, uint3
|
||||
}
|
||||
|
||||
uint32_t pseudoheader_checksum(IPv6Address source_ip, IPv6Address dest_ip, uint32_t len, uint32_t flag) {
|
||||
uint32_t checksum = 0;
|
||||
IPv6Address::const_iterator it;
|
||||
for(it = source_ip.begin(); it != source_ip.end(); ++it)
|
||||
checksum += *it;
|
||||
for(it = dest_ip.begin(); it != dest_ip.end(); ++it)
|
||||
checksum += *it;
|
||||
uint32_t checksum(0);
|
||||
uint16_t *ptr = (uint16_t*) source_ip.begin();
|
||||
uint16_t *end = (uint16_t*) source_ip.end();
|
||||
while(ptr < end)
|
||||
checksum += (uint32_t) Endian::be_to_host(*ptr++);
|
||||
|
||||
ptr = (uint16_t*) dest_ip.begin();
|
||||
end = (uint16_t*) dest_ip.end();
|
||||
while(ptr < end)
|
||||
checksum += (uint32_t) Endian::be_to_host(*ptr++);
|
||||
checksum += flag + len;
|
||||
return checksum;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user