SubnetMaskValidationRule.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. using System;
  2. using System.Globalization;
  3. using System.Net;
  4. using System.Text.RegularExpressions;
  5. using System.Windows.Controls;
  6. namespace TeamAAS_VP.ValidationRules
  7. {
  8. public class SubnetMaskValidationRule : ValidationRule
  9. {
  10. public override ValidationResult Validate(object value, CultureInfo cultureInfo)
  11. {
  12. string input = value as string;
  13. // 检查输入是否为空
  14. if (string.IsNullOrWhiteSpace(input))
  15. return new ValidationResult(false, "Subnet mask cannot be empty.".Replace("\\n", Environment.NewLine).Replace("\\t", "\t"));
  16. // 检查是否包含全角字符
  17. if (ContainsFullWidthCharacters(input))
  18. return new ValidationResult(false, "Subnet mask contains full-width characters.".Replace("\\n", Environment.NewLine).Replace("\\t", "\t"));
  19. // 检查是否为有效的 IPv4 格式
  20. if (!IsValidIPv4Format(input))
  21. return new ValidationResult(false, "Invalid subnet mask format.".Replace("\\n", Environment.NewLine).Replace("\\t", "\t"));
  22. // 检查每个部分的数值是否在 0 到 255 之间
  23. if (!IsValidIPv4Range(input))
  24. return new ValidationResult(false, "Subnet mask out of range.".Replace("\\n", Environment.NewLine).Replace("\\t", "\t"));
  25. // 检查是否为有效的子网掩码
  26. if (!IsValidSubnetMask(input))
  27. return new ValidationResult(false, "Invalid subnet mask.".Replace("\\n", Environment.NewLine).Replace("\\t", "\t"));
  28. return ValidationResult.ValidResult;
  29. }
  30. // 检查是否包含全角字符
  31. private bool ContainsFullWidthCharacters(string input)
  32. {
  33. foreach (char c in input)
  34. {
  35. if (c >= 0xFF01 && c <= 0xFF5E) // 全角字符范围
  36. return true;
  37. }
  38. return false;
  39. }
  40. // 检查是否为有效的 IPv4 格式
  41. private bool IsValidIPv4Format(string input)
  42. {
  43. string pattern = @"^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$";
  44. return Regex.IsMatch(input, pattern);
  45. }
  46. // 检查每个部分的数值是否在 0 到 255 之间
  47. private bool IsValidIPv4Range(string input)
  48. {
  49. string[] parts = input.Split('.');
  50. foreach (string part in parts)
  51. {
  52. if (!int.TryParse(part, out int number) || number < 0 || number > 255)
  53. return false;
  54. }
  55. return true;
  56. }
  57. // 检查是否为有效的子网掩码
  58. private bool IsValidSubnetMask(string input)
  59. {
  60. string[] parts = input.Split('.');
  61. int[] maskParts = new int[4];
  62. for (int i = 0; i < 4; i++)
  63. {
  64. maskParts[i] = int.Parse(parts[i]);
  65. }
  66. // 将子网掩码转换为二进制形式
  67. uint mask = (uint)(maskParts[0] << 24 | maskParts[1] << 16 | maskParts[2] << 8 | maskParts[3]);
  68. // 检查是否为连续的 1 和 0
  69. bool foundZero = false;
  70. for (int i = 31; i >= 0; i--)
  71. {
  72. uint bit = (mask >> i) & 1;
  73. if (bit == 0)
  74. {
  75. foundZero = true;
  76. }
  77. else if (foundZero)
  78. {
  79. // 如果发现 1 在 0 之后,说明不是有效的子网掩码
  80. return false;
  81. }
  82. }
  83. return true;
  84. }
  85. }
  86. }