|
|
@@ -10,27 +10,27 @@
|
|
|
// out that in ms.net there's an internal class called
|
|
|
// IPv6Address in namespace System.
|
|
|
//
|
|
|
-
|
|
|
-//
|
|
|
-// Permission is hereby granted, free of charge, to any person obtaining
|
|
|
-// a copy of this software and associated documentation files (the
|
|
|
-// "Software"), to deal in the Software without restriction, including
|
|
|
-// without limitation the rights to use, copy, modify, merge, publish,
|
|
|
-// distribute, sublicense, and/or sell copies of the Software, and to
|
|
|
-// permit persons to whom the Software is furnished to do so, subject to
|
|
|
-// the following conditions:
|
|
|
-//
|
|
|
-// The above copyright notice and this permission notice shall be
|
|
|
-// included in all copies or substantial portions of the Software.
|
|
|
-//
|
|
|
-// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
|
-// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
|
-// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
|
-// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
|
|
-// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
|
|
-// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
|
|
-// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
|
-//
|
|
|
+
|
|
|
+//
|
|
|
+// Permission is hereby granted, free of charge, to any person obtaining
|
|
|
+// a copy of this software and associated documentation files (the
|
|
|
+// "Software"), to deal in the Software without restriction, including
|
|
|
+// without limitation the rights to use, copy, modify, merge, publish,
|
|
|
+// distribute, sublicense, and/or sell copies of the Software, and to
|
|
|
+// permit persons to whom the Software is furnished to do so, subject to
|
|
|
+// the following conditions:
|
|
|
+//
|
|
|
+// The above copyright notice and this permission notice shall be
|
|
|
+// included in all copies or substantial portions of the Software.
|
|
|
+//
|
|
|
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
|
+// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
|
+// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
|
+// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
|
|
+// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
|
|
+// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
|
|
+// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
|
+//
|
|
|
|
|
|
|
|
|
using System;
|
|
|
@@ -159,7 +159,7 @@ namespace System.Net {
|
|
|
if (len > (ipv4 ? 6 : 8))
|
|
|
throw new FormatException ("Not a valid IPv6 address");
|
|
|
int piecedouble = -1;
|
|
|
- bool ipv6 = false;
|
|
|
+ bool ipv6 = false;
|
|
|
for (int i = 0; i < len; i++) {
|
|
|
string piece = pieces [i];
|
|
|
if (piece == "!")
|
|
|
@@ -175,9 +175,9 @@ namespace System.Net {
|
|
|
} catch (ArgumentException) {
|
|
|
throw new FormatException ("Not a valid IPv6 address");
|
|
|
}
|
|
|
- addr [i] = (ushort) p;
|
|
|
- if (p != 0 || (i == 5 && p != 0xffff))
|
|
|
- ipv6 = true;
|
|
|
+ addr [i] = (ushort) p;
|
|
|
+ if (p != 0 || (i == 5 && p != 0xffff))
|
|
|
+ ipv6 = true;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@@ -194,20 +194,20 @@ namespace System.Net {
|
|
|
} else if (len != (ipv4 ? 6 : 8))
|
|
|
throw new FormatException ("Not a valid IPv6 address");
|
|
|
|
|
|
- // check IPv4 validity
|
|
|
- if (ipv4 && !ipv6) {
|
|
|
- for (int i = 0; i < 5; i++) {
|
|
|
- if (addr [i] != 0)
|
|
|
- throw new FormatException ("Not a valid IPv6 address");
|
|
|
- }
|
|
|
-
|
|
|
- if (addr [5] != 0 && addr [5] != 0xffff)
|
|
|
- throw new FormatException ("Not a valid IPv6 address");
|
|
|
- }
|
|
|
-
|
|
|
- return new IPv6Address (addr, prefixLen, scopeId);
|
|
|
- }
|
|
|
-
|
|
|
+ // check IPv4 validity
|
|
|
+ if (ipv4 && !ipv6) {
|
|
|
+ for (int i = 0; i < 5; i++) {
|
|
|
+ if (addr [i] != 0)
|
|
|
+ throw new FormatException ("Not a valid IPv6 address");
|
|
|
+ }
|
|
|
+
|
|
|
+ if (addr [5] != 0 && addr [5] != 0xffff)
|
|
|
+ throw new FormatException ("Not a valid IPv6 address");
|
|
|
+ }
|
|
|
+
|
|
|
+ return new IPv6Address (addr, prefixLen, scopeId);
|
|
|
+ }
|
|
|
+
|
|
|
public ushort [] Address {
|
|
|
get { return address; }
|
|
|
}
|
|
|
@@ -232,44 +232,44 @@ namespace System.Net {
|
|
|
public AddressFamily AddressFamily {
|
|
|
get { return AddressFamily.InterNetworkV6; }
|
|
|
}
|
|
|
-
|
|
|
- public static bool IsLoopback (IPv6Address addr)
|
|
|
- {
|
|
|
- if (addr.address [7] != 1)
|
|
|
- return false;
|
|
|
-
|
|
|
- int x = addr.address [6] >> 8;
|
|
|
- if (x != 0x7f && x != 0)
|
|
|
- return false;
|
|
|
-
|
|
|
- for (int i = 0; i < 4; i++) {
|
|
|
- if (addr.address [i] != 0)
|
|
|
- return false;
|
|
|
- }
|
|
|
-
|
|
|
- if (addr.address [5] != 0 && addr.address [5] != 0xffff)
|
|
|
- return false;
|
|
|
-
|
|
|
- return true;
|
|
|
- }
|
|
|
-
|
|
|
- private static ushort SwapUShort (ushort number)
|
|
|
- {
|
|
|
- return (ushort) ( ((number >> 8) & 0xFF) + ((number << 8) & 0xFF00) );
|
|
|
- }
|
|
|
-
|
|
|
- // Convert the address into a format expected by the IPAddress (long) ctor
|
|
|
- private int AsIPv4Int ()
|
|
|
- {
|
|
|
- return (SwapUShort (address [7]) << 16) + SwapUShort (address [6]);
|
|
|
- }
|
|
|
-
|
|
|
+
|
|
|
+ public static bool IsLoopback (IPv6Address addr)
|
|
|
+ {
|
|
|
+ if (addr.address [7] != 1)
|
|
|
+ return false;
|
|
|
+
|
|
|
+ int x = addr.address [6] >> 8;
|
|
|
+ if (x != 0x7f && x != 0)
|
|
|
+ return false;
|
|
|
+
|
|
|
+ for (int i = 0; i < 4; i++) {
|
|
|
+ if (addr.address [i] != 0)
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ if (addr.address [5] != 0 && addr.address [5] != 0xffff)
|
|
|
+ return false;
|
|
|
+
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+
|
|
|
+ private static ushort SwapUShort (ushort number)
|
|
|
+ {
|
|
|
+ return (ushort) ( ((number >> 8) & 0xFF) + ((number << 8) & 0xFF00) );
|
|
|
+ }
|
|
|
+
|
|
|
+ // Convert the address into a format expected by the IPAddress (long) ctor
|
|
|
+ private int AsIPv4Int ()
|
|
|
+ {
|
|
|
+ return (SwapUShort (address [7]) << 16) + SwapUShort (address [6]);
|
|
|
+ }
|
|
|
+
|
|
|
public bool IsIPv4Compatible ()
|
|
|
{
|
|
|
for (int i = 0; i < 6; i++)
|
|
|
if (address [i] != 0)
|
|
|
return false;
|
|
|
- return (AsIPv4Int () > 1);
|
|
|
+ return (AsIPv4Int () > 1);
|
|
|
}
|
|
|
|
|
|
public bool IsIPv4Mapped ()
|
|
|
@@ -296,7 +296,7 @@ namespace System.Net {
|
|
|
if(IsIPv4Mapped())
|
|
|
s.Append("ffff:");
|
|
|
|
|
|
- s.Append(new IPAddress( AsIPv4Int ()).ToString ());
|
|
|
+ s.Append(new IPAddress( AsIPv4Int ()).ToString ());
|
|
|
|
|
|
return s.ToString ();
|
|
|
}
|
|
|
@@ -346,18 +346,18 @@ namespace System.Net {
|
|
|
}
|
|
|
return s.ToString ();
|
|
|
}
|
|
|
-
|
|
|
- public string ToString (bool fullLength)
|
|
|
- {
|
|
|
- if (!fullLength)
|
|
|
- return ToString ();
|
|
|
-
|
|
|
- StringBuilder sb = new StringBuilder ();
|
|
|
- for (int i=0; i < address.Length - 1; i++) {
|
|
|
+
|
|
|
+ public string ToString (bool fullLength)
|
|
|
+ {
|
|
|
+ if (!fullLength)
|
|
|
+ return ToString ();
|
|
|
+
|
|
|
+ StringBuilder sb = new StringBuilder ();
|
|
|
+ for (int i=0; i < address.Length - 1; i++) {
|
|
|
sb.AppendFormat ("{0:X4}:", address [i]);
|
|
|
- }
|
|
|
+ }
|
|
|
sb.AppendFormat ("{0:X4}", address [address.Length - 1]);
|
|
|
- return sb.ToString ();
|
|
|
+ return sb.ToString ();
|
|
|
}
|
|
|
|
|
|
/// <returns>
|