1
0
mirror of https://github.com/RIOT-OS/RIOT.git synced 2024-12-29 04:50:03 +01:00

net_help: prefix csum properly and add documentation

This commit is contained in:
Martine Lenders 2014-11-17 09:38:06 +01:00
parent 4d82c2c646
commit c9219b87d6
5 changed files with 21 additions and 8 deletions

View File

@ -19,7 +19,7 @@
#include "net_help.h"
uint16_t csum(uint16_t sum, uint8_t *buf, uint16_t len)
uint16_t net_help_csum(uint16_t sum, uint8_t *buf, uint16_t len)
{
int count = len >> 1;

View File

@ -65,7 +65,20 @@ static inline uint64_t NTOHLL(uint64_t a)
return byteorder_ntohll(*(network_uint64_t *) &a);
}
uint16_t csum(uint16_t sum, uint8_t *buf, uint16_t len);
/**
* @brief Computes the Internet Checksum for *buf* with initial value *init*
*
* @see <a href="https://tools.ietf.org/html/rfc1071">
* RFC 1071
* </a>
*
* @param[in] init Initial value for the checksum (0 in most cases)
* @param[in] buf A byte array to calculate the checksum of
* @param[in] buf_len Length of *buf*
*
* @return The Internet Checksum of *buf* with initial value *init*
*/
uint16_t net_help_csum(uint16_t init, uint8_t *buf, uint16_t buf_len);
#ifdef __cplusplus
}

View File

@ -1437,8 +1437,8 @@ uint16_t icmpv6_csum(ipv6_hdr_t *ipv6_buf, icmpv6_hdr_t *icmpv6_buf)
icmpv6_buf->checksum = 0;
sum = len + IPV6_PROTO_NUM_ICMPV6;
sum = csum(sum, (uint8_t *)&ipv6_buf->srcaddr, 2 * sizeof(ipv6_addr_t));
sum = csum(sum, (uint8_t *)icmpv6_buf, len);
sum = net_help_csum(sum, (uint8_t *)&ipv6_buf->srcaddr, 2 * sizeof(ipv6_addr_t));
sum = net_help_csum(sum, (uint8_t *)icmpv6_buf, len);
return (sum == 0) ? 0 : ~HTONS(sum);
}

View File

@ -820,7 +820,7 @@ uint16_t ipv6_csum(ipv6_hdr_t *ipv6_header, uint8_t *buf, uint16_t len, uint8_t
&ipv6_header->destaddr),
len, buf, proto);
sum = len + proto;
sum = csum(sum, (uint8_t *)&ipv6_header->srcaddr, 2 * sizeof(ipv6_addr_t));
sum = csum(sum, buf, len);
sum = net_help_csum(sum, (uint8_t *)&ipv6_header->srcaddr, 2 * sizeof(ipv6_addr_t));
sum = net_help_csum(sum, buf, len);
return (sum == 0) ? 0xffff : HTONS(sum);
}

View File

@ -43,8 +43,8 @@ uint16_t udp_csum(ipv6_hdr_t *ipv6_header, udp_hdr_t *udp_header)
uint16_t len = NTOHS(udp_header->length);
sum = len + IPPROTO_UDP;
sum = csum(sum, (uint8_t *)&ipv6_header->srcaddr, 2 * sizeof(ipv6_addr_t));
sum = csum(sum, (uint8_t *)udp_header, len);
sum = net_help_csum(sum, (uint8_t *)&ipv6_header->srcaddr, 2 * sizeof(ipv6_addr_t));
sum = net_help_csum(sum, (uint8_t *)udp_header, len);
return (sum == 0) ? 0xffff : HTONS(sum);
}