Browse Source

inet_csum: initial import of Internet Checksum module

dev/timer
Martine Lenders 8 years ago
parent
commit
903a9d54ce
  1. 3
      sys/Makefile
  2. 52
      sys/include/net/ng_inet_csum.h
  3. 1
      sys/net/crosslayer/ng_inet_csum/Makefile
  4. 36
      sys/net/crosslayer/ng_inet_csum/ng_inet_csum.c

3
sys/Makefile

@ -71,6 +71,9 @@ endif
ifneq (,$(filter ng_ipv6_netif,$(USEMODULE)))
DIRS += net/network_layer/ng_ipv6/netif
endif
ifneq (,$(filter ng_inet_csum,$(USEMODULE)))
DIRS += net/crosslayer/ng_inet_csum
endif
ifneq (,$(filter ng_netapi,$(USEMODULE)))
DIRS += net/crosslayer/ng_netapi
endif

52
sys/include/net/ng_inet_csum.h

@ -0,0 +1,52 @@
/*
* Copyright (C) 2015 Martine Lenders <mlenders@inf.fu-berlin.de>
*
* This file is subject to the terms and conditions of the GNU Lesser
* General Public License v2.1. See the file LICENSE in the top level
* directory for more details.
*/
/**
* @defgroup net_ng_inet_csum Internet Checksum
* @ingroup net
* @brief Provides a function to calculate the Internet Checksum
* @{
*
* @file
* @brief Internet Checksum definitions
*
* @author Martine Lenders <mlenders@inf.fu-berlin.de>
*/
#ifndef NG_INET_CSUM_H_
#define NG_INET_CSUM_H_
#include <inttypes.h>
#ifdef __cplusplus
extern "C" {
#endif
/**
* @brief Calculates the unnormalized Internet Checksum of @p buf.
*
* @see <a href="https://tools.ietf.org/html/rfc1071">
* RFC 1071
* </a>
*
* @details The Internet Checksum is not normalized (i. e. its 1's complement
* was not taken of the result) to use it for further calculation.
*
* @param[in] sum An initial value for the checksum.
* @param[in] buf A buffer.
* @param[in] len Length of @p buf in byte.
*
* @return The unnormalized Internet Checksum of @p buf.
*/
uint16_t ng_inet_csum(uint16_t sum, const uint8_t *buf, uint16_t len);
#ifdef __cplusplus
}
#endif
#endif /* NG_INET_CSUM_H_ */
/** @} */

1
sys/net/crosslayer/ng_inet_csum/Makefile

@ -0,0 +1 @@
include $(RIOTBASE)/Makefile.base

36
sys/net/crosslayer/ng_inet_csum/ng_inet_csum.c

@ -0,0 +1,36 @@
/*
* Copyright (C) 2015 Martine Lenders <mlenders@inf.fu-berlin.de>
*
* This file is subject to the terms and conditions of the GNU Lesser
* General Public License v2.1. See the file LICENSE in the top level
* directory for more details.
*/
/**
* @{
*
* @file
*/
#include <stdio.h>
#include "net/ng_inet_csum.h"
uint16_t ng_inet_csum(uint16_t sum, const uint8_t *buf, uint16_t len)
{
uint32_t csum = sum;
for (int i = 0; i < (len >> 1); buf += 2, i++) {
csum += (*buf << 8) + *(buf + 1); /* group bytes by 16-byte words
* and add them*/
}
if (len & 1) { /* if len is odd */
csum += (*buf << 8); /* add last byte as top half of 16-byte word */
}
csum += csum >> 16;
return (csum & 0xffff);
}
/** @} */
Loading…
Cancel
Save