13 void ICMP_Initialise();
14 void ICMP_GetPacket(tInterface *Interface, void *Address, int Length, void *Buffer);
18 tInterface *Interface;
20 } gICMP_PingSlots[PING_SLOTS];
24 * \fn void ICMP_Initialise()
25 * \brief Initialise the ICMP Layer
27 void ICMP_Initialise()
29 IPv4_RegisterCallback(IP4PROT_ICMP, ICMP_GetPacket);
33 * \fn void ICMP_GetPacket(tInterface *Interface, void *Address, int Length, void *Buffer)
34 * \brief Handles a packet from the IP Layer
36 void ICMP_GetPacket(tInterface *Interface, void *Address, int Length, void *Buffer)
38 tICMPHeader *hdr = Buffer;
40 Log("[ICMP ] Length = %i", Length);
41 Log("[ICMP ] hdr->Type = %i", hdr->Type);
42 Log("[ICMP ] hdr->Code = %i", hdr->Code);
43 Log("[ICMP ] hdr->Checksum = 0x%x", ntohs(hdr->Checksum));
44 Log("[ICMP ] hdr->ID = 0x%x", ntohs(hdr->ID));
45 Log("[ICMP ] hdr->Sequence = 0x%x", ntohs(hdr->Sequence));
52 Warning("[ICMP ] Code == %i for ICMP Echo Reply, should be 0", hdr->Code);
55 if(hdr->ID != (Uint16)~hdr->Sequence) {
56 Warning("[ICMP ] ID and Sequence values do not match");
59 gICMP_PingSlots[hdr->ID].bArrived = 1;
62 // -- 3: Destination Unreachable
63 case ICMP_UNREACHABLE:
66 case 3: // Port Unreachable
67 Log("[ICMP ] Destination Unreachable (Port Unreachable)");
70 Log("[ICMP ] Destination Unreachable (Code %i)", hdr->Code);
73 // IPv4_Unreachable( Interface, hdr->Code, htons(hdr->Length)-sizeof(tICMPHeader), hdr->Data );
79 Warning("[ICMP ] Code == %i for ICMP Echo Request, should be 0", hdr->Code);
82 Log("[ICMP ] Replying");
83 hdr->Type = ICMP_ECHOREPLY;
85 hdr->Checksum = htons( IPv4_Checksum(hdr, Length) );
86 Log("[ICMP ] Checksum = 0x%04x", hdr->Checksum);
87 IPv4_SendPacket(Interface, *(tIPv4*)Address, 1, ntohs(hdr->Sequence), Length, hdr);
96 * \brief Sends ICMP Echo and waits for the reply
97 * \note Times out after \a Interface->TimeoutDelay has elapsed
99 int ICMP_Ping(tInterface *Interface, tIPv4 Addr)
103 char buf[32] = "\x8\0\0\0\0\0\0\0Acess2 I"
105 tICMPHeader *hdr = (void*)buf;
110 for(i=0;i<PING_SLOTS;i++)
112 if(gICMP_PingSlots[i].Interface == NULL) break;
114 if( i < PING_SLOTS ) break;
117 gICMP_PingSlots[i].Interface = Interface;
118 gICMP_PingSlots[i].bArrived = 0;
121 hdr->Checksum = htons( IPv4_Checksum(hdr, sizeof(buf)) );
122 IPv4_SendPacket(Interface, Addr, 1, i, sizeof(buf), buf);
125 end = ts + Interface->TimeoutDelay;
126 while( !gICMP_PingSlots[i].bArrived && now() < end) Threads_Yield();
129 if(ts > Interface->TimeoutDelay)