www.pudn.com > pppcode.rar > icmp.c


#include "ip.h" 
#include "icmp.h" 
/*///////////////////////////////////////////////////////////////////////////// 
File Name : ICMP.c 
Author : Rene Trenado 
Location : Motorola Applications Lab, Baja California 
Date Created : January 2001 
Current Revision : 0.0 
Notes : This file contains the code to handle and create ICMP messages 
//////////////////////////////////////////////////////////////////////////////*/ 
 
/*********************************************************************** 
Function : ICMPPing 
Parameters : IP Address to ping 
Date : September 2000 
Desc : Sends a ICMP ECHO message to a remote host 
***********************************************************************/ 
void IcmpPing (BYTE Ip[]) { 
WORD Value; 
static BYTE Seq = 0xAB; 
ip_out->SourceAddress[0]=IPAddress[0];// Ping will have our source address 
ip_out->SourceAddress [1] = IPAddress [1]; 
ip_out->SourceAddress [2] = IPAddress [2]; 
ip_out->SourceAddress [3] = IPAddress [3]; 
ip_out->DestAddress [0] = Ip[0]; // Set destination IP address 
ip_out->DestAddress [1] = Ip[1]; 
ip_out->DestAddress [2] = Ip[2]; 
ip_out->DestAddress [3] = Ip[3]; 
ip_out->Payload [0] = ECHO; // ICMP message type set to ECHO 
ip_out->Payload [1] = 0; // ICMP code must by set to zero 
ip_out->Payload [2] = 0; // reset checksum 
ip_out->Payload [3] = 0; 
ip_out->Payload [4] = 1; // set ID of ICMP message 
ip_out->Payload [5] = 0; 
Seq++; 
ip_out->Payload [6] = (Seq >> 8) & 0xFF; // set sequence number of ICMP Msg 
ip_out->Payload [7] = Seq & 0xFF; 
ip_out->Protocol = ICMP; // IP datagram will carry ICMP data 
ip_out->Length = 28; // ECHO message doesn't include data 
Value = IPCheckSum ((BYTE *)&ip_out->Payload[0], (ip_out->Length - 20) >> 1); 
ip_out->Payload [2] = (Value >> 8); // obtain ICMP checksum 
ip_out->Payload [3] = (Value & 0xFF); 
IPNetSend (ip_out); // Net send to IP layer 
}/*********************************************************************** 
Function : ICMP_Handler 
Parameters : IP Datagram containing ICMP data 
Date : September 2000 
Desc : Handles incoming IP datagrams according to the TYPE field 
of the ICMP message contained in the input IP datagram 
***********************************************************************/ 
void IcmpHandler (IPDatagram* ip) { 
WORD Value; 
switch (ip->Payload [0]) { 
case ECHO: 
/* Move ping datagram to output buffer */ 
Move ((BYTE *)ip, (BYTE *)ip_out, ip->Length); 
/* Swap source and destination IP addresses on Output Buffer */ 
ip_out->DestAddress[0]=ip->SourceAddress[0]; 
ip_out->DestAddress [1] = ip->SourceAddress [1]; 
ip_out->DestAddress [2] = ip->SourceAddress [2]; 
ip_out->DestAddress [3] = ip->SourceAddress [3]; 
ip_out->SourceAddress [0] = ip->DestAddress [0]; 
ip_out->SourceAddress [1] = ip->DestAddress [1]; 
ip_out->SourceAddress [2] = ip->DestAddress [2]; 
ip_out->SourceAddress [3] = ip->DestAddress [3]; 
ip_out->Payload [0] = ECHO_REPLY;/* Echo reply */ 
ip_out->Payload [1] = 0; /* Set ICMP Code to 0 */ 
ip_out->Payload [2] = 0; /* Set ICMP checksum to 0 
during checksum generation */ 
ip_out->Payload [3] = 0; 
Value = IPCheckSum ((BYTE *)&ip_out->Payload[0], (ip->Length - 20)>> 1); /* Calculate ICMP checksum */ 
ip_out->Payload [2] = (Value >> 8);/* Set ICMP checksum */ 
ip_out->Payload [3] = (Value & 0xFF); 
IPNetSend (ip_out); /* Send ICMP packet over IP */ 
break; 
case ECHO_REPLY: // Code to handle ping responses 
// goes here 
NoOperation; 
break; 
case TRACEROUTE: 
break; 
default: 
break; 
} 
}