From f6d6f0367bf62435dfaf7d122415d31977f889aa Mon Sep 17 00:00:00 2001 From: Shadowghost Date: Mon, 17 Oct 2022 15:38:42 +0200 Subject: Properly handle IPs with subnetmasks --- MediaBrowser.Common/Net/NetworkExtensions.cs | 26 ++++++++++++++++++-------- 1 file changed, 18 insertions(+), 8 deletions(-) (limited to 'MediaBrowser.Common/Net/NetworkExtensions.cs') diff --git a/MediaBrowser.Common/Net/NetworkExtensions.cs b/MediaBrowser.Common/Net/NetworkExtensions.cs index 1c2b65346..e37a5dc6b 100644 --- a/MediaBrowser.Common/Net/NetworkExtensions.cs +++ b/MediaBrowser.Common/Net/NetworkExtensions.cs @@ -152,13 +152,14 @@ namespace MediaBrowser.Common.Net } /// - /// Try parsing an array of strings into subnets, respecting exclusions. + /// Try parsing an array of strings into objects, respecting exclusions. + /// Elements without a subnet mask will be represented as with a single IP. /// /// Input string array to be parsed. /// Collection of . /// Boolean signaling if negated or not negated values should be parsed. /// True if parsing was successful. - public static bool TryParseSubnets(string[] values, out List result, bool negated = false) + public static bool TryParseToSubnets(string[] values, out List result, bool negated = false) { result = new List(); @@ -183,10 +184,14 @@ namespace MediaBrowser.Common.Net if (address != IPAddress.None && address != null) { - if (int.TryParse(v[1], out var netmask)) + if (v.Length > 1 && int.TryParse(v[1], out var netmask)) { result.Add(new IPNetwork(address, netmask)); } + else if (v.Length > 1 && IPAddress.TryParse(v[1], out var netmaskAddress)) + { + result.Add(new IPNetwork(address, NetworkExtensions.MaskToCidr(netmaskAddress))); + } else if (address.AddressFamily == AddressFamily.InterNetwork) { result.Add(new IPNetwork(address, 32)); @@ -207,15 +212,16 @@ namespace MediaBrowser.Common.Net } /// - /// Try parsing a string into a subnet, respecting exclusions. + /// Try parsing a string into an , respecting exclusions. + /// Inputs without a subnet mask will be represented as with a single IP. /// /// Input string to be parsed. /// An . /// Boolean signaling if negated or not negated values should be parsed. /// True if parsing was successful. - public static bool TryParseSubnet(string value, out IPNetwork? result, bool negated = false) + public static bool TryParseToSubnet(string value, out IPNetwork result, bool negated = false) { - result = null; + result = new IPNetwork(IPAddress.None, 32); if (string.IsNullOrEmpty(value)) { @@ -236,10 +242,14 @@ namespace MediaBrowser.Common.Net if (address != IPAddress.None && address != null) { - if (int.TryParse(v[1], out var netmask)) + if (v.Length > 1 && int.TryParse(v[1], out var netmask)) { result = new IPNetwork(address, netmask); } + else if (v.Length > 1 && IPAddress.TryParse(v[1], out var netmaskAddress)) + { + result = new IPNetwork(address, NetworkExtensions.MaskToCidr(netmaskAddress)); + } else if (address.AddressFamily == AddressFamily.InterNetwork) { result = new IPNetwork(address, 32); @@ -250,7 +260,7 @@ namespace MediaBrowser.Common.Net } } - if (result != null) + if (!result.Prefix.Equals(IPAddress.None)) { return true; } -- cgit v1.2.3