NetworkExtensions.cs 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. #pragma warning disable CA1062 // Validate arguments of public methods
  2. using System;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. using System.Net;
  6. using System.Runtime.CompilerServices;
  7. using System.Text;
  8. using NetCollection = System.Collections.ObjectModel.Collection<MediaBrowser.Common.Net.IPObject>;
  9. namespace MediaBrowser.Common.Net
  10. {
  11. /// <summary>
  12. /// Defines the <see cref="NetworkExtensions" />.
  13. /// </summary>
  14. public static class NetworkExtensions
  15. {
  16. /// <summary>
  17. /// Add an address to the collection.
  18. /// </summary>
  19. /// <param name="source">The <see cref="NetCollection"/>.</param>
  20. /// <param name="ip">Item to add.</param>
  21. public static void AddItem(this NetCollection source, IPAddress ip)
  22. {
  23. if (!source.ContainsAddress(ip))
  24. {
  25. source.Add(new IPNetAddress(ip, 32));
  26. }
  27. }
  28. /// <summary>
  29. /// Adds a network to the collection.
  30. /// </summary>
  31. /// <param name="source">The <see cref="NetCollection"/>.</param>
  32. /// <param name="item">Item to add.</param>
  33. public static void AddItem(this NetCollection source, IPObject item)
  34. {
  35. if (!source.ContainsAddress(item))
  36. {
  37. source.Add(item);
  38. }
  39. }
  40. /// <summary>
  41. /// Converts this object to a string.
  42. /// </summary>
  43. /// <param name="source">The <see cref="NetCollection"/>.</param>
  44. /// <returns>Returns a string representation of this object.</returns>
  45. public static string Readable(this NetCollection source)
  46. {
  47. var sb = new StringBuilder();
  48. string output = "[";
  49. if (source.Count > 0)
  50. {
  51. foreach (var i in source)
  52. {
  53. output += $"{i},";
  54. }
  55. output = output[0..^1];
  56. }
  57. return $"{output}]";
  58. }
  59. /// <summary>
  60. /// Returns true if the collection contains an item with the ip address,
  61. /// or the ip address falls within any of the collection's network ranges.
  62. /// </summary>
  63. /// <param name="source">The <see cref="NetCollection"/>.</param>
  64. /// <param name="item">The item to look for.</param>
  65. /// <returns>True if the collection contains the item.</returns>
  66. public static bool ContainsAddress(this NetCollection source, IPAddress item)
  67. {
  68. if (source.Count == 0)
  69. {
  70. return false;
  71. }
  72. if (item == null)
  73. {
  74. throw new ArgumentNullException(nameof(item));
  75. }
  76. if (item.IsIPv4MappedToIPv6)
  77. {
  78. item = item.MapToIPv4();
  79. }
  80. foreach (var i in source)
  81. {
  82. if (i.Contains(item))
  83. {
  84. return true;
  85. }
  86. }
  87. return false;
  88. }
  89. /// <summary>
  90. /// Returns true if the collection contains an item with the ip address,
  91. /// or the ip address falls within any of the collection's network ranges.
  92. /// </summary>
  93. /// <param name="source">The <see cref="NetCollection"/>.</param>
  94. /// <param name="item">The item to look for.</param>
  95. /// <returns>True if the collection contains the item.</returns>
  96. public static bool ContainsAddress(this NetCollection source, IPObject item)
  97. {
  98. if (source.Count == 0)
  99. {
  100. return false;
  101. }
  102. if (item == null)
  103. {
  104. throw new ArgumentNullException(nameof(item));
  105. }
  106. foreach (var i in source)
  107. {
  108. if (i.Contains(item))
  109. {
  110. return true;
  111. }
  112. }
  113. return false;
  114. }
  115. /// <summary>
  116. /// Compares two NetCollection objects. The order is ignored.
  117. /// </summary>
  118. /// <param name="source">The <see cref="NetCollection"/>.</param>
  119. /// <param name="dest">Item to compare to.</param>
  120. /// <returns>True if both are equal.</returns>
  121. public static bool Compare(this NetCollection source, NetCollection dest)
  122. {
  123. if (dest == null || source.Count != dest.Count)
  124. {
  125. return false;
  126. }
  127. foreach (var sourceItem in source)
  128. {
  129. bool found = false;
  130. foreach (var destItem in dest)
  131. {
  132. if (sourceItem.Equals(destItem))
  133. {
  134. found = true;
  135. break;
  136. }
  137. }
  138. if (!found)
  139. {
  140. return false;
  141. }
  142. }
  143. return true;
  144. }
  145. /// <summary>
  146. /// Returns a collection containing the subnets of this collection given.
  147. /// </summary>
  148. /// <param name="source">The <see cref="NetCollection"/>.</param>
  149. /// <returns>NetCollection object containing the subnets.</returns>
  150. public static NetCollection AsNetworks(this NetCollection source)
  151. {
  152. if (source == null)
  153. {
  154. throw new ArgumentNullException(nameof(source));
  155. }
  156. NetCollection res = new NetCollection();
  157. foreach (IPObject i in source)
  158. {
  159. if (i is IPNetAddress nw)
  160. {
  161. // Add the subnet calculated from the interface address/mask.
  162. var na = nw.NetworkAddress;
  163. na.Tag = i.Tag;
  164. res.AddItem(na);
  165. }
  166. else
  167. {
  168. // Flatten out IPHost and add all its ip addresses.
  169. foreach (var addr in ((IPHost)i).GetAddresses())
  170. {
  171. IPNetAddress host = new IPNetAddress(addr)
  172. {
  173. Tag = i.Tag
  174. };
  175. res.AddItem(host);
  176. }
  177. }
  178. }
  179. return res;
  180. }
  181. /// <summary>
  182. /// Excludes all the items from this list that are found in excludeList.
  183. /// </summary>
  184. /// <param name="source">The <see cref="NetCollection"/>.</param>
  185. /// <param name="excludeList">Items to exclude.</param>
  186. /// <returns>A new collection, with the items excluded.</returns>
  187. public static NetCollection Exclude(this NetCollection source, NetCollection excludeList)
  188. {
  189. if (source.Count == 0 || excludeList == null)
  190. {
  191. return new NetCollection(source);
  192. }
  193. NetCollection results = new NetCollection();
  194. bool found;
  195. foreach (var outer in source)
  196. {
  197. found = false;
  198. foreach (var inner in excludeList)
  199. {
  200. if (outer.Equals(inner))
  201. {
  202. found = true;
  203. break;
  204. }
  205. }
  206. if (!found)
  207. {
  208. results.AddItem(outer);
  209. }
  210. }
  211. return results;
  212. }
  213. /// <summary>
  214. /// Returns all items that co-exist in this object and target.
  215. /// </summary>
  216. /// <param name="source">The <see cref="NetCollection"/>.</param>
  217. /// <param name="target">Collection to compare with.</param>
  218. /// <returns>A collection containing all the matches.</returns>
  219. public static NetCollection Union(this NetCollection source, NetCollection target)
  220. {
  221. if (source.Count == 0)
  222. {
  223. return new NetCollection();
  224. }
  225. if (target == null)
  226. {
  227. throw new ArgumentNullException(nameof(target));
  228. }
  229. NetCollection nc = new NetCollection();
  230. foreach (IPObject i in source)
  231. {
  232. if (target.ContainsAddress(i))
  233. {
  234. nc.AddItem(i);
  235. }
  236. }
  237. return nc;
  238. }
  239. }
  240. }