/// <summary> /// Process request /// </summary> /// <param name="ip">IP</param> /// <param name="id">ID</param> /// <param name="seq">Sequence number</param> /// <param name="data">Packet data</param> /// <param name="length">Packet length</param> private static unsafe void EchoReply(byte[] ip, ushort id, ushort seq, byte *data, int length) { NetPacketDesc *packet = NetPacket.Alloc(); ICMPHeader *hdr = (ICMPHeader *)(packet->buffer + packet->start); hdr->Type = TYPE_ECHO_REPLY; hdr->ID = id; hdr->SeqNum = seq; hdr->Code = 0; hdr->CheckSum = 0; packet->end += (short)sizeof(ICMPHeader); Memory.Memcpy(packet->buffer + packet->end, data, length); packet->end += (short)length; hdr->CheckSum = NetworkTools.Checksum((byte *)(packet->buffer + packet->start), sizeof(ICMPHeader) + length); IPV4.Send(packet, ip, 0x01); NetPacket.Free(packet); }
/// <summary> /// ICMP packet handler /// </summary> /// <param name="sourceIp">Source IP</param> /// <param name="buffer">Packet buffer</param> /// <param name="size">Packet size</param> private static unsafe void handler(byte[] sourceIp, byte *buffer, uint size) { ICMPHeader *hdr = (ICMPHeader *)buffer; if (hdr->Type == TYPE_ECHO_REQUEST) { int length = (int)size - sizeof(ICMPHeader); byte *data = buffer + sizeof(ICMPHeader); EchoReply(sourceIp, hdr->ID, hdr->SeqNum, data, length); } }