Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openjdk-multiarch-jdk8u
Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/sun/nio/ch/Net.java
38918 views
1
/*
2
* Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4
*
5
* This code is free software; you can redistribute it and/or modify it
6
* under the terms of the GNU General Public License version 2 only, as
7
* published by the Free Software Foundation. Oracle designates this
8
* particular file as subject to the "Classpath" exception as provided
9
* by Oracle in the LICENSE file that accompanied this code.
10
*
11
* This code is distributed in the hope that it will be useful, but WITHOUT
12
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14
* version 2 for more details (a copy is included in the LICENSE file that
15
* accompanied this code).
16
*
17
* You should have received a copy of the GNU General Public License version
18
* 2 along with this work; if not, write to the Free Software Foundation,
19
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20
*
21
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22
* or visit www.oracle.com if you need additional information or have any
23
* questions.
24
*/
25
26
package sun.nio.ch;
27
28
import java.io.*;
29
import java.net.*;
30
import jdk.net.*;
31
import java.nio.channels.*;
32
import java.util.*;
33
import java.security.AccessController;
34
import java.security.PrivilegedAction;
35
import java.security.PrivilegedExceptionAction;
36
import sun.net.ExtendedOptionsImpl;
37
import static jdk.net.ExtendedSocketOptions.TCP_KEEPCOUNT;
38
import static jdk.net.ExtendedSocketOptions.TCP_KEEPIDLE;
39
import static jdk.net.ExtendedSocketOptions.TCP_KEEPINTERVAL;
40
41
42
public class Net {
43
44
private Net() { }
45
46
// unspecified protocol family
47
static final ProtocolFamily UNSPEC = new ProtocolFamily() {
48
public String name() {
49
return "UNSPEC";
50
}
51
};
52
53
// set to true if exclusive binding is on for Windows
54
private static final boolean exclusiveBind;
55
56
// set to true if the fast tcp loopback should be enabled on Windows
57
private static final boolean fastLoopback;
58
59
// -- Miscellaneous utilities --
60
61
private static volatile boolean checkedIPv6 = false;
62
private static volatile boolean isIPv6Available;
63
64
/**
65
* Tells whether dual-IPv4/IPv6 sockets should be used.
66
*/
67
static boolean isIPv6Available() {
68
if (!checkedIPv6) {
69
isIPv6Available = isIPv6Available0();
70
checkedIPv6 = true;
71
}
72
return isIPv6Available;
73
}
74
75
/**
76
* Returns true if exclusive binding is on
77
*/
78
static boolean useExclusiveBind() {
79
return exclusiveBind;
80
}
81
82
/**
83
* Tells whether IPv6 sockets can join IPv4 multicast groups
84
*/
85
static boolean canIPv6SocketJoinIPv4Group() {
86
return canIPv6SocketJoinIPv4Group0();
87
}
88
89
/**
90
* Tells whether {@link #join6} can be used to join an IPv4
91
* multicast group (IPv4 group as IPv4-mapped IPv6 address)
92
*/
93
static boolean canJoin6WithIPv4Group() {
94
return canJoin6WithIPv4Group0();
95
}
96
97
public static InetSocketAddress checkAddress(SocketAddress sa) {
98
if (sa == null)
99
throw new NullPointerException();
100
if (!(sa instanceof InetSocketAddress))
101
throw new UnsupportedAddressTypeException(); // ## needs arg
102
InetSocketAddress isa = (InetSocketAddress)sa;
103
if (isa.isUnresolved())
104
throw new UnresolvedAddressException(); // ## needs arg
105
InetAddress addr = isa.getAddress();
106
if (!(addr instanceof Inet4Address || addr instanceof Inet6Address))
107
throw new IllegalArgumentException("Invalid address type");
108
return isa;
109
}
110
111
static InetSocketAddress asInetSocketAddress(SocketAddress sa) {
112
if (!(sa instanceof InetSocketAddress))
113
throw new UnsupportedAddressTypeException();
114
return (InetSocketAddress)sa;
115
}
116
117
static void translateToSocketException(Exception x)
118
throws SocketException
119
{
120
if (x instanceof SocketException)
121
throw (SocketException)x;
122
Exception nx = x;
123
if (x instanceof ClosedChannelException)
124
nx = new SocketException("Socket is closed");
125
else if (x instanceof NotYetConnectedException)
126
nx = new SocketException("Socket is not connected");
127
else if (x instanceof AlreadyBoundException)
128
nx = new SocketException("Already bound");
129
else if (x instanceof NotYetBoundException)
130
nx = new SocketException("Socket is not bound yet");
131
else if (x instanceof UnsupportedAddressTypeException)
132
nx = new SocketException("Unsupported address type");
133
else if (x instanceof UnresolvedAddressException) {
134
nx = new SocketException("Unresolved address");
135
}
136
if (nx != x)
137
nx.initCause(x);
138
139
if (nx instanceof SocketException)
140
throw (SocketException)nx;
141
else if (nx instanceof RuntimeException)
142
throw (RuntimeException)nx;
143
else
144
throw new Error("Untranslated exception", nx);
145
}
146
147
static void translateException(Exception x,
148
boolean unknownHostForUnresolved)
149
throws IOException
150
{
151
if (x instanceof IOException)
152
throw (IOException)x;
153
// Throw UnknownHostException from here since it cannot
154
// be thrown as a SocketException
155
if (unknownHostForUnresolved &&
156
(x instanceof UnresolvedAddressException))
157
{
158
throw new UnknownHostException();
159
}
160
translateToSocketException(x);
161
}
162
163
static void translateException(Exception x)
164
throws IOException
165
{
166
translateException(x, false);
167
}
168
169
/**
170
* Returns the local address after performing a SecurityManager#checkConnect.
171
*/
172
static InetSocketAddress getRevealedLocalAddress(InetSocketAddress addr) {
173
SecurityManager sm = System.getSecurityManager();
174
if (addr == null || sm == null)
175
return addr;
176
177
try{
178
sm.checkConnect(addr.getAddress().getHostAddress(), -1);
179
// Security check passed
180
} catch (SecurityException e) {
181
// Return loopback address only if security check fails
182
addr = getLoopbackAddress(addr.getPort());
183
}
184
return addr;
185
}
186
187
static String getRevealedLocalAddressAsString(InetSocketAddress addr) {
188
return System.getSecurityManager() == null ? addr.toString() :
189
getLoopbackAddress(addr.getPort()).toString();
190
}
191
192
private static InetSocketAddress getLoopbackAddress(int port) {
193
return new InetSocketAddress(InetAddress.getLoopbackAddress(),
194
port);
195
}
196
197
/**
198
* Returns any IPv4 address of the given network interface, or
199
* null if the interface does not have any IPv4 addresses.
200
*/
201
static Inet4Address anyInet4Address(final NetworkInterface interf) {
202
return AccessController.doPrivileged(new PrivilegedAction<Inet4Address>() {
203
public Inet4Address run() {
204
Enumeration<InetAddress> addrs = interf.getInetAddresses();
205
while (addrs.hasMoreElements()) {
206
InetAddress addr = addrs.nextElement();
207
if (addr instanceof Inet4Address) {
208
return (Inet4Address)addr;
209
}
210
}
211
return null;
212
}
213
});
214
}
215
216
/**
217
* Returns an IPv4 address as an int.
218
*/
219
static int inet4AsInt(InetAddress ia) {
220
if (ia instanceof Inet4Address) {
221
byte[] addr = ia.getAddress();
222
int address = addr[3] & 0xFF;
223
address |= ((addr[2] << 8) & 0xFF00);
224
address |= ((addr[1] << 16) & 0xFF0000);
225
address |= ((addr[0] << 24) & 0xFF000000);
226
return address;
227
}
228
throw new AssertionError("Should not reach here");
229
}
230
231
/**
232
* Returns an InetAddress from the given IPv4 address
233
* represented as an int.
234
*/
235
static InetAddress inet4FromInt(int address) {
236
byte[] addr = new byte[4];
237
addr[0] = (byte) ((address >>> 24) & 0xFF);
238
addr[1] = (byte) ((address >>> 16) & 0xFF);
239
addr[2] = (byte) ((address >>> 8) & 0xFF);
240
addr[3] = (byte) (address & 0xFF);
241
try {
242
return InetAddress.getByAddress(addr);
243
} catch (UnknownHostException uhe) {
244
throw new AssertionError("Should not reach here");
245
}
246
}
247
248
/**
249
* Returns an IPv6 address as a byte array
250
*/
251
static byte[] inet6AsByteArray(InetAddress ia) {
252
if (ia instanceof Inet6Address) {
253
return ia.getAddress();
254
}
255
256
// need to construct IPv4-mapped address
257
if (ia instanceof Inet4Address) {
258
byte[] ip4address = ia.getAddress();
259
byte[] address = new byte[16];
260
address[10] = (byte)0xff;
261
address[11] = (byte)0xff;
262
address[12] = ip4address[0];
263
address[13] = ip4address[1];
264
address[14] = ip4address[2];
265
address[15] = ip4address[3];
266
return address;
267
}
268
269
throw new AssertionError("Should not reach here");
270
}
271
272
// -- Socket options
273
274
static void setSocketOption(FileDescriptor fd, ProtocolFamily family,
275
SocketOption<?> name, Object value)
276
throws IOException
277
{
278
if (value == null)
279
throw new IllegalArgumentException("Invalid option value");
280
281
// only simple values supported by this method
282
Class<?> type = name.type();
283
284
if (type == SocketFlow.class) {
285
ExtendedOptionsImpl.checkSetOptionPermission(name);
286
ExtendedOptionsImpl.checkValueType(value, SocketFlow.class);
287
ExtendedOptionsImpl.setFlowOption(fd, (SocketFlow)value);
288
return;
289
}
290
if (name == TCP_KEEPINTERVAL) {
291
ExtendedOptionsImpl.checkSetOptionPermission(name);
292
ExtendedOptionsImpl.checkValueType(value, Integer.class);
293
ExtendedOptionsImpl.setTcpKeepAliveIntvl(fd, (Integer)value);
294
return;
295
}
296
if (name == TCP_KEEPIDLE) {
297
ExtendedOptionsImpl.checkSetOptionPermission(name);
298
ExtendedOptionsImpl.checkValueType(value, Integer.class);
299
ExtendedOptionsImpl.setTcpKeepAliveTime(fd, (Integer)value);
300
return;
301
}
302
if (name == TCP_KEEPCOUNT) {
303
ExtendedOptionsImpl.checkSetOptionPermission(name);
304
ExtendedOptionsImpl.checkValueType(value, Integer.class);
305
ExtendedOptionsImpl.setTcpKeepAliveProbes(fd, (Integer)value);
306
return;
307
}
308
309
if (type != Integer.class && type != Boolean.class)
310
throw new AssertionError("Should not reach here");
311
312
// special handling
313
if (name == StandardSocketOptions.SO_RCVBUF ||
314
name == StandardSocketOptions.SO_SNDBUF)
315
{
316
int i = ((Integer)value).intValue();
317
if (i < 0)
318
throw new IllegalArgumentException("Invalid send/receive buffer size");
319
}
320
if (name == StandardSocketOptions.SO_LINGER) {
321
int i = ((Integer)value).intValue();
322
if (i < 0)
323
value = Integer.valueOf(-1);
324
if (i > 65535)
325
value = Integer.valueOf(65535);
326
}
327
if (name == StandardSocketOptions.IP_TOS) {
328
int i = ((Integer)value).intValue();
329
if (i < 0 || i > 255)
330
throw new IllegalArgumentException("Invalid IP_TOS value");
331
}
332
if (name == StandardSocketOptions.IP_MULTICAST_TTL) {
333
int i = ((Integer)value).intValue();
334
if (i < 0 || i > 255)
335
throw new IllegalArgumentException("Invalid TTL/hop value");
336
}
337
338
// map option name to platform level/name
339
OptionKey key = SocketOptionRegistry.findOption(name, family);
340
if (key == null)
341
throw new AssertionError("Option not found");
342
343
int arg;
344
if (type == Integer.class) {
345
arg = ((Integer)value).intValue();
346
} else {
347
boolean b = ((Boolean)value).booleanValue();
348
arg = (b) ? 1 : 0;
349
}
350
351
boolean mayNeedConversion = (family == UNSPEC);
352
boolean isIPv6 = (family == StandardProtocolFamily.INET6);
353
setIntOption0(fd, mayNeedConversion, key.level(), key.name(), arg, isIPv6);
354
}
355
356
static Object getSocketOption(FileDescriptor fd, ProtocolFamily family,
357
SocketOption<?> name)
358
throws IOException
359
{
360
Class<?> type = name.type();
361
362
if (type == SocketFlow.class) {
363
ExtendedOptionsImpl.checkGetOptionPermission(name);
364
SocketFlow flow = SocketFlow.create();
365
ExtendedOptionsImpl.getFlowOption(fd, flow);
366
return flow;
367
}
368
if (name == TCP_KEEPINTERVAL) {
369
ExtendedOptionsImpl.checkGetOptionPermission(name);
370
return ExtendedOptionsImpl.getTcpKeepAliveIntvl(fd);
371
}
372
if (name == TCP_KEEPIDLE) {
373
ExtendedOptionsImpl.checkGetOptionPermission(name);
374
return ExtendedOptionsImpl.getTcpKeepAliveTime(fd);
375
}
376
if (name == TCP_KEEPCOUNT) {
377
ExtendedOptionsImpl.checkGetOptionPermission(name);
378
return ExtendedOptionsImpl.getTcpKeepAliveProbes(fd);
379
}
380
381
// only simple values supported by this method
382
if (type != Integer.class && type != Boolean.class)
383
throw new AssertionError("Should not reach here");
384
385
// map option name to platform level/name
386
OptionKey key = SocketOptionRegistry.findOption(name, family);
387
if (key == null)
388
throw new AssertionError("Option not found");
389
390
boolean mayNeedConversion = (family == UNSPEC);
391
int value = getIntOption0(fd, mayNeedConversion, key.level(), key.name());
392
393
if (type == Integer.class) {
394
return Integer.valueOf(value);
395
} else {
396
return (value == 0) ? Boolean.FALSE : Boolean.TRUE;
397
}
398
}
399
400
public static boolean isFastTcpLoopbackRequested() {
401
String loopbackProp = java.security.AccessController.doPrivileged(
402
new PrivilegedAction<String>() {
403
@Override
404
public String run() {
405
return System.getProperty("jdk.net.useFastTcpLoopback");
406
}
407
});
408
boolean enable;
409
if ("".equals(loopbackProp)) {
410
enable = true;
411
} else {
412
enable = Boolean.parseBoolean(loopbackProp);
413
}
414
return enable;
415
}
416
417
// -- Socket operations --
418
419
private static native boolean isIPv6Available0();
420
421
/*
422
* Returns 1 for Windows versions that support exclusive binding by default, 0
423
* for those that do not, and -1 for Solaris/Linux/Mac OS
424
*/
425
private static native int isExclusiveBindAvailable();
426
427
private static native boolean canIPv6SocketJoinIPv4Group0();
428
429
private static native boolean canJoin6WithIPv4Group0();
430
431
static FileDescriptor socket(boolean stream) throws IOException {
432
return socket(UNSPEC, stream);
433
}
434
435
static FileDescriptor socket(ProtocolFamily family, boolean stream)
436
throws IOException {
437
boolean preferIPv6 = isIPv6Available() &&
438
(family != StandardProtocolFamily.INET);
439
return IOUtil.newFD(socket0(preferIPv6, stream, false, fastLoopback));
440
}
441
442
static FileDescriptor serverSocket(boolean stream) {
443
return IOUtil.newFD(socket0(isIPv6Available(), stream, true, fastLoopback));
444
}
445
446
// Due to oddities SO_REUSEADDR on windows reuse is ignored
447
private static native int socket0(boolean preferIPv6, boolean stream, boolean reuse,
448
boolean fastLoopback);
449
450
public static void bind(FileDescriptor fd, InetAddress addr, int port)
451
throws IOException
452
{
453
bind(UNSPEC, fd, addr, port);
454
}
455
456
static void bind(ProtocolFamily family, FileDescriptor fd,
457
InetAddress addr, int port) throws IOException
458
{
459
boolean preferIPv6 = isIPv6Available() &&
460
(family != StandardProtocolFamily.INET);
461
bind0(fd, preferIPv6, exclusiveBind, addr, port);
462
}
463
464
private static native void bind0(FileDescriptor fd, boolean preferIPv6,
465
boolean useExclBind, InetAddress addr,
466
int port)
467
throws IOException;
468
469
static native void listen(FileDescriptor fd, int backlog) throws IOException;
470
471
static int connect(FileDescriptor fd, InetAddress remote, int remotePort)
472
throws IOException
473
{
474
return connect(UNSPEC, fd, remote, remotePort);
475
}
476
477
static int connect(ProtocolFamily family, FileDescriptor fd, InetAddress remote, int remotePort)
478
throws IOException
479
{
480
boolean preferIPv6 = isIPv6Available() &&
481
(family != StandardProtocolFamily.INET);
482
return connect0(preferIPv6, fd, remote, remotePort);
483
}
484
485
private static native int connect0(boolean preferIPv6,
486
FileDescriptor fd,
487
InetAddress remote,
488
int remotePort)
489
throws IOException;
490
491
492
public final static int SHUT_RD = 0;
493
public final static int SHUT_WR = 1;
494
public final static int SHUT_RDWR = 2;
495
496
static native void shutdown(FileDescriptor fd, int how) throws IOException;
497
498
private static native int localPort(FileDescriptor fd)
499
throws IOException;
500
501
private static native InetAddress localInetAddress(FileDescriptor fd)
502
throws IOException;
503
504
public static InetSocketAddress localAddress(FileDescriptor fd)
505
throws IOException
506
{
507
return new InetSocketAddress(localInetAddress(fd), localPort(fd));
508
}
509
510
private static native int remotePort(FileDescriptor fd)
511
throws IOException;
512
513
private static native InetAddress remoteInetAddress(FileDescriptor fd)
514
throws IOException;
515
516
static InetSocketAddress remoteAddress(FileDescriptor fd)
517
throws IOException
518
{
519
return new InetSocketAddress(remoteInetAddress(fd), remotePort(fd));
520
}
521
522
private static native int getIntOption0(FileDescriptor fd, boolean mayNeedConversion,
523
int level, int opt)
524
throws IOException;
525
526
private static native void setIntOption0(FileDescriptor fd, boolean mayNeedConversion,
527
int level, int opt, int arg, boolean isIPv6)
528
throws IOException;
529
530
static native int poll(FileDescriptor fd, int events, long timeout)
531
throws IOException;
532
533
// -- Multicast support --
534
535
536
/**
537
* Join IPv4 multicast group
538
*/
539
static int join4(FileDescriptor fd, int group, int interf, int source)
540
throws IOException
541
{
542
return joinOrDrop4(true, fd, group, interf, source);
543
}
544
545
/**
546
* Drop membership of IPv4 multicast group
547
*/
548
static void drop4(FileDescriptor fd, int group, int interf, int source)
549
throws IOException
550
{
551
joinOrDrop4(false, fd, group, interf, source);
552
}
553
554
private static native int joinOrDrop4(boolean join, FileDescriptor fd, int group, int interf, int source)
555
throws IOException;
556
557
/**
558
* Block IPv4 source
559
*/
560
static int block4(FileDescriptor fd, int group, int interf, int source)
561
throws IOException
562
{
563
return blockOrUnblock4(true, fd, group, interf, source);
564
}
565
566
/**
567
* Unblock IPv6 source
568
*/
569
static void unblock4(FileDescriptor fd, int group, int interf, int source)
570
throws IOException
571
{
572
blockOrUnblock4(false, fd, group, interf, source);
573
}
574
575
private static native int blockOrUnblock4(boolean block, FileDescriptor fd, int group,
576
int interf, int source)
577
throws IOException;
578
579
/**
580
* Join IPv6 multicast group
581
*/
582
static int join6(FileDescriptor fd, byte[] group, int index, byte[] source)
583
throws IOException
584
{
585
return joinOrDrop6(true, fd, group, index, source);
586
}
587
588
/**
589
* Drop membership of IPv6 multicast group
590
*/
591
static void drop6(FileDescriptor fd, byte[] group, int index, byte[] source)
592
throws IOException
593
{
594
joinOrDrop6(false, fd, group, index, source);
595
}
596
597
private static native int joinOrDrop6(boolean join, FileDescriptor fd, byte[] group, int index, byte[] source)
598
throws IOException;
599
600
/**
601
* Block IPv6 source
602
*/
603
static int block6(FileDescriptor fd, byte[] group, int index, byte[] source)
604
throws IOException
605
{
606
return blockOrUnblock6(true, fd, group, index, source);
607
}
608
609
/**
610
* Unblock IPv6 source
611
*/
612
static void unblock6(FileDescriptor fd, byte[] group, int index, byte[] source)
613
throws IOException
614
{
615
blockOrUnblock6(false, fd, group, index, source);
616
}
617
618
static native int blockOrUnblock6(boolean block, FileDescriptor fd, byte[] group, int index, byte[] source)
619
throws IOException;
620
621
static native void setInterface4(FileDescriptor fd, int interf) throws IOException;
622
623
static native int getInterface4(FileDescriptor fd) throws IOException;
624
625
static native void setInterface6(FileDescriptor fd, int index) throws IOException;
626
627
static native int getInterface6(FileDescriptor fd) throws IOException;
628
629
private static native void initIDs();
630
631
/**
632
* Event masks for the various poll system calls.
633
* They will be set platform dependant in the static initializer below.
634
*/
635
public static final short POLLIN;
636
public static final short POLLOUT;
637
public static final short POLLERR;
638
public static final short POLLHUP;
639
public static final short POLLNVAL;
640
public static final short POLLCONN;
641
642
static native short pollinValue();
643
static native short polloutValue();
644
static native short pollerrValue();
645
static native short pollhupValue();
646
static native short pollnvalValue();
647
static native short pollconnValue();
648
649
static {
650
IOUtil.load();
651
initIDs();
652
653
POLLIN = pollinValue();
654
POLLOUT = polloutValue();
655
POLLERR = pollerrValue();
656
POLLHUP = pollhupValue();
657
POLLNVAL = pollnvalValue();
658
POLLCONN = pollconnValue();
659
}
660
661
static {
662
int availLevel = isExclusiveBindAvailable();
663
if (availLevel >= 0) {
664
String exclBindProp =
665
java.security.AccessController.doPrivileged(
666
new PrivilegedAction<String>() {
667
@Override
668
public String run() {
669
return System.getProperty(
670
"sun.net.useExclusiveBind");
671
}
672
});
673
if (exclBindProp != null) {
674
exclusiveBind = exclBindProp.length() == 0 ?
675
true : Boolean.parseBoolean(exclBindProp);
676
} else if (availLevel == 1) {
677
exclusiveBind = true;
678
} else {
679
exclusiveBind = false;
680
}
681
} else {
682
exclusiveBind = false;
683
}
684
685
fastLoopback = isFastTcpLoopbackRequested();
686
}
687
}
688
689