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

📄 etherif.c.svn-base

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

#include "GloblDef.h"
#include "TCPIPmem.h"
#include "IP.h"
#include "etherif.h"
#include "ARP.h"
#include "Netif.h"


/* call output to put a packet from IP layer to device. After
	Ip layer selected a device, it use output to send this packet.
	MemHead contain a packet and Netif tell dirver which netif it
	is. NOTE:MemHead->pStart point to pIPHead
return:
	TRUE: send successfuly.*/
unsigned char EtherOutput(struct SMemHead xdata *MemHead,struct SNetIf xdata* NetIf,
		IP_ADDR DestIP) reentrant
{
	unsigned short NextIP;	/* next host to receive the packet in rout */
	struct SEtherHead xdata * pEtherHead;
	struct SMemHead xdata *p;

	pEtherHead = (struct SEtherHead xdata *)(MemHead->pStart - sizeof(struct SEtherHead));

	/* if DestIP in this subnet ... */
	if((NetIf->NetMask & NetIf->IPAddr) == (NetIf->NetMask & DestIP))
		NextIP = DestIP;
	else
		NextIP = NetIf->GateWay;

	/* find Ether addr of NextIP */
	if(ARPFind(pEtherHead->DestAddr,NextIP) == FALSE)
	{
		/* send a arp query */
		if((p = ARPQuery(NetIf,NextIP)) != NULL)
		{
			((struct SEtherDevice xdata *)(NetIf->Info))->send(
				p->pStart,sizeof(struct SARPPacket) +
				sizeof(struct SEtherHead));

			MemFree(p);
		}
	}
	else
	{
		/* fill ehter header, DestAddr already filled in ARPFind */
		MemCopy(pEtherHead->ScrAddr,
			((struct SEtherDevice xdata *)(NetIf->Info))->Addr,ETHER_ADDR_LEN);

		pEtherHead->type = htons(ETHER_TYPE_IP);

		/* send the packet. packet lenth is less than MemHead size */
		return ((struct SEtherDevice xdata *)(NetIf->Info))->send(
			pEtherHead,(WORD)(MemHead->pEnd - (BYTE xdata *)pEtherHead));
	}
	return FALSE;
	/* free MemHead when it is acked in tcp model */
}

/* this function is called periodically.Get a packet from specific
device. If there is a packet, call NetIf->Input to do more */
void EtherInput(struct SNetIf xdata * NetIf) reentrant
{
	struct SMemHead xdata *MemHead;
	struct SEtherHead xdata *pEtherHead;
	struct SMemHead xdata *p;

	/* if there is a packet to deal with */
	while((MemHead = ((struct SEtherDevice xdata *)(NetIf->Info))->recv())
		!= NULL)
	{
		/* Note, pStart point to EtherHead */
		pEtherHead = (struct SEtherHead xdata *)(MemHead->pStart);

		/* which packet type */
		switch(ntohs(pEtherHead->type))
		{
		case ETHER_TYPE_IP:
			/* before pass to IP layer, let MemHead->pStart point
			to IP header */
			MemHead->pStart += sizeof(struct SEtherHead);

			/* pass to IP layer for more dealing */
			IPInput(MemHead);
			break;

		case ETHER_TYPE_ARP:
			if((p = ARPInput(MemHead,NetIf)) != NULL)
			{
				/* a arp reply need to be send */
				((struct SEtherDevice xdata *)(NetIf->Info))->send(
					p->pStart,sizeof(struct SARPPacket)
					+ sizeof(struct SEtherHead));

				MemFree(p);
			}
			/* 'MemHead' is freed in ARPInput() */
			break;
		default:

			/* unknown packet type free */
			MemFree(MemHead);
		}
	}
}
/* ethernet device init */
void EtherDevInit(struct SEtherDevice xdata * pDevice, BYTE EtherAddr[],
				  unsigned char (DT_CODE * send)(void xdata *buf, WORD size)  reentrant,
				  struct SMemHead xdata *(DT_CODE * recv)()  reentrant) reentrant
{
	MemCopy(pDevice->Addr,EtherAddr,ETHER_ADDR_LEN);
	pDevice->recv = recv;
	pDevice->send = send;
}



⌨️ 快捷键说明

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