public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
        {
            // if Validation.HasError is true...
            if ((bool)values[0] || (bool)values[1])
            {
                return(false);
            }

            string subnet = values[2] as string;
            string newSubnetmaskOrCIDR = values[3] as string;

            // Catch null exceptions...
            if (string.IsNullOrEmpty(subnet) || string.IsNullOrEmpty(newSubnetmaskOrCIDR))
            {
                return(false);
            }

            // Get the cidr to compare...
            string subnetmaskOrCIDR = subnet.Split('/')[1];
            int    cidr;

            if (subnetmaskOrCIDR.Length < 3)
            {
                cidr = int.Parse(subnetmaskOrCIDR);
            }
            else
            {
                cidr = SubnetmaskHelper.ConvertSubnetmaskToCidr(IPAddress.Parse(subnetmaskOrCIDR));
            }

            newSubnetmaskOrCIDR = newSubnetmaskOrCIDR.TrimStart('/');
            int newCidr;

            if (newSubnetmaskOrCIDR.Length < 3)
            {
                newCidr = int.Parse(newSubnetmaskOrCIDR);
            }
            else
            {
                newCidr = SubnetmaskHelper.ConvertSubnetmaskToCidr(IPAddress.Parse(newSubnetmaskOrCIDR));
            }

            // Compare
            return(newCidr > cidr);
        }
示例#2
0
        public static SubnetInfo[] SplitIPv4Subnet(IPAddress ipv4Address, IPAddress subnetmask, IPAddress newSubnetmask, CancellationToken cancellationToken)
        {
            ConcurrentBag <SubnetInfo> bag = new ConcurrentBag <SubnetInfo>();

            // Calculate the current subnet
            SubnetInfo subnetInfo = CalculateIPv4Subnet(ipv4Address, subnetmask);

            int newCidr = SubnetmaskHelper.ConvertSubnetmaskToCidr(newSubnetmask);

            // Get new  HostBits based on SubnetBits (CIDR) // Hostbits (32 - /24 = 8 -> 00000000000000000000000011111111)
            string newHostBits = (new string('1', (32 - newCidr))).PadLeft(32, '0');

            // Convert Bits to Int64, add +1 to get all available IPs
            int newTotalIPs = Convert.ToInt32(newHostBits, 2) + 1;

            // Get bytes...
            byte[] networkAddressBytes = subnetInfo.NetworkAddress.GetAddressBytes();

            // Get int...
            if (BitConverter.IsLittleEndian)
            {
                Array.Reverse(networkAddressBytes);
            }

            int networkAddress = BitConverter.ToInt32(networkAddressBytes, 0);

            Parallel.For(0, subnetInfo.IPAddresses / newTotalIPs, new ParallelOptions()
            {
                CancellationToken = cancellationToken
            }, i =>
            {
                byte[] newNetworkAddressBytes = BitConverter.GetBytes(networkAddress + (int)(newTotalIPs * i));

                if (BitConverter.IsLittleEndian)
                {
                    Array.Reverse(newNetworkAddressBytes);
                }

                SubnetInfo info = CalculateIPv4Subnet(new IPAddress(newNetworkAddressBytes), newSubnetmask);

                bag.Add(new SubnetInfo(info.NetworkAddress, info.Broadcast, info.IPAddresses, info.Subnetmask, info.CIDR, info.HostFirstIP, info.HostLastIP, info.Hosts));
            });

            return(bag.ToArray());
        }
示例#3
0
        public static SubnetInfo CalculateIPv4Subnet(IPAddress ipv4Address, IPAddress subnetmask)
        {
            IPAddress networkAddress = SubnetHelper.GetIPv4NetworkAddress(ipv4Address, subnetmask);
            IPAddress broadcast      = SubnetHelper.GetIPv4Broadcast(ipv4Address, subnetmask);
            int       cidr           = SubnetmaskHelper.ConvertSubnetmaskToCidr(subnetmask);
            int       totalIPs       = SubnetmaskHelper.GetNumberIPv4Addresses(cidr);

            return(new SubnetInfo
            {
                NetworkAddress = networkAddress,
                Broadcast = broadcast,
                TotalIPs = totalIPs,
                Subnetmask = subnetmask,
                CIDR = cidr,
                HostFirstIP = IPv4AddressHelper.IncrementIPv4Address(networkAddress, 1),
                HostLastIP = IPv4AddressHelper.DecrementIPv4Address(broadcast, 1),
                HostIPs = totalIPs - 2
            });
        }
示例#4
0
        public static SubnetInfo CalculateIPv4Subnet(IPAddress ipv4Address, IPAddress subnetmask)
        {
            IPAddress networkAddress = SubnetHelper.GetIPv4NetworkAddress(ipv4Address, subnetmask);
            IPAddress broadcast      = SubnetHelper.GetIPv4Broadcast(ipv4Address, subnetmask);
            int       cidr           = SubnetmaskHelper.ConvertSubnetmaskToCidr(subnetmask);
            long      totalIPs       = SubnetmaskHelper.GetNumberIPv4Addresses(cidr);

            // Fix bug when /31 (host first/last can be null)
            IPAddress hostFirstIP = null;
            IPAddress hostLastIP  = null;
            long      hostIPs     = 0;

            if (totalIPs == 1) // Fix bug when /32 (show range for 1 IP)
            {
                hostFirstIP = networkAddress;
                hostLastIP  = broadcast;
                hostIPs     = 0;
            }
            else if (totalIPs > 2) // Calculate for /0-/30
            {
                hostFirstIP = IPv4AddressHelper.IncrementIPv4Address(networkAddress, 1);
                hostLastIP  = IPv4AddressHelper.DecrementIPv4Address(broadcast, 1);
                hostIPs     = totalIPs - 2;
            }

            return(new SubnetInfo
            {
                NetworkAddress = networkAddress,
                Broadcast = broadcast,
                IPAddresses = totalIPs,
                Subnetmask = subnetmask,
                CIDR = cidr,
                HostFirstIP = hostFirstIP,
                HostLastIP = hostLastIP,
                Hosts = hostIPs
            });
        }
        private async void StartSplit()
        {
            DisplayStatusMessage = false;
            IsSplitRunning       = true;

            SplitResult.Clear();

            string[] subnetSplit         = Subnet.Trim().Split('/');
            string   newSubnetmaskOrCidr = NewSubnetmaskOrCIDR.TrimStart('/');

            // Validate the user input and display warning
            double cidr    = subnetSplit[1].Length < 3 ? double.Parse(subnetSplit[1]) : SubnetmaskHelper.ConvertSubnetmaskToCidr(subnetSplit[1]);
            double newCidr = newSubnetmaskOrCidr.Length < 3 ? double.Parse(newSubnetmaskOrCidr) : SubnetmaskHelper.ConvertSubnetmaskToCidr(newSubnetmaskOrCidr);

            if (65535 < (Math.Pow(2, (32 - cidr)) / Math.Pow(2, (32 - newCidr))))
            {
                MetroDialogSettings settings = AppearanceManager.MetroDialog;

                settings.AffirmativeButtonText = Application.Current.Resources["String_Button_Continue"] as string;
                settings.NegativeButtonText    = Application.Current.Resources["String_Button_Cancel"] as string;

                settings.DefaultButtonFocus = MessageDialogResult.Affirmative;

                if (await dialogCoordinator.ShowMessageAsync(this, Application.Current.Resources["String_Header_AreYouSure"] as string, Application.Current.Resources["String_TheProcessCanTakeUpSomeTimeAndResources"] as string, MessageDialogStyle.AffirmativeAndNegative, settings) != MessageDialogResult.Affirmative)
                {
                    CancelSplit    = false;
                    IsSplitRunning = false;

                    return;
                }
            }

            // Convert CIDR to subnetmask
            string subnetmask    = Subnetmask.GetFromCidr((int)cidr).Subnetmask;
            string newSubnetmask = Subnetmask.GetFromCidr((int)newCidr).Subnetmask;

            // Add history
            AddSubnetToHistory(Subnet);
            AddNewSubnetmaskOrCIDRToHistory(NewSubnetmaskOrCIDR);

            cancellationTokenSource = new CancellationTokenSource();

            try
            {
                foreach (SubnetInfo subnetInfo in await Models.Network.Subnet.SplitIPv4SubnetAsync(IPAddress.Parse(subnetSplit[0]), IPAddress.Parse(subnetmask), IPAddress.Parse(newSubnetmask), cancellationTokenSource.Token))
                {
                    SplitResult.Add(subnetInfo);
                }
            }
            catch (OperationCanceledException)
            {
                StatusMessage        = Application.Current.Resources["String_CanceledByUser"] as string;
                DisplayStatusMessage = true;
            }

            CancelSplit    = false;
            IsSplitRunning = false;
        }