NetworkExtensions.cs 7.8 KB

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