2 // System.Net.IPAddress.cs
5 // Miguel de Icaza (miguel@ximian.com)
6 // Lawrence Pit (loz@cable.a2000.nl)
8 // (C) Ximian, Inc. http://www.ximian.com
10 // Permission is hereby granted, free of charge, to any person obtaining
11 // a copy of this software and associated documentation files (the
12 // "Software"), to deal in the Software without restriction, including
13 // without limitation the rights to use, copy, modify, merge, publish,
14 // distribute, sublicense, and/or sell copies of the Software, and to
15 // permit persons to whom the Software is furnished to do so, subject to
16 // the following conditions:
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
21 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
24 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
25 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
26 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
27 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31 using System.Globalization;
32 using System.Net.Sockets;
33 using System.Runtime.InteropServices;
35 namespace System.Net {
38 /// Encapsulates an IP Address.
41 public class IPAddress {
42 // Don't change the name of this field without also
43 // changing socket-io.c in the runtime
44 // The IP address is stored in little-endian order inside the int,
45 // meaning the lower order bytes contain the netid
46 private long m_Address;
47 private AddressFamily m_Family = AddressFamily.InterNetwork;
48 private ushort[] m_Numbers = new ushort[8]; /// ip6 Stored in network order (as ip4)
49 private long m_ScopeId = 0;
50 private int m_HashCode; // Added for serialization compatibility with MS.NET
52 public static readonly IPAddress Any = new IPAddress(0);
53 public static readonly IPAddress Broadcast = IPAddress.Parse ("255.255.255.255");
54 public static readonly IPAddress Loopback = IPAddress.Parse ("127.0.0.1");
55 public static readonly IPAddress None = IPAddress.Parse ("255.255.255.255");
58 public static readonly IPAddress IPv6Any = IPAddress.ParseIPV6 ("::");
59 public static readonly IPAddress IPv6Loopback = IPAddress.ParseIPV6 ("::1");
60 public static readonly IPAddress IPv6None = IPAddress.ParseIPV6 ("::");
63 private static short SwapShort (short number)
65 return (short) ( ((number >> 8) & 0xFF) + ((number << 8) & 0xFF00) );
68 private static int SwapInt (int number)
70 byte b0 = (byte) ((number >> 24) & 0xFF);
71 byte b1 = (byte) ((number >> 16) & 0xFF);
72 byte b2 = (byte) ((number >> 8) & 0xFF);
73 byte b3 = (byte) (number & 0xFF);
74 return b0 + (b1 << 8) + (b2 << 16) + (b3 << 24);
77 private static long SwapLong (long number)
79 byte b0 = (byte) ((number >> 56) & 0xFF);
80 byte b1 = (byte) ((number >> 48) & 0xFF);
81 byte b2 = (byte) ((number >> 40) & 0xFF);
82 byte b3 = (byte) ((number >> 32) & 0xFF);
83 byte b4 = (byte) ((number >> 24) & 0xFF);
84 byte b5 = (byte) ((number >> 16) & 0xFF);
85 byte b6 = (byte) ((number >> 8) & 0xFF);
86 byte b7 = (byte) (number & 0xFF);
87 return (long) b0 + ((long) b1 << 8) + ((long) b2 << 16) + ((long) b3 << 24) + ((long) b4 << 32) + ((long) b5 << 40) + ((long) b6 << 48) + ((long) b7 << 56);
90 public static short HostToNetworkOrder(short host) {
91 if (!BitConverter.IsLittleEndian)
94 return SwapShort (host);
97 public static int HostToNetworkOrder(int host) {
98 if (!BitConverter.IsLittleEndian)
101 return SwapInt (host);
104 public static long HostToNetworkOrder(long host) {
105 if (!BitConverter.IsLittleEndian)
108 return SwapLong (host);
111 public static short NetworkToHostOrder(short network) {
112 if (!BitConverter.IsLittleEndian)
115 return SwapShort (network);
118 public static int NetworkToHostOrder(int network) {
119 if (!BitConverter.IsLittleEndian)
122 return SwapInt (network);
125 public static long NetworkToHostOrder(long network) {
126 if (!BitConverter.IsLittleEndian)
129 return SwapLong (network);
133 /// Constructor from a 32-bit constant with the address bytes in
134 /// little-endian order (the lower order bytes contain the netid)
136 public IPAddress (long addr)
142 public IPAddress(byte[] address) : this(address, 0)
146 public IPAddress(byte[] address, long scopeId)
148 if(address.Length != 16)
149 throw new ArgumentException("address");
151 Buffer.BlockCopy(address, 0, m_Numbers, 0, 16);
152 m_Family = AddressFamily.InterNetworkV6;
156 internal IPAddress(ushort[] address, long scopeId)
160 for(int i=0; i<8; i++)
161 m_Numbers[i] = (ushort)HostToNetworkOrder((short)m_Numbers[i]);
163 m_Family = AddressFamily.InterNetworkV6;
168 public static IPAddress Parse (string ip)
173 throw new ArgumentNullException ("Value cannot be null.");
176 if( (ret = ParseIPV4(ip)) == null)
177 if( (ret = ParseIPV6(ip)) == null)
178 throw new FormatException("An invalid IP address was specified.");
180 if( (ret = ParseIPV4(ip)) == null)
181 throw new FormatException("An invalid IP address was specified.");
186 private static IPAddress ParseIPV4 (string ip)
188 if (ip.Length == 0 || ip == " ")
189 return new IPAddress (0);
191 int pos = ip.IndexOf (' ');
193 ip = ip.Substring (0, pos);
195 if (ip.Length == 0 || ip [ip.Length - 1] == '.')
198 string [] ips = ip.Split (new char [] {'.'});
202 // Make the number in network order
207 for (int i = 0; i < ips.Length; i++) {
208 string subnet = ips [i];
209 if ((3 <= subnet.Length && subnet.Length <= 4) &&
210 (subnet [0] == '0') &&
211 (subnet [1] == 'x' || subnet [2] == 'X')) {
212 if (subnet.Length == 3)
213 val = (byte) Uri.FromHex (subnet [2]);
215 val = (byte) ((Uri.FromHex (subnet [2]) << 4) | Uri.FromHex (subnet [3]));
216 } else if (subnet.Length == 0)
219 val = Byte.Parse (subnet, NumberStyles.None);
221 if (ips.Length < 4 && i == (ips.Length - 1))
224 a |= (long) val << (i << 3);
227 return (new IPAddress (a));
228 } catch (Exception) {
234 private static IPAddress ParseIPV6 (string ip)
238 IPv6Address newIPv6Address = IPv6Address.Parse(ip);
239 return new IPAddress(newIPv6Address.Address, newIPv6Address.ScopeId);
246 [Obsolete("This property is obsolete. Use GetAddressBytes.")]
251 if(m_Family != AddressFamily.InterNetwork)
252 throw new Exception("The attempted operation is not supported for the type of object referenced");
257 /* no need to do this test, ms.net accepts any value.
258 if (value < 0 || value > 0x00000000FFFFFFFF)
259 throw new ArgumentOutOfRangeException (
260 "the address must be between 0 and 0xFFFFFFFF");
263 if(m_Family != AddressFamily.InterNetwork)
264 throw new Exception("The attempted operation is not supported for the type of object referenced");
270 internal long InternalIPv4Address {
271 get { return m_Address; }
275 public long ScopeId {
277 if(m_Family != AddressFamily.InterNetworkV6)
278 throw new Exception("The attempted operation is not supported for the type of object referenced");
283 if(m_Family != AddressFamily.InterNetworkV6)
284 throw new Exception("The attempted operation is not supported for the type of object referenced");
290 public byte [] GetAddressBytes ()
292 if(m_Family == AddressFamily.InterNetworkV6) {
293 byte [] addressBytes = new byte [16];
294 Buffer.BlockCopy (m_Numbers, 0, addressBytes, 0, 16);
297 return new byte [4] { (byte)(m_Address & 0xFF),
298 (byte)((m_Address >> 8) & 0xFF),
299 (byte)((m_Address >> 16) & 0xFF),
300 (byte)(m_Address >> 24) };
304 public AddressFamily AddressFamily
313 /// Used to tell whether an address is a loopback.
314 /// All IP addresses of the form 127.X.Y.Z, where X, Y, and Z are in
315 /// the range 0-255, are loopback addresses.
317 /// <param name="addr">Address to compare</param>
318 /// <returns></returns>
319 public static bool IsLoopback (IPAddress addr)
321 if(addr.m_Family == AddressFamily.InterNetwork)
322 return (addr.m_Address & 0xFF) == 127;
324 for(int i=0; i<6; i++) {
325 if(addr.m_Numbers[i] != 0)
329 return NetworkToHostOrder((short)addr.m_Numbers[7]) == 1;
334 /// Overrides System.Object.ToString to return
335 /// this object rendered in a quad-dotted notation
337 public override string ToString ()
339 if(m_Family == AddressFamily.InterNetwork)
340 return ToString (m_Address);
343 ushort[] numbers = m_Numbers.Clone() as ushort[];
345 for(int i=0; i<numbers.Length; i++)
346 numbers[i] = (ushort)NetworkToHostOrder((short)numbers[i]);
348 return new IPv6Address(numbers).ToString();
353 /// Returns this object rendered in a quad-dotted notation
355 static string ToString (long addr)
357 // addr is in network order
358 return (addr & 0xff).ToString () + "." +
359 ((addr >> 8) & 0xff).ToString () + "." +
360 ((addr >> 16) & 0xff).ToString () + "." +
361 ((addr >> 24) & 0xff).ToString ();
365 /// Whether both objects are equal.
367 public override bool Equals (object other)
369 if (other is System.Net.IPAddress){
370 IPAddress otherAddr = other as IPAddress;
372 if(AddressFamily != otherAddr.AddressFamily)
375 if(AddressFamily == AddressFamily.InterNetwork) {
376 return m_Address == otherAddr.m_Address;
378 ushort[] vals = otherAddr.m_Numbers;
380 for(int i=0; i<8; i++)
381 if(m_Numbers[i] != vals[i])
390 public override int GetHashCode ()
392 if(m_Family == AddressFamily.InterNetwork)
393 return (int)m_Address;
395 return Hash (((((int) m_Numbers[0]) << 16) + m_Numbers [1]),
396 ((((int) m_Numbers [2]) << 16) + m_Numbers [3]),
397 ((((int) m_Numbers [4]) << 16) + m_Numbers [5]),
398 ((((int) m_Numbers [6]) << 16) + m_Numbers [7]));
401 private static int Hash (int i, int j, int k, int l)
403 return i ^ (j << 13 | j >> 19) ^ (k << 26 | k >> 6) ^ (l << 7 | l >> 25);