NetworkExtensions.cs 8.4 KB

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