public static DhcpPacket CreateFromRaw(byte[] rawPacket) { DhcpPacket packet = new DhcpPacket(); using (var reader = new BinaryReader(new MemoryStream(rawPacket))) { packet.MessageType = (DhcpMessageType)reader.ReadByte(); HardwareAddressType hardwareAddressType = (HardwareAddressType)reader.ReadByte(); byte hardwareAddressLength = reader.ReadByte(); packet.Hops = reader.ReadByte(); packet.TransactionId = (uint)IPAddress.NetworkToHostOrder(reader.ReadInt32()); packet.SecondsElapsed = (ushort)IPAddress.NetworkToHostOrder(reader.ReadInt16()); packet.Flags = (ushort)IPAddress.NetworkToHostOrder(reader.ReadInt16()); packet.ClientAddress = new IPAddress(reader.ReadBytes(4)); packet.YourClientAddress = new IPAddress(reader.ReadBytes(4)); packet.NextServerAddress = new IPAddress(reader.ReadBytes(4)); packet.RelayAgentAddress = new IPAddress(reader.ReadBytes(4)); packet.HardwareAddress = new HardwareAddress(hardwareAddressType, reader.ReadBytes(hardwareAddressLength)); reader.ReadBytes(16 - hardwareAddressLength); // padding packet.ServerName = Encoding.ASCII.GetString(reader.ReadBytes(64)); packet.BootFileName = Encoding.ASCII.GetString(reader.ReadBytes(128)); int packetMagicCookie = IPAddress.NetworkToHostOrder(reader.ReadInt32()); if (packetMagicCookie != MagicCookie) { throw new InvalidDataException("Magic cookie not found!"); } byte messageType = reader.ReadByte(); if (messageType != 53) { throw new InvalidDataException("Options should start with Message type!"); } reader.ReadByte(); // length packet.MessageType = (DhcpMessageType)reader.ReadByte(); var optionFactory = new DhcpOptionFactory(); byte optionType = reader.ReadByte(); while (optionType != 255) { DhcpOptionType type = (DhcpOptionType)optionType; byte length = reader.ReadByte(); byte[] optionData = reader.ReadBytes(length); DhcpOption option = optionFactory.CreateFromRaw(type, optionData); if (option != null) { packet.Options.Add(option); } else { Console.WriteLine($"WARN: Unsupported option type: {type}"); } optionType = reader.ReadByte(); } } return(packet); }
public void TryGetShouldReturnNullWhenDataNotValid() { byte[] option = new byte[] { 255, 3, 65, 66 }; var factory = new DhcpOptionFactory(); var result = factory.TryGetOption(option); Assert.Null(result); }
public void ShouldThrowWhenOptionLengthIncorrect2() { byte[] option = new byte[] { 255, 1, 65, 66 }; var factory = new DhcpOptionFactory(); Assert.Throws <ArgumentException>(() => { var result = factory.GetOption(option); }); }
public void ShouldThrowWhenMessageTypeOptionLengthIncorrect() { byte[] option = new byte[] { 53, 2, 3, 5 }; var factory = new DhcpOptionFactory(); Assert.Throws <ArgumentException>(() => { var result = factory.GetOption(option) as DhcpMessageTypeOption; }); }
public void TryGetReturnsValidRequestedIpOption() { byte[] option = new byte[] { 0x32, 0x4, 0xc0, 0xa8, 0x01, 0x07 }; var factory = new DhcpOptionFactory(); var result = factory.TryGetOption(option) as DhcpRequestedIpAddressOption; Assert.NotNull(result); Assert.Equal(IPAddress.Parse("192.168.1.7"), result.RequestedIp); }
public void TryGetReturnsValidOption() { byte[] option = new byte[] { 53, 2, 65, 66 }; var factory = new DhcpOptionFactory(); var result = factory.TryGetOption(option); Assert.NotNull(result); Assert.Equal("AB", result.GetStringValue()); }
public void CreatesGenericDhcpOption() { byte[] option = new byte[] { 255, 2, 65, 66 }; var factory = new DhcpOptionFactory(); var result = factory.GetOption(option); Assert.Equal(255, result.Type); Assert.Equal(2, result.Length); Assert.Equal(new byte[] { 65, 66 }, result.Value); Assert.Equal("AB", result.GetStringValue()); }
public void CreatesMessagesTypeOption() { byte[] option = new byte[] { 53, 1, 3 }; var factory = new DhcpOptionFactory(); var result = factory.GetOption(option) as DhcpMessageTypeOption; Assert.NotNull(result); //verifies the returned type is correct. Assert.Equal(53, result.Type); Assert.Equal(1, result.Length); Assert.Equal(DhcpMessageType.Request, result.MessageType); }
public void CreatesVendorClassIdOption() { byte[] option = new byte[] { 60, 6, 65, 66, 67, 68, 69, 70 }; var factory = new DhcpOptionFactory(); var result = factory.GetOption(option) as DhcpVendorClassIdentifierOption; Assert.NotNull(result); //verifies the returned type is correct. Assert.Equal(60, result.Type); Assert.Equal(6, result.Length); Assert.Equal("ABCDEF", result.VendorClassId); }
public void CreatesDhcpHostnameOption() { byte[] option = new byte[] { 12, 6, 65, 66, 67, 68, 69, 70 }; var factory = new DhcpOptionFactory(); var result = factory.GetOption(option) as DhcpHostnameOption; Assert.NotNull(result); //verifies the returned type is correct. Assert.Equal(12, result.Type); Assert.Equal(6, result.Length); Assert.Equal("ABCDEF", result.HostName); }
public void CreatesDhcpClientIdOption() { byte[] option = new byte[] { 61, 7, 1, 0x8c, 0x70, 0x5a, 1, 2, 3 }; var factory = new DhcpOptionFactory(); var result = factory.GetOption(option) as DhcpClientIdentifierOption; Assert.NotNull(result); //verifies the returned type is correct. Assert.Equal(61, result.Type); Assert.Equal(7, result.Length); Assert.Equal(ArpHardwareType.Ethernet, result.HardwareType); Assert.Equal(new byte[] { 0x8c, 0x70, 0x5a, 1, 2, 3 }, result.ClientMACAddress.GetAddressBytes()); }