Esempio n. 1
0
        private bool SaveAdvancedOptions()
        {
            // Validate input.

            var regex = new Regex("^\\d+$");

            // Validate TTL.
            if (!regex.IsMatch(TTL.Text) || int.Parse(TTL.Text) < 1 || int.Parse(TTL.Text) > 255)
            {
                AdvancedTab.Focus();
                MessageBox.Show(
                    "Please enter a valid TTL between 1 and 255.",
                    "vmPing Error",
                    MessageBoxButton.OK,
                    MessageBoxImage.Error);
                TTL.Focus();
                return(false);
            }

            // Apply TTL.
            ApplicationOptions.TTL = int.Parse(TTL.Text);

            // Validate packet size.
            if (PacketSizeOption.IsChecked == true)
            {
                if (!regex.IsMatch(PacketSize.Text) || int.Parse(PacketSize.Text) < 0 || int.Parse(PacketSize.Text) > 65500)
                {
                    AdvancedTab.Focus();
                    MessageBox.Show(
                        "Please enter a valid ICMP data size between 0 and 65,500.",
                        "vmPing Error",
                        MessageBoxButton.OK,
                        MessageBoxImage.Error);
                    PacketSize.Focus();
                    return(false);
                }

                // Apply packet size.
                ApplicationOptions.Buffer          = new byte[int.Parse(PacketSize.Text)];
                ApplicationOptions.UseCustomBuffer = false;

                // Fill buffer with default text.
                if (ApplicationOptions.Buffer.Length >= 33)
                {
                    Buffer.BlockCopy(Encoding.ASCII.GetBytes(Constants.PING_DATA), 0, ApplicationOptions.Buffer, 0, 33);
                }
            }
            else
            {
                // Use custom packet data.
                ApplicationOptions.Buffer          = Encoding.ASCII.GetBytes(PacketData.Text);
                ApplicationOptions.UseCustomBuffer = true;
            }

            // Apply fragment / don't fragment option.
            if (DontFragment.IsChecked == true)
            {
                ApplicationOptions.DontFragment = true;
            }
            else
            {
                ApplicationOptions.DontFragment = false;
            }

            // Update ping options (TTL / Don't fragment settings)
            ApplicationOptions.UpdatePingOptions();

            return(true);
        }