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

📄 icmp.c

📁 开发工具用EMBEST IDE进行开发,该程序是一个S3C44B0直接驱动网卡的程序,包括8019初始化,物理层收包发包,UDP的接收和发送,IP的发送和接收.已及ARM的地址解板,整个程序是自顶而下
💻 C
字号:
//-----------------------------------------------------------------------------
// Net ICMP.C
//
// This module handles ICMP messages
// Refer to RFC 792, 896, 950, 1122, and 1191
//-----------------------------------------------------------------------------
#include "44b.h"
#include "44blib.h"
#include "def.h"
#include "option.h"
#include "ethernet.h"
#include "arp.h"
#include "string.h"
//#include "stdlib.h"
#include "stdio.h"
#include "ip.h"
#include "icmp.h"
#include "lib.h"
extern unsigned char debug;
extern char  net_out_data[1536];

//------------------------------------------------------------------------
// This builds a ping response message.  It allocates memory for the
// entire outgoing message, including Eth and IP headers.  See "TCP/IP
// Illustrated, Volume 1" Sect 7.2 for info on Ping messages
//------------------------------------------------------------------------
void ping_send(unsigned char * inbuf, unsigned long ipaddr, unsigned short len)
{
   struct PING_HEADER  * ping_in;
    struct PING_HEADER  * ping_out;
   unsigned char  * outbuf;
            
   ping_in = (struct PING_HEADER  *)(inbuf + 34);
      
   // Allocate memory for entire outgoing message
   outbuf = (unsigned char  *)malloc(len + 34);
   	memset(outbuf,0,len+34);
   	outbuf=net_out_data;
   if (outbuf == NULL)
   {
   //   if (debug) printf("PING: Oops, out of memory\n");
  // 	Uart_Printf("PING: Oops, out of memory\n");
      return;
   }      
 //     	Uart_Printf("PING:  memory is space finshie \n");
   // Ping response message payload starts at offset 34
   ping_out = (struct PING_HEADER  *)(outbuf + 34);

   ping_out->msg_type = 0;
   ping_out->msg_code = 0;
   ping_out->checksum = 0;
   ping_out->identifier = ping_in->identifier;
   ping_out->sequence = ping_in->sequence;
   
   memcpy(&ping_out->echo_data, &ping_in->echo_data, len - 8);
            
   // Compute checksum over the ICMP header plus
	// optional data and insert complement
   ping_out->checksum =ntohs( ~cksum(outbuf + 34, len));
                        
 //  if (debug) printf("ICMP: Sending response to IP layer\n");
//	Uart_Printf("ICMP: Sending response to IP layer\n");
   ip_send(outbuf, ipaddr, ICMP_TYPE, len);
}


/*
//------------------------------------------------------------------------
// This builds an outgoing ICMP destination port unreachable response
// message.  See See "TCP/IP Illustrated, Volume 1" Sect 6.5.  This
// message is typically sent in response to a UDP message directed
// to a port that has no corresponding application running. 
// Todo: The spec says we should return all options that were in
// the original incoming IP header.  Right now we cut off everything
// after the first 20 bytes. 
//------------------------------------------------------------------------
void dest_unreach_send(unsigned char xdata * inbuf, unsigned long ipaddr)
{
   unsigned char xdata * outbuf;
   ICMP_ERR_HEADER xdata * icmp;
            
   // Allocate memory for entire outgoing message
   // including eth and IP haders.  Always 70 bytes
   outbuf = (unsigned char xdata *)malloc(70);
   if (outbuf == NULL)
   {
 //     if (debug) printf("ICMP: Oops, out of memory\n");
      return;
   }      
      
   icmp = (ICMP_ERR_HEADER xdata *)(outbuf + 34);
   
   // Fill in ICMP error message header
   icmp->msg_type = 3;   // destination unreachable
   icmp->msg_code = 3;   // port unreachable
   icmp->checksum = 0;
      
   // Fill in ICMP error message data
   icmp->msg_data = 0;
           
   // Copy in 20 byte original incoming IP header
   // plus 8 bytes of data
   memcpy(&icmp->echo_data, inbuf + 14, 28);
                               
   // Compute checksum over the 36 byte long ICMP
   // header plus data and insert complement
   icmp->checksum = ~cksum(outbuf + 34, 36);
      
   // Forward message to the IP layer
 //  if (debug) printf("ICMP: Sending dest unreach to IP layer\n");
   ip_send(outbuf, ipaddr, ICMP_TYPE, 36);
}
*/



//------------------------------------------------------------------------
// This handles incoming ICMP messages.  See "TCP/IP Illustrated,
// Volume 1" Sect 6.2 for discussion of the various ICMP messages
//------------------------------------------------------------------------
void icmp_rcve(unsigned char  * inbuf, unsigned short len)
{
   struct IP_HEADER * ip;
   unsigned char  msg_type;
   unsigned short  temp;
   unsigned long   icmp_temp_ipaddr;
   // Allow for 14 bytes eth header
   ip = (struct IP_HEADER *)(inbuf + 14);
        
   // IP header has been adjusted if necessary to always be 
	// 20 bytes so message starts at an offset of 34
   // Validate checksum of entire ICMP message incl data 
	temp = cksum(inbuf + 34, len);
	
	if (temp != 0xFFFF)
	{
//	  	if (debug) printf("ICMP: Error, cksum bad\n");
//	Uart_Printf("ICMP: Error, cksum bad\n");
      return; 
   }
      
   // Switch on the message type
   msg_type = *(inbuf + 34);
  // 	Uart_Printf("\n msg_type=%x \n",msg_type);
  icmp_temp_ipaddr=ntohl(ip->source_ipaddr);
   switch(msg_type)
   {
      case 3:
  //    if (debug) printf("ICMP: Dest unreachable rcvd\n");
 // Uart_Printf("ICMP: Dest unreachable rcvd\n");
      break;

	   case 8:
	   
  	
      ping_send(inbuf, icmp_temp_ipaddr, len); 
      break;

      default:
 //     if (debug) printf("ICMP: Error, unknown msg rcvd\n");
      break;
   }
}


⌨️ 快捷键说明

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