author | vtewari |
Thu, 18 Jan 2018 13:55:26 +0530 | |
changeset 51151 | d6b131d2bc8b |
parent 47216 | 71c04702a3d5 |
child 51153 | 78f16a9f7563 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
51151 | 2 |
* Copyright (c) 1997, 2018, 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 |
#include <errno.h> |
|
26 |
#include <stdlib.h> |
|
27 |
#include <string.h> |
|
41771
18c9669e76ca
8167481: cleanup of headers and includes for native libnet
clanger
parents:
41380
diff
changeset
|
28 |
#include <sys/ioctl.h> |
2 | 29 |
|
41771
18c9669e76ca
8167481: cleanup of headers and includes for native libnet
clanger
parents:
41380
diff
changeset
|
30 |
#if defined(__solaris__) |
18c9669e76ca
8167481: cleanup of headers and includes for native libnet
clanger
parents:
41380
diff
changeset
|
31 |
#include <sys/filio.h> |
18c9669e76ca
8167481: cleanup of headers and includes for native libnet
clanger
parents:
41380
diff
changeset
|
32 |
#endif |
32503
1478c9130534
8072466: Deadlock when initializing MulticastSocket and DatagramSocket
igerasim
parents:
30963
diff
changeset
|
33 |
|
41771
18c9669e76ca
8167481: cleanup of headers and includes for native libnet
clanger
parents:
41380
diff
changeset
|
34 |
#include "net_util.h" |
18c9669e76ca
8167481: cleanup of headers and includes for native libnet
clanger
parents:
41380
diff
changeset
|
35 |
|
18c9669e76ca
8167481: cleanup of headers and includes for native libnet
clanger
parents:
41380
diff
changeset
|
36 |
#include "java_net_PlainDatagramSocketImpl.h" |
18c9669e76ca
8167481: cleanup of headers and includes for native libnet
clanger
parents:
41380
diff
changeset
|
37 |
#include "java_net_InetAddress.h" |
18c9669e76ca
8167481: cleanup of headers and includes for native libnet
clanger
parents:
41380
diff
changeset
|
38 |
#include "java_net_NetworkInterface.h" |
18c9669e76ca
8167481: cleanup of headers and includes for native libnet
clanger
parents:
41380
diff
changeset
|
39 |
#include "java_net_SocketOptions.h" |
18c9669e76ca
8167481: cleanup of headers and includes for native libnet
clanger
parents:
41380
diff
changeset
|
40 |
|
2 | 41 |
#ifdef __linux__ |
42 |
#define IPV6_MULTICAST_IF 17 |
|
43 |
#ifndef SO_BSDCOMPAT |
|
44 |
#define SO_BSDCOMPAT 14 |
|
45 |
#endif |
|
18534
2d131cf5db63
8014499: MulticastSocket should enable IP_MULTICAST_ALL (lnx)
chegar
parents:
16870
diff
changeset
|
46 |
/** |
2d131cf5db63
8014499: MulticastSocket should enable IP_MULTICAST_ALL (lnx)
chegar
parents:
16870
diff
changeset
|
47 |
* IP_MULTICAST_ALL has been supported since kernel version 2.6.31 |
2d131cf5db63
8014499: MulticastSocket should enable IP_MULTICAST_ALL (lnx)
chegar
parents:
16870
diff
changeset
|
48 |
* but we may be building on a machine that is older than that. |
2d131cf5db63
8014499: MulticastSocket should enable IP_MULTICAST_ALL (lnx)
chegar
parents:
16870
diff
changeset
|
49 |
*/ |
2d131cf5db63
8014499: MulticastSocket should enable IP_MULTICAST_ALL (lnx)
chegar
parents:
16870
diff
changeset
|
50 |
#ifndef IP_MULTICAST_ALL |
2d131cf5db63
8014499: MulticastSocket should enable IP_MULTICAST_ALL (lnx)
chegar
parents:
16870
diff
changeset
|
51 |
#define IP_MULTICAST_ALL 49 |
2 | 52 |
#endif |
18540
1f637992378a
8017271: Crash may occur in java.net.DualStackPlainSocketImpl::initIDs due to unchecked values returned from JNI functions
chegar
parents:
18534
diff
changeset
|
53 |
#endif // __linux__ |
2 | 54 |
|
41771
18c9669e76ca
8167481: cleanup of headers and includes for native libnet
clanger
parents:
41380
diff
changeset
|
55 |
#ifdef __solaris__ |
18c9669e76ca
8167481: cleanup of headers and includes for native libnet
clanger
parents:
41380
diff
changeset
|
56 |
#ifndef BSD_COMP |
18c9669e76ca
8167481: cleanup of headers and includes for native libnet
clanger
parents:
41380
diff
changeset
|
57 |
#define BSD_COMP |
18c9669e76ca
8167481: cleanup of headers and includes for native libnet
clanger
parents:
41380
diff
changeset
|
58 |
#endif |
18c9669e76ca
8167481: cleanup of headers and includes for native libnet
clanger
parents:
41380
diff
changeset
|
59 |
#endif |
32503
1478c9130534
8072466: Deadlock when initializing MulticastSocket and DatagramSocket
igerasim
parents:
30963
diff
changeset
|
60 |
|
2 | 61 |
#ifndef IPTOS_TOS_MASK |
62 |
#define IPTOS_TOS_MASK 0x1e |
|
63 |
#endif |
|
64 |
#ifndef IPTOS_PREC_MASK |
|
65 |
#define IPTOS_PREC_MASK 0xe0 |
|
66 |
#endif |
|
67 |
||
68 |
/************************************************************************ |
|
69 |
* PlainDatagramSocketImpl |
|
70 |
*/ |
|
71 |
||
72 |
static jfieldID IO_fd_fdID; |
|
73 |
||
74 |
static jfieldID pdsi_fdID; |
|
75 |
static jfieldID pdsi_timeoutID; |
|
76 |
static jfieldID pdsi_trafficClassID; |
|
77 |
static jfieldID pdsi_localPortID; |
|
78 |
static jfieldID pdsi_connected; |
|
79 |
static jfieldID pdsi_connectedAddress; |
|
80 |
static jfieldID pdsi_connectedPort; |
|
81 |
||
12047
320a714614e9
7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
9035
diff
changeset
|
82 |
extern void setDefaultScopeID(JNIEnv *env, struct sockaddr *him); |
12551
5be0a162ed89
7144274: [macosx] Default IPv6 multicast interface is not being set when calling MulticastSocket.joinGroup()
khazra
parents:
12047
diff
changeset
|
83 |
extern int getDefaultScopeID(JNIEnv *env); |
12047
320a714614e9
7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
9035
diff
changeset
|
84 |
|
28549 | 85 |
|
2 | 86 |
/* |
87 |
* Returns a java.lang.Integer based on 'i' |
|
88 |
*/ |
|
89 |
static jobject createInteger(JNIEnv *env, int i) { |
|
90 |
static jclass i_class; |
|
91 |
static jmethodID i_ctrID; |
|
92 |
||
93 |
if (i_class == NULL) { |
|
94 |
jclass c = (*env)->FindClass(env, "java/lang/Integer"); |
|
95 |
CHECK_NULL_RETURN(c, NULL); |
|
96 |
i_ctrID = (*env)->GetMethodID(env, c, "<init>", "(I)V"); |
|
97 |
CHECK_NULL_RETURN(i_ctrID, NULL); |
|
98 |
i_class = (*env)->NewGlobalRef(env, c); |
|
99 |
CHECK_NULL_RETURN(i_class, NULL); |
|
100 |
} |
|
101 |
||
43100 | 102 |
return (*env)->NewObject(env, i_class, i_ctrID, i); |
2 | 103 |
} |
104 |
||
105 |
/* |
|
106 |
* Returns a java.lang.Boolean based on 'b' |
|
107 |
*/ |
|
108 |
static jobject createBoolean(JNIEnv *env, int b) { |
|
109 |
static jclass b_class; |
|
110 |
static jmethodID b_ctrID; |
|
111 |
||
112 |
if (b_class == NULL) { |
|
113 |
jclass c = (*env)->FindClass(env, "java/lang/Boolean"); |
|
114 |
CHECK_NULL_RETURN(c, NULL); |
|
115 |
b_ctrID = (*env)->GetMethodID(env, c, "<init>", "(Z)V"); |
|
116 |
CHECK_NULL_RETURN(b_ctrID, NULL); |
|
117 |
b_class = (*env)->NewGlobalRef(env, c); |
|
118 |
CHECK_NULL_RETURN(b_class, NULL); |
|
119 |
} |
|
120 |
||
43100 | 121 |
return (*env)->NewObject(env, b_class, b_ctrID, (jboolean)(b != 0)); |
2 | 122 |
} |
123 |
||
124 |
/* |
|
125 |
* Returns the fd for a PlainDatagramSocketImpl or -1 |
|
126 |
* if closed. |
|
127 |
*/ |
|
128 |
static int getFD(JNIEnv *env, jobject this) { |
|
129 |
jobject fdObj = (*env)->GetObjectField(env, this, pdsi_fdID); |
|
130 |
if (fdObj == NULL) { |
|
131 |
return -1; |
|
132 |
} |
|
133 |
return (*env)->GetIntField(env, fdObj, IO_fd_fdID); |
|
134 |
} |
|
135 |
||
136 |
/* |
|
137 |
* Class: java_net_PlainDatagramSocketImpl |
|
138 |
* Method: init |
|
139 |
* Signature: ()V |
|
140 |
*/ |
|
141 |
JNIEXPORT void JNICALL |
|
142 |
Java_java_net_PlainDatagramSocketImpl_init(JNIEnv *env, jclass cls) { |
|
143 |
||
144 |
pdsi_fdID = (*env)->GetFieldID(env, cls, "fd", |
|
145 |
"Ljava/io/FileDescriptor;"); |
|
146 |
CHECK_NULL(pdsi_fdID); |
|
147 |
pdsi_timeoutID = (*env)->GetFieldID(env, cls, "timeout", "I"); |
|
148 |
CHECK_NULL(pdsi_timeoutID); |
|
149 |
pdsi_trafficClassID = (*env)->GetFieldID(env, cls, "trafficClass", "I"); |
|
150 |
CHECK_NULL(pdsi_trafficClassID); |
|
151 |
pdsi_localPortID = (*env)->GetFieldID(env, cls, "localPort", "I"); |
|
152 |
CHECK_NULL(pdsi_localPortID); |
|
153 |
pdsi_connected = (*env)->GetFieldID(env, cls, "connected", "Z"); |
|
154 |
CHECK_NULL(pdsi_connected); |
|
155 |
pdsi_connectedAddress = (*env)->GetFieldID(env, cls, "connectedAddress", |
|
156 |
"Ljava/net/InetAddress;"); |
|
157 |
CHECK_NULL(pdsi_connectedAddress); |
|
158 |
pdsi_connectedPort = (*env)->GetFieldID(env, cls, "connectedPort", "I"); |
|
159 |
CHECK_NULL(pdsi_connectedPort); |
|
160 |
||
161 |
IO_fd_fdID = NET_GetFileDescriptorID(env); |
|
162 |
CHECK_NULL(IO_fd_fdID); |
|
163 |
||
22646
5fa3669fd35d
8025306: Inet[4|6]Address class and fieldID initialization in networking native code
chegar
parents:
22095
diff
changeset
|
164 |
initInetAddressIDs(env); |
5fa3669fd35d
8025306: Inet[4|6]Address class and fieldID initialization in networking native code
chegar
parents:
22095
diff
changeset
|
165 |
JNU_CHECK_EXCEPTION(env); |
2 | 166 |
Java_java_net_NetworkInterface_init(env, 0); |
167 |
} |
|
168 |
||
169 |
/* |
|
170 |
* Class: java_net_PlainDatagramSocketImpl |
|
171 |
* Method: bind |
|
172 |
* Signature: (ILjava/net/InetAddress;)V |
|
173 |
*/ |
|
174 |
JNIEXPORT void JNICALL |
|
175 |
Java_java_net_PlainDatagramSocketImpl_bind0(JNIEnv *env, jobject this, |
|
43100 | 176 |
jint localport, jobject iaObj) { |
2 | 177 |
/* fdObj is the FileDescriptor field on this */ |
178 |
jobject fdObj = (*env)->GetObjectField(env, this, pdsi_fdID); |
|
179 |
/* fd is an int field on fdObj */ |
|
180 |
int fd; |
|
181 |
int len = 0; |
|
43100 | 182 |
SOCKETADDRESS sa; |
41380
c27cf95dd7e6
8167295: Further cleanup to the native parts of libnet/libnio
clanger
parents:
41214
diff
changeset
|
183 |
socklen_t slen = sizeof(SOCKETADDRESS); |
2 | 184 |
|
185 |
if (IS_NULL(fdObj)) { |
|
186 |
JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException", |
|
187 |
"Socket closed"); |
|
188 |
return; |
|
189 |
} else { |
|
190 |
fd = (*env)->GetIntField(env, fdObj, IO_fd_fdID); |
|
191 |
} |
|
192 |
||
193 |
if (IS_NULL(iaObj)) { |
|
194 |
JNU_ThrowNullPointerException(env, "iaObj is null."); |
|
195 |
return; |
|
196 |
} |
|
197 |
||
198 |
/* bind */ |
|
43100 | 199 |
if (NET_InetAddressToSockaddr(env, iaObj, localport, &sa, &len, |
200 |
JNI_TRUE) != 0) { |
|
2 | 201 |
return; |
202 |
} |
|
43100 | 203 |
setDefaultScopeID(env, &sa.sa); |
2 | 204 |
|
43100 | 205 |
if (NET_Bind(fd, &sa, len) < 0) { |
2 | 206 |
if (errno == EADDRINUSE || errno == EADDRNOTAVAIL || |
207 |
errno == EPERM || errno == EACCES) { |
|
208 |
NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "BindException", |
|
209 |
"Bind failed"); |
|
210 |
} else { |
|
39318
2006d1d41c8b
8158023: SocketExceptions contain too little information sometimes
clanger
parents:
36115
diff
changeset
|
211 |
JNU_ThrowByNameWithMessageAndLastError |
2006d1d41c8b
8158023: SocketExceptions contain too little information sometimes
clanger
parents:
36115
diff
changeset
|
212 |
(env, JNU_JAVANETPKG "SocketException", "Bind failed"); |
2 | 213 |
} |
214 |
return; |
|
215 |
} |
|
216 |
||
13999
f6a2ce6a3e40
7193520: Removed references to Linux kernel version 2.2
chegar
parents:
13574
diff
changeset
|
217 |
/* initialize the local port */ |
2 | 218 |
if (localport == 0) { |
219 |
/* Now that we're a connected socket, let's extract the port number |
|
220 |
* that the system chose for us and store it in the Socket object. |
|
221 |
*/ |
|
43100 | 222 |
if (getsockname(fd, &sa.sa, &slen) == -1) { |
39318
2006d1d41c8b
8158023: SocketExceptions contain too little information sometimes
clanger
parents:
36115
diff
changeset
|
223 |
JNU_ThrowByNameWithMessageAndLastError |
2006d1d41c8b
8158023: SocketExceptions contain too little information sometimes
clanger
parents:
36115
diff
changeset
|
224 |
(env, JNU_JAVANETPKG "SocketException", "Error getting socket name"); |
2 | 225 |
return; |
226 |
} |
|
227 |
||
43100 | 228 |
localport = NET_GetPortFromSockaddr(&sa); |
2 | 229 |
|
230 |
(*env)->SetIntField(env, this, pdsi_localPortID, localport); |
|
231 |
} else { |
|
232 |
(*env)->SetIntField(env, this, pdsi_localPortID, localport); |
|
233 |
} |
|
234 |
} |
|
235 |
||
236 |
/* |
|
237 |
* Class: java_net_PlainDatagramSocketImpl |
|
238 |
* Method: connect0 |
|
239 |
* Signature: (Ljava/net/InetAddress;I)V |
|
240 |
*/ |
|
241 |
JNIEXPORT void JNICALL |
|
242 |
Java_java_net_PlainDatagramSocketImpl_connect0(JNIEnv *env, jobject this, |
|
243 |
jobject address, jint port) { |
|
244 |
/* The object's field */ |
|
245 |
jobject fdObj = (*env)->GetObjectField(env, this, pdsi_fdID); |
|
246 |
/* The fdObj'fd */ |
|
247 |
jint fd; |
|
248 |
/* The packetAddress address, family and port */ |
|
41380
c27cf95dd7e6
8167295: Further cleanup to the native parts of libnet/libnio
clanger
parents:
41214
diff
changeset
|
249 |
SOCKETADDRESS rmtaddr; |
2 | 250 |
int len = 0; |
251 |
||
252 |
if (IS_NULL(fdObj)) { |
|
253 |
JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException", |
|
254 |
"Socket closed"); |
|
255 |
return; |
|
256 |
} |
|
257 |
fd = (*env)->GetIntField(env, fdObj, IO_fd_fdID); |
|
258 |
||
259 |
if (IS_NULL(address)) { |
|
260 |
JNU_ThrowNullPointerException(env, "address"); |
|
261 |
return; |
|
262 |
} |
|
263 |
||
43100 | 264 |
if (NET_InetAddressToSockaddr(env, address, port, &rmtaddr, &len, |
265 |
JNI_TRUE) != 0) { |
|
2 | 266 |
return; |
267 |
} |
|
268 |
||
41380
c27cf95dd7e6
8167295: Further cleanup to the native parts of libnet/libnio
clanger
parents:
41214
diff
changeset
|
269 |
setDefaultScopeID(env, &rmtaddr.sa); |
13999
f6a2ce6a3e40
7193520: Removed references to Linux kernel version 2.2
chegar
parents:
13574
diff
changeset
|
270 |
|
41380
c27cf95dd7e6
8167295: Further cleanup to the native parts of libnet/libnio
clanger
parents:
41214
diff
changeset
|
271 |
if (NET_Connect(fd, &rmtaddr.sa, len) == -1) { |
13999
f6a2ce6a3e40
7193520: Removed references to Linux kernel version 2.2
chegar
parents:
13574
diff
changeset
|
272 |
NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "ConnectException", |
f6a2ce6a3e40
7193520: Removed references to Linux kernel version 2.2
chegar
parents:
13574
diff
changeset
|
273 |
"Connect failed"); |
2 | 274 |
} |
275 |
} |
|
276 |
||
277 |
/* |
|
278 |
* Class: java_net_PlainDatagramSocketImpl |
|
279 |
* Method: disconnect0 |
|
280 |
* Signature: ()V |
|
281 |
*/ |
|
282 |
JNIEXPORT void JNICALL |
|
283 |
Java_java_net_PlainDatagramSocketImpl_disconnect0(JNIEnv *env, jobject this, jint family) { |
|
284 |
/* The object's field */ |
|
285 |
jobject fdObj = (*env)->GetObjectField(env, this, pdsi_fdID); |
|
286 |
/* The fdObj'fd */ |
|
287 |
jint fd; |
|
288 |
||
12047
320a714614e9
7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
9035
diff
changeset
|
289 |
#if defined(__linux__) || defined(_ALLBSD_SOURCE) |
41380
c27cf95dd7e6
8167295: Further cleanup to the native parts of libnet/libnio
clanger
parents:
41214
diff
changeset
|
290 |
SOCKETADDRESS addr; |
23015
73b21ab36615
8034174: Remove use of JVM_* functions from java.net code
chegar
parents:
23010
diff
changeset
|
291 |
socklen_t len; |
43100 | 292 |
#if defined(__linux__) |
293 |
int localPort = 0; |
|
294 |
#endif |
|
2 | 295 |
#endif |
296 |
||
297 |
if (IS_NULL(fdObj)) { |
|
298 |
return; |
|
299 |
} |
|
300 |
fd = (*env)->GetIntField(env, fdObj, IO_fd_fdID); |
|
301 |
||
12047
320a714614e9
7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
9035
diff
changeset
|
302 |
#if defined(__linux__) || defined(_ALLBSD_SOURCE) |
43100 | 303 |
memset(&addr, 0, sizeof(addr)); |
304 |
if (ipv6_available()) { |
|
305 |
addr.sa6.sin6_family = AF_UNSPEC; |
|
306 |
len = sizeof(struct sockaddr_in6); |
|
307 |
} else { |
|
308 |
addr.sa4.sin_family = AF_UNSPEC; |
|
309 |
len = sizeof(struct sockaddr_in); |
|
310 |
} |
|
311 |
NET_Connect(fd, &addr.sa, len); |
|
2 | 312 |
|
43100 | 313 |
#if defined(__linux__) |
314 |
if (getsockname(fd, &addr.sa, &len) == -1) |
|
315 |
return; |
|
13999
f6a2ce6a3e40
7193520: Removed references to Linux kernel version 2.2
chegar
parents:
13574
diff
changeset
|
316 |
|
43100 | 317 |
localPort = NET_GetPortFromSockaddr(&addr); |
318 |
if (localPort == 0) { |
|
319 |
localPort = (*env)->GetIntField(env, this, pdsi_localPortID); |
|
320 |
if (addr.sa.sa_family == AF_INET6) { |
|
321 |
addr.sa6.sin6_port = htons(localPort); |
|
322 |
} else { |
|
323 |
addr.sa4.sin_port = htons(localPort); |
|
324 |
} |
|
13999
f6a2ce6a3e40
7193520: Removed references to Linux kernel version 2.2
chegar
parents:
13574
diff
changeset
|
325 |
|
43100 | 326 |
NET_Bind(fd, &addr, len); |
327 |
} |
|
13999
f6a2ce6a3e40
7193520: Removed references to Linux kernel version 2.2
chegar
parents:
13574
diff
changeset
|
328 |
|
12047
320a714614e9
7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
9035
diff
changeset
|
329 |
#endif |
2 | 330 |
#else |
23015
73b21ab36615
8034174: Remove use of JVM_* functions from java.net code
chegar
parents:
23010
diff
changeset
|
331 |
NET_Connect(fd, 0, 0); |
2 | 332 |
#endif |
333 |
} |
|
334 |
||
335 |
/* |
|
336 |
* Class: java_net_PlainDatagramSocketImpl |
|
337 |
* Method: send |
|
338 |
* Signature: (Ljava/net/DatagramPacket;)V |
|
339 |
*/ |
|
340 |
JNIEXPORT void JNICALL |
|
341 |
Java_java_net_PlainDatagramSocketImpl_send(JNIEnv *env, jobject this, |
|
342 |
jobject packet) { |
|
343 |
||
344 |
char BUF[MAX_BUFFER_LEN]; |
|
345 |
char *fullPacket = NULL; |
|
346 |
int ret, mallocedPacket = JNI_FALSE; |
|
347 |
/* The object's field */ |
|
348 |
jobject fdObj = (*env)->GetObjectField(env, this, pdsi_fdID); |
|
349 |
jint trafficClass = (*env)->GetIntField(env, this, pdsi_trafficClassID); |
|
350 |
||
351 |
jbyteArray packetBuffer; |
|
352 |
jobject packetAddress; |
|
353 |
jint packetBufferOffset, packetBufferLen, packetPort; |
|
354 |
jboolean connected; |
|
355 |
||
356 |
/* The fdObj'fd */ |
|
357 |
jint fd; |
|
358 |
||
43100 | 359 |
SOCKETADDRESS rmtaddr; |
360 |
struct sockaddr *rmtaddrP = 0; |
|
361 |
int len = 0; |
|
2 | 362 |
|
363 |
if (IS_NULL(fdObj)) { |
|
364 |
JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException", |
|
365 |
"Socket closed"); |
|
366 |
return; |
|
367 |
} |
|
368 |
fd = (*env)->GetIntField(env, fdObj, IO_fd_fdID); |
|
369 |
||
370 |
if (IS_NULL(packet)) { |
|
371 |
JNU_ThrowNullPointerException(env, "packet"); |
|
372 |
return; |
|
373 |
} |
|
374 |
||
375 |
connected = (*env)->GetBooleanField(env, this, pdsi_connected); |
|
376 |
||
377 |
packetBuffer = (*env)->GetObjectField(env, packet, dp_bufID); |
|
378 |
packetAddress = (*env)->GetObjectField(env, packet, dp_addressID); |
|
379 |
if (IS_NULL(packetBuffer) || IS_NULL(packetAddress)) { |
|
380 |
JNU_ThrowNullPointerException(env, "null buffer || null address"); |
|
381 |
return; |
|
382 |
} |
|
383 |
||
384 |
packetBufferOffset = (*env)->GetIntField(env, packet, dp_offsetID); |
|
385 |
packetBufferLen = (*env)->GetIntField(env, packet, dp_lengthID); |
|
386 |
||
43100 | 387 |
// arg to NET_Sendto() null, if connected |
388 |
if (!connected) { |
|
2 | 389 |
packetPort = (*env)->GetIntField(env, packet, dp_portID); |
43100 | 390 |
if (NET_InetAddressToSockaddr(env, packetAddress, packetPort, &rmtaddr, |
391 |
&len, JNI_TRUE) != 0) { |
|
41380
c27cf95dd7e6
8167295: Further cleanup to the native parts of libnet/libnio
clanger
parents:
41214
diff
changeset
|
392 |
return; |
2 | 393 |
} |
43100 | 394 |
rmtaddrP = &rmtaddr.sa; |
2 | 395 |
} |
41380
c27cf95dd7e6
8167295: Further cleanup to the native parts of libnet/libnio
clanger
parents:
41214
diff
changeset
|
396 |
setDefaultScopeID(env, &rmtaddr.sa); |
2 | 397 |
|
398 |
if (packetBufferLen > MAX_BUFFER_LEN) { |
|
399 |
/* When JNI-ifying the JDK's IO routines, we turned |
|
13999
f6a2ce6a3e40
7193520: Removed references to Linux kernel version 2.2
chegar
parents:
13574
diff
changeset
|
400 |
* reads and writes of byte arrays of size greater |
2 | 401 |
* than 2048 bytes into several operations of size 2048. |
402 |
* This saves a malloc()/memcpy()/free() for big |
|
403 |
* buffers. This is OK for file IO and TCP, but that |
|
404 |
* strategy violates the semantics of a datagram protocol. |
|
405 |
* (one big send) != (several smaller sends). So here |
|
13999
f6a2ce6a3e40
7193520: Removed references to Linux kernel version 2.2
chegar
parents:
13574
diff
changeset
|
406 |
* we *must* allocate the buffer. Note it needn't be bigger |
f6a2ce6a3e40
7193520: Removed references to Linux kernel version 2.2
chegar
parents:
13574
diff
changeset
|
407 |
* than 65,536 (0xFFFF), the max size of an IP packet. |
2 | 408 |
* Anything bigger should be truncated anyway. |
409 |
* |
|
410 |
* We may want to use a smarter allocation scheme at some |
|
411 |
* point. |
|
412 |
*/ |
|
413 |
if (packetBufferLen > MAX_PACKET_LEN) { |
|
414 |
packetBufferLen = MAX_PACKET_LEN; |
|
415 |
} |
|
416 |
fullPacket = (char *)malloc(packetBufferLen); |
|
417 |
||
418 |
if (!fullPacket) { |
|
13245
7ab3ef5b9520
7181353: Update error message to distinguish native OOM and java OOM in net
zhouyx
parents:
12551
diff
changeset
|
419 |
JNU_ThrowOutOfMemoryError(env, "Send buffer native heap allocation failed"); |
2 | 420 |
return; |
421 |
} else { |
|
422 |
mallocedPacket = JNI_TRUE; |
|
423 |
} |
|
424 |
} else { |
|
425 |
fullPacket = &(BUF[0]); |
|
426 |
} |
|
427 |
||
428 |
(*env)->GetByteArrayRegion(env, packetBuffer, packetBufferOffset, packetBufferLen, |
|
429 |
(jbyte *)fullPacket); |
|
430 |
if (trafficClass != 0 && ipv6_available()) { |
|
43100 | 431 |
NET_SetTrafficClass(&rmtaddr, trafficClass); |
2 | 432 |
} |
433 |
||
434 |
/* |
|
435 |
* Send the datagram. |
|
436 |
* |
|
437 |
* If we are connected it's possible that sendto will return |
|
438 |
* ECONNREFUSED indicating that an ICMP port unreachable has |
|
439 |
* received. |
|
440 |
*/ |
|
43100 | 441 |
ret = NET_SendTo(fd, fullPacket, packetBufferLen, 0, rmtaddrP, len); |
2 | 442 |
|
443 |
if (ret < 0) { |
|
23015
73b21ab36615
8034174: Remove use of JVM_* functions from java.net code
chegar
parents:
23010
diff
changeset
|
444 |
if (errno == ECONNREFUSED) { |
73b21ab36615
8034174: Remove use of JVM_* functions from java.net code
chegar
parents:
23010
diff
changeset
|
445 |
JNU_ThrowByName(env, JNU_JAVANETPKG "PortUnreachableException", |
2 | 446 |
"ICMP Port Unreachable"); |
23015
73b21ab36615
8034174: Remove use of JVM_* functions from java.net code
chegar
parents:
23010
diff
changeset
|
447 |
} else { |
39318
2006d1d41c8b
8158023: SocketExceptions contain too little information sometimes
clanger
parents:
36115
diff
changeset
|
448 |
JNU_ThrowIOExceptionWithLastError(env, "sendto failed"); |
2 | 449 |
} |
450 |
} |
|
451 |
||
452 |
if (mallocedPacket) { |
|
453 |
free(fullPacket); |
|
454 |
} |
|
455 |
return; |
|
456 |
} |
|
457 |
||
458 |
/* |
|
459 |
* Class: java_net_PlainDatagramSocketImpl |
|
460 |
* Method: peek |
|
461 |
* Signature: (Ljava/net/InetAddress;)I |
|
462 |
*/ |
|
463 |
JNIEXPORT jint JNICALL |
|
464 |
Java_java_net_PlainDatagramSocketImpl_peek(JNIEnv *env, jobject this, |
|
465 |
jobject addressObj) { |
|
466 |
||
467 |
jobject fdObj = (*env)->GetObjectField(env, this, pdsi_fdID); |
|
468 |
jint timeout = (*env)->GetIntField(env, this, pdsi_timeoutID); |
|
469 |
jint fd; |
|
470 |
ssize_t n; |
|
41380
c27cf95dd7e6
8167295: Further cleanup to the native parts of libnet/libnio
clanger
parents:
41214
diff
changeset
|
471 |
SOCKETADDRESS rmtaddr; |
c27cf95dd7e6
8167295: Further cleanup to the native parts of libnet/libnio
clanger
parents:
41214
diff
changeset
|
472 |
socklen_t slen = sizeof(SOCKETADDRESS); |
2 | 473 |
char buf[1]; |
474 |
jint family; |
|
475 |
jobject iaObj; |
|
476 |
int port; |
|
477 |
if (IS_NULL(fdObj)) { |
|
478 |
JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException", "Socket closed"); |
|
479 |
return -1; |
|
480 |
} else { |
|
481 |
fd = (*env)->GetIntField(env, fdObj, IO_fd_fdID); |
|
482 |
} |
|
483 |
if (IS_NULL(addressObj)) { |
|
484 |
JNU_ThrowNullPointerException(env, "Null address in peek()"); |
|
22651
bfc6ca5245fd
8033689: PlainDatagramSocketImpl missing returns after throwing an exception
chegar
parents:
22646
diff
changeset
|
485 |
return -1; |
2 | 486 |
} |
487 |
if (timeout) { |
|
45061
74b09ee3cd55
8179905: Remove the use of gettimeofday in Networking code
vtewari
parents:
44921
diff
changeset
|
488 |
int ret = NET_Timeout(env, fd, timeout, JVM_NanoTime(env, 0)); |
2 | 489 |
if (ret == 0) { |
490 |
JNU_ThrowByName(env, JNU_JAVANETPKG "SocketTimeoutException", |
|
491 |
"Peek timed out"); |
|
492 |
return ret; |
|
23015
73b21ab36615
8034174: Remove use of JVM_* functions from java.net code
chegar
parents:
23010
diff
changeset
|
493 |
} else if (ret == -1) { |
2 | 494 |
if (errno == EBADF) { |
495 |
JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException", "Socket closed"); |
|
23048 | 496 |
} else if (errno == ENOMEM) { |
497 |
JNU_ThrowOutOfMemoryError(env, "NET_Timeout native heap allocation failed"); |
|
2 | 498 |
} else { |
39318
2006d1d41c8b
8158023: SocketExceptions contain too little information sometimes
clanger
parents:
36115
diff
changeset
|
499 |
JNU_ThrowByNameWithMessageAndLastError |
2006d1d41c8b
8158023: SocketExceptions contain too little information sometimes
clanger
parents:
36115
diff
changeset
|
500 |
(env, JNU_JAVANETPKG "SocketException", "Peek failed"); |
2 | 501 |
} |
502 |
return ret; |
|
503 |
} |
|
504 |
} |
|
505 |
||
41380
c27cf95dd7e6
8167295: Further cleanup to the native parts of libnet/libnio
clanger
parents:
41214
diff
changeset
|
506 |
n = NET_RecvFrom(fd, buf, 1, MSG_PEEK, &rmtaddr.sa, &slen); |
2 | 507 |
|
23015
73b21ab36615
8034174: Remove use of JVM_* functions from java.net code
chegar
parents:
23010
diff
changeset
|
508 |
if (n == -1) { |
2 | 509 |
|
510 |
#ifdef __solaris__ |
|
511 |
if (errno == ECONNREFUSED) { |
|
512 |
int orig_errno = errno; |
|
43100 | 513 |
recv(fd, buf, 1, 0); |
2 | 514 |
errno = orig_errno; |
515 |
} |
|
516 |
#endif |
|
517 |
if (errno == ECONNREFUSED) { |
|
518 |
JNU_ThrowByName(env, JNU_JAVANETPKG "PortUnreachableException", |
|
519 |
"ICMP Port Unreachable"); |
|
520 |
} else { |
|
521 |
if (errno == EBADF) { |
|
522 |
JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException", "Socket closed"); |
|
523 |
} else { |
|
39318
2006d1d41c8b
8158023: SocketExceptions contain too little information sometimes
clanger
parents:
36115
diff
changeset
|
524 |
JNU_ThrowByNameWithMessageAndLastError |
2006d1d41c8b
8158023: SocketExceptions contain too little information sometimes
clanger
parents:
36115
diff
changeset
|
525 |
(env, JNU_JAVANETPKG "SocketException", "Peek failed"); |
2 | 526 |
} |
527 |
} |
|
528 |
return 0; |
|
529 |
} |
|
530 |
||
43100 | 531 |
iaObj = NET_SockaddrToInetAddress(env, &rmtaddr, &port); |
41771
18c9669e76ca
8167481: cleanup of headers and includes for native libnet
clanger
parents:
41380
diff
changeset
|
532 |
family = getInetAddress_family(env, iaObj) == java_net_InetAddress_IPv4 ? |
18c9669e76ca
8167481: cleanup of headers and includes for native libnet
clanger
parents:
41380
diff
changeset
|
533 |
AF_INET : AF_INET6; |
51151 | 534 |
JNU_CHECK_EXCEPTION_RETURN(env, -1); |
13999
f6a2ce6a3e40
7193520: Removed references to Linux kernel version 2.2
chegar
parents:
13574
diff
changeset
|
535 |
if (family == AF_INET) { /* this API can't handle IPV6 addresses */ |
16870 | 536 |
int address = getInetAddress_addr(env, iaObj); |
51151 | 537 |
JNU_CHECK_EXCEPTION_RETURN(env, -1); |
16870 | 538 |
setInetAddress_addr(env, addressObj, address); |
51151 | 539 |
JNU_CHECK_EXCEPTION_RETURN(env, -1); |
2 | 540 |
} |
541 |
return port; |
|
542 |
} |
|
543 |
||
544 |
JNIEXPORT jint JNICALL |
|
545 |
Java_java_net_PlainDatagramSocketImpl_peekData(JNIEnv *env, jobject this, |
|
546 |
jobject packet) { |
|
547 |
||
548 |
char BUF[MAX_BUFFER_LEN]; |
|
549 |
char *fullPacket = NULL; |
|
550 |
int mallocedPacket = JNI_FALSE; |
|
551 |
jobject fdObj = (*env)->GetObjectField(env, this, pdsi_fdID); |
|
552 |
jint timeout = (*env)->GetIntField(env, this, pdsi_timeoutID); |
|
553 |
jbyteArray packetBuffer; |
|
554 |
jint packetBufferOffset, packetBufferLen; |
|
555 |
int fd; |
|
556 |
int n; |
|
41380
c27cf95dd7e6
8167295: Further cleanup to the native parts of libnet/libnio
clanger
parents:
41214
diff
changeset
|
557 |
SOCKETADDRESS rmtaddr; |
c27cf95dd7e6
8167295: Further cleanup to the native parts of libnet/libnio
clanger
parents:
41214
diff
changeset
|
558 |
socklen_t slen = sizeof(SOCKETADDRESS); |
c27cf95dd7e6
8167295: Further cleanup to the native parts of libnet/libnio
clanger
parents:
41214
diff
changeset
|
559 |
int port = -1; |
2 | 560 |
|
561 |
if (IS_NULL(fdObj)) { |
|
562 |
JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException", |
|
563 |
"Socket closed"); |
|
564 |
return -1; |
|
565 |
} |
|
566 |
||
567 |
fd = (*env)->GetIntField(env, fdObj, IO_fd_fdID); |
|
568 |
||
569 |
if (IS_NULL(packet)) { |
|
570 |
JNU_ThrowNullPointerException(env, "packet"); |
|
571 |
return -1; |
|
572 |
} |
|
573 |
||
574 |
packetBuffer = (*env)->GetObjectField(env, packet, dp_bufID); |
|
575 |
if (IS_NULL(packetBuffer)) { |
|
576 |
JNU_ThrowNullPointerException(env, "packet buffer"); |
|
577 |
return -1; |
|
578 |
} |
|
579 |
packetBufferOffset = (*env)->GetIntField(env, packet, dp_offsetID); |
|
580 |
packetBufferLen = (*env)->GetIntField(env, packet, dp_bufLengthID); |
|
581 |
if (timeout) { |
|
45061
74b09ee3cd55
8179905: Remove the use of gettimeofday in Networking code
vtewari
parents:
44921
diff
changeset
|
582 |
int ret = NET_Timeout(env, fd, timeout, JVM_NanoTime(env, 0)); |
2 | 583 |
if (ret == 0) { |
584 |
JNU_ThrowByName(env, JNU_JAVANETPKG "SocketTimeoutException", |
|
585 |
"Receive timed out"); |
|
586 |
return -1; |
|
23015
73b21ab36615
8034174: Remove use of JVM_* functions from java.net code
chegar
parents:
23010
diff
changeset
|
587 |
} else if (ret == -1) { |
23048 | 588 |
if (errno == ENOMEM) { |
589 |
JNU_ThrowOutOfMemoryError(env, "NET_Timeout native heap allocation failed"); |
|
2 | 590 |
#ifdef __linux__ |
23048 | 591 |
} else if (errno == EBADF) { |
2 | 592 |
JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException", "Socket closed"); |
593 |
} else { |
|
39318
2006d1d41c8b
8158023: SocketExceptions contain too little information sometimes
clanger
parents:
36115
diff
changeset
|
594 |
JNU_ThrowByNameWithMessageAndLastError |
2006d1d41c8b
8158023: SocketExceptions contain too little information sometimes
clanger
parents:
36115
diff
changeset
|
595 |
(env, JNU_JAVANETPKG "SocketException", "Receive failed"); |
23048 | 596 |
#else |
597 |
} else { |
|
598 |
JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException", "Socket closed"); |
|
599 |
#endif |
|
2 | 600 |
} |
601 |
return -1; |
|
602 |
} |
|
603 |
} |
|
604 |
||
605 |
if (packetBufferLen > MAX_BUFFER_LEN) { |
|
606 |
||
607 |
/* When JNI-ifying the JDK's IO routines, we turned |
|
13999
f6a2ce6a3e40
7193520: Removed references to Linux kernel version 2.2
chegar
parents:
13574
diff
changeset
|
608 |
* reads and writes of byte arrays of size greater |
2 | 609 |
* than 2048 bytes into several operations of size 2048. |
610 |
* This saves a malloc()/memcpy()/free() for big |
|
611 |
* buffers. This is OK for file IO and TCP, but that |
|
612 |
* strategy violates the semantics of a datagram protocol. |
|
613 |
* (one big send) != (several smaller sends). So here |
|
13999
f6a2ce6a3e40
7193520: Removed references to Linux kernel version 2.2
chegar
parents:
13574
diff
changeset
|
614 |
* we *must* allocate the buffer. Note it needn't be bigger |
f6a2ce6a3e40
7193520: Removed references to Linux kernel version 2.2
chegar
parents:
13574
diff
changeset
|
615 |
* than 65,536 (0xFFFF), the max size of an IP packet. |
2 | 616 |
* anything bigger is truncated anyway. |
617 |
* |
|
618 |
* We may want to use a smarter allocation scheme at some |
|
619 |
* point. |
|
620 |
*/ |
|
621 |
if (packetBufferLen > MAX_PACKET_LEN) { |
|
622 |
packetBufferLen = MAX_PACKET_LEN; |
|
623 |
} |
|
624 |
fullPacket = (char *)malloc(packetBufferLen); |
|
625 |
||
626 |
if (!fullPacket) { |
|
13245
7ab3ef5b9520
7181353: Update error message to distinguish native OOM and java OOM in net
zhouyx
parents:
12551
diff
changeset
|
627 |
JNU_ThrowOutOfMemoryError(env, "Peek buffer native heap allocation failed"); |
2 | 628 |
return -1; |
629 |
} else { |
|
630 |
mallocedPacket = JNI_TRUE; |
|
631 |
} |
|
632 |
} else { |
|
633 |
fullPacket = &(BUF[0]); |
|
634 |
} |
|
635 |
||
636 |
n = NET_RecvFrom(fd, fullPacket, packetBufferLen, MSG_PEEK, |
|
41380
c27cf95dd7e6
8167295: Further cleanup to the native parts of libnet/libnio
clanger
parents:
41214
diff
changeset
|
637 |
&rmtaddr.sa, &slen); |
2 | 638 |
/* truncate the data if the packet's length is too small */ |
639 |
if (n > packetBufferLen) { |
|
640 |
n = packetBufferLen; |
|
641 |
} |
|
23015
73b21ab36615
8034174: Remove use of JVM_* functions from java.net code
chegar
parents:
23010
diff
changeset
|
642 |
if (n == -1) { |
2 | 643 |
|
644 |
#ifdef __solaris__ |
|
645 |
if (errno == ECONNREFUSED) { |
|
646 |
int orig_errno = errno; |
|
647 |
(void) recv(fd, fullPacket, 1, 0); |
|
648 |
errno = orig_errno; |
|
649 |
} |
|
650 |
#endif |
|
651 |
(*env)->SetIntField(env, packet, dp_offsetID, 0); |
|
652 |
(*env)->SetIntField(env, packet, dp_lengthID, 0); |
|
653 |
if (errno == ECONNREFUSED) { |
|
654 |
JNU_ThrowByName(env, JNU_JAVANETPKG "PortUnreachableException", |
|
655 |
"ICMP Port Unreachable"); |
|
656 |
} else { |
|
657 |
if (errno == EBADF) { |
|
658 |
JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException", "Socket closed"); |
|
659 |
} else { |
|
39318
2006d1d41c8b
8158023: SocketExceptions contain too little information sometimes
clanger
parents:
36115
diff
changeset
|
660 |
JNU_ThrowByNameWithMessageAndLastError |
2006d1d41c8b
8158023: SocketExceptions contain too little information sometimes
clanger
parents:
36115
diff
changeset
|
661 |
(env, JNU_JAVANETPKG "SocketException", "Receive failed"); |
2 | 662 |
} |
663 |
} |
|
664 |
} else { |
|
665 |
/* |
|
666 |
* success - fill in received address... |
|
667 |
* |
|
668 |
* REMIND: Fill in an int on the packet, and create inetadd |
|
669 |
* object in Java, as a performance improvement. Also |
|
670 |
* construct the inetadd object lazily. |
|
671 |
*/ |
|
672 |
||
673 |
jobject packetAddress; |
|
674 |
||
675 |
/* |
|
676 |
* Check if there is an InetAddress already associated with this |
|
677 |
* packet. If so we check if it is the same source address. We |
|
678 |
* can't update any existing InetAddress because it is immutable |
|
679 |
*/ |
|
680 |
packetAddress = (*env)->GetObjectField(env, packet, dp_addressID); |
|
681 |
if (packetAddress != NULL) { |
|
43100 | 682 |
if (!NET_SockaddrEqualsInetAddress(env, &rmtaddr, packetAddress)) { |
2 | 683 |
/* force a new InetAddress to be created */ |
684 |
packetAddress = NULL; |
|
685 |
} |
|
686 |
} |
|
687 |
if (packetAddress == NULL) { |
|
43100 | 688 |
packetAddress = NET_SockaddrToInetAddress(env, &rmtaddr, &port); |
2 | 689 |
/* stuff the new Inetaddress in the packet */ |
690 |
(*env)->SetObjectField(env, packet, dp_addressID, packetAddress); |
|
691 |
} else { |
|
692 |
/* only get the new port number */ |
|
43100 | 693 |
port = NET_GetPortFromSockaddr(&rmtaddr); |
2 | 694 |
} |
695 |
/* and fill in the data, remote address/port and such */ |
|
696 |
(*env)->SetByteArrayRegion(env, packetBuffer, packetBufferOffset, n, |
|
697 |
(jbyte *)fullPacket); |
|
698 |
(*env)->SetIntField(env, packet, dp_portID, port); |
|
699 |
(*env)->SetIntField(env, packet, dp_lengthID, n); |
|
700 |
} |
|
701 |
||
702 |
if (mallocedPacket) { |
|
703 |
free(fullPacket); |
|
704 |
} |
|
705 |
return port; |
|
706 |
} |
|
707 |
||
708 |
/* |
|
709 |
* Class: java_net_PlainDatagramSocketImpl |
|
710 |
* Method: receive |
|
711 |
* Signature: (Ljava/net/DatagramPacket;)V |
|
712 |
*/ |
|
713 |
JNIEXPORT void JNICALL |
|
714 |
Java_java_net_PlainDatagramSocketImpl_receive0(JNIEnv *env, jobject this, |
|
715 |
jobject packet) { |
|
716 |
||
717 |
char BUF[MAX_BUFFER_LEN]; |
|
718 |
char *fullPacket = NULL; |
|
719 |
int mallocedPacket = JNI_FALSE; |
|
720 |
jobject fdObj = (*env)->GetObjectField(env, this, pdsi_fdID); |
|
721 |
jint timeout = (*env)->GetIntField(env, this, pdsi_timeoutID); |
|
722 |
||
723 |
jbyteArray packetBuffer; |
|
724 |
jint packetBufferOffset, packetBufferLen; |
|
725 |
||
726 |
int fd; |
|
727 |
||
728 |
int n; |
|
41380
c27cf95dd7e6
8167295: Further cleanup to the native parts of libnet/libnio
clanger
parents:
41214
diff
changeset
|
729 |
SOCKETADDRESS rmtaddr; |
c27cf95dd7e6
8167295: Further cleanup to the native parts of libnet/libnio
clanger
parents:
41214
diff
changeset
|
730 |
socklen_t slen = sizeof(SOCKETADDRESS); |
2 | 731 |
jboolean retry; |
732 |
#ifdef __linux__ |
|
733 |
jboolean connected = JNI_FALSE; |
|
4814 | 734 |
jobject connectedAddress = NULL; |
735 |
jint connectedPort = 0; |
|
736 |
jlong prevTime = 0; |
|
2 | 737 |
#endif |
738 |
||
739 |
if (IS_NULL(fdObj)) { |
|
740 |
JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException", |
|
741 |
"Socket closed"); |
|
742 |
return; |
|
743 |
} |
|
744 |
||
745 |
fd = (*env)->GetIntField(env, fdObj, IO_fd_fdID); |
|
746 |
||
747 |
if (IS_NULL(packet)) { |
|
748 |
JNU_ThrowNullPointerException(env, "packet"); |
|
749 |
return; |
|
750 |
} |
|
751 |
||
752 |
packetBuffer = (*env)->GetObjectField(env, packet, dp_bufID); |
|
753 |
if (IS_NULL(packetBuffer)) { |
|
754 |
JNU_ThrowNullPointerException(env, "packet buffer"); |
|
755 |
return; |
|
756 |
} |
|
757 |
packetBufferOffset = (*env)->GetIntField(env, packet, dp_offsetID); |
|
758 |
packetBufferLen = (*env)->GetIntField(env, packet, dp_bufLengthID); |
|
759 |
||
760 |
if (packetBufferLen > MAX_BUFFER_LEN) { |
|
761 |
||
762 |
/* When JNI-ifying the JDK's IO routines, we turned |
|
13999
f6a2ce6a3e40
7193520: Removed references to Linux kernel version 2.2
chegar
parents:
13574
diff
changeset
|
763 |
* reads and writes of byte arrays of size greater |
2 | 764 |
* than 2048 bytes into several operations of size 2048. |
765 |
* This saves a malloc()/memcpy()/free() for big |
|
766 |
* buffers. This is OK for file IO and TCP, but that |
|
767 |
* strategy violates the semantics of a datagram protocol. |
|
768 |
* (one big send) != (several smaller sends). So here |
|
13999
f6a2ce6a3e40
7193520: Removed references to Linux kernel version 2.2
chegar
parents:
13574
diff
changeset
|
769 |
* we *must* allocate the buffer. Note it needn't be bigger |
f6a2ce6a3e40
7193520: Removed references to Linux kernel version 2.2
chegar
parents:
13574
diff
changeset
|
770 |
* than 65,536 (0xFFFF) the max size of an IP packet, |
2 | 771 |
* anything bigger is truncated anyway. |
772 |
* |
|
773 |
* We may want to use a smarter allocation scheme at some |
|
774 |
* point. |
|
775 |
*/ |
|
776 |
if (packetBufferLen > MAX_PACKET_LEN) { |
|
777 |
packetBufferLen = MAX_PACKET_LEN; |
|
778 |
} |
|
779 |
fullPacket = (char *)malloc(packetBufferLen); |
|
780 |
||
781 |
if (!fullPacket) { |
|
13245
7ab3ef5b9520
7181353: Update error message to distinguish native OOM and java OOM in net
zhouyx
parents:
12551
diff
changeset
|
782 |
JNU_ThrowOutOfMemoryError(env, "Receive buffer native heap allocation failed"); |
2 | 783 |
return; |
784 |
} else { |
|
785 |
mallocedPacket = JNI_TRUE; |
|
786 |
} |
|
787 |
} else { |
|
788 |
fullPacket = &(BUF[0]); |
|
789 |
} |
|
790 |
||
791 |
do { |
|
792 |
retry = JNI_FALSE; |
|
793 |
||
794 |
if (timeout) { |
|
45061
74b09ee3cd55
8179905: Remove the use of gettimeofday in Networking code
vtewari
parents:
44921
diff
changeset
|
795 |
int ret = NET_Timeout(env, fd, timeout, JVM_NanoTime(env, 0)); |
2 | 796 |
if (ret <= 0) { |
23048 | 797 |
if (ret == 0) { |
2 | 798 |
JNU_ThrowByName(env, JNU_JAVANETPKG "SocketTimeoutException", |
799 |
"Receive timed out"); |
|
23015
73b21ab36615
8034174: Remove use of JVM_* functions from java.net code
chegar
parents:
23010
diff
changeset
|
800 |
} else if (ret == -1) { |
23048 | 801 |
if (errno == ENOMEM) { |
802 |
JNU_ThrowOutOfMemoryError(env, "NET_Timeout native heap allocation failed"); |
|
2 | 803 |
#ifdef __linux__ |
23048 | 804 |
} else if (errno == EBADF) { |
2 | 805 |
JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException", "Socket closed"); |
23048 | 806 |
} else { |
39318
2006d1d41c8b
8158023: SocketExceptions contain too little information sometimes
clanger
parents:
36115
diff
changeset
|
807 |
JNU_ThrowByNameWithMessageAndLastError |
2006d1d41c8b
8158023: SocketExceptions contain too little information sometimes
clanger
parents:
36115
diff
changeset
|
808 |
(env, JNU_JAVANETPKG "SocketException", "Receive failed"); |
2 | 809 |
#else |
23048 | 810 |
} else { |
811 |
JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException", "Socket closed"); |
|
2 | 812 |
#endif |
23048 | 813 |
} |
2 | 814 |
} |
815 |
||
816 |
if (mallocedPacket) { |
|
817 |
free(fullPacket); |
|
818 |
} |
|
819 |
||
820 |
return; |
|
821 |
} |
|
822 |
} |
|
823 |
||
824 |
n = NET_RecvFrom(fd, fullPacket, packetBufferLen, 0, |
|
41380
c27cf95dd7e6
8167295: Further cleanup to the native parts of libnet/libnio
clanger
parents:
41214
diff
changeset
|
825 |
&rmtaddr.sa, &slen); |
2 | 826 |
/* truncate the data if the packet's length is too small */ |
827 |
if (n > packetBufferLen) { |
|
828 |
n = packetBufferLen; |
|
829 |
} |
|
23015
73b21ab36615
8034174: Remove use of JVM_* functions from java.net code
chegar
parents:
23010
diff
changeset
|
830 |
if (n == -1) { |
2 | 831 |
(*env)->SetIntField(env, packet, dp_offsetID, 0); |
832 |
(*env)->SetIntField(env, packet, dp_lengthID, 0); |
|
833 |
if (errno == ECONNREFUSED) { |
|
834 |
JNU_ThrowByName(env, JNU_JAVANETPKG "PortUnreachableException", |
|
835 |
"ICMP Port Unreachable"); |
|
836 |
} else { |
|
837 |
if (errno == EBADF) { |
|
838 |
JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException", "Socket closed"); |
|
839 |
} else { |
|
39318
2006d1d41c8b
8158023: SocketExceptions contain too little information sometimes
clanger
parents:
36115
diff
changeset
|
840 |
JNU_ThrowByNameWithMessageAndLastError |
2006d1d41c8b
8158023: SocketExceptions contain too little information sometimes
clanger
parents:
36115
diff
changeset
|
841 |
(env, JNU_JAVANETPKG "SocketException", "Receive failed"); |
2 | 842 |
} |
843 |
} |
|
844 |
} else { |
|
845 |
int port; |
|
846 |
jobject packetAddress; |
|
847 |
||
848 |
/* |
|
849 |
* success - fill in received address... |
|
850 |
* |
|
851 |
* REMIND: Fill in an int on the packet, and create inetadd |
|
852 |
* object in Java, as a performance improvement. Also |
|
853 |
* construct the inetadd object lazily. |
|
854 |
*/ |
|
855 |
||
856 |
/* |
|
857 |
* Check if there is an InetAddress already associated with this |
|
858 |
* packet. If so we check if it is the same source address. We |
|
859 |
* can't update any existing InetAddress because it is immutable |
|
860 |
*/ |
|
861 |
packetAddress = (*env)->GetObjectField(env, packet, dp_addressID); |
|
862 |
if (packetAddress != NULL) { |
|
43100 | 863 |
if (!NET_SockaddrEqualsInetAddress(env, &rmtaddr, |
864 |
packetAddress)) { |
|
2 | 865 |
/* force a new InetAddress to be created */ |
866 |
packetAddress = NULL; |
|
867 |
} |
|
868 |
} |
|
869 |
if (packetAddress == NULL) { |
|
43100 | 870 |
packetAddress = NET_SockaddrToInetAddress(env, &rmtaddr, &port); |
2 | 871 |
/* stuff the new Inetaddress in the packet */ |
872 |
(*env)->SetObjectField(env, packet, dp_addressID, packetAddress); |
|
873 |
} else { |
|
874 |
/* only get the new port number */ |
|
43100 | 875 |
port = NET_GetPortFromSockaddr(&rmtaddr); |
2 | 876 |
} |
877 |
/* and fill in the data, remote address/port and such */ |
|
878 |
(*env)->SetByteArrayRegion(env, packetBuffer, packetBufferOffset, n, |
|
879 |
(jbyte *)fullPacket); |
|
880 |
(*env)->SetIntField(env, packet, dp_portID, port); |
|
881 |
(*env)->SetIntField(env, packet, dp_lengthID, n); |
|
882 |
} |
|
883 |
||
884 |
} while (retry); |
|
885 |
||
886 |
if (mallocedPacket) { |
|
887 |
free(fullPacket); |
|
888 |
} |
|
889 |
} |
|
890 |
||
891 |
/* |
|
892 |
* Class: java_net_PlainDatagramSocketImpl |
|
893 |
* Method: datagramSocketCreate |
|
894 |
* Signature: ()V |
|
895 |
*/ |
|
896 |
JNIEXPORT void JNICALL |
|
897 |
Java_java_net_PlainDatagramSocketImpl_datagramSocketCreate(JNIEnv *env, |
|
898 |
jobject this) { |
|
899 |
jobject fdObj = (*env)->GetObjectField(env, this, pdsi_fdID); |
|
12047
320a714614e9
7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
9035
diff
changeset
|
900 |
int arg, fd, t = 1; |
32846
5383225ebd0d
8133249: Occasional SIGSEGV: non thread-safe use of strerr in getLastErrorString
robm
parents:
32652
diff
changeset
|
901 |
char tmpbuf[1024]; |
6299
a44d48dbcd50
6882910: Unexplained lack of IP4 network ability when transparent IP6 to IP4 is disabled.
chegar
parents:
5506
diff
changeset
|
902 |
int domain = ipv6_available() ? AF_INET6 : AF_INET; |
2 | 903 |
|
904 |
if (IS_NULL(fdObj)) { |
|
905 |
JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException", |
|
906 |
"Socket closed"); |
|
907 |
return; |
|
908 |
} |
|
6299
a44d48dbcd50
6882910: Unexplained lack of IP4 network ability when transparent IP6 to IP4 is disabled.
chegar
parents:
5506
diff
changeset
|
909 |
|
23015
73b21ab36615
8034174: Remove use of JVM_* functions from java.net code
chegar
parents:
23010
diff
changeset
|
910 |
if ((fd = socket(domain, SOCK_DGRAM, 0)) == -1) { |
39318
2006d1d41c8b
8158023: SocketExceptions contain too little information sometimes
clanger
parents:
36115
diff
changeset
|
911 |
JNU_ThrowByNameWithMessageAndLastError |
2006d1d41c8b
8158023: SocketExceptions contain too little information sometimes
clanger
parents:
36115
diff
changeset
|
912 |
(env, JNU_JAVANETPKG "SocketException", "Error creating socket"); |
2 | 913 |
return; |
914 |
} |
|
915 |
||
6299
a44d48dbcd50
6882910: Unexplained lack of IP4 network ability when transparent IP6 to IP4 is disabled.
chegar
parents:
5506
diff
changeset
|
916 |
/* Disable IPV6_V6ONLY to ensure dual-socket support */ |
a44d48dbcd50
6882910: Unexplained lack of IP4 network ability when transparent IP6 to IP4 is disabled.
chegar
parents:
5506
diff
changeset
|
917 |
if (domain == AF_INET6) { |
12047
320a714614e9
7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
9035
diff
changeset
|
918 |
arg = 0; |
6299
a44d48dbcd50
6882910: Unexplained lack of IP4 network ability when transparent IP6 to IP4 is disabled.
chegar
parents:
5506
diff
changeset
|
919 |
if (setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, (char*)&arg, |
a44d48dbcd50
6882910: Unexplained lack of IP4 network ability when transparent IP6 to IP4 is disabled.
chegar
parents:
5506
diff
changeset
|
920 |
sizeof(int)) < 0) { |
a44d48dbcd50
6882910: Unexplained lack of IP4 network ability when transparent IP6 to IP4 is disabled.
chegar
parents:
5506
diff
changeset
|
921 |
NET_ThrowNew(env, errno, "cannot set IPPROTO_IPV6"); |
a44d48dbcd50
6882910: Unexplained lack of IP4 network ability when transparent IP6 to IP4 is disabled.
chegar
parents:
5506
diff
changeset
|
922 |
close(fd); |
a44d48dbcd50
6882910: Unexplained lack of IP4 network ability when transparent IP6 to IP4 is disabled.
chegar
parents:
5506
diff
changeset
|
923 |
return; |
a44d48dbcd50
6882910: Unexplained lack of IP4 network ability when transparent IP6 to IP4 is disabled.
chegar
parents:
5506
diff
changeset
|
924 |
} |
a44d48dbcd50
6882910: Unexplained lack of IP4 network ability when transparent IP6 to IP4 is disabled.
chegar
parents:
5506
diff
changeset
|
925 |
} |
a44d48dbcd50
6882910: Unexplained lack of IP4 network ability when transparent IP6 to IP4 is disabled.
chegar
parents:
5506
diff
changeset
|
926 |
|
12047
320a714614e9
7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
9035
diff
changeset
|
927 |
#ifdef __APPLE__ |
320a714614e9
7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
9035
diff
changeset
|
928 |
arg = 65507; |
23015
73b21ab36615
8034174: Remove use of JVM_* functions from java.net code
chegar
parents:
23010
diff
changeset
|
929 |
if (setsockopt(fd, SOL_SOCKET, SO_SNDBUF, |
73b21ab36615
8034174: Remove use of JVM_* functions from java.net code
chegar
parents:
23010
diff
changeset
|
930 |
(char *)&arg, sizeof(arg)) < 0) { |
32846
5383225ebd0d
8133249: Occasional SIGSEGV: non thread-safe use of strerr in getLastErrorString
robm
parents:
32652
diff
changeset
|
931 |
getErrorString(errno, tmpbuf, sizeof(tmpbuf)); |
5383225ebd0d
8133249: Occasional SIGSEGV: non thread-safe use of strerr in getLastErrorString
robm
parents:
32652
diff
changeset
|
932 |
JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException", tmpbuf); |
32652
aed323c3a01d
8073542: File Leak in jdk/src/java/base/unix/native/libnet/PlainDatagramSocketImpl.c
dfuchs
parents:
32503
diff
changeset
|
933 |
close(fd); |
12047
320a714614e9
7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
9035
diff
changeset
|
934 |
return; |
320a714614e9
7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
9035
diff
changeset
|
935 |
} |
23015
73b21ab36615
8034174: Remove use of JVM_* functions from java.net code
chegar
parents:
23010
diff
changeset
|
936 |
if (setsockopt(fd, SOL_SOCKET, SO_RCVBUF, |
73b21ab36615
8034174: Remove use of JVM_* functions from java.net code
chegar
parents:
23010
diff
changeset
|
937 |
(char *)&arg, sizeof(arg)) < 0) { |
32846
5383225ebd0d
8133249: Occasional SIGSEGV: non thread-safe use of strerr in getLastErrorString
robm
parents:
32652
diff
changeset
|
938 |
getErrorString(errno, tmpbuf, sizeof(tmpbuf)); |
5383225ebd0d
8133249: Occasional SIGSEGV: non thread-safe use of strerr in getLastErrorString
robm
parents:
32652
diff
changeset
|
939 |
JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException", tmpbuf); |
32652
aed323c3a01d
8073542: File Leak in jdk/src/java/base/unix/native/libnet/PlainDatagramSocketImpl.c
dfuchs
parents:
32503
diff
changeset
|
940 |
close(fd); |
12047
320a714614e9
7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
9035
diff
changeset
|
941 |
return; |
320a714614e9
7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
9035
diff
changeset
|
942 |
} |
320a714614e9
7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
9035
diff
changeset
|
943 |
#endif /* __APPLE__ */ |
320a714614e9
7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
9035
diff
changeset
|
944 |
|
32652
aed323c3a01d
8073542: File Leak in jdk/src/java/base/unix/native/libnet/PlainDatagramSocketImpl.c
dfuchs
parents:
32503
diff
changeset
|
945 |
if (setsockopt(fd, SOL_SOCKET, SO_BROADCAST, (char*) &t, sizeof (int)) < 0) { |
32846
5383225ebd0d
8133249: Occasional SIGSEGV: non thread-safe use of strerr in getLastErrorString
robm
parents:
32652
diff
changeset
|
946 |
getErrorString(errno, tmpbuf, sizeof(tmpbuf)); |
5383225ebd0d
8133249: Occasional SIGSEGV: non thread-safe use of strerr in getLastErrorString
robm
parents:
32652
diff
changeset
|
947 |
JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException", tmpbuf); |
32652
aed323c3a01d
8073542: File Leak in jdk/src/java/base/unix/native/libnet/PlainDatagramSocketImpl.c
dfuchs
parents:
32503
diff
changeset
|
948 |
close(fd); |
aed323c3a01d
8073542: File Leak in jdk/src/java/base/unix/native/libnet/PlainDatagramSocketImpl.c
dfuchs
parents:
32503
diff
changeset
|
949 |
return; |
aed323c3a01d
8073542: File Leak in jdk/src/java/base/unix/native/libnet/PlainDatagramSocketImpl.c
dfuchs
parents:
32503
diff
changeset
|
950 |
} |
2 | 951 |
|
18534
2d131cf5db63
8014499: MulticastSocket should enable IP_MULTICAST_ALL (lnx)
chegar
parents:
16870
diff
changeset
|
952 |
#if defined(__linux__) |
2d131cf5db63
8014499: MulticastSocket should enable IP_MULTICAST_ALL (lnx)
chegar
parents:
16870
diff
changeset
|
953 |
arg = 0; |
2d131cf5db63
8014499: MulticastSocket should enable IP_MULTICAST_ALL (lnx)
chegar
parents:
16870
diff
changeset
|
954 |
int level = (domain == AF_INET6) ? IPPROTO_IPV6 : IPPROTO_IP; |
2d131cf5db63
8014499: MulticastSocket should enable IP_MULTICAST_ALL (lnx)
chegar
parents:
16870
diff
changeset
|
955 |
if ((setsockopt(fd, level, IP_MULTICAST_ALL, (char*)&arg, sizeof(arg)) < 0) && |
32846
5383225ebd0d
8133249: Occasional SIGSEGV: non thread-safe use of strerr in getLastErrorString
robm
parents:
32652
diff
changeset
|
956 |
(errno != ENOPROTOOPT)) |
5383225ebd0d
8133249: Occasional SIGSEGV: non thread-safe use of strerr in getLastErrorString
robm
parents:
32652
diff
changeset
|
957 |
{ |
5383225ebd0d
8133249: Occasional SIGSEGV: non thread-safe use of strerr in getLastErrorString
robm
parents:
32652
diff
changeset
|
958 |
getErrorString(errno, tmpbuf, sizeof(tmpbuf)); |
5383225ebd0d
8133249: Occasional SIGSEGV: non thread-safe use of strerr in getLastErrorString
robm
parents:
32652
diff
changeset
|
959 |
JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException", tmpbuf); |
18534
2d131cf5db63
8014499: MulticastSocket should enable IP_MULTICAST_ALL (lnx)
chegar
parents:
16870
diff
changeset
|
960 |
close(fd); |
2d131cf5db63
8014499: MulticastSocket should enable IP_MULTICAST_ALL (lnx)
chegar
parents:
16870
diff
changeset
|
961 |
return; |
2d131cf5db63
8014499: MulticastSocket should enable IP_MULTICAST_ALL (lnx)
chegar
parents:
16870
diff
changeset
|
962 |
} |
2d131cf5db63
8014499: MulticastSocket should enable IP_MULTICAST_ALL (lnx)
chegar
parents:
16870
diff
changeset
|
963 |
#endif |
2d131cf5db63
8014499: MulticastSocket should enable IP_MULTICAST_ALL (lnx)
chegar
parents:
16870
diff
changeset
|
964 |
|
41834
99c4f72c0a91
8168771: Remove #ifdef AF_INET6 guards in libnet native coding
clanger
parents:
41771
diff
changeset
|
965 |
#if defined (__linux__) |
2 | 966 |
/* |
967 |
* On Linux for IPv6 sockets we must set the hop limit |
|
13999
f6a2ce6a3e40
7193520: Removed references to Linux kernel version 2.2
chegar
parents:
13574
diff
changeset
|
968 |
* to 1 to be compatible with default TTL of 1 for IPv4 sockets. |
2 | 969 |
*/ |
6299
a44d48dbcd50
6882910: Unexplained lack of IP4 network ability when transparent IP6 to IP4 is disabled.
chegar
parents:
5506
diff
changeset
|
970 |
if (domain == AF_INET6) { |
2 | 971 |
int ttl = 1; |
32652
aed323c3a01d
8073542: File Leak in jdk/src/java/base/unix/native/libnet/PlainDatagramSocketImpl.c
dfuchs
parents:
32503
diff
changeset
|
972 |
if (setsockopt(fd, IPPROTO_IPV6, IPV6_MULTICAST_HOPS, (char *) &ttl, |
aed323c3a01d
8073542: File Leak in jdk/src/java/base/unix/native/libnet/PlainDatagramSocketImpl.c
dfuchs
parents:
32503
diff
changeset
|
973 |
sizeof (ttl)) < 0) { |
32846
5383225ebd0d
8133249: Occasional SIGSEGV: non thread-safe use of strerr in getLastErrorString
robm
parents:
32652
diff
changeset
|
974 |
getErrorString(errno, tmpbuf, sizeof(tmpbuf)); |
5383225ebd0d
8133249: Occasional SIGSEGV: non thread-safe use of strerr in getLastErrorString
robm
parents:
32652
diff
changeset
|
975 |
JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException", tmpbuf); |
32652
aed323c3a01d
8073542: File Leak in jdk/src/java/base/unix/native/libnet/PlainDatagramSocketImpl.c
dfuchs
parents:
32503
diff
changeset
|
976 |
close(fd); |
aed323c3a01d
8073542: File Leak in jdk/src/java/base/unix/native/libnet/PlainDatagramSocketImpl.c
dfuchs
parents:
32503
diff
changeset
|
977 |
return; |
aed323c3a01d
8073542: File Leak in jdk/src/java/base/unix/native/libnet/PlainDatagramSocketImpl.c
dfuchs
parents:
32503
diff
changeset
|
978 |
} |
2 | 979 |
} |
980 |
#endif /* __linux__ */ |
|
981 |
||
982 |
(*env)->SetIntField(env, fdObj, IO_fd_fdID, fd); |
|
983 |
} |
|
984 |
||
985 |
/* |
|
986 |
* Class: java_net_PlainDatagramSocketImpl |
|
987 |
* Method: datagramSocketClose |
|
988 |
* Signature: ()V |
|
989 |
*/ |
|
990 |
JNIEXPORT void JNICALL |
|
991 |
Java_java_net_PlainDatagramSocketImpl_datagramSocketClose(JNIEnv *env, |
|
992 |
jobject this) { |
|
993 |
/* |
|
994 |
* REMIND: PUT A LOCK AROUND THIS CODE |
|
995 |
*/ |
|
996 |
jobject fdObj = (*env)->GetObjectField(env, this, pdsi_fdID); |
|
997 |
int fd; |
|
998 |
||
999 |
if (IS_NULL(fdObj)) { |
|
1000 |
return; |
|
1001 |
} |
|
1002 |
fd = (*env)->GetIntField(env, fdObj, IO_fd_fdID); |
|
1003 |
if (fd == -1) { |
|
1004 |
return; |
|
1005 |
} |
|
1006 |
(*env)->SetIntField(env, fdObj, IO_fd_fdID, -1); |
|
1007 |
NET_SocketClose(fd); |
|
1008 |
} |
|
1009 |
||
1010 |
||
1011 |
/* |
|
1012 |
* Set outgoing multicast interface designated by a NetworkInterface. |
|
1013 |
* Throw exception if failed. |
|
1014 |
*/ |
|
1015 |
static void mcast_set_if_by_if_v4(JNIEnv *env, jobject this, int fd, jobject value) { |
|
1016 |
static jfieldID ni_addrsID; |
|
1017 |
struct in_addr in; |
|
1018 |
jobjectArray addrArray; |
|
1019 |
jsize len; |
|
1020 |
jobject addr; |
|
1021 |
int i; |
|
1022 |
||
16870 | 1023 |
if (ni_addrsID == NULL ) { |
2 | 1024 |
jclass c = (*env)->FindClass(env, "java/net/NetworkInterface"); |
1025 |
CHECK_NULL(c); |
|
1026 |
ni_addrsID = (*env)->GetFieldID(env, c, "addrs", |
|
1027 |
"[Ljava/net/InetAddress;"); |
|
1028 |
CHECK_NULL(ni_addrsID); |
|
1029 |
} |
|
1030 |
||
1031 |
addrArray = (*env)->GetObjectField(env, value, ni_addrsID); |
|
1032 |
len = (*env)->GetArrayLength(env, addrArray); |
|
1033 |
||
1034 |
/* |
|
1035 |
* Check that there is at least one address bound to this |
|
1036 |
* interface. |
|
1037 |
*/ |
|
1038 |
if (len < 1) { |
|
1039 |
JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException", |
|
1040 |
"bad argument for IP_MULTICAST_IF2: No IP addresses bound to interface"); |
|
1041 |
return; |
|
1042 |
} |
|
1043 |
||
1044 |
/* |
|
1045 |
* We need an ipv4 address here |
|
1046 |
*/ |
|
43100 | 1047 |
in.s_addr = 0; |
2 | 1048 |
for (i = 0; i < len; i++) { |
1049 |
addr = (*env)->GetObjectArrayElement(env, addrArray, i); |
|
41771
18c9669e76ca
8167481: cleanup of headers and includes for native libnet
clanger
parents:
41380
diff
changeset
|
1050 |
if (getInetAddress_family(env, addr) == java_net_InetAddress_IPv4) { |
51151 | 1051 |
JNU_CHECK_EXCEPTION(env); |
16870 | 1052 |
in.s_addr = htonl(getInetAddress_addr(env, addr)); |
51151 | 1053 |
JNU_CHECK_EXCEPTION(env); |
2 | 1054 |
break; |
1055 |
} |
|
1056 |
} |
|
1057 |
||
23015
73b21ab36615
8034174: Remove use of JVM_* functions from java.net code
chegar
parents:
23010
diff
changeset
|
1058 |
if (setsockopt(fd, IPPROTO_IP, IP_MULTICAST_IF, |
43100 | 1059 |
(const char *)&in, sizeof(in)) < 0) { |
39318
2006d1d41c8b
8158023: SocketExceptions contain too little information sometimes
clanger
parents:
36115
diff
changeset
|
1060 |
JNU_ThrowByNameWithMessageAndLastError |
2006d1d41c8b
8158023: SocketExceptions contain too little information sometimes
clanger
parents:
36115
diff
changeset
|
1061 |
(env, JNU_JAVANETPKG "SocketException", "Error setting socket option"); |
2 | 1062 |
} |
1063 |
} |
|
1064 |
||
1065 |
/* |
|
1066 |
* Set outgoing multicast interface designated by a NetworkInterface. |
|
1067 |
* Throw exception if failed. |
|
1068 |
*/ |
|
1069 |
static void mcast_set_if_by_if_v6(JNIEnv *env, jobject this, int fd, jobject value) { |
|
1070 |
static jfieldID ni_indexID; |
|
1071 |
int index; |
|
1072 |
||
1073 |
if (ni_indexID == NULL) { |
|
1074 |
jclass c = (*env)->FindClass(env, "java/net/NetworkInterface"); |
|
1075 |
CHECK_NULL(c); |
|
1076 |
ni_indexID = (*env)->GetFieldID(env, c, "index", "I"); |
|
1077 |
CHECK_NULL(ni_indexID); |
|
1078 |
} |
|
1079 |
index = (*env)->GetIntField(env, value, ni_indexID); |
|
1080 |
||
23015
73b21ab36615
8034174: Remove use of JVM_* functions from java.net code
chegar
parents:
23010
diff
changeset
|
1081 |
if (setsockopt(fd, IPPROTO_IPV6, IPV6_MULTICAST_IF, |
73b21ab36615
8034174: Remove use of JVM_* functions from java.net code
chegar
parents:
23010
diff
changeset
|
1082 |
(const char*)&index, sizeof(index)) < 0) { |
2 | 1083 |
if (errno == EINVAL && index > 0) { |
1084 |
JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException", |
|
1085 |
"IPV6_MULTICAST_IF failed (interface has IPv4 " |
|
1086 |
"address only?)"); |
|
1087 |
} else { |
|
39318
2006d1d41c8b
8158023: SocketExceptions contain too little information sometimes
clanger
parents:
36115
diff
changeset
|
1088 |
JNU_ThrowByNameWithMessageAndLastError |
2006d1d41c8b
8158023: SocketExceptions contain too little information sometimes
clanger
parents:
36115
diff
changeset
|
1089 |
(env, JNU_JAVANETPKG "SocketException", "Error setting socket option"); |
2 | 1090 |
} |
1091 |
return; |
|
1092 |
} |
|
1093 |
} |
|
1094 |
||
1095 |
/* |
|
1096 |
* Set outgoing multicast interface designated by an InetAddress. |
|
1097 |
* Throw exception if failed. |
|
1098 |
*/ |
|
1099 |
static void mcast_set_if_by_addr_v4(JNIEnv *env, jobject this, int fd, jobject value) { |
|
1100 |
struct in_addr in; |
|
1101 |
||
16870 | 1102 |
in.s_addr = htonl( getInetAddress_addr(env, value) ); |
51151 | 1103 |
JNU_CHECK_EXCEPTION(env); |
23015
73b21ab36615
8034174: Remove use of JVM_* functions from java.net code
chegar
parents:
23010
diff
changeset
|
1104 |
if (setsockopt(fd, IPPROTO_IP, IP_MULTICAST_IF, |
73b21ab36615
8034174: Remove use of JVM_* functions from java.net code
chegar
parents:
23010
diff
changeset
|
1105 |
(const char*)&in, sizeof(in)) < 0) { |
39318
2006d1d41c8b
8158023: SocketExceptions contain too little information sometimes
clanger
parents:
36115
diff
changeset
|
1106 |
JNU_ThrowByNameWithMessageAndLastError |
2006d1d41c8b
8158023: SocketExceptions contain too little information sometimes
clanger
parents:
36115
diff
changeset
|
1107 |
(env, JNU_JAVANETPKG "SocketException", "Error setting socket option"); |
2 | 1108 |
} |
1109 |
} |
|
1110 |
||
1111 |
/* |
|
1112 |
* Set outgoing multicast interface designated by an InetAddress. |
|
1113 |
* Throw exception if failed. |
|
1114 |
*/ |
|
1115 |
static void mcast_set_if_by_addr_v6(JNIEnv *env, jobject this, int fd, jobject value) { |
|
1116 |
static jclass ni_class; |
|
1117 |
if (ni_class == NULL) { |
|
1118 |
jclass c = (*env)->FindClass(env, "java/net/NetworkInterface"); |
|
1119 |
CHECK_NULL(c); |
|
1120 |
ni_class = (*env)->NewGlobalRef(env, c); |
|
1121 |
CHECK_NULL(ni_class); |
|
1122 |
} |
|
1123 |
||
1124 |
value = Java_java_net_NetworkInterface_getByInetAddress0(env, ni_class, value); |
|
1125 |
if (value == NULL) { |
|
1126 |
if (!(*env)->ExceptionOccurred(env)) { |
|
1127 |
JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException", |
|
1128 |
"bad argument for IP_MULTICAST_IF" |
|
1129 |
": address not bound to any interface"); |
|
1130 |
} |
|
1131 |
return; |
|
1132 |
} |
|
1133 |
||
1134 |
mcast_set_if_by_if_v6(env, this, fd, value); |
|
1135 |
} |
|
1136 |
||
1137 |
/* |
|
1138 |
* Sets the multicast interface. |
|
1139 |
* |
|
1140 |
* SocketOptions.IP_MULTICAST_IF :- |
|
1141 |
* value is a InetAddress |
|
1142 |
* IPv4: set outgoing multicast interface using |
|
1143 |
* IPPROTO_IP/IP_MULTICAST_IF |
|
1144 |
* IPv6: Get the index of the interface to which the |
|
1145 |
* InetAddress is bound |
|
1146 |
* Set outgoing multicast interface using |
|
1147 |
* IPPROTO_IPV6/IPV6_MULTICAST_IF |
|
1148 |
* |
|
1149 |
* SockOptions.IF_MULTICAST_IF2 :- |
|
1150 |
* value is a NetworkInterface |
|
1151 |
* IPv4: Obtain IP address bound to network interface |
|
1152 |
* (NetworkInterface.addres[0]) |
|
1153 |
* set outgoing multicast interface using |
|
1154 |
* IPPROTO_IP/IP_MULTICAST_IF |
|
1155 |
* IPv6: Obtain NetworkInterface.index |
|
1156 |
* Set outgoing multicast interface using |
|
1157 |
* IPPROTO_IPV6/IPV6_MULTICAST_IF |
|
1158 |
* |
|
1159 |
*/ |
|
1160 |
static void setMulticastInterface(JNIEnv *env, jobject this, int fd, |
|
1161 |
jint opt, jobject value) |
|
1162 |
{ |
|
1163 |
if (opt == java_net_SocketOptions_IP_MULTICAST_IF) { |
|
1164 |
/* |
|
1165 |
* value is an InetAddress. |
|
1166 |
*/ |
|
13574
9d58db93e184
7191275: Cleanup OS specific blocks in PlainDatagramSocketImpl.c to support more unix-like platforms
dingxmin
parents:
13245
diff
changeset
|
1167 |
#ifdef __linux__ |
9d58db93e184
7191275: Cleanup OS specific blocks in PlainDatagramSocketImpl.c to support more unix-like platforms
dingxmin
parents:
13245
diff
changeset
|
1168 |
mcast_set_if_by_addr_v4(env, this, fd, value); |
9d58db93e184
7191275: Cleanup OS specific blocks in PlainDatagramSocketImpl.c to support more unix-like platforms
dingxmin
parents:
13245
diff
changeset
|
1169 |
if (ipv6_available()) { |
22095
e613710a08a6
8027903: java/net/MulticastSocket/SetGetNetworkInterfaceTest.java throws java.net.SocketException: Cannot assign requested address
msheppar
parents:
20821
diff
changeset
|
1170 |
if ((*env)->ExceptionCheck(env)){ |
e613710a08a6
8027903: java/net/MulticastSocket/SetGetNetworkInterfaceTest.java throws java.net.SocketException: Cannot assign requested address
msheppar
parents:
20821
diff
changeset
|
1171 |
(*env)->ExceptionClear(env); |
e613710a08a6
8027903: java/net/MulticastSocket/SetGetNetworkInterfaceTest.java throws java.net.SocketException: Cannot assign requested address
msheppar
parents:
20821
diff
changeset
|
1172 |
} |
13574
9d58db93e184
7191275: Cleanup OS specific blocks in PlainDatagramSocketImpl.c to support more unix-like platforms
dingxmin
parents:
13245
diff
changeset
|
1173 |
mcast_set_if_by_addr_v6(env, this, fd, value); |
9d58db93e184
7191275: Cleanup OS specific blocks in PlainDatagramSocketImpl.c to support more unix-like platforms
dingxmin
parents:
13245
diff
changeset
|
1174 |
} |
9d58db93e184
7191275: Cleanup OS specific blocks in PlainDatagramSocketImpl.c to support more unix-like platforms
dingxmin
parents:
13245
diff
changeset
|
1175 |
#else /* __linux__ not defined */ |
2 | 1176 |
if (ipv6_available()) { |
1177 |
mcast_set_if_by_addr_v6(env, this, fd, value); |
|
1178 |
} else { |
|
1179 |
mcast_set_if_by_addr_v4(env, this, fd, value); |
|
1180 |
} |
|
13574
9d58db93e184
7191275: Cleanup OS specific blocks in PlainDatagramSocketImpl.c to support more unix-like platforms
dingxmin
parents:
13245
diff
changeset
|
1181 |
#endif /* __linux__ */ |
2 | 1182 |
} |
1183 |
||
1184 |
if (opt == java_net_SocketOptions_IP_MULTICAST_IF2) { |
|
1185 |
/* |
|
1186 |
* value is a NetworkInterface. |
|
1187 |
*/ |
|
13574
9d58db93e184
7191275: Cleanup OS specific blocks in PlainDatagramSocketImpl.c to support more unix-like platforms
dingxmin
parents:
13245
diff
changeset
|
1188 |
#ifdef __linux__ |
9d58db93e184
7191275: Cleanup OS specific blocks in PlainDatagramSocketImpl.c to support more unix-like platforms
dingxmin
parents:
13245
diff
changeset
|
1189 |
mcast_set_if_by_if_v4(env, this, fd, value); |
9d58db93e184
7191275: Cleanup OS specific blocks in PlainDatagramSocketImpl.c to support more unix-like platforms
dingxmin
parents:
13245
diff
changeset
|
1190 |
if (ipv6_available()) { |
22095
e613710a08a6
8027903: java/net/MulticastSocket/SetGetNetworkInterfaceTest.java throws java.net.SocketException: Cannot assign requested address
msheppar
parents:
20821
diff
changeset
|
1191 |
if ((*env)->ExceptionCheck(env)){ |
e613710a08a6
8027903: java/net/MulticastSocket/SetGetNetworkInterfaceTest.java throws java.net.SocketException: Cannot assign requested address
msheppar
parents:
20821
diff
changeset
|
1192 |
(*env)->ExceptionClear(env); |
e613710a08a6
8027903: java/net/MulticastSocket/SetGetNetworkInterfaceTest.java throws java.net.SocketException: Cannot assign requested address
msheppar
parents:
20821
diff
changeset
|
1193 |
} |
13574
9d58db93e184
7191275: Cleanup OS specific blocks in PlainDatagramSocketImpl.c to support more unix-like platforms
dingxmin
parents:
13245
diff
changeset
|
1194 |
mcast_set_if_by_if_v6(env, this, fd, value); |
9d58db93e184
7191275: Cleanup OS specific blocks in PlainDatagramSocketImpl.c to support more unix-like platforms
dingxmin
parents:
13245
diff
changeset
|
1195 |
} |
9d58db93e184
7191275: Cleanup OS specific blocks in PlainDatagramSocketImpl.c to support more unix-like platforms
dingxmin
parents:
13245
diff
changeset
|
1196 |
#else /* __linux__ not defined */ |
2 | 1197 |
if (ipv6_available()) { |
1198 |
mcast_set_if_by_if_v6(env, this, fd, value); |
|
1199 |
} else { |
|
1200 |
mcast_set_if_by_if_v4(env, this, fd, value); |
|
1201 |
} |
|
13574
9d58db93e184
7191275: Cleanup OS specific blocks in PlainDatagramSocketImpl.c to support more unix-like platforms
dingxmin
parents:
13245
diff
changeset
|
1202 |
#endif /* __linux__ */ |
2 | 1203 |
} |
1204 |
} |
|
1205 |
||
1206 |
/* |
|
1207 |
* Enable/disable local loopback of multicast datagrams. |
|
1208 |
*/ |
|
1209 |
static void mcast_set_loop_v4(JNIEnv *env, jobject this, int fd, jobject value) { |
|
1210 |
jclass cls; |
|
1211 |
jfieldID fid; |
|
1212 |
jboolean on; |
|
1213 |
char loopback; |
|
1214 |
||
1215 |
cls = (*env)->FindClass(env, "java/lang/Boolean"); |
|
1216 |
CHECK_NULL(cls); |
|
1217 |
fid = (*env)->GetFieldID(env, cls, "value", "Z"); |
|
1218 |
CHECK_NULL(fid); |
|
1219 |
||
1220 |
on = (*env)->GetBooleanField(env, value, fid); |
|
1221 |
loopback = (!on ? 1 : 0); |
|
1222 |
||
39318
2006d1d41c8b
8158023: SocketExceptions contain too little information sometimes
clanger
parents:
36115
diff
changeset
|
1223 |
if (NET_SetSockOpt(fd, IPPROTO_IP, IP_MULTICAST_LOOP, |
2006d1d41c8b
8158023: SocketExceptions contain too little information sometimes
clanger
parents:
36115
diff
changeset
|
1224 |
(const void *)&loopback, sizeof(char)) < 0) { |
2006d1d41c8b
8158023: SocketExceptions contain too little information sometimes
clanger
parents:
36115
diff
changeset
|
1225 |
JNU_ThrowByNameWithMessageAndLastError |
2006d1d41c8b
8158023: SocketExceptions contain too little information sometimes
clanger
parents:
36115
diff
changeset
|
1226 |
(env, JNU_JAVANETPKG "SocketException", "Error setting socket option"); |
2 | 1227 |
return; |
1228 |
} |
|
1229 |
} |
|
1230 |
||
1231 |
/* |
|
1232 |
* Enable/disable local loopback of multicast datagrams. |
|
1233 |
*/ |
|
1234 |
static void mcast_set_loop_v6(JNIEnv *env, jobject this, int fd, jobject value) { |
|
1235 |
jclass cls; |
|
1236 |
jfieldID fid; |
|
1237 |
jboolean on; |
|
1238 |
int loopback; |
|
1239 |
||
1240 |
cls = (*env)->FindClass(env, "java/lang/Boolean"); |
|
1241 |
CHECK_NULL(cls); |
|
1242 |
fid = (*env)->GetFieldID(env, cls, "value", "Z"); |
|
1243 |
CHECK_NULL(fid); |
|
1244 |
||
1245 |
on = (*env)->GetBooleanField(env, value, fid); |
|
1246 |
loopback = (!on ? 1 : 0); |
|
1247 |
||
39318
2006d1d41c8b
8158023: SocketExceptions contain too little information sometimes
clanger
parents:
36115
diff
changeset
|
1248 |
if (NET_SetSockOpt(fd, IPPROTO_IPV6, IPV6_MULTICAST_LOOP, |
2006d1d41c8b
8158023: SocketExceptions contain too little information sometimes
clanger
parents:
36115
diff
changeset
|
1249 |
(const void *)&loopback, sizeof(int)) < 0) { |
2006d1d41c8b
8158023: SocketExceptions contain too little information sometimes
clanger
parents:
36115
diff
changeset
|
1250 |
JNU_ThrowByNameWithMessageAndLastError |
2006d1d41c8b
8158023: SocketExceptions contain too little information sometimes
clanger
parents:
36115
diff
changeset
|
1251 |
(env, JNU_JAVANETPKG "SocketException", "Error setting socket option"); |
2 | 1252 |
return; |
1253 |
} |
|
1254 |
||
1255 |
} |
|
1256 |
||
1257 |
/* |
|
1258 |
* Sets the multicast loopback mode. |
|
1259 |
*/ |
|
1260 |
static void setMulticastLoopbackMode(JNIEnv *env, jobject this, int fd, |
|
41214
855ac576eb77
8166850: No runtime error expected after calling NET_MapSocketOption
clanger
parents:
39318
diff
changeset
|
1261 |
jint opt, jobject value) { |
13574
9d58db93e184
7191275: Cleanup OS specific blocks in PlainDatagramSocketImpl.c to support more unix-like platforms
dingxmin
parents:
13245
diff
changeset
|
1262 |
#ifdef __linux__ |
9d58db93e184
7191275: Cleanup OS specific blocks in PlainDatagramSocketImpl.c to support more unix-like platforms
dingxmin
parents:
13245
diff
changeset
|
1263 |
mcast_set_loop_v4(env, this, fd, value); |
9d58db93e184
7191275: Cleanup OS specific blocks in PlainDatagramSocketImpl.c to support more unix-like platforms
dingxmin
parents:
13245
diff
changeset
|
1264 |
if (ipv6_available()) { |
22095
e613710a08a6
8027903: java/net/MulticastSocket/SetGetNetworkInterfaceTest.java throws java.net.SocketException: Cannot assign requested address
msheppar
parents:
20821
diff
changeset
|
1265 |
if ((*env)->ExceptionCheck(env)){ |
e613710a08a6
8027903: java/net/MulticastSocket/SetGetNetworkInterfaceTest.java throws java.net.SocketException: Cannot assign requested address
msheppar
parents:
20821
diff
changeset
|
1266 |
(*env)->ExceptionClear(env); |
e613710a08a6
8027903: java/net/MulticastSocket/SetGetNetworkInterfaceTest.java throws java.net.SocketException: Cannot assign requested address
msheppar
parents:
20821
diff
changeset
|
1267 |
} |
13574
9d58db93e184
7191275: Cleanup OS specific blocks in PlainDatagramSocketImpl.c to support more unix-like platforms
dingxmin
parents:
13245
diff
changeset
|
1268 |
mcast_set_loop_v6(env, this, fd, value); |
9d58db93e184
7191275: Cleanup OS specific blocks in PlainDatagramSocketImpl.c to support more unix-like platforms
dingxmin
parents:
13245
diff
changeset
|
1269 |
} |
9d58db93e184
7191275: Cleanup OS specific blocks in PlainDatagramSocketImpl.c to support more unix-like platforms
dingxmin
parents:
13245
diff
changeset
|
1270 |
#else /* __linux__ not defined */ |
2 | 1271 |
if (ipv6_available()) { |
1272 |
mcast_set_loop_v6(env, this, fd, value); |
|
1273 |
} else { |
|
1274 |
mcast_set_loop_v4(env, this, fd, value); |
|
1275 |
} |
|
13574
9d58db93e184
7191275: Cleanup OS specific blocks in PlainDatagramSocketImpl.c to support more unix-like platforms
dingxmin
parents:
13245
diff
changeset
|
1276 |
#endif /* __linux__ */ |
2 | 1277 |
} |
1278 |
||
1279 |
/* |
|
1280 |
* Class: java_net_PlainDatagramSocketImpl |
|
30963
88469d06e03f
8072384: Setting IP_TOS on java.net sockets not working on unix
coffeys
parents:
28549
diff
changeset
|
1281 |
* Method: socketSetOption0 |
2 | 1282 |
* Signature: (ILjava/lang/Object;)V |
1283 |
*/ |
|
1284 |
JNIEXPORT void JNICALL |
|
41214
855ac576eb77
8166850: No runtime error expected after calling NET_MapSocketOption
clanger
parents:
39318
diff
changeset
|
1285 |
Java_java_net_PlainDatagramSocketImpl_socketSetOption0 |
855ac576eb77
8166850: No runtime error expected after calling NET_MapSocketOption
clanger
parents:
39318
diff
changeset
|
1286 |
(JNIEnv *env, jobject this, jint opt, jobject value) |
855ac576eb77
8166850: No runtime error expected after calling NET_MapSocketOption
clanger
parents:
39318
diff
changeset
|
1287 |
{ |
2 | 1288 |
int fd; |
1289 |
int level, optname, optlen; |
|
14195
57fe6c437882
8000206: Uninitialized variable in PlainDatagramSocketImpl.c
chegar
parents:
13999
diff
changeset
|
1290 |
int optval; |
57fe6c437882
8000206: Uninitialized variable in PlainDatagramSocketImpl.c
chegar
parents:
13999
diff
changeset
|
1291 |
optlen = sizeof(int); |
2 | 1292 |
|
1293 |
/* |
|
1294 |
* Check that socket hasn't been closed |
|
1295 |
*/ |
|
1296 |
fd = getFD(env, this); |
|
1297 |
if (fd < 0) { |
|
1298 |
JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException", |
|
1299 |
"Socket closed"); |
|
1300 |
return; |
|
1301 |
} |
|
1302 |
||
1303 |
/* |
|
1304 |
* Check argument has been provided |
|
1305 |
*/ |
|
1306 |
if (IS_NULL(value)) { |
|
1307 |
JNU_ThrowNullPointerException(env, "value argument"); |
|
1308 |
return; |
|
1309 |
} |
|
1310 |
||
1311 |
/* |
|
13999
f6a2ce6a3e40
7193520: Removed references to Linux kernel version 2.2
chegar
parents:
13574
diff
changeset
|
1312 |
* Setting the multicast interface handled separately |
2 | 1313 |
*/ |
1314 |
if (opt == java_net_SocketOptions_IP_MULTICAST_IF || |
|
1315 |
opt == java_net_SocketOptions_IP_MULTICAST_IF2) { |
|
1316 |
||
1317 |
setMulticastInterface(env, this, fd, opt, value); |
|
1318 |
return; |
|
1319 |
} |
|
1320 |
||
1321 |
/* |
|
1322 |
* Setting the multicast loopback mode handled separately |
|
1323 |
*/ |
|
1324 |
if (opt == java_net_SocketOptions_IP_MULTICAST_LOOP) { |
|
1325 |
setMulticastLoopbackMode(env, this, fd, opt, value); |
|
1326 |
return; |
|
1327 |
} |
|
1328 |
||
1329 |
/* |
|
1330 |
* Map the Java level socket option to the platform specific |
|
1331 |
* level and option name. |
|
1332 |
*/ |
|
1333 |
if (NET_MapSocketOption(opt, &level, &optname)) { |
|
41214
855ac576eb77
8166850: No runtime error expected after calling NET_MapSocketOption
clanger
parents:
39318
diff
changeset
|
1334 |
JNU_ThrowByName(env, "java/net/SocketException", "Invalid option"); |
2 | 1335 |
return; |
1336 |
} |
|
1337 |
||
1338 |
switch (opt) { |
|
1339 |
case java_net_SocketOptions_SO_SNDBUF : |
|
1340 |
case java_net_SocketOptions_SO_RCVBUF : |
|
1341 |
case java_net_SocketOptions_IP_TOS : |
|
1342 |
{ |
|
1343 |
jclass cls; |
|
1344 |
jfieldID fid; |
|
1345 |
||
1346 |
cls = (*env)->FindClass(env, "java/lang/Integer"); |
|
1347 |
CHECK_NULL(cls); |
|
1348 |
fid = (*env)->GetFieldID(env, cls, "value", "I"); |
|
1349 |
CHECK_NULL(fid); |
|
1350 |
||
14195
57fe6c437882
8000206: Uninitialized variable in PlainDatagramSocketImpl.c
chegar
parents:
13999
diff
changeset
|
1351 |
optval = (*env)->GetIntField(env, value, fid); |
2 | 1352 |
break; |
1353 |
} |
|
1354 |
||
1355 |
case java_net_SocketOptions_SO_REUSEADDR: |
|
36115 | 1356 |
case java_net_SocketOptions_SO_REUSEPORT: |
2 | 1357 |
case java_net_SocketOptions_SO_BROADCAST: |
1358 |
{ |
|
1359 |
jclass cls; |
|
1360 |
jfieldID fid; |
|
1361 |
jboolean on; |
|
1362 |
||
1363 |
cls = (*env)->FindClass(env, "java/lang/Boolean"); |
|
1364 |
CHECK_NULL(cls); |
|
1365 |
fid = (*env)->GetFieldID(env, cls, "value", "Z"); |
|
1366 |
CHECK_NULL(fid); |
|
1367 |
||
1368 |
on = (*env)->GetBooleanField(env, value, fid); |
|
1369 |
||
1370 |
/* SO_REUSEADDR or SO_BROADCAST */ |
|
14195
57fe6c437882
8000206: Uninitialized variable in PlainDatagramSocketImpl.c
chegar
parents:
13999
diff
changeset
|
1371 |
optval = (on ? 1 : 0); |
2 | 1372 |
|
1373 |
break; |
|
1374 |
} |
|
1375 |
||
1376 |
default : |
|
1377 |
JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException", |
|
1378 |
"Socket option not supported by PlainDatagramSocketImp"); |
|
22651
bfc6ca5245fd
8033689: PlainDatagramSocketImpl missing returns after throwing an exception
chegar
parents:
22646
diff
changeset
|
1379 |
return; |
2 | 1380 |
|
1381 |
} |
|
1382 |
||
1383 |
if (NET_SetSockOpt(fd, level, optname, (const void *)&optval, optlen) < 0) { |
|
39318
2006d1d41c8b
8158023: SocketExceptions contain too little information sometimes
clanger
parents:
36115
diff
changeset
|
1384 |
JNU_ThrowByNameWithMessageAndLastError |
2006d1d41c8b
8158023: SocketExceptions contain too little information sometimes
clanger
parents:
36115
diff
changeset
|
1385 |
(env, JNU_JAVANETPKG "SocketException", "Error setting socket option"); |
2 | 1386 |
return; |
1387 |
} |
|
1388 |
} |
|
1389 |
||
1390 |
||
1391 |
/* |
|
1392 |
* Return the multicast interface: |
|
1393 |
* |
|
1394 |
* SocketOptions.IP_MULTICAST_IF |
|
1395 |
* IPv4: Query IPPROTO_IP/IP_MULTICAST_IF |
|
1396 |
* Create InetAddress |
|
1397 |
* IP_MULTICAST_IF returns struct ip_mreqn on 2.2 |
|
1398 |
* kernel but struct in_addr on 2.4 kernel |
|
13999
f6a2ce6a3e40
7193520: Removed references to Linux kernel version 2.2
chegar
parents:
13574
diff
changeset
|
1399 |
* IPv6: Query IPPROTO_IPV6 / IPV6_MULTICAST_IF |
2 | 1400 |
* If index == 0 return InetAddress representing |
1401 |
* anyLocalAddress. |
|
1402 |
* If index > 0 query NetworkInterface by index |
|
1403 |
* and returns addrs[0] |
|
1404 |
* |
|
1405 |
* SocketOptions.IP_MULTICAST_IF2 |
|
1406 |
* IPv4: Query IPPROTO_IP/IP_MULTICAST_IF |
|
1407 |
* Query NetworkInterface by IP address and |
|
1408 |
* return the NetworkInterface that the address |
|
1409 |
* is bound too. |
|
1410 |
* IPv6: Query IPPROTO_IPV6 / IPV6_MULTICAST_IF |
|
1411 |
* (except Linux .2 kernel) |
|
1412 |
* Query NetworkInterface by index and |
|
1413 |
* return NetworkInterface. |
|
1414 |
*/ |
|
1415 |
jobject getMulticastInterface(JNIEnv *env, jobject this, int fd, jint opt) { |
|
1416 |
jboolean isIPV4 = JNI_TRUE; |
|
1417 |
||
1418 |
if (ipv6_available()) { |
|
1419 |
isIPV4 = JNI_FALSE; |
|
1420 |
} |
|
1421 |
||
1422 |
/* |
|
1423 |
* IPv4 implementation |
|
1424 |
*/ |
|
1425 |
if (isIPV4) { |
|
1426 |
static jclass inet4_class; |
|
1427 |
static jmethodID inet4_ctrID; |
|
1428 |
||
1429 |
static jclass ni_class; |
|
1430 |
static jmethodID ni_ctrID; |
|
1431 |
static jfieldID ni_indexID; |
|
1432 |
static jfieldID ni_addrsID; |
|
28549 | 1433 |
static jfieldID ni_nameID; |
2 | 1434 |
|
1435 |
jobjectArray addrArray; |
|
1436 |
jobject addr; |
|
1437 |
jobject ni; |
|
28549 | 1438 |
jobject ni_name; |
2 | 1439 |
|
1440 |
struct in_addr in; |
|
1441 |
struct in_addr *inP = ∈ |
|
23015
73b21ab36615
8034174: Remove use of JVM_* functions from java.net code
chegar
parents:
23010
diff
changeset
|
1442 |
socklen_t len = sizeof(struct in_addr); |
2 | 1443 |
|
23015
73b21ab36615
8034174: Remove use of JVM_* functions from java.net code
chegar
parents:
23010
diff
changeset
|
1444 |
if (getsockopt(fd, IPPROTO_IP, IP_MULTICAST_IF, |
73b21ab36615
8034174: Remove use of JVM_* functions from java.net code
chegar
parents:
23010
diff
changeset
|
1445 |
(char *)inP, &len) < 0) { |
39318
2006d1d41c8b
8158023: SocketExceptions contain too little information sometimes
clanger
parents:
36115
diff
changeset
|
1446 |
JNU_ThrowByNameWithMessageAndLastError |
2006d1d41c8b
8158023: SocketExceptions contain too little information sometimes
clanger
parents:
36115
diff
changeset
|
1447 |
(env, JNU_JAVANETPKG "SocketException", "Error getting socket option"); |
2 | 1448 |
return NULL; |
1449 |
} |
|
1450 |
||
1451 |
/* |
|
1452 |
* Construct and populate an Inet4Address |
|
1453 |
*/ |
|
1454 |
if (inet4_class == NULL) { |
|
1455 |
jclass c = (*env)->FindClass(env, "java/net/Inet4Address"); |
|
1456 |
CHECK_NULL_RETURN(c, NULL); |
|
1457 |
inet4_ctrID = (*env)->GetMethodID(env, c, "<init>", "()V"); |
|
1458 |
CHECK_NULL_RETURN(inet4_ctrID, NULL); |
|
1459 |
inet4_class = (*env)->NewGlobalRef(env, c); |
|
1460 |
CHECK_NULL_RETURN(inet4_class, NULL); |
|
1461 |
} |
|
1462 |
addr = (*env)->NewObject(env, inet4_class, inet4_ctrID, 0); |
|
1463 |
CHECK_NULL_RETURN(addr, NULL); |
|
1464 |
||
16870 | 1465 |
setInetAddress_addr(env, addr, ntohl(in.s_addr)); |
51151 | 1466 |
JNU_CHECK_EXCEPTION_RETURN(env, NULL); |
2 | 1467 |
|
1468 |
/* |
|
1469 |
* For IP_MULTICAST_IF return InetAddress |
|
1470 |
*/ |
|
1471 |
if (opt == java_net_SocketOptions_IP_MULTICAST_IF) { |
|
1472 |
return addr; |
|
1473 |
} |
|
1474 |
||
1475 |
/* |
|
1476 |
* For IP_MULTICAST_IF2 we get the NetworkInterface for |
|
1477 |
* this address and return it |
|
1478 |
*/ |
|
1479 |
if (ni_class == NULL) { |
|
1480 |
jclass c = (*env)->FindClass(env, "java/net/NetworkInterface"); |
|
1481 |
CHECK_NULL_RETURN(c, NULL); |
|
1482 |
ni_ctrID = (*env)->GetMethodID(env, c, "<init>", "()V"); |
|
1483 |
CHECK_NULL_RETURN(ni_ctrID, NULL); |
|
1484 |
ni_indexID = (*env)->GetFieldID(env, c, "index", "I"); |
|
1485 |
CHECK_NULL_RETURN(ni_indexID, NULL); |
|
1486 |
ni_addrsID = (*env)->GetFieldID(env, c, "addrs", |
|
1487 |
"[Ljava/net/InetAddress;"); |
|
1488 |
CHECK_NULL_RETURN(ni_addrsID, NULL); |
|
28549 | 1489 |
ni_nameID = (*env)->GetFieldID(env, c,"name", "Ljava/lang/String;"); |
1490 |
CHECK_NULL_RETURN(ni_nameID, NULL); |
|
2 | 1491 |
ni_class = (*env)->NewGlobalRef(env, c); |
1492 |
CHECK_NULL_RETURN(ni_class, NULL); |
|
1493 |
} |
|
1494 |
ni = Java_java_net_NetworkInterface_getByInetAddress0(env, ni_class, addr); |
|
1495 |
if (ni) { |
|
1496 |
return ni; |
|
1497 |
} |
|
1498 |
||
1499 |
/* |
|
1500 |
* The address doesn't appear to be bound at any known |
|
1501 |
* NetworkInterface. Therefore we construct a NetworkInterface |
|
1502 |
* with this address. |
|
1503 |
*/ |
|
1504 |
ni = (*env)->NewObject(env, ni_class, ni_ctrID, 0); |
|
1505 |
CHECK_NULL_RETURN(ni, NULL); |
|
1506 |
||
1507 |
(*env)->SetIntField(env, ni, ni_indexID, -1); |
|
1508 |
addrArray = (*env)->NewObjectArray(env, 1, inet4_class, NULL); |
|
1509 |
CHECK_NULL_RETURN(addrArray, NULL); |
|
1510 |
(*env)->SetObjectArrayElement(env, addrArray, 0, addr); |
|
1511 |
(*env)->SetObjectField(env, ni, ni_addrsID, addrArray); |
|
28549 | 1512 |
ni_name = (*env)->NewStringUTF(env, ""); |
1513 |
if (ni_name != NULL) { |
|
1514 |
(*env)->SetObjectField(env, ni, ni_nameID, ni_name); |
|
1515 |
} |
|
2 | 1516 |
return ni; |
1517 |
} |
|
1518 |
||
1519 |
||
1520 |
/* |
|
1521 |
* IPv6 implementation |
|
1522 |
*/ |
|
1523 |
if ((opt == java_net_SocketOptions_IP_MULTICAST_IF) || |
|
1524 |
(opt == java_net_SocketOptions_IP_MULTICAST_IF2)) { |
|
1525 |
||
1526 |
static jclass ni_class; |
|
1527 |
static jmethodID ni_ctrID; |
|
1528 |
static jfieldID ni_indexID; |
|
1529 |
static jfieldID ni_addrsID; |
|
1530 |
static jclass ia_class; |
|
28549 | 1531 |
static jfieldID ni_nameID; |
2 | 1532 |
static jmethodID ia_anyLocalAddressID; |
1533 |
||
28549 | 1534 |
int index = 0; |
23015
73b21ab36615
8034174: Remove use of JVM_* functions from java.net code
chegar
parents:
23010
diff
changeset
|
1535 |
socklen_t len = sizeof(index); |
2 | 1536 |
|
1537 |
jobjectArray addrArray; |
|
1538 |
jobject addr; |
|
1539 |
jobject ni; |
|
28549 | 1540 |
jobject ni_name; |
2 | 1541 |
|
23015
73b21ab36615
8034174: Remove use of JVM_* functions from java.net code
chegar
parents:
23010
diff
changeset
|
1542 |
if (getsockopt(fd, IPPROTO_IPV6, IPV6_MULTICAST_IF, |
73b21ab36615
8034174: Remove use of JVM_* functions from java.net code
chegar
parents:
23010
diff
changeset
|
1543 |
(char*)&index, &len) < 0) { |
39318
2006d1d41c8b
8158023: SocketExceptions contain too little information sometimes
clanger
parents:
36115
diff
changeset
|
1544 |
JNU_ThrowByNameWithMessageAndLastError |
2006d1d41c8b
8158023: SocketExceptions contain too little information sometimes
clanger
parents:
36115
diff
changeset
|
1545 |
(env, JNU_JAVANETPKG "SocketException", "Error getting socket option"); |
13999
f6a2ce6a3e40
7193520: Removed references to Linux kernel version 2.2
chegar
parents:
13574
diff
changeset
|
1546 |
return NULL; |
2 | 1547 |
} |
1548 |
||
1549 |
if (ni_class == NULL) { |
|
1550 |
jclass c = (*env)->FindClass(env, "java/net/NetworkInterface"); |
|
1551 |
CHECK_NULL_RETURN(c, NULL); |
|
1552 |
ni_ctrID = (*env)->GetMethodID(env, c, "<init>", "()V"); |
|
1553 |
CHECK_NULL_RETURN(ni_ctrID, NULL); |
|
1554 |
ni_indexID = (*env)->GetFieldID(env, c, "index", "I"); |
|
1555 |
CHECK_NULL_RETURN(ni_indexID, NULL); |
|
1556 |
ni_addrsID = (*env)->GetFieldID(env, c, "addrs", |
|
1557 |
"[Ljava/net/InetAddress;"); |
|
1558 |
CHECK_NULL_RETURN(ni_addrsID, NULL); |
|
1559 |
||
1560 |
ia_class = (*env)->FindClass(env, "java/net/InetAddress"); |
|
1561 |
CHECK_NULL_RETURN(ia_class, NULL); |
|
1562 |
ia_class = (*env)->NewGlobalRef(env, ia_class); |
|
1563 |
CHECK_NULL_RETURN(ia_class, NULL); |
|
1564 |
ia_anyLocalAddressID = (*env)->GetStaticMethodID(env, |
|
1565 |
ia_class, |
|
1566 |
"anyLocalAddress", |
|
1567 |
"()Ljava/net/InetAddress;"); |
|
1568 |
CHECK_NULL_RETURN(ia_anyLocalAddressID, NULL); |
|
28549 | 1569 |
ni_nameID = (*env)->GetFieldID(env, c,"name", "Ljava/lang/String;"); |
1570 |
CHECK_NULL_RETURN(ni_nameID, NULL); |
|
2 | 1571 |
ni_class = (*env)->NewGlobalRef(env, c); |
1572 |
CHECK_NULL_RETURN(ni_class, NULL); |
|
1573 |
} |
|
1574 |
||
1575 |
/* |
|
1576 |
* If multicast to a specific interface then return the |
|
1577 |
* interface (for IF2) or the any address on that interface |
|
1578 |
* (for IF). |
|
1579 |
*/ |
|
1580 |
if (index > 0) { |
|
1097
af4930f761df
6717876: Make java.net.NetworkInterface.getIndex() public
jccollet
parents:
910
diff
changeset
|
1581 |
ni = Java_java_net_NetworkInterface_getByIndex0(env, ni_class, |
2 | 1582 |
index); |
1583 |
if (ni == NULL) { |
|
1584 |
char errmsg[255]; |
|
1585 |
sprintf(errmsg, |
|
1586 |
"IPV6_MULTICAST_IF returned index to unrecognized interface: %d", |
|
1587 |
index); |
|
1588 |
JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException", errmsg); |
|
1589 |
return NULL; |
|
1590 |
} |
|
1591 |
||
1592 |
/* |
|
1593 |
* For IP_MULTICAST_IF2 return the NetworkInterface |
|
1594 |
*/ |
|
1595 |
if (opt == java_net_SocketOptions_IP_MULTICAST_IF2) { |
|
1596 |
return ni; |
|
1597 |
} |
|
1598 |
||
1599 |
/* |
|
1600 |
* For IP_MULTICAST_IF return addrs[0] |
|
1601 |
*/ |
|
1602 |
addrArray = (*env)->GetObjectField(env, ni, ni_addrsID); |
|
1603 |
if ((*env)->GetArrayLength(env, addrArray) < 1) { |
|
1604 |
JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException", |
|
1605 |
"IPV6_MULTICAST_IF returned interface without IP bindings"); |
|
1606 |
return NULL; |
|
1607 |
} |
|
1608 |
||
1609 |
addr = (*env)->GetObjectArrayElement(env, addrArray, 0); |
|
1610 |
return addr; |
|
1611 |
} |
|
1612 |
||
1613 |
/* |
|
1614 |
* Multicast to any address - return anyLocalAddress |
|
1615 |
* or a NetworkInterface with addrs[0] set to anyLocalAddress |
|
1616 |
*/ |
|
1617 |
||
1618 |
addr = (*env)->CallStaticObjectMethod(env, ia_class, ia_anyLocalAddressID, |
|
1619 |
NULL); |
|
1620 |
if (opt == java_net_SocketOptions_IP_MULTICAST_IF) { |
|
1621 |
return addr; |
|
1622 |
} |
|
1623 |
||
1624 |
ni = (*env)->NewObject(env, ni_class, ni_ctrID, 0); |
|
1625 |
CHECK_NULL_RETURN(ni, NULL); |
|
1626 |
(*env)->SetIntField(env, ni, ni_indexID, -1); |
|
1627 |
addrArray = (*env)->NewObjectArray(env, 1, ia_class, NULL); |
|
1628 |
CHECK_NULL_RETURN(addrArray, NULL); |
|
1629 |
(*env)->SetObjectArrayElement(env, addrArray, 0, addr); |
|
1630 |
(*env)->SetObjectField(env, ni, ni_addrsID, addrArray); |
|
28549 | 1631 |
ni_name = (*env)->NewStringUTF(env, ""); |
1632 |
if (ni_name != NULL) { |
|
1633 |
(*env)->SetObjectField(env, ni, ni_nameID, ni_name); |
|
1634 |
} |
|
2 | 1635 |
return ni; |
1636 |
} |
|
1637 |
return NULL; |
|
1638 |
} |
|
1639 |
||
1640 |
||
1641 |
||
1642 |
/* |
|
1643 |
* Returns relevant info as a jint. |
|
1644 |
* |
|
1645 |
* Class: java_net_PlainDatagramSocketImpl |
|
1646 |
* Method: socketGetOption |
|
1647 |
* Signature: (I)Ljava/lang/Object; |
|
1648 |
*/ |
|
1649 |
JNIEXPORT jobject JNICALL |
|
41214
855ac576eb77
8166850: No runtime error expected after calling NET_MapSocketOption
clanger
parents:
39318
diff
changeset
|
1650 |
Java_java_net_PlainDatagramSocketImpl_socketGetOption |
855ac576eb77
8166850: No runtime error expected after calling NET_MapSocketOption
clanger
parents:
39318
diff
changeset
|
1651 |
(JNIEnv *env, jobject this, jint opt) |
855ac576eb77
8166850: No runtime error expected after calling NET_MapSocketOption
clanger
parents:
39318
diff
changeset
|
1652 |
{ |
2 | 1653 |
int fd; |
1654 |
int level, optname, optlen; |
|
1655 |
union { |
|
1656 |
int i; |
|
1657 |
char c; |
|
1658 |
} optval; |
|
1659 |
||
1660 |
fd = getFD(env, this); |
|
1661 |
if (fd < 0) { |
|
1662 |
JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException", |
|
1663 |
"socket closed"); |
|
1664 |
return NULL; |
|
1665 |
} |
|
1666 |
||
1667 |
/* |
|
13999
f6a2ce6a3e40
7193520: Removed references to Linux kernel version 2.2
chegar
parents:
13574
diff
changeset
|
1668 |
* Handle IP_MULTICAST_IF separately |
2 | 1669 |
*/ |
1670 |
if (opt == java_net_SocketOptions_IP_MULTICAST_IF || |
|
1671 |
opt == java_net_SocketOptions_IP_MULTICAST_IF2) { |
|
1672 |
return getMulticastInterface(env, this, fd, opt); |
|
1673 |
||
1674 |
} |
|
1675 |
||
1676 |
/* |
|
1677 |
* SO_BINDADDR implemented using getsockname |
|
1678 |
*/ |
|
1679 |
if (opt == java_net_SocketOptions_SO_BINDADDR) { |
|
1680 |
/* find out local IP address */ |
|
43100 | 1681 |
SOCKETADDRESS sa; |
41380
c27cf95dd7e6
8167295: Further cleanup to the native parts of libnet/libnio
clanger
parents:
41214
diff
changeset
|
1682 |
socklen_t len = sizeof(SOCKETADDRESS); |
2 | 1683 |
int port; |
1684 |
jobject iaObj; |
|
1685 |
||
43100 | 1686 |
if (getsockname(fd, &sa.sa, &len) == -1) { |
39318
2006d1d41c8b
8158023: SocketExceptions contain too little information sometimes
clanger
parents:
36115
diff
changeset
|
1687 |
JNU_ThrowByNameWithMessageAndLastError |
2006d1d41c8b
8158023: SocketExceptions contain too little information sometimes
clanger
parents:
36115
diff
changeset
|
1688 |
(env, JNU_JAVANETPKG "SocketException", "Error getting socket name"); |
2 | 1689 |
return NULL; |
1690 |
} |
|
43100 | 1691 |
iaObj = NET_SockaddrToInetAddress(env, &sa, &port); |
2 | 1692 |
|
1693 |
return iaObj; |
|
1694 |
} |
|
1695 |
||
1696 |
/* |
|
1697 |
* Map the Java level socket option to the platform specific |
|
1698 |
* level and option name. |
|
1699 |
*/ |
|
1700 |
if (NET_MapSocketOption(opt, &level, &optname)) { |
|
41214
855ac576eb77
8166850: No runtime error expected after calling NET_MapSocketOption
clanger
parents:
39318
diff
changeset
|
1701 |
JNU_ThrowByName(env, "java/net/SocketException", "Invalid option"); |
2 | 1702 |
return NULL; |
1703 |
} |
|
1704 |
||
1705 |
if (opt == java_net_SocketOptions_IP_MULTICAST_LOOP && |
|
1706 |
level == IPPROTO_IP) { |
|
1707 |
optlen = sizeof(optval.c); |
|
1708 |
} else { |
|
1709 |
optlen = sizeof(optval.i); |
|
1710 |
} |
|
1711 |
||
1712 |
if (NET_GetSockOpt(fd, level, optname, (void *)&optval, &optlen) < 0) { |
|
39318
2006d1d41c8b
8158023: SocketExceptions contain too little information sometimes
clanger
parents:
36115
diff
changeset
|
1713 |
JNU_ThrowByNameWithMessageAndLastError |
2006d1d41c8b
8158023: SocketExceptions contain too little information sometimes
clanger
parents:
36115
diff
changeset
|
1714 |
(env, JNU_JAVANETPKG "SocketException", "Error getting socket option"); |
2 | 1715 |
return NULL; |
1716 |
} |
|
1717 |
||
1718 |
switch (opt) { |
|
1719 |
case java_net_SocketOptions_IP_MULTICAST_LOOP: |
|
1720 |
/* getLoopbackMode() returns true if IP_MULTICAST_LOOP disabled */ |
|
1721 |
if (level == IPPROTO_IP) { |
|
1722 |
return createBoolean(env, (int)!optval.c); |
|
1723 |
} else { |
|
1724 |
return createBoolean(env, !optval.i); |
|
1725 |
} |
|
1726 |
||
1727 |
case java_net_SocketOptions_SO_BROADCAST: |
|
1728 |
case java_net_SocketOptions_SO_REUSEADDR: |
|
1729 |
return createBoolean(env, optval.i); |
|
1730 |
||
36115 | 1731 |
case java_net_SocketOptions_SO_REUSEPORT: |
1732 |
return createBoolean(env, optval.i); |
|
1733 |
||
2 | 1734 |
case java_net_SocketOptions_SO_SNDBUF: |
1735 |
case java_net_SocketOptions_SO_RCVBUF: |
|
1736 |
case java_net_SocketOptions_IP_TOS: |
|
1737 |
return createInteger(env, optval.i); |
|
1738 |
||
1739 |
} |
|
1740 |
||
13999
f6a2ce6a3e40
7193520: Removed references to Linux kernel version 2.2
chegar
parents:
13574
diff
changeset
|
1741 |
/* should never reach here */ |
2 | 1742 |
return NULL; |
1743 |
} |
|
1744 |
||
1745 |
/* |
|
1746 |
* Multicast-related calls |
|
1747 |
*/ |
|
1748 |
||
1749 |
JNIEXPORT void JNICALL |
|
1750 |
Java_java_net_PlainDatagramSocketImpl_setTTL(JNIEnv *env, jobject this, |
|
1751 |
jbyte ttl) { |
|
1752 |
jint ittl = ttl; |
|
1753 |
if (ittl < 0) { |
|
1754 |
ittl += 0x100; |
|
1755 |
} |
|
1756 |
Java_java_net_PlainDatagramSocketImpl_setTimeToLive(env, this, ittl); |
|
1757 |
} |
|
1758 |
||
1759 |
/* |
|
1760 |
* Set TTL for a socket. Throw exception if failed. |
|
1761 |
*/ |
|
1762 |
static void setTTL(JNIEnv *env, int fd, jint ttl) { |
|
1763 |
char ittl = (char)ttl; |
|
23015
73b21ab36615
8034174: Remove use of JVM_* functions from java.net code
chegar
parents:
23010
diff
changeset
|
1764 |
if (setsockopt(fd, IPPROTO_IP, IP_MULTICAST_TTL, (char*)&ittl, |
73b21ab36615
8034174: Remove use of JVM_* functions from java.net code
chegar
parents:
23010
diff
changeset
|
1765 |
sizeof(ittl)) < 0) { |
39318
2006d1d41c8b
8158023: SocketExceptions contain too little information sometimes
clanger
parents:
36115
diff
changeset
|
1766 |
JNU_ThrowByNameWithMessageAndLastError |
2006d1d41c8b
8158023: SocketExceptions contain too little information sometimes
clanger
parents:
36115
diff
changeset
|
1767 |
(env, JNU_JAVANETPKG "SocketException", "Error setting socket option"); |
2 | 1768 |
} |
1769 |
} |
|
1770 |
||
1771 |
/* |
|
1772 |
* Set hops limit for a socket. Throw exception if failed. |
|
1773 |
*/ |
|
1774 |
static void setHopLimit(JNIEnv *env, int fd, jint ttl) { |
|
1775 |
int ittl = (int)ttl; |
|
23015
73b21ab36615
8034174: Remove use of JVM_* functions from java.net code
chegar
parents:
23010
diff
changeset
|
1776 |
if (setsockopt(fd, IPPROTO_IPV6, IPV6_MULTICAST_HOPS, |
73b21ab36615
8034174: Remove use of JVM_* functions from java.net code
chegar
parents:
23010
diff
changeset
|
1777 |
(char*)&ittl, sizeof(ittl)) < 0) { |
39318
2006d1d41c8b
8158023: SocketExceptions contain too little information sometimes
clanger
parents:
36115
diff
changeset
|
1778 |
JNU_ThrowByNameWithMessageAndLastError |
2006d1d41c8b
8158023: SocketExceptions contain too little information sometimes
clanger
parents:
36115
diff
changeset
|
1779 |
(env, JNU_JAVANETPKG "SocketException", "Error setting socket option"); |
2 | 1780 |
} |
1781 |
} |
|
1782 |
||
1783 |
/* |
|
1784 |
* Class: java_net_PlainDatagramSocketImpl |
|
1785 |
* Method: setTTL |
|
1786 |
* Signature: (B)V |
|
1787 |
*/ |
|
1788 |
JNIEXPORT void JNICALL |
|
1789 |
Java_java_net_PlainDatagramSocketImpl_setTimeToLive(JNIEnv *env, jobject this, |
|
1790 |
jint ttl) { |
|
1791 |
||
1792 |
jobject fdObj = (*env)->GetObjectField(env, this, pdsi_fdID); |
|
1793 |
int fd; |
|
1794 |
/* it is important to cast this to a char, otherwise setsockopt gets confused */ |
|
1795 |
||
1796 |
if (IS_NULL(fdObj)) { |
|
1797 |
JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException", |
|
1798 |
"Socket closed"); |
|
1799 |
return; |
|
1800 |
} else { |
|
1801 |
fd = (*env)->GetIntField(env, fdObj, IO_fd_fdID); |
|
1802 |
} |
|
13999
f6a2ce6a3e40
7193520: Removed references to Linux kernel version 2.2
chegar
parents:
13574
diff
changeset
|
1803 |
/* setsockopt to be correct TTL */ |
2 | 1804 |
#ifdef __linux__ |
1805 |
setTTL(env, fd, ttl); |
|
22651
bfc6ca5245fd
8033689: PlainDatagramSocketImpl missing returns after throwing an exception
chegar
parents:
22646
diff
changeset
|
1806 |
JNU_CHECK_EXCEPTION(env); |
2 | 1807 |
if (ipv6_available()) { |
1808 |
setHopLimit(env, fd, ttl); |
|
1809 |
} |
|
13574
9d58db93e184
7191275: Cleanup OS specific blocks in PlainDatagramSocketImpl.c to support more unix-like platforms
dingxmin
parents:
13245
diff
changeset
|
1810 |
#else /* __linux__ not defined */ |
9d58db93e184
7191275: Cleanup OS specific blocks in PlainDatagramSocketImpl.c to support more unix-like platforms
dingxmin
parents:
13245
diff
changeset
|
1811 |
if (ipv6_available()) { |
9d58db93e184
7191275: Cleanup OS specific blocks in PlainDatagramSocketImpl.c to support more unix-like platforms
dingxmin
parents:
13245
diff
changeset
|
1812 |
setHopLimit(env, fd, ttl); |
9d58db93e184
7191275: Cleanup OS specific blocks in PlainDatagramSocketImpl.c to support more unix-like platforms
dingxmin
parents:
13245
diff
changeset
|
1813 |
} else { |
9d58db93e184
7191275: Cleanup OS specific blocks in PlainDatagramSocketImpl.c to support more unix-like platforms
dingxmin
parents:
13245
diff
changeset
|
1814 |
setTTL(env, fd, ttl); |
9d58db93e184
7191275: Cleanup OS specific blocks in PlainDatagramSocketImpl.c to support more unix-like platforms
dingxmin
parents:
13245
diff
changeset
|
1815 |
} |
9d58db93e184
7191275: Cleanup OS specific blocks in PlainDatagramSocketImpl.c to support more unix-like platforms
dingxmin
parents:
13245
diff
changeset
|
1816 |
#endif /* __linux__ */ |
2 | 1817 |
} |
1818 |
||
1819 |
/* |
|
1820 |
* Class: java_net_PlainDatagramSocketImpl |
|
1821 |
* Method: getTTL |
|
1822 |
* Signature: ()B |
|
1823 |
*/ |
|
1824 |
JNIEXPORT jbyte JNICALL |
|
1825 |
Java_java_net_PlainDatagramSocketImpl_getTTL(JNIEnv *env, jobject this) { |
|
1826 |
return (jbyte)Java_java_net_PlainDatagramSocketImpl_getTimeToLive(env, this); |
|
1827 |
} |
|
1828 |
||
1829 |
||
1830 |
/* |
|
1831 |
* Class: java_net_PlainDatagramSocketImpl |
|
1832 |
* Method: getTTL |
|
1833 |
* Signature: ()B |
|
1834 |
*/ |
|
1835 |
JNIEXPORT jint JNICALL |
|
1836 |
Java_java_net_PlainDatagramSocketImpl_getTimeToLive(JNIEnv *env, jobject this) { |
|
1837 |
||
1838 |
jobject fdObj = (*env)->GetObjectField(env, this, pdsi_fdID); |
|
1839 |
jint fd = -1; |
|
1840 |
||
1841 |
if (IS_NULL(fdObj)) { |
|
1842 |
JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException", |
|
1843 |
"Socket closed"); |
|
1844 |
return -1; |
|
1845 |
} else { |
|
1846 |
fd = (*env)->GetIntField(env, fdObj, IO_fd_fdID); |
|
1847 |
} |
|
13999
f6a2ce6a3e40
7193520: Removed references to Linux kernel version 2.2
chegar
parents:
13574
diff
changeset
|
1848 |
/* getsockopt of TTL */ |
2 | 1849 |
if (ipv6_available()) { |
1850 |
int ttl = 0; |
|
23015
73b21ab36615
8034174: Remove use of JVM_* functions from java.net code
chegar
parents:
23010
diff
changeset
|
1851 |
socklen_t len = sizeof(ttl); |
2 | 1852 |
|
23015
73b21ab36615
8034174: Remove use of JVM_* functions from java.net code
chegar
parents:
23010
diff
changeset
|
1853 |
if (getsockopt(fd, IPPROTO_IPV6, IPV6_MULTICAST_HOPS, |
73b21ab36615
8034174: Remove use of JVM_* functions from java.net code
chegar
parents:
23010
diff
changeset
|
1854 |
(char*)&ttl, &len) < 0) { |
39318
2006d1d41c8b
8158023: SocketExceptions contain too little information sometimes
clanger
parents:
36115
diff
changeset
|
1855 |
JNU_ThrowByNameWithMessageAndLastError |
2006d1d41c8b
8158023: SocketExceptions contain too little information sometimes
clanger
parents:
36115
diff
changeset
|
1856 |
(env, JNU_JAVANETPKG "SocketException", "Error getting socket option"); |
23015
73b21ab36615
8034174: Remove use of JVM_* functions from java.net code
chegar
parents:
23010
diff
changeset
|
1857 |
return -1; |
73b21ab36615
8034174: Remove use of JVM_* functions from java.net code
chegar
parents:
23010
diff
changeset
|
1858 |
} |
2 | 1859 |
return (jint)ttl; |
41834
99c4f72c0a91
8168771: Remove #ifdef AF_INET6 guards in libnet native coding
clanger
parents:
41771
diff
changeset
|
1860 |
} else { |
99c4f72c0a91
8168771: Remove #ifdef AF_INET6 guards in libnet native coding
clanger
parents:
41771
diff
changeset
|
1861 |
u_char ttl = 0; |
99c4f72c0a91
8168771: Remove #ifdef AF_INET6 guards in libnet native coding
clanger
parents:
41771
diff
changeset
|
1862 |
socklen_t len = sizeof(ttl); |
99c4f72c0a91
8168771: Remove #ifdef AF_INET6 guards in libnet native coding
clanger
parents:
41771
diff
changeset
|
1863 |
if (getsockopt(fd, IPPROTO_IP, IP_MULTICAST_TTL, |
99c4f72c0a91
8168771: Remove #ifdef AF_INET6 guards in libnet native coding
clanger
parents:
41771
diff
changeset
|
1864 |
(char*)&ttl, &len) < 0) { |
99c4f72c0a91
8168771: Remove #ifdef AF_INET6 guards in libnet native coding
clanger
parents:
41771
diff
changeset
|
1865 |
JNU_ThrowByNameWithMessageAndLastError |
99c4f72c0a91
8168771: Remove #ifdef AF_INET6 guards in libnet native coding
clanger
parents:
41771
diff
changeset
|
1866 |
(env, JNU_JAVANETPKG "SocketException", "Error getting socket option"); |
99c4f72c0a91
8168771: Remove #ifdef AF_INET6 guards in libnet native coding
clanger
parents:
41771
diff
changeset
|
1867 |
return -1; |
2 | 1868 |
} |
41834
99c4f72c0a91
8168771: Remove #ifdef AF_INET6 guards in libnet native coding
clanger
parents:
41771
diff
changeset
|
1869 |
return (jint)ttl; |
99c4f72c0a91
8168771: Remove #ifdef AF_INET6 guards in libnet native coding
clanger
parents:
41771
diff
changeset
|
1870 |
} |
2 | 1871 |
} |
1872 |
||
1873 |
||
1874 |
/* |
|
1875 |
* mcast_join_leave: Join or leave a multicast group. |
|
1876 |
* |
|
1877 |
* For IPv4 sockets use IP_ADD_MEMBERSHIP/IP_DROP_MEMBERSHIP socket option |
|
1878 |
* to join/leave multicast group. |
|
1879 |
* |
|
1880 |
* For IPv6 sockets use IPV6_ADD_MEMBERSHIP/IPV6_DROP_MEMBERSHIP socket option |
|
1881 |
* to join/leave multicast group. If multicast group is an IPv4 address then |
|
1882 |
* an IPv4-mapped address is used. |
|
1883 |
* |
|
1884 |
* On Linux with IPv6 if we wish to join/leave an IPv4 multicast group then |
|
1885 |
* we must use the IPv4 socket options. This is because the IPv6 socket options |
|
1886 |
* don't support IPv4-mapped addresses. This is true as per 2.2.19 and 2.4.7 |
|
1887 |
* kernel releases. In the future it's possible that IP_ADD_MEMBERSHIP |
|
1888 |
* will be updated to return ENOPROTOOPT if uses with an IPv6 socket (Solaris |
|
1889 |
* already does this). Thus to cater for this we first try with the IPv4 |
|
1890 |
* socket options and if they fail we use the IPv6 socket options. This |
|
1891 |
* seems a reasonable failsafe solution. |
|
1892 |
*/ |
|
1893 |
static void mcast_join_leave(JNIEnv *env, jobject this, |
|
1894 |
jobject iaObj, jobject niObj, |
|
1895 |
jboolean join) { |
|
1896 |
||
1897 |
jobject fdObj = (*env)->GetObjectField(env, this, pdsi_fdID); |
|
1898 |
jint fd; |
|
1899 |
jint ipv6_join_leave; |
|
1900 |
||
1901 |
if (IS_NULL(fdObj)) { |
|
1902 |
JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException", |
|
1903 |
"Socket closed"); |
|
1904 |
return; |
|
1905 |
} else { |
|
1906 |
fd = (*env)->GetIntField(env, fdObj, IO_fd_fdID); |
|
1907 |
} |
|
1908 |
if (IS_NULL(iaObj)) { |
|
1909 |
JNU_ThrowNullPointerException(env, "iaObj"); |
|
1910 |
return; |
|
1911 |
} |
|
1912 |
||
1913 |
/* |
|
1914 |
* Determine if this is an IPv4 or IPv6 join/leave. |
|
1915 |
*/ |
|
1916 |
ipv6_join_leave = ipv6_available(); |
|
1917 |
||
1918 |
#ifdef __linux__ |
|
41771
18c9669e76ca
8167481: cleanup of headers and includes for native libnet
clanger
parents:
41380
diff
changeset
|
1919 |
if (getInetAddress_family(env, iaObj) == java_net_InetAddress_IPv4) { |
51151 | 1920 |
JNU_CHECK_EXCEPTION(env); |
2 | 1921 |
ipv6_join_leave = JNI_FALSE; |
1922 |
} |
|
1923 |
#endif |
|
1924 |
||
1925 |
/* |
|
1926 |
* For IPv4 join use IP_ADD_MEMBERSHIP/IP_DROP_MEMBERSHIP socket option |
|
1927 |
* |
|
1928 |
* On Linux if IPv4 or IPv6 use IP_ADD_MEMBERSHIP/IP_DROP_MEMBERSHIP |
|
1929 |
*/ |
|
1930 |
if (!ipv6_join_leave) { |
|
1931 |
#ifdef __linux__ |
|
1932 |
struct ip_mreqn mname; |
|
1933 |
#else |
|
1934 |
struct ip_mreq mname; |
|
1935 |
#endif |
|
1936 |
int mname_len; |
|
1937 |
||
1938 |
/* |
|
1939 |
* joinGroup(InetAddress, NetworkInterface) implementation :- |
|
1940 |
* |
|
1941 |
* Linux/IPv6: use ip_mreqn structure populated with multicast |
|
1942 |
* address and interface index. |
|
1943 |
* |
|
1944 |
* IPv4: use ip_mreq structure populated with multicast |
|
1945 |
* address and first address obtained from |
|
1946 |
* NetworkInterface |
|
1947 |
*/ |
|
1948 |
if (niObj != NULL) { |
|
41834
99c4f72c0a91
8168771: Remove #ifdef AF_INET6 guards in libnet native coding
clanger
parents:
41771
diff
changeset
|
1949 |
#if defined(__linux__) |
2 | 1950 |
if (ipv6_available()) { |
1951 |
static jfieldID ni_indexID; |
|
1952 |
||
1953 |
if (ni_indexID == NULL) { |
|
1954 |
jclass c = (*env)->FindClass(env, "java/net/NetworkInterface"); |
|
1955 |
CHECK_NULL(c); |
|
1956 |
ni_indexID = (*env)->GetFieldID(env, c, "index", "I"); |
|
1957 |
CHECK_NULL(ni_indexID); |
|
1958 |
} |
|
1959 |
||
16870 | 1960 |
mname.imr_multiaddr.s_addr = htonl(getInetAddress_addr(env, iaObj)); |
51151 | 1961 |
JNU_CHECK_EXCEPTION(env); |
2 | 1962 |
mname.imr_address.s_addr = 0; |
1963 |
mname.imr_ifindex = (*env)->GetIntField(env, niObj, ni_indexID); |
|
1964 |
mname_len = sizeof(struct ip_mreqn); |
|
1965 |
} else |
|
1966 |
#endif |
|
1967 |
{ |
|
1968 |
jobjectArray addrArray = (*env)->GetObjectField(env, niObj, ni_addrsID); |
|
1969 |
jobject addr; |
|
1970 |
||
1971 |
if ((*env)->GetArrayLength(env, addrArray) < 1) { |
|
1972 |
JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException", |
|
1973 |
"bad argument for IP_ADD_MEMBERSHIP: " |
|
1974 |
"No IP addresses bound to interface"); |
|
1975 |
return; |
|
1976 |
} |
|
1977 |
addr = (*env)->GetObjectArrayElement(env, addrArray, 0); |
|
1978 |
||
16870 | 1979 |
mname.imr_multiaddr.s_addr = htonl(getInetAddress_addr(env, iaObj)); |
51151 | 1980 |
JNU_CHECK_EXCEPTION(env); |
2 | 1981 |
#ifdef __linux__ |
16870 | 1982 |
mname.imr_address.s_addr = htonl(getInetAddress_addr(env, addr)); |
51151 | 1983 |
JNU_CHECK_EXCEPTION(env); |
43100 | 1984 |
mname.imr_ifindex = 0; |
2 | 1985 |
#else |
16870 | 1986 |
mname.imr_interface.s_addr = htonl(getInetAddress_addr(env, addr)); |
51151 | 1987 |
JNU_CHECK_EXCEPTION(env); |
2 | 1988 |
#endif |
1989 |
mname_len = sizeof(struct ip_mreq); |
|
1990 |
} |
|
1991 |
} |
|
1992 |
||
1993 |
||
1994 |
/* |
|
1995 |
* joinGroup(InetAddress) implementation :- |
|
1996 |
* |
|
1997 |
* Linux/IPv6: use ip_mreqn structure populated with multicast |
|
1998 |
* address and interface index. index obtained |
|
1999 |
* from cached value or IPV6_MULTICAST_IF. |
|
2000 |
* |
|
2001 |
* IPv4: use ip_mreq structure populated with multicast |
|
2002 |
* address and local address obtained from |
|
2003 |
* IP_MULTICAST_IF. On Linux IP_MULTICAST_IF |
|
2004 |
* returns different structure depending on |
|
2005 |
* kernel. |
|
2006 |
*/ |
|
2007 |
||
2008 |
if (niObj == NULL) { |
|
2009 |
||
41834
99c4f72c0a91
8168771: Remove #ifdef AF_INET6 guards in libnet native coding
clanger
parents:
41771
diff
changeset
|
2010 |
#if defined(__linux__) |
2 | 2011 |
if (ipv6_available()) { |
2012 |
||
2013 |
int index; |
|
23015
73b21ab36615
8034174: Remove use of JVM_* functions from java.net code
chegar
parents:
23010
diff
changeset
|
2014 |
socklen_t len = sizeof(index); |
2 | 2015 |
|
23015
73b21ab36615
8034174: Remove use of JVM_* functions from java.net code
chegar
parents:
23010
diff
changeset
|
2016 |
if (getsockopt(fd, IPPROTO_IPV6, IPV6_MULTICAST_IF, |
73b21ab36615
8034174: Remove use of JVM_* functions from java.net code
chegar
parents:
23010
diff
changeset
|
2017 |
(char*)&index, &len) < 0) { |
13999
f6a2ce6a3e40
7193520: Removed references to Linux kernel version 2.2
chegar
parents:
13574
diff
changeset
|
2018 |
NET_ThrowCurrent(env, "getsockopt IPV6_MULTICAST_IF failed"); |
f6a2ce6a3e40
7193520: Removed references to Linux kernel version 2.2
chegar
parents:
13574
diff
changeset
|
2019 |
return; |
2 | 2020 |
} |
2021 |
||
16870 | 2022 |
mname.imr_multiaddr.s_addr = htonl(getInetAddress_addr(env, iaObj)); |
51151 | 2023 |
JNU_CHECK_EXCEPTION(env); |
2 | 2024 |
mname.imr_address.s_addr = 0 ; |
2025 |
mname.imr_ifindex = index; |
|
2026 |
mname_len = sizeof(struct ip_mreqn); |
|
2027 |
} else |
|
2028 |
#endif |
|
2029 |
{ |
|
2030 |
struct in_addr in; |
|
2031 |
struct in_addr *inP = ∈ |
|
6886 | 2032 |
socklen_t len = sizeof(struct in_addr); |
2 | 2033 |
|
2034 |
if (getsockopt(fd, IPPROTO_IP, IP_MULTICAST_IF, (char *)inP, &len) < 0) { |
|
2035 |
NET_ThrowCurrent(env, "getsockopt IP_MULTICAST_IF failed"); |
|
2036 |
return; |
|
2037 |
} |
|
2038 |
||
2039 |
#ifdef __linux__ |
|
13999
f6a2ce6a3e40
7193520: Removed references to Linux kernel version 2.2
chegar
parents:
13574
diff
changeset
|
2040 |
mname.imr_address.s_addr = in.s_addr; |
43100 | 2041 |
mname.imr_ifindex = 0; |
2 | 2042 |
#else |
2043 |
mname.imr_interface.s_addr = in.s_addr; |
|
2044 |
#endif |
|
16870 | 2045 |
mname.imr_multiaddr.s_addr = htonl(getInetAddress_addr(env, iaObj)); |
51151 | 2046 |
JNU_CHECK_EXCEPTION(env); |
2 | 2047 |
mname_len = sizeof(struct ip_mreq); |
2048 |
} |
|
2049 |
} |
|
2050 |
||
2051 |
||
2052 |
/* |
|
2053 |
* Join the multicast group. |
|
2054 |
*/ |
|
23015
73b21ab36615
8034174: Remove use of JVM_* functions from java.net code
chegar
parents:
23010
diff
changeset
|
2055 |
if (setsockopt(fd, IPPROTO_IP, (join ? IP_ADD_MEMBERSHIP:IP_DROP_MEMBERSHIP), |
73b21ab36615
8034174: Remove use of JVM_* functions from java.net code
chegar
parents:
23010
diff
changeset
|
2056 |
(char *) &mname, mname_len) < 0) { |
2 | 2057 |
|
2058 |
/* |
|
2059 |
* If IP_ADD_MEMBERSHIP returns ENOPROTOOPT on Linux and we've got |
|
2060 |
* IPv6 enabled then it's possible that the kernel has been fixed |
|
2061 |
* so we switch to IPV6_ADD_MEMBERSHIP socket option. |
|
13999
f6a2ce6a3e40
7193520: Removed references to Linux kernel version 2.2
chegar
parents:
13574
diff
changeset
|
2062 |
* As of 2.4.7 kernel IPV6_ADD_MEMBERSHIP can't handle IPv4-mapped |
f6a2ce6a3e40
7193520: Removed references to Linux kernel version 2.2
chegar
parents:
13574
diff
changeset
|
2063 |
* addresses so we have to use IP_ADD_MEMBERSHIP for IPv4 multicast |
28059
e576535359cc
8067377: My hobby: caning, then then canning, the the can-can
martin
parents:
25859
diff
changeset
|
2064 |
* groups. However if the socket is an IPv6 socket then setsockopt |
13999
f6a2ce6a3e40
7193520: Removed references to Linux kernel version 2.2
chegar
parents:
13574
diff
changeset
|
2065 |
* should return ENOPROTOOPT. We assume this will be fixed in Linux |
2 | 2066 |
* at some stage. |
2067 |
*/ |
|
41834
99c4f72c0a91
8168771: Remove #ifdef AF_INET6 guards in libnet native coding
clanger
parents:
41771
diff
changeset
|
2068 |
#if defined(__linux__) |
2 | 2069 |
if (errno == ENOPROTOOPT) { |
2070 |
if (ipv6_available()) { |
|
2071 |
ipv6_join_leave = JNI_TRUE; |
|
2072 |
errno = 0; |
|
2073 |
} else { |
|
2074 |
errno = ENOPROTOOPT; /* errno can be changed by ipv6_available */ |
|
2075 |
} |
|
2076 |
} |
|
2077 |
#endif |
|
2078 |
if (errno) { |
|
2079 |
if (join) { |
|
2080 |
NET_ThrowCurrent(env, "setsockopt IP_ADD_MEMBERSHIP failed"); |
|
2081 |
} else { |
|
2082 |
if (errno == ENOENT) |
|
2083 |
JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException", |
|
2084 |
"Not a member of the multicast group"); |
|
2085 |
else |
|
2086 |
NET_ThrowCurrent(env, "setsockopt IP_DROP_MEMBERSHIP failed"); |
|
2087 |
} |
|
22651
bfc6ca5245fd
8033689: PlainDatagramSocketImpl missing returns after throwing an exception
chegar
parents:
22646
diff
changeset
|
2088 |
return; |
2 | 2089 |
} |
2090 |
} |
|
2091 |
||
2092 |
/* |
|
2093 |
* If we haven't switched to IPv6 socket option then we're done. |
|
2094 |
*/ |
|
2095 |
if (!ipv6_join_leave) { |
|
2096 |
return; |
|
2097 |
} |
|
2098 |
} |
|
2099 |
||
2100 |
||
2101 |
/* |
|
2102 |
* IPv6 join. If it's an IPv4 multicast group then we use an IPv4-mapped |
|
2103 |
* address. |
|
2104 |
*/ |
|
2105 |
{ |
|
2106 |
struct ipv6_mreq mname6; |
|
2107 |
jbyteArray ipaddress; |
|
2108 |
jbyte caddr[16]; |
|
2109 |
jint family; |
|
2110 |
jint address; |
|
41771
18c9669e76ca
8167481: cleanup of headers and includes for native libnet
clanger
parents:
41380
diff
changeset
|
2111 |
family = getInetAddress_family(env, iaObj) == java_net_InetAddress_IPv4 ? |
18c9669e76ca
8167481: cleanup of headers and includes for native libnet
clanger
parents:
41380
diff
changeset
|
2112 |
AF_INET : AF_INET6; |
51151 | 2113 |
JNU_CHECK_EXCEPTION(env); |
2 | 2114 |
if (family == AF_INET) { /* will convert to IPv4-mapped address */ |
2115 |
memset((char *) caddr, 0, 16); |
|
16870 | 2116 |
address = getInetAddress_addr(env, iaObj); |
51151 | 2117 |
JNU_CHECK_EXCEPTION(env); |
2 | 2118 |
caddr[10] = 0xff; |
2119 |
caddr[11] = 0xff; |
|
2120 |
||
2121 |
caddr[12] = ((address >> 24) & 0xff); |
|
2122 |
caddr[13] = ((address >> 16) & 0xff); |
|
2123 |
caddr[14] = ((address >> 8) & 0xff); |
|
2124 |
caddr[15] = (address & 0xff); |
|
2125 |
} else { |
|
22956 | 2126 |
getInet6Address_ipaddress(env, iaObj, (char*)caddr); |
2 | 2127 |
} |
2128 |
||
2129 |
memcpy((void *)&(mname6.ipv6mr_multiaddr), caddr, sizeof(struct in6_addr)); |
|
2130 |
if (IS_NULL(niObj)) { |
|
2131 |
int index; |
|
23015
73b21ab36615
8034174: Remove use of JVM_* functions from java.net code
chegar
parents:
23010
diff
changeset
|
2132 |
socklen_t len = sizeof(index); |
2 | 2133 |
|
23015
73b21ab36615
8034174: Remove use of JVM_* functions from java.net code
chegar
parents:
23010
diff
changeset
|
2134 |
if (getsockopt(fd, IPPROTO_IPV6, IPV6_MULTICAST_IF, |
73b21ab36615
8034174: Remove use of JVM_* functions from java.net code
chegar
parents:
23010
diff
changeset
|
2135 |
(char*)&index, &len) < 0) { |
13999
f6a2ce6a3e40
7193520: Removed references to Linux kernel version 2.2
chegar
parents:
13574
diff
changeset
|
2136 |
NET_ThrowCurrent(env, "getsockopt IPV6_MULTICAST_IF failed"); |
23015
73b21ab36615
8034174: Remove use of JVM_* functions from java.net code
chegar
parents:
23010
diff
changeset
|
2137 |
return; |
2 | 2138 |
} |
2139 |
||
2140 |
#ifdef __linux__ |
|
2141 |
/* |
|
2142 |
* On 2.4.8+ if we join a group with the interface set to 0 |
|
2143 |
* then the kernel records the interface it decides. This causes |
|
2144 |
* subsequent leave groups to fail as there is no match. Thus we |
|
2145 |
* pick the interface if there is a matching route. |
|
2146 |
*/ |
|
13999
f6a2ce6a3e40
7193520: Removed references to Linux kernel version 2.2
chegar
parents:
13574
diff
changeset
|
2147 |
if (index == 0) { |
2 | 2148 |
int rt_index = getDefaultIPv6Interface(&(mname6.ipv6mr_multiaddr)); |
2149 |
if (rt_index > 0) { |
|
2150 |
index = rt_index; |
|
2151 |
} |
|
2152 |
} |
|
2153 |
#endif |
|
12551
5be0a162ed89
7144274: [macosx] Default IPv6 multicast interface is not being set when calling MulticastSocket.joinGroup()
khazra
parents:
12047
diff
changeset
|
2154 |
#ifdef MACOSX |
5be0a162ed89
7144274: [macosx] Default IPv6 multicast interface is not being set when calling MulticastSocket.joinGroup()
khazra
parents:
12047
diff
changeset
|
2155 |
if (family == AF_INET6 && index == 0) { |
5be0a162ed89
7144274: [macosx] Default IPv6 multicast interface is not being set when calling MulticastSocket.joinGroup()
khazra
parents:
12047
diff
changeset
|
2156 |
index = getDefaultScopeID(env); |
5be0a162ed89
7144274: [macosx] Default IPv6 multicast interface is not being set when calling MulticastSocket.joinGroup()
khazra
parents:
12047
diff
changeset
|
2157 |
} |
5be0a162ed89
7144274: [macosx] Default IPv6 multicast interface is not being set when calling MulticastSocket.joinGroup()
khazra
parents:
12047
diff
changeset
|
2158 |
#endif |
2 | 2159 |
mname6.ipv6mr_interface = index; |
2160 |
} else { |
|
2161 |
jint idx = (*env)->GetIntField(env, niObj, ni_indexID); |
|
2162 |
mname6.ipv6mr_interface = idx; |
|
2163 |
} |
|
2164 |
||
12047
320a714614e9
7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
9035
diff
changeset
|
2165 |
#if defined(_ALLBSD_SOURCE) |
320a714614e9
7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
9035
diff
changeset
|
2166 |
#define ADD_MEMBERSHIP IPV6_JOIN_GROUP |
320a714614e9
7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
9035
diff
changeset
|
2167 |
#define DRP_MEMBERSHIP IPV6_LEAVE_GROUP |
320a714614e9
7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
9035
diff
changeset
|
2168 |
#define S_ADD_MEMBERSHIP "IPV6_JOIN_GROUP" |
320a714614e9
7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
9035
diff
changeset
|
2169 |
#define S_DRP_MEMBERSHIP "IPV6_LEAVE_GROUP" |
320a714614e9
7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
9035
diff
changeset
|
2170 |
#else |
320a714614e9
7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
9035
diff
changeset
|
2171 |
#define ADD_MEMBERSHIP IPV6_ADD_MEMBERSHIP |
320a714614e9
7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
9035
diff
changeset
|
2172 |
#define DRP_MEMBERSHIP IPV6_DROP_MEMBERSHIP |
320a714614e9
7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
9035
diff
changeset
|
2173 |
#define S_ADD_MEMBERSHIP "IPV6_ADD_MEMBERSHIP" |
320a714614e9
7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
9035
diff
changeset
|
2174 |
#define S_DRP_MEMBERSHIP "IPV6_DROP_MEMBERSHIP" |
320a714614e9
7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
9035
diff
changeset
|
2175 |
#endif |
320a714614e9
7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
9035
diff
changeset
|
2176 |
|
2 | 2177 |
/* Join the multicast group */ |
23015
73b21ab36615
8034174: Remove use of JVM_* functions from java.net code
chegar
parents:
23010
diff
changeset
|
2178 |
if (setsockopt(fd, IPPROTO_IPV6, (join ? ADD_MEMBERSHIP : DRP_MEMBERSHIP), |
73b21ab36615
8034174: Remove use of JVM_* functions from java.net code
chegar
parents:
23010
diff
changeset
|
2179 |
(char *) &mname6, sizeof (mname6)) < 0) { |
2 | 2180 |
|
2181 |
if (join) { |
|
12047
320a714614e9
7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
9035
diff
changeset
|
2182 |
NET_ThrowCurrent(env, "setsockopt " S_ADD_MEMBERSHIP " failed"); |
2 | 2183 |
} else { |
2184 |
if (errno == ENOENT) { |
|
2185 |
JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException", |
|
2186 |
"Not a member of the multicast group"); |
|
2187 |
} else { |
|
12047
320a714614e9
7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
9035
diff
changeset
|
2188 |
NET_ThrowCurrent(env, "setsockopt " S_DRP_MEMBERSHIP " failed"); |
2 | 2189 |
} |
2190 |
} |
|
2191 |
} |
|
2192 |
} |
|
2193 |
} |
|
2194 |
||
2195 |
/* |
|
2196 |
* Class: java_net_PlainDatagramSocketImpl |
|
2197 |
* Method: join |
|
2198 |
* Signature: (Ljava/net/InetAddress;)V |
|
2199 |
*/ |
|
2200 |
JNIEXPORT void JNICALL |
|
2201 |
Java_java_net_PlainDatagramSocketImpl_join(JNIEnv *env, jobject this, |
|
2202 |
jobject iaObj, jobject niObj) |
|
2203 |
{ |
|
2204 |
mcast_join_leave(env, this, iaObj, niObj, JNI_TRUE); |
|
2205 |
} |
|
2206 |
||
2207 |
/* |
|
2208 |
* Class: java_net_PlainDatagramSocketImpl |
|
2209 |
* Method: leave |
|
2210 |
* Signature: (Ljava/net/InetAddress;)V |
|
2211 |
*/ |
|
2212 |
JNIEXPORT void JNICALL |
|
2213 |
Java_java_net_PlainDatagramSocketImpl_leave(JNIEnv *env, jobject this, |
|
2214 |
jobject iaObj, jobject niObj) |
|
2215 |
{ |
|
2216 |
mcast_join_leave(env, this, iaObj, niObj, JNI_FALSE); |
|
2217 |
} |
|
32503
1478c9130534
8072466: Deadlock when initializing MulticastSocket and DatagramSocket
igerasim
parents:
30963
diff
changeset
|
2218 |
|
1478c9130534
8072466: Deadlock when initializing MulticastSocket and DatagramSocket
igerasim
parents:
30963
diff
changeset
|
2219 |
/* |
1478c9130534
8072466: Deadlock when initializing MulticastSocket and DatagramSocket
igerasim
parents:
30963
diff
changeset
|
2220 |
* Class: java_net_PlainDatagramSocketImpl |
1478c9130534
8072466: Deadlock when initializing MulticastSocket and DatagramSocket
igerasim
parents:
30963
diff
changeset
|
2221 |
* Method: dataAvailable |
1478c9130534
8072466: Deadlock when initializing MulticastSocket and DatagramSocket
igerasim
parents:
30963
diff
changeset
|
2222 |
* Signature: ()I |
1478c9130534
8072466: Deadlock when initializing MulticastSocket and DatagramSocket
igerasim
parents:
30963
diff
changeset
|
2223 |
*/ |
1478c9130534
8072466: Deadlock when initializing MulticastSocket and DatagramSocket
igerasim
parents:
30963
diff
changeset
|
2224 |
JNIEXPORT jint JNICALL |
1478c9130534
8072466: Deadlock when initializing MulticastSocket and DatagramSocket
igerasim
parents:
30963
diff
changeset
|
2225 |
Java_java_net_PlainDatagramSocketImpl_dataAvailable(JNIEnv *env, jobject this) |
1478c9130534
8072466: Deadlock when initializing MulticastSocket and DatagramSocket
igerasim
parents:
30963
diff
changeset
|
2226 |
{ |
1478c9130534
8072466: Deadlock when initializing MulticastSocket and DatagramSocket
igerasim
parents:
30963
diff
changeset
|
2227 |
int fd, retval; |
1478c9130534
8072466: Deadlock when initializing MulticastSocket and DatagramSocket
igerasim
parents:
30963
diff
changeset
|
2228 |
|
1478c9130534
8072466: Deadlock when initializing MulticastSocket and DatagramSocket
igerasim
parents:
30963
diff
changeset
|
2229 |
jobject fdObj = (*env)->GetObjectField(env, this, pdsi_fdID); |
1478c9130534
8072466: Deadlock when initializing MulticastSocket and DatagramSocket
igerasim
parents:
30963
diff
changeset
|
2230 |
|
1478c9130534
8072466: Deadlock when initializing MulticastSocket and DatagramSocket
igerasim
parents:
30963
diff
changeset
|
2231 |
if (IS_NULL(fdObj)) { |
1478c9130534
8072466: Deadlock when initializing MulticastSocket and DatagramSocket
igerasim
parents:
30963
diff
changeset
|
2232 |
JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException", |
1478c9130534
8072466: Deadlock when initializing MulticastSocket and DatagramSocket
igerasim
parents:
30963
diff
changeset
|
2233 |
"Socket closed"); |
1478c9130534
8072466: Deadlock when initializing MulticastSocket and DatagramSocket
igerasim
parents:
30963
diff
changeset
|
2234 |
return -1; |
1478c9130534
8072466: Deadlock when initializing MulticastSocket and DatagramSocket
igerasim
parents:
30963
diff
changeset
|
2235 |
} |
1478c9130534
8072466: Deadlock when initializing MulticastSocket and DatagramSocket
igerasim
parents:
30963
diff
changeset
|
2236 |
fd = (*env)->GetIntField(env, fdObj, IO_fd_fdID); |
1478c9130534
8072466: Deadlock when initializing MulticastSocket and DatagramSocket
igerasim
parents:
30963
diff
changeset
|
2237 |
|
1478c9130534
8072466: Deadlock when initializing MulticastSocket and DatagramSocket
igerasim
parents:
30963
diff
changeset
|
2238 |
if (ioctl(fd, FIONREAD, &retval) < 0) { |
1478c9130534
8072466: Deadlock when initializing MulticastSocket and DatagramSocket
igerasim
parents:
30963
diff
changeset
|
2239 |
return -1; |
1478c9130534
8072466: Deadlock when initializing MulticastSocket and DatagramSocket
igerasim
parents:
30963
diff
changeset
|
2240 |
} |
1478c9130534
8072466: Deadlock when initializing MulticastSocket and DatagramSocket
igerasim
parents:
30963
diff
changeset
|
2241 |
return retval; |
1478c9130534
8072466: Deadlock when initializing MulticastSocket and DatagramSocket
igerasim
parents:
30963
diff
changeset
|
2242 |
} |