IPAddress.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. using System;
  3. using System.Globalization;
  4. using Org.BouncyCastle.Math;
  5. namespace Org.BouncyCastle.Utilities.Net
  6. {
  7. public class IPAddress
  8. {
  9. /**
  10. * Validate the given IPv4 or IPv6 address.
  11. *
  12. * @param address the IP address as a string.
  13. *
  14. * @return true if a valid address, false otherwise
  15. */
  16. public static bool IsValid(
  17. string address)
  18. {
  19. return IsValidIPv4(address) || IsValidIPv6(address);
  20. }
  21. /**
  22. * Validate the given IPv4 or IPv6 address and netmask.
  23. *
  24. * @param address the IP address as a string.
  25. *
  26. * @return true if a valid address with netmask, false otherwise
  27. */
  28. public static bool IsValidWithNetMask(
  29. string address)
  30. {
  31. return IsValidIPv4WithNetmask(address) || IsValidIPv6WithNetmask(address);
  32. }
  33. /**
  34. * Validate the given IPv4 address.
  35. *
  36. * @param address the IP address as a string.
  37. *
  38. * @return true if a valid IPv4 address, false otherwise
  39. */
  40. public static bool IsValidIPv4(
  41. string address)
  42. {
  43. try
  44. {
  45. return unsafeIsValidIPv4(address);
  46. }
  47. catch (FormatException) {}
  48. catch (OverflowException) {}
  49. return false;
  50. }
  51. private static bool unsafeIsValidIPv4(
  52. string address)
  53. {
  54. if (address.Length == 0)
  55. return false;
  56. int octets = 0;
  57. string temp = address + ".";
  58. int pos;
  59. int start = 0;
  60. while (start < temp.Length
  61. && (pos = temp.IndexOf('.', start)) > start)
  62. {
  63. if (octets == 4)
  64. return false;
  65. string octetStr = temp.Substring(start, pos - start);
  66. int octet = Int32.Parse(octetStr);
  67. if (octet < 0 || octet > 255)
  68. return false;
  69. start = pos + 1;
  70. octets++;
  71. }
  72. return octets == 4;
  73. }
  74. public static bool IsValidIPv4WithNetmask(
  75. string address)
  76. {
  77. int index = address.IndexOf('/');
  78. string mask = address.Substring(index + 1);
  79. return (index > 0) && IsValidIPv4(address.Substring(0, index))
  80. && (IsValidIPv4(mask) || IsMaskValue(mask, 32));
  81. }
  82. public static bool IsValidIPv6WithNetmask(
  83. string address)
  84. {
  85. int index = address.IndexOf('/');
  86. string mask = address.Substring(index + 1);
  87. return (index > 0) && (IsValidIPv6(address.Substring(0, index))
  88. && (IsValidIPv6(mask) || IsMaskValue(mask, 128)));
  89. }
  90. private static bool IsMaskValue(
  91. string component,
  92. int size)
  93. {
  94. int val = Int32.Parse(component);
  95. try
  96. {
  97. return val >= 0 && val <= size;
  98. }
  99. catch (FormatException) {}
  100. catch (OverflowException) {}
  101. return false;
  102. }
  103. /**
  104. * Validate the given IPv6 address.
  105. *
  106. * @param address the IP address as a string.
  107. *
  108. * @return true if a valid IPv4 address, false otherwise
  109. */
  110. public static bool IsValidIPv6(
  111. string address)
  112. {
  113. try
  114. {
  115. return unsafeIsValidIPv6(address);
  116. }
  117. catch (FormatException) {}
  118. catch (OverflowException) {}
  119. return false;
  120. }
  121. private static bool unsafeIsValidIPv6(
  122. string address)
  123. {
  124. if (address.Length == 0)
  125. {
  126. return false;
  127. }
  128. int octets = 0;
  129. string temp = address + ":";
  130. bool doubleColonFound = false;
  131. int pos;
  132. int start = 0;
  133. while (start < temp.Length
  134. && (pos = temp.IndexOf(':', start)) >= start)
  135. {
  136. if (octets == 8)
  137. {
  138. return false;
  139. }
  140. if (start != pos)
  141. {
  142. string value = temp.Substring(start, pos - start);
  143. if (pos == (temp.Length - 1) && value.IndexOf('.') > 0)
  144. {
  145. if (!IsValidIPv4(value))
  146. {
  147. return false;
  148. }
  149. octets++; // add an extra one as address covers 2 words.
  150. }
  151. else
  152. {
  153. string octetStr = temp.Substring(start, pos - start);
  154. int octet = Int32.Parse(octetStr, NumberStyles.AllowHexSpecifier);
  155. if (octet < 0 || octet > 0xffff)
  156. return false;
  157. }
  158. }
  159. else
  160. {
  161. if (pos != 1 && pos != temp.Length - 1 && doubleColonFound)
  162. {
  163. return false;
  164. }
  165. doubleColonFound = true;
  166. }
  167. start = pos + 1;
  168. octets++;
  169. }
  170. return octets == 8 || doubleColonFound;
  171. }
  172. }
  173. }
  174. #endif