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

📄 ip.c.svn-base

📁 数字广播系统的开发源码
💻 SVN-BASE
字号:
/*

 */

#include "GloblDef.h"
#include "TCPIPmem.h"
#include "IP.h"
#include "icmp.h"
#include "Netif.h"
#include "TCP.h"

/* Check sum calulation. data in buff, size, InSum is initial sum */
unsigned int CheckSum(unsigned int xdata * buff,unsigned int size,unsigned long InSum) reentrant
{
	/* TO DO:in packet memory high part of short is in low memory. add all data in
	form of 16 bits get a result of 32 bits, add high 16 bits to low 16 bits two
	times.  get a 16 bits result then complement it. */

	unsigned long cksum = InSum;

	/* sum all unsigned int except the last odd unsigned char(if size is a odd num) */
	unsigned int xdata * EndBuf = buff + size/2;
	while(buff < EndBuf)
	{
		/* net order is equeal as host order in mirochip, so no need to change */
		cksum += *(buff++);
	}

	/**((unsigned int xdata *)CheckSumInParam) = size;
	*((unsigned int xdata *)(CheckSumInParam+2)) = buff;
	asmAddCheckSum();
	cksum = CheckSumOutParm;
	*/

	/* if has last odd unsigned char. use this unsigned char as the high part of 16 bits, and add. */
	if((size & 0x0001) != 0)
		cksum += (*buff) & 0xff00;

	cksum = (cksum >> 16) + (cksum & 0xffff);
	cksum += (cksum >>16);
	return (unsigned int)(~cksum);
}

/* IP input process */
void IPInput(struct SMemHead xdata *MemHead) reentrant
{
	struct SIPHead xdata *pIPHead;
	struct SNetIf  xdata *pNetIf;		/* for search netif list */

	pIPHead = (struct SIPHead xdata *)(MemHead->pStart);

	/* check ip version */
	if(IP_VERSION(pIPHead) != IP_VERSION_4)
	{
		MemFree(MemHead);
		return;
	}

	/* if checksum is ok */
	if(CheckSum((unsigned int xdata *)pIPHead,(unsigned int)IP_HEAD_LEN(pIPHead),0) != 0)
	{
		MemFree(MemHead);
		return;
	}

	/* ip packet with options is not supported */
	if(IP_HEAD_LEN(pIPHead) != IP_HEAD_MIN_LEN)
	{
		MemFree(MemHead);
		return;
	}

	/* ip packet fragmented is not supported */
	if((pIPHead->FragmentFlag_Offset & IP_FRAGMENT_OFFSET_MASK)!= 0)
	{
		MemFree(MemHead);
		return;
	}


	/* if this packet for us. check all the netif. if a host
	has tow device(tow ip). This packet may come from one device
	but send for the IP of the other deviec. In this case we should
	not drop or forward this packet */

	/* if this packet is not for us. forward it */
	if((pNetIf = NetIfFindIP(pIPHead->IPDest)) == NULL)
	{
		#ifdef IP_ENABLE_FORWARD	/* if act as a router */
		/* We should decrease the IPHead->ttl */
		if(pIPHead->LifeLength != 0)
		{
			pIPHead->LifeLength--;

			/* recaculate IP head checksum. there is a easy method
			to recaculate, leave for later version improvment */
			CheckSum((unsigned int xdata *)pIPHead,(unsigned int)IP_HEAD_LEN(pIPHead),0);

			/* find a rout( a interface ) */
			if((pNetIf = NetIfFindRout(pIPHead->IPDest)) != NULL)
			{
				/* forward. send it through this interface. if return FALSE, we
				do not care, the soure of the packet will deel with it. */
				pNetIf->output(MemHead,pNetIf,pIPHead->IPDest);
			}
		}
		#endif

		MemFree(MemHead);
		return;
	}
	else
	{
		/* MemHead->pStart set to point uper layer */
		MemHead->pStart += sizeof(struct SIPHead);

		/* pass to the uper layer */
		switch(pIPHead->Protocol)
		{
		case IP_PROTOCOL_TCP:
                        //只调试网络驱动和ICMP
			//TCPInput(MemHead);
			break;
#if	ICMP_EN
		case IP_PROTOCOL_ICMP:
			ICMPInput(MemHead);
			break;
#endif
		default:
			MemFree(MemHead);
		}
	}
}

/* out put a ip packet,NOTE:MemHead->pStart point to IPHead.
IPScr IPDest Protocol TotalLen is already filled at uper layer.
To do so TCPCheckSum is easy to generate and pass augument to
IPOutput is easyer.
return :
	TURE: send the packt successful. */
unsigned char IPOutput(struct SMemHead xdata * MemHead) reentrant
{
	struct SNetIf  xdata *pNetIf;
	struct SIPHead xdata *pIPHead;
	unsigned int tCheckSum;

	pIPHead = (struct SIPHead xdata *)(MemHead->pStart);

	/* found a rout */
	if((pNetIf = NetIfFindRout(pIPHead->IPDest)) != NULL)
	{
		/* fill IP head */
		pIPHead->CheckSum				= 0;
		pIPHead->FragmentFlag_Offset	= 0;
		pIPHead->FragmentID				= 0;
		pIPHead->LifeLength				= IP_INITIAL_LIFE;
		pIPHead->ServeType				= 0;
		pIPHead->Ver_HeadLen			= (IP_VERSION_4 << 4) + IP_HEAD_MIN_LEN/4;

		/* checksum */
		tCheckSum = CheckSum((unsigned int xdata *)pIPHead,(unsigned int)IP_HEAD_LEN(pIPHead),0);
		pIPHead->CheckSum = htons(tCheckSum);

		/* output it */
		return pNetIf->output(MemHead,pNetIf,pIPHead->IPDest);
	}
	else
		return FALSE;
	/* 'MemHead' freeing is at tcp model when it is acked */
}

⌨️ 快捷键说明

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