protected void CheckPacketOptions(Guid scopeId, DHCPv4RootScope rootScope, DHCPv4Packet result) { var scope = rootScope.GetScopeById(scopeId); if (scope.Properties != null) { foreach (var item in scope.Properties.Properties) { } } var subnetOption = result.GetOptionByIdentifier((Byte)DHCPv4OptionTypes.SubnetMask) as DHCPv4PacketAddressOption; if (result.YourIPAdress == IPv4Address.Empty) { Assert.Null(subnetOption); } else { Assert.NotNull(subnetOption); Assert.True(ByteHelper.AreEqual(subnetOption.Address.GetBytes(), scope.AddressRelatedProperties.Mask.GetBytes())); } }
public void TestInform_CheckReponsePacket() { Random random = new Random(); IPv4Address start = random.GetIPv4Address(); IPv4Address end = random.GetIPv4AddressGreaterThan(start); IPv4Address sourceAddress = random.GetIPv4AddressBetween(start, end); IPv4Address destinationAddress = random.GetIPv4AddressBetween(start, end); DHCPv4Packet input = GetInformPacket(random, sourceAddress, destinationAddress); List <DHCPv4ScopeProperty> scopeProperties = random.GenerateProperties(new List <DHCPv4OptionTypes> { DHCPv4OptionTypes.RebindingTimeValue, DHCPv4OptionTypes.RenewalTimeValue, DHCPv4OptionTypes.IPAddressLeaseTime, DHCPv4OptionTypes.ServerIdentifier, DHCPv4OptionTypes.MessageType, }); DHCPv4RootScope rootScope = GetRootScope(); Guid scopeId = Guid.NewGuid(); rootScope.Load(new List <DomainEvent> { new DHCPv4ScopeAddedEvent(new DHCPv4ScopeCreateInstruction { AddressProperties = new DHCPv4ScopeAddressProperties(start, end, new List <IPv4Address>(), informsAreAllowd: true, renewalTime: TimeSpan.FromMinutes(random.Next(3, 10)), preferredLifetime: TimeSpan.FromMinutes(random.Next(20, 30)), leaseTime: TimeSpan.FromMinutes(random.Next(40, 60)) ), Id = scopeId, ScopeProperties = new DHCPv4ScopeProperties(scopeProperties) }) }); DHCPv4Packet result = rootScope.HandleInform(input); Assert.NotEqual(DHCPv4Packet.Empty, result); Assert.Equal(sourceAddress, result.Header.Destionation); Assert.Equal(destinationAddress, result.Header.Source); Assert.Equal(DHCPv4MessagesTypes.Acknowledge, result.MessageType); Assert.Equal(input.ClientHardwareAddress, result.ClientHardwareAddress); Assert.Equal(input.HardwareAddressLength, result.HardwareAddressLength); Assert.Equal(input.HardwareType, result.HardwareType); Assert.Equal(0, result.Hops); Assert.Equal(0, result.SecondsElapsed); Assert.Equal(input.TransactionId, result.TransactionId); Assert.Equal(String.Empty, result.FileName); Assert.Equal(String.Empty, result.ServerHostname); Assert.Equal(DHCPv4PacketFlags.Unicast, result.Flags); Assert.Equal(DHCPv4PacketOperationCodes.BootReply, result.OpCode); Assert.Equal(IPv4Address.Empty, result.GatewayIPAdress); Assert.Equal(IPv4Address.Empty, result.YourIPAdress); Assert.Equal(IPv4Address.Empty, result.ServerIPAdress); Assert.Equal(input.ClientIPAdress, result.ClientIPAdress); Assert.Equal(input.ClientIPAdress, result.Header.Destionation); // no time values Assert.False(IsOptionPresentend(result, DHCPv4OptionTypes.RebindingTimeValue)); Assert.False(IsOptionPresentend(result, DHCPv4OptionTypes.RenewalTimeValue)); Assert.False(IsOptionPresentend(result, DHCPv4OptionTypes.IPAddressLeaseTime)); // server identifier Assert.True(IsOptionPresentend(result, DHCPv4OptionTypes.ServerIdentifier)); Assert.True(HasOptionThisIPv4Adress(result, DHCPv4OptionTypes.ServerIdentifier, destinationAddress)); foreach (var item in scopeProperties) { Assert.True(IsOptionPresentend(result, item.OptionIdentifier)); DHCPv4PacketOption existingOption = result.GetOptionByIdentifier(item.OptionIdentifier); Assert.NotEqual(DHCPv4PacketOption.NotPresented, existingOption); ChecKIfPropertyCorrelatesToOption(item, existingOption); } }
public async Task SendAndReceive() { Random random = new Random(); String dbName = $"{random.Next()}"; String eventStorePrefix = random.GetAlphanumericString(); var serviceInteractions = GetTestClient(dbName, eventStorePrefix); Byte[] opt82Value = random.NextBytes(10); try { //Get and Add Interface var interfacesResult = await serviceInteractions.GetAsync("/api/interfaces/dhcpv4/"); var interfaces = await IsObjectResult <DHCPv4InterfaceOverview>(interfacesResult); DHCPv4InterfaceEntry usedInterface = null; foreach (var item in interfaces.Entries) { try { var createInterfaceResult = await serviceInteractions.PostAsync("/api/interfaces/dhcpv4/", GetContent(new { name = "my test interface", ipv4Address = item.IPv4Address, interfaceId = item.PhysicalInterfaceId })); Guid interfaceDaAPIId = await IsObjectResult <Guid>(createInterfaceResult); Assert.NotEqual(Guid.Empty, interfaceDaAPIId); usedInterface = item; break; } catch (Exception) { continue; } } Assert.NotNull(usedInterface); var interfaceAddress = IPv4Address.FromString(usedInterface.IPv4Address); //Create scope var createScopeResult = await serviceInteractions.PostAsync("/api/scopes/dhcpv4/", GetContent(new { name = "Testscope", id = Guid.NewGuid(), addressProperties = new { start = "192.168.10.1", end = "192.168.10.254", excludedAddresses = new[] { "192.168.10.1" }, preferredLifetime = TimeSpan.FromDays(0.8), leaseTime = TimeSpan.FromDays(1), renewalTime = TimeSpan.FromDays(0.5), maskLength = 24, supportDirectUnicast = true, acceptDecline = true, informsAreAllowd = true, reuseAddressIfPossible = true, addressAllocationStrategy = Beer.DaAPI.Shared.Requests.DHCPv4ScopeRequests.V1.DHCPv4ScopeAddressPropertyReqest.AddressAllocationStrategies.Next }, resolver = new { typename = nameof(DHCPv4Option82Resolver), propertiesAndValues = new Dictionary <String, String> { { nameof(DHCPv4Option82Resolver.Value), System.Text.Json.JsonSerializer.Serialize(opt82Value) }, } } })); Guid scopeId = await IsObjectResult <Guid>(createScopeResult); Assert.NotEqual(Guid.Empty, scopeId); IPAddress address = new IPAddress(interfaceAddress.GetBytes()); IPEndPoint ownEndPoint = new IPEndPoint(address, 68); IPEndPoint serverEndPoint = new IPEndPoint(address, 67); UdpClient client = new UdpClient(ownEndPoint); IPv4HeaderInformation headerInformation = new IPv4HeaderInformation(IPv4Address.FromString(address.ToString()), IPv4Address.Broadcast); DHCPv4Packet discoverPacket = new DHCPv4Packet( headerInformation, random.NextBytes(6), (UInt32)random.Next(), IPv4Address.Empty, IPv4Address.Empty, IPv4Address.Empty, DHCPv4PacketFlags.Broadcast, new DHCPv4PacketMessageTypeOption(DHCPv4MessagesTypes.Discover), new DHCPv4PacketRawByteOption(82, opt82Value), new DHCPv4PacketClientIdentifierOption(DHCPv4ClientIdentifier.FromIdentifierValue("my custom client"))); byte[] discoverPacketStream = discoverPacket.GetAsStream(); await client.SendAsync(discoverPacketStream, discoverPacketStream.Length, serverEndPoint); await Task.Delay(2000); var content = await client.ReceiveAsync(); Byte[] receivedBytes = content.Buffer; DHCPv4Packet response = DHCPv4Packet.FromByteArray(receivedBytes, new IPv4HeaderInformation(interfaceAddress, interfaceAddress)); Assert.NotNull(response); Assert.True(response.IsValid); var serverIdentifierOption = response.GetOptionByIdentifier(DHCPv4OptionTypes.ServerIdentifier) as DHCPv4PacketAddressOption; Assert.NotNull(serverIdentifierOption); Assert.Equal(interfaceAddress, serverIdentifierOption.Address); var subnetOption = response.GetOptionByIdentifier(DHCPv4OptionTypes.SubnetMask) as DHCPv4PacketAddressOption; Assert.NotNull(subnetOption); Assert.Equal(IPv4Address.FromString("255.255.255.0"), subnetOption.Address); var clientIdentifierOption = response.GetOptionByIdentifier(DHCPv4OptionTypes.ClientIdentifier) as DHCPv4PacketClientIdentifierOption; Assert.NotNull(clientIdentifierOption); Assert.Equal("my custom client", clientIdentifierOption.Identifier.IdentifierValue); Assert.Equal(DUID.Empty, clientIdentifierOption.Identifier.DUID); Assert.Equal((UInt32)0, clientIdentifierOption.Identifier.IaId); Assert.Empty(clientIdentifierOption.Identifier.HwAddress); var incoming82Option = response.GetOptionByIdentifier(DHCPv4OptionTypes.Option82) as DHCPv4PacketRawByteOption; Assert.NotNull(incoming82Option); Assert.Equal(opt82Value, incoming82Option.OptionData); } finally { await EventStoreClientDisposer.CleanUp(eventStorePrefix, null); await DatabaseTestingUtility.DeleteDatabase(dbName); } }