Ejemplo n.º 1
0
        public void SetServerIp()
        {
            var packet = DhcpPacketBuilder.Create()
                         .WithGatewayIp(IPAddress.Parse("192.168.1.3"))
                         .Build();

            Assert.Equal(IPAddress.Parse("192.168.1.3"), packet.GatewayIp);
        }
        public void SetClientIp()
        {
            var packet = DhcpPacketBuilder.Create()
                         .WithClientIp(IPAddress.Broadcast)
                         .Build();

            Assert.Equal(IPAddress.Broadcast, packet.ClientIp);
        }
        public void SetSecs()
        {
            var packet = DhcpPacketBuilder.Create()
                         .WithSecs(50)
                         .Build();

            Assert.Equal(50, packet.Secs);
        }
        public void SetTransactionIdFromUint()
        {
            var packet = DhcpPacketBuilder.Create()
                         .WithTransactionId(123456U)
                         .Build();

            Assert.Equal(123456U, packet.TransactionId);
        }
        public void SetIsBroadcast()
        {
            var packet = DhcpPacketBuilder.Create()
                         .WithBroadcastFlag(true)
                         .Build();

            Assert.True(packet.IsBroadcast);
        }
        public void SetHops()
        {
            var packet = DhcpPacketBuilder.Create()
                         .WithHops(50)
                         .Build();

            Assert.Equal(50U, packet.Hops);
        }
        public void SetBootFile()
        {
            var packet = DhcpPacketBuilder.Create()
                         .WithBootFile("BootFile")
                         .Build();

            Assert.Equal("BootFile", packet.BootFile);
        }
        public void SetYourIp()
        {
            var packet = DhcpPacketBuilder.Create()
                         .WithYourIp(IPAddress.Parse("192.168.1.1"))
                         .Build();

            Assert.Equal(IPAddress.Parse("192.168.1.1"), packet.YourIp);
        }
Ejemplo n.º 9
0
        public void ReturnTrue_GivenPacketWhichContainsTheOption()
        {
            var packet = DhcpPacketBuilder.Create()
                         .WithOption(new DhcpTimeOffsetOption(0))
                         .Build();

            Assert.True(packet.HasOption <DhcpTimeOffsetOption>());
        }
        public void SetServerName()
        {
            var packet = DhcpPacketBuilder.Create()
                         .WithServerName("ServerName")
                         .Build();

            Assert.Equal("ServerName", packet.ServerName);
        }
Ejemplo n.º 11
0
        public void ReturnFalse_GivenPacketWhichDoesNotContainTheOption()
        {
            var packet = DhcpPacketBuilder.Create()
                         .WithOption(new DhcpTimeOffsetOption(0))
                         .Build();

            Assert.False(packet.HasOption <DhcpSubnetMaskOption>());
        }
        public void SetOperationType()
        {
            var packet = DhcpPacketBuilder.Create()
                         .WithOperation(DhcpOperation.BootReply)
                         .Build();

            Assert.Equal(DhcpOperation.BootReply, packet.Operation);
        }
Ejemplo n.º 13
0
        public void ThrowInvalidOperationException_GivenOptionTypePacketDoesNotContain()
        {
            var packet = DhcpPacketBuilder.Create()
                         .WithOption(new DhcpTimeOffsetOption(0))
                         .Build();

            Assert.Throws <InvalidOperationException>(
                () => packet.GetOption <DhcpSubnetMaskOption>());
        }
Ejemplo n.º 14
0
        public void SetClientHardwareAddress()
        {
            var packet = DhcpPacketBuilder.Create()
                         .WithClientHardwareAddress(ClientHardwareAddressType.Ethernet, new byte[] { 0xAA, 0x88 })
                         .Build();

            Assert.Equal(ClientHardwareAddressType.Ethernet, packet.ClientHardwareAddress.Type);
            Assert.Equal(new byte[] { 0xAA, 0x88 }, packet.ClientHardwareAddress.AddressBytes);
        }
        public void AddOption()
        {
            var option = new DhcpSubnetMaskOption(IPAddress.Broadcast);

            var packet = DhcpPacketBuilder.Create()
                         .WithOption(option)
                         .Build();

            Assert.True(ReferenceEquals(option, packet.GetOption <DhcpSubnetMaskOption>()));
        }
Ejemplo n.º 16
0
        public void ReturnOption()
        {
            var option = new DhcpTimeOffsetOption(0);

            var packet = DhcpPacketBuilder.Create()
                         .WithOption(option)
                         .Build();

            Assert.Equal(option, packet.GetOption <DhcpTimeOffsetOption>());
        }
Ejemplo n.º 17
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 ReplaceExistingOption()
        {
            var option1 = new DhcpSubnetMaskOption(IPAddress.Broadcast);
            var option2 = new DhcpSubnetMaskOption(IPAddress.Broadcast);

            var packet = DhcpPacketBuilder.Create()
                         .WithOption(option1)
                         .WithOption(option2)
                         .Build();

            Assert.True(ReferenceEquals(option2, packet.GetOption <DhcpSubnetMaskOption>()));
        }
        public void ReplaceExistingOption()
        {
            var updatedOptions = new IDhcpOption[]
            {
                new DhcpTimeOffsetOption(0),
                new DhcpSubnetMaskOption(IPAddress.Broadcast)
            };

            var packet = DhcpPacketBuilder.Create()
                         .WithOption(new DhcpTimeOffsetOption(0))
                         .WithOptions(updatedOptions)
                         .Build();

            Assert.Equal(updatedOptions[0], packet.GetOption <DhcpTimeOffsetOption>());
        }
        public void AddOption()
        {
            var options = new IDhcpOption[]
            {
                new DhcpTimeOffsetOption(0),
                new DhcpSubnetMaskOption(IPAddress.Broadcast),
            };

            var packet = DhcpPacketBuilder.Create()
                         .WithOptions(options)
                         .Build();

            Assert.Equal(options[0], packet.GetOption <DhcpTimeOffsetOption>());
            Assert.Equal(options[1], packet.GetOption <DhcpSubnetMaskOption>());
        }