Advertisement
Guest User

Untitled

a guest
Nov 25th, 2015
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.96 KB | None | 0 0
  1. // the order is important, if we want to support bitwise OR: IPv4 | IPv6 equals IPv4and6
  2. public enum IpVersion
  3. {
  4. None,
  5. IPv4,
  6. IPv6,
  7. IPv4and6
  8. }
  9.  
  10. public async Task<IpVersion> GetCurrentIpVersion()
  11. {
  12. try
  13. {
  14. // resolves domain name to IP addresses (may contain several)
  15. var endPointPairs = await DatagramSocket.GetEndpointPairsAsync(new HostName("google.com"), "0");
  16. if (endPointPairs == null)
  17. {
  18. return IpVersion.None;
  19. }
  20.  
  21. var result = IpVersion.None;
  22. foreach (var endPoint in endPointPairs)
  23. {
  24. if (endPoint.RemoteHostName != null && (endPoint.RemoteHostName.Type == HostNameType.Ipv4 || endPoint.RemoteHostName.Type == HostNameType.Ipv6))
  25. {
  26. result |= (endPoint.RemoteHostName.Type == HostNameType.Ipv4 ? IpVersion.IPv4 : IpVersion.IPv6);
  27. }
  28. }
  29. return result;
  30. }
  31. catch
  32. {
  33. return IpVersion.None;
  34. }
  35. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement