IPAdress.cs 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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 uint address;
  15. public const uint InaddrAny = 0;
  16. public const uint InaddrBroadcast = 0xffffffff;
  17. public const uint InaddrLoopback = 0x7f000001;
  18. public const uint InaddrNone = 0xffffffff;
  19. // <summary>
  20. // Constructor from a 32-bit constant.
  21. // </summary>
  22. public IPAddress (uint address)
  23. {
  24. this.address = address;
  25. }
  26. // <summary>
  27. // Constructor from a dotted quad notation.
  28. // </summary>
  29. public IPAddress (string ip)
  30. {
  31. string[] ips = ip.Split (new char[] {'.'});
  32. int i;
  33. uint a = 0;
  34. for (i = 0; i < ips.Length; i++)
  35. a = (a << 8) | (UInt16.Parse(ips [i]));
  36. address = a;
  37. }
  38. // <summary>
  39. // Used to tell whether an address is a loopback.
  40. // </summary>
  41. // <param name="addr">Address to compare</param>
  42. // <returns></returns>
  43. public static bool IsLoopback (IPAddress addr)
  44. {
  45. return addr.address == InaddrLoopback;
  46. }
  47. // <summary>
  48. // Overrides System.Object.ToString to return
  49. // this object rendered in a quad-dotted notation
  50. // </summary>
  51. public override string ToString ()
  52. {
  53. return ToString (address);
  54. }
  55. // <summary>
  56. // Returns this object rendered in a quad-dotted notation
  57. // </summary>
  58. public static string ToString (uint addr)
  59. {
  60. return (addr >> 24).ToString () + "." +
  61. ((addr >> 16) & 0xff).ToString () + "." +
  62. ((addr >> 8) & 0xff).ToString () + "." +
  63. (addr & 0xff).ToString ();
  64. }
  65. // <returns>
  66. // Whether both objects are equal.
  67. // </returns>
  68. public override bool Equals (object other)
  69. {
  70. if (other is System.Net.IPAddress){
  71. return address == ((System.Net.IPAddress) other).address;
  72. }
  73. return false;
  74. }
  75. public override int GetHashCode ()
  76. {
  77. return (int)address;
  78. }
  79. }
  80. }