2007-11-07 00:38:14 +01:00
|
|
|
/****************************************************************************
|
2014-06-27 23:42:21 +02:00
|
|
|
* net/utils/net_chksum.c
|
2007-11-07 00:38:14 +01:00
|
|
|
*
|
2019-03-11 19:48:17 +01:00
|
|
|
* Copyright (C) 2007-2010, 2012, 2014-2015 Gregory Nutt. All rights
|
|
|
|
* reserved.
|
2012-02-18 23:09:09 +01:00
|
|
|
* Author: Gregory Nutt <gnutt@nuttx.org>
|
2007-11-07 00:38:14 +01:00
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
*
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in
|
|
|
|
* the documentation and/or other materials provided with the
|
|
|
|
* distribution.
|
|
|
|
* 3. Neither the name NuttX nor the names of its contributors may be
|
|
|
|
* used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
|
|
|
|
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
|
|
|
|
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
|
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
|
|
|
|
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
|
|
|
|
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
|
|
|
|
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
|
|
|
|
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
* POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
#ifdef CONFIG_NET
|
|
|
|
|
2014-06-27 23:42:21 +02:00
|
|
|
#include "utils/utils.h"
|
2007-11-07 00:38:14 +01:00
|
|
|
|
|
|
|
/****************************************************************************
|
2017-04-02 22:30:01 +02:00
|
|
|
* Public Functions
|
2007-11-07 00:38:14 +01:00
|
|
|
****************************************************************************/
|
|
|
|
|
2014-06-28 01:51:32 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: chksum
|
2017-04-02 22:30:01 +02:00
|
|
|
*
|
|
|
|
* Description:
|
2021-01-15 10:01:00 +01:00
|
|
|
* Calculate the raw change sum over the memory region described by
|
2017-04-02 22:30:01 +02:00
|
|
|
* data and len.
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
2019-03-11 19:48:17 +01:00
|
|
|
* sum - Partial calculations carried over from a previous call to
|
|
|
|
* chksum(). This should be zero on the first time that check
|
|
|
|
* sum is called.
|
2017-04-02 22:30:01 +02:00
|
|
|
* data - Beginning of the data to include in the checksum.
|
|
|
|
* len - Length of the data to include in the checksum.
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* The updated checksum value.
|
|
|
|
*
|
2014-06-28 01:51:32 +02:00
|
|
|
****************************************************************************/
|
|
|
|
|
2015-09-03 03:48:31 +02:00
|
|
|
#ifndef CONFIG_NET_ARCH_CHKSUM
|
2017-04-02 22:30:01 +02:00
|
|
|
uint16_t chksum(uint16_t sum, FAR const uint8_t *data, uint16_t len)
|
2007-11-07 00:38:14 +01:00
|
|
|
{
|
2015-01-20 18:06:51 +01:00
|
|
|
FAR const uint8_t *dataptr;
|
|
|
|
FAR const uint8_t *last_byte;
|
2009-12-15 15:53:45 +01:00
|
|
|
uint16_t t;
|
2007-11-07 00:38:14 +01:00
|
|
|
|
|
|
|
dataptr = data;
|
|
|
|
last_byte = data + len - 1;
|
|
|
|
|
2014-04-12 21:09:48 +02:00
|
|
|
while (dataptr < last_byte)
|
2007-11-07 00:38:14 +01:00
|
|
|
{
|
|
|
|
/* At least two more bytes */
|
|
|
|
|
2015-01-20 18:06:51 +01:00
|
|
|
t = ((uint16_t)dataptr[0] << 8) + dataptr[1];
|
2007-11-07 00:38:14 +01:00
|
|
|
sum += t;
|
|
|
|
if (sum < t)
|
|
|
|
{
|
|
|
|
sum++; /* carry */
|
|
|
|
}
|
2014-04-12 20:13:01 +02:00
|
|
|
|
2007-11-07 00:38:14 +01:00
|
|
|
dataptr += 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (dataptr == last_byte)
|
|
|
|
{
|
|
|
|
t = (dataptr[0] << 8) + 0;
|
|
|
|
sum += t;
|
|
|
|
if (sum < t)
|
|
|
|
{
|
|
|
|
sum++; /* carry */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Return sum in host byte order. */
|
|
|
|
|
|
|
|
return sum;
|
|
|
|
}
|
2014-06-28 01:51:32 +02:00
|
|
|
#endif /* CONFIG_NET_ARCH_CHKSUM */
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: net_chksum
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Calculate the Internet checksum over a buffer.
|
|
|
|
*
|
|
|
|
* The Internet checksum is the one's complement of the one's complement
|
|
|
|
* sum of all 16-bit words in the buffer.
|
|
|
|
*
|
|
|
|
* See RFC1071.
|
|
|
|
*
|
|
|
|
* If CONFIG_NET_ARCH_CHKSUM is defined, then this function must be
|
|
|
|
* provided by architecture-specific logic.
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
*
|
|
|
|
* buf - A pointer to the buffer over which the checksum is to be computed.
|
|
|
|
*
|
2021-01-15 10:01:00 +01:00
|
|
|
* len - The length of the buffer over which the checksum is to be
|
|
|
|
* computed.
|
2014-06-28 01:51:32 +02:00
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* The Internet checksum of the buffer.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2015-09-03 03:48:31 +02:00
|
|
|
#ifndef CONFIG_NET_ARCH_CHKSUM
|
2014-06-28 01:51:32 +02:00
|
|
|
uint16_t net_chksum(FAR uint16_t *data, uint16_t len)
|
2007-11-07 00:38:14 +01:00
|
|
|
{
|
2009-12-15 15:53:45 +01:00
|
|
|
return htons(chksum(0, (uint8_t *)data, len));
|
2007-11-07 00:38:14 +01:00
|
|
|
}
|
2014-06-28 01:51:32 +02:00
|
|
|
#endif /* CONFIG_NET_ARCH_CHKSUM */
|
2007-11-07 00:38:14 +01:00
|
|
|
|
|
|
|
#endif /* CONFIG_NET */
|