📄 net_util.c
字号:
/*
*********************************************************************************************************
* uC/TCP-IP
* The Embedded TCP/IP Suite
*
* (c) Copyright 2003-2006; Micrium, Inc.; Weston, FL
*
* All rights reserved. Protected by international copyright laws.
*
* uC/TCP-IP is provided in source form for FREE evaluation, for educational
* use or peaceful research. If you plan on using uC/TCP-IP in a commercial
* product you need to contact Micrium to properly license its use in your
* product. We provide ALL the source code for your convenience and to help
* you experience uC/TCP-IP. The fact that the source code is provided does
* NOT mean that you can use it without paying a licensing fee.
*
* Knowledge of the source code may NOT be used to develop a similar product.
*
* Please help us continue to provide the Embedded community with the finest
* software available. Your honesty is greatly appreciated.
*********************************************************************************************************
*/
/*
*********************************************************************************************************
*
* NETWORK UTILITY LIBRARY
*
* Filename : net_util.c
* Version : V1.87
* Programmer(s) : ITJ
*********************************************************************************************************
* Note(s) : (1) NO compiler-supplied standard library functions are used by the network protocol suite.
* 'net_util.*' implements ALL network-specific library functions.
*
* See also 'net.h NETWORK INCLUDE FILES Note #3'.
*********************************************************************************************************
*/
/*
*********************************************************************************************************
* INCLUDE FILES
*********************************************************************************************************
*/
#define NET_UTIL_MODULE
#include <net.h>
/*$PAGE*/
/*
*********************************************************************************************************
* LOCAL DEFINES
*********************************************************************************************************
*/
#define NET_UTIL_16_BIT_ONES_CPL_NEG_ZERO 0xFFFF
#define NET_UTIL_16_BIT_SUM_ERR_NONE DEF_BIT_NONE
#define NET_UTIL_16_BIT_SUM_ERR_NULL_SIZE DEF_BIT_01
#define NET_UTIL_16_BIT_SUM_ERR_LAST_OCTET DEF_BIT_02
/*
*********************************************************************************************************
* LOCAL CONSTANTS
*********************************************************************************************************
*/
/*
*********************************************************************************************************
* LOCAL DATA TYPES
*********************************************************************************************************
*/
/*
*********************************************************************************************************
* LOCAL TABLES
*********************************************************************************************************
*/
/*
*********************************************************************************************************
* LOCAL GLOBAL VARIABLES
*********************************************************************************************************
*/
/*$PAGE*/
/*
*********************************************************************************************************
* LOCAL FUNCTION PROTOTYPES
*********************************************************************************************************
*/
static CPU_INT32U NetUtil_16BitSumHdrCalc (void *phdr,
CPU_INT16U hdr_size);
static CPU_INT32U NetUtil_16BitSumDataCalc (void *p_data,
CPU_INT16U data_size,
CPU_INT08U *poctet_prev,
CPU_INT08U *poctet_last,
CPU_BOOLEAN prev_octet_valid,
CPU_BOOLEAN last_pkt_buf,
CPU_INT08U *psum_err);
static CPU_INT16U NetUtil_16BitOnesCplSumDataCalc(void *pdata_buf,
void *ppseudo_hdr,
CPU_INT16U pseudo_hdr_size,
NET_ERR *perr);
/*
*********************************************************************************************************
* LOCAL CONFIGURATION ERRORS
*********************************************************************************************************
*/
/*$PAGE*/
/*
*********************************************************************************************************
* NetUtil_16BitOnesCplChkSumHdrCalc()
*
* Description : Calculate 16-bit one's-complement check-sum on packet header.
*
* (1) See RFC #1071, Sections 1, 2.(1), & 4.1 for summary of 16-bit one's-complement
* check-sum & algorithm.
*
* (2) To correctly calculate 16-bit one's-complement check-sums on memory buffers of any
* octet-length & word-alignment, the check-sums MUST be calculated in network-order
* on headers that are arranged in network-order (see also 'NetUtil_16BitSumHdrCalc()
* Note #5b').
*
*
* Argument(s) : phdr Pointer to packet header.
*
* hdr_size Size of packet header.
*
* perr Pointer to variable that will receive the return error code from this function :
*
* NET_UTIL_ERR_NONE Check-sum calculated; check return value.
* NET_UTIL_ERR_NULL_PTR Argument 'phdr' passed a NULL pointer.
* NET_UTIL_ERR_NULL_SIZE Argument 'hdr_size' passed a zero size.
*
* Return(s) : 16-bit one's-complement check-sum, if NO errors.
*
* 0, otherwise.
*
* Caller(s) : various.
*
* This function is an INTERNAL network protocol suite function & SHOULD NOT be called by
* application function(s).
*
* Note(s) : (3) (a) Since the 16-bit sum calculation is returned as a 32-bit network-order value
* (see 'NetUtil_16BitSumHdrCalc() Note #5c1'), ...
*
* (b) ... the final check-sum MUST be converted to host-order but MUST NOT be re-
* converted back to network-order (see 'NetUtil_16BitSumHdrCalc() Note #5c3').
*
* (4) #### 'perr' may NOT be necessary (remove if unnecessary).
*********************************************************************************************************
*/
NET_CHK_SUM NetUtil_16BitOnesCplChkSumHdrCalc (void *phdr,
CPU_INT16U hdr_size,
NET_ERR *perr)
{
CPU_INT32U sum;
NET_CHK_SUM chk_sum;
NET_CHK_SUM chk_sum_host;
#if (NET_ERR_CFG_ARG_CHK_DBG_EN == DEF_ENABLED)
/* ------------------- VALIDATE PTR ------------------- */
if (phdr == (void *)0) {
*perr = NET_UTIL_ERR_NULL_PTR;
return (0);
}
/* ------------------- VALIDATE SIZE ------------------ */
if (hdr_size < 1) {
*perr = NET_UTIL_ERR_NULL_SIZE;
return (0);
}
#endif
/* --------- CALC HDR'S 16-BIT ONE'S-CPL SUM ---------- */
sum = NetUtil_16BitSumHdrCalc(phdr, hdr_size); /* Calc 16-bit sum (see Note #3a). */
while (sum >> 16) { /* While 16-bit sum ovf's, ... */
sum = (sum & 0x0000FFFF) + (sum >> 16); /* ... sum ovf bits back into 16-bit one's-cpl sum. */
}
chk_sum = ~((NET_CHK_SUM)sum); /* Perform one's cpl on one's-cpl sum. */
chk_sum_host = NET_UTIL_NET_TO_HOST_16(chk_sum); /* Conv back to host-order (see Note #3b). */
*perr = NET_UTIL_ERR_NONE;
return (chk_sum_host); /* Rtn 16-bit chk sum (see Note #3). */
}
/*$PAGE*/
/*
*********************************************************************************************************
* NetUtil_16BitOnesCplChkSumHdrVerify()
*
* Description : (1) Verify 16-bit one's-complement check-sum on packet header :
*
* (a) Calculate one's-complement sum on packet header
* (b) Verify check-sum by comparison of one's-complement sum to one's-complement
* '-0' value (negative zero)
*
* (2) See RFC #1071, Sections 1, 2.(1), & 4.1 for summary of 16-bit one's-complement
* check-sum & algorithm.
*
* (3) To correctly calculate 16-bit one's-complement check-sums on memory buffers of any
* octet-length & word-alignment, the check-sums MUST be calculated in network-order
* on headers that are arranged in network-order (see also 'NetUtil_16BitSumHdrCalc()
* Note #5b').
*
*
* Argument(s) : phdr Pointer to packet header.
*
* hdr_size Size of packet header.
*
* perr Pointer to variable that will receive the return error code from this function :
*
* NET_UTIL_ERR_NONE Check-sum verified; check return value.
* NET_UTIL_ERR_NULL_PTR Argument 'phdr' passed a NULL pointer.
* NET_UTIL_ERR_NULL_SIZE Argument 'hdr_size' passed a zero size.
*
* Return(s) : DEF_OK, if valid check-sum.
*
* DEF_FAIL, otherwise.
*
* Caller(s) : various.
*
* This function is an INTERNAL network protocol suite function & SHOULD NOT be called by
* application function(s).
*
* Note(s) : (4) (a) Since the 16-bit sum calculation is returned as a 32-bit network-order value
* (see 'NetUtil_16BitSumHdrCalc() Note #5c1'), ...
*
* (b) ... the check-sum MUST be converted to host-order but MUST NOT be re-converted
* back to network-order for the final check-sum comparison
* (see 'NetUtil_16BitSumHdrCalc() Note #5c3').
*
* (5) #### 'perr' may NOT be necessary (remove if unnecessary).
*********************************************************************************************************
*/
CPU_BOOLEAN NetUtil_16BitOnesCplChkSumHdrVerify (void *phdr,
CPU_INT16U hdr_size,
NET_ERR *perr)
{
CPU_INT32U sum;
NET_CHK_SUM chk_sum;
NET_CHK_SUM chk_sum_host;
CPU_BOOLEAN valid;
#if (NET_ERR_CFG_ARG_CHK_DBG_EN == DEF_ENABLED)
/* ------------------- VALIDATE PTR ------------------- */
if (phdr == (void *)0) {
*perr = NET_UTIL_ERR_NULL_PTR;
return (DEF_FAIL);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -