Last active
November 26, 2015 17:42
-
-
Save wojciech-kulik/62db89e0ece95ea17779 to your computer and use it in GitHub Desktop.
IPConnectivity
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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