NetworkExtensions.cs 8.5 KB

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