IPAddress.cs 5.6 KB

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