Apply review suggestions

This commit is contained in:
Shadowghost 2023-07-04 14:35:51 +02:00
parent e56275fb46
commit 2bc2848b8e
1 changed files with 6 additions and 13 deletions

View File

@ -19,7 +19,7 @@ public static partial class NetworkExtensions
// Use regular expression as CheckHostName isn't RFC5892 compliant. // Use regular expression as CheckHostName isn't RFC5892 compliant.
// Modified from gSkinner's expression at https://stackoverflow.com/questions/11809631/fully-qualified-domain-name-validation // Modified from gSkinner's expression at https://stackoverflow.com/questions/11809631/fully-qualified-domain-name-validation
[GeneratedRegex(@"(?im)^(?!:\/\/)(?=.{1,255}$)((.{1,63}\.){0,127}(?![0-9]*$)[a-z0-9-]+\.?)(:(\d){1,5}){0,1}$", RegexOptions.IgnoreCase, "en-US")] [GeneratedRegex(@"(?im)^(?!:\/\/)(?=.{1,255}$)((.{1,63}\.){0,127}(?![0-9]*$)[a-z0-9-]+\.?)(:(\d){1,5}){0,1}$", RegexOptions.IgnoreCase, "en-US")]
private static partial Regex fqdnGeneratedRegex(); private static partial Regex FqdnGeneratedRegex();
/// <summary> /// <summary>
/// Returns true if the IPAddress contains an IP6 Local link address. /// Returns true if the IPAddress contains an IP6 Local link address.
@ -256,14 +256,13 @@ public static partial class NetworkExtensions
/// <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)
{ {
host = host.Trim();
if (host.IsEmpty) if (host.IsEmpty)
{ {
addresses = null; addresses = null;
return false; return false;
} }
host = host.Trim();
// See if it's an IPv6 with port address e.g. [::1] or [::1]:120. // See if it's an IPv6 with port address e.g. [::1] or [::1]:120.
if (host[0] == '[') if (host[0] == '[')
{ {
@ -286,7 +285,7 @@ public static partial class NetworkExtensions
if (hosts.Count <= 2) if (hosts.Count <= 2)
{ {
// Is hostname or hostname:port // Is hostname or hostname:port
if (fqdnGeneratedRegex().IsMatch(hosts[0])) if (FqdnGeneratedRegex().IsMatch(hosts[0]))
{ {
try try
{ {
@ -295,8 +294,7 @@ public static partial class NetworkExtensions
} }
catch (SocketException) catch (SocketException)
{ {
// Log and then ignore socket errors, as the result value will just be an empty array. // Ignore socket errors, as the result value will just be an empty array.
Console.WriteLine("GetHostAddresses failed.");
} }
} }
@ -337,14 +335,9 @@ public static partial class NetworkExtensions
public static IPAddress GetBroadcastAddress(IPNetwork network) public static IPAddress GetBroadcastAddress(IPNetwork network)
{ {
var addressBytes = network.Prefix.GetAddressBytes(); var addressBytes = network.Prefix.GetAddressBytes();
if (BitConverter.IsLittleEndian) uint ipAddress = BitConverter.ToUInt32(addressBytes, 0);
{
addressBytes = addressBytes.Reverse().ToArray();
}
uint iPAddress = BitConverter.ToUInt32(addressBytes, 0);
uint ipMaskV4 = BitConverter.ToUInt32(CidrToMask(network.PrefixLength, AddressFamily.InterNetwork).GetAddressBytes(), 0); uint ipMaskV4 = BitConverter.ToUInt32(CidrToMask(network.PrefixLength, AddressFamily.InterNetwork).GetAddressBytes(), 0);
uint broadCastIPAddress = iPAddress | ~ipMaskV4; uint broadCastIPAddress = ipAddress | ~ipMaskV4;
return new IPAddress(BitConverter.GetBytes(broadCastIPAddress)); return new IPAddress(BitConverter.GetBytes(broadCastIPAddress));
} }