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/UnixDomainSockets.java
41159 views
1
/*
2
* Copyright (c) 2020, 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.BindException;
31
import java.net.NetPermission;
32
import java.net.SocketAddress;
33
import java.net.UnixDomainSocketAddress;
34
import java.nio.channels.UnsupportedAddressTypeException;
35
import java.nio.file.FileSystems;
36
import java.nio.file.InvalidPathException;
37
import java.nio.file.Path;
38
import java.nio.file.spi.FileSystemProvider;
39
import java.security.NoSuchAlgorithmException;
40
import java.security.SecureRandom;
41
import java.util.Random;
42
import sun.nio.fs.AbstractFileSystemProvider;
43
44
class UnixDomainSockets {
45
private UnixDomainSockets() { }
46
47
static final UnixDomainSocketAddress UNNAMED = UnixDomainSocketAddress.of("");
48
49
private static final boolean supported;
50
51
private static final String tempDir = UnixDomainSocketsUtil.getTempDir();
52
53
private static final NetPermission accessUnixDomainSocket =
54
new NetPermission("accessUnixDomainSocket");
55
56
static boolean isSupported() {
57
return supported;
58
}
59
60
static void checkPermission() {
61
@SuppressWarnings("removal")
62
SecurityManager sm = System.getSecurityManager();
63
if (sm != null)
64
sm.checkPermission(accessUnixDomainSocket);
65
}
66
67
static UnixDomainSocketAddress getRevealedLocalAddress(SocketAddress sa) {
68
UnixDomainSocketAddress addr = (UnixDomainSocketAddress) sa;
69
try {
70
checkPermission();
71
// Security check passed
72
} catch (SecurityException e) {
73
// Return unnamed address only if security check fails
74
addr = UNNAMED;
75
}
76
return addr;
77
}
78
79
static UnixDomainSocketAddress localAddress(FileDescriptor fd) throws IOException {
80
String path = new String(localAddress0(fd), UnixDomainSocketsUtil.getCharset());
81
return UnixDomainSocketAddress.of(path);
82
}
83
84
private static native byte[] localAddress0(FileDescriptor fd) throws IOException;
85
86
@SuppressWarnings("removal")
87
static String getRevealedLocalAddressAsString(SocketAddress sa) {
88
return (System.getSecurityManager() != null) ? sa.toString() : "";
89
}
90
91
static UnixDomainSocketAddress checkAddress(SocketAddress sa) {
92
if (sa == null)
93
throw new NullPointerException();
94
if (!(sa instanceof UnixDomainSocketAddress))
95
throw new UnsupportedAddressTypeException();
96
return (UnixDomainSocketAddress) sa;
97
}
98
99
static byte[] getPathBytes(Path path) {
100
FileSystemProvider provider = FileSystems.getDefault().provider();
101
return ((AbstractFileSystemProvider) provider).getSunPathForSocketFile(path);
102
}
103
104
static FileDescriptor socket() throws IOException {
105
return IOUtil.newFD(socket0());
106
}
107
108
static void bind(FileDescriptor fd, Path addr) throws IOException {
109
byte[] path = getPathBytes(addr);
110
if (path.length == 0) {
111
throw new BindException("Server socket cannot bind to unnamed address");
112
}
113
bind0(fd, path);
114
}
115
116
private static Random getRandom() {
117
try {
118
return SecureRandom.getInstance("NativePRNGNonBlocking");
119
} catch (NoSuchAlgorithmException e) {
120
return new SecureRandom(); // This should not fail
121
}
122
}
123
124
private static final Random random = getRandom();
125
126
/**
127
* Return a possible temporary name to bind to, which is different for each call
128
* Name is of the form <temp dir>/socket_<random>
129
*/
130
static UnixDomainSocketAddress generateTempName() throws IOException {
131
String dir = UnixDomainSockets.tempDir;
132
if (dir == null)
133
throw new BindException("Could not locate temporary directory for sockets");
134
int rnd = random.nextInt(Integer.MAX_VALUE);
135
try {
136
Path path = Path.of(dir, "socket_" + rnd);
137
return UnixDomainSocketAddress.of(path);
138
} catch (InvalidPathException e) {
139
throw new BindException("Invalid temporary directory");
140
}
141
}
142
143
static int connect(FileDescriptor fd, SocketAddress sa) throws IOException {
144
return UnixDomainSockets.connect(fd, ((UnixDomainSocketAddress) sa).getPath());
145
}
146
147
static int connect(FileDescriptor fd, Path path) throws IOException {
148
return connect0(fd, getPathBytes(path));
149
}
150
151
static int accept(FileDescriptor fd, FileDescriptor newfd, String[] paths)
152
throws IOException
153
{
154
Object[] array = new Object[1];
155
int n = accept0(fd, newfd, array);
156
if (n > 0) {
157
byte[] bytes = (byte[]) array[0];
158
paths[0] = new String(bytes, UnixDomainSocketsUtil.getCharset());
159
}
160
return n;
161
}
162
163
private static native boolean socketSupported();
164
165
private static native int socket0() throws IOException;
166
167
private static native void bind0(FileDescriptor fd, byte[] path)
168
throws IOException;
169
170
private static native int connect0(FileDescriptor fd, byte[] path)
171
throws IOException;
172
173
private static native int accept0(FileDescriptor fd, FileDescriptor newfd, Object[] array)
174
throws IOException;
175
176
static {
177
// Load all required native libs
178
IOUtil.load();
179
supported = socketSupported();
180
}
181
}
182
183