2 // System.Net.IPAddress.cs
5 // Miguel de Icaza (miguel@ximian.com)
7 // (C) Ximian, Inc. http://www.ximian.com
10 // Note: the address is stored in host order
12 using System.Net.Sockets;
13 using System.Runtime.InteropServices;
17 namespace System.Net {
20 /// Encapsulates an IP Address.
23 public class IPAddress {
24 // Don't change the name of this field without also
25 // changing socket-io.c in the runtime
28 public static readonly IPAddress Any = new IPAddress(0);
29 public static readonly IPAddress Broadcast = IPAddress.Parse ("255.255.255.255");
30 public static readonly IPAddress Loopback = IPAddress.Parse ("127.0.0.1");
31 public static readonly IPAddress None = IPAddress.Parse ("255.255.255.255");
33 private static short SwapShort (short number)
35 return (short) ( ((number >> 8) & 0xFF) + ((number << 8) & 0xFF00) );
38 private static int SwapInt (int number)
40 byte b0 = (byte) ((number >> 24) & 0xFF);
41 byte b1 = (byte) ((number >> 16) & 0xFF);
42 byte b2 = (byte) ((number >> 8) & 0xFF);
43 byte b3 = (byte) (number & 0xFF);
44 return b0 + (b1 << 8) + (b2 << 16) + (b3 << 24);
47 private static long SwapLong (long number)
49 byte b0 = (byte) ((number >> 56) & 0xFF);
50 byte b1 = (byte) ((number >> 48) & 0xFF);
51 byte b2 = (byte) ((number >> 40) & 0xFF);
52 byte b3 = (byte) ((number >> 32) & 0xFF);
53 byte b4 = (byte) ((number >> 24) & 0xFF);
54 byte b5 = (byte) ((number >> 16) & 0xFF);
55 byte b6 = (byte) ((number >> 8) & 0xFF);
56 byte b7 = (byte) (number & 0xFF);
57 return b0 + (b1 << 8) + (b2 << 16) + (b3 << 24) + (b4 << 32) + (b5 << 40) + (b6 << 48) + (b7 << 56);
60 public static short HostToNetworkOrder(short host) {
61 if (!BitConverter.IsLittleEndian)
64 return SwapShort (host);
67 public static int HostToNetworkOrder(int host) {
68 if (!BitConverter.IsLittleEndian)
71 return SwapInt (host);
74 public static long HostToNetworkOrder(long host) {
75 if (!BitConverter.IsLittleEndian)
78 return SwapLong (host);
81 public static short NetworkToHostOrder(short network) {
82 if (!BitConverter.IsLittleEndian)
85 return SwapShort (network);
88 public static int NetworkToHostOrder(int network) {
89 if (!BitConverter.IsLittleEndian)
92 return SwapInt (network);
95 public static long NetworkToHostOrder(long network) {
96 if (!BitConverter.IsLittleEndian)
99 return SwapLong (network);
103 /// Constructor from a 32-bit constant with its bytes
104 /// in network order.
106 public IPAddress (long addr)
111 public static IPAddress Parse(string ip)
114 throw new ArgumentNullException("null ip string");
119 bool prevDigit = false;
121 while (pos < ip.Length) {
122 current = ip [pos++];
123 if (Char.IsDigit (current))
126 if (current == '.') {
127 // No more than 3 dots. Doesn't allow ending with a dot.
128 if (++ndots > 3 || pos == ip.Length || prevDigit == false)
129 throw new FormatException ("the string is not a valid ip");
133 else if (!Char.IsDigit (current)) {
134 if (!Char.IsWhiteSpace (current))
135 throw new FormatException ("the string is not a valid ip");
137 // The same as MS does
139 return new IPAddress (0);
146 throw new FormatException ("the string is not a valid ip");
150 string [] ips = ip.Split (new char [] {'.'});
151 // Make the number in network order
152 for (int i = ips.Length - 1; i >= 0; i--)
153 a = (a << 8) | (Byte.Parse(ips [i]));
155 return (new IPAddress (a));
158 public long Address {
163 // FIXME: Temporarily disabled as a workaround for bug #23547
164 /*if (value < 0 || value > 0x00000000FFFFFFFF)
165 throw new ArgumentOutOfRangeException (
166 "the address must be between 0 and 0xFFFFFFFF");*/
172 public AddressFamily AddressFamily {
174 return(AddressFamily.InterNetwork);
180 /// Used to tell whether an address is a loopback.
181 /// All IP addresses of the form 127.X.Y.Z, where X, Y, and Z are in
182 /// the range 0-255, are loopback addresses.
184 /// <param name="addr">Address to compare</param>
185 /// <returns></returns>
186 public static bool IsLoopback (IPAddress addr)
188 return (NetworkToHostOrder (addr.address) & 0xFF) == 127;
192 /// Overrides System.Object.ToString to return
193 /// this object rendered in a quad-dotted notation
195 public override string ToString ()
197 return ToString (address);
201 /// Returns this object rendered in a quad-dotted notation
203 static string ToString (long addr)
205 // addr is in network order
206 return (addr & 0xff).ToString () + "." +
207 ((addr >> 8) & 0xff).ToString () + "." +
208 ((addr >> 16) & 0xff).ToString () + "." +
209 ((addr >> 24) & 0xff).ToString ();
213 /// Whether both objects are equal.
215 public override bool Equals (object other)
217 if (other is System.Net.IPAddress){
218 return Address == ((System.Net.IPAddress) other).Address;
223 public override int GetHashCode ()