Пример #1
0
        public DhcpPacket Deserialize(byte[] bytes)
        {
            var  reader        = new DhcpBinaryReader(bytes);
            var  packetBuilder = DhcpPacketBuilder.Create();
            uint magicCookie;

            try
            {
                packetBuilder.WithOperation((DhcpOperation)reader.ReadValue(DhcpBinaryValue.ByteLength).AsByte());

                var clientHardwareAddressType =
                    (ClientHardwareAddressType)reader.ReadValue(DhcpBinaryValue.ByteLength).AsByte();
                var clientHardwareAddressLength = reader.ReadValue(DhcpBinaryValue.ByteLength).AsByte();

                packetBuilder.WithHops(reader.ReadValue(DhcpBinaryValue.ByteLength).AsByte());
                packetBuilder.WithTransactionId(reader.ReadValue(DhcpBinaryValue.UnsignedInt32Length)
                                                .AsUnsignedInt32());
                packetBuilder.WithSecs(reader.ReadValue(DhcpBinaryValue.UnsignedInt16Length).AsUnsignedInt16());
                packetBuilder.WithBroadcastFlag(
                    reader.ReadValue(DhcpBinaryValue.UnsignedInt16Length).AsUnsignedInt16() == BroadcastFlag);
                packetBuilder.WithClientIp(reader.ReadValue(DhcpBinaryValue.IpAddressLength).AsIpAddress());
                packetBuilder.WithYourIp(reader.ReadValue(DhcpBinaryValue.IpAddressLength).AsIpAddress());
                packetBuilder.WithServerIp(reader.ReadValue(DhcpBinaryValue.IpAddressLength).AsIpAddress());
                packetBuilder.WithGatewayIp(reader.ReadValue(DhcpBinaryValue.IpAddressLength).AsIpAddress());

                var clientHardwareAddressBytes = ReadClientHardwareAddress(reader, clientHardwareAddressLength);

                packetBuilder.WithClientHardwareAddress(clientHardwareAddressType, clientHardwareAddressBytes);

                packetBuilder.WithServerName(reader.ReadValue(64).AsString());
                packetBuilder.WithBootFile(reader.ReadValue(128).AsString());

                magicCookie = reader.ReadValue(DhcpBinaryValue.UnsignedInt32Length).AsUnsignedInt32();

                var options = _optionsSerializer.DeserializeOptions(reader);

                packetBuilder.WithOptions(options);
            }
            catch (InvalidOperationException e)
            {
                throw new DhcpSerializationException("The packet is not a valid DHCP packet.", e);
            }
            catch (IndexOutOfRangeException e)
            {
                throw new DhcpSerializationException("The packet is not a valid DHCP packet.", e);
            }

            if (magicCookie != MagicCookie)
            {
                throw new DhcpSerializationException("The packet does not contain the Magic cookie. It can be a valid BOOTP packet, but it is not a DHCP packet.");
            }

            return(packetBuilder.Build());
        }
        public void SkipUnsupportedOptions()
        {
            var packetBytes = new byte[]
            {
                (byte)DhcpOptionTypeCode.Etherboot,
                2, // length
                1, // If the serializer does not skip this byte, it would try to parse it as a SubnetMask
                0,
                (byte)DhcpOptionTypeCode.TimeOffset,
                4,
                1,
                1,
                1,
                1
            };

            var reader = new DhcpBinaryReader(packetBytes);

            _optionsSerializer.DeserializeOptions(reader);

            Assert.False(reader.CanRead());
        }