This commit is contained in:
jsaey 2023-11-20 14:57:29 +01:00
parent 2dca7a7f4f
commit 2e302c1909
3 changed files with 20 additions and 6 deletions

View File

@ -6,6 +6,7 @@ using System.Collections.Generic;
using System.Collections.Specialized;
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Net.Sockets;
using System.Text;
@ -1322,7 +1323,7 @@ namespace EonaCat.Network
return string.Format("{0}; {1}", m, parameters.ToString("; "));
}
internal static System.Net.IPAddress ToIPAddress(this string value)
internal static System.Net.IPAddress ToIPAddress(this string value, bool useIPv6 = false)
{
if (value == null || value.Length == 0)
{
@ -1337,7 +1338,19 @@ namespace EonaCat.Network
try
{
var addrs = System.Net.Dns.GetHostAddresses(value);
return addrs[0];
if (useIPv6)
{
// Find the first IPv6 address in the list, if available
var ipv6Addr = addrs.FirstOrDefault(a => a.AddressFamily == System.Net.Sockets.AddressFamily.InterNetworkV6);
if (ipv6Addr != null)
{
return ipv6Addr;
}
}
// If IPv6 is not requested or not available, return the first IPv4 address
return addrs.FirstOrDefault(a => a.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork);
}
catch
{
@ -1345,6 +1358,7 @@ namespace EonaCat.Network
}
}
internal static List<TSource> ToList<TSource>(this IEnumerable<TSource> source)
{
return new List<TSource>(source);

View File

@ -28,7 +28,7 @@ namespace EonaCat.Network
{
}
public HttpServer(string url)
public HttpServer(string url, bool forceIpV6 = false)
{
if (url == null)
{
@ -47,7 +47,7 @@ namespace EonaCat.Network
var host = uri.GetDnsSafeHost(true);
var addr = host.ToIPAddress();
var addr = host.ToIPAddress(forceIpV6);
if (addr == null)
{
message = "The host part could not be converted to an IP address.";

View File

@ -37,7 +37,7 @@ namespace EonaCat.Network
{
}
public WSServer(string url)
public WSServer(string url, bool forceIpV6 = false)
{
if (url == null)
{
@ -56,7 +56,7 @@ namespace EonaCat.Network
var host = uri.DnsSafeHost;
var addr = host.ToIPAddress();
var addr = host.ToIPAddress(forceIpV6);
if (addr == null)
{
message = "The host part could not be converted to an IP address.";