Spanish Italian
9404 Users    

SUCKERmenu

17 ICMP

ICMP
The ICMP is a service protocol (descried from the RFC 792), used to effectuate the controls and the signals inside a network. An ICMP packet can transport different messages types, but that most known and the unique treat in here, is the Echo request, known also like Ping.
In the ISO/OSI model this protocol is usually insert to the third level, doesn't being properly a transport protocol, but it is found above the IP protocol inside of this one is encapsulated.

The packet structure
The ICMP packet structure varies upon the transported message, with the exception of the heading, common to all the types.
Let's see the particular case of an eco request:

- Type: takes the value 8 for the request, while in the answer packet is 0.
- Code: unused.
- Checksum: By now known the control field. The algorithm used is the same with the IP protocol..
- Identifier e Sequence Number: are generated by the applicant to recognize the answer. Are left unchanged from who respond.
- Data: contains the dates variable number that are turned in in the answer.

The code
First of all let's see the content of icmp.h file that contains some define, but especially the ICMP packet definition.

#define ICMP_ECHO 8
#define ICMP_ECHO_REPLY 0
#define MAX_ICMP_DATA 32 // the maximum length of the dates field

typedef struct {
u8 type;
u8 code;
u16 checksum;
u16 id;
u16 sn;
u8 data[MAX_ICMP_DATA];
} ICMPPacket;

void processICMP(IP_Header ipHeader);
Like you can see are presented the different fields described in the previous paragraph.

The icmp.c file contains the code that permits to answer to a ping.
#include "stack.h"

#define ICMP_Offset sizeof(IP_Header)

void processICMP(IP_Header ipHeader){
ICMPPacket packet;
u8 size;

size = ipHeader.totalLength - (ipHeader.verlen & 0x0F)*4;
if (size > sizeof(packet)) size = sizeof(packet);
encGetArray((u8*)&packet, size);

if (packet.type == ICMP_ECHO){
packet.type = ICMP_ECHO_REPLY;
packet.checksum = 0;
IPPutHeader(ipHeader.sourceIP, IPPROTO_ICMP, (u8*)&packet, size, size);
putChecksum(ICMP_Offset+2,DMAChecksum(ICMP_Offset,size,TRUE));
MACSend();
}
}
Through the encGetArray method the packet is read and saved in the variable packet . Then, once examined the type field, if this results to be an eco request, is built the answer packet:
the type field is set to 0 (Echo Reply),the checksum is reseted to zero and recalculated once that the packet is wrote in the buffer; at last is sent everything withMACSend.

ProcessPacket
To functioning everything, must implement a method that examines the packets in arrived and send them to the relatives administrator. (IP and ARP for the third level).
This method is processPacketans is recalled inside a infinite loop in the method main:
void main() {
encInit();

while (1)
processPacket();
}

Let's see the stack.c file
#include "stack.h"

MACAddr remoteAddr;

void processPacket(){
MAC_Header header;
setBank(1);
while (readETH(EPKTCNT)) { // if there is almost a packet

MACGetHeader(&header);
if (header.type == TYPE_IP){
remoteAddr = header.sourceMAC;
processIP();
} else
if (header.type == TYPE_ARP)
processARP();

freeRxSpace(); // free the space in the RX buffer
}
}
Reading the EPKCNT register we verify that there is almost a packet waiting to be read, then with the MACGetHeader is read the MAC heading and through the type field is selected if call processIP or proccessARP (or none of these).
These methods were treat in the previous pages.

AttachmentSize
ICMP - C1816.05 KB

Who's new

  • mallmusa
  • Linoko
  • chandima.ediriweera
  • lello
  • vaio007

Who's online

There are currently 0 users and 123 guests online.