IPAddress.cs 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. //
  2. // System.Net.IPAddress.cs
  3. //
  4. // Author:
  5. // Miguel de Icaza ([email protected])
  6. //
  7. // (C) Ximian, Inc. http://www.ximian.com
  8. //
  9. namespace System.Net {
  10. /// <remarks>
  11. /// Encapsulates an IP Address.
  12. /// </remarks>
  13. public class IPAddress {
  14. public long Address;
  15. public static readonly IPAddress Any;
  16. public static readonly IPAddress Broadcast;
  17. public static readonly IPAddress Loopback;
  18. public static readonly IPAddress None;
  19. public const uint InaddrAny = 0;
  20. public const uint InaddrBroadcast = 0xffffffff;
  21. public const uint InaddrLoopback = 0x7f000001;
  22. public const uint InaddrNone = 0xffffffff;
  23. static IPAddress ()
  24. {
  25. Any.Address = InaddrAny;
  26. Broadcast.Address = InaddrBroadcast;
  27. Loopback.Address = InaddrLoopback;
  28. None.Address = InaddrNone;
  29. }
  30. /// <summary>
  31. /// Constructor from a 32-bit constant.
  32. /// </summary>
  33. public IPAddress (long address)
  34. {
  35. this.Address = address;
  36. }
  37. /// <summary>
  38. /// Constructor from a dotted quad notation.
  39. /// </summary>
  40. public IPAddress (string ip)
  41. {
  42. string[] ips = ip.Split (new char[] {'.'});
  43. int i;
  44. uint a = 0;
  45. for (i = 0; i < ips.Length; i++)
  46. a = (a << 8) | (UInt16.Parse(ips [i]));
  47. Address = a;
  48. }
  49. /// <summary>
  50. /// Used to tell whether an address is a loopback.
  51. /// </summary>
  52. /// <param name="addr">Address to compare</param>
  53. /// <returns></returns>
  54. public static bool IsLoopback (IPAddress addr)
  55. {
  56. return addr.Address == InaddrLoopback;
  57. }
  58. /// <summary>
  59. /// Overrides System.Object.ToString to return
  60. /// this object rendered in a quad-dotted notation
  61. /// </summary>
  62. public override string ToString ()
  63. {
  64. return ToString (Address);
  65. }
  66. /// <summary>
  67. /// Returns this object rendered in a quad-dotted notation
  68. /// </summary>
  69. static string ToString (long addr)
  70. {
  71. return (addr >> 24).ToString () + "." +
  72. ((addr >> 16) & 0xff).ToString () + "." +
  73. ((addr >> 8) & 0xff).ToString () + "." +
  74. (addr & 0xff).ToString ();
  75. }
  76. /// <returns>
  77. /// Whether both objects are equal.
  78. /// </returns>
  79. public override bool Equals (object other)
  80. {
  81. if (other is System.Net.IPAddress){
  82. return Address == ((System.Net.IPAddress) other).Address;
  83. }
  84. return false;
  85. }
  86. public override int GetHashCode ()
  87. {
  88. return (int)Address;
  89. }
  90. [MonoTODO]
  91. public static short HostToNetworkOrder (short host)
  92. {
  93. return host;
  94. }
  95. [MonoTODO]
  96. public static int HostToNetworkOrder (int host)
  97. {
  98. return host;
  99. }
  100. [MonoTODO]
  101. public static long HostToNetworkOrder (long host)
  102. {
  103. return host;
  104. }
  105. [MonoTODO]
  106. public static short NetworkToHostOrder (short host)
  107. {
  108. return host;
  109. }
  110. [MonoTODO]
  111. public static int NetworkToHostOrder (int host)
  112. {
  113. return host;
  114. }
  115. [MonoTODO]
  116. public static long NetworkToHostOrder (long host)
  117. {
  118. return host;
  119. }
  120. }
  121. }