author | michaelm |
Thu, 14 Nov 2019 15:01:49 +0000 | |
branch | unixdomainchannels |
changeset 59082 | 5e250ee9259e |
parent 59018 | 6d621f34e24a |
permissions | -rw-r--r-- |
2 | 1 |
/* |
58295
7973073dd048
8231187: SelectorProvider.inheritedChannel() returns TCP socket channel for Unix domain socket
michaelm
parents:
47216
diff
changeset
|
2 |
* Copyright (c) 2003, 2019, Oracle and/or its affiliates. All rights reserved. |
2 | 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 |
|
5506 | 7 |
* published by the Free Software Foundation. Oracle designates this |
2 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
2 | 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 |
* |
|
5506 | 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. |
|
2 | 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 |
||
38 |
#include "sun_nio_ch_InheritedChannel.h" |
|
59007 | 39 |
#include "java_nio_channels_UnixDomainSocketAddress.h" |
2 | 40 |
|
58988 | 41 |
static int matchFamilyInet(SOCKETADDRESS *sa) { |
43889
212e765112d4
8174834: nio (ch): Remove #ifdef AF_INET6 guards in native coding
clanger
parents:
43100
diff
changeset
|
42 |
return (sa->sa.sa_family == (ipv6_available() ? AF_INET6 : AF_INET)); |
2 | 43 |
} |
44 |
||
26207
a02f6165d5be
8055955: (ch) Remove unnecessary initialization of InetAddress from FileChannel
chegar
parents:
25859
diff
changeset
|
45 |
JNIEXPORT void JNICALL |
a02f6165d5be
8055955: (ch) Remove unnecessary initialization of InetAddress from FileChannel
chegar
parents:
25859
diff
changeset
|
46 |
Java_sun_nio_ch_InheritedChannel_initIDs(JNIEnv *env, jclass cla) |
a02f6165d5be
8055955: (ch) Remove unnecessary initialization of InetAddress from FileChannel
chegar
parents:
25859
diff
changeset
|
47 |
{ |
a02f6165d5be
8055955: (ch) Remove unnecessary initialization of InetAddress from FileChannel
chegar
parents:
25859
diff
changeset
|
48 |
/* Initialize InetAddress IDs before later use of NET_XXX functions */ |
a02f6165d5be
8055955: (ch) Remove unnecessary initialization of InetAddress from FileChannel
chegar
parents:
25859
diff
changeset
|
49 |
initInetAddressIDs(env); |
59007 | 50 |
/* Same for UnixDomainSocketAddress */ |
51 |
Java_java_nio_channels_UnixDomainSocketAddress_init(env, NULL); |
|
26207
a02f6165d5be
8055955: (ch) Remove unnecessary initialization of InetAddress from FileChannel
chegar
parents:
25859
diff
changeset
|
52 |
} |
a02f6165d5be
8055955: (ch) Remove unnecessary initialization of InetAddress from FileChannel
chegar
parents:
25859
diff
changeset
|
53 |
|
2 | 54 |
JNIEXPORT jobject JNICALL |
58988 | 55 |
Java_sun_nio_ch_InheritedChannel_peerAddressInet(JNIEnv *env, jclass cla, jint fd) |
2 | 56 |
{ |
41380
c27cf95dd7e6
8167295: Further cleanup to the native parts of libnet/libnio
clanger
parents:
26207
diff
changeset
|
57 |
SOCKETADDRESS sa; |
c27cf95dd7e6
8167295: Further cleanup to the native parts of libnet/libnio
clanger
parents:
26207
diff
changeset
|
58 |
socklen_t len = sizeof(SOCKETADDRESS); |
2 | 59 |
jobject remote_ia = NULL; |
60 |
jint remote_port; |
|
61 |
||
41380
c27cf95dd7e6
8167295: Further cleanup to the native parts of libnet/libnio
clanger
parents:
26207
diff
changeset
|
62 |
if (getpeername(fd, &sa.sa, &len) == 0) { |
58988 | 63 |
if (matchFamilyInet(&sa)) { |
43100 | 64 |
remote_ia = NET_SockaddrToInetAddress(env, &sa, (int *)&remote_port); |
2 | 65 |
} |
66 |
} |
|
67 |
||
68 |
return remote_ia; |
|
69 |
} |
|
70 |
||
58988 | 71 |
JNIEXPORT jobject JNICALL |
72 |
Java_sun_nio_ch_InheritedChannel_peerAddressUnix(JNIEnv *env, jclass cla, jint fd) |
|
73 |
{ |
|
59018
6d621f34e24a
moved unix socket address out of SOCKETADDRESS structure
michaelm
parents:
59007
diff
changeset
|
74 |
struct sockaddr_un sa; |
6d621f34e24a
moved unix socket address out of SOCKETADDRESS structure
michaelm
parents:
59007
diff
changeset
|
75 |
socklen_t len = sizeof(struct sockaddr_un); |
58988 | 76 |
jobject remote_sa = NULL; |
77 |
||
59018
6d621f34e24a
moved unix socket address out of SOCKETADDRESS structure
michaelm
parents:
59007
diff
changeset
|
78 |
if (getpeername(fd, (struct sockaddr *)&sa, &len) == 0) { |
6d621f34e24a
moved unix socket address out of SOCKETADDRESS structure
michaelm
parents:
59007
diff
changeset
|
79 |
if (sa.sun_family == AF_UNIX) { |
59082
5e250ee9259e
unixdomainchannels: fix some sockaddr_un address handling
michaelm
parents:
59018
diff
changeset
|
80 |
remote_sa = NET_SockaddrToUnixAddress(env, &sa, len); |
59003 | 81 |
} |
58988 | 82 |
} |
83 |
return remote_sa; |
|
84 |
} |
|
85 |
||
2 | 86 |
JNIEXPORT jint JNICALL |
87 |
Java_sun_nio_ch_InheritedChannel_peerPort0(JNIEnv *env, jclass cla, jint fd) |
|
88 |
{ |
|
41380
c27cf95dd7e6
8167295: Further cleanup to the native parts of libnet/libnio
clanger
parents:
26207
diff
changeset
|
89 |
SOCKETADDRESS sa; |
c27cf95dd7e6
8167295: Further cleanup to the native parts of libnet/libnio
clanger
parents:
26207
diff
changeset
|
90 |
socklen_t len = sizeof(SOCKETADDRESS); |
2 | 91 |
jint remote_port = -1; |
92 |
||
59018
6d621f34e24a
moved unix socket address out of SOCKETADDRESS structure
michaelm
parents:
59007
diff
changeset
|
93 |
if (getpeername(fd, (struct sockaddr *)&sa.sa, &len) == 0) { |
58988 | 94 |
if (matchFamilyInet(&sa)) { |
43100 | 95 |
NET_SockaddrToInetAddress(env, &sa, (int *)&remote_port); |
59003 | 96 |
} |
2 | 97 |
} |
98 |
||
99 |
return remote_port; |
|
100 |
} |
|
101 |
||
102 |
JNIEXPORT jint JNICALL |
|
58295
7973073dd048
8231187: SelectorProvider.inheritedChannel() returns TCP socket channel for Unix domain socket
michaelm
parents:
47216
diff
changeset
|
103 |
Java_sun_nio_ch_InheritedChannel_addressFamily(JNIEnv *env, jclass cla, jint fd) |
7973073dd048
8231187: SelectorProvider.inheritedChannel() returns TCP socket channel for Unix domain socket
michaelm
parents:
47216
diff
changeset
|
104 |
{ |
7973073dd048
8231187: SelectorProvider.inheritedChannel() returns TCP socket channel for Unix domain socket
michaelm
parents:
47216
diff
changeset
|
105 |
SOCKETADDRESS addr; |
7973073dd048
8231187: SelectorProvider.inheritedChannel() returns TCP socket channel for Unix domain socket
michaelm
parents:
47216
diff
changeset
|
106 |
socklen_t addrlen = sizeof(addr); |
7973073dd048
8231187: SelectorProvider.inheritedChannel() returns TCP socket channel for Unix domain socket
michaelm
parents:
47216
diff
changeset
|
107 |
|
7973073dd048
8231187: SelectorProvider.inheritedChannel() returns TCP socket channel for Unix domain socket
michaelm
parents:
47216
diff
changeset
|
108 |
if (getsockname(fd, (struct sockaddr *)&addr, &addrlen) < 0) { |
7973073dd048
8231187: SelectorProvider.inheritedChannel() returns TCP socket channel for Unix domain socket
michaelm
parents:
47216
diff
changeset
|
109 |
return sun_nio_ch_InheritedChannel_AF_UNKNOWN; |
7973073dd048
8231187: SelectorProvider.inheritedChannel() returns TCP socket channel for Unix domain socket
michaelm
parents:
47216
diff
changeset
|
110 |
} |
7973073dd048
8231187: SelectorProvider.inheritedChannel() returns TCP socket channel for Unix domain socket
michaelm
parents:
47216
diff
changeset
|
111 |
if (addr.sa.sa_family == AF_INET) { |
7973073dd048
8231187: SelectorProvider.inheritedChannel() returns TCP socket channel for Unix domain socket
michaelm
parents:
47216
diff
changeset
|
112 |
return sun_nio_ch_InheritedChannel_AF_INET; |
7973073dd048
8231187: SelectorProvider.inheritedChannel() returns TCP socket channel for Unix domain socket
michaelm
parents:
47216
diff
changeset
|
113 |
} |
7973073dd048
8231187: SelectorProvider.inheritedChannel() returns TCP socket channel for Unix domain socket
michaelm
parents:
47216
diff
changeset
|
114 |
if (addr.sa.sa_family == AF_INET6) { |
7973073dd048
8231187: SelectorProvider.inheritedChannel() returns TCP socket channel for Unix domain socket
michaelm
parents:
47216
diff
changeset
|
115 |
return sun_nio_ch_InheritedChannel_AF_INET6; |
7973073dd048
8231187: SelectorProvider.inheritedChannel() returns TCP socket channel for Unix domain socket
michaelm
parents:
47216
diff
changeset
|
116 |
} |
7973073dd048
8231187: SelectorProvider.inheritedChannel() returns TCP socket channel for Unix domain socket
michaelm
parents:
47216
diff
changeset
|
117 |
if (addr.sa.sa_family == AF_UNIX) { |
7973073dd048
8231187: SelectorProvider.inheritedChannel() returns TCP socket channel for Unix domain socket
michaelm
parents:
47216
diff
changeset
|
118 |
return sun_nio_ch_InheritedChannel_AF_UNIX; |
7973073dd048
8231187: SelectorProvider.inheritedChannel() returns TCP socket channel for Unix domain socket
michaelm
parents:
47216
diff
changeset
|
119 |
} |
7973073dd048
8231187: SelectorProvider.inheritedChannel() returns TCP socket channel for Unix domain socket
michaelm
parents:
47216
diff
changeset
|
120 |
return sun_nio_ch_InheritedChannel_AF_UNKNOWN; |
7973073dd048
8231187: SelectorProvider.inheritedChannel() returns TCP socket channel for Unix domain socket
michaelm
parents:
47216
diff
changeset
|
121 |
} |
7973073dd048
8231187: SelectorProvider.inheritedChannel() returns TCP socket channel for Unix domain socket
michaelm
parents:
47216
diff
changeset
|
122 |
|
7973073dd048
8231187: SelectorProvider.inheritedChannel() returns TCP socket channel for Unix domain socket
michaelm
parents:
47216
diff
changeset
|
123 |
JNIEXPORT jboolean JNICALL |
7973073dd048
8231187: SelectorProvider.inheritedChannel() returns TCP socket channel for Unix domain socket
michaelm
parents:
47216
diff
changeset
|
124 |
Java_sun_nio_ch_InheritedChannel_isConnected(JNIEnv *env, jclass cla, jint fd) |
7973073dd048
8231187: SelectorProvider.inheritedChannel() returns TCP socket channel for Unix domain socket
michaelm
parents:
47216
diff
changeset
|
125 |
{ |
7973073dd048
8231187: SelectorProvider.inheritedChannel() returns TCP socket channel for Unix domain socket
michaelm
parents:
47216
diff
changeset
|
126 |
SOCKETADDRESS addr; |
7973073dd048
8231187: SelectorProvider.inheritedChannel() returns TCP socket channel for Unix domain socket
michaelm
parents:
47216
diff
changeset
|
127 |
socklen_t addrlen = sizeof(addr); |
7973073dd048
8231187: SelectorProvider.inheritedChannel() returns TCP socket channel for Unix domain socket
michaelm
parents:
47216
diff
changeset
|
128 |
|
7973073dd048
8231187: SelectorProvider.inheritedChannel() returns TCP socket channel for Unix domain socket
michaelm
parents:
47216
diff
changeset
|
129 |
if (getpeername(fd, (struct sockaddr *)&addr, &addrlen) < 0) { |
7973073dd048
8231187: SelectorProvider.inheritedChannel() returns TCP socket channel for Unix domain socket
michaelm
parents:
47216
diff
changeset
|
130 |
return JNI_FALSE; |
7973073dd048
8231187: SelectorProvider.inheritedChannel() returns TCP socket channel for Unix domain socket
michaelm
parents:
47216
diff
changeset
|
131 |
} |
7973073dd048
8231187: SelectorProvider.inheritedChannel() returns TCP socket channel for Unix domain socket
michaelm
parents:
47216
diff
changeset
|
132 |
return JNI_TRUE; |
7973073dd048
8231187: SelectorProvider.inheritedChannel() returns TCP socket channel for Unix domain socket
michaelm
parents:
47216
diff
changeset
|
133 |
} |
7973073dd048
8231187: SelectorProvider.inheritedChannel() returns TCP socket channel for Unix domain socket
michaelm
parents:
47216
diff
changeset
|
134 |
|
7973073dd048
8231187: SelectorProvider.inheritedChannel() returns TCP socket channel for Unix domain socket
michaelm
parents:
47216
diff
changeset
|
135 |
JNIEXPORT jint JNICALL |
2 | 136 |
Java_sun_nio_ch_InheritedChannel_soType0(JNIEnv *env, jclass cla, jint fd) |
137 |
{ |
|
895 | 138 |
int sotype; |
43889
212e765112d4
8174834: nio (ch): Remove #ifdef AF_INET6 guards in native coding
clanger
parents:
43100
diff
changeset
|
139 |
socklen_t arglen = sizeof(sotype); |
2 | 140 |
if (getsockopt(fd, SOL_SOCKET, SO_TYPE, (void *)&sotype, &arglen) == 0) { |
141 |
if (sotype == SOCK_STREAM) |
|
142 |
return sun_nio_ch_InheritedChannel_SOCK_STREAM; |
|
143 |
if (sotype == SOCK_DGRAM) |
|
144 |
return sun_nio_ch_InheritedChannel_SOCK_DGRAM; |
|
145 |
} |
|
146 |
return sun_nio_ch_InheritedChannel_UNKNOWN; |
|
147 |
} |
|
148 |
||
149 |
JNIEXPORT jint JNICALL |
|
150 |
Java_sun_nio_ch_InheritedChannel_dup(JNIEnv *env, jclass cla, jint fd) |
|
151 |
{ |
|
152 |
int newfd = dup(fd); |
|
153 |
if (newfd < 0) { |
|
154 |
JNU_ThrowIOExceptionWithLastError(env, "dup failed"); |
|
155 |
} |
|
156 |
return (jint)newfd; |
|
157 |
} |
|
158 |
||
159 |
JNIEXPORT void JNICALL |
|
160 |
Java_sun_nio_ch_InheritedChannel_dup2(JNIEnv *env, jclass cla, jint fd, jint fd2) |
|
161 |
{ |
|
162 |
if (dup2(fd, fd2) < 0) { |
|
163 |
JNU_ThrowIOExceptionWithLastError(env, "dup2 failed"); |
|
164 |
} |
|
165 |
} |
|
166 |
||
167 |
JNIEXPORT jint JNICALL |
|
168 |
Java_sun_nio_ch_InheritedChannel_open0(JNIEnv *env, jclass cla, jstring path, jint oflag) |
|
169 |
{ |
|
43889
212e765112d4
8174834: nio (ch): Remove #ifdef AF_INET6 guards in native coding
clanger
parents:
43100
diff
changeset
|
170 |
const char *str; |
2 | 171 |
int oflag_actual; |
172 |
||
173 |
/* convert to OS specific value */ |
|
174 |
switch (oflag) { |
|
175 |
case sun_nio_ch_InheritedChannel_O_RDWR : |
|
176 |
oflag_actual = O_RDWR; |
|
177 |
break; |
|
178 |
case sun_nio_ch_InheritedChannel_O_RDONLY : |
|
179 |
oflag_actual = O_RDONLY; |
|
180 |
break; |
|
181 |
case sun_nio_ch_InheritedChannel_O_WRONLY : |
|
182 |
oflag_actual = O_WRONLY; |
|
183 |
break; |
|
184 |
default : |
|
185 |
JNU_ThrowInternalError(env, "Unrecognized file mode"); |
|
186 |
return -1; |
|
187 |
} |
|
188 |
||
189 |
str = JNU_GetStringPlatformChars(env, path, NULL); |
|
190 |
if (str == NULL) { |
|
191 |
return (jint)-1; |
|
192 |
} else { |
|
193 |
int fd = open(str, oflag_actual); |
|
194 |
if (fd < 0) { |
|
195 |
JNU_ThrowIOExceptionWithLastError(env, str); |
|
196 |
} |
|
197 |
JNU_ReleaseStringPlatformChars(env, path, str); |
|
198 |
return (jint)fd; |
|
199 |
} |
|
200 |
} |
|
201 |
||
202 |
JNIEXPORT void JNICALL |
|
203 |
Java_sun_nio_ch_InheritedChannel_close0(JNIEnv *env, jclass cla, jint fd) |
|
204 |
{ |
|
205 |
if (close(fd) < 0) { |
|
206 |
JNU_ThrowIOExceptionWithLastError(env, "close failed"); |
|
207 |
} |
|
208 |
} |