NetworkExtensions.cs 8.1 KB

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