/// <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> /// Finish header and create checksum /// </summary> /// <param name="packet"></param> /// <param name="header"></param> /// <param name="sourceIp"></param> /// <param name="packetLength"></param> /// <param name="dataLength"></param> /// <returns></returns> private static unsafe bool FinishHeader(NetPacketDesc *packet, TCPHeader *header, byte[] sourceIp, int packetLength, int dataLength) { // Welp! if (packetLength % 4 != 0) { return(false); } header->Length = (byte)((packetLength / 4) << 4); ushort size = (ushort)packetLength; size += (ushort)sizeof(TCPChecksum); // Let's introduce some junk (i love that :)) TCPChecksum *checksumHeader = (TCPChecksum *)(packet->buffer + packet->start - sizeof(TCPChecksum)); Memory.Memcpy(checksumHeader->SrcIP, Network.Settings->IP, 4); Memory.Memcpy(checksumHeader->DstIP, Util.ObjectToVoidPtr(sourceIp), 4); checksumHeader->Protocol = PROTOCOL_TCP; checksumHeader->Reserved = 0; checksumHeader->Length = Byte.ReverseBytes((ushort)((ushort)packetLength + dataLength)); byte *ptr = packet->buffer + packet->start - sizeof(TCPChecksum); header->Checksum = NetworkTools.Checksum(ptr, size + dataLength); return(true); }
/// <summary> /// Add IPV4 header to packet /// </summary> /// <param name="packet">Packet structure</param> /// <param name="sourceIP">Source IP</param> /// <param name="destIP">Destination IP</param> /// <param name="protocol">Protocol</param> /// <returns></returns> private static unsafe IPV4Header *addHeader(NetPacketDesc *packet, byte[] sourceIP, byte[] destIP, byte protocol) { packet->start -= (short)sizeof(IPV4Header); IPV4Header *header = (IPV4Header *)(packet->buffer + packet->start); header->Version = (4 << 4) | 5; header->ServicesField = 0; header->totalLength = Byte.ReverseBytes((ushort)(packet->end - packet->start)); header->ID = Byte.ReverseBytes(0xa836); // TODO: FIX THIS! header->FragmentOffset = 0; header->TTL = 250; header->Protocol = protocol; // The checksum calculation needs to be done with header checksum filled in as zero // then it is filled in later header->HeaderChecksum = 0; Memory.Memcpy(header->Source, Util.ObjectToVoidPtr(sourceIP), 4); Memory.Memcpy(header->Destination, Util.ObjectToVoidPtr(destIP), 4); header->HeaderChecksum = (NetworkTools.Checksum(packet->buffer + packet->start, sizeof(IPV4Header))); return(header); }