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