ospf_old_ip_dependencies.c

来自「vxworks下ospf协议栈」· C语言 代码 · 共 118 行

C
118
字号
/* ospf_old_ip_dependencies.c *//* Copyright 2000 Wind River Systems, Inc. *//*modification history___________________ *//* DESCRIPTIONospf_old_ip_dependencies.c is used for checksums for IP.This file is used whenever the IP checksum needs to be evaluated.*/#include "ospf.h"/*************************************************************************/static USHORT end_around_carry (ULONG sum);				/* Carries in high order 16 bits */static USHORT calculate_word_checksum (USHORT *usptr_short, USHORT length);/****************************************************************************/static USHORT end_around_carry (ULONG sum)				/* Carries in high order 16 bits */{	USHORT csum;	USHORT return_value;	OSPF_PRINTF_DEBUG (OSPF_DEBUG_PRINTF, "OSPF: Entering end_around_carry\r\n");	csum = (USHORT) (sum >> 16);	while (csum != 0x0000)		{		sum = csum + (sum & 0xffffL);		csum = (USHORT) (sum >> 16);		}	/* Chops to 16 bits */	return_value = (USHORT) (sum & 0x0000ffffL);	return (return_value);}/****************************************************************************/static USHORT calculate_word_checksum (USHORT *usptr_short, USHORT length){	ULONG sum;	USHORT result;	OSPF_PRINTF_DEBUG (OSPF_DEBUG_PRINTF, "OSPF: Entering calculate_word_checksum\r\n");	sum = 0x00000000L;	while (length != 0x0000)		{		--length;		sum += *usptr_short;		++usptr_short;		}	result = end_around_carry (sum);	return (result);}/*************************************************************************//* the data order is assumed to be network order. */USHORT calculate_ip_checksum (PSEUDO_IP_PARAMETERS *sptr_pseudo_header, BYTE *bptr_start_from, USHORT length){	USHORT word_checksum;	ULONG sum;	ULONG checksum;	USHORT return_value;	OSPF_PRINTF_DEBUG (OSPF_DEBUG_PRINTF, "OSPF: Entering calculate_ip_checksum\r\n");	sum = 0x00000000L;	if (sptr_pseudo_header != NULL)		{			word_checksum = calculate_word_checksum ((USHORT *) sptr_pseudo_header, (USHORT) (sizeof (PSEUDO_IP_PARAMETERS)) >> 1);		sum = word_checksum;		}	checksum = 0x00000000L;	if (length > 1)		{		/* ptr_start_from must be on short word boundary */		word_checksum = calculate_word_checksum ((USHORT *) bptr_start_from, (USHORT) (length >> 1));		checksum += word_checksum;		}	/* Handle odd trailing byte */	if ((length & 0x0001) != 0x0000)		{#if (_BYTE_ORDER == _LITTLE_ENDIAN )		checksum += (unsigned char) (bptr_start_from[--length]);#else		checksum += (USHORT)(((unsigned char) (bptr_start_from[--length])) << 8);#endif		}	sum += checksum;	/* Do final end-around carry, complement and return */	return_value = (USHORT) (~end_around_carry (sum) & 0xffff);	return (return_value);}/*************************************************************************/

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?