2 // System.Net.IPAddress.cs
5 // Miguel de Icaza (miguel@ximian.com)
7 // (C) Ximian, Inc. http://www.ximian.com
10 using System.Net.Sockets;
11 using System.Runtime.InteropServices;
15 namespace System.Net {
18 /// Encapsulates an IP Address.
21 public class IPAddress {
22 // Don't change the name of this field without also
23 // changing socket-io.c in the runtime
24 // This will stored in network order
27 public static readonly IPAddress Any=new IPAddress(0);
28 public static readonly IPAddress Broadcast=new IPAddress(0xffffffff);
29 public static readonly IPAddress Loopback=new IPAddress(0x7f000001);
30 public static readonly IPAddress None=new IPAddress(0xffffffff);
32 private static bool isLittleEndian;
34 [StructLayout(LayoutKind.Explicit)]
35 private struct EndianTest
37 [FieldOffset (0)] public byte b0;
38 [FieldOffset (0)] public short s0;
43 EndianTest typeEndian = new EndianTest ();
45 isLittleEndian = typeEndian.b0 == 1;
48 private static short SwapShort (short number)
50 return (short) ( ((number >> 8) & 0xFF) + ((number << 8) & 0xFF00) );
53 private static int SwapInt (int number)
55 byte b0 = (byte) ((number >> 24) & 0xFF);
56 byte b1 = (byte) ((number >> 16) & 0xFF);
57 byte b2 = (byte) ((number >> 8) & 0xFF);
58 byte b3 = (byte) (number & 0xFF);
59 return b0 + (b1 << 8) + (b2 << 16) + (b3 << 24);
62 private static long SwapLong (long number)
64 byte b0 = (byte) ((number >> 56) & 0xFF);
65 byte b1 = (byte) ((number >> 48) & 0xFF);
66 byte b2 = (byte) ((number >> 40) & 0xFF);
67 byte b3 = (byte) ((number >> 32) & 0xFF);
68 byte b4 = (byte) ((number >> 24) & 0xFF);
69 byte b5 = (byte) ((number >> 16) & 0xFF);
70 byte b6 = (byte) ((number >> 8) & 0xFF);
71 byte b7 = (byte) (number & 0xFF);
72 return b0 + (b1 << 8) + (b2 << 16) + (b3 << 24) + (b4 << 32) + (b5 << 40) + (b6 << 48) + (b7 << 56);
75 public static short HostToNetworkOrder(short host) {
79 return SwapShort (host);
82 public static int HostToNetworkOrder(int host) {
86 return SwapInt (host);
89 public static long HostToNetworkOrder(long host) {
93 return SwapLong (host);
96 public static short NetworkToHostOrder(short network) {
100 return SwapShort (network);
103 public static int NetworkToHostOrder(int network) {
107 return SwapInt (network);
110 public static long NetworkToHostOrder(long network) {
114 return SwapLong (network);
118 /// Constructor from a 32-bit constant.
120 public IPAddress (long addr)
125 public static IPAddress Parse(string ip)
128 throw new ArgumentNullException("null ip string");
133 bool prevDigit = false;
135 while (pos < ip.Length) {
136 current = ip [pos++];
137 if (Char.IsDigit (current))
140 if (current == '.') {
141 // No more than 3 dots. Doesn't allow ending with a dot.
142 if (++ndots > 3 || pos == ip.Length || prevDigit == false)
143 throw new FormatException ("the string is not a valid ip");
147 else if (!Char.IsDigit (current)) {
148 if (!Char.IsWhiteSpace (current))
149 throw new FormatException ("the string is not a valid ip");
151 // The same as MS does
153 return new IPAddress (0);
160 throw new FormatException ("the string is not a valid ip");
164 string [] ips = ip.Split (new char [] {'.'});
165 for (int i = 0; i < ips.Length; i++)
166 a = (a << 8) | (Byte.Parse(ips [i]));
168 return (new IPAddress (a));
171 public long Address {
173 return (NetworkToHostOrder (address));
176 if (value < 0 || value > 0x00000000FFFFFFFF)
177 throw new ArgumentOutOfRangeException (
178 "the address must be between 0 and 0xFFFFFFFF");
180 address = HostToNetworkOrder (value);
184 public AddressFamily AddressFamily {
186 return(AddressFamily.InterNetwork);
192 /// Used to tell whether an address is a loopback.
193 /// All IP addresses of the form 127.X.Y.Z, where X, Y, and Z are in
194 /// the range 0-255, are loopback addresses.
196 /// <param name="addr">Address to compare</param>
197 /// <returns></returns>
198 public static bool IsLoopback (IPAddress addr)
200 return (addr.address & 0xFF) == 127;
204 /// Overrides System.Object.ToString to return
205 /// this object rendered in a quad-dotted notation
207 public override string ToString ()
209 return ToString (address);
213 /// Returns this object rendered in a quad-dotted notation
215 static string ToString (long addr)
217 // addr is in network order
218 return (addr & 0xff).ToString () + "." +
219 ((addr >> 8) & 0xff).ToString () + "." +
220 ((addr >> 16) & 0xff).ToString () + "." +
221 ((addr >> 24) & 0xff).ToString ();
225 /// Whether both objects are equal.
227 public override bool Equals (object other)
229 if (other is System.Net.IPAddress){
230 return Address == ((System.Net.IPAddress) other).Address;
235 public override int GetHashCode ()