diff --git a/EonaCat.Network/System/Sockets/Web/Extensions.cs b/EonaCat.Network/System/Sockets/Web/Extensions.cs index d5248e1..77f673b 100644 --- a/EonaCat.Network/System/Sockets/Web/Extensions.cs +++ b/EonaCat.Network/System/Sockets/Web/Extensions.cs @@ -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 ToList(this IEnumerable source) { return new List(source); diff --git a/EonaCat.Network/System/Sockets/Web/Server/HttpServer.cs b/EonaCat.Network/System/Sockets/Web/Server/HttpServer.cs index c4a140c..fefac57 100644 --- a/EonaCat.Network/System/Sockets/Web/Server/HttpServer.cs +++ b/EonaCat.Network/System/Sockets/Web/Server/HttpServer.cs @@ -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."; diff --git a/EonaCat.Network/System/Sockets/Web/Server/WSServer.cs b/EonaCat.Network/System/Sockets/Web/Server/WSServer.cs index 08a1d51..d4cd042 100644 --- a/EonaCat.Network/System/Sockets/Web/Server/WSServer.cs +++ b/EonaCat.Network/System/Sockets/Web/Server/WSServer.cs @@ -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.";