View | Details | Raw Unified | Return to bug 18574
Collapse All | Expand All

(-)a/mcs/class/System/System.Net/IPv6Address.cs (-24 / +29 lines)
Lines 89-94 namespace System.Net { Link Here
89
		static int Fill (ushort [] addr, string ipString)
89
		static int Fill (ushort [] addr, string ipString)
90
		{
90
		{
91
			int p = 0;
91
			int p = 0;
92
			int pdigits = 0;
92
			int slot = 0;
93
			int slot = 0;
93
94
94
			if (ipString.Length == 0)
95
			if (ipString.Length == 0)
Lines 103-108 namespace System.Net { Link Here
103
				int n;
104
				int n;
104
105
105
				if (c == ':'){
106
				if (c == ':'){
107
					// Leading : is not allowed.
108
					if (i == 0)
109
						return -1;
110
					
106
					// Trailing : is not allowed.
111
					// Trailing : is not allowed.
107
					if (i == ipString.Length-1)
112
					if (i == ipString.Length-1)
108
						return -1;
113
						return -1;
Lines 112-119 namespace System.Net { Link Here
112
					
117
					
113
					addr [slot++] = (ushort) p;
118
					addr [slot++] = (ushort) p;
114
					p = 0;
119
					p = 0;
120
					pdigits = 0;
115
					continue;
121
					continue;
116
				} if ('0' <= c && c <= '9')
122
				}
123
124
				pdigits++;
125
				if (pdigits > 4)
126
					return -1;
127
128
				if ('0' <= c && c <= '9')
117
					n = (int) (c - '0');
129
					n = (int) (c - '0');
118
				else if ('a' <= c && c <= 'f')
130
				else if ('a' <= c && c <= 'f')
119
					n = (int) (c - 'a' + 10);
131
					n = (int) (c - 'a' + 10);
Lines 182-188 namespace System.Net { Link Here
182
			//
194
			//
183
			// Is there an ipv4 address at the end?
195
			// Is there an ipv4 address at the end?
184
			//
196
			//
185
			bool ipv4 = false;
186
			int pos2 = ipString.LastIndexOf (':');
197
			int pos2 = ipString.LastIndexOf (':');
187
			if (pos2 == -1)
198
			if (pos2 == -1)
188
				return false;
199
				return false;
Lines 203-209 namespace System.Net { Link Here
203
						ipString = ipString.Substring (0, pos2 + 1);
214
						ipString = ipString.Substring (0, pos2 + 1);
204
					else
215
					else
205
						ipString = ipString.Substring (0, pos2);
216
						ipString = ipString.Substring (0, pos2);
206
					ipv4 = true;
207
					slots = 2;
217
					slots = 2;
208
				}
218
				}
209
			}	
219
			}	
Lines 240-263 namespace System.Net { Link Here
240
					return false;
250
					return false;
241
			}
251
			}
242
252
243
			// Now check the results in the ipv6-address range only
244
			bool ipv6 = false;
245
			for (int i = 0; i < slots; i++){
246
				if (addr [i] != 0 || i == 5 && addr [i] != 0xffff)
247
					ipv6 = true;
248
			}
249
			
250
			// check IPv4 validity
251
			if (ipv4 && !ipv6) {
252
				for (int i = 0; i < 5; i++) {
253
					if (addr [i] != 0)
254
						return false;
255
				}
256
257
				if (addr [5] != 0 && addr [5] != 0xffff)
258
					return false;
259
			}
260
261
			result = new IPv6Address (addr, prefixLen, scopeId);
253
			result = new IPv6Address (addr, prefixLen, scopeId);
262
			return true;
254
			return true;
263
		}
255
		}
Lines 312-321 namespace System.Net { Link Here
312
			return (ushort) ( ((number >> 8) & 0xFF) + ((number << 8) & 0xFF00) );
304
			return (ushort) ( ((number >> 8) & 0xFF) + ((number << 8) & 0xFF00) );
313
		}
305
		}
314
306
315
		// Convert the address into a format expected by the IPAddress (long) ctor
307
		// Convert the address into a format expected by the IPAddress (long) ctor.
316
		private int AsIPv4Int ()
308
		// This needs to be unsigned to satisfy the '> 1' test in IsIPv4Compatible()
309
		private uint AsIPv4Int ()
317
		{
310
		{
318
			return (SwapUShort (address [7]) << 16) + SwapUShort (address [6]);
311
			return (uint)(SwapUShort (address [7]) << 16) + SwapUShort (address [6]);
319
		}			
312
		}			
320
313
321
		public bool IsIPv4Compatible ()
314
		public bool IsIPv4Compatible ()
Lines 323-328 namespace System.Net { Link Here
323
			for (int i = 0; i < 6; i++) 
316
			for (int i = 0; i < 6; i++) 
324
				if (address [i] != 0)
317
				if (address [i] != 0)
325
					return false;
318
					return false;
319
			/* MS .net only seems to format the last 4
320
			 * bytes as an IPv4 address if address[6] is
321
			 * non-zero
322
			 */
323
			if (address[6] == 0)
324
				return false;
326
			return (AsIPv4Int () > 1);
325
			return (AsIPv4Int () > 1);
327
		}
326
		}
328
		
327
		
Lines 331-336 namespace System.Net { Link Here
331
			for (int i = 0; i < 5; i++) 
330
			for (int i = 0; i < 5; i++) 
332
				if (address [i] != 0)
331
				if (address [i] != 0)
333
					return false;
332
					return false;
333
			/* MS .net only seems to format the last 4
334
			 * bytes as an IPv4 address if address[6] is
335
			 * non-zero
336
			 */
337
			if (address[6] == 0)
338
				return false;
334
			return address [5] == 0xffff;
339
			return address [5] == 0xffff;
335
		}
340
		}
336
		
341
		
(-)a/mcs/class/System/Test/System.Net/IPAddressTest.cs (-1 / +94 lines)
Lines 27-33 public class IPAddressTest Link Here
27
		   "1::", "1:0:0:0:0:0:0:0",
27
		   "1::", "1:0:0:0:0:0:0:0",
28
		   "2:2::", "2:2:0:0:0:0:0:0",
28
		   "2:2::", "2:2:0:0:0:0:0:0",
29
		   "7:7:7:7:7:7:7:0", "7:7:7:7:7:7:7:0",
29
		   "7:7:7:7:7:7:7:0", "7:7:7:7:7:7:7:0",
30
//		   "::1", "0:0:0:0:0:0:0:1", FIXME: ToString not working
30
		   "::1", "0:0:0:0:0:0:0:1",
31
		   "::2", "0:0:0:0:0:0:0:2",
32
		   "::F", "0:0:0:0:0:0:0:F",
33
		   "::10", "0:0:0:0:0:0:0:10",
34
		   "::A0", "0:0:0:0:0:0:0:A0",
35
		   "::F0", "0:0:0:0:0:0:0:F0",
36
		   "::FF", "0:0:0:0:0:0:0:FF",
37
		   "::0.1.0.0", "0:0:0:0:0:0:1:0",
38
		   "::0.2.0.0", "0:0:0:0:0:0:2:0",
39
		   "::0.15.0.0", "0:0:0:0:0:0:F:0",
40
		   "::0.16.0.0", "0:0:0:0:0:0:10:0",
41
		   "::0.160.0.0", "0:0:0:0:0:0:A0:0",
42
		   "::0.240.0.0", "0:0:0:0:0:0:F0:0",
43
		   "::0.255.0.0", "0:0:0:0:0:0:FF:0",
44
		   "::1001", "0:0:0:0:0:0:0:1001",
45
		   "::1002", "0:0:0:0:0:0:0:1002",
46
		   "::100F", "0:0:0:0:0:0:0:100F",
47
		   "::1010", "0:0:0:0:0:0:0:1010",
48
		   "::10A0", "0:0:0:0:0:0:0:10A0",
49
		   "::10F0", "0:0:0:0:0:0:0:10F0",
50
		   "::10FF", "0:0:0:0:0:0:0:10FF",
51
		   "::0.1.0.1", "0:0:0:0:0:0:1:1",
52
		   "::0.2.0.2", "0:0:0:0:0:0:2:2",
53
		   "::0.15.0.15", "0:0:0:0:0:0:F:F",
54
		   "::0.16.0.16", "0:0:0:0:0:0:10:10",
55
		   "::0.160.0.160", "0:0:0:0:0:0:A0:A0",
56
		   "::0.240.0.240", "0:0:0:0:0:0:F0:F0",
57
		   "::0.255.0.255", "0:0:0:0:0:0:FF:FF",
58
		   "::FFFF:0:1", "0:0:0:0:0:FFFF:0:1",
59
		   "::FFFF:0:2", "0:0:0:0:0:FFFF:0:2",
60
		   "::FFFF:0:F", "0:0:0:0:0:FFFF:0:F",
61
		   "::FFFF:0:10", "0:0:0:0:0:FFFF:0:10",
62
		   "::FFFF:0:A0", "0:0:0:0:0:FFFF:0:A0",
63
		   "::FFFF:0:F0", "0:0:0:0:0:FFFF:0:F0",
64
		   "::FFFF:0:FF", "0:0:0:0:0:FFFF:0:FF",
65
		   "::FFFF:0.1.0.0", "0:0:0:0:0:FFFF:1:0",
66
		   "::FFFF:0.2.0.0", "0:0:0:0:0:FFFF:2:0",
67
		   "::FFFF:0.15.0.0", "0:0:0:0:0:FFFF:F:0",
68
		   "::FFFF:0.16.0.0", "0:0:0:0:0:FFFF:10:0",
69
		   "::FFFF:0.160.0.0", "0:0:0:0:0:FFFF:A0:0",
70
		   "::FFFF:0.240.0.0", "0:0:0:0:0:FFFF:F0:0",
71
		   "::FFFF:0.255.0.0", "0:0:0:0:0:FFFF:FF:0",
72
		   "::FFFF:0:1001", "0:0:0:0:0:FFFF:0:1001",
73
		   "::FFFF:0:1002", "0:0:0:0:0:FFFF:0:1002",
74
		   "::FFFF:0:100F", "0:0:0:0:0:FFFF:0:100F",
75
		   "::FFFF:0:1010", "0:0:0:0:0:FFFF:0:1010",
76
		   "::FFFF:0:10A0", "0:0:0:0:0:FFFF:0:10A0",
77
		   "::FFFF:0:10F0", "0:0:0:0:0:FFFF:0:10F0",
78
		   "::FFFF:0:10FF", "0:0:0:0:0:FFFF:0:10FF",
79
		   "::FFFF:0.1.0.1", "0:0:0:0:0:FFFF:1:1",
80
		   "::FFFF:0.2.0.2", "0:0:0:0:0:FFFF:2:2",
81
		   "::FFFF:0.15.0.15", "0:0:0:0:0:FFFF:F:F",
82
		   "::FFFF:0.16.0.16", "0:0:0:0:0:FFFF:10:10",
83
		   "::FFFF:0.160.0.160", "0:0:0:0:0:FFFF:A0:A0",
84
		   "::FFFF:0.240.0.240", "0:0:0:0:0:FFFF:F0:F0",
85
		   "::FFFF:0.255.0.255", "0:0:0:0:0:FFFF:FF:FF",
31
		   "0:7:7:7:7:7:7:7", "0:7:7:7:7:7:7:7",
86
		   "0:7:7:7:7:7:7:7", "0:7:7:7:7:7:7:7",
32
		   "E::1", "E:0:0:0:0:0:0:1",
87
		   "E::1", "E:0:0:0:0:0:0:1",
33
		   "E::2:2", "E:0:0:0:0:0:2:2",
88
		   "E::2:2", "E:0:0:0:0:0:2:2",
Lines 48-53 public class IPAddressTest Link Here
48
		   "::FFFF:192.168.0.1", "::FFFF:192.168.0.1",
103
		   "::FFFF:192.168.0.1", "::FFFF:192.168.0.1",
49
		   "::FFFF:0.168.0.1", "::FFFF:0.168.0.1",
104
		   "::FFFF:0.168.0.1", "::FFFF:0.168.0.1",
50
		   "::FFFF", "::0.0.255.255",
105
		   "::FFFF", "::0.0.255.255",
106
		   "::EEEE:A00:1", "::EEEE:10.0.0.1",
51
		   "::10.0.0.1", "::10.0.0.1",
107
		   "::10.0.0.1", "::10.0.0.1",
52
		   "1234::1234:0:0", "1234:0:0:0:0:1234:0:0",
108
		   "1234::1234:0:0", "1234:0:0:0:0:1234:0:0",
53
		   "1:0:1:0:1:0:1:0", "1:0:1:0:1:0:1:0",
109
		   "1:0:1:0:1:0:1:0", "1:0:1:0:1:0:1:0",
Lines 66-71 public class IPAddressTest Link Here
66
		   "fec0:0:0:ffff::1%1",
122
		   "fec0:0:0:ffff::1%1",
67
	};
123
	};
68
124
125
	static string[] ipv6ParseWrong = new string[] {
126
		   ":::4df",
127
		   "4df:::",
128
		   "0:::4df",
129
		   "4df:::0",
130
		   "::4df:::",
131
		   "0::4df:::",
132
		   " ::1",
133
		   ":: 1",
134
		   ":",
135
		   "0:0:0:0:0:0:0:0:0",
136
		   "0:0:0:0:0:0:0",
137
		   "0FFFF::",
138
		   "FFFF0::",
139
		   "[::1",
140
	};
141
69
	static string[] ipv4ParseOk = new string[] {
142
	static string[] ipv4ParseOk = new string[] {
70
		"192.168.1.1", "192.168.1.1",
143
		"192.168.1.1", "192.168.1.1",
71
		"0xff.0x7f.0x20.0x01", "255.127.32.1",
144
		"0xff.0x7f.0x20.0x01", "255.127.32.1",
Lines 553-558 public class IPAddressTest Link Here
553
		Assert.IsTrue (IPAddress.Parse ("2001::1").IsIPv6Teredo, "#1");
626
		Assert.IsTrue (IPAddress.Parse ("2001::1").IsIPv6Teredo, "#1");
554
		Assert.IsFalse (IPAddress.Parse ("2002::1").IsIPv6Teredo, "#2");
627
		Assert.IsFalse (IPAddress.Parse ("2002::1").IsIPv6Teredo, "#2");
555
	}
628
	}
629
630
	[Test]
631
	public void ParseWrongV6 ()
632
	{
633
		if (!Socket.SupportsIPv6)
634
			Assert.Ignore ("IPv6 must be enabled in machine.config");
635
636
		for (int i = 0; i < ipv6ParseWrong.Length; i++) {
637
			string ipAddress = ipv6ParseWrong [i];
638
639
			try {
640
				IPAddress ip = IPAddress.Parse (ipAddress);
641
				Assert.Fail ("#1:" + i + " (" + ipAddress + ")");
642
			} catch (FormatException ex) {
643
				Assert.AreEqual (typeof (FormatException), ex.GetType (), "#2:" + i);
644
				Assert.IsNull (ex.InnerException, "#3:" + i);
645
				Assert.IsNotNull (ex.Message, "#4:" + i);
646
			}
647
		}
648
	}
556
#endif
649
#endif
557
}
650
}
558
}
651
}

Return to bug 18574