chunk: Add function to calculate Internet Checksums according to RFC 1071

This commit is contained in:
Tobias Brunner
2014-07-22 11:10:35 +02:00
parent a10eb93566
commit b557f4a7cd
3 changed files with 105 additions and 0 deletions
+31
View File
@@ -987,6 +987,37 @@ u_int32_t chunk_hash_static(chunk_t chunk)
return chunk_mac(chunk, static_key);
}
/**
* Described in header.
*/
u_int16_t chunk_internet_checksum_inc(chunk_t data, u_int16_t checksum)
{
u_int32_t sum = ntohs(~checksum);
while (data.len > 1)
{
sum += untoh16(data.ptr);
data = chunk_skip(data, 2);
}
if (data.len)
{
sum += (u_int16_t)*data.ptr << 8;
}
while (sum >> 16)
{
sum = (sum & 0xffff) + (sum >> 16);
}
return htons(~sum);
}
/**
* Described in header.
*/
u_int16_t chunk_internet_checksum(chunk_t data)
{
return chunk_internet_checksum_inc(data, 0xffff);
}
/**
* Described in header.
*/
+25
View File
@@ -411,6 +411,31 @@ u_int32_t chunk_hash_static_inc(chunk_t chunk, u_int32_t hash);
*/
u_int64_t chunk_mac(chunk_t chunk, u_char *key);
/**
* Calculate the Internet Checksum according to RFC 1071 for the given chunk.
*
* If the result is used with chunk_internet_checksum_inc() and the data length
* is not a multiple of 16 bit the checksum bytes have to be swapped to
* compensate the even/odd alignment.
*
* @param chunk data to process
* @return checksum (one's complement, network order)
*/
u_int16_t chunk_internet_checksum(chunk_t data);
/**
* Extend the given Internet Checksum (one's complement, in network byte order)
* with the given data.
*
* If data is not a multiple of 16 bits the checksum may have to be swapped to
* compensate even/odd alignment (see chunk_internet_checksum()).
*
* @param chunk data to process
* @param checksum previous checksum (one's complement, network order)
* @return checksum (one's complement, network order)
*/
u_int16_t chunk_internet_checksum_inc(chunk_t data, u_int16_t checksum);
/**
* printf hook function for chunk_t.
*