IPUtil.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Net;
  5. using System.Net.Sockets;
  6. using System.Text;
  7. namespace CommonLang.Net
  8. {
  9. public static class IPUtil
  10. {
  11. // public static IPEndPoint ToEndPoint(Uri uri)
  12. // {
  13. // IPAddress[] ip = Dns.GetHostAddresses(uri.Host);
  14. // IPEndPoint ret = new IPEndPoint(ip[0], uri.Port);
  15. // return ret;
  16. // }
  17. // public static IPEndPoint ToEndPoint(string host, int port)
  18. // {
  19. // IPAddress[] ip = Dns.GetHostAddresses(host);
  20. // IPEndPoint ret = new IPEndPoint(ip[0], port);
  21. // return ret;
  22. // }
  23. public static IPAddress ToExpectAddress(string host, AddressFamily family)
  24. {
  25. IPAddress[] ips = Dns.GetHostAddresses(host);
  26. foreach (var ip in ips)
  27. {
  28. if (ip.AddressFamily == family)
  29. {
  30. return ip;
  31. }
  32. }
  33. return null;
  34. }
  35. /// <summary>
  36. /// 用System.net.dns类中的GetHostEntry()和GetHostName()方法来建立一个本
  37. /// 地IP地址,并且建立了一个IPHostEntry对象。用以下两句代码来获取本地IP
  38. /// 地址
  39. /// </summary>
  40. /// <returns></returns>
  41. public static IPEndPoint CreateLocalConnectorEndPoint()
  42. {
  43. return new IPEndPoint(IPAddress.IPv6Any, 0);
  44. }
  45. public static IPEndPoint CreateLocalListenEndPoint(int port)
  46. {
  47. return new IPEndPoint(IPAddress.IPv6Any, port);
  48. }
  49. public static bool IsOnlyIPv6(IPHostEntry ips)
  50. {
  51. if (ips.AddressList.Length == 0)
  52. {
  53. return false;
  54. }
  55. foreach (var ip in ips.AddressList)
  56. {
  57. if (ip.AddressFamily != AddressFamily.InterNetworkV6)
  58. {
  59. return false;
  60. }
  61. }
  62. return true;
  63. }
  64. public static IPAddress MapToIPv6(this IPAddress addr)
  65. {
  66. if (addr.AddressFamily != AddressFamily.InterNetwork)
  67. throw new ArgumentException("Must pass an IPv4 address to MapToIPv6");
  68. string ipv4str = addr.ToString();
  69. return IPAddress.Parse("::ffff:" + ipv4str);
  70. }
  71. public static IPAddress MapToIPv6(this string ipv4str)
  72. {
  73. return IPAddress.Parse("::ffff:" + ipv4str);
  74. }
  75. public static bool IsIPv4MappedToIPv6(this IPAddress addr)
  76. {
  77. bool pass1 = addr.AddressFamily == System.Net.Sockets.AddressFamily.InterNetworkV6, pass2;
  78. try
  79. {
  80. pass2 = (addr.ToString().StartsWith("0000:0000:0000:0000:0000:ffff:") ||
  81. addr.ToString().StartsWith("0:0:0:0:0:ffff:") ||
  82. addr.ToString().StartsWith("::ffff:")) &&
  83. IPAddress.Parse(addr.ToString().Substring(addr.ToString().LastIndexOf(":") + 1)).AddressFamily == AddressFamily.InterNetwork;
  84. }
  85. catch
  86. {
  87. return false;
  88. }
  89. return pass1 && pass2;
  90. }
  91. }
  92. }