Welcome to mirror list, hosted at ThFree Co, Russian Federation.

java.net.cs « openjdk « runtime - github.com/mono/ikvm-fork.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 4f3ade93d77ef0b0309680b87ed0324f2e47a0fe (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
/*
  Copyright (C) 2007-2015 Jeroen Frijters

  This software is provided 'as-is', without any express or implied
  warranty.  In no event will the authors be held liable for any damages
  arising from the use of this software.

  Permission is granted to anyone to use this software for any purpose,
  including commercial applications, and to alter it and redistribute it
  freely, subject to the following restrictions:

  1. The origin of this software must not be misrepresented; you must not
     claim that you wrote the original software. If you use this software
     in a product, an acknowledgment in the product documentation would be
     appreciated but is not required.
  2. Altered source versions must be plainly marked as such, and must not be
     misrepresented as being the original software.
  3. This notice may not be removed or altered from any source distribution.

  Jeroen Frijters
  jeroen@frijters.net
  
*/
using System;
using System.Collections.Generic;
using System.Net;
using System.Net.NetworkInformation;
using System.Net.Sockets;
using System.Security;

static class Java_java_net_AbstractPlainDatagramSocketImpl
{
	public static void init()
	{
	}

	public static int dataAvailable(object _this)
	{
#if FIRST_PASS
		return 0;
#else
		try
		{
			java.net.AbstractPlainDatagramSocketImpl obj = (java.net.AbstractPlainDatagramSocketImpl)_this;
			if (obj.fd != null)
			{
				return obj.fd.getSocket().Available;
			}
		}
		catch (ObjectDisposedException)
		{
		}
		catch (SocketException)
		{
		}
		throw new java.net.SocketException("Socket closed");
#endif
	}
}

static class Java_java_net_DatagramPacket
{
	public static void init()
	{
	}
}

static class Java_java_net_InetAddress
{
	public static void init()
	{
	}

#if !FIRST_PASS
	internal static java.net.InetAddress ConvertIPAddress(IPAddress address, string hostname)
	{
		if (address.IsIPv6LinkLocal || address.IsIPv6SiteLocal)
		{
			return java.net.Inet6Address.getByAddress(hostname, address.GetAddressBytes(), (int)address.ScopeId);
		}
		else
		{
			return java.net.InetAddress.getByAddress(hostname, address.GetAddressBytes());
		}
	}
#endif
}

static class Java_java_net_InetAddressImplFactory
{
	private static readonly bool ipv6supported = Init();

	private static bool Init()
	{
		string env = IKVM.Internal.JVM.SafeGetEnvironmentVariable("IKVM_IPV6");
		int val;
		if (env != null && Int32.TryParse(env, out val))
		{
			return (val & 1) != 0;
		}
		// On Linux we can't bind both an IPv4 and IPv6 to the same port, so we have to disable IPv6 until we have a dual-stack implementation.
		// Mono on Windows doesn't appear to support IPv6 either (Mono on Linux does).
		return Type.GetType("Mono.Runtime") == null
			&& Environment.OSVersion.Platform == PlatformID.Win32NT
			&& Socket.OSSupportsIPv6;
	}

	public static bool isIPv6Supported()
	{
		return ipv6supported;
	}
}

static class Java_java_net_Inet4Address
{
	public static void init()
	{
	}
}

static class Java_java_net_Inet4AddressImpl
{
	public static string getLocalHostName(object thisInet4AddressImpl)
	{
#if FIRST_PASS
		return null;
#else
		try
		{
			return Dns.GetHostName();
		}
		catch (SocketException)
		{
		}
		catch (SecurityException)
		{
		}
		return "localhost";
#endif
	}

	public static object lookupAllHostAddr(object thisInet4AddressImpl, string hostname)
	{
#if FIRST_PASS
		return null;
#else
		try
		{
			IPAddress[] addr = Dns.GetHostAddresses(hostname);
			List<java.net.InetAddress> addresses = new List<java.net.InetAddress>();
			for (int i = 0; i < addr.Length; i++)
			{
				byte[] b = addr[i].GetAddressBytes();
				if (b.Length == 4)
				{
					addresses.Add(java.net.InetAddress.getByAddress(hostname, b));
				}
			}
			if (addresses.Count == 0)
			{
				throw new java.net.UnknownHostException(hostname);
			}
			return addresses.ToArray();
		}
		catch (ArgumentException x)
		{
			throw new java.net.UnknownHostException(x.Message);
		}
		catch (SocketException x)
		{
			throw new java.net.UnknownHostException(x.Message);
		}
#endif
	}

	public static string getHostByAddr(object thisInet4AddressImpl, byte[] addr)
	{
#if FIRST_PASS
		return null;
#else
		try
		{
			return Dns.GetHostEntry(new IPAddress(addr)).HostName;
		}
		catch (ArgumentException x)
		{
			throw new java.net.UnknownHostException(x.Message);
		}
		catch (SocketException x)
		{
			throw new java.net.UnknownHostException(x.Message);
		}
#endif
	}

	public static bool isReachable0(object thisInet4AddressImpl, byte[] addr, int timeout, byte[] ifaddr, int ttl)
	{
		// like the JDK, we don't use Ping, but we try a TCP connection to the echo port
		// (.NET 2.0 has a System.Net.NetworkInformation.Ping class, but that doesn't provide the option of binding to a specific interface)
		try
		{
			using (Socket sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp))
			{
				if (ifaddr != null)
				{
					sock.Bind(new IPEndPoint(((ifaddr[3] << 24) + (ifaddr[2] << 16) + (ifaddr[1] << 8) + ifaddr[0]) & 0xFFFFFFFFL, 0));
				}
				if (ttl > 0)
				{
					sock.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.IpTimeToLive, ttl);
				}
				IPEndPoint ep = new IPEndPoint(((addr[3] << 24) + (addr[2] << 16) + (addr[1] << 8) + addr[0]) & 0xFFFFFFFFL, 7);
				IAsyncResult res = sock.BeginConnect(ep, null, null);
				if (res.AsyncWaitHandle.WaitOne(timeout, false))
				{
					try
					{
						sock.EndConnect(res);
						return true;
					}
					catch (SocketException x)
					{
						const int WSAECONNREFUSED = 10061;
						if (x.ErrorCode == WSAECONNREFUSED)
						{
							// we got back an explicit "connection refused", that means the host was reachable.
							return true;
						}
					}
				}
			}
		}
		catch (SocketException)
		{
		}
		return false;
	}
}

static class Java_java_net_Inet6Address
{
	public static void init()
	{
	}
}

static class Java_java_net_Inet6AddressImpl
{
	public static string getLocalHostName(object thisInet6AddressImpl)
	{
#if FIRST_PASS
		return null;
#else
		try
		{
			return Dns.GetHostName();
		}
		catch (SocketException x)
		{
			throw new java.net.UnknownHostException(x.Message);
		}
#endif
	}

	public static object lookupAllHostAddr(object thisInet6AddressImpl, string hostname)
	{
#if FIRST_PASS
		return null;
#else
		try
		{
			IPAddress[] addr = Dns.GetHostAddresses(hostname);
			java.net.InetAddress[] addresses = new java.net.InetAddress[addr.Length];
			int pos = 0;
			for (int i = 0; i < addr.Length; i++)
			{
				if (addr[i].AddressFamily == AddressFamily.InterNetworkV6 == java.net.InetAddress.preferIPv6Address)
				{
					addresses[pos++] = Java_java_net_InetAddress.ConvertIPAddress(addr[i], hostname);
				}
			}
			for (int i = 0; i < addr.Length; i++)
			{
				if (addr[i].AddressFamily == AddressFamily.InterNetworkV6 != java.net.InetAddress.preferIPv6Address)
				{
					addresses[pos++] = Java_java_net_InetAddress.ConvertIPAddress(addr[i], hostname);
				}
			}
			if (addresses.Length == 0)
			{
				throw new java.net.UnknownHostException(hostname);
			}
			return addresses;
		}
		catch (ArgumentException x)
		{
			throw new java.net.UnknownHostException(x.Message);
		}
		catch (SocketException x)
		{
			throw new java.net.UnknownHostException(x.Message);
		}
#endif
	}

	public static string getHostByAddr(object thisInet6AddressImpl, byte[] addr)
	{
#if FIRST_PASS
		return null;
#else
		try
		{
			return Dns.GetHostEntry(new IPAddress(addr)).HostName;
		}
		catch (ArgumentException x)
		{
			throw new java.net.UnknownHostException(x.Message);
		}
		catch (SocketException x)
		{
			throw new java.net.UnknownHostException(x.Message);
		}
#endif
	}

	public static bool isReachable0(object thisInet6AddressImpl, byte[] addr, int scope, int timeout, byte[] inf, int ttl, int if_scope)
	{
		if (addr.Length == 4)
		{
			return Java_java_net_Inet4AddressImpl.isReachable0(null, addr, timeout, inf, ttl);
		}
		// like the JDK, we don't use Ping, but we try a TCP connection to the echo port
		// (.NET 2.0 has a System.Net.NetworkInformation.Ping class, but that doesn't provide the option of binding to a specific interface)
		try
		{
			using (Socket sock = new Socket(AddressFamily.InterNetworkV6, SocketType.Stream, ProtocolType.Tcp))
			{
				if (inf != null)
				{
					sock.Bind(new IPEndPoint(new IPAddress(inf, (uint)if_scope), 0));
				}
				if (ttl > 0)
				{
					sock.SetSocketOption(SocketOptionLevel.IPv6, SocketOptionName.HopLimit, ttl);
				}
				IPEndPoint ep = new IPEndPoint(new IPAddress(addr, (uint)scope), 7);
				IAsyncResult res = sock.BeginConnect(ep, null, null);
				if (res.AsyncWaitHandle.WaitOne(timeout, false))
				{
					try
					{
						sock.EndConnect(res);
						return true;
					}
					catch (SocketException x)
					{
						const int WSAECONNREFUSED = 10061;
						if (x.ErrorCode == WSAECONNREFUSED)
						{
							// we got back an explicit "connection refused", that means the host was reachable.
							return true;
						}
					}
				}
			}
		}
		catch (ArgumentException)
		{
		}
		catch (SocketException)
		{
		}
		return false;
	}
}

static class Java_java_net_NetworkInterface
{
#if !FIRST_PASS
	private static NetworkInterfaceInfo cache;
	private static DateTime cachedSince;
#endif

	public static void init()
	{
	}

#if !FIRST_PASS
	private sealed class NetworkInterfaceInfo
	{
		internal NetworkInterface[] dotnetInterfaces;
		internal java.net.NetworkInterface[] javaInterfaces;
	}

	private static int Compare(NetworkInterface ni1, NetworkInterface ni2)
	{
		int index1 = GetIndex(ni1);
		int index2 = GetIndex(ni2);
		return index1.CompareTo(index2);
	}

	private static IPv4InterfaceProperties GetIPv4Properties(IPInterfaceProperties props)
	{
		try
		{
			return props.GetIPv4Properties();
		}
		catch (NetworkInformationException)
		{
			return null;
		}
	}

	private static IPv6InterfaceProperties GetIPv6Properties(IPInterfaceProperties props)
	{
		try
		{
			return props.GetIPv6Properties();
		}
		catch (NetworkInformationException)
		{
			return null;
		}
	}

	private static int GetIndex(NetworkInterface ni)
	{
		IPInterfaceProperties ipprops = ni.GetIPProperties();
		IPv4InterfaceProperties ipv4props = GetIPv4Properties(ipprops);
		if (ipv4props != null)
		{
			return ipv4props.Index;
		}
		else if (Java_java_net_InetAddressImplFactory.isIPv6Supported())
		{
			IPv6InterfaceProperties ipv6props = GetIPv6Properties(ipprops);
			if (ipv6props != null)
			{
				return ipv6props.Index;
			}
		}
		return -1;
	}

	private static bool IsValid(NetworkInterface ni)
	{
		return GetIndex(ni) != -1;
	}

	private static NetworkInterfaceInfo GetInterfaces()
	{
		// Since many of the methods in java.net.NetworkInterface end up calling this method and the underlying stuff this is
		// based on isn't very quick either, we cache the array for a couple of seconds.
		if (cache != null && DateTime.UtcNow - cachedSince < new TimeSpan(0, 0, 5))
		{
			return cache;
		}
		NetworkInterface[] ifaces = NetworkInterface.GetAllNetworkInterfaces();
		// on Mono (on Windows) we need to filter out the network interfaces that don't have any IP properties
		ifaces = Array.FindAll(ifaces, IsValid);
		Array.Sort(ifaces, Compare);
		java.net.NetworkInterface[] ret = new java.net.NetworkInterface[ifaces.Length];
		int eth = 0;
		int tr = 0;
		int fddi = 0;
		int lo = 0;
		int ppp = 0;
		int sl = 0;
		int wlan = 0;
		int net = 0;
		for (int i = 0; i < ifaces.Length; i++)
		{
			string name;
			switch (ifaces[i].NetworkInterfaceType)
			{
				case NetworkInterfaceType.Ethernet:
					name = "eth" + eth++;
					break;
				case NetworkInterfaceType.TokenRing:
					name = "tr" + tr++;
					break;
				case NetworkInterfaceType.Fddi:
					name = "fddi" + fddi++;
					break;
				case NetworkInterfaceType.Loopback:
					if (lo > 0)
					{
						continue;
					}
					name = "lo";
					lo++;
					break;
				case NetworkInterfaceType.Ppp:
					name = "ppp" + ppp++;
					break;
				case NetworkInterfaceType.Slip:
					name = "sl" + sl++;
					break;
				case NetworkInterfaceType.Wireless80211:
					name = "wlan" + wlan++;
					break;
				default:
					name = "net" + net++;
					break;
			}
			java.net.NetworkInterface netif = new java.net.NetworkInterface();
			ret[i] = netif;
			netif._set1(name, ifaces[i].Description, GetIndex(ifaces[i]));
			UnicastIPAddressInformationCollection uipaic = ifaces[i].GetIPProperties().UnicastAddresses;
			List<java.net.InetAddress> addresses = new List<java.net.InetAddress>();
			List<java.net.InterfaceAddress> bindings = new List<java.net.InterfaceAddress>();
			for (int j = 0; j < uipaic.Count; j++)
			{
				IPAddress addr = uipaic[j].Address;
				if (addr.AddressFamily == AddressFamily.InterNetwork)
				{
					java.net.Inet4Address address = new java.net.Inet4Address(null, addr.GetAddressBytes());
					java.net.InterfaceAddress binding = new java.net.InterfaceAddress();
					short mask = 32;
					java.net.Inet4Address broadcast = null;
					IPAddress v4mask;
					try
					{
						v4mask = uipaic[j].IPv4Mask;
					}
					catch (NotImplementedException)
					{
						// Mono (as of 2.6.7) doesn't implement the IPv4Mask property
						v4mask = null;
					}
					if (v4mask != null && !v4mask.Equals(IPAddress.Any))
					{
						broadcast = new java.net.Inet4Address(null, -1);
						mask = 0;
						foreach (byte b in v4mask.GetAddressBytes())
						{
							mask += (short)java.lang.Integer.bitCount(b);
						}
					}
					else if (address.isLoopbackAddress())
					{
						mask = 8;
						broadcast = new java.net.Inet4Address(null, 0xffffff);
					}
					binding._set(address, broadcast, mask);
					addresses.Add(address);
					bindings.Add(binding);
				}
				else if (Java_java_net_InetAddressImplFactory.isIPv6Supported())
				{
					int scope = 0;
					if (addr.IsIPv6LinkLocal || addr.IsIPv6SiteLocal)
					{
						scope = (int)addr.ScopeId;
					}
					java.net.Inet6Address ia6 = new java.net.Inet6Address();
					ia6._holder().ipaddress = addr.GetAddressBytes();
					if (scope != 0)
					{
						ia6._holder().scope_id = scope;
						ia6._holder().scope_id_set = true;
						ia6._holder().scope_ifname = netif;
						ia6._holder().scope_ifname_set = true;
					}
					java.net.InterfaceAddress binding = new java.net.InterfaceAddress();
					// TODO where do we get the IPv6 subnet prefix length?
					short mask = 128;
					binding._set(ia6, null, mask);
					addresses.Add(ia6);
					bindings.Add(binding);
				}
			}
			netif._set2(addresses.ToArray(), bindings.ToArray(), new java.net.NetworkInterface[0]);
		}
		NetworkInterfaceInfo nii = new NetworkInterfaceInfo();
		nii.dotnetInterfaces = ifaces;
		nii.javaInterfaces = ret;
		cache = nii;
		cachedSince = DateTime.UtcNow;
		return nii;
	}
#endif

	private static NetworkInterface GetDotNetNetworkInterfaceByIndex(int index)
	{
#if FIRST_PASS
		return null;
#else
		NetworkInterfaceInfo nii = GetInterfaces();
		for (int i = 0; i < nii.javaInterfaces.Length; i++)
		{
			if (nii.javaInterfaces[i].getIndex() == index)
			{
				return nii.dotnetInterfaces[i];
			}
		}
		throw new java.net.SocketException("interface index not found");
#endif
	}

	public static object getAll()
	{
#if FIRST_PASS
		return null;
#else
		return GetInterfaces().javaInterfaces;
#endif
	}

	public static object getByName0(string name)
	{
#if FIRST_PASS
		return null;
#else
		foreach (java.net.NetworkInterface iface in GetInterfaces().javaInterfaces)
		{
			if (iface.getName() == name)
			{
				return iface;
			}
		}
		return null;
#endif
	}

	public static object getByIndex0(int index)
	{
#if FIRST_PASS
		return null;
#else
		foreach (java.net.NetworkInterface iface in GetInterfaces().javaInterfaces)
		{
			if (iface.getIndex() == index)
			{
				return iface;
			}
		}
		return null;
#endif
	}

	public static object getByInetAddress0(object addr)
	{
#if FIRST_PASS
		return null;
#else
		foreach (java.net.NetworkInterface iface in GetInterfaces().javaInterfaces)
		{
			java.util.Enumeration addresses = iface.getInetAddresses();
			while (addresses.hasMoreElements())
			{
				if (addresses.nextElement().Equals(addr))
				{
					return iface;
				}
			}
		}
		return null;
#endif
	}

	public static bool isUp0(string name, int ind)
	{
#if FIRST_PASS
		return false;
#else
		return GetDotNetNetworkInterfaceByIndex(ind).OperationalStatus == OperationalStatus.Up;
#endif
	}

	public static bool isLoopback0(string name, int ind)
	{
#if FIRST_PASS
		return false;
#else
		return GetDotNetNetworkInterfaceByIndex(ind).NetworkInterfaceType == NetworkInterfaceType.Loopback;
#endif
	}

	public static bool supportsMulticast0(string name, int ind)
	{
#if FIRST_PASS
		return false;
#else
		return GetDotNetNetworkInterfaceByIndex(ind).SupportsMulticast;
#endif
	}

	public static bool isP2P0(string name, int ind)
	{
#if FIRST_PASS
		return false;
#else
		switch (GetDotNetNetworkInterfaceByIndex(ind).NetworkInterfaceType)
		{
			case NetworkInterfaceType.Ppp:
			case NetworkInterfaceType.Slip:
				return true;
			default:
				return false;
		}
#endif
	}

	public static byte[] getMacAddr0(byte[] inAddr, string name, int ind)
	{
#if FIRST_PASS
		return null;
#else
		return GetDotNetNetworkInterfaceByIndex(ind).GetPhysicalAddress().GetAddressBytes();
#endif
	}

	public static int getMTU0(string name, int ind)
	{
#if FIRST_PASS
		return 0;
#else
		IPInterfaceProperties ipprops = GetDotNetNetworkInterfaceByIndex(ind).GetIPProperties();
		IPv4InterfaceProperties v4props = GetIPv4Properties(ipprops);
		if (v4props != null)
		{
			return v4props.Mtu;
		}
		if (Java_java_net_InetAddressImplFactory.isIPv6Supported())
		{
			IPv6InterfaceProperties v6props = GetIPv6Properties(ipprops);
			if (v6props != null)
			{
				return v6props.Mtu;
			}
		}
		return -1;
#endif
	}
}