Пример #1
0
        private byte[] GetHeaderBytes()
        {
            //TODO - the following things don't work (because of endianism)
            // Identification + Flags + Fragment Offset
            // Header Checksum

            List <byte>   bytes  = new List <byte>();
            List <UInt32> fields = new List <UInt32>();

            //Add the packetsections together into 32-bit words
            //UInt32 field1 = 0;
            UInt32 xVersion       = (UInt32)(this.Version << 28);
            UInt32 xHeaderLength  = (UInt32)(this.HeaderLength << 24);
            UInt32 xTypeOfService = (UInt32)(this.TypeOfService << 16);
            UInt32 xTotalLength   = (UInt32)(this.TotalLength << 0);

            //field1 = ;
            fields.Add(HostToNetwork(xVersion + xHeaderLength + xTypeOfService + xTotalLength));

            //UInt32 field2 = (UInt32)((this.Identification << 16) | ((byte)(this.FragmentFlags)) << 12 | (this.FragmentOffset << 1));
            UInt32 field2   = 0;
            UInt32 Identity = (UInt32)(this.Identification << 16);
            UInt32 Flags    = (UInt32)((byte)(this.FragmentFlags)) << 14;
            UInt32 Offset   = (UInt32)((this.FragmentOffset) & (UInt16)0xFF);

            field2 = Identity + Flags + Offset;
            fields.Add(HostToNetwork(field2));

            //UInt32 field3 = (UInt32)((this.TimeToLive << 0) | (((byte)(this.Protocol)) << 8) | (UInt16)(this.HeaderChecksum << 16));
            UInt32 xTimeToLive     = (UInt32)(this.TimeToLive << 24);
            UInt32 xProtocol       = (UInt32)((byte)this.Protocol << 16);
            UInt32 xHeaderChecksum = (UInt32)(this.HeaderChecksum << 0);

            //Console.WriteLine("Field3: " + field3.ToBinary(32));
            fields.Add(HostToNetwork(xTimeToLive + xProtocol + xHeaderChecksum));

            //Split the 32-bit words into bytes
            for (int i = 0; i < fields.Count; i++)
            {
                bytes.Add((byte)(fields[i] >> 0));
                bytes.Add((byte)(fields[i] >> 8));
                bytes.Add((byte)(fields[i] >> 16));
                bytes.Add((byte)(fields[i] >> 24));
            }

            //Source and Destination
            foreach (byte b in SourceAddress.ToByteArray())
            {
                bytes.Add(b);
            }

            foreach (byte b in DestinationAddress.ToByteArray())
            {
                bytes.Add(b);
            }

            //TODO - Options field

            return(bytes.ToArray());
        }