Skip to content

Instantly share code, notes, and snippets.

@wojciech-kulik
Last active November 26, 2015 17:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wojciech-kulik/62db89e0ece95ea17779 to your computer and use it in GitHub Desktop.
Save wojciech-kulik/62db89e0ece95ea17779 to your computer and use it in GitHub Desktop.
IPConnectivity
// the order is important, if we want to support bitwise OR: IPv4 | IPv6 equals IPv4and6
public enum IpVersion
{
None,
IPv4,
IPv6,
IPv4and6
}
public async Task<IpVersion> GetCurrentIpVersion()
{
try
{
// resolves domain name to IP addresses (may contain several)
var endPointPairs = await DatagramSocket.GetEndpointPairsAsync(new HostName("google.com"), "0");
if (endPointPairs == null)
{
return IpVersion.None;
}
// detect which IP version is supported
var result = IpVersion.None;
foreach (var endPoint in endPointPairs)
{
if (endPoint.RemoteHostName != null)
{
if (endPoint.RemoteHostName.Type == HostNameType.Ipv4)
{
result |= IpVersion.IPv4;
}
else if (endPoint.RemoteHostName.Type == HostNameType.Ipv6)
{
result |= IpVersion.IPv6;
}
}
}
return result;
}
catch
{
return IpVersion.None;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment