Since IPEndpoint contains a ToString() method that outputs:
10.10.10.10:1010
There should also be
using System;
using System.Net;
static class Helper {
public static IPEndPoint ToIPEndPoint(this string value, int port = IPEndPoint.MinPort) {
if (string.IsNullOrEmpty(value) || ! IPAddress.TryParse(value, out var address))
return null;
var offset = (value = value.Replace(address.ToString(), string.Empty)).LastIndexOf(':');
if (offset >= 0)
if (! int.TryParse(value.Substring(offset + 1), out port) || port < IPEndPoint.MinPort || port > IPEndPoint.MaxPort)
return null;
return new IPEndPoint(address, port);
}
}
class Program {
static void Main() {
foreach (var sample in new [] {
// See https://docops.ca.com/ca-data-protection-15/en/implementing/platform-deployment/technical-information/ipv6-address-and-port-formats
"192.168.0.3",
"fe80::214:c2ff:fec8:c920",
"10.0.1.53-10.0.1.80",
"10.0",
"10/7",
"2001:0db8:85a3/48",
"192.168.0.5:10",
"[fe80::e828:209d:20e:c0ae]:375",
":137-139",
"192.168:1024-65535",
"[fe80::]-[fe81::]:80"
}) {
var point = sample.ToIPEndPoint();
var report = point == null ? "NULL" : $@"IPEndPoint {{
Address: {point.Address}
AddressFamily: {point.AddressFamily}
Port: {point.Port}
}}";
Console.WriteLine($@"""{sample}"" to IPEndPoint is {report}
");
}
}
}