NetworkExtensions.cs 8.2 KB

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