Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/src/java.base/share/classes/sun/nio/ch/Net.java
41159 views
1
/*
2
* Copyright (c) 2000, 2021, 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.FileDescriptor;
29
import java.io.IOException;
30
import java.net.Inet4Address;
31
import java.net.Inet6Address;
32
import java.net.InetAddress;
33
import java.net.InetSocketAddress;
34
import java.net.NetworkInterface;
35
import java.net.ProtocolFamily;
36
import java.net.SocketAddress;
37
import java.net.SocketException;
38
import java.net.SocketOption;
39
import java.net.StandardProtocolFamily;
40
import java.net.StandardSocketOptions;
41
import java.net.UnknownHostException;
42
import java.nio.channels.AlreadyBoundException;
43
import java.nio.channels.ClosedChannelException;
44
import java.nio.channels.NotYetBoundException;
45
import java.nio.channels.NotYetConnectedException;
46
import java.nio.channels.UnresolvedAddressException;
47
import java.nio.channels.UnsupportedAddressTypeException;
48
import java.security.AccessController;
49
import java.security.PrivilegedAction;
50
import java.util.Enumeration;
51
52
import sun.net.ext.ExtendedSocketOptions;
53
import sun.net.util.IPAddressUtil;
54
import sun.security.action.GetPropertyAction;
55
56
public class Net {
57
58
private Net() { }
59
60
// unspecified protocol family
61
static final ProtocolFamily UNSPEC = new ProtocolFamily() {
62
public String name() {
63
return "UNSPEC";
64
}
65
};
66
67
// set to true if exclusive binding is on for Windows
68
private static final boolean exclusiveBind;
69
70
// set to true if the fast tcp loopback should be enabled on Windows
71
private static final boolean fastLoopback;
72
73
// -- Miscellaneous utilities --
74
75
private static volatile boolean checkedIPv6;
76
private static volatile boolean isIPv6Available;
77
private static volatile boolean checkedReusePort;
78
private static volatile boolean isReusePortAvailable;
79
80
/**
81
* Tells whether dual-IPv4/IPv6 sockets should be used.
82
*/
83
static boolean isIPv6Available() {
84
if (!checkedIPv6) {
85
isIPv6Available = isIPv6Available0();
86
checkedIPv6 = true;
87
}
88
return isIPv6Available;
89
}
90
91
/**
92
* Tells whether SO_REUSEPORT is supported.
93
*/
94
static boolean isReusePortAvailable() {
95
if (!checkedReusePort) {
96
isReusePortAvailable = isReusePortAvailable0();
97
checkedReusePort = true;
98
}
99
return isReusePortAvailable;
100
}
101
102
/**
103
* Returns true if exclusive binding is on
104
*/
105
static boolean useExclusiveBind() {
106
return exclusiveBind;
107
}
108
109
/**
110
* Tells whether both IPV6_XXX and IP_XXX socket options should be set on
111
* IPv6 sockets. On some kernels, both IPV6_XXX and IP_XXX socket options
112
* need to be set so that the settings are effective for IPv4 multicast
113
* datagrams sent using the socket.
114
*/
115
static boolean shouldSetBothIPv4AndIPv6Options() {
116
return shouldSetBothIPv4AndIPv6Options0();
117
}
118
119
/**
120
* Tells whether IPv6 sockets can join IPv4 multicast groups
121
*/
122
static boolean canIPv6SocketJoinIPv4Group() {
123
return canIPv6SocketJoinIPv4Group0();
124
}
125
126
/**
127
* Tells whether {@link #join6} can be used to join an IPv4
128
* multicast group (IPv4 group as IPv4-mapped IPv6 address)
129
*/
130
static boolean canJoin6WithIPv4Group() {
131
return canJoin6WithIPv4Group0();
132
}
133
134
/**
135
* Tells whether IPV6_XXX socket options should be used on an IPv6 socket
136
* that is bound to an IPv4 address.
137
*/
138
static boolean canUseIPv6OptionsWithIPv4LocalAddress() {
139
return canUseIPv6OptionsWithIPv4LocalAddress0();
140
}
141
142
public static InetSocketAddress checkAddress(SocketAddress sa) {
143
if (sa == null)
144
throw new NullPointerException();
145
if (!(sa instanceof InetSocketAddress))
146
throw new UnsupportedAddressTypeException(); // ## needs arg
147
InetSocketAddress isa = (InetSocketAddress)sa;
148
if (isa.isUnresolved())
149
throw new UnresolvedAddressException(); // ## needs arg
150
InetAddress addr = isa.getAddress();
151
if (!(addr instanceof Inet4Address || addr instanceof Inet6Address))
152
throw new IllegalArgumentException("Invalid address type");
153
return isa;
154
}
155
156
static InetSocketAddress checkAddress(SocketAddress sa, ProtocolFamily family) {
157
InetSocketAddress isa = checkAddress(sa);
158
if (family == StandardProtocolFamily.INET) {
159
InetAddress addr = isa.getAddress();
160
if (!(addr instanceof Inet4Address))
161
throw new UnsupportedAddressTypeException();
162
}
163
return isa;
164
}
165
166
static InetSocketAddress asInetSocketAddress(SocketAddress sa) {
167
if (!(sa instanceof InetSocketAddress))
168
throw new UnsupportedAddressTypeException();
169
return (InetSocketAddress)sa;
170
}
171
172
static void translateToSocketException(Exception x)
173
throws SocketException
174
{
175
if (x instanceof SocketException)
176
throw (SocketException)x;
177
Exception nx = x;
178
if (x instanceof ClosedChannelException)
179
nx = new SocketException("Socket is closed");
180
else if (x instanceof NotYetConnectedException)
181
nx = new SocketException("Socket is not connected");
182
else if (x instanceof AlreadyBoundException)
183
nx = new SocketException("Already bound");
184
else if (x instanceof NotYetBoundException)
185
nx = new SocketException("Socket is not bound yet");
186
else if (x instanceof UnsupportedAddressTypeException)
187
nx = new SocketException("Unsupported address type");
188
else if (x instanceof UnresolvedAddressException)
189
nx = new SocketException("Unresolved address");
190
else if (x instanceof IOException) {
191
nx = new SocketException(x.getMessage());
192
}
193
if (nx != x)
194
nx.initCause(x);
195
196
if (nx instanceof SocketException)
197
throw (SocketException)nx;
198
else if (nx instanceof RuntimeException)
199
throw (RuntimeException)nx;
200
else
201
throw new Error("Untranslated exception", nx);
202
}
203
204
static void translateException(Exception x,
205
boolean unknownHostForUnresolved)
206
throws IOException
207
{
208
if (x instanceof IOException)
209
throw (IOException)x;
210
// Throw UnknownHostException from here since it cannot
211
// be thrown as a SocketException
212
if (unknownHostForUnresolved &&
213
(x instanceof UnresolvedAddressException))
214
{
215
throw new UnknownHostException();
216
}
217
translateToSocketException(x);
218
}
219
220
static void translateException(Exception x)
221
throws IOException
222
{
223
translateException(x, false);
224
}
225
226
/**
227
* Returns the local address after performing a SecurityManager#checkConnect.
228
*/
229
static InetSocketAddress getRevealedLocalAddress(SocketAddress sa) {
230
InetSocketAddress isa = (InetSocketAddress) sa;
231
@SuppressWarnings("removal")
232
SecurityManager sm = System.getSecurityManager();
233
if (isa != null && sm != null) {
234
try {
235
sm.checkConnect(isa.getAddress().getHostAddress(), -1);
236
} catch (SecurityException e) {
237
// Return loopback address only if security check fails
238
isa = getLoopbackAddress(isa.getPort());
239
}
240
}
241
return isa;
242
}
243
244
@SuppressWarnings("removal")
245
static String getRevealedLocalAddressAsString(SocketAddress sa) {
246
InetSocketAddress isa = (InetSocketAddress) sa;
247
if (System.getSecurityManager() == null) {
248
return isa.toString();
249
} else {
250
return getLoopbackAddress(isa.getPort()).toString();
251
}
252
}
253
254
private static InetSocketAddress getLoopbackAddress(int port) {
255
return new InetSocketAddress(InetAddress.getLoopbackAddress(), port);
256
}
257
258
private static final InetAddress anyLocalInet4Address;
259
private static final InetAddress anyLocalInet6Address;
260
private static final InetAddress inet4LoopbackAddress;
261
private static final InetAddress inet6LoopbackAddress;
262
static {
263
try {
264
anyLocalInet4Address = inet4FromInt(0);
265
assert anyLocalInet4Address instanceof Inet4Address
266
&& anyLocalInet4Address.isAnyLocalAddress();
267
268
anyLocalInet6Address = InetAddress.getByAddress(new byte[16]);
269
assert anyLocalInet6Address instanceof Inet6Address
270
&& anyLocalInet6Address.isAnyLocalAddress();
271
272
inet4LoopbackAddress = inet4FromInt(0x7f000001);
273
assert inet4LoopbackAddress instanceof Inet4Address
274
&& inet4LoopbackAddress.isLoopbackAddress();
275
276
byte[] bytes = new byte[16];
277
bytes[15] = 0x01;
278
inet6LoopbackAddress = InetAddress.getByAddress(bytes);
279
assert inet6LoopbackAddress instanceof Inet6Address
280
&& inet6LoopbackAddress.isLoopbackAddress();
281
} catch (Exception e) {
282
throw new InternalError(e);
283
}
284
}
285
286
static InetAddress inet4LoopbackAddress() {
287
return inet4LoopbackAddress;
288
}
289
290
static InetAddress inet6LoopbackAddress() {
291
return inet6LoopbackAddress;
292
}
293
294
/**
295
* Returns the wildcard address that corresponds to the given protocol family.
296
*
297
* @see InetAddress#isAnyLocalAddress()
298
*/
299
static InetAddress anyLocalAddress(ProtocolFamily family) {
300
if (family == StandardProtocolFamily.INET) {
301
return anyLocalInet4Address;
302
} else if (family == StandardProtocolFamily.INET6) {
303
return anyLocalInet6Address;
304
} else {
305
throw new IllegalArgumentException();
306
}
307
}
308
309
/**
310
* Returns any IPv4 address of the given network interface, or
311
* null if the interface does not have any IPv4 addresses.
312
*/
313
@SuppressWarnings("removal")
314
static Inet4Address anyInet4Address(final NetworkInterface interf) {
315
return AccessController.doPrivileged(new PrivilegedAction<Inet4Address>() {
316
public Inet4Address run() {
317
Enumeration<InetAddress> addrs = interf.getInetAddresses();
318
while (addrs.hasMoreElements()) {
319
InetAddress addr = addrs.nextElement();
320
if (addr instanceof Inet4Address) {
321
return (Inet4Address)addr;
322
}
323
}
324
return null;
325
}
326
});
327
}
328
329
/**
330
* Returns an IPv4 address as an int.
331
*/
332
static int inet4AsInt(InetAddress ia) {
333
if (ia instanceof Inet4Address) {
334
byte[] addr = ia.getAddress();
335
int address = addr[3] & 0xFF;
336
address |= ((addr[2] << 8) & 0xFF00);
337
address |= ((addr[1] << 16) & 0xFF0000);
338
address |= ((addr[0] << 24) & 0xFF000000);
339
return address;
340
}
341
throw new AssertionError("Should not reach here");
342
}
343
344
/**
345
* Returns an InetAddress from the given IPv4 address
346
* represented as an int.
347
*/
348
static InetAddress inet4FromInt(int address) {
349
byte[] addr = new byte[4];
350
addr[0] = (byte) ((address >>> 24) & 0xFF);
351
addr[1] = (byte) ((address >>> 16) & 0xFF);
352
addr[2] = (byte) ((address >>> 8) & 0xFF);
353
addr[3] = (byte) (address & 0xFF);
354
try {
355
return InetAddress.getByAddress(addr);
356
} catch (UnknownHostException uhe) {
357
throw new AssertionError("Should not reach here");
358
}
359
}
360
361
/**
362
* Returns an IPv6 address as a byte array
363
*/
364
static byte[] inet6AsByteArray(InetAddress ia) {
365
if (ia instanceof Inet6Address) {
366
return ia.getAddress();
367
}
368
369
// need to construct IPv4-mapped address
370
if (ia instanceof Inet4Address) {
371
byte[] ip4address = ia.getAddress();
372
byte[] address = new byte[16];
373
address[10] = (byte)0xff;
374
address[11] = (byte)0xff;
375
address[12] = ip4address[0];
376
address[13] = ip4address[1];
377
address[14] = ip4address[2];
378
address[15] = ip4address[3];
379
return address;
380
}
381
382
throw new AssertionError("Should not reach here");
383
}
384
385
// -- Socket options
386
387
static final ExtendedSocketOptions extendedOptions =
388
ExtendedSocketOptions.getInstance();
389
390
static void setSocketOption(FileDescriptor fd, SocketOption<?> name, Object value)
391
throws IOException
392
{
393
setSocketOption(fd, Net.UNSPEC, name, value);
394
}
395
396
static void setSocketOption(FileDescriptor fd, ProtocolFamily family,
397
SocketOption<?> name, Object value)
398
throws IOException
399
{
400
if (value == null)
401
throw new IllegalArgumentException("Invalid option value");
402
403
// only simple values supported by this method
404
Class<?> type = name.type();
405
406
if (extendedOptions.isOptionSupported(name)) {
407
extendedOptions.setOption(fd, name, value);
408
return;
409
}
410
411
if (type != Integer.class && type != Boolean.class)
412
throw new AssertionError("Should not reach here");
413
414
// special handling
415
if (name == StandardSocketOptions.SO_RCVBUF ||
416
name == StandardSocketOptions.SO_SNDBUF)
417
{
418
int i = ((Integer)value).intValue();
419
if (i < 0)
420
throw new IllegalArgumentException("Invalid send/receive buffer size");
421
}
422
if (name == StandardSocketOptions.SO_LINGER) {
423
int i = ((Integer)value).intValue();
424
if (i < 0)
425
value = Integer.valueOf(-1);
426
if (i > 65535)
427
value = Integer.valueOf(65535);
428
}
429
if (name == StandardSocketOptions.IP_TOS) {
430
int i = ((Integer)value).intValue();
431
if (i < 0 || i > 255)
432
throw new IllegalArgumentException("Invalid IP_TOS value");
433
}
434
if (name == StandardSocketOptions.IP_MULTICAST_TTL) {
435
int i = ((Integer)value).intValue();
436
if (i < 0 || i > 255)
437
throw new IllegalArgumentException("Invalid TTL/hop value");
438
}
439
440
// map option name to platform level/name
441
OptionKey key = SocketOptionRegistry.findOption(name, family);
442
if (key == null)
443
throw new AssertionError("Option not found");
444
445
int arg;
446
if (type == Integer.class) {
447
arg = ((Integer)value).intValue();
448
} else {
449
boolean b = ((Boolean)value).booleanValue();
450
arg = (b) ? 1 : 0;
451
}
452
453
boolean mayNeedConversion = (family == UNSPEC);
454
boolean isIPv6 = (family == StandardProtocolFamily.INET6);
455
setIntOption0(fd, mayNeedConversion, key.level(), key.name(), arg, isIPv6);
456
}
457
458
static Object getSocketOption(FileDescriptor fd, SocketOption<?> name)
459
throws IOException
460
{
461
return getSocketOption(fd, Net.UNSPEC, name);
462
}
463
464
static Object getSocketOption(FileDescriptor fd, ProtocolFamily family, SocketOption<?> name)
465
throws IOException
466
{
467
Class<?> type = name.type();
468
469
if (extendedOptions.isOptionSupported(name)) {
470
return extendedOptions.getOption(fd, name);
471
}
472
473
// only simple values supported by this method
474
if (type != Integer.class && type != Boolean.class)
475
throw new AssertionError("Should not reach here");
476
477
// map option name to platform level/name
478
OptionKey key = SocketOptionRegistry.findOption(name, family);
479
if (key == null)
480
throw new AssertionError("Option not found");
481
482
boolean mayNeedConversion = (family == UNSPEC);
483
int value = getIntOption0(fd, mayNeedConversion, key.level(), key.name());
484
485
if (type == Integer.class) {
486
return Integer.valueOf(value);
487
} else {
488
return (value == 0) ? Boolean.FALSE : Boolean.TRUE;
489
}
490
}
491
492
public static boolean isFastTcpLoopbackRequested() {
493
String loopbackProp = GetPropertyAction
494
.privilegedGetProperty("jdk.net.useFastTcpLoopback", "false");
495
return loopbackProp.isEmpty() ? true : Boolean.parseBoolean(loopbackProp);
496
}
497
498
// -- Socket operations --
499
500
private static native boolean isIPv6Available0();
501
502
private static native boolean isReusePortAvailable0();
503
504
/*
505
* Returns 1 for Windows and -1 for Linux/Mac OS
506
*/
507
private static native int isExclusiveBindAvailable();
508
509
private static native boolean shouldSetBothIPv4AndIPv6Options0();
510
511
private static native boolean canIPv6SocketJoinIPv4Group0();
512
513
private static native boolean canJoin6WithIPv4Group0();
514
515
private static native boolean canUseIPv6OptionsWithIPv4LocalAddress0();
516
517
static FileDescriptor socket(boolean stream) throws IOException {
518
return socket(UNSPEC, stream);
519
}
520
521
static FileDescriptor socket(ProtocolFamily family, boolean stream) throws IOException {
522
boolean preferIPv6 = isIPv6Available() &&
523
(family != StandardProtocolFamily.INET);
524
return IOUtil.newFD(socket0(preferIPv6, stream, false, fastLoopback));
525
}
526
527
static FileDescriptor serverSocket(boolean stream) {
528
return serverSocket(UNSPEC, stream);
529
}
530
531
static FileDescriptor serverSocket(ProtocolFamily family, boolean stream) {
532
boolean preferIPv6 = isIPv6Available() &&
533
(family != StandardProtocolFamily.INET);
534
return IOUtil.newFD(socket0(preferIPv6, stream, true, fastLoopback));
535
}
536
537
// Due to oddities SO_REUSEADDR on windows reuse is ignored
538
private static native int socket0(boolean preferIPv6, boolean stream, boolean reuse,
539
boolean fastLoopback);
540
541
public static void bind(FileDescriptor fd, InetAddress addr, int port)
542
throws IOException
543
{
544
bind(UNSPEC, fd, addr, port);
545
}
546
547
static void bind(ProtocolFamily family, FileDescriptor fd,
548
InetAddress addr, int port) throws IOException
549
{
550
boolean preferIPv6 = isIPv6Available() &&
551
(family != StandardProtocolFamily.INET);
552
if (addr.isLinkLocalAddress()) {
553
addr = IPAddressUtil.toScopedAddress(addr);
554
}
555
bind0(fd, preferIPv6, exclusiveBind, addr, port);
556
}
557
558
private static native void bind0(FileDescriptor fd, boolean preferIPv6,
559
boolean useExclBind, InetAddress addr,
560
int port)
561
throws IOException;
562
563
static native void listen(FileDescriptor fd, int backlog) throws IOException;
564
565
static int connect(FileDescriptor fd, InetAddress remote, int remotePort)
566
throws IOException
567
{
568
return connect(UNSPEC, fd, remote, remotePort);
569
}
570
571
static int connect(ProtocolFamily family, FileDescriptor fd, InetAddress remote, int remotePort)
572
throws IOException
573
{
574
if (remote.isLinkLocalAddress()) {
575
remote = IPAddressUtil.toScopedAddress(remote);
576
}
577
boolean preferIPv6 = isIPv6Available() &&
578
(family != StandardProtocolFamily.INET);
579
return connect0(preferIPv6, fd, remote, remotePort);
580
}
581
582
static int connect(ProtocolFamily family, FileDescriptor fd, SocketAddress remote)
583
throws IOException
584
{
585
InetSocketAddress isa = (InetSocketAddress) remote;
586
return connect(family, fd, isa.getAddress(), isa.getPort());
587
}
588
589
private static native int connect0(boolean preferIPv6,
590
FileDescriptor fd,
591
InetAddress remote,
592
int remotePort)
593
throws IOException;
594
595
public static native int accept(FileDescriptor fd,
596
FileDescriptor newfd,
597
InetSocketAddress[] isaa)
598
throws IOException;
599
600
public static final int SHUT_RD = 0;
601
public static final int SHUT_WR = 1;
602
public static final int SHUT_RDWR = 2;
603
604
static native void shutdown(FileDescriptor fd, int how) throws IOException;
605
606
private static native int localPort(FileDescriptor fd)
607
throws IOException;
608
609
private static native InetAddress localInetAddress(FileDescriptor fd)
610
throws IOException;
611
612
public static InetSocketAddress localAddress(FileDescriptor fd)
613
throws IOException
614
{
615
return new InetSocketAddress(localInetAddress(fd), localPort(fd));
616
}
617
618
private static native int remotePort(FileDescriptor fd)
619
throws IOException;
620
621
private static native InetAddress remoteInetAddress(FileDescriptor fd)
622
throws IOException;
623
624
static InetSocketAddress remoteAddress(FileDescriptor fd)
625
throws IOException
626
{
627
return new InetSocketAddress(remoteInetAddress(fd), remotePort(fd));
628
}
629
630
private static native int getIntOption0(FileDescriptor fd, boolean mayNeedConversion,
631
int level, int opt)
632
throws IOException;
633
634
private static native void setIntOption0(FileDescriptor fd, boolean mayNeedConversion,
635
int level, int opt, int arg, boolean isIPv6)
636
throws IOException;
637
638
/**
639
* Polls a file descriptor for events.
640
* @param timeout the timeout to wait; 0 to not wait, -1 to wait indefinitely
641
* @return the polled events or 0 if no events are polled
642
*/
643
static native int poll(FileDescriptor fd, int events, long timeout)
644
throws IOException;
645
646
/**
647
* Performs a non-blocking poll of a file descriptor.
648
* @return the polled events or 0 if no events are polled
649
*/
650
static int pollNow(FileDescriptor fd, int events) throws IOException {
651
return poll(fd, events, 0);
652
}
653
654
/**
655
* Polls a connecting socket to test if the connection has been established.
656
*
657
* @apiNote This method is public to allow it be used by code in jdk.sctp.
658
*
659
* @param timeout the timeout to wait; 0 to not wait, -1 to wait indefinitely
660
* @return true if connected
661
*/
662
public static native boolean pollConnect(FileDescriptor fd, long timeout)
663
throws IOException;
664
665
/**
666
* Performs a non-blocking poll of a connecting socket to test if the
667
* connection has been established.
668
*
669
* @return true if connected
670
*/
671
static boolean pollConnectNow(FileDescriptor fd) throws IOException {
672
return pollConnect(fd, 0);
673
}
674
675
/**
676
* Return the number of bytes in the socket input buffer.
677
*/
678
static native int available(FileDescriptor fd) throws IOException;
679
680
/**
681
* Send one byte of urgent data (MSG_OOB) on the socket.
682
*/
683
static native int sendOOB(FileDescriptor fd, byte data) throws IOException;
684
685
/**
686
* Read and discard urgent data (MSG_OOB) on the socket.
687
*/
688
static native boolean discardOOB(FileDescriptor fd) throws IOException;
689
690
// -- Multicast support --
691
692
/**
693
* Join IPv4 multicast group
694
*/
695
static int join4(FileDescriptor fd, int group, int interf, int source)
696
throws IOException
697
{
698
return joinOrDrop4(true, fd, group, interf, source);
699
}
700
701
/**
702
* Drop membership of IPv4 multicast group
703
*/
704
static void drop4(FileDescriptor fd, int group, int interf, int source)
705
throws IOException
706
{
707
joinOrDrop4(false, fd, group, interf, source);
708
}
709
710
private static native int joinOrDrop4(boolean join, FileDescriptor fd, int group, int interf, int source)
711
throws IOException;
712
713
/**
714
* Block IPv4 source
715
*/
716
static int block4(FileDescriptor fd, int group, int interf, int source)
717
throws IOException
718
{
719
return blockOrUnblock4(true, fd, group, interf, source);
720
}
721
722
/**
723
* Unblock IPv6 source
724
*/
725
static void unblock4(FileDescriptor fd, int group, int interf, int source)
726
throws IOException
727
{
728
blockOrUnblock4(false, fd, group, interf, source);
729
}
730
731
private static native int blockOrUnblock4(boolean block, FileDescriptor fd, int group,
732
int interf, int source)
733
throws IOException;
734
735
/**
736
* Join IPv6 multicast group
737
*/
738
static int join6(FileDescriptor fd, byte[] group, int index, byte[] source)
739
throws IOException
740
{
741
return joinOrDrop6(true, fd, group, index, source);
742
}
743
744
/**
745
* Drop membership of IPv6 multicast group
746
*/
747
static void drop6(FileDescriptor fd, byte[] group, int index, byte[] source)
748
throws IOException
749
{
750
joinOrDrop6(false, fd, group, index, source);
751
}
752
753
private static native int joinOrDrop6(boolean join, FileDescriptor fd, byte[] group, int index, byte[] source)
754
throws IOException;
755
756
/**
757
* Block IPv6 source
758
*/
759
static int block6(FileDescriptor fd, byte[] group, int index, byte[] source)
760
throws IOException
761
{
762
return blockOrUnblock6(true, fd, group, index, source);
763
}
764
765
/**
766
* Unblock IPv6 source
767
*/
768
static void unblock6(FileDescriptor fd, byte[] group, int index, byte[] source)
769
throws IOException
770
{
771
blockOrUnblock6(false, fd, group, index, source);
772
}
773
774
static native int blockOrUnblock6(boolean block, FileDescriptor fd, byte[] group, int index, byte[] source)
775
throws IOException;
776
777
static native void setInterface4(FileDescriptor fd, int interf) throws IOException;
778
779
static native int getInterface4(FileDescriptor fd) throws IOException;
780
781
static native void setInterface6(FileDescriptor fd, int index) throws IOException;
782
783
static native int getInterface6(FileDescriptor fd) throws IOException;
784
785
private static native void initIDs();
786
787
/**
788
* Event masks for the various poll system calls.
789
* They will be set platform dependent in the static initializer below.
790
*/
791
public static final short POLLIN;
792
public static final short POLLOUT;
793
public static final short POLLERR;
794
public static final short POLLHUP;
795
public static final short POLLNVAL;
796
public static final short POLLCONN;
797
798
static native short pollinValue();
799
static native short polloutValue();
800
static native short pollerrValue();
801
static native short pollhupValue();
802
static native short pollnvalValue();
803
static native short pollconnValue();
804
805
static {
806
IOUtil.load();
807
initIDs();
808
809
POLLIN = pollinValue();
810
POLLOUT = polloutValue();
811
POLLERR = pollerrValue();
812
POLLHUP = pollhupValue();
813
POLLNVAL = pollnvalValue();
814
POLLCONN = pollconnValue();
815
}
816
817
static {
818
int availLevel = isExclusiveBindAvailable();
819
if (availLevel >= 0) {
820
String exclBindProp = GetPropertyAction
821
.privilegedGetProperty("sun.net.useExclusiveBind");
822
if (exclBindProp != null) {
823
exclusiveBind = exclBindProp.isEmpty() ?
824
true : Boolean.parseBoolean(exclBindProp);
825
} else if (availLevel == 1) {
826
exclusiveBind = true;
827
} else {
828
exclusiveBind = false;
829
}
830
} else {
831
exclusiveBind = false;
832
}
833
834
fastLoopback = isFastTcpLoopbackRequested();
835
}
836
}
837
838