
4 changed files with 92 additions and 0 deletions
@ -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); |
||||
} |
||||
|
||||
/** @} */ |
Loading…
Reference in new issue