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, NULL);
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_Debug("ICMPv4", "Length = %i", Length);
41 Log_Debug("ICMPv4", "hdr->Type, hdr->Code = %i, %i", hdr->Type, hdr->Code);
42 //Log_Debug("ICMPv4", "hdr->Checksum = 0x%x", ntohs(hdr->Checksum));
43 Log_Debug("ICMPv4", "hdr->ID = 0x%x", ntohs(hdr->ID));
44 Log_Debug("ICMPv4", "hdr->Sequence = 0x%x", ntohs(hdr->Sequence));
51 Log_Warning("ICMPv4", "Code == %i for ICMP Echo Reply, should be 0", hdr->Code);
54 if(hdr->ID != (Uint16)~hdr->Sequence) {
55 Log_Warning("ICMPv4", "ID and Sequence values do not match");
58 gICMP_PingSlots[hdr->ID].bArrived = 1;
61 // -- 3: Destination Unreachable
62 case ICMP_UNREACHABLE:
65 case 3: // Port Unreachable
66 Log_Debug("ICMPv4", "Destination Unreachable (Port Unreachable)");
67 IPv4_HandleError( Interface, IPERR_PORT_UNREACHABLE,
68 htons(Length)-sizeof(tICMPHeader), hdr->Data );
71 Log_Debug("ICMPv4", "Destination Unreachable (Code %i)", hdr->Code);
72 IPv4_HandleError( Interface, IPERR_MISC,
73 htons(Length)-sizeof(tICMPHeader), hdr->Data );
81 Log_Warning("ICMPv4", "Code == %i for ICMP Echo Request, should be 0", hdr->Code);
84 //Log_Debug("ICMPv4", "Replying");
85 hdr->Type = ICMP_ECHOREPLY;
87 hdr->Checksum = htons( IPv4_Checksum( (Uint16*)hdr, Length/2 ) );
88 //Log_Debug("ICMPv4", "Checksum = 0x%04x", hdr->Checksum);
90 tIPStackBuffer *buffer = IPStack_Buffer_CreateBuffer(1 + IPV4_BUFFERS);
91 IPStack_Buffer_AppendSubBuffer(buffer, Length, 0, hdr, NULL, NULL);
92 IPv4_SendPacket(Interface, *(tIPv4*)Address, 1, ntohs(hdr->Sequence), buffer);
101 * \brief Sends ICMP Echo and waits for the reply
102 * \note Times out after \a Interface->TimeoutDelay has elapsed
104 int ICMP_Ping(tInterface *Interface, tIPv4 Addr)
108 char buf[32] = "\x8\0\0\0\0\0\0\0Acess2 I"
110 tICMPHeader *hdr = (void*)buf;
115 for(i=0;i<PING_SLOTS;i++)
117 if(gICMP_PingSlots[i].Interface == NULL) break;
119 if( i < PING_SLOTS ) break;
122 gICMP_PingSlots[i].Interface = Interface;
123 gICMP_PingSlots[i].bArrived = 0;
126 hdr->Checksum = htons( IPv4_Checksum((Uint16*)buf, sizeof(buf)) );
130 tIPStackBuffer *buffer = IPStack_Buffer_CreateBuffer(1 + IPV4_BUFFERS);
131 IPStack_Buffer_AppendSubBuffer(buffer, sizeof(buf), 0, buf, NULL, NULL);
132 IPv4_SendPacket(Interface, Addr, 1, i, buffer);
134 end = ts + Interface->TimeoutDelay;
135 while( !gICMP_PingSlots[i].bArrived && now() < end) Threads_Yield();
137 if( !gICMP_PingSlots[i].bArrived )
140 return (int)( now() - ts );