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

📄 icmp.c

📁 完整的TCP/IP源代码,绝对好用
💻 C
字号:


#include "C:\wql\tcpipsocket\ip.h"
#include "C:\wql\tcpipsocket\icmp.h"
#include "C:\wql\tcpipsocket\target.h"
//#include <stdio.h>
#include <string.h>
#include "C:\wql\tcpipsocket\func.h"
/* This list will contain information needed to send pings. It is used
   to determine if reply is for a ping that we sent. */

ICMP_ECHO_Cache ICMP_ECHO_Cache_IN[icmp_cache_num];
/* This will be used for generating a sequence number when sending ICMP
   echo requests (pings). */
static UINT16 ICMP_Echo_Req_Seq_Num;



/***********************************************************************
*                                                                       
* FUNCTION
*
*     ICMP_Init                                                        
*                                                                       
* DESCRIPTION                                                           
*                                                                       
*      Initialize the ICMP module. In this case is only consists of     
* nulling the ICMP_Echo_List.                                           
*                                                                       
* INPUTS                                                                
*                                                                       
*      none                                                             
*                                                                       
* OUTPUTS                                                               
*                                                                       
*      none                                                             
*                                                                       
*************************************************************************/
void ICMP_Init ()
{
    UINT8 i,j;
    /* Null the head and tail pointers of the echo list. */
    for(i=0;i<icmp_cache_num;i++)
    	{
		for (j=0;j<sizeof(ICMP_ECHO_Cache);j++)
		 {
    	  *((UINT8 *)(&ICMP_ECHO_Cache_IN[i])+j) =0;
		 }
    	}
    /* Start the sequence number at zero. */
    ICMP_Echo_Req_Seq_Num = 0;
}

/***********************************************************************
*                                                                       
* FUNCTION
*
*      ICMP_Interpret                                                   
*                                                                       
* DESCRIPTION                                                           
*                                                                       
*      Process received ICMP datagrams.                                 
*                                                                       
* INPUTS                                                                
*                                                                       
*      buf_ptr   - the ip  buffer pointer                                                          
*      buf_size  - buffer size
*      ip_source - the IP source                                                       
*                                                                       
* OUTPUTS                                                               
*                                                                       
*      0         -  Success                          
*      1         -  Failure                          
*                                                                       
*************************************************************************/
UINT8 ICMP_Interpret (UINT8* buf_ptr, UINT16 buf_size , UINT8 *ip_source)
{
    ICMP_LAYER              *icp;
    UINT16                    i;
    UINT8             j;

 //   printf("received icmp");
    icp = (ICMP_LAYER *)(buf_ptr+20);   // discard ip header

//     i = icp->icmp_type;
	i= Check_sum (buf_ptr,buf_size);
    if (icp->icmp_cksum)
    {        /* ignore if chksum=0 */
        if (i != 0xffff)//Check_sum (buf_ptr,buf_size))
        {
            return (1);
        } /* end if */
    } /* end if */

    i = icp->icmp_type;
    switch(i)
    {
        case ICMP_ECHO:                    /* ping request sent to me */
	//	printf("received echo");
            
            icp->icmp_code = ICMP_ECHOREPLY;           /* echo reply type */
            icp->icmp_type = ICMP_ECHOREPLY;
			icp->icmp_cksum = 0;
			icp->icmp_cksum = ~ Check_sum(buf_ptr+20,buf_size-20);
            IP_Send((UINT8 *)icp, (buf_size-20), ip_source, 0x01, 0x120);      
            break;

        case ICMP_ECHOREPLY:                /* ping reply requested by me */
	//	printf("received echorply");
            /* Search the list looking for a matcing ID and seq num. */
            for( i = 0; i < icmp_cache_num; i++)
            	{
            	j=memcmp(ICMP_ECHO_Cache_IN[i].dest_ip,ip_source,4);
            	if((ICMP_ECHO_Cache_IN[i].Flag == 4) && (j == 0))
            		{

            		ICMP_ECHO_Cache_IN[i].Flag = 0; // blank the cache.
            		break;
            		}

            	}


            break;

        case ICMP_REDIRECT:


            break;

        case ICMP_SOURCEQUENCH:
             
             break;

        case ICMP_UNREACH:
        	
            break;

        default:

            break;

    } /* end switch */

    return (0);
}   /* end icmpinterpret() */

//*****************************************************************
void ICMP_Send_Echo_Request( UINT8 *dest_ip, UINT16 timeout)
{
    ICMP_LAYER                 icmp_ptr;
    UINT16                       len;
    UINT8                      i,j;
      
        /* Bump the sequence number. */
        ICMP_Echo_Req_Seq_Num++;

        /* Set the data pointer to the correct location. */
        /* Get the length of the ICMP header. */
        len = ICMP_ECHO_REQ_HEADER_SIZE;

        /* Set the ICMP type to echo */
       icmp_ptr.icmp_type = ICMP_ECHO;

        /* Set the code to zero, always zero for echo packets */
        icmp_ptr.icmp_code = 0;

        /* Set the ID and sequence number fields. */
        icmp_ptr.icmp_hun.ih_idseq.icd_id = ICMP_ECHO_REQ_ID;
        icmp_ptr.icmp_hun.ih_idseq.icd_seq = ICMP_Echo_Req_Seq_Num;

        /* Fill in the standard 32 bytes of data. */
		j='a';
		for (i=0; i<32;i++)
		{
        icmp_ptr.icmp_dun.id_data[i] = j++;
		if(j>'z') j='a';
        }
		/* Compute the checksum */
        icmp_ptr.icmp_cksum =  0;
        //icmp_ptr.icmp_cksum = Check_sum((UINT8 *)&icmp_ptr, 40);

        /* Send this packet. */
        IP_Send((UINT8 *)&icmp_ptr ,40,dest_ip,0x0800,120);
        // change the flag and timer.
        for(i =0 ;i <5; i++)
        	{
        	if( ICMP_ECHO_Cache_IN[i].Flag == 0)
        		{
        		memcpy(ICMP_ECHO_Cache_IN[i].dest_ip,dest_ip,4);
        		ICMP_ECHO_Cache_IN[i].Flag = 4;
        		ICMP_ECHO_Cache_IN[i].Timer = timeout  ;  //  0.3s   20ms/tick
				break;
        		}
        	}
        if(i==5)
        	{
        	//printf(" the ping cache is not enough");
        	
        	}

} /* ICMP_Send_Echo_Reuest */







⌨️ 快捷键说明

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