Exemple #1
0
        /// <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);
        }