Ejemplo n.º 1
0
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (DesignerProperties.GetIsInDesignMode(new DependencyObject()))
            {
                return("-/-");
            }

            if (!(value is Tuple <IPAddress, IPAddress>[] ipAddresses))
            {
                return("-/-");
            }

            var result = string.Empty;

            foreach (var ipAddr in ipAddresses)
            {
                if (!string.IsNullOrEmpty(result))
                {
                    result += Environment.NewLine;
                }

                result += ipAddr.Item1.ToString() + "/" + Subnetmask.ConvertSubnetmaskToCidr(ipAddr.Item2);
            }

            return(result);
        }
Ejemplo n.º 2
0
        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);
            }

            var subnet = values[2] as string;
            var newSubnetmaskOrCidr = values[3] as string;

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

            // Get the cidr to compare...
            var subnetData = subnet.Split('/');

            var ipAddress        = IPAddress.Parse(subnetData[0]);
            var subnetmaskOrCidr = subnetData[1];
            int cidr;

            // ReSharper disable once SwitchStatementMissingSomeCases
            switch (ipAddress.AddressFamily)
            {
            case System.Net.Sockets.AddressFamily.InterNetwork when subnetmaskOrCidr.Length < 3:
                cidr = int.Parse(subnetmaskOrCidr);
                break;

            case System.Net.Sockets.AddressFamily.InterNetwork:
                cidr = Subnetmask.ConvertSubnetmaskToCidr(IPAddress.Parse(subnetmaskOrCidr));
                break;

            default:
                cidr = int.Parse(subnetmaskOrCidr);
                break;
            }

            int newCidr;

            // Support subnetmask like 255.255.255.0
            if (Regex.IsMatch(newSubnetmaskOrCidr, RegexHelper.SubnetmaskRegex))
            {
                newCidr = System.Convert.ToByte(Subnetmask.ConvertSubnetmaskToCidr(IPAddress.Parse(newSubnetmaskOrCidr)));
            }
            else
            {
                newCidr = System.Convert.ToByte(newSubnetmaskOrCidr.TrimStart('/'));
            }

            // Compare
            return(newCidr > cidr);
        }
        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 = Subnetmask.ConvertSubnetmaskToCidr(IPAddress.Parse(subnetmaskOrCIDR));
            }

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

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

            // Compare
            return(newCidr > cidr);
        }
        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]) : Subnetmask.ConvertSubnetmaskToCidr(subnetSplit[1]);
            double newCidr = newSubnetmaskOrCidr.Length < 3 ? double.Parse(newSubnetmaskOrCidr) : Subnetmask.ConvertSubnetmaskToCidr(newSubnetmaskOrCidr);

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

                settings.AffirmativeButtonText = LocalizationManager.GetStringByKey("String_Button_Continue");
                settings.NegativeButtonText    = LocalizationManager.GetStringByKey("String_Button_Cancel");

                settings.DefaultButtonFocus = MessageDialogResult.Affirmative;

                if (await dialogCoordinator.ShowMessageAsync(this, LocalizationManager.GetStringByKey("String_Header_AreYouSure"), LocalizationManager.GetStringByKey("String_TheProcessCanTakeUpSomeTimeAndResources"), 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        = LocalizationManager.GetStringByKey("String_CanceledByUser");
                DisplayStatusMessage = true;
            }

            CancelSplit    = false;
            IsSplitRunning = false;
        }