2 // System.Net.IPv6AddressFormatter.cs
\r
5 // Lawrence Pit (loz@cable.a2000.nl)
\r
7 // Permission is hereby granted, free of charge, to any person obtaining
\r
8 // a copy of this software and associated documentation files (the
\r
9 // "Software"), to deal in the Software without restriction, including
\r
10 // without limitation the rights to use, copy, modify, merge, publish,
\r
11 // distribute, sublicense, and/or sell copies of the Software, and to
\r
12 // permit persons to whom the Software is furnished to do so, subject to
\r
13 // the following conditions:
\r
15 // The above copyright notice and this permission notice shall be
\r
16 // included in all copies or substantial portions of the Software.
\r
18 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
\r
19 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
\r
20 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
\r
21 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
\r
22 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
\r
23 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
\r
24 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
\r
27 using System.Globalization;
\r
30 namespace System.Net {
\r
32 struct IPv6AddressFormatter
\r
37 public IPv6AddressFormatter (ushort[] addr, long scopeId)
\r
39 this.address = addr;
\r
40 this.scopeId = scopeId;
\r
43 static ushort SwapUShort (ushort number)
\r
45 return (ushort) ( ((number >> 8) & 0xFF) + ((number << 8) & 0xFF00) );
\r
48 // Convert the address into a format expected by the IPAddress (long) ctor
\r
49 // This needs to be unsigned to satisfy the '> 1' test in IsIPv4Compatible()
\r
52 return (uint)(SwapUShort (address [7]) << 16) + SwapUShort (address [6]);
\r
55 bool IsIPv4Compatible ()
\r
57 for (int i = 0; i < 6; i++)
\r
58 if (address [i] != 0)
\r
60 /* MS .net only seems to format the last 4
\r
61 * bytes as an IPv4 address if address[6] is
\r
64 if (address[6] == 0)
\r
66 return (AsIPv4Int () > 1);
\r
69 bool IsIPv4Mapped ()
\r
71 for (int i = 0; i < 5; i++)
\r
72 if (address [i] != 0)
\r
74 /* MS .net only seems to format the last 4
\r
75 * bytes as an IPv4 address if address[6] is
\r
78 if (address[6] == 0)
\r
81 return address [5] == 0xffff;
\r
84 public override string ToString ()
\r
86 StringBuilder s = new StringBuilder ();
\r
89 if(IsIPv4Compatible() || IsIPv4Mapped())
\r
96 s.Append(new IPAddress( AsIPv4Int ()).ToString ());
\r
98 return s.ToString ();
\r
101 int bestChStart = -1; // Best chain start
\r
102 int bestChLen = 0; // Best chain length
\r
103 int currChLen = 0; // Current chain length
\r
105 // Looks for the longest zero chain
\r
106 for (int i=0; i<8; i++)
\r
108 if (address[i] != 0)
\r
110 if ((currChLen > bestChLen)
\r
111 && (currChLen > 1))
\r
113 bestChLen = currChLen;
\r
114 bestChStart = i - currChLen;
\r
121 if ((currChLen > bestChLen)
\r
122 && (currChLen > 1))
\r
124 bestChLen = currChLen;
\r
125 bestChStart = 8 - currChLen;
\r
128 // makes the string
\r
129 if (bestChStart == 0)
\r
131 for (int i=0; i<8; i++)
\r
133 if (i == bestChStart)
\r
136 i += (bestChLen - 1);
\r
139 s.AppendFormat("{0:x}", address [i]);
\r
140 if (i < 7) s.Append (':');
\r
144 s.Append ('%').Append (scopeId);
\r
145 return s.ToString ();
\r