public void IPv6Address_Equals() { Random random = new Random(); Byte[] firstAddressBytes = random.NextBytes(16); Byte[] secondAddressBytes = new Byte[16]; firstAddressBytes.CopyTo(secondAddressBytes, 0); IPv6Address firstAddress = IPv6Address.FromByteArray(firstAddressBytes); IPv6Address secondAddress = IPv6Address.FromByteArray(secondAddressBytes); Boolean result = firstAddress.Equals(secondAddress); Assert.True(result); Assert.Equal(firstAddress, secondAddress); Byte[] thirdAddressBytes = random.NextBytes(16); IPv6Address thirdAddress = IPv6Address.FromByteArray(thirdAddressBytes); Boolean nonEqualResult = firstAddress.Equals(thirdAddress); Assert.False(nonEqualResult); Assert.NotEqual(firstAddress, thirdAddress); Assert.True(firstAddress == secondAddress); Assert.False(firstAddress != secondAddress); Assert.False(firstAddress == thirdAddress); Assert.True(firstAddress != thirdAddress); }
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) { String rawValue = (String)reader.Value; if (String.IsNullOrEmpty(rawValue) == true) { return(null); } Byte[] value = Convert.FromBase64String(rawValue); IPv6Address address = IPv6Address.FromByteArray(value); return(address); }
internal static DHCPv6RelayPacket FromByteArray(byte[] rawData, int offset) { DHCPv6PacketTypes type = (DHCPv6PacketTypes)rawData[offset]; offset += 1; Byte hopCount = rawData[offset]; offset += 1; IPv6Address linkAddress = IPv6Address.FromByteArray(rawData, offset); offset += 16; IPv6Address peerAddress = IPv6Address.FromByteArray(rawData, offset); offset += 16; List <DHCPv6PacketOption> options = new List <DHCPv6PacketOption>(); Byte[] innerPacketOptionValue = null; Int32 byteIndex = offset; while (byteIndex < rawData.Length) { UInt16 optionCode = ByteHelper.ConvertToUInt16FromByte(rawData, byteIndex); UInt16 length = ByteHelper.ConvertToUInt16FromByte(rawData, byteIndex + 2); Byte[] optionData = ByteHelper.CopyData(rawData, byteIndex, length + 4); if (optionCode == (Byte)DHCPv6PacketOptionTypes.RelayMessage) { innerPacketOptionValue = ByteHelper.CopyData(optionData, 4); } else { DHCPv6PacketOption suboption = DHCPv6PacketOptionFactory.GetOption(optionCode, optionData); options.Add(suboption); } byteIndex += 4 + length; } return(new DHCPv6RelayPacket( type, hopCount, linkAddress, peerAddress, options, DHCPv6Packet.FromByteArray(innerPacketOptionValue))); }
public static DHCPv6PacketIPAddressOption FromByteArray(Byte[] data, Int32 offset) { if (data == null || data.Length < offset + 4 + _expectedDataLength) { throw new ArgumentException(nameof(data)); } UInt16 code = ByteHelper.ConvertToUInt16FromByte(data, offset); UInt16 length = ByteHelper.ConvertToUInt16FromByte(data, offset + 2); if (length != _expectedDataLength) { throw new ArgumentException(nameof(data)); } IPv6Address address = IPv6Address.FromByteArray(data, offset + 4); return(new DHCPv6PacketIPAddressOption(code, address)); }
public async Task ShouldPacketBeFilterd_No() { DHCPv6RateLimitBasedFilter filter = new DHCPv6RateLimitBasedFilter( Mock.Of <ILogger <DHCPv6RateLimitBasedFilter> >()); Int32 packetAmount = _rand.Next(1000, 10000); IPv6Address serverAddress = IPv6Address.FromByteArray(_rand.NextBytes(16)); for (int i = 0; i < packetAmount; i++) { IPv6Address address = IPv6Address.FromByteArray(_rand.NextBytes(16)); IPv6HeaderInformation header = new IPv6HeaderInformation(address, serverAddress); DHCPv6Packet packet = new DHCPv6Packet(header, 1, DHCPv6PacketTypes.Solicit, Array.Empty <DHCPv6PacketOption>()); Boolean result = await filter.ShouldPacketBeFiltered(packet); Assert.False(result); } }
public static DHCPv6PacketIdentityAssociationAddressSuboption FromByteArray(Byte[] data, Int32 offset) { UInt16 lenght = ByteHelper.ConvertToUInt16FromByte(data, offset + 2); IPv6Address address = IPv6Address.FromByteArray(data, offset + 4); UInt32 preferredLifetime = ByteHelper.ConvertToUInt32FromByte(data, offset + 4 + 16); UInt32 validLifetime = ByteHelper.ConvertToUInt32FromByte(data, offset + 4 + 16 + 4); List <DHCPv6PacketSuboption> suboptions = new List <DHCPv6PacketSuboption>(); if (lenght > 16 + 4 + 4) { Byte[] subOptionsData = ByteHelper.CopyData(data, offset + 4 + 16 + 4 + 4); suboptions.AddRange(DHCPv6PacketSuboptionFactory.GetOptions(subOptionsData)); } return(new DHCPv6PacketIdentityAssociationAddressSuboption( address, TimeSpan.FromSeconds(preferredLifetime), TimeSpan.FromSeconds(validLifetime), suboptions)); }
public static DHCPv6PacketIPAddressListOption FromByteArray(Byte[] data, Int32 offset) { if (data == null || data.Length < offset + 4) { throw new ArgumentException(nameof(data)); } UInt16 code = ByteHelper.ConvertToUInt16FromByte(data, offset); UInt16 length = ByteHelper.ConvertToUInt16FromByte(data, offset + 2); Int32 addressAmount = length / 16; var addresses = new IPv6Address[addressAmount]; for (int i = 0; i < addressAmount; i++) { IPv6Address address = IPv6Address.FromByteArray(data, offset + 4 + (i * 16)); addresses[i] = address; } return(new DHCPv6PacketIPAddressListOption(code, addresses)); }
public async Task ShouldPacketBeFilterd_SameAdressBelowLimit() { DHCPv6RateLimitBasedFilter filter = new DHCPv6RateLimitBasedFilter( Mock.Of <ILogger <DHCPv6RateLimitBasedFilter> >()); UInt16 packetsPerSeconds = (UInt16)_rand.Next(30, 100); filter.PacketsPerSecons = packetsPerSeconds; TimeSpan timePerPacket = TimeSpan.FromSeconds(packetsPerSeconds / 1000.0); Int32 packetAmount = _rand.Next(packetsPerSeconds * 2, packetsPerSeconds * 4); IPv6Address serverAddress = IPv6Address.FromByteArray(_rand.NextBytes(16)); IPv6Address address = IPv6Address.FromByteArray(_rand.NextBytes(16)); for (int i = 0; i < packetAmount; i++) { DateTime start = DateTime.Now; IPv6HeaderInformation header = new IPv6HeaderInformation(address, serverAddress); DHCPv6Packet packet = new DHCPv6Packet(header, 1, DHCPv6PacketTypes.Solicit, Array.Empty <DHCPv6PacketOption>()); Boolean result = await filter.ShouldPacketBeFiltered(packet); Assert.False(result); DateTime end = DateTime.Now; TimeSpan diff = end - start; TimeSpan timeToWait = timePerPacket - diff; if (timeToWait.TotalMilliseconds > 0) { await Task.Delay(timeToWait); } } }
public async Task ShouldPacketBeFilterd_ToManyPackets() { IPv6Address serverAddress = IPv6Address.FromByteArray(_rand.NextBytes(16)); DHCPv6RateLimitBasedFilter filter = new DHCPv6RateLimitBasedFilter( Mock.Of <ILogger <DHCPv6RateLimitBasedFilter> >()); UInt16 packetsPerSeconds = (UInt16)_rand.Next(4, 10); filter.PacketsPerSecons = packetsPerSeconds; TimeSpan timePerPacket = TimeSpan.FromSeconds(packetsPerSeconds / 1000.0); IPv6Address address = IPv6Address.FromByteArray(_rand.NextBytes(16)); Int32 durationInSecods = _rand.Next(3, 10); for (int i = 0; i < durationInSecods; i++) { DateTime tempNow = DateTime.Now; await Task.Delay(1000 - tempNow.Millisecond); Int32 packetAmount = _rand.Next(packetsPerSeconds * 2, packetsPerSeconds * 4); Boolean limitShouldBeExceeded = true; if (_rand.NextDouble() > 0.5) { packetAmount = _rand.Next(1, packetsPerSeconds); limitShouldBeExceeded = false; } for (int j = 0; j < packetAmount; j++) { DateTime start = DateTime.Now; IPv6HeaderInformation header = new IPv6HeaderInformation(address, serverAddress); DHCPv6Packet packet = new DHCPv6Packet(header, 1, DHCPv6PacketTypes.Solicit, Array.Empty <DHCPv6PacketOption>()); Boolean result = await filter.ShouldPacketBeFiltered(packet); if (j > filter.PacketsPerSecons) { if (limitShouldBeExceeded == true) { Assert.True(result); } else { Assert.False(result); } } else { Assert.False(result); } DateTime end = DateTime.Now; TimeSpan diff = end - start; TimeSpan timeToWait = (timePerPacket - diff) - TimeSpan.FromMilliseconds(10); if (timeToWait.TotalMilliseconds > 0) { await Task.Delay(timeToWait); } } } }
public static IPv6Address GetIPv6Address(this Random random) { return(IPv6Address.FromByteArray(random.NextBytes(16))); }
public async Task SeedDatabase(Boolean reset, StorageContext storageContext) { if (reset == true) { { var packets = await storageContext.DHCPv6PacketEntries.AsQueryable().ToListAsync(); var entries = await storageContext.DHCPv6LeaseEntries.AsQueryable().ToListAsync(); storageContext.RemoveRange(packets); storageContext.RemoveRange(entries); } { var packets = await storageContext.DHCPv4PacketEntries.AsQueryable().ToListAsync(); var entries = await storageContext.DHCPv4LeaseEntries.AsQueryable().ToListAsync(); storageContext.RemoveRange(packets); storageContext.RemoveRange(entries); } await storageContext.SaveChangesAsync(); } if (storageContext.DHCPv6PacketEntries.Count() == 0) { DateTime start = DateTime.UtcNow.AddDays(-20); DateTime end = DateTime.UtcNow.AddDays(20); Int32 diff = (Int32)(end - start).TotalMinutes; Random random = new Random(); List <DHCPv6PacketHandledEntryDataModel> dhcpv6PacketEntries = new List <DHCPv6PacketHandledEntryDataModel>(); var requestPacketTypes = new[] { DHCPv6PacketTypes.Solicit, DHCPv6PacketTypes.CONFIRM, DHCPv6PacketTypes.DECLINE, DHCPv6PacketTypes.REBIND, DHCPv6PacketTypes.RELEASE, DHCPv6PacketTypes.RENEW, DHCPv6PacketTypes.REQUEST }; for (int i = 0; i < 30_000; i++) { var request = DHCPv6RelayPacket.AsOuterRelay(new IPv6HeaderInformation(IPv6Address.FromString("fe80::1"), IPv6Address.FromString("fe80::2")), true, 2, IPv6Address.FromString("faf::2"), IPv6Address.FromString("fefc::23"), new DHCPv6PacketOption[] { new DHCPv6PacketRemoteIdentifierOption((UInt32)random.Next(), GetRandomBytes(random)), new DHCPv6PacketByteArrayOption(DHCPv6PacketOptionTypes.InterfaceId, GetRandomBytes(random)), }, DHCPv6RelayPacket.AsInnerRelay(true, 1, IPv6Address.FromString("fe70::2"), IPv6Address.FromString("fecc::23"), new DHCPv6PacketOption[] { new DHCPv6PacketByteArrayOption(DHCPv6PacketOptionTypes.InterfaceId, GetRandomBytes(random)), }, DHCPv6Packet.AsInner( (UInt16)random.Next(0, UInt16.MaxValue), random.NextDouble() > 0.3 ? DHCPv6PacketTypes.Solicit : DHCPv6PacketTypes.RELEASE, new DHCPv6PacketOption[] { new DHCPv6PacketIdentifierOption(DHCPv6PacketOptionTypes.ServerIdentifer, new UUIDDUID(Guid.NewGuid())), new DHCPv6PacketIdentifierOption(DHCPv6PacketOptionTypes.ClientIdentifier, new UUIDDUID(Guid.NewGuid())), new DHCPv6PacketIdentityAssociationNonTemporaryAddressesOption((UInt32)random.Next()), }))); var entry = new DHCPv6PacketHandledEntryDataModel { Id = Guid.NewGuid(), Timestamp = start.AddMinutes(random.Next(0, diff)), ScopeId = Guid.NewGuid(), RequestType = requestPacketTypes[random.Next(0, requestPacketTypes.Length)], RequestSize = request.GetSize(), RequestStream = request.GetAsStream(), RequestDestination = request.Header.Destionation.ToString(), RequestSource = request.Header.Source.ToString(), }; if (random.NextDouble() > 0.8) { entry.FilteredBy = "something"; } else { if (random.NextDouble() > 0.8) { entry.InvalidRequest = true; } else { if (random.NextDouble() > 0.5) { entry.HandledSuccessfully = true; entry.ErrorCode = 0; var response = DHCPv6Packet.AsOuter( new IPv6HeaderInformation(IPv6Address.FromString("fe80::1"), IPv6Address.FromString("fe80::2")), (UInt16)random.Next(0, UInt16.MaxValue), random.NextDouble() > 0.3 ? DHCPv6PacketTypes.REPLY : DHCPv6PacketTypes.ADVERTISE, new DHCPv6PacketOption[] { new DHCPv6PacketIdentifierOption(DHCPv6PacketOptionTypes.ServerIdentifer, new UUIDDUID(Guid.NewGuid())), new DHCPv6PacketIdentifierOption(DHCPv6PacketOptionTypes.ClientIdentifier, new UUIDDUID(Guid.NewGuid())), DHCPv6PacketIdentityAssociationNonTemporaryAddressesOption.AsSuccess( (UInt16)random.Next(0, UInt16.MaxValue), TimeSpan.FromMinutes(random.Next(30, 100)), TimeSpan.FromMinutes(random.Next(30, 100)), IPv6Address.FromString("fe80::100"), TimeSpan.FromMinutes(random.Next(30, 100)), TimeSpan.FromMinutes(random.Next(30, 100))), DHCPv6PacketIdentityAssociationPrefixDelegationOption.AsSuccess((UInt32)random.Next(), TimeSpan.FromMinutes(random.Next(30, 100)), TimeSpan.FromMinutes(random.Next(30, 100)), (Byte)random.Next(30, 68), IPv6Address.FromString("fc:12::0"), TimeSpan.FromMinutes(random.Next(30, 100)), TimeSpan.FromMinutes(random.Next(30, 100))), new DHCPv6PacketBooleanOption(DHCPv6PacketOptionTypes.Auth, random.NextDouble() > 0.5), new DHCPv6PacketByteOption(DHCPv6PacketOptionTypes.Preference, (Byte)random.Next(0, 256)), new DHCPv6PacketTrueOption(DHCPv6PacketOptionTypes.RapitCommit), new DHCPv6PacketIPAddressOption(DHCPv6PacketOptionTypes.ServerUnicast, IPv6Address.FromString("fd::1")), new DHCPv6PacketIPAddressListOption(48, new [] { IPv6Address.FromString("2001::1"), IPv6Address.FromString("2001::1") }), }); entry.ResponseDestination = response.Header.Destionation.ToString(); entry.ResponseSource = response.Header.Source.ToString(); entry.ResponseStream = response.GetAsStream(); entry.ResponseSize = response.GetSize(); entry.ResponseType = random.NextDouble() > 0.3 ? DHCPv6PacketTypes.REPLY : DHCPv6PacketTypes.ADVERTISE; } else { entry.HandledSuccessfully = false; entry.ErrorCode = random.Next(0, 5); } } } entry.SetTimestampDates(); dhcpv6PacketEntries.Add(entry); } List <DHCPv4PacketHandledEntryDataModel> dhcpv4PacketEntries = new(); var requestDHCPv4PacketTypes = new[] { DHCPv4MessagesTypes.Discover, DHCPv4MessagesTypes.Decline, DHCPv4MessagesTypes.Inform, DHCPv4MessagesTypes.Release, DHCPv4MessagesTypes.Request }; for (int i = 0; i < 30_000; i++) { var hwAddress = new Byte[6]; random.NextBytes(hwAddress); var option82Value = new Byte[30]; random.NextBytes(option82Value); var request = new DHCPv4Packet(new IPv4HeaderInformation(IPv4Address.FromString("192.168.0.1"), IPv4Address.FromString("10.10.10.10")), hwAddress, (UInt32)random.Next(), IPv4Address.FromString("0.0.0.0"), IPv4Address.FromString("192.168.0.5"), IPv4Address.FromString("0.0.0.0"), DHCPv4PacketFlags.Unicast, new DHCPv4PacketParameterRequestListOption(new DHCPv4OptionTypes[] { DHCPv4OptionTypes.NetworkTimeProtocolServers, DHCPv4OptionTypes.DNSServers, DHCPv4OptionTypes.Router, DHCPv4OptionTypes.DomainName }), new DHCPv4PacketRawByteOption((Byte)DHCPv4OptionTypes.Option82, option82Value) ); var entry = new DHCPv4PacketHandledEntryDataModel { Id = Guid.NewGuid(), Timestamp = start.AddMinutes(random.Next(0, diff)), ScopeId = Guid.NewGuid(), RequestType = requestDHCPv4PacketTypes[random.Next(0, requestDHCPv4PacketTypes.Length)], RequestSize = request.GetSize(), RequestStream = request.GetAsStream(), RequestDestination = request.Header.Destionation.ToString(), RequestSource = request.Header.Source.ToString(), }; if (random.NextDouble() > 0.8) { entry.FilteredBy = "something"; } else { if (random.NextDouble() > 0.8) { entry.InvalidRequest = true; } else { if (random.NextDouble() > 0.5) { entry.HandledSuccessfully = true; entry.ErrorCode = 0; var response = new DHCPv4Packet(new IPv4HeaderInformation(IPv4Address.FromString("10.10.10.10"), IPv4Address.FromString("192.168.0.1")), hwAddress, (UInt32)random.Next(), IPv4Address.FromString("0.0.0.0"), IPv4Address.FromString("192.168.0.5"), IPv4Address.FromString("192.168.0.15"), DHCPv4PacketFlags.Unicast, new DHCPv4PacketAddressListOption(DHCPv4OptionTypes.DNSServers, new[] { IPv4Address.FromString("1.1.1.1"), IPv4Address.FromString("8.8.8.8") }), new DHCPv4PacketAddressOption(DHCPv4OptionTypes.Router, IPv4Address.FromString("192.168.0.253")) ); entry.ResponseDestination = response.Header.Destionation.ToString(); entry.ResponseSource = response.Header.Source.ToString(); entry.ResponseStream = response.GetAsStream(); entry.ResponseSize = response.GetSize(); entry.ResponseType = random.NextDouble() > 0.3 ? DHCPv4MessagesTypes.Offer : DHCPv4MessagesTypes.Acknowledge; } else { entry.HandledSuccessfully = false; entry.ErrorCode = random.Next(0, 5); } } } entry.SetTimestampDates(); dhcpv4PacketEntries.Add(entry); } List <DHCPv6LeaseEntryDataModel> dhcpv6LeaseEntries = new List <DHCPv6LeaseEntryDataModel>(); for (int i = 0; i < 30_000; i++) { Byte[] addressBytes = new byte[16]; Byte[] prefixBytes = new byte[16]; random.NextBytes(addressBytes); random.NextBytes(prefixBytes); DHCPv6LeaseEntryDataModel entryDataModel = new DHCPv6LeaseEntryDataModel { Id = Guid.NewGuid(), Timestamp = start.AddMinutes(random.Next(0, diff)), LeaseId = Guid.NewGuid(), Address = IPv6Address.FromByteArray(addressBytes).ToString(), EndReason = StatisticsControllerResponses.V1.ReasonToEndLease.Nothing, ScopeId = Guid.NewGuid(), Start = start.AddMinutes(random.Next(0, diff - 50)), }; Int32 leaseDiff = (Int32)(end.AddDays(4) - entryDataModel.Start).TotalMinutes; entryDataModel.End = entryDataModel.Start.AddMinutes(random.Next(10, leaseDiff)); TimeSpan lifetime = entryDataModel.End - entryDataModel.Start; TimeSpan renewalTime = lifetime / 2; TimeSpan rebindingTime = lifetime * (2.0 / 3.0); entryDataModel.EndOfRenewalTime = entryDataModel.Start + renewalTime; entryDataModel.EndOfPreferredLifetime = entryDataModel.Start + rebindingTime; if (random.NextDouble() > 0.5) { entryDataModel.Prefix = IPv6Address.FromByteArray(prefixBytes).ToString(); entryDataModel.PrefixLength = (Byte)random.Next(48, 76); } dhcpv6LeaseEntries.Add(entryDataModel); } List <DHCPv4LeaseEntryDataModel> dhcpv4LeaseEntries = new(); for (int i = 0; i < 30_000; i++) { Byte[] addressBytes = new byte[4]; random.NextBytes(addressBytes); DHCPv4LeaseEntryDataModel entryDataModel = new DHCPv4LeaseEntryDataModel { Id = Guid.NewGuid(), Timestamp = start.AddMinutes(random.Next(0, diff)), LeaseId = Guid.NewGuid(), Address = IPv4Address.FromByteArray(addressBytes).ToString(), EndReason = StatisticsControllerResponses.V1.ReasonToEndLease.Nothing, ScopeId = Guid.NewGuid(), Start = start.AddMinutes(random.Next(0, diff - 50)), }; Int32 leaseDiff = (Int32)(end.AddDays(4) - entryDataModel.Start).TotalMinutes; entryDataModel.End = entryDataModel.Start.AddMinutes(random.Next(10, leaseDiff)); TimeSpan lifetime = entryDataModel.End - entryDataModel.Start; TimeSpan renewalTime = lifetime / 2; TimeSpan rebindingTime = lifetime * (2.0 / 3.0); entryDataModel.EndOfRenewalTime = entryDataModel.Start + renewalTime; entryDataModel.EndOfPreferredLifetime = entryDataModel.Start + rebindingTime; dhcpv4LeaseEntries.Add(entryDataModel); } storageContext.AddRange(dhcpv6PacketEntries); storageContext.AddRange(dhcpv6LeaseEntries); storageContext.AddRange(dhcpv4PacketEntries); storageContext.AddRange(dhcpv4LeaseEntries); storageContext.SaveChanges(); } }