Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/src/java.base/unix/native/libnio/ch/InheritedChannel.c
41133 views
1
/*
2
* Copyright (c) 2003, 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 <stdlib.h>
27
#include <sys/types.h>
28
#include <sys/socket.h>
29
#include <unistd.h>
30
#include <fcntl.h>
31
32
#include "jni.h"
33
34
#include "jni.h"
35
#include "jni_util.h"
36
#include "net_util.h"
37
#include "nio_util.h"
38
39
#include "sun_nio_ch_InheritedChannel.h"
40
41
static int toInetFamily(SOCKETADDRESS *sa) {
42
return (sa->sa.sa_family == (ipv6_available() ? AF_INET6 : AF_INET));
43
}
44
45
JNIEXPORT void JNICALL
46
Java_sun_nio_ch_InheritedChannel_initIDs(JNIEnv *env, jclass cla)
47
{
48
/* Initialize InetAddress IDs before later use of NET_XXX functions */
49
initInetAddressIDs(env);
50
}
51
52
JNIEXPORT jobject JNICALL
53
Java_sun_nio_ch_InheritedChannel_inetPeerAddress0(JNIEnv *env, jclass cla, jint fd)
54
{
55
SOCKETADDRESS sa;
56
socklen_t len = sizeof(SOCKETADDRESS);
57
jobject remote_ia = NULL;
58
jint remote_port;
59
60
if (getpeername(fd, &sa.sa, &len) == 0) {
61
if (toInetFamily(&sa)) {
62
remote_ia = NET_SockaddrToInetAddress(env, &sa, (int *)&remote_port);
63
}
64
}
65
66
return remote_ia;
67
}
68
69
JNIEXPORT jbyteArray JNICALL
70
Java_sun_nio_ch_InheritedChannel_unixPeerAddress0(JNIEnv *env, jclass cla, jint fd)
71
{
72
struct sockaddr_un sa;
73
socklen_t len = sizeof(struct sockaddr_un);
74
jobject remote_sa = NULL;
75
76
if (getpeername(fd, (struct sockaddr *)&sa, &len) == 0) {
77
if (sa.sun_family == AF_UNIX) {
78
remote_sa = sockaddrToUnixAddressBytes(env, &sa, len);
79
}
80
}
81
return remote_sa;
82
}
83
84
JNIEXPORT jint JNICALL
85
Java_sun_nio_ch_InheritedChannel_peerPort0(JNIEnv *env, jclass cla, jint fd)
86
{
87
SOCKETADDRESS sa;
88
socklen_t len = sizeof(SOCKETADDRESS);
89
jint remote_port = -1;
90
91
if (getpeername(fd, (struct sockaddr *)&sa.sa, &len) == 0) {
92
if (toInetFamily(&sa)) {
93
NET_SockaddrToInetAddress(env, &sa, (int *)&remote_port);
94
}
95
}
96
97
return remote_port;
98
}
99
100
JNIEXPORT jint JNICALL
101
Java_sun_nio_ch_InheritedChannel_addressFamily(JNIEnv *env, jclass cla, jint fd)
102
{
103
SOCKETADDRESS addr;
104
socklen_t addrlen = sizeof(addr);
105
106
if (getsockname(fd, (struct sockaddr *)&addr, &addrlen) < 0) {
107
return sun_nio_ch_InheritedChannel_AF_UNKNOWN;
108
}
109
if (addr.sa.sa_family == AF_INET) {
110
return sun_nio_ch_InheritedChannel_AF_INET;
111
}
112
if (addr.sa.sa_family == AF_INET6) {
113
return sun_nio_ch_InheritedChannel_AF_INET6;
114
}
115
if (addr.sa.sa_family == AF_UNIX) {
116
return sun_nio_ch_InheritedChannel_AF_UNIX;
117
}
118
return sun_nio_ch_InheritedChannel_AF_UNKNOWN;
119
}
120
121
JNIEXPORT jboolean JNICALL
122
Java_sun_nio_ch_InheritedChannel_isConnected(JNIEnv *env, jclass cla, jint fd)
123
{
124
SOCKETADDRESS addr;
125
socklen_t addrlen = sizeof(addr);
126
127
if (getpeername(fd, (struct sockaddr *)&addr, &addrlen) < 0) {
128
return JNI_FALSE;
129
}
130
return JNI_TRUE;
131
}
132
133
JNIEXPORT jint JNICALL
134
Java_sun_nio_ch_InheritedChannel_soType0(JNIEnv *env, jclass cla, jint fd)
135
{
136
int sotype;
137
socklen_t arglen = sizeof(sotype);
138
if (getsockopt(fd, SOL_SOCKET, SO_TYPE, (void *)&sotype, &arglen) == 0) {
139
if (sotype == SOCK_STREAM)
140
return sun_nio_ch_InheritedChannel_SOCK_STREAM;
141
if (sotype == SOCK_DGRAM)
142
return sun_nio_ch_InheritedChannel_SOCK_DGRAM;
143
}
144
return sun_nio_ch_InheritedChannel_UNKNOWN;
145
}
146
147
JNIEXPORT jint JNICALL
148
Java_sun_nio_ch_InheritedChannel_dup(JNIEnv *env, jclass cla, jint fd)
149
{
150
int newfd = dup(fd);
151
if (newfd < 0) {
152
JNU_ThrowIOExceptionWithLastError(env, "dup failed");
153
}
154
return (jint)newfd;
155
}
156
157
JNIEXPORT void JNICALL
158
Java_sun_nio_ch_InheritedChannel_dup2(JNIEnv *env, jclass cla, jint fd, jint fd2)
159
{
160
if (dup2(fd, fd2) < 0) {
161
JNU_ThrowIOExceptionWithLastError(env, "dup2 failed");
162
}
163
}
164
165
JNIEXPORT jint JNICALL
166
Java_sun_nio_ch_InheritedChannel_open0(JNIEnv *env, jclass cla, jstring path, jint oflag)
167
{
168
const char *str;
169
int oflag_actual;
170
171
/* convert to OS specific value */
172
switch (oflag) {
173
case sun_nio_ch_InheritedChannel_O_RDWR :
174
oflag_actual = O_RDWR;
175
break;
176
case sun_nio_ch_InheritedChannel_O_RDONLY :
177
oflag_actual = O_RDONLY;
178
break;
179
case sun_nio_ch_InheritedChannel_O_WRONLY :
180
oflag_actual = O_WRONLY;
181
break;
182
default :
183
JNU_ThrowInternalError(env, "Unrecognized file mode");
184
return -1;
185
}
186
187
str = JNU_GetStringPlatformChars(env, path, NULL);
188
if (str == NULL) {
189
return (jint)-1;
190
} else {
191
int fd = open(str, oflag_actual);
192
if (fd < 0) {
193
JNU_ThrowIOExceptionWithLastError(env, str);
194
}
195
JNU_ReleaseStringPlatformChars(env, path, str);
196
return (jint)fd;
197
}
198
}
199
200
JNIEXPORT void JNICALL
201
Java_sun_nio_ch_InheritedChannel_close0(JNIEnv *env, jclass cla, jint fd)
202
{
203
if (close(fd) < 0) {
204
JNU_ThrowIOExceptionWithLastError(env, "close failed");
205
}
206
}
207
208