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

📄 ip.c

📁 CP2201和51单片机实现ARP ICMP IP UDP协议
💻 C
字号:
#include "ne2000.h"
#include "eth.h"
#include "arp.h"
#include "icmp.h"
#include "udp.h"
#include <stdio.h>
extern union NetNode xdata myNode;
WAIT xdata wait;
extern unsigned int chksum(unsigned char xdata *check,unsigned int length);
void ip_rcve(INT8U xdata * inbuf, union ethernet_address_type eth_addr)
{
   INT8U i;
   INT16U  reslaut;
   INT8U idata header_len, payload_len;
   union ip_address_type  xdata ip_source;
   ip_head *ip;
   ip=(ip_head *)(inbuf+14);
   header_len = 4 * (0x0F & ip->verandihl);
   payload_len = ip->totallength - header_len;
   reslaut=chksum(inbuf+14,header_len);
   if(reslaut!=0xffff)
    return;
   if(((ip->verandihl&0xf0) != 0x40)	
        || (ip->destip[0] != myNode.nodebytes.ipbytes[0])
     	|| (ip->destip[1] != myNode.nodebytes.ipbytes[1])
    	|| (ip->destip[2] != myNode.nodebytes.ipbytes[2])
    	|| (ip->destip[3] != myNode.nodebytes.ipbytes[3]))
    return;
    if(ip->segment&0x3fff!=0)
    return;
     arp_ip_mac(inbuf,eth_addr);
     for(i=0;i<4;i++)
      ip_source.bytes[i]=ip->sourceip[i];
      switch(ip->protocal)
      		 {	
       		    case 1:
      			if(inbuf[34] == 8)		
      			{
        		ping_answer(inbuf,ip_source,payload_len);
        		}          
     			break;
     			case 17:
     			udp_rcve(inbuf,payload_len);
    			break;
    			default:
    			break;
      	    }
}

void ip_send(INT8U xdata * outbuf, union ip_address_type ipaddr, INT8U proto_id, INT16U len)
{   
   ip_head xdata * ip;
   union ethernet_address_type xdata hwaddr;
   static INT16U ip_ident = 0;
   INT8U idata i;
   ip=(ip_head xdata *)(outbuf+14);
   ip->verandihl= 0x45;   //版本号和头长度
   ip->typeofserver=0;   // 服务类型
   ip->totallength=len+20;    //IP报总长度
   ip->frameindex=ip_ident+1;    //标示字段
   ip->segment=0;                //分段标志
   ip->ttl=0x80;                 //  生存时间
   ip->protocal=proto_id;       //上层协议
   for(i=0;i<4;i++)        // 目的IP与源IP
	{
	   ip->sourceip[i]=myNode.nodebytes.ipbytes[i];
	   ip->destip[i]=ipaddr.bytes[i];
      }
   ip->crc=0;
   ip->crc=~chksum((outbuf+14), 20);    //校验和
   if(arp_find_mac(ipaddr,&hwaddr)) //如果ARP表中找到目的IP的MAC地址
    eth_send (outbuf, hwaddr, IP_PACKET, ip->totallength);
   else  
	 {  
	    for(i=14;i<ip->totallength+14;i++)
        wait.bytebuf[i] =outbuf[i];
		wait.ipaddr = ipaddr.dwords;
		wait.proto_id = IP_PACKET;
		wait.len = len+20;
		arp_request(ipaddr);
	    return;
	  }     
             
}     
   
    	
    	
    	
    	
    	
    	
    	
    	
    	
    	
    	
    	
    	
    	
    	
    	
    	

⌨️ 快捷键说明

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