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); }