// out that in ms.net there's an internal class called\r
// IPv6Address in namespace System.\r
//\r
+
+//
+// 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.
+//
\r
\r
using System;\r
internal class IPv6Address {\r
private ushort [] address;\r
private int prefixLength;\r
+ private long scopeId = 0;\r
\r
public static readonly IPv6Address Loopback = IPv6Address.Parse ("::1");\r
public static readonly IPv6Address Unspecified = IPv6Address.Parse ("::");\r
if (prefixLength < 0 || prefixLength > 128)\r
throw new ArgumentException ("prefixLength");\r
this.prefixLength = prefixLength;\r
+ } \r
+ \r
+ public IPv6Address (ushort [] addr, int prefixLength, int scopeId) : this (addr, prefixLength)\r
+ {\r
+ this.scopeId = scopeId;\r
} \r
\r
public static IPv6Address Parse (string ipString)\r
throw new FormatException ("Not a valid IPv6 address");\r
\r
int prefixLen = 0;\r
+ int scopeId = 0;\r
int pos = ipString.LastIndexOf ('/');\r
if (pos != -1) {\r
string prefix = ipString.Substring (pos + 1);\r
prefixLen = -1;\r
}\r
if (prefixLen < 0 || prefixLen > 128)\r
- throw new FormatException ("Not a valid prefix length");;\r
+ throw new FormatException ("Not a valid prefix length");\r
ipString = ipString.Substring (0, pos);\r
+ } else {\r
+ pos = ipString.LastIndexOf ('%');\r
+ if (pos != -1) {\r
+ string prefix = ipString.Substring (pos + 1);\r
+ try {\r
+ scopeId = Int32.Parse (prefix);\r
+ } \r
+ catch (Exception) {\r
+ scopeId = 0;\r
+ }\r
+ ipString = ipString.Substring (0, pos);\r
+ } \r
}\r
\r
ushort [] addr = new ushort [8]; \r
string ipv4Str = ipString.Substring (pos2 + 1);\r
if (ipv4Str.IndexOf ('.') != -1) {\r
try {\r
- long a = IPAddress.Parse (ipv4Str).Address;\r
+ long a = IPAddress.Parse (ipv4Str).InternalIPv4Address;\r
addr [6] = (ushort) (((int) (a & 0xff) << 8) + ((int) ((a >> 8) & 0xff)));\r
addr [7] = (ushort) (((int) ((a >> 16) & 0xff) << 8) + ((int) ((a >> 24) & 0xff)));\r
if (ipString [pos2 - 1] == ':') \r
throw new FormatException ("Not a valid IPv6 address");\r
}\r
\r
- return new IPv6Address (addr, prefixLen);\r
+ return new IPv6Address (addr, prefixLen, scopeId);\r
}\r
\r
public ushort [] Address {\r
get { return this.prefixLength; }\r
}\r
\r
+ public long ScopeId {\r
+ get {\r
+ return scopeId;\r
+ }\r
+ set {\r
+ scopeId = value;\r
+ }\r
+ }\r
+\r
public ushort this [int index] {\r
get { return address [index]; }\r
} \r
{\r
for (int i = 0; i < 6; i++) \r
if (address [i] != 0)\r
- return false; \r
- return true;\r
+ return false;\r
+ return ( (IPAddress.NetworkToHostOrder(address[7]) << 16) | IPAddress.NetworkToHostOrder(address[6])) > 1;\r
}\r
\r
public bool IsIPv4Mapped ()\r
public override string ToString ()\r
{\r
StringBuilder s = new StringBuilder ();\r
- for (int i = 0; i < 7; i++)\r
- s.Append (String.Format ("{0:X4}", address [i])).Append (':');\r
- s.Append (String.Format ("{0:X4}", address [7]));\r
+\r
+\r
+ if(IsIPv4Compatible() || IsIPv4Mapped())\r
+ {\r
+ s.Append("::");\r
+\r
+ if(IsIPv4Mapped())\r
+ s.Append("ffff:");\r
+\r
+ s.Append(new IPAddress( IPAddress.NetworkToHostOrder(address[6]<<16) + IPAddress.NetworkToHostOrder(address[7])).ToString());\r
+\r
+ return s.ToString ();\r
+ }\r
+ else\r
+ {\r
+ int bestChStart = -1; // Best chain start\r
+ int bestChLen = 0; // Best chain length\r
+ int currChLen = 0; // Current chain length\r
+\r
+ // Looks for the longest zero chain\r
+ for (int i=0; i<8; i++)\r
+ {\r
+ if (address[i] != 0)\r
+ {\r
+ if ((currChLen > bestChLen) \r
+ && (currChLen > 1))\r
+ {\r
+ bestChLen = currChLen;\r
+ bestChStart = i - currChLen;\r
+ }\r
+ currChLen = 0;\r
+ }\r
+ else\r
+ currChLen++;\r
+ }\r
+ if ((currChLen > bestChLen) \r
+ && (currChLen > 1))\r
+ {\r
+ bestChLen = currChLen;\r
+ bestChStart = 8 - currChLen;\r
+ }\r
+\r
+ // makes the string\r
+ if (bestChStart == 0)\r
+ s.Append(":");\r
+ for (int i=0; i<8; i++)\r
+ {\r
+ if (i == bestChStart)\r
+ {\r
+ s.Append (":");\r
+ i += (bestChLen - 1);\r
+ continue;\r
+ }\r
+ s.AppendFormat("{0:x}", address [i]);\r
+ if (i < 7) s.Append (':');\r
+ }\r
+ }\r
return s.ToString ();\r
}\r
\r
if (address [5] != 0 && address [5] != 0xffff)\r
return false;\r
\r
- long a = ipv4.Address;\r
+ long a = ipv4.InternalIPv4Address;\r
if (address [6] != (ushort) (((int) (a & 0xff) << 8) + ((int) ((a >> 8) & 0xff))) ||\r
address [7] != (ushort) (((int) ((a >> 16) & 0xff) << 8) + ((int) ((a >> 24) & 0xff))))\r
return false;\r
public override int GetHashCode ()\r
{\r
return Hash (((((int) address [0]) << 16) + address [1]), \r
- ((((int) address [2]) << 16) + address [3]),\r
- ((((int) address [4]) << 16) + address [5]),\r
- ((((int) address [6]) << 16) + address [7]));\r
+ ((((int) address [2]) << 16) + address [3]),\r
+ ((((int) address [4]) << 16) + address [5]),\r
+ ((((int) address [6]) << 16) + address [7]));\r
}\r
\r
- private static int Hash (int i, int j, int k, int l) \r
- {\r
- return i ^ (j << 13 | j >> 19) ^ (k << 26 | k >> 6) ^ (l << 7 | l >> 25);\r
- }\r
+ private static int Hash (int i, int j, int k, int l) \r
+ {\r
+ return i ^ (j << 13 | j >> 19) ^ (k << 26 | k >> 6) ^ (l << 7 | l >> 25);\r
+ }\r
}\r
}\r