⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ospf_checksum.c

📁 vxworks下ospf协议栈
💻 C
字号:
/* ospf_checksum.c - OSPF checksum utility *//* Copyright 1998 - 2002 Wind River Systems, Inc. *//*modification history-------------------- 16,26september00,reshma	Added WindRiver CopyRight 15,25september00,reshma	RFC-1587 implementation for OSPF NSSA Option, also tested against ANVL. 14,07july00,reshma			Unix compatibility related changes. 13,04april00,reshma		Added some MIB support (Read only).Passed all important ANVL OSPF tests. 12,23december99,reshma		Compatibility with VxWorks-IP and VxWorks RTM-interface 11,28december98,jack		Compiled and added some comments 10,11november98,jack		Config changes, linted and big endian changes 09,30october98,jack		Incorporate changes for compilation on Vxworks 08,23august98,jack			ANVL tested OSPF with PATRICIA tree route table and no recursion 07,10august98,jack			PATRICIA Route Table Based OSPF Code Base 06,04june98,jack			Integration with RTM and BGP 05,24april98,jack			RTM changes 04,10july97,cindy			Pre-release v1.52b   03,02october97,cindy		Release Version 1.52 02,22october96,cindy		Release Version 1.50 01,05june96,cindy			First Beta Release*//*DESCRIPTIONospf_checksum.c is used for creating and validating checksums for an OSPF packet.This file is used during the sending and receiving of OSPF packets.*/#include	"ospf.h"/******************************************************************************* ospf_generate_LS_checksum - generates a checksum for data** This routine generates a checksum for the data passed in.** <vptr_data>	Data that checksum will be calculated for** <length_of_data> Length of data** <bptr_checksum> Initial checksum of data** RETURNS: New checksum in a USHORT** ERRNO: N/A** NOMANUAL*/USHORT ospf_generate_LS_checksum (void *vptr_data,USHORT length_of_data,BYTE *bptr_checksum){	long c0;	long c1;	BYTE *bptr_data;	ULONG length =0;	ULONG number_of_bytes_to_process =0;	ULONG index =0;	ULONG number_of_iterations =0;	USHORT new_checksum =0;	OSPF_ROUTER_LINK_ADVERTISEMENT_HEADER* sptr_router_link_advertisement =NULL; 	USHORT link_age_stored =0;     	OSPF_PRINTF_PROLOGUE (OSPF_PROLOGUE_PRINTF, "OSPF: Entering ospf_generate_LS_checksum\r\n");	sptr_router_link_advertisement = (OSPF_ROUTER_LINK_ADVERTISEMENT_HEADER*) vptr_data;  	link_age_stored = sptr_router_link_advertisement->ls_header.age;    /* preserve age field */	sptr_router_link_advertisement->ls_header.age = 0x0000; /* restored at the end #$-NOTE:note6-$#*/	c0 = 0x0000;	c1 = 0x0000;	bptr_data = (BYTE *) vptr_data;	length = length_of_data;	*bptr_checksum = 0x00;	*(bptr_checksum + 1) = 0x00;	/* Process enough of the packet to make the remaining length an even multiple of 4096 */	number_of_bytes_to_process = length & OSPF_MOD_MASK;	for (index = number_of_bytes_to_process & OSPF_MASK_FOR_UNEVEN_BITS; index > 0x00000000L; --index)		{		c0 = (long) (c0 + (*bptr_data++));		c1 = (long) (c1 + c0);		}  	number_of_bytes_to_process >>= OSPF_INLINED_SHIFT;	while (number_of_bytes_to_process-- > 0)		{		for (index = 0x00000008; index > 0x00000000L; --index)			{			c0 = (long) (c0 + (*bptr_data++));			c1 = (long) (c1 + c0);			}		}	/*	 * Now process the remainder in 4096 chunks, with a mod beforehand to avoid overflow.	 */	number_of_bytes_to_process = length >> OSPF_LOG2_OF_NUMBER_OF_ITERATIONS;	for (; number_of_bytes_to_process > 0x00000000L; --number_of_bytes_to_process)		{		if (bptr_data != (BYTE *) vptr_data)			{			c0 %= OSPF_MODULUS;			c1 %= OSPF_MODULUS;			}		for (number_of_iterations = OSPF_NUMBER_OF_INLINE_ITERATIONS; number_of_iterations > 0x00000000L; --number_of_iterations)			{			for (index = 0x00000008; index > 0x00000000L; --index)				{				c0 = (long) (c0 + (*bptr_data++));				c1 = (long) (c1 + c0);				}			}		}	c0 %= OSPF_MODULUS;							/* do modulus of c0 now to avoid overflow below */	c1 = (long) ((c1 - ((long) (((BYTE *) vptr_data + length) - bptr_checksum) * c0)) % OSPF_MODULUS);	if (c1 <= 0x00000000L)		{		c1 += OSPF_MODULUS;		}	c0 = (long) (OSPF_MODULUS - c1 - c0);	if (c0 <= 0x00000000L)		{		c0 += OSPF_MODULUS;		}	*bptr_checksum++ = (BYTE) c0;	*bptr_checksum = (BYTE) c1;	new_checksum = (USHORT) ((c0 << OSPF_FINAL_CHECKSUM_SHIFT) | c1);	new_checksum = host_to_net_short (new_checksum);	sptr_router_link_advertisement->ls_header.age = link_age_stored;  /* restore age field */	return (new_checksum);}/******************************************************************************* ospf_verify_LS_checksum - verifies the checksum for the data** This function calculates a checksum for data and compares it with the * checksum from the input** <vptr_data> Data that checksum will be calculated for** <length_of_data> Length of data** <checksum> Checksum to be compared with** RETURNS: PASS or FAIL** ERRNO: N/A** NOMANUAL*/enum TEST ospf_verify_LS_checksum (void *vptr_data, USHORT length_of_data /* in network order */, USHORT checksum){	BYTE first_byte_of_checksum;	BYTE second_byte_of_checksum;	USHORT byte_in_data =0;	BYTE *bptr_data;	ULONG c0 =0;	ULONG c1 =0;	USHORT length =0;	OSPF_ROUTER_LINK_ADVERTISEMENT_HEADER* sptr_router_link_advertisement =NULL; 	USHORT link_age_stored =0;    	OSPF_PRINTF_PROLOGUE (OSPF_PROLOGUE_PRINTF, "OSPF: Entering ospf_verify_LS_checksum\r\n");	sptr_router_link_advertisement = (OSPF_ROUTER_LINK_ADVERTISEMENT_HEADER*) vptr_data;  	link_age_stored = sptr_router_link_advertisement->ls_header.age;    /* preserve age field */	sptr_router_link_advertisement->ls_header.age = 0x0000; /* restored at the end  #$-NOTE:note5-$#*/ 	length = net_to_host_short (length_of_data);	checksum = net_to_host_short (checksum);	first_byte_of_checksum = (BYTE) high_byte_of_ushort (checksum);	second_byte_of_checksum = (BYTE) low_byte_of_ushort (checksum);	if (first_byte_of_checksum == 0x0)		{		sptr_router_link_advertisement->ls_header.age = link_age_stored;  /* restore age field */		return (FAIL);		}	if (second_byte_of_checksum == 0x0)		{		sptr_router_link_advertisement->ls_header.age = link_age_stored;  /* restore age field */		return (FAIL);		}	c0 = 0x00000000L;	c1 = 0x00000000L;	bptr_data = (BYTE *) vptr_data;	for (byte_in_data = 0x0001; byte_in_data <= length; ++byte_in_data, ++bptr_data)		{		c0 += *bptr_data;		c1 += c0;		}	c0 %= 0xff;	c1 %= 0xff;	sptr_router_link_advertisement->ls_header.age = link_age_stored;  /* restore age field */		if ((c0 == 0x00000000L) && (c1 == 0x00000000L))		{		return (PASS);		}	else		{		return (FAIL);		}}

⌨️ 快捷键说明

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