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

📄 ip.c

📁 MC9S12NE64串口与网络通信源代码
💻 C
字号:
 /*****************************************************************************
 *
 * File Name     : ip.c
 *
 * DESCRIPTION   : Used for IP Protocol  
 *
 * Written by houlei  @2004.10.10
 * Recently Modified by houlei @2004.12.10
 *****************************************************************************/

#include "ethernet.h"
#include "ne64api.h"
#include "etherinit.h"
#include "MOTTYPES.h"
#include "IO_MAP.h"


eth_HwAddress sed_lclEthAddr;     
eth_HwAddress sed_ethBcastAddr;   
in_HwAddress  sin_lclINAddr;  
   
in_Header	*ip_ip;               //pointer to ip header

extern tU08 sed_CheckPacket(tU08 *, tU16);
extern tU08 sar_CheckPacket(arp_Header *);
extern void icmp_in(in_Header *,tU16);
extern void udp_receive(in_Header *);
extern tU08 activebuffer;
extern tU08 emacFIFOtx[EMAC_TX_SZ];			
extern tU08 emacFIFOa[EMAC_TX_SZ];		
extern tU08 emacFIFOb[EMAC_TX_SZ];		 


tU16 checksum(tU16 *, tU16);
tU08  *EtherPtr;


void ip_daemon(void){

	tU16	old_ip_checksum;	
#if ZERO_COPY
    if (activebuffer == BUFA_FULL)  EtherPtr=(tU08 *)emacFIFOa;
    else if (activebuffer == BUFB_FULL) EtherPtr=(tU08 *)emacFIFOb;
#else
        //NOT IMPLEMENTED (ZERO_COPY REQUIRED)
#endif  //ZERO_COPY

		ip_ip = (in_Header *)(EtherPtr+sizeof(eth_Header)); //to get the ip header
		if ( ip_ip == NIL )                                 //If the header address is not valid
		 {
		   goto toexit;
		  }
	    else{                                               //valid, then process the packet
			if ( sed_CheckPacket((tU08 *)ip_ip, 0x0806) == 1 )// to check for arp packet
			 {
              _DEBUGT("\r\n--ARP---\r\n");
			  sar_CheckPacket((arp_Header *)ip_ip);// deal with arp packet
			 }
			else if ( sed_CheckPacket((tU08 *)ip_ip, 0x0800) == 1 )//to check for ip packet
			 {
              _DEBUGT("\r\n--IP---\r\n");
              
			  old_ip_checksum = ip_ip->checksum;
			  ip_ip->checksum = 0x0000;
			  if ( ip_ip->destination == sin_lclINAddr && in_GetVersion(ip_ip)== 0x04
			     && checksum((tU16 *)ip_ip, sizeof(in_Header))==old_ip_checksum )// to check the ip packet is correct
			  {  ip_ip->checksum = old_ip_checksum;
			     switch(in_GetProtocol(ip_ip))
			    {
				case ip_prot_icmp:
					_DEBUGT("\r\n--ICMP---\r\n");
					icmp_in(ip_ip,ip_ip->length);//deal with icmp packet
					break;
				case ip_prot_tcp:
					_DEBUGT("\r\n--TCP---\r\n");
					       //tcp_Handler(ip_ip);
					break; //deal with tcp packet
				case ip_prot_egp:
					_DEBUGT("\r\n--NEGP---\r\n");
					break; //deal with egp packet
				case ip_prot_udp:
					_DEBUGT("\r\n--UDP---\r\n");
					udp_receive(ip_ip);
					break; //deal with udp packet
				default:break;
			       }//for switch
			     }//for if
			}//for else if
		}//for else
toexit:
#if ZERO_COPY
     if (activebuffer == BUFA_FULL)  IEVENT = IEVENT_RXACIF_MASK;
     else if (activebuffer == BUFB_FULL) IEVENT = IEVENT_RXBCIF_MASK;
#endif

}


/*
 * Do a one's complement checksum
 */
tU16 checksum(tU16 *dp, tU16 length)
{
	tU16 len;
	tU32 sum;

	len = length >> 1;
	sum = 0;
	while ( len-- > 0 ) sum += *dp++;
	if ( length & 1 ) sum += (*dp & 0xFF00);
	sum = (sum & 0xFFFF) + ((sum >> 16) & 0xFFFF);
	sum = (sum & 0xFFFF) + ((sum >> 16) & 0xFFFF);

	return ( ~sum );

}

/*
 * Do a protocol check for the packet
 */

tU08 sed_CheckPacket( tU08 *BufLocation, tU16 expectedType )
{
		
	if ( *(tU16 *)(BufLocation-2) != expectedType ) {
		return ( 0 );
	}
	return (1);
}


⌨️ 快捷键说明

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