Merge pull request #2730 from authmillenon/inet_csum/feat/initial

inet_csum: initial import of Internet Checksum module
dev/timer
Martine Lenders 8 years ago
commit 687947d3db

@ -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

@ -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_ */
/** @} */

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

@ -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);
}
/** @} */

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

@ -0,0 +1,128 @@
/*
* Copyright (C) 2014 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 <errno.h>
#include <stdlib.h>
#include "embUnit.h"
#include "net/ng_inet_csum.h"
#include "unittests-constants.h"
#include "tests-inet_csum.h"
static void test_inet_csum__rfc_example(void)
{
/* source: https://tools.ietf.org/html/rfc1071#section-3 */
uint8_t data[] = {
0x00, 0x01, 0xf2, 0x03, 0xf4, 0xf5, 0xf6, 0xf7
};
TEST_ASSERT_EQUAL_INT(0xddf2, ng_inet_csum(0, data, sizeof(data)));
}
static void test_inet_csum__ipv6_pseudo_hdr(void)
{
/* source: https://www.cloudshark.org/captures/ea72fbab241b (No. 56) */
uint8_t data[] = {
0xfe, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* IPv6 source */
0x5a, 0x6d, 0x8f, 0xff, 0xfe, 0x56, 0x30, 0x09,
0xff, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* IPv6 destination */
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
0x00, 0x00, 0x00, 0x38, 0x00, 0x00, 0x00, 0x3a, /* payload length + next header */
0x86, 0x00, 0xab, 0x32, 0x40, 0x58, 0x07, 0x08, /* ICMPv6 payload */
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x03, 0x04, 0x40, 0xc0, 0x00, 0x00, 0x00, 0x1e,
0x00, 0x00, 0x00, 0x14, 0x00, 0x00, 0x00, 0x00,
0x20, 0x02, 0x18, 0x3d, 0xdb, 0xa4, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x01, 0x01, 0x58, 0x6d, 0x8f, 0x56, 0x30, 0x09
};
/* result unnormalized: take 1's-complement of 0 */
TEST_ASSERT_EQUAL_INT(0xffff, ng_inet_csum(0x0, data, sizeof(data)));
}
static void test_inet_csum__set_initial_sum(void)
{
/* source: https://www.cloudshark.org/captures/ea72fbab241b (No. 56) */
uint8_t data[] = {
0xfe, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* IPv6 source */
0x5a, 0x6d, 0x8f, 0xff, 0xfe, 0x56, 0x30, 0x09,
0xff, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* IPv6 destination */
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
0x86, 0x00, 0xab, 0x32, 0x40, 0x58, 0x07, 0x08, /* ICMPv6 payload */
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x03, 0x04, 0x40, 0xc0, 0x00, 0x00, 0x00, 0x1e,
0x00, 0x00, 0x00, 0x14, 0x00, 0x00, 0x00, 0x00,
0x20, 0x02, 0x18, 0x3d, 0xdb, 0xa4, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x01, 0x01, 0x58, 0x6d, 0x8f, 0x56, 0x30, 0x09
};
/* result unnormalized: take 1's-complement of 0
* set next header and payload length as initial value */
TEST_ASSERT_EQUAL_INT(0xffff, ng_inet_csum(0x38 + 0x3a, data, sizeof(data)));
}
static void test_inet_csum__calculate_csum(void)
{
/* source: http://en.wikipedia.org/w/index.php?title=IPv4_header_checksum&oldid=645516564
* but left checksum 0 */
uint8_t data[] = {
0x45, 0x00, 0x00, 0x73, 0x00, 0x00, 0x40, 0x00,
0x40, 0x11, 0x00, 0x00, 0xc0, 0xa8, 0x00, 0x01,
0xc0, 0xa8, 0x00, 0xc7,
};
/* result unnormalized: take 1's-complement of 0xb861 */
TEST_ASSERT_EQUAL_INT(0x479e, ng_inet_csum(0, data, sizeof(data)));
}
static void test_inet_csum__odd_len(void)
{
/* source: https://www.cloudshark.org/captures/ea72fbab241b (No. 1) */
uint8_t data[] = {
0xc0, 0xa8, 0x01, 0x91, 0x4b, 0x4b, 0x4b, 0x4b, /* IPv4 source + dest*/
0xf6, 0xfb, 0x00, 0x35, 0x00, 0x27, 0xd1, 0xa2, /* UDP header */
0xa5, 0x6f, 0x01, 0x00, 0x00, 0x01, 0x00, 0x00, /* DNS payload */
0x00, 0x00, 0x00, 0x00, 0x09, 0x74, 0x65, 0x73,
0x74, 0x2d, 0x69, 0x70, 0x76, 0x36, 0x03, 0x63,
0x6f, 0x6d, 0x00, 0x00, 0x01, 0x00, 0x01,
};
/* result unnormalized: take 1's-complement of 0
* set next header and payload length as initial value */
TEST_ASSERT_EQUAL_INT(0xffff, ng_inet_csum(17 + 39, data, sizeof(data)));
}
Test *tests_inet_csum_tests(void)
{
EMB_UNIT_TESTFIXTURES(fixtures) {
new_TestFixture(test_inet_csum__rfc_example),
new_TestFixture(test_inet_csum__ipv6_pseudo_hdr),
new_TestFixture(test_inet_csum__set_initial_sum),
new_TestFixture(test_inet_csum__calculate_csum),
new_TestFixture(test_inet_csum__odd_len),
};
EMB_UNIT_TESTCALLER(inet_csum_tests, NULL, NULL, fixtures);
return (Test *)&inet_csum_tests;
}
void tests_inet_csum(void)
{
TESTS_RUN(tests_inet_csum_tests());
}
/** @} */

@ -0,0 +1,37 @@
/*
* Copyright (C) 2015 Martin Lenders
*
* 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.
*/
/**
* @addtogroup unittests
* @{
*
* @file tests-inet_csum.h
* @brief Unittests for the ``inet_csum`` module
*
* @author Martine Lenders <mlenders@inf.fu-berlin.de>
*/
#ifndef TESTS_INET_CSUM_H_
#define TESTS_INET_CSUM_H_
#include "embUnit.h"
#ifdef __cplusplus
extern "C" {
#endif
/**
* @brief The entry point of this test suite.
*/
void tests_inet_csum(void);
#ifdef __cplusplus
}
#endif
#endif /* TESTS_INET_CSUM_H_ */
/** @} */
Loading…
Cancel
Save