Apply review suggestions

This commit is contained in:
Shadowghost 2023-07-02 12:34:50 +02:00
parent a6e9824e8a
commit 9b0e44019a
4 changed files with 30 additions and 26 deletions

View File

@ -1,5 +1,6 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Globalization; using System.Globalization;
using System.Linq; using System.Linq;
using System.Net; using System.Net;
@ -380,7 +381,7 @@ namespace Jellyfin.Networking.Manager
// Remove all interfaces matching any virtual machine interface prefix // Remove all interfaces matching any virtual machine interface prefix
if (config.IgnoreVirtualInterfaces) if (config.IgnoreVirtualInterfaces)
{ {
// Remove potentially exisiting * and split config string into prefixes // Remove potentially existing * and split config string into prefixes
var virtualInterfacePrefixes = config.VirtualInterfaceNames var virtualInterfacePrefixes = config.VirtualInterfaceNames
.Select(i => i.Replace("*", string.Empty, StringComparison.OrdinalIgnoreCase)); .Select(i => i.Replace("*", string.Empty, StringComparison.OrdinalIgnoreCase));
@ -587,7 +588,7 @@ namespace Jellyfin.Networking.Manager
} }
/// <inheritdoc/> /// <inheritdoc/>
public bool TryParseInterface(string intf, out IReadOnlyList<IPData> result) public bool TryParseInterface(string intf, [NotNullWhen(true)] out IReadOnlyList<IPData>? result)
{ {
var resultList = new List<IPData>(); var resultList = new List<IPData>();
if (string.IsNullOrEmpty(intf) || _interfaces is null) if (string.IsNullOrEmpty(intf) || _interfaces is null)
@ -660,7 +661,7 @@ namespace Jellyfin.Networking.Manager
/// <inheritdoc/> /// <inheritdoc/>
public IReadOnlyList<IPData> GetLoopbacks() public IReadOnlyList<IPData> GetLoopbacks()
{ {
if (!(IsIPv4Enabled && IsIPv4Enabled)) if (!IsIPv4Enabled && !IsIPv6Enabled)
{ {
return Array.Empty<IPData>(); return Array.Empty<IPData>();
} }

View File

@ -1,5 +1,6 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Net; using System.Net;
using System.Net.NetworkInformation; using System.Net.NetworkInformation;
using MediaBrowser.Model.Net; using MediaBrowser.Model.Net;
@ -120,7 +121,7 @@ namespace MediaBrowser.Common.Net
/// <param name="intf">Interface name.</param> /// <param name="intf">Interface name.</param>
/// <param name="result">Resulting object's IP addresses, if successful.</param> /// <param name="result">Resulting object's IP addresses, if successful.</param>
/// <returns>Success of the operation.</returns> /// <returns>Success of the operation.</returns>
bool TryParseInterface(string intf, out IReadOnlyList<IPData> result); bool TryParseInterface(string intf, [NotNullWhen(true)] out IReadOnlyList<IPData>? result);
/// <summary> /// <summary>
/// Returns all internal (LAN) bind interface addresses. /// Returns all internal (LAN) bind interface addresses.

View File

@ -166,28 +166,30 @@ namespace MediaBrowser.Common.Net
/// <param name="result">Collection of <see cref="IPNetwork"/>.</param> /// <param name="result">Collection of <see cref="IPNetwork"/>.</param>
/// <param name="negated">Boolean signaling if negated or not negated values should be parsed.</param> /// <param name="negated">Boolean signaling if negated or not negated values should be parsed.</param>
/// <returns><c>True</c> if parsing was successful.</returns> /// <returns><c>True</c> if parsing was successful.</returns>
public static bool TryParseToSubnets(string[] values, out List<IPNetwork> result, bool negated = false) public static bool TryParseToSubnets(string[] values, [NotNullWhen(true)] out IReadOnlyList<IPNetwork>? result, bool negated = false)
{ {
result = new List<IPNetwork>();
if (values is null || values.Length == 0) if (values is null || values.Length == 0)
{ {
result = null;
return false; return false;
} }
var tmpResult = new List<IPNetwork>();
for (int a = 0; a < values.Length; a++) for (int a = 0; a < values.Length; a++)
{ {
if (TryParseToSubnet(values[a], out var innerResult, negated)) if (TryParseToSubnet(values[a], out var innerResult, negated))
{ {
result.Add(innerResult); tmpResult.Add(innerResult);
} }
} }
if (result.Count > 0) if (tmpResult.Count > 0)
{ {
result = tmpResult;
return true; return true;
} }
result = null;
return false; return false;
} }
@ -199,9 +201,8 @@ namespace MediaBrowser.Common.Net
/// <param name="result">An <see cref="IPNetwork"/>.</param> /// <param name="result">An <see cref="IPNetwork"/>.</param>
/// <param name="negated">Boolean signaling if negated or not negated values should be parsed.</param> /// <param name="negated">Boolean signaling if negated or not negated values should be parsed.</param>
/// <returns><c>True</c> if parsing was successful.</returns> /// <returns><c>True</c> if parsing was successful.</returns>
public static bool TryParseToSubnet(ReadOnlySpan<char> value, out IPNetwork result, bool negated = false) public static bool TryParseToSubnet(ReadOnlySpan<char> value, [NotNullWhen(true)] out IPNetwork? result, bool negated = false)
{ {
result = new IPNetwork(IPAddress.None, 32);
var splitString = value.Trim().Split('/'); var splitString = value.Trim().Split('/');
if (splitString.MoveNext()) if (splitString.MoveNext())
{ {
@ -224,25 +225,28 @@ namespace MediaBrowser.Common.Net
if (int.TryParse(subnetBlock, out var netmask)) if (int.TryParse(subnetBlock, out var netmask))
{ {
result = new IPNetwork(address, netmask); result = new IPNetwork(address, netmask);
return true;
} }
else if (IPAddress.TryParse(subnetBlock, out var netmaskAddress)) else if (IPAddress.TryParse(subnetBlock, out var netmaskAddress))
{ {
result = new IPNetwork(address, NetworkExtensions.MaskToCidr(netmaskAddress)); result = new IPNetwork(address, NetworkExtensions.MaskToCidr(netmaskAddress));
return true;
} }
} }
else if (address.AddressFamily == AddressFamily.InterNetwork) else if (address.AddressFamily == AddressFamily.InterNetwork)
{ {
result = new IPNetwork(address, 32); result = new IPNetwork(address, 32);
return true;
} }
else if (address.AddressFamily == AddressFamily.InterNetworkV6) else if (address.AddressFamily == AddressFamily.InterNetworkV6)
{ {
result = new IPNetwork(address, 128); result = new IPNetwork(address, 128);
}
return true; return true;
} }
} }
}
result = null;
return false; return false;
} }
@ -254,11 +258,11 @@ namespace MediaBrowser.Common.Net
/// <param name="isIPv4Enabled"><c>true</c> if IPv4 is enabled.</param> /// <param name="isIPv4Enabled"><c>true</c> if IPv4 is enabled.</param>
/// <param name="isIPv6Enabled"><c>true</c> if IPv6 is enabled.</param> /// <param name="isIPv6Enabled"><c>true</c> if IPv6 is enabled.</param>
/// <returns><c>true</c> if the parsing is successful, <c>false</c> if not.</returns> /// <returns><c>true</c> if the parsing is successful, <c>false</c> if not.</returns>
public static bool TryParseHost(ReadOnlySpan<char> host, [NotNullWhen(true)] out IPAddress[] addresses, bool isIPv4Enabled = true, bool isIPv6Enabled = false) public static bool TryParseHost(ReadOnlySpan<char> host, [NotNullWhen(true)] out IPAddress[]? addresses, bool isIPv4Enabled = true, bool isIPv6Enabled = false)
{ {
if (host.IsEmpty) if (host.IsEmpty)
{ {
addresses = Array.Empty<IPAddress>(); addresses = null;
return false; return false;
} }
@ -301,9 +305,7 @@ namespace MediaBrowser.Common.Net
} }
// Is an IP4 or IP4:port // Is an IP4 or IP4:port
host = hosts[0].Split('/')[0]; if (IPAddress.TryParse(hosts[0].AsSpan().LeftPart('/'), out var address))
if (IPAddress.TryParse(host, out var address))
{ {
if (((address.AddressFamily == AddressFamily.InterNetwork) && (!isIPv4Enabled && isIPv6Enabled)) || if (((address.AddressFamily == AddressFamily.InterNetwork) && (!isIPv4Enabled && isIPv6Enabled)) ||
((address.AddressFamily == AddressFamily.InterNetworkV6) && (isIPv4Enabled && !isIPv6Enabled))) ((address.AddressFamily == AddressFamily.InterNetworkV6) && (isIPv4Enabled && !isIPv6Enabled)))
@ -318,10 +320,9 @@ namespace MediaBrowser.Common.Net
return true; return true;
} }
} }
else if (hosts.Count <= 9) // 8 octets + port else if (hosts.Count > 0 && hosts.Count <= 9) // 8 octets + port
{ {
var splitSpan = host.Split('/'); if (IPAddress.TryParse(host.LeftPart('/'), out var address))
if (splitSpan.MoveNext() && IPAddress.TryParse(splitSpan.Current, out var address))
{ {
addresses = new[] { address }; addresses = new[] { address };
return true; return true;

View File

@ -203,12 +203,13 @@ namespace Jellyfin.Networking.Tests
using var nm = new NetworkManager(GetMockConfig(conf), new NullLogger<NetworkManager>()); using var nm = new NetworkManager(GetMockConfig(conf), new NullLogger<NetworkManager>());
NetworkManager.MockNetworkSettings = string.Empty; NetworkManager.MockNetworkSettings = string.Empty;
_ = nm.TryParseInterface(result, out IReadOnlyList<IPData>? resultObj); // Check to see if DNS resolution is working. If not, skip test.
if (!NetworkExtensions.TryParseHost(source, out var host))
{
return;
}
// Check to see if dns resolution is working. If not, skip test. if (nm.TryParseInterface(result, out var resultObj))
_ = NetworkExtensions.TryParseHost(source, out var host);
if (resultObj is not null && host.Length > 0)
{ {
result = resultObj.First().Address.ToString(); result = resultObj.First().Address.ToString();
var intf = nm.GetBindAddress(source, out _); var intf = nm.GetBindAddress(source, out _);