PageRenderTime 58ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/NET6/Hermod/Hermod/Helpers/Datastructures/IIPAddress.cs

https://github.com/Vanaheimr/Hermod
C# | 160 lines | 78 code | 46 blank | 36 comment | 16 complexity | ab5feeea68976d29b97f5deebfc85ecc MD5 | raw file
  1. /*
  2. * Copyright (c) 2010-2022 GraphDefined GmbH <achim.friedland@graphdefined.com>
  3. * This file is part of Vanaheimr Hermod <https://www.github.com/Vanaheimr/Hermod>
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. #region Usings
  18. using org.GraphDefined.Vanaheimr.Hermod.HTTP;
  19. using org.GraphDefined.Vanaheimr.Illias;
  20. using System;
  21. using System.Text.RegularExpressions;
  22. #endregion
  23. namespace org.GraphDefined.Vanaheimr.Hermod
  24. {
  25. public static class IPAddress
  26. {
  27. //ToDo: Better do this by hand!
  28. public static Regex IPv4AddressRegExpr = new Regex(@"\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b");
  29. //ToDo: Better do this by hand!
  30. public static Regex IPv6AddressRegExpr = new Regex(@"(([a-f0-9:]+:+)+[a-f0-9]+)");
  31. public static IIPAddress Parse(String Text)
  32. {
  33. if (TryParse(Text, out IIPAddress ipAddress))
  34. return ipAddress;
  35. if (Text.IsNullOrEmpty())
  36. throw new ArgumentNullException(nameof(Text), "The given IP address must not be null or empty!");
  37. return null;
  38. }
  39. public static Boolean TryParse(String Text, out IIPAddress IPAddress)
  40. {
  41. Text = Text?.Trim();
  42. if (Text.IsNotNullOrEmpty())
  43. {
  44. if (IsIPv4(Text))
  45. {
  46. IPAddress = IPv4Address.Parse(Text);
  47. return true;
  48. }
  49. if (IsIPv6(Text))
  50. {
  51. IPAddress = IPv6Address.Parse(Text);
  52. return true;
  53. }
  54. }
  55. IPAddress = null;
  56. return false;
  57. }
  58. public static Boolean IsIPv4(String IPAddress)
  59. => IPAddress.IsNotNullOrEmpty() &&
  60. IPv4AddressRegExpr.IsMatch(IPAddress?.Trim());
  61. public static Boolean IsIPv4(HTTPHostname Hostname)
  62. => Hostname.IsNotNullOrEmpty &&
  63. IPv4AddressRegExpr.IsMatch(Hostname.ToString());
  64. public static Boolean IsIPv6(String IPAddress)
  65. => IPAddress.IsNotNullOrEmpty() &&
  66. IPv6AddressRegExpr.IsMatch(IPAddress?.Trim());
  67. public static Boolean IsIPv6(HTTPHostname Hostname)
  68. => Hostname.IsNotNullOrEmpty &&
  69. IPv6AddressRegExpr.IsMatch(Hostname.ToString());
  70. public static Boolean IsLocalhost(String Text)
  71. => IsIPv4Localhost(Text) || IsIPv6Localhost(Text);
  72. public static Boolean IsLocalhost(HTTPHostname Hostname)
  73. => IsIPv4Localhost(Hostname) || IsIPv6Localhost(Hostname);
  74. public static Boolean IsIPv4Localhost(String Text)
  75. => IsIPv4(Text) && (Text.StartsWith("127.") || Text.ToLower() == "localhost");
  76. public static Boolean IsIPv4Localhost(HTTPHostname Hostname)
  77. => Hostname.IsNotNullOrEmpty && IsIPv4Localhost(Hostname.ToString());
  78. public static Boolean IsIPv6Localhost(String Text)
  79. => IsIPv6(Text) && (Text == "::1" || Text.ToLower() == "localhost6");
  80. public static Boolean IsIPv6Localhost(HTTPHostname Hostname)
  81. => Hostname.IsNotNullOrEmpty && IsIPv6Localhost(Hostname.ToString());
  82. }
  83. /// <summary>
  84. /// A common interface for all kinds of Internet protocol addresses.
  85. /// </summary>
  86. public interface IIPAddress : IComparable,
  87. IComparable<IIPAddress>,
  88. IEquatable<IIPAddress>
  89. {
  90. /// <summary>
  91. /// The length of the IP Address.
  92. /// </summary>
  93. Byte Length { get; }
  94. /// <summary>
  95. /// Whether the IP address is an IPv4 multicast address.
  96. /// </summary>
  97. Boolean IsMulticast { get; }
  98. Boolean IsIPv4 { get; }
  99. Boolean IsIPv6 { get; }
  100. Boolean IsLocalhost { get; }
  101. /// <summary>
  102. /// Return a byte array representation of this object.
  103. /// </summary>
  104. Byte[] GetBytes();
  105. /// <summary>
  106. /// Return the HashCode of this object.
  107. /// </summary>
  108. Int32 GetHashCode();
  109. /// <summary>
  110. /// Return a text representation of this object.
  111. /// </summary>
  112. String ToString();
  113. }
  114. }