NetworkExtensions.cs 8.2 KB

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