IPAddress.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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. //
  10. // Note: the address is stored in host order
  11. using System.Net.Sockets;
  12. using System.Runtime.InteropServices;
  13. using System;
  14. namespace System.Net {
  15. /// <remarks>
  16. /// Encapsulates an IP Address.
  17. /// </remarks>
  18. [Serializable]
  19. public class IPAddress {
  20. // Don't change the name of this field without also
  21. // changing socket-io.c in the runtime
  22. private long address;
  23. public static readonly IPAddress Any = new IPAddress(0);
  24. public static readonly IPAddress Broadcast = IPAddress.Parse ("255.255.255.255");
  25. public static readonly IPAddress Loopback = IPAddress.Parse ("127.0.0.1");
  26. public static readonly IPAddress None = IPAddress.Parse ("255.255.255.255");
  27. private static short SwapShort (short number)
  28. {
  29. return (short) ( ((number >> 8) & 0xFF) + ((number << 8) & 0xFF00) );
  30. }
  31. private static int SwapInt (int number)
  32. {
  33. byte b0 = (byte) ((number >> 24) & 0xFF);
  34. byte b1 = (byte) ((number >> 16) & 0xFF);
  35. byte b2 = (byte) ((number >> 8) & 0xFF);
  36. byte b3 = (byte) (number & 0xFF);
  37. return b0 + (b1 << 8) + (b2 << 16) + (b3 << 24);
  38. }
  39. private static long SwapLong (long number)
  40. {
  41. byte b0 = (byte) ((number >> 56) & 0xFF);
  42. byte b1 = (byte) ((number >> 48) & 0xFF);
  43. byte b2 = (byte) ((number >> 40) & 0xFF);
  44. byte b3 = (byte) ((number >> 32) & 0xFF);
  45. byte b4 = (byte) ((number >> 24) & 0xFF);
  46. byte b5 = (byte) ((number >> 16) & 0xFF);
  47. byte b6 = (byte) ((number >> 8) & 0xFF);
  48. byte b7 = (byte) (number & 0xFF);
  49. return (long) b0 + ((long) b1 << 8) + ((long) b2 << 16) + ((long) b3 << 24) + ((long) b4 << 32) + ((long) b5 << 40) + ((long) b6 << 48) + ((long) b7 << 56);
  50. }
  51. public static short HostToNetworkOrder(short host) {
  52. if (!BitConverter.IsLittleEndian)
  53. return(host);
  54. return SwapShort (host);
  55. }
  56. public static int HostToNetworkOrder(int host) {
  57. if (!BitConverter.IsLittleEndian)
  58. return(host);
  59. return SwapInt (host);
  60. }
  61. public static long HostToNetworkOrder(long host) {
  62. if (!BitConverter.IsLittleEndian)
  63. return(host);
  64. return SwapLong (host);
  65. }
  66. public static short NetworkToHostOrder(short network) {
  67. if (!BitConverter.IsLittleEndian)
  68. return(network);
  69. return SwapShort (network);
  70. }
  71. public static int NetworkToHostOrder(int network) {
  72. if (!BitConverter.IsLittleEndian)
  73. return(network);
  74. return SwapInt (network);
  75. }
  76. public static long NetworkToHostOrder(long network) {
  77. if (!BitConverter.IsLittleEndian)
  78. return(network);
  79. return SwapLong (network);
  80. }
  81. /// <summary>
  82. /// Constructor from a 32-bit constant with its bytes
  83. /// in network order.
  84. /// </summary>
  85. public IPAddress (long addr)
  86. {
  87. Address = addr;
  88. }
  89. public static IPAddress Parse(string ip)
  90. {
  91. if(ip == null)
  92. throw new ArgumentNullException("null ip string");
  93. int pos = 0;
  94. int ndots = 0;
  95. char current;
  96. bool prevDigit = false;
  97. while (pos < ip.Length) {
  98. current = ip [pos++];
  99. if (Char.IsDigit (current))
  100. prevDigit = true;
  101. else
  102. if (current == '.') {
  103. // No more than 3 dots. Doesn't allow ending with a dot.
  104. if (++ndots > 3 || pos == ip.Length || prevDigit == false)
  105. throw new FormatException ("the string is not a valid ip");
  106. prevDigit = false;
  107. }
  108. else if (!Char.IsDigit (current)) {
  109. if (!Char.IsWhiteSpace (current))
  110. throw new FormatException ("the string is not a valid ip");
  111. // The same as MS does
  112. if (pos == 1)
  113. return new IPAddress (0);
  114. break;
  115. }
  116. }
  117. if (ndots != 3)
  118. throw new FormatException ("the string is not a valid ip");
  119. long a = 0;
  120. string [] ips = ip.Split (new char [] {'.'});
  121. // Make the number in network order
  122. for (int i = ips.Length - 1; i >= 0; i--)
  123. a = (a << 8) | (Byte.Parse(ips [i]));
  124. return (new IPAddress (a));
  125. }
  126. public long Address {
  127. get {
  128. return address;
  129. }
  130. set {
  131. /* no need to do this test, ms.net accepts any value.
  132. if (value < 0 || value > 0x00000000FFFFFFFF)
  133. throw new ArgumentOutOfRangeException (
  134. "the address must be between 0 and 0xFFFFFFFF");
  135. */
  136. address = value;
  137. }
  138. }
  139. public AddressFamily AddressFamily {
  140. get {
  141. return(AddressFamily.InterNetwork);
  142. }
  143. }
  144. /// <summary>
  145. /// Used to tell whether an address is a loopback.
  146. /// All IP addresses of the form 127.X.Y.Z, where X, Y, and Z are in
  147. /// the range 0-255, are loopback addresses.
  148. /// </summary>
  149. /// <param name="addr">Address to compare</param>
  150. /// <returns></returns>
  151. public static bool IsLoopback (IPAddress addr)
  152. {
  153. return (addr.address & 0xFF) == 127;
  154. }
  155. /// <summary>
  156. /// Overrides System.Object.ToString to return
  157. /// this object rendered in a quad-dotted notation
  158. /// </summary>
  159. public override string ToString ()
  160. {
  161. return ToString (address);
  162. }
  163. /// <summary>
  164. /// Returns this object rendered in a quad-dotted notation
  165. /// </summary>
  166. static string ToString (long addr)
  167. {
  168. // addr is in network order
  169. return (addr & 0xff).ToString () + "." +
  170. ((addr >> 8) & 0xff).ToString () + "." +
  171. ((addr >> 16) & 0xff).ToString () + "." +
  172. ((addr >> 24) & 0xff).ToString ();
  173. }
  174. /// <returns>
  175. /// Whether both objects are equal.
  176. /// </returns>
  177. public override bool Equals (object other)
  178. {
  179. if (other is System.Net.IPAddress){
  180. return Address == ((System.Net.IPAddress) other).Address;
  181. }
  182. return false;
  183. }
  184. public override int GetHashCode ()
  185. {
  186. return (int)Address;
  187. }
  188. }
  189. }