Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/jdk17u
Path: blob/master/src/java.base/windows/native/libnio/ch/UnixDomainSockets.c
67760 views
1
/*
2
* Copyright (c) 2020, 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
#include <windows.h>
27
#include <winsock2.h>
28
29
#include "jni.h"
30
#include "jni_util.h"
31
#include "jvm.h"
32
#include "jlong.h"
33
#include "nio.h"
34
#include "nio_util.h"
35
#include "net_util.h"
36
37
#include "java_net_InetAddress.h"
38
#include "sun_nio_ch_Net.h"
39
#include "sun_nio_ch_PollArrayWrapper.h"
40
41
/* The winsock provider ID of the Microsoft AF_UNIX implementation */
42
static GUID MS_PROVIDER_ID = {0xA00943D9,0x9C2E,0x4633,{0x9B,0x59,0,0x57,0xA3,0x16,0x09,0x94}};
43
44
jbyteArray sockaddrToUnixAddressBytes(JNIEnv *env, struct sockaddr_un *sa, socklen_t len)
45
{
46
if (sa->sun_family == AF_UNIX) {
47
int namelen = (int)strlen(sa->sun_path);
48
jbyteArray name = (*env)->NewByteArray(env, namelen);
49
if (name != NULL) {
50
(*env)->SetByteArrayRegion(env, name, 0, namelen, (jbyte*)sa->sun_path);
51
if ((*env)->ExceptionOccurred(env)) {
52
return NULL;
53
}
54
}
55
return name;
56
}
57
return NULL;
58
}
59
60
jint unixSocketAddressToSockaddr(JNIEnv *env, jbyteArray addr, struct sockaddr_un *sa, int *len)
61
{
62
memset(sa, 0, sizeof(struct sockaddr_un));
63
sa->sun_family = AF_UNIX;
64
if (addr == 0L) {
65
/* Do explicit bind on Windows */
66
*len = (int)(offsetof(struct sockaddr_un, sun_path));
67
return 0;
68
}
69
int ret;
70
jboolean isCopy;
71
char *pname = (*env)->GetByteArrayElements(env, addr, &isCopy);
72
if (pname == NULL) {
73
JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException", "Unix domain path not present");
74
return -1;
75
}
76
77
size_t name_len = (size_t)(*env)->GetArrayLength(env, addr);
78
if (name_len > MAX_UNIX_DOMAIN_PATH_LEN) {
79
JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException", "Unix domain path too long");
80
ret = -1;
81
} else {
82
strncpy(sa->sun_path, pname, name_len);
83
*len = (int)(offsetof(struct sockaddr_un, sun_path) + name_len);
84
ret = 0;
85
}
86
(*env)->ReleaseByteArrayElements(env, addr, pname, JNI_ABORT);
87
return ret;
88
}
89
90
static int cmpGuid(GUID *g1, GUID *g2) {
91
if (g1->Data1 != g2->Data1)
92
return JNI_FALSE;
93
if (g1->Data2 != g2->Data2)
94
return JNI_FALSE;
95
if (g1->Data3 != g2->Data3)
96
return JNI_FALSE;
97
for (int i=0; i<8; i++) {
98
if (g1->Data4[i] != g2->Data4[i])
99
return JNI_FALSE;
100
}
101
return JNI_TRUE;
102
}
103
104
static WSAPROTOCOL_INFOW provider;
105
106
JNIEXPORT jboolean JNICALL
107
Java_sun_nio_ch_UnixDomainSockets_init(JNIEnv *env, jclass cl)
108
{
109
WSAPROTOCOL_INFOW info[5]; // if not large enough, a buffer is malloc'd
110
LPWSAPROTOCOL_INFOW infoPtr = &info[0];
111
DWORD len = sizeof(info);
112
jboolean found = JNI_FALSE;
113
114
/*
115
* First locate the Microsoft AF_UNIX Winsock provider
116
*/
117
int result = WSAEnumProtocolsW(0, infoPtr, &len);
118
if (result == SOCKET_ERROR) {
119
if (GetLastError() == WSAENOBUFS) {
120
infoPtr = (LPWSAPROTOCOL_INFOW)malloc(len);
121
result = WSAEnumProtocolsW(0, infoPtr, &len);
122
if (result == SOCKET_ERROR) {
123
free(infoPtr);
124
return JNI_FALSE;
125
}
126
} else {
127
return JNI_FALSE;
128
}
129
}
130
for (int i=0; i<result; i++) {
131
if (infoPtr[i].iAddressFamily == AF_UNIX) {
132
GUID g = infoPtr[i].ProviderId;
133
if (cmpGuid(&g, &MS_PROVIDER_ID)) {
134
found = JNI_TRUE;
135
provider = infoPtr[i];
136
break;
137
}
138
}
139
}
140
if (infoPtr != &info[0]) {
141
free(infoPtr);
142
}
143
/*
144
* check we can create a socket
145
*/
146
if (found) {
147
SOCKET s = WSASocketW(PF_UNIX, SOCK_STREAM, 0, &provider, 0, WSA_FLAG_OVERLAPPED);
148
if (s == INVALID_SOCKET) {
149
return JNI_FALSE;
150
}
151
closesocket(s);
152
}
153
return found;
154
}
155
156
JNIEXPORT jint JNICALL
157
Java_sun_nio_ch_UnixDomainSockets_socket0(JNIEnv *env, jclass cl)
158
{
159
SOCKET s = WSASocketW(PF_UNIX, SOCK_STREAM, 0, &provider, 0, WSA_FLAG_OVERLAPPED);
160
if (s == INVALID_SOCKET) {
161
return handleSocketError(env, WSAGetLastError());
162
}
163
SetHandleInformation((HANDLE)s, HANDLE_FLAG_INHERIT, 0);
164
return (int)s;
165
}
166
167
/**
168
* Windows does not support auto bind. So, the windows version of unixSocketAddressToSockaddr
169
* looks out for a null 'uaddr' and handles it specially
170
*/
171
JNIEXPORT void JNICALL
172
Java_sun_nio_ch_UnixDomainSockets_bind0(JNIEnv *env, jclass clazz, jobject fdo, jbyteArray addr)
173
{
174
struct sockaddr_un sa;
175
int sa_len = 0;
176
int rv = 0;
177
178
if (unixSocketAddressToSockaddr(env, addr, &sa, &sa_len) != 0)
179
return;
180
181
rv = bind(fdval(env, fdo), (struct sockaddr *)&sa, sa_len);
182
if (rv == SOCKET_ERROR) {
183
int err = WSAGetLastError();
184
NET_ThrowNew(env, err, "bind");
185
}
186
}
187
188
JNIEXPORT jint JNICALL
189
Java_sun_nio_ch_UnixDomainSockets_connect0(JNIEnv *env, jclass clazz, jobject fdo, jbyteArray addr)
190
{
191
struct sockaddr_un sa;
192
int sa_len = 0;
193
int rv;
194
195
if (unixSocketAddressToSockaddr(env, addr, &sa, &sa_len) != 0) {
196
return IOS_THROWN;
197
}
198
199
rv = connect(fdval(env, fdo), (const struct sockaddr *)&sa, sa_len);
200
if (rv != 0) {
201
int err = WSAGetLastError();
202
if (err == WSAEINPROGRESS || err == WSAEWOULDBLOCK) {
203
return IOS_UNAVAILABLE;
204
}
205
NET_ThrowNew(env, err, "connect");
206
return IOS_THROWN;
207
}
208
return 1;
209
}
210
211
JNIEXPORT jint JNICALL
212
Java_sun_nio_ch_UnixDomainSockets_accept0(JNIEnv *env, jclass clazz, jobject fdo, jobject newfdo,
213
jobjectArray array)
214
{
215
jint fd = fdval(env, fdo);
216
jint newfd;
217
struct sockaddr_un sa;
218
socklen_t sa_len = sizeof(sa);
219
jbyteArray address;
220
221
memset((char *)&sa, 0, sizeof(sa));
222
newfd = (jint) accept(fd, (struct sockaddr *)&sa, &sa_len);
223
if (newfd == INVALID_SOCKET) {
224
int theErr = (jint)WSAGetLastError();
225
if (theErr == WSAEWOULDBLOCK) {
226
return IOS_UNAVAILABLE;
227
}
228
JNU_ThrowIOExceptionWithLastError(env, "Accept failed");
229
return IOS_THROWN;
230
}
231
232
SetHandleInformation((HANDLE)(UINT_PTR)newfd, HANDLE_FLAG_INHERIT, 0);
233
setfdval(env, newfdo, newfd);
234
235
address = sockaddrToUnixAddressBytes(env, &sa, sa_len);
236
CHECK_NULL_RETURN(address, IOS_THROWN);
237
(*env)->SetObjectArrayElement(env, array, 0, address);
238
239
return 1;
240
}
241
242
JNIEXPORT jbyteArray JNICALL
243
Java_sun_nio_ch_UnixDomainSockets_localAddress0(JNIEnv *env, jclass clazz, jobject fdo)
244
{
245
struct sockaddr_un sa;
246
int sa_len = sizeof(sa);
247
248
if (getsockname(fdval(env, fdo), (struct sockaddr *)&sa, &sa_len) == SOCKET_ERROR) {
249
JNU_ThrowIOExceptionWithLastError(env, "getsockname");
250
return NULL;
251
}
252
return sockaddrToUnixAddressBytes(env, &sa, sa_len);
253
}
254
255
256