NetworkExtensions.cs 8.0 KB

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