jellyfin/MediaBrowser.Common/Net/NetworkExtensions.cs

263 lines
8.2 KiB
C#
Raw Normal View History

2020-10-31 18:21:46 +00:00
#pragma warning disable CA1062 // Validate arguments of public methods
using System;
using System.Collections;
using System.Collections.Generic;
2020-11-16 19:37:38 +00:00
using System.Collections.ObjectModel;
2020-10-31 18:21:46 +00:00
using System.Net;
using System.Runtime.CompilerServices;
2020-10-31 19:16:28 +00:00
using System.Text;
2020-10-31 18:21:46 +00:00
namespace MediaBrowser.Common.Net
{
/// <summary>
/// Defines the <see cref="NetworkExtensions" />.
/// </summary>
public static class NetworkExtensions
{
/// <summary>
/// Add an address to the collection.
/// </summary>
2020-11-16 19:37:38 +00:00
/// <param name="source">The <see cref="Collection{IPObject}"/>.</param>
2020-10-31 18:21:46 +00:00
/// <param name="ip">Item to add.</param>
2020-11-16 19:37:38 +00:00
public static void AddItem(this Collection<IPObject> source, IPAddress ip)
2020-10-31 18:21:46 +00:00
{
if (!source.ContainsAddress(ip))
{
source.Add(new IPNetAddress(ip, 32));
}
}
/// <summary>
/// Adds a network to the collection.
/// </summary>
2020-11-16 19:37:38 +00:00
/// <param name="source">The <see cref="Collection{IPObject}"/>.</param>
2020-10-31 18:21:46 +00:00
/// <param name="item">Item to add.</param>
2020-11-16 19:37:38 +00:00
public static void AddItem(this Collection<IPObject> source, IPObject item)
2020-10-31 18:21:46 +00:00
{
if (!source.ContainsAddress(item))
{
source.Add(item);
}
}
/// <summary>
/// Converts this object to a string.
/// </summary>
2020-11-16 19:37:38 +00:00
/// <param name="source">The <see cref="Collection{IPObject}"/>.</param>
2020-10-31 18:21:46 +00:00
/// <returns>Returns a string representation of this object.</returns>
2020-11-16 19:37:38 +00:00
public static string AsString(this Collection<IPObject> source)
2020-10-31 18:21:46 +00:00
{
2020-11-16 19:37:38 +00:00
return $"[{string.Join(',', source)}]";
2020-10-31 18:21:46 +00:00
}
/// <summary>
/// Returns true if the collection contains an item with the ip address,
/// or the ip address falls within any of the collection's network ranges.
/// </summary>
2020-11-16 19:37:38 +00:00
/// <param name="source">The <see cref="Collection{IPObject}"/>.</param>
2020-10-31 18:21:46 +00:00
/// <param name="item">The item to look for.</param>
/// <returns>True if the collection contains the item.</returns>
2020-11-16 19:37:38 +00:00
public static bool ContainsAddress(this Collection<IPObject> source, IPAddress item)
2020-10-31 18:21:46 +00:00
{
if (source.Count == 0)
{
return false;
}
if (item == null)
{
throw new ArgumentNullException(nameof(item));
}
if (item.IsIPv4MappedToIPv6)
{
item = item.MapToIPv4();
}
foreach (var i in source)
{
if (i.Contains(item))
{
return true;
}
}
return false;
}
/// <summary>
/// Returns true if the collection contains an item with the ip address,
/// or the ip address falls within any of the collection's network ranges.
/// </summary>
2020-11-16 19:37:38 +00:00
/// <param name="source">The <see cref="Collection{IPObject}"/>.</param>
2020-10-31 18:21:46 +00:00
/// <param name="item">The item to look for.</param>
/// <returns>True if the collection contains the item.</returns>
2020-11-16 19:37:38 +00:00
public static bool ContainsAddress(this Collection<IPObject> source, IPObject item)
2020-10-31 18:21:46 +00:00
{
if (source.Count == 0)
{
return false;
}
if (item == null)
{
throw new ArgumentNullException(nameof(item));
}
foreach (var i in source)
{
if (i.Contains(item))
{
return true;
}
}
return false;
}
2020-10-31 19:16:28 +00:00
/// <summary>
2020-11-16 19:37:38 +00:00
/// Compares two Collection{IPObject} objects. The order is ignored.
2020-10-31 19:16:28 +00:00
/// </summary>
2020-11-16 19:37:38 +00:00
/// <param name="source">The <see cref="Collection{IPObject}"/>.</param>
2020-10-31 19:16:28 +00:00
/// <param name="dest">Item to compare to.</param>
/// <returns>True if both are equal.</returns>
2020-11-16 19:37:38 +00:00
public static bool Compare(this Collection<IPObject> source, Collection<IPObject> dest)
2020-10-31 19:16:28 +00:00
{
if (dest == null || source.Count != dest.Count)
{
return false;
}
foreach (var sourceItem in source)
{
bool found = false;
foreach (var destItem in dest)
{
if (sourceItem.Equals(destItem))
{
found = true;
break;
}
}
if (!found)
{
return false;
}
}
return true;
}
2020-10-31 18:21:46 +00:00
/// <summary>
/// Returns a collection containing the subnets of this collection given.
/// </summary>
2020-11-16 19:37:38 +00:00
/// <param name="source">The <see cref="Collection{IPObject}"/>.</param>
/// <returns>Collection{IPObject} object containing the subnets.</returns>
public static Collection<IPObject> AsNetworks(this Collection<IPObject> source)
2020-10-31 18:21:46 +00:00
{
if (source == null)
{
throw new ArgumentNullException(nameof(source));
}
2020-11-16 19:37:38 +00:00
Collection<IPObject> res = new Collection<IPObject>();
2020-10-31 18:21:46 +00:00
foreach (IPObject i in source)
{
if (i is IPNetAddress nw)
{
// Add the subnet calculated from the interface address/mask.
var na = nw.NetworkAddress;
na.Tag = i.Tag;
2020-10-31 19:16:28 +00:00
res.AddItem(na);
2020-10-31 18:21:46 +00:00
}
2020-11-16 19:37:38 +00:00
else if (i is IPHost ipHost)
2020-10-31 18:21:46 +00:00
{
// Flatten out IPHost and add all its ip addresses.
2020-11-16 19:37:38 +00:00
foreach (var addr in ipHost.GetAddresses())
2020-10-31 18:21:46 +00:00
{
IPNetAddress host = new IPNetAddress(addr)
{
Tag = i.Tag
};
2020-10-31 19:16:28 +00:00
res.AddItem(host);
2020-10-31 18:21:46 +00:00
}
}
}
return res;
}
/// <summary>
/// Excludes all the items from this list that are found in excludeList.
/// </summary>
2020-11-16 19:37:38 +00:00
/// <param name="source">The <see cref="Collection{IPObject}"/>.</param>
2020-10-31 18:21:46 +00:00
/// <param name="excludeList">Items to exclude.</param>
/// <returns>A new collection, with the items excluded.</returns>
2020-11-16 19:37:38 +00:00
public static Collection<IPObject> Exclude(this Collection<IPObject> source, Collection<IPObject> excludeList)
2020-10-31 18:21:46 +00:00
{
if (source.Count == 0 || excludeList == null)
{
2020-11-16 19:37:38 +00:00
return new Collection<IPObject>(source);
2020-10-31 18:21:46 +00:00
}
2020-11-16 19:37:38 +00:00
Collection<IPObject> results = new Collection<IPObject>();
2020-10-31 18:21:46 +00:00
bool found;
foreach (var outer in source)
{
found = false;
foreach (var inner in excludeList)
{
if (outer.Equals(inner))
{
found = true;
break;
}
}
if (!found)
{
2020-10-31 19:16:28 +00:00
results.AddItem(outer);
2020-10-31 18:21:46 +00:00
}
}
return results;
}
/// <summary>
/// Returns all items that co-exist in this object and target.
/// </summary>
2020-11-16 19:37:38 +00:00
/// <param name="source">The <see cref="Collection{IPObject}"/>.</param>
2020-10-31 18:21:46 +00:00
/// <param name="target">Collection to compare with.</param>
/// <returns>A collection containing all the matches.</returns>
2020-11-16 19:37:38 +00:00
public static Collection<IPObject> Union(this Collection<IPObject> source, Collection<IPObject> target)
2020-10-31 18:21:46 +00:00
{
if (source.Count == 0)
{
2020-11-16 19:37:38 +00:00
return new Collection<IPObject>();
2020-10-31 18:21:46 +00:00
}
if (target == null)
{
throw new ArgumentNullException(nameof(target));
}
2020-11-16 19:37:38 +00:00
Collection<IPObject> nc = new Collection<IPObject>();
2020-10-31 18:21:46 +00:00
foreach (IPObject i in source)
{
if (target.ContainsAddress(i))
{
2020-10-31 19:16:28 +00:00
nc.AddItem(i);
2020-10-31 18:21:46 +00:00
}
}
return nc;
}
}
}