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

📄 ipv6.c

📁 udp,tcp/ip在智能家居芯片上的代码实现.包括芯片NE64的网络驱动源代码
💻 C
字号:
#include <inet/datatypes.h>
#include <inet/ethernet.h>
#include <inet/ipv6.h>

#ifndef _htons
#define _htons(A)	((((A) & 0xFF00) >> 8) | \
			 (((A) & 0x00FF) << 8))
#endif

extern MBUF* ETH_CurrentInFrame (void);

/**	\brief Used for storing various information about the incoming IP packet
 *	
 *	Various fields from the IP packet are stored in this structure. These
 *	values are later used from other upper layer protocols (ICMP, UDP, TCP
 *	and possibly others) to extract needed information about the received
 *	packet. See ip_frame definition for struct information.
 */
struct ipv6_frame received_ip_packet;

/**	\brief Used for storing various information about the outgoing IP packet
 *	
 *	Various fields from the IP packet are stored in this structure. These
 *	values are filled based on the information supplied by the upper 
 * 	layer protocols (ICMP, UDP, TCP and possibly others) and used to form
 *	a correct IP packet (correct filed values, checksum,..). See ip_frame
 *	definition for struct information.
 */
struct ipv6_frame send_ip_packet;



/** \brief Process received IP frame
 *	\ingroup periodic_functions
 * 	\author 
 *		\li Jari Lahti
 *	\date 11.06.2002
 *	\param frame pointer to ethernet_frame structure holding information
 *		about the received frame that carries IP datagram.
 *	\return
 *		\li -1 - IP packet not OK
 *		\li >0 - Length of next layer data (IP packet OK)
 *
 *	Process received IP packet by checking necessary header information
 *	and storing it accordingly to received_ip_packet variable. If everything
 *	checks out, return length of the data carried in the IP datagram (for
 *	higher-level protocols), otherwise return -1.
 */
INT16 process_ip_in (struct ethernet_frame* frame)
{
  MBUF *pNBuf = 0;
  (void)frame;
	
	pNBuf = ETH_CurrentInFrame ();
	if ((received_ip_packet.protocol = (signed char)ipv6Input (pNBuf)) > 0)
	{
		memcpy (&received_ip_packet.sip, &((IPv6BUF *)pNBuf->data)->hdr.sip, sizeof(IPv6Addr));
		memcpy (&received_ip_packet.sip, &((IPv6BUF *)pNBuf->data)->hdr.sip, 2*sizeof(IPv6Addr));
		received_ip_packet.buf_index = sizeof(IPv6BUF);
		return _htons(((IPv6BUF *)pNBuf->data)->hdr.len);
	}
	return 0;
}


/******************************************************************************
*
*
******************************************************************************/
INT16 process_ip_out (IPv6Addr *rem_ip, UINT8 pcol, UINT8 tos, 
					  UINT8 ttl, 
					  UINT8* dat, 
					  UINT16 len )
{
	return ipv6Send (rem_ip,pcol,dat,len);
}



/******************************************************************************
 * \brief Used for constructuing IP checksum
 * 	\author 
 *		\li Jari Lahti
 *	\date 24.02.2002
 *	\param cs last checksum value
 *	\param dat byte to be added to checksum
 *	\param count byte indicating whether dat is MSB or LSB byte
 *	\return new checksum value
 *
 *	Based on count value, dat byte is added to checksum either as a MSB
 *	or a LSB byte and the new checksum value is then returned.
 *
 *****************************************************************************/
UINT16 ip_checksum (UINT16 cs, UINT8 dat, UINT8 count)
{
	UINT8 b = dat;
	UINT8 cs_l;
	UINT8 cs_h;
	
	cs_h = (UINT8)(cs >> 8); 
	cs_l = (UINT8)cs;

	if( count & 0x01 ) 
	{		/* We are processing LSB	*/
		
		if( (cs_l = cs_l + b) < b ) 
		{
			if( ++cs_h == 0 )
				cs_l++;
		}
		
	} else {
		/* We are processing MSB	*/
		
		if( (cs_h = cs_h + b) < b )	
		{
			if( ++cs_l == 0 )
				cs_h++;
		}
	}

	return( ( (UINT16)cs_h << 8 ) + cs_l);

}



/******************************************************************************
 *  \brief Used for constructuing IP checksum of a data buffer
 * 	\author 
 *		\li Jari Lahti
 *	\date 03.08.2003
 *	\param cs last checksum value
 *	\param buf buffer who's checksum we're calculating
 *	\param len length of data in buffer
 *	\return new checksum value
 *
 *	Calculates checksum of the data in buffer and returns new
 *	checksum value.
 *
 *****************************************************************************/
UINT32 ip_checksum_buf (UINT16 cs, UINT8* buf, UINT16 len)
{
	UINT16 dat;
	UINT32 temp;
	
	temp = cs;
	
	while(len>1)
	{
		len -= 2;
		dat = *buf++;
		dat <<= 8;
		dat |= *buf++;
		temp += dat;
	}
	
	temp = (temp >> 16) + (temp & 0xFFFF);	/* Add in carry		*/
	temp += (temp >>16);					/* Maybe one more	*/
	
	if(len)
		temp = ip_checksum(temp, *buf, 0);
	
	return( (UINT16) temp );

}

⌨️ 快捷键说明

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