NetUtils.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. using System;
  2. using System.Net;
  3. using System.Net.Sockets;
  4. namespace NatTypeTester_Console.Net
  5. {
  6. public static class NetUtils
  7. {
  8. #region static method CompareArray
  9. /// <summary>
  10. /// Compares if specified array items equals.
  11. /// </summary>
  12. /// <param name="array1">Array 1.</param>
  13. /// <param name="array2">Array 2</param>
  14. /// <returns>Returns true if both arrays are equal.</returns>
  15. public static bool CompareArray(Array array1, Array array2)
  16. {
  17. return CompareArray(array1, array2, array2.Length);
  18. }
  19. /// <summary>
  20. /// Compares if specified array items equals.
  21. /// </summary>
  22. /// <param name="array1">Array 1.</param>
  23. /// <param name="array2">Array 2</param>
  24. /// <param name="array2Count">Number of bytes in array 2 used for compare.</param>
  25. /// <returns>Returns true if both arrays are equal.</returns>
  26. public static bool CompareArray(Array array1, Array array2, int array2Count)
  27. {
  28. if (array1 == null && array2 == null)
  29. {
  30. return true;
  31. }
  32. if (array1 == null)
  33. {
  34. return false;
  35. }
  36. if (array2 == null)
  37. {
  38. return false;
  39. }
  40. if (array1.Length != array2Count)
  41. {
  42. return false;
  43. }
  44. for (var i = 0; i < array1.Length; i++)
  45. {
  46. if (!array1.GetValue(i).Equals(array2.GetValue(i)))
  47. {
  48. return false;
  49. }
  50. }
  51. return true;
  52. }
  53. #endregion
  54. #region static method IsPrivateIP
  55. /// <summary>
  56. /// Gets if specified IP address is private LAN IP address. For example 192.168.x.x is private ip.
  57. /// </summary>
  58. /// <param name="ip">IP address to check.</param>
  59. /// <returns>Returns true if IP is private IP.</returns>
  60. /// <exception cref="ArgumentNullException">Is raised when <b>ip</b> is null reference.</exception>
  61. public static bool IsPrivateIP(IPAddress ip)
  62. {
  63. if (ip == null)
  64. {
  65. throw new ArgumentNullException(nameof(ip));
  66. }
  67. if (ip.AddressFamily == AddressFamily.InterNetwork)
  68. {
  69. var ipBytes = ip.GetAddressBytes();
  70. /* Private IPs:
  71. First Octet = 192 AND Second Octet = 168 (Example: 192.168.X.X)
  72. First Octet = 172 AND (Second Octet >= 16 AND Second Octet <= 31) (Example: 172.16.X.X - 172.31.X.X)
  73. First Octet = 10 (Example: 10.X.X.X)
  74. First Octet = 169 AND Second Octet = 254 (Example: 169.254.X.X)
  75. */
  76. if (ipBytes[0] == 192 && ipBytes[1] == 168)
  77. {
  78. return true;
  79. }
  80. if (ipBytes[0] == 172 && ipBytes[1] >= 16 && ipBytes[1] <= 31)
  81. {
  82. return true;
  83. }
  84. if (ipBytes[0] == 10)
  85. {
  86. return true;
  87. }
  88. if (ipBytes[0] == 169 && ipBytes[1] == 254)
  89. {
  90. return true;
  91. }
  92. }
  93. return false;
  94. }
  95. #endregion
  96. #region static method CreateSocket
  97. /// <summary>
  98. /// Creates new socket for the specified end point.
  99. /// </summary>
  100. /// <param name="localEP">Local end point.</param>
  101. /// <param name="protocolType">Protocol type.</param>
  102. /// <returns>Return newly created socket.</returns>
  103. /// <exception cref="ArgumentNullException">Is raised when <b>localEP</b> is null reference.</exception>
  104. public static Socket CreateSocket(IPEndPoint localEP, ProtocolType protocolType)
  105. {
  106. if (localEP == null)
  107. {
  108. throw new ArgumentNullException(nameof(localEP));
  109. }
  110. var socketType = SocketType.Stream;
  111. if (protocolType == ProtocolType.Udp)
  112. {
  113. socketType = SocketType.Dgram;
  114. }
  115. if (localEP.AddressFamily == AddressFamily.InterNetwork)
  116. {
  117. var socket = new Socket(AddressFamily.InterNetwork, socketType, protocolType);
  118. socket.Bind(localEP);
  119. return socket;
  120. }
  121. if (localEP.AddressFamily == AddressFamily.InterNetworkV6)
  122. {
  123. var socket = new Socket(AddressFamily.InterNetworkV6, socketType, protocolType);
  124. socket.Bind(localEP);
  125. return socket;
  126. }
  127. throw new ArgumentException(@"Invalid IPEndPoint address family.");
  128. }
  129. #endregion
  130. }
  131. }