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

📄 ethernet.c

📁 rtl8019的程序
💻 C
字号:
#define ETHERNET_ENTITY
/*
*************************************************************
*
*            ethernet.c 
*   
*        数据链路层:以太网协议
*created data 2008.8.21
*
*by  g.m
*
**************************************************************
*/
#include "includes.h"

/*
**********************************************
*函数:INT8U Rec_Ethernet_Packet(INT8U *RecData)
*功能:
*     以太网层的接收函数:
*       1.负责更新arp表
*       2.将接收到的数据交给上层协议处理
*
*参数:
*     由rtl8019接收到的数据
*
*
*********************************************
*/
INT8U Rec_Ethernet_Packed(INT8U *RecData)
{
	ipethernet *pnet;
	
	pnet=(ipethernet*)RecData;
	
	if((pnet->NextProtocal)==0x0008)  //为ip数据报
	{
		RecData+=14;
	    //尚需添加
	    //************************************//
	}
	
	else if((pnet->NextProtocal)==0x0608)  //arp 报
	{
		RecData+=14;
		PROCESS_ARP_REC(RecData);
		return 1;
	}
	
	return 3;  //网络层协议不支持ip
}
		
	

/*
*********************************************
*函数:uint8 Send_Ip_To_LLC(_pkst *TxData,uint8 *de_ip)
*功能:
*		以太网层的数据发送,由上层协议调用,ip协议
*
*参数:TxData 上层协议数据信息,
*      de_ip ip地址
*注意:
*     该函数负责查询arp表中是否有对应ip的mac信息,若有则调用
*     Send_Ethernet_Fram()去发送数据,
*     否则则发送arp请求,并返回失败标志
*
*
*********************************************
*/
INT8U  Send_Ip_To_LLC(_pkst * TxData,INT8U *de_ip)
{
	INT8U i;
	
	//是否是本子网
	if(de_ip[0]==My_Ip[0])
	if(de_ip[1]==My_Ip[1])
	if(de_ip[2]==My_Ip[2])
	{
		//表示可以发送,查找arp表找到对应的MAc地址进行发送
		for(i=0;i<MAX_ARP_NUM;i++)
		{
			//OS_ENTER_CRITICAL();
			if(de_ip[0]==My_Arp[i].IP_NUM[0])
			if(de_ip[1]==My_Arp[i].IP_NUM[1])
			if(de_ip[2]==My_Arp[i].IP_NUM[2])
			if(de_ip[3]==My_Arp[i].IP_NUM[3])
			if(My_Arp[i].TTL!=0)
			{
				My_Arp[i].TTL=100;
				Send_ethernet_Frame(TxData,My_Arp[i].MAC_NUM,IP_PACKED);
				//OS_EXIT_CRITICAL();
				return 1;
			}
		}	
		//没有找到对应的arp表项
		//OS_EXIT_CRITICAL();
		Arp_Request(de_ip);  //发送arp请求
		
		return 0;
	}
	
	//不是本子网,直接发给网管
	Send_ethernet_Frame(TxData, My_GateArp.MAC_NUM,IP_PACKED);
	return 1;
}


/*
****************************************************
*函数:uint8 Send_ethernet_Fram(_pkst TxData,uint8 *de_mac,uint8 protocal)
*功能:
*		以太网层发送协议
*       接收来字ip协议和arp协议
*
*参数:TxData 上层协议的数据
*	   de_mac 接收端的协议地址
*	   protocal 协议的类型,ip,arp
*					】
*
****************************************************
*/
INT8U Send_ethernet_Frame(_pkst * TxData,INT8U *de_mac,INT8U protocal)
{
	ipethernet ethernet_head;
	_pkst ethernet;
	
	ethernet_head.DestMacId[0]=*de_mac++;
	ethernet_head.DestMacId[1]=*de_mac++;
	ethernet_head.DestMacId[2]=*de_mac++;
	ethernet_head.DestMacId[3]=*de_mac++;
	ethernet_head.DestMacId[4]=*de_mac++;
	ethernet_head.DestMacId[5]=*de_mac;
	
	ethernet_head.SourceMacId[0]=My_Mac[0];
	ethernet_head.SourceMacId[1]=My_Mac[1];
	ethernet_head.SourceMacId[2]=My_Mac[2];
	ethernet_head.SourceMacId[3]=My_Mac[3];
	ethernet_head.SourceMacId[4]=My_Mac[4];
	ethernet_head.SourceMacId[5]=My_Mac[5];
	
	if(protocal==ARP_PACKED)
	{
	 	ethernet_head.NextProtocal=0x0608;
	}
	else
	{
		ethernet_head.NextProtocal=0x0008;
	}
	
	//准备数据结构,将数据传送到下层协议
	ethernet.next=TxData;
	ethernet.length=14;
	ethernet.pdata=(INT8U *)&ethernet_head;
	
	//OS_ENTER_CRITICAL();
	eth_send(&ethernet);
	//OS_EXIT_CRITICAL();
	
	return 1;
}
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		

⌨️ 快捷键说明

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