public DhcpTaggedValueCollectionReader(DhcpBinaryReader reader)
        {
            _reader = reader;

            if (reader.PeekByte() == PadByte)
            {
                reader.Seek(1);
            }
        }
예제 #2
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());
        }
예제 #3
0
        private byte[] ReadClientHardwareAddress(DhcpBinaryReader reader, byte addressLength)
        {
            const byte addressMaxLength = 16;

            var clientHardwareAddress = reader.ReadValue(Math.Min(addressLength, addressMaxLength));

            // Jump over padding bytes of the ClientHardwareAddress
            var paddingLength = addressMaxLength - Math.Min(addressLength, addressMaxLength);

            reader.Seek(paddingLength);

            return(clientHardwareAddress.AsBytes());
        }