Exemplo n.º 1
0
        public void GetMaskBytes(Byte identiifer, Byte[] expectedMask)
        {
            IPv6SubnetMask mask = new IPv6SubnetMask(new IPv6SubnetMaskIdentifier(identiifer));

            Assert.Equal(identiifer, mask.Identifier);
            Assert.Equal(expectedMask, mask.GetMaskBytes());
        }
Exemplo n.º 2
0
        public void ApplyValues()
        {
            String ipAddress        = "fe80::";
            Byte   subnetmaskLength = 32;

            IPv6Address    address = IPv6Address.FromString(ipAddress);
            IPv6SubnetMask mask    = new IPv6SubnetMask(new IPv6SubnetMaskIdentifier(subnetmaskLength));

            Mock <ISerializer> serializerMock = new Mock <ISerializer>(MockBehavior.Strict);

            serializerMock.Setup(x => x.Deserialze <IPv6Address>(ipAddress)).Returns(address).Verifiable();
            serializerMock.Setup(x => x.Deserialze <IPv6SubnetMask>(subnetmaskLength.ToString())).Returns(mask).Verifiable();

            var resolver = new DHCPv6RelayAgentSubnetResolver();

            resolver.ApplyValues(new Dictionary <String, String> {
                { "NetworkAddress", ipAddress },
                { "SubnetMask", subnetmaskLength.ToString() },
            }, serializerMock.Object);

            Assert.Equal(address, resolver.NetworkAddress);
            Assert.Equal(mask, resolver.SubnetMask);

            serializerMock.Verify();

            var expectedValues = new Dictionary <String, String>
            {
                { "NetworkAddress", ipAddress },
                { "SubnetMask", subnetmaskLength.ToString() },
            };

            Assert.Equal(expectedValues.ToArray(), resolver.GetValues().ToArray());
        }
Exemplo n.º 3
0
        public void PacketMeetsCondition(Boolean isInSubnet)
        {
            Random random = new Random();

            String ipAddress        = "fe80::0";
            Byte   subnetmaskLength = 32;

            IPv6Address    address = IPv6Address.FromString(ipAddress);
            IPv6SubnetMask mask    = new IPv6SubnetMask(new IPv6SubnetMaskIdentifier(subnetmaskLength));

            Mock <ISerializer> serializerMock = new Mock <ISerializer>(MockBehavior.Strict);

            serializerMock.Setup(x => x.Deserialze <IPv6Address>(ipAddress)).Returns(address).Verifiable();
            serializerMock.Setup(x => x.Deserialze <IPv6SubnetMask>(subnetmaskLength.ToString())).Returns(mask).Verifiable();

            DHCPv6RelayAgentSubnetResolver resolver = new DHCPv6RelayAgentSubnetResolver();

            resolver.ApplyValues(new Dictionary <String, String> {
                { "NetworkAddress", ipAddress },
                { "SubnetMask", subnetmaskLength.ToString() },
            }, serializerMock.Object);

            var packet = DHCPv6RelayPacket.AsOuterRelay(new IPv6HeaderInformation(IPv6Address.FromString("fe80::1"), IPv6Address.FromString("fe80::2")),
                                                        true, 1, random.GetIPv6Address(), random.GetIPv6Address(), Array.Empty <DHCPv6PacketOption>(), DHCPv6RelayPacket.AsInnerRelay(
                                                            true, 0, isInSubnet == true ? IPv6Address.FromString("fe80::1") : IPv6Address.FromString("2004::1"), IPv6Address.FromString("fe80::1"), new DHCPv6PacketOption[]
            {
            }, DHCPv6Packet.AsInner(random.NextUInt16(), DHCPv6PacketTypes.Solicit, Array.Empty <DHCPv6PacketOption>())));

            Boolean result = resolver.PacketMeetsCondition(packet);

            Assert.Equal(isInSubnet, result);

            serializerMock.Verify();
        }
Exemplo n.º 4
0
 internal DHCPv6PrefixDelegation(IPv6Address address, Byte length, UInt32 identityAssociation, DateTime started)
 {
     NetworkAddress      = address;
     Mask                = new IPv6SubnetMask(new IPv6SubnetMaskIdentifier(length));
     IdentityAssociation = identityAssociation;
     Started             = started;
 }
Exemplo n.º 5
0
        public void IsIPv6AdressANetworkAddress(String ipv6Adress, Byte identiifer, Boolean expectedResult)
        {
            IPv6SubnetMask mask = new IPv6SubnetMask(new IPv6SubnetMaskIdentifier(identiifer));

            Boolean actual = mask.IsIPv6AdressANetworkAddress(IPv6Address.FromString(ipv6Adress));

            Assert.Equal(expectedResult, actual);
        }
Exemplo n.º 6
0
        public void IsAddressInSubnet(String networkAddress, Byte length, String address, Boolean expectedResult)
        {
            IPv6Address    parsedNetworkAddress = IPv6Address.FromString(networkAddress);
            IPv6SubnetMask mask          = new IPv6SubnetMask(new IPv6SubnetMaskIdentifier(length));
            IPv6Address    parsedAddress = IPv6Address.FromString(address);
            Boolean        actual        = mask.IsAddressInSubnet(parsedNetworkAddress, parsedAddress);

            Assert.Equal(expectedResult, actual);
        }
Exemplo n.º 7
0
        public void FromValues_Failed_AddressNotMatch()
        {
            Random random = new Random();

            IPv6Address    address = IPv6Address.FromString("2001:e68:5423:3a2b:716b:ef6e:b215:fb96");
            IPv6SubnetMask mask    = new IPv6SubnetMask(new IPv6SubnetMaskIdentifier(64));
            UInt32         id      = random.NextUInt32();

            Assert.ThrowsAny <Exception>(() => DHCPv6PrefixDelegation.FromValues
                                             (address, mask, id));
        }
Exemplo n.º 8
0
        public void SerialzeAndDeserilize()
        {
            JSONBasedSerializer serializer = new JSONBasedSerializer();

            for (Byte i = 0; i <= 128; i++)
            {
                IPv6SubnetMask mask = new IPv6SubnetMask(new IPv6SubnetMaskIdentifier(i));

                var serilizedValue = serializer.Seralize(mask);
                Assert.Equal($"\"{i}\"", serilizedValue);
                IPv6SubnetMask actual = serializer.Deserialze <IPv6SubnetMask>(serilizedValue);

                Assert.Equal(mask, actual);
            }
        }
Exemplo n.º 9
0
        public static DHCPv6PrefixDelegation FromValues(
            IPv6Address address, IPv6SubnetMask mask, UInt32 identityAssociation)
        {
            if (mask.IsIPv6AdressANetworkAddress(address) == false)
            {
                throw new ArgumentException(nameof(address));
            }

            return(new DHCPv6PrefixDelegation
            {
                IdentityAssociation = identityAssociation,
                Mask = mask,
                NetworkAddress = address,
            });
        }
Exemplo n.º 10
0
        public PrefixBinding(IPv6Address prefix, IPv6SubnetMask mask, IPv6Address host)
        {
            if (mask.IsIPv6AdressANetworkAddress(prefix) == false)
            {
                throw new ArgumentException("");
            }

            if (mask.IsAddressInSubnet(prefix, host) == true)
            {
                throw new ArgumentException();
            }

            Prefix = prefix;
            Mask   = mask;
            Host   = host;
        }
Exemplo n.º 11
0
        public void FromValues()
        {
            Random random = new Random();

            IPv6Address    address = IPv6Address.FromString("fe90::0");
            IPv6SubnetMask mask    = new IPv6SubnetMask(new IPv6SubnetMaskIdentifier(64));
            UInt32         id      = random.NextUInt32();

            DHCPv6PrefixDelegation value = DHCPv6PrefixDelegation.FromValues
                                               (address, mask, id);

            Assert.NotNull(value);
            Assert.Equal(address, value.NetworkAddress);
            Assert.Equal(64, value.Mask.Identifier);
            Assert.Equal(id, value.IdentityAssociation);
        }
Exemplo n.º 12
0
        public static DHCPv6PrefixDelgationInfo FromValues(
            IPv6Address prefix, IPv6SubnetMaskIdentifier prefixLength, IPv6SubnetMaskIdentifier assignedPrefixLength)
        {
            if (prefixLength.Value > assignedPrefixLength.Value)
            {
                throw new ArgumentException();
            }

            IPv6SubnetMask mask = new IPv6SubnetMask(prefixLength);

            if (mask.IsIPv6AdressANetworkAddress(prefix) == false)
            {
                throw new ArgumentException();
            }

            return(new DHCPv6PrefixDelgationInfo(prefix, prefixLength, assignedPrefixLength));
        }
Exemplo n.º 13
0
        public void Constructor(String prefix, Byte prefixLength, String host, Boolean expectedResult)
        {
            IPv6Address    prefixAddress = IPv6Address.FromString(prefix);
            IPv6Address    hostAddress   = IPv6Address.FromString(host);
            IPv6SubnetMask mask          = new IPv6SubnetMask(new IPv6SubnetMaskIdentifier(prefixLength));

            if (expectedResult == true)
            {
                var binding = new PrefixBinding(prefixAddress, mask, hostAddress);
                Assert.Equal(prefixAddress, binding.Prefix);
                Assert.Equal(hostAddress, binding.Host);
                Assert.Equal(mask, binding.Mask);
            }
            else
            {
                Assert.ThrowsAny <Exception>(() => new PrefixBinding(prefixAddress, mask, hostAddress));
            }
        }
Exemplo n.º 14
0
        public Boolean ArePropertiesAndValuesValid(IDictionary <String, String> valueMapper, ISerializer serializer)
        {
            if (valueMapper.ContainsKeys(new[] { nameof(NetworkAddress), nameof(SubnetMask) }) == false)
            {
                return(false);
            }

            try
            {
                IPv6Address    networkAddress = serializer.Deserialze <IPv6Address>(valueMapper[nameof(NetworkAddress)]);
                IPv6SubnetMask mask           = serializer.Deserialze <IPv6SubnetMask>(valueMapper[nameof(SubnetMask)]);

                return(mask.IsIPv6AdressANetworkAddress(networkAddress));
            }
            catch (Exception)
            {
                return(false);
            }
        }
Exemplo n.º 15
0
        public void ArePropertiesAndValuesValid(String ipAddress, Byte subnetmaskLength, Boolean shouldBeValid)
        {
            IPv6Address    address = IPv6Address.FromString(ipAddress);
            IPv6SubnetMask mask    = new IPv6SubnetMask(new IPv6SubnetMaskIdentifier(subnetmaskLength));

            Mock <ISerializer> serializerMock = new Mock <ISerializer>(MockBehavior.Strict);

            serializerMock.Setup(x => x.Deserialze <IPv6Address>(ipAddress)).Returns(address).Verifiable();
            serializerMock.Setup(x => x.Deserialze <IPv6SubnetMask>(subnetmaskLength.ToString())).Returns(mask).Verifiable();

            var     resolver = new DHCPv6RelayAgentSubnetResolver();
            Boolean actual   = resolver.ArePropertiesAndValuesValid(new Dictionary <String, String> {
                { "NetworkAddress", ipAddress },
                { "SubnetMask", subnetmaskLength.ToString() },
            }, serializerMock.Object);

            Assert.Equal(shouldBeValid, actual);

            serializerMock.Verify();
        }
Exemplo n.º 16
0
        internal IPv6Address GetNextPrefix(IEnumerable <IPv6Address> used)
        {
            IList <IPv6Address> sorted = used.OrderBy(x => x).ToList();

            if (sorted.Count == 0)
            {
                return(IPv6Address.FromAddress(PrefixDelgationInfo.Prefix));
            }

            Double max = Math.Pow(2, PrefixDelgationInfo.AssignedPrefixLength - PrefixDelgationInfo.PrefixLength);

            if (sorted.Count == max)
            {
                return(IPv6Address.Empty);
            }

            Byte[] maskBytes  = new IPv6SubnetMask(PrefixDelgationInfo.AssignedPrefixLength).GetMaskBytes();
            Byte[] deltaBytes = new Byte[16];
            for (int i = 0; i < 16; i++)
            {
                deltaBytes[i] = (Byte)(255 - maskBytes[i]);
            }

            IPv6Address delta = IPv6Address.FromByteArray(deltaBytes) + 1;

            IPv6Address current = PrefixDelgationInfo.Prefix - delta.GetBytes();

            foreach (var item in sorted)
            {
                IPv6Address expectedAddresses = current + delta;
                if (item != expectedAddresses)
                {
                    break;
                }

                current = expectedAddresses;
            }

            return(current + delta);
        }
Exemplo n.º 17
0
        internal IPv6Address GetRandomPrefix(HashSet <IPv6Address> hashedUsedPrefixes)
        {
            Random random = new Random();

            Byte[] addressBytes = new byte[16];

            IPv6SubnetMask mask = new IPv6SubnetMask(new IPv6SubnetMaskIdentifier(PrefixDelgationInfo.PrefixLength));

            Byte[] addressMaskBytes = mask.GetMaskBytes();
            Byte[] ipAddressBytes   = PrefixDelgationInfo.Prefix.GetBytes();

            IPv6SubnetMask delegatedMask = new IPv6SubnetMask(new IPv6SubnetMaskIdentifier(PrefixDelgationInfo.AssignedPrefixLength));

            Byte[] delegatedMaskBytes = delegatedMask.GetMaskBytes();

            Int32 randomizationIndex = -1;

            for (int i = 0; i < 16; i++)
            {
                if (addressMaskBytes[i] != 255)
                {
                    randomizationIndex = i;
                    break;
                }
                else
                {
                    addressBytes[i] = ipAddressBytes[i];
                }
            }

            IPv6Address nextNetworkAddress;
            Int32       trysLeft = _maxTriesForRandom;

            do
            {
                if (randomizationIndex >= 0)
                {
                    Boolean lengthAndAssigendLengthInSameByte = PrefixDelgationInfo.PrefixLength / 8 == (PrefixDelgationInfo.AssignedPrefixLength - 1) / 8;
                    if (lengthAndAssigendLengthInSameByte == true)
                    {
                        Byte diff = (Byte)(delegatedMaskBytes[randomizationIndex] - addressMaskBytes[randomizationIndex]);
                        ByteHelper.AddRandomBits(random, addressBytes, randomizationIndex, diff, ipAddressBytes[randomizationIndex]);
                    }
                    else
                    {
                        addressBytes[randomizationIndex] = (Byte)(addressMaskBytes[randomizationIndex] + random.Next(0, 255 - addressMaskBytes[randomizationIndex] + 1));
                    }

                    for (int i = randomizationIndex + 1; i < 16; i++)
                    {
                        if (delegatedMaskBytes[i] == 255)
                        {
                            addressBytes[i] = (Byte)random.Next(0, 256);
                        }
                        else
                        {
                            addressBytes[i] = (Byte)(random.Next(0, delegatedMaskBytes[i]));
                            ByteHelper.AddRandomBits(random, addressBytes, i, delegatedMaskBytes[i], 0);

                            break;
                        }
                    }
                }

                nextNetworkAddress = IPv6Address.FromByteArray(addressBytes);
            } while (trysLeft-- > 0 && hashedUsedPrefixes.Contains(nextNetworkAddress) == true);

            return(nextNetworkAddress);
        }
Exemplo n.º 18
0
 public void ApplyValues(IDictionary <string, string> valueMapper, ISerializer serializer)
 {
     NetworkAddress = serializer.Deserialze <IPv6Address>(valueMapper[nameof(NetworkAddress)]);
     SubnetMask     = serializer.Deserialze <IPv6SubnetMask>(valueMapper[nameof(SubnetMask)]);
 }