author | khazra |
Thu, 14 Mar 2013 13:46:15 -0700 | |
changeset 18192 | fa6bd0992104 |
parent 16870 | f35b2bd19761 |
child 18253 | 4323a5fe8bc4 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
14342
8435a30053c1
7197491: update copyright year to match last edit in jdk8 jdk repository
alanb
parents:
13245
diff
changeset
|
2 |
* Copyright (c) 1997, 2012, 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 <windows.h> |
|
27 |
#include <winsock2.h> |
|
28 |
#include <ws2tcpip.h> |
|
29 |
#include <ctype.h> |
|
30 |
#include <stdio.h> |
|
31 |
#include <stdlib.h> |
|
32 |
#include <malloc.h> |
|
33 |
#include <sys/types.h> |
|
34 |
||
35 |
#ifndef IPTOS_TOS_MASK |
|
36 |
#define IPTOS_TOS_MASK 0x1e |
|
37 |
#endif |
|
38 |
#ifndef IPTOS_PREC_MASK |
|
39 |
#define IPTOS_PREC_MASK 0xe0 |
|
40 |
#endif |
|
41 |
||
42 |
#include "java_net_TwoStacksPlainDatagramSocketImpl.h" |
|
43 |
#include "java_net_SocketOptions.h" |
|
44 |
#include "java_net_NetworkInterface.h" |
|
45 |
||
46 |
#include "jvm.h" |
|
47 |
#include "jni_util.h" |
|
48 |
#include "net_util.h" |
|
49 |
||
50 |
#define IN_CLASSD(i) (((long)(i) & 0xf0000000) == 0xe0000000) |
|
51 |
#define IN_MULTICAST(i) IN_CLASSD(i) |
|
52 |
||
53 |
/************************************************************************ |
|
54 |
* TwoStacksPlainDatagramSocketImpl |
|
55 |
*/ |
|
56 |
||
57 |
static jfieldID IO_fd_fdID; |
|
58 |
static jfieldID pdsi_trafficClassID; |
|
59 |
jfieldID pdsi_fdID; |
|
60 |
jfieldID pdsi_fd1ID; |
|
61 |
jfieldID pdsi_fduseID; |
|
62 |
jfieldID pdsi_lastfdID; |
|
63 |
jfieldID pdsi_timeoutID; |
|
64 |
||
65 |
jfieldID pdsi_localPortID; |
|
66 |
jfieldID pdsi_connected; |
|
67 |
||
68 |
static jclass ia4_clazz; |
|
69 |
static jmethodID ia4_ctor; |
|
70 |
||
71 |
static CRITICAL_SECTION sizeCheckLock; |
|
72 |
||
73 |
/* Windows OS version is XP or better */ |
|
74 |
static int xp_or_later = 0; |
|
75 |
/* Windows OS version is Windows 2000 or better */ |
|
76 |
static int w2k_or_later = 0; |
|
77 |
||
78 |
/* |
|
79 |
* Notes about UDP/IPV6 on Windows (XP and 2003 server): |
|
80 |
* |
|
81 |
* fd always points to the IPv4 fd, and fd1 points to the IPv6 fd. |
|
82 |
* Both fds are used when we bind to a wild-card address. When a specific |
|
83 |
* address is used, only one of them is used. |
|
84 |
*/ |
|
85 |
||
86 |
/* |
|
87 |
* Returns a java.lang.Integer based on 'i' |
|
88 |
*/ |
|
89 |
jobject createInteger(JNIEnv *env, int i) { |
|
90 |
static jclass i_class; |
|
91 |
static jmethodID i_ctrID; |
|
92 |
static jfieldID i_valueID; |
|
93 |
||
94 |
if (i_class == NULL) { |
|
95 |
jclass c = (*env)->FindClass(env, "java/lang/Integer"); |
|
96 |
CHECK_NULL_RETURN(c, NULL); |
|
97 |
i_ctrID = (*env)->GetMethodID(env, c, "<init>", "(I)V"); |
|
98 |
CHECK_NULL_RETURN(i_ctrID, NULL); |
|
99 |
i_class = (*env)->NewGlobalRef(env, c); |
|
100 |
CHECK_NULL_RETURN(i_class, NULL); |
|
101 |
} |
|
102 |
||
103 |
return ( (*env)->NewObject(env, i_class, i_ctrID, i) ); |
|
104 |
} |
|
105 |
||
106 |
/* |
|
107 |
* Returns a java.lang.Boolean based on 'b' |
|
108 |
*/ |
|
109 |
jobject createBoolean(JNIEnv *env, int b) { |
|
110 |
static jclass b_class; |
|
111 |
static jmethodID b_ctrID; |
|
112 |
static jfieldID b_valueID; |
|
113 |
||
114 |
if (b_class == NULL) { |
|
115 |
jclass c = (*env)->FindClass(env, "java/lang/Boolean"); |
|
116 |
CHECK_NULL_RETURN(c, NULL); |
|
117 |
b_ctrID = (*env)->GetMethodID(env, c, "<init>", "(Z)V"); |
|
118 |
CHECK_NULL_RETURN(b_ctrID, NULL); |
|
119 |
b_class = (*env)->NewGlobalRef(env, c); |
|
120 |
CHECK_NULL_RETURN(b_class, NULL); |
|
121 |
} |
|
122 |
||
123 |
return( (*env)->NewObject(env, b_class, b_ctrID, (jboolean)(b!=0)) ); |
|
124 |
} |
|
125 |
||
126 |
||
127 |
static int getFD(JNIEnv *env, jobject this) { |
|
128 |
jobject fdObj = (*env)->GetObjectField(env, this, pdsi_fdID); |
|
129 |
||
130 |
if (fdObj == NULL) { |
|
131 |
return -1; |
|
132 |
} |
|
133 |
return (*env)->GetIntField(env, fdObj, IO_fd_fdID); |
|
134 |
} |
|
135 |
||
136 |
static int getFD1(JNIEnv *env, jobject this) { |
|
137 |
jobject fdObj = (*env)->GetObjectField(env, this, pdsi_fd1ID); |
|
138 |
||
139 |
if (fdObj == NULL) { |
|
140 |
return -1; |
|
141 |
} |
|
142 |
return (*env)->GetIntField(env, fdObj, IO_fd_fdID); |
|
143 |
} |
|
144 |
||
145 |
/* |
|
146 |
* This function returns JNI_TRUE if the datagram size exceeds the underlying |
|
147 |
* provider's ability to send to the target address. The following OS |
|
148 |
* oddies have been observed :- |
|
149 |
* |
|
150 |
* 1. On Windows 95/98 if we try to send a datagram > 12k to an application |
|
151 |
* on the same machine then the send will fail silently. |
|
152 |
* |
|
153 |
* 2. On Windows ME if we try to send a datagram > supported by underlying |
|
154 |
* provider then send will not return an error. |
|
155 |
* |
|
156 |
* 3. On Windows NT/2000 if we exceeds the maximum size then send will fail |
|
157 |
* with WSAEADDRNOTAVAIL. |
|
158 |
* |
|
159 |
* 4. On Windows 95/98 if we exceed the maximum size when sending to |
|
160 |
* another machine then WSAEINVAL is returned. |
|
161 |
* |
|
162 |
*/ |
|
163 |
jboolean exceedSizeLimit(JNIEnv *env, jint fd, jint addr, jint size) |
|
164 |
{ |
|
165 |
#define DEFAULT_MSG_SIZE 65527 |
|
166 |
static jboolean initDone; |
|
167 |
static jboolean is95or98; |
|
168 |
static int maxmsg; |
|
169 |
||
170 |
typedef struct _netaddr { /* Windows 95/98 only */ |
|
171 |
unsigned long addr; |
|
172 |
struct _netaddr *next; |
|
173 |
} netaddr; |
|
174 |
static netaddr *addrList; |
|
175 |
netaddr *curr; |
|
176 |
||
177 |
/* |
|
178 |
* First time we are called we must determine which OS this is and also |
|
179 |
* get the maximum size supported by the underlying provider. |
|
180 |
* |
|
181 |
* In addition on 95/98 we must enumerate our IP addresses. |
|
182 |
*/ |
|
183 |
if (!initDone) { |
|
184 |
EnterCriticalSection(&sizeCheckLock); |
|
185 |
||
186 |
if (initDone) { |
|
187 |
/* another thread got there first */ |
|
188 |
LeaveCriticalSection(&sizeCheckLock); |
|
189 |
||
190 |
} else { |
|
191 |
OSVERSIONINFO ver; |
|
192 |
int len; |
|
193 |
||
194 |
/* |
|
195 |
* Step 1: Determine which OS this is. |
|
196 |
*/ |
|
197 |
ver.dwOSVersionInfoSize = sizeof(ver); |
|
198 |
GetVersionEx(&ver); |
|
199 |
||
200 |
is95or98 = JNI_FALSE; |
|
201 |
if (ver.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS && |
|
202 |
ver.dwMajorVersion == 4 && |
|
203 |
(ver.dwMinorVersion == 0 || ver.dwMinorVersion == 10)) { |
|
204 |
||
205 |
is95or98 = JNI_TRUE; |
|
206 |
} |
|
207 |
||
208 |
/* |
|
209 |
* Step 2: Determine the maximum datagram supported by the |
|
210 |
* underlying provider. On Windows 95 if winsock hasn't been |
|
211 |
* upgraded (ie: unsupported configuration) then we assume |
|
212 |
* the default 64k limit. |
|
213 |
*/ |
|
214 |
len = sizeof(maxmsg); |
|
215 |
if (NET_GetSockOpt(fd, SOL_SOCKET, SO_MAX_MSG_SIZE, (char *)&maxmsg, &len) < 0) { |
|
216 |
maxmsg = DEFAULT_MSG_SIZE; |
|
217 |
} |
|
218 |
||
219 |
/* |
|
220 |
* Step 3: On Windows 95/98 then enumerate the IP addresses on |
|
221 |
* this machine. This is necesary because we need to check if the |
|
222 |
* datagram is being sent to an application on the same machine. |
|
223 |
*/ |
|
224 |
if (is95or98) { |
|
225 |
char hostname[255]; |
|
226 |
struct hostent *hp; |
|
227 |
||
228 |
if (gethostname(hostname, sizeof(hostname)) == -1) { |
|
229 |
LeaveCriticalSection(&sizeCheckLock); |
|
230 |
JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException", "Unable to obtain hostname"); |
|
231 |
return JNI_TRUE; |
|
232 |
} |
|
233 |
hp = (struct hostent *)gethostbyname(hostname); |
|
234 |
if (hp != NULL) { |
|
235 |
struct in_addr **addrp = (struct in_addr **) hp->h_addr_list; |
|
236 |
||
237 |
while (*addrp != (struct in_addr *) 0) { |
|
238 |
curr = (netaddr *)malloc(sizeof(netaddr)); |
|
239 |
if (curr == NULL) { |
|
240 |
while (addrList != NULL) { |
|
241 |
curr = addrList->next; |
|
242 |
free(addrList); |
|
243 |
addrList = curr; |
|
244 |
} |
|
245 |
LeaveCriticalSection(&sizeCheckLock); |
|
13245
7ab3ef5b9520
7181353: Update error message to distinguish native OOM and java OOM in net
zhouyx
parents:
10693
diff
changeset
|
246 |
JNU_ThrowOutOfMemoryError(env, "Native heap allocation failed"); |
2 | 247 |
return JNI_TRUE; |
248 |
} |
|
249 |
curr->addr = htonl((*addrp)->S_un.S_addr); |
|
250 |
curr->next = addrList; |
|
251 |
addrList = curr; |
|
252 |
addrp++; |
|
253 |
} |
|
254 |
} |
|
255 |
} |
|
256 |
||
257 |
/* |
|
258 |
* Step 4: initialization is done so set flag and unlock cs |
|
259 |
*/ |
|
260 |
initDone = JNI_TRUE; |
|
261 |
LeaveCriticalSection(&sizeCheckLock); |
|
262 |
} |
|
263 |
} |
|
264 |
||
265 |
/* |
|
266 |
* Now examine the size of the datagram :- |
|
267 |
* |
|
268 |
* (a) If exceeds size of service provider return 'false' to indicate that |
|
269 |
* we exceed the limit. |
|
270 |
* (b) If not 95/98 then return 'true' to indicate that the size is okay. |
|
271 |
* (c) On 95/98 if the size is <12k we are okay. |
|
272 |
* (d) On 95/98 if size > 12k then check if the destination is the current |
|
273 |
* machine. |
|
274 |
*/ |
|
275 |
if (size > maxmsg) { /* step (a) */ |
|
276 |
return JNI_TRUE; |
|
277 |
} |
|
278 |
if (!is95or98) { /* step (b) */ |
|
279 |
return JNI_FALSE; |
|
280 |
} |
|
281 |
if (size <= 12280) { /* step (c) */ |
|
282 |
return JNI_FALSE; |
|
283 |
} |
|
284 |
||
285 |
/* step (d) */ |
|
286 |
||
287 |
if ((addr & 0x7f000000) == 0x7f000000) { |
|
288 |
return JNI_TRUE; |
|
289 |
} |
|
290 |
curr = addrList; |
|
291 |
while (curr != NULL) { |
|
292 |
if (curr->addr == addr) { |
|
293 |
return JNI_TRUE; |
|
294 |
} |
|
295 |
curr = curr->next; |
|
296 |
} |
|
297 |
return JNI_FALSE; |
|
298 |
} |
|
299 |
||
300 |
/* |
|
301 |
* Return JNI_TRUE if this Windows edition supports ICMP Port Unreachable |
|
302 |
*/ |
|
303 |
__inline static jboolean supportPortUnreachable() { |
|
304 |
static jboolean initDone; |
|
305 |
static jboolean portUnreachableSupported; |
|
306 |
||
307 |
if (!initDone) { |
|
308 |
OSVERSIONINFO ver; |
|
309 |
ver.dwOSVersionInfoSize = sizeof(ver); |
|
310 |
GetVersionEx(&ver); |
|
311 |
if (ver.dwPlatformId == VER_PLATFORM_WIN32_NT && ver.dwMajorVersion >= 5) { |
|
312 |
portUnreachableSupported = JNI_TRUE; |
|
313 |
} else { |
|
314 |
portUnreachableSupported = JNI_FALSE; |
|
315 |
} |
|
316 |
initDone = JNI_TRUE; |
|
317 |
} |
|
318 |
return portUnreachableSupported; |
|
319 |
} |
|
320 |
||
321 |
/* |
|
322 |
* This function "purges" all outstanding ICMP port unreachable packets |
|
323 |
* outstanding on a socket and returns JNI_TRUE if any ICMP messages |
|
324 |
* have been purged. The rational for purging is to emulate normal BSD |
|
325 |
* behaviour whereby receiving a "connection reset" status resets the |
|
326 |
* socket. |
|
327 |
*/ |
|
328 |
static jboolean purgeOutstandingICMP(JNIEnv *env, jobject this, jint fd) |
|
329 |
{ |
|
330 |
jboolean got_icmp = JNI_FALSE; |
|
331 |
char buf[1]; |
|
332 |
fd_set tbl; |
|
333 |
struct timeval t = { 0, 0 }; |
|
334 |
struct sockaddr_in rmtaddr; |
|
335 |
int addrlen = sizeof(rmtaddr); |
|
336 |
||
337 |
/* |
|
338 |
* A no-op if this OS doesn't support it. |
|
339 |
*/ |
|
340 |
if (!supportPortUnreachable()) { |
|
341 |
return JNI_FALSE; |
|
342 |
} |
|
343 |
||
344 |
/* |
|
345 |
* Peek at the queue to see if there is an ICMP port unreachable. If there |
|
346 |
* is then receive it. |
|
347 |
*/ |
|
348 |
FD_ZERO(&tbl); |
|
349 |
FD_SET(fd, &tbl); |
|
350 |
while(1) { |
|
351 |
if (select(/*ignored*/fd+1, &tbl, 0, 0, &t) <= 0) { |
|
352 |
break; |
|
353 |
} |
|
354 |
if (recvfrom(fd, buf, 1, MSG_PEEK, |
|
355 |
(struct sockaddr *)&rmtaddr, &addrlen) != JVM_IO_ERR) { |
|
356 |
break; |
|
357 |
} |
|
358 |
if (WSAGetLastError() != WSAECONNRESET) { |
|
359 |
/* some other error - we don't care here */ |
|
360 |
break; |
|
361 |
} |
|
362 |
||
363 |
recvfrom(fd, buf, 1, 0, (struct sockaddr *)&rmtaddr, &addrlen); |
|
364 |
got_icmp = JNI_TRUE; |
|
365 |
} |
|
366 |
||
367 |
return got_icmp; |
|
368 |
} |
|
369 |
||
370 |
||
371 |
/* |
|
372 |
* Class: java_net_TwoStacksPlainDatagramSocketImpl |
|
373 |
* Method: init |
|
374 |
* Signature: ()V |
|
375 |
*/ |
|
376 |
JNIEXPORT void JNICALL |
|
377 |
Java_java_net_TwoStacksPlainDatagramSocketImpl_init(JNIEnv *env, jclass cls) { |
|
378 |
||
379 |
OSVERSIONINFO ver; |
|
380 |
int version; |
|
381 |
ver.dwOSVersionInfoSize = sizeof(ver); |
|
382 |
GetVersionEx(&ver); |
|
383 |
||
384 |
version = ver.dwMajorVersion * 10 + ver.dwMinorVersion; |
|
385 |
xp_or_later = (ver.dwPlatformId == VER_PLATFORM_WIN32_NT) && (version >= 51); |
|
386 |
w2k_or_later = (ver.dwPlatformId == VER_PLATFORM_WIN32_NT) && (version >= 50); |
|
387 |
||
388 |
/* get fieldIDs */ |
|
389 |
pdsi_fdID = (*env)->GetFieldID(env, cls, "fd", "Ljava/io/FileDescriptor;"); |
|
390 |
CHECK_NULL(pdsi_fdID); |
|
391 |
pdsi_fd1ID = (*env)->GetFieldID(env, cls, "fd1", "Ljava/io/FileDescriptor;"); |
|
392 |
CHECK_NULL(pdsi_fd1ID); |
|
393 |
pdsi_timeoutID = (*env)->GetFieldID(env, cls, "timeout", "I"); |
|
394 |
CHECK_NULL(pdsi_timeoutID); |
|
395 |
pdsi_fduseID = (*env)->GetFieldID(env, cls, "fduse", "I"); |
|
396 |
CHECK_NULL(pdsi_fduseID); |
|
397 |
pdsi_lastfdID = (*env)->GetFieldID(env, cls, "lastfd", "I"); |
|
398 |
CHECK_NULL(pdsi_lastfdID); |
|
399 |
pdsi_trafficClassID = (*env)->GetFieldID(env, cls, "trafficClass", "I"); |
|
400 |
CHECK_NULL(pdsi_trafficClassID); |
|
401 |
pdsi_localPortID = (*env)->GetFieldID(env, cls, "localPort", "I"); |
|
402 |
CHECK_NULL(pdsi_localPortID); |
|
403 |
pdsi_connected = (*env)->GetFieldID(env, cls, "connected", "Z"); |
|
404 |
CHECK_NULL(pdsi_connected); |
|
405 |
||
406 |
cls = (*env)->FindClass(env, "java/io/FileDescriptor"); |
|
407 |
CHECK_NULL(cls); |
|
408 |
IO_fd_fdID = NET_GetFileDescriptorID(env); |
|
409 |
CHECK_NULL(IO_fd_fdID); |
|
410 |
||
411 |
ia4_clazz = (*env)->FindClass(env, "java/net/Inet4Address"); |
|
412 |
CHECK_NULL(ia4_clazz); |
|
413 |
ia4_clazz = (*env)->NewGlobalRef(env, ia4_clazz); |
|
414 |
CHECK_NULL(ia4_clazz); |
|
415 |
ia4_ctor = (*env)->GetMethodID(env, ia4_clazz, "<init>", "()V"); |
|
416 |
CHECK_NULL(ia4_ctor); |
|
417 |
||
418 |
||
419 |
InitializeCriticalSection(&sizeCheckLock); |
|
420 |
} |
|
421 |
||
422 |
JNIEXPORT void JNICALL |
|
423 |
Java_java_net_TwoStacksPlainDatagramSocketImpl_bind0(JNIEnv *env, jobject this, |
|
18192 | 424 |
jint port, jobject addressObj, |
425 |
jboolean exclBind) { |
|
2 | 426 |
jobject fdObj = (*env)->GetObjectField(env, this, pdsi_fdID); |
427 |
jobject fd1Obj = (*env)->GetObjectField(env, this, pdsi_fd1ID); |
|
428 |
||
429 |
int fd, fd1, family; |
|
430 |
int ipv6_supported = ipv6_available(); |
|
431 |
||
432 |
SOCKETADDRESS lcladdr; |
|
433 |
int lcladdrlen; |
|
434 |
int address; |
|
435 |
||
16870 | 436 |
family = getInetAddress_family(env, addressObj); |
2 | 437 |
if (family == IPv6 && !ipv6_supported) { |
438 |
JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException", |
|
439 |
"Protocol family not supported"); |
|
440 |
return; |
|
441 |
} |
|
442 |
||
443 |
if (IS_NULL(fdObj) || (ipv6_supported && IS_NULL(fd1Obj))) { |
|
444 |
JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException", "socket closed"); |
|
445 |
return; |
|
446 |
} else { |
|
447 |
fd = (*env)->GetIntField(env, fdObj, IO_fd_fdID); |
|
448 |
if (ipv6_supported) { |
|
449 |
fd1 = (*env)->GetIntField(env, fd1Obj, IO_fd_fdID); |
|
450 |
} |
|
451 |
} |
|
452 |
if (IS_NULL(addressObj)) { |
|
453 |
JNU_ThrowNullPointerException(env, "argument address"); |
|
454 |
return; |
|
455 |
} else { |
|
16870 | 456 |
address = getInetAddress_addr(env, addressObj); |
2 | 457 |
} |
458 |
||
459 |
if (NET_InetAddressToSockaddr(env, addressObj, port, (struct sockaddr *)&lcladdr, &lcladdrlen, JNI_FALSE) != 0) { |
|
460 |
return; |
|
461 |
} |
|
462 |
||
463 |
if (ipv6_supported) { |
|
464 |
struct ipv6bind v6bind; |
|
465 |
v6bind.addr = &lcladdr; |
|
466 |
v6bind.ipv4_fd = fd; |
|
467 |
v6bind.ipv6_fd = fd1; |
|
18192 | 468 |
if (NET_BindV6(&v6bind, exclBind) != -1) { |
2 | 469 |
/* check if the fds have changed */ |
470 |
if (v6bind.ipv4_fd != fd) { |
|
471 |
fd = v6bind.ipv4_fd; |
|
472 |
if (fd == -1) { |
|
473 |
/* socket is closed. */ |
|
474 |
(*env)->SetObjectField(env, this, pdsi_fdID, NULL); |
|
475 |
} else { |
|
476 |
/* socket was re-created */ |
|
477 |
(*env)->SetIntField(env, fdObj, IO_fd_fdID, fd); |
|
478 |
} |
|
479 |
} |
|
480 |
if (v6bind.ipv6_fd != fd1) { |
|
481 |
fd1 = v6bind.ipv6_fd; |
|
482 |
if (fd1 == -1) { |
|
483 |
/* socket is closed. */ |
|
484 |
(*env)->SetObjectField(env, this, pdsi_fd1ID, NULL); |
|
485 |
} else { |
|
486 |
/* socket was re-created */ |
|
487 |
(*env)->SetIntField(env, fd1Obj, IO_fd_fdID, fd1); |
|
488 |
} |
|
489 |
} |
|
490 |
} else { |
|
491 |
NET_ThrowCurrent (env, "Cannot bind"); |
|
492 |
return; |
|
493 |
} |
|
494 |
} else { |
|
18192 | 495 |
if (NET_WinBind(fd, (struct sockaddr *)&lcladdr, lcladdrlen, exclBind) == -1) { |
2 | 496 |
if (WSAGetLastError() == WSAEACCES) { |
497 |
WSASetLastError(WSAEADDRINUSE); |
|
498 |
} |
|
499 |
NET_ThrowCurrent(env, "Cannot bind"); |
|
500 |
return; |
|
501 |
} |
|
502 |
} |
|
503 |
||
504 |
if (port == 0) { |
|
505 |
if (fd == -1) { |
|
506 |
/* must be an IPV6 only socket. */ |
|
507 |
fd = fd1; |
|
508 |
} |
|
509 |
if (getsockname(fd, (struct sockaddr *)&lcladdr, &lcladdrlen) == -1) { |
|
510 |
NET_ThrowCurrent(env, "JVM_GetSockName"); |
|
511 |
return; |
|
512 |
} |
|
513 |
port = ntohs((u_short) GET_PORT (&lcladdr)); |
|
514 |
} |
|
515 |
(*env)->SetIntField(env, this, pdsi_localPortID, port); |
|
516 |
} |
|
517 |
||
518 |
||
519 |
/* |
|
520 |
* Class: java_net_TwoStacksPlainDatagramSocketImpl |
|
521 |
* Method: connect0 |
|
522 |
* Signature: (Ljava/net/InetAddress;I)V |
|
523 |
*/ |
|
524 |
||
525 |
JNIEXPORT void JNICALL |
|
526 |
Java_java_net_TwoStacksPlainDatagramSocketImpl_connect0(JNIEnv *env, jobject this, |
|
527 |
jobject address, jint port) { |
|
528 |
/* The object's field */ |
|
529 |
jobject fdObj = (*env)->GetObjectField(env, this, pdsi_fdID); |
|
530 |
jobject fd1Obj = (*env)->GetObjectField(env, this, pdsi_fd1ID); |
|
531 |
/* The fdObj'fd */ |
|
532 |
jint fd=-1, fd1=-1, fdc; |
|
533 |
/* The packetAddress address, family and port */ |
|
534 |
jint addr, family; |
|
535 |
SOCKETADDRESS rmtaddr; |
|
536 |
int rmtaddrlen; |
|
537 |
int ipv6_supported = ipv6_available(); |
|
538 |
||
539 |
if (IS_NULL(fdObj) && IS_NULL(fd1Obj)) { |
|
540 |
JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException", |
|
541 |
"Socket closed"); |
|
542 |
return; |
|
543 |
} |
|
544 |
if (!IS_NULL(fdObj)) { |
|
545 |
fd = (*env)->GetIntField(env, fdObj, IO_fd_fdID); |
|
546 |
} |
|
547 |
if (!IS_NULL(fd1Obj)) { |
|
548 |
fd1 = (*env)->GetIntField(env, fd1Obj, IO_fd_fdID); |
|
549 |
} |
|
550 |
||
551 |
if (IS_NULL(address)) { |
|
552 |
JNU_ThrowNullPointerException(env, "address"); |
|
553 |
return; |
|
554 |
} |
|
555 |
||
16870 | 556 |
addr = getInetAddress_addr(env, address); |
2 | 557 |
|
16870 | 558 |
family = getInetAddress_family(env, address); |
2 | 559 |
if (family == IPv6 && !ipv6_supported) { |
560 |
JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException", |
|
561 |
"Protocol family not supported"); |
|
562 |
return; |
|
563 |
} |
|
564 |
||
565 |
fdc = family == IPv4? fd: fd1; |
|
566 |
||
567 |
if (xp_or_later) { |
|
568 |
/* SIO_UDP_CONNRESET fixes a bug introduced in Windows 2000, which |
|
569 |
* returns connection reset errors un connected UDP sockets (as well |
|
570 |
* as connected sockets. The solution is to only enable this feature |
|
571 |
* when the socket is connected |
|
572 |
*/ |
|
573 |
DWORD x1, x2; /* ignored result codes */ |
|
574 |
int res, t = TRUE; |
|
575 |
res = WSAIoctl(fdc,SIO_UDP_CONNRESET,&t,sizeof(t),&x1,sizeof(x1),&x2,0,0); |
|
576 |
} |
|
577 |
||
578 |
if (NET_InetAddressToSockaddr(env, address, port,(struct sockaddr *)&rmtaddr, &rmtaddrlen, JNI_FALSE) != 0) { |
|
579 |
return; |
|
580 |
} |
|
581 |
||
582 |
if (connect(fdc, (struct sockaddr *)&rmtaddr, sizeof(rmtaddr)) == -1) { |
|
583 |
NET_ThrowCurrent(env, "connect"); |
|
584 |
return; |
|
585 |
} |
|
586 |
} |
|
587 |
||
588 |
/* |
|
589 |
* Class: java_net_TwoStacksPlainDatagramSocketImpl |
|
590 |
* Method: disconnect0 |
|
591 |
* Signature: ()V |
|
592 |
*/ |
|
593 |
||
594 |
JNIEXPORT void JNICALL |
|
595 |
Java_java_net_TwoStacksPlainDatagramSocketImpl_disconnect0(JNIEnv *env, jobject this, jint family) { |
|
596 |
/* The object's field */ |
|
597 |
jobject fdObj; |
|
598 |
/* The fdObj'fd */ |
|
599 |
jint fd, len; |
|
600 |
SOCKETADDRESS addr; |
|
601 |
||
602 |
if (family == IPv4) { |
|
603 |
fdObj = (*env)->GetObjectField(env, this, pdsi_fdID); |
|
604 |
len = sizeof (struct sockaddr_in); |
|
605 |
} else { |
|
606 |
fdObj = (*env)->GetObjectField(env, this, pdsi_fd1ID); |
|
607 |
len = sizeof (struct SOCKADDR_IN6); |
|
608 |
} |
|
609 |
||
610 |
if (IS_NULL(fdObj)) { |
|
611 |
/* disconnect doesn't throw any exceptions */ |
|
612 |
return; |
|
613 |
} |
|
614 |
fd = (*env)->GetIntField(env, fdObj, IO_fd_fdID); |
|
615 |
||
616 |
memset(&addr, 0, len); |
|
617 |
connect(fd, (struct sockaddr *)&addr, len); |
|
618 |
||
619 |
/* |
|
620 |
* use SIO_UDP_CONNRESET |
|
621 |
* to disable ICMP port unreachable handling here. |
|
622 |
*/ |
|
623 |
if (xp_or_later) { |
|
624 |
DWORD x1, x2; /* ignored result codes */ |
|
625 |
int t = FALSE; |
|
626 |
WSAIoctl(fd,SIO_UDP_CONNRESET,&t,sizeof(t),&x1,sizeof(x1),&x2,0,0); |
|
627 |
} |
|
628 |
} |
|
629 |
||
630 |
/* |
|
631 |
* Class: java_net_TwoStacksPlainDatagramSocketImpl |
|
632 |
* Method: send |
|
633 |
* Signature: (Ljava/net/DatagramPacket;)V |
|
634 |
*/ |
|
635 |
JNIEXPORT void JNICALL |
|
636 |
Java_java_net_TwoStacksPlainDatagramSocketImpl_send(JNIEnv *env, jobject this, |
|
637 |
jobject packet) { |
|
638 |
||
639 |
char BUF[MAX_BUFFER_LEN]; |
|
640 |
char *fullPacket; |
|
641 |
jobject fdObj; |
|
642 |
jint fd; |
|
643 |
||
644 |
jobject iaObj; |
|
645 |
jint address; |
|
646 |
jint family; |
|
647 |
||
648 |
jint packetBufferOffset, packetBufferLen, packetPort; |
|
649 |
jbyteArray packetBuffer; |
|
650 |
jboolean connected; |
|
651 |
||
652 |
SOCKETADDRESS rmtaddr; |
|
653 |
SOCKETADDRESS *addrp = &rmtaddr; |
|
654 |
int addrlen; |
|
655 |
||
656 |
||
657 |
if (IS_NULL(packet)) { |
|
658 |
JNU_ThrowNullPointerException(env, "null packet"); |
|
659 |
return; |
|
660 |
} |
|
661 |
||
662 |
iaObj = (*env)->GetObjectField(env, packet, dp_addressID); |
|
663 |
||
664 |
packetPort = (*env)->GetIntField(env, packet, dp_portID); |
|
665 |
packetBufferOffset = (*env)->GetIntField(env, packet, dp_offsetID); |
|
666 |
packetBuffer = (jbyteArray)(*env)->GetObjectField(env, packet, dp_bufID); |
|
667 |
connected = (*env)->GetBooleanField(env, this, pdsi_connected); |
|
668 |
||
669 |
if (IS_NULL(iaObj) || IS_NULL(packetBuffer)) { |
|
670 |
JNU_ThrowNullPointerException(env, "null address || null buffer"); |
|
671 |
return; |
|
672 |
} |
|
673 |
||
16870 | 674 |
family = getInetAddress_family(env, iaObj); |
2 | 675 |
if (family == IPv4) { |
676 |
fdObj = (*env)->GetObjectField(env, this, pdsi_fdID); |
|
677 |
} else { |
|
678 |
if (!ipv6_available()) { |
|
679 |
JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException", |
|
680 |
"Protocol not allowed"); |
|
681 |
return; |
|
682 |
} |
|
683 |
fdObj = (*env)->GetObjectField(env, this, pdsi_fd1ID); |
|
684 |
} |
|
685 |
||
686 |
if (IS_NULL(fdObj)) { |
|
687 |
JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException", |
|
688 |
"Socket closed"); |
|
689 |
return; |
|
690 |
} |
|
691 |
fd = (*env)->GetIntField(env, fdObj, IO_fd_fdID); |
|
692 |
||
693 |
packetBufferLen = (*env)->GetIntField(env, packet, dp_lengthID); |
|
694 |
||
695 |
if (connected) { |
|
696 |
addrp = 0; /* arg to JVM_Sendto () null in this case */ |
|
697 |
addrlen = 0; |
|
698 |
} else { |
|
699 |
if (NET_InetAddressToSockaddr(env, iaObj, packetPort, (struct sockaddr *)&rmtaddr, &addrlen, JNI_FALSE) != 0) { |
|
700 |
return; |
|
701 |
} |
|
702 |
} |
|
703 |
||
704 |
if (packetBufferLen > MAX_BUFFER_LEN) { |
|
705 |
||
706 |
/* |
|
707 |
* On 95/98 if we try to send a datagram >12k to an application |
|
708 |
* on the same machine then this will fail silently. Thus we |
|
709 |
* catch this situation here so that we can throw an exception |
|
710 |
* when this arises. |
|
711 |
* On ME if we try to send a datagram with a size greater than |
|
712 |
* that supported by the service provider then no error is |
|
713 |
* returned. |
|
714 |
*/ |
|
715 |
if (!w2k_or_later) { /* avoid this check on Win 2K or better. Does not work with IPv6. |
|
716 |
* Check is not necessary on these OSes */ |
|
717 |
if (connected) { |
|
16870 | 718 |
address = getInetAddress_addr(env, iaObj); |
2 | 719 |
} else { |
720 |
address = ntohl(rmtaddr.him4.sin_addr.s_addr); |
|
721 |
} |
|
722 |
||
723 |
if (exceedSizeLimit(env, fd, address, packetBufferLen)) { |
|
724 |
if (!((*env)->ExceptionOccurred(env))) { |
|
725 |
NET_ThrowNew(env, WSAEMSGSIZE, "Datagram send failed"); |
|
726 |
} |
|
727 |
return; |
|
728 |
} |
|
729 |
} |
|
730 |
||
731 |
/* When JNI-ifying the JDK's IO routines, we turned |
|
732 |
* read's and write's of byte arrays of size greater |
|
733 |
* than 2048 bytes into several operations of size 2048. |
|
734 |
* This saves a malloc()/memcpy()/free() for big |
|
735 |
* buffers. This is OK for file IO and TCP, but that |
|
736 |
* strategy violates the semantics of a datagram protocol. |
|
737 |
* (one big send) != (several smaller sends). So here |
|
738 |
* we *must* alloc the buffer. Note it needn't be bigger |
|
739 |
* than 65,536 (0xFFFF) the max size of an IP packet. |
|
740 |
* anything bigger is truncated anyway. |
|
741 |
*/ |
|
742 |
fullPacket = (char *)malloc(packetBufferLen); |
|
743 |
if (!fullPacket) { |
|
13245
7ab3ef5b9520
7181353: Update error message to distinguish native OOM and java OOM in net
zhouyx
parents:
10693
diff
changeset
|
744 |
JNU_ThrowOutOfMemoryError(env, "Send buf native heap allocation failed"); |
2 | 745 |
return; |
746 |
} |
|
747 |
} else { |
|
748 |
fullPacket = &(BUF[0]); |
|
749 |
} |
|
750 |
||
751 |
(*env)->GetByteArrayRegion(env, packetBuffer, packetBufferOffset, packetBufferLen, |
|
752 |
(jbyte *)fullPacket); |
|
753 |
switch (sendto(fd, fullPacket, packetBufferLen, 0, |
|
754 |
(struct sockaddr *)addrp, addrlen)) { |
|
755 |
case JVM_IO_ERR: |
|
756 |
NET_ThrowCurrent(env, "Datagram send failed"); |
|
757 |
break; |
|
758 |
||
759 |
case JVM_IO_INTR: |
|
760 |
JNU_ThrowByName(env, JNU_JAVAIOPKG "InterruptedIOException", |
|
761 |
"operation interrupted"); |
|
762 |
} |
|
763 |
||
764 |
if (packetBufferLen > MAX_BUFFER_LEN) { |
|
765 |
free(fullPacket); |
|
766 |
} |
|
767 |
} |
|
768 |
||
769 |
/* |
|
770 |
* check which socket was last serviced when there was data on both sockets. |
|
771 |
* Only call this if sure that there is data on both sockets. |
|
772 |
*/ |
|
773 |
static int checkLastFD (JNIEnv *env, jobject this, int fd, int fd1) { |
|
774 |
int nextfd, lastfd = (*env)->GetIntField(env, this, pdsi_lastfdID); |
|
775 |
if (lastfd == -1) { |
|
776 |
/* arbitrary. Choose fd */ |
|
777 |
(*env)->SetIntField(env, this, pdsi_lastfdID, fd); |
|
778 |
return fd; |
|
779 |
} else { |
|
780 |
if (lastfd == fd) { |
|
781 |
nextfd = fd1; |
|
782 |
} else { |
|
783 |
nextfd = fd; |
|
784 |
} |
|
785 |
(*env)->SetIntField(env, this, pdsi_lastfdID, nextfd); |
|
786 |
return nextfd; |
|
787 |
} |
|
788 |
} |
|
789 |
||
790 |
/* |
|
791 |
* Class: java_net_TwoStacksPlainDatagramSocketImpl |
|
792 |
* Method: peek |
|
793 |
* Signature: (Ljava/net/InetAddress;)I |
|
794 |
*/ |
|
795 |
JNIEXPORT jint JNICALL |
|
796 |
Java_java_net_TwoStacksPlainDatagramSocketImpl_peek(JNIEnv *env, jobject this, |
|
797 |
jobject addressObj) { |
|
798 |
||
799 |
jobject fdObj = (*env)->GetObjectField(env, this, pdsi_fdID); |
|
800 |
jint timeout = (*env)->GetIntField(env, this, pdsi_timeoutID); |
|
801 |
jint fd; |
|
802 |
||
803 |
/* The address and family fields of addressObj */ |
|
804 |
jint address, family; |
|
805 |
||
806 |
int n; |
|
807 |
struct sockaddr_in remote_addr; |
|
808 |
jint remote_addrsize = sizeof (remote_addr); |
|
809 |
char buf[1]; |
|
810 |
BOOL retry; |
|
811 |
jlong prevTime = 0; |
|
812 |
||
813 |
if (IS_NULL(fdObj)) { |
|
814 |
JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException", "Socket closed"); |
|
815 |
return -1; |
|
816 |
} else { |
|
817 |
fd = (*env)->GetIntField(env, fdObj, IO_fd_fdID); |
|
818 |
if (fd < 0) { |
|
819 |
JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException", |
|
820 |
"socket closed"); |
|
821 |
return -1; |
|
822 |
} |
|
823 |
} |
|
824 |
if (IS_NULL(addressObj)) { |
|
825 |
JNU_ThrowNullPointerException(env, "Null address in peek()"); |
|
826 |
} else { |
|
16870 | 827 |
address = getInetAddress_addr(env, addressObj); |
2 | 828 |
/* We only handle IPv4 for now. Will support IPv6 once its in the os */ |
829 |
family = AF_INET; |
|
830 |
} |
|
831 |
||
832 |
do { |
|
833 |
retry = FALSE; |
|
834 |
||
835 |
/* |
|
836 |
* If a timeout has been specified then we select on the socket |
|
837 |
* waiting for a read event or a timeout. |
|
838 |
*/ |
|
839 |
if (timeout) { |
|
840 |
int ret; |
|
841 |
prevTime = JVM_CurrentTimeMillis(env, 0); |
|
842 |
ret = NET_Timeout (fd, timeout); |
|
843 |
if (ret == 0) { |
|
844 |
JNU_ThrowByName(env, JNU_JAVANETPKG "SocketTimeoutException", |
|
845 |
"Peek timed out"); |
|
846 |
return ret; |
|
847 |
} else if (ret == JVM_IO_ERR) { |
|
848 |
NET_ThrowCurrent(env, "timeout in datagram socket peek"); |
|
849 |
return ret; |
|
850 |
} else if (ret == JVM_IO_INTR) { |
|
851 |
JNU_ThrowByName(env, JNU_JAVAIOPKG "InterruptedIOException", |
|
852 |
"operation interrupted"); |
|
853 |
return ret; |
|
854 |
} |
|
855 |
} |
|
856 |
||
857 |
/* now try the peek */ |
|
858 |
n = recvfrom(fd, buf, 1, MSG_PEEK, |
|
859 |
(struct sockaddr *)&remote_addr, &remote_addrsize); |
|
860 |
||
861 |
if (n == JVM_IO_ERR) { |
|
862 |
if (WSAGetLastError() == WSAECONNRESET) { |
|
863 |
jboolean connected; |
|
864 |
||
865 |
/* |
|
866 |
* An icmp port unreachable - we must receive this as Windows |
|
867 |
* does not reset the state of the socket until this has been |
|
868 |
* received. |
|
869 |
*/ |
|
870 |
purgeOutstandingICMP(env, this, fd); |
|
871 |
||
872 |
connected = (*env)->GetBooleanField(env, this, pdsi_connected); |
|
873 |
if (connected) { |
|
874 |
JNU_ThrowByName(env, JNU_JAVANETPKG "PortUnreachableException", |
|
875 |
"ICMP Port Unreachable"); |
|
876 |
return 0; |
|
877 |
} |
|
878 |
||
879 |
/* |
|
880 |
* If a timeout was specified then we need to adjust it because |
|
881 |
* we may have used up some of the timeout befor the icmp port |
|
882 |
* unreachable arrived. |
|
883 |
*/ |
|
884 |
if (timeout) { |
|
885 |
jlong newTime = JVM_CurrentTimeMillis(env, 0); |
|
910 | 886 |
timeout -= (jint)(newTime - prevTime); |
2 | 887 |
if (timeout <= 0) { |
888 |
JNU_ThrowByName(env, JNU_JAVANETPKG "SocketTimeoutException", |
|
889 |
"Receive timed out"); |
|
890 |
return 0; |
|
891 |
} |
|
892 |
prevTime = newTime; |
|
893 |
} |
|
894 |
||
895 |
/* Need to retry the recv */ |
|
896 |
retry = TRUE; |
|
897 |
} |
|
898 |
} |
|
899 |
} while (retry); |
|
900 |
||
901 |
if (n == JVM_IO_ERR && WSAGetLastError() != WSAEMSGSIZE) { |
|
902 |
NET_ThrowCurrent(env, "Datagram peek failed"); |
|
903 |
return 0; |
|
904 |
} |
|
905 |
if (n == JVM_IO_INTR) { |
|
906 |
JNU_ThrowByName(env, JNU_JAVAIOPKG "InterruptedIOException", 0); |
|
907 |
return 0; |
|
908 |
} |
|
16870 | 909 |
setInetAddress_addr(env, addressObj, ntohl(remote_addr.sin_addr.s_addr)); |
910 |
setInetAddress_family(env, addressObj, IPv4); |
|
2 | 911 |
|
912 |
/* return port */ |
|
913 |
return ntohs(remote_addr.sin_port); |
|
914 |
} |
|
915 |
||
916 |
JNIEXPORT jint JNICALL |
|
917 |
Java_java_net_TwoStacksPlainDatagramSocketImpl_peekData(JNIEnv *env, jobject this, |
|
918 |
jobject packet) { |
|
919 |
||
920 |
char BUF[MAX_BUFFER_LEN]; |
|
921 |
char *fullPacket; |
|
922 |
jobject fdObj = (*env)->GetObjectField(env, this, pdsi_fdID); |
|
923 |
jobject fd1Obj = (*env)->GetObjectField(env, this, pdsi_fd1ID); |
|
924 |
jint timeout = (*env)->GetIntField(env, this, pdsi_timeoutID); |
|
925 |
||
926 |
jbyteArray packetBuffer; |
|
927 |
jint packetBufferOffset, packetBufferLen; |
|
928 |
||
929 |
int fd, fd1, fduse, nsockets=0, errorCode; |
|
930 |
int port; |
|
931 |
||
910 | 932 |
int checkBoth = 0; |
2 | 933 |
int n; |
934 |
SOCKETADDRESS remote_addr; |
|
935 |
jint remote_addrsize=sizeof(remote_addr); |
|
936 |
BOOL retry; |
|
937 |
jlong prevTime = 0; |
|
938 |
||
939 |
if (!IS_NULL(fdObj)) { |
|
940 |
fd = (*env)->GetIntField(env, fdObj, IO_fd_fdID); |
|
941 |
if (fd < 0) { |
|
942 |
JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException", |
|
943 |
"socket closed"); |
|
944 |
return -1; |
|
945 |
} |
|
946 |
nsockets = 1; |
|
947 |
} |
|
948 |
||
949 |
if (!IS_NULL(fd1Obj)) { |
|
950 |
fd1 = (*env)->GetIntField(env, fd1Obj, IO_fd_fdID); |
|
951 |
if (fd1 < 0) { |
|
952 |
JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException", |
|
953 |
"socket closed"); |
|
954 |
return -1; |
|
955 |
} |
|
956 |
nsockets ++; |
|
957 |
} |
|
958 |
||
959 |
switch (nsockets) { |
|
960 |
case 0: |
|
961 |
JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException", |
|
962 |
"socket closed"); |
|
963 |
return -1; |
|
964 |
case 1: |
|
965 |
if (!IS_NULL(fdObj)) { |
|
966 |
fduse = fd; |
|
967 |
} else { |
|
968 |
fduse = fd1; |
|
969 |
} |
|
970 |
break; |
|
971 |
case 2: |
|
972 |
checkBoth = TRUE; |
|
973 |
break; |
|
974 |
} |
|
975 |
||
976 |
if (IS_NULL(packet)) { |
|
977 |
JNU_ThrowNullPointerException(env, "packet"); |
|
978 |
return -1; |
|
979 |
} |
|
980 |
||
981 |
packetBuffer = (*env)->GetObjectField(env, packet, dp_bufID); |
|
982 |
||
983 |
if (IS_NULL(packetBuffer)) { |
|
984 |
JNU_ThrowNullPointerException(env, "packet buffer"); |
|
985 |
return -1; |
|
986 |
} |
|
987 |
||
988 |
packetBufferOffset = (*env)->GetIntField(env, packet, dp_offsetID); |
|
989 |
packetBufferLen = (*env)->GetIntField(env, packet, dp_bufLengthID); |
|
990 |
||
991 |
if (packetBufferLen > MAX_BUFFER_LEN) { |
|
992 |
||
993 |
/* When JNI-ifying the JDK's IO routines, we turned |
|
994 |
* read's and write's of byte arrays of size greater |
|
995 |
* than 2048 bytes into several operations of size 2048. |
|
996 |
* This saves a malloc()/memcpy()/free() for big |
|
997 |
* buffers. This is OK for file IO and TCP, but that |
|
998 |
* strategy violates the semantics of a datagram protocol. |
|
999 |
* (one big send) != (several smaller sends). So here |
|
1000 |
* we *must* alloc the buffer. Note it needn't be bigger |
|
1001 |
* than 65,536 (0xFFFF) the max size of an IP packet. |
|
1002 |
* anything bigger is truncated anyway. |
|
1003 |
*/ |
|
1004 |
fullPacket = (char *)malloc(packetBufferLen); |
|
1005 |
if (!fullPacket) { |
|
13245
7ab3ef5b9520
7181353: Update error message to distinguish native OOM and java OOM in net
zhouyx
parents:
10693
diff
changeset
|
1006 |
JNU_ThrowOutOfMemoryError(env, "Native heap allocation failed"); |
2 | 1007 |
return -1; |
1008 |
} |
|
1009 |
} else { |
|
1010 |
fullPacket = &(BUF[0]); |
|
1011 |
} |
|
1012 |
||
1013 |
do { |
|
1014 |
int ret; |
|
1015 |
retry = FALSE; |
|
1016 |
||
1017 |
/* |
|
1018 |
* If a timeout has been specified then we select on the socket |
|
1019 |
* waiting for a read event or a timeout. |
|
1020 |
*/ |
|
1021 |
if (checkBoth) { |
|
1022 |
int t = timeout == 0 ? -1: timeout; |
|
1023 |
prevTime = JVM_CurrentTimeMillis(env, 0); |
|
1024 |
ret = NET_Timeout2 (fd, fd1, t, &fduse); |
|
1025 |
/* all subsequent calls to recv() or select() will use the same fd |
|
1026 |
* for this call to peek() */ |
|
1027 |
if (ret <= 0) { |
|
1028 |
if (ret == 0) { |
|
1029 |
JNU_ThrowByName(env,JNU_JAVANETPKG "SocketTimeoutException", |
|
1030 |
"Peek timed out"); |
|
1031 |
} else if (ret == JVM_IO_ERR) { |
|
1032 |
NET_ThrowCurrent(env, "timeout in datagram socket peek"); |
|
1033 |
} else if (ret == JVM_IO_INTR) { |
|
1034 |
JNU_ThrowByName(env, JNU_JAVAIOPKG "InterruptedIOException", |
|
1035 |
"operation interrupted"); |
|
1036 |
} |
|
1037 |
if (packetBufferLen > MAX_BUFFER_LEN) { |
|
1038 |
free(fullPacket); |
|
1039 |
} |
|
1040 |
return -1; |
|
1041 |
} |
|
1042 |
if (ret == 2) { |
|
1043 |
fduse = checkLastFD (env, this, fd, fd1); |
|
1044 |
} |
|
1045 |
checkBoth = FALSE; |
|
1046 |
} else if (timeout) { |
|
1047 |
if (prevTime == 0) { |
|
1048 |
prevTime = JVM_CurrentTimeMillis(env, 0); |
|
1049 |
} |
|
1050 |
ret = NET_Timeout (fduse, timeout); |
|
1051 |
if (ret <= 0) { |
|
1052 |
if (ret == 0) { |
|
1053 |
JNU_ThrowByName(env,JNU_JAVANETPKG "SocketTimeoutException", |
|
1054 |
"Receive timed out"); |
|
1055 |
} else if (ret == JVM_IO_ERR) { |
|
1056 |
JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException", |
|
1057 |
"Socket closed"); |
|
1058 |
} else if (ret == JVM_IO_INTR) { |
|
1059 |
JNU_ThrowByName(env, JNU_JAVAIOPKG "InterruptedIOException", |
|
1060 |
"operation interrupted"); |
|
1061 |
} |
|
1062 |
if (packetBufferLen > MAX_BUFFER_LEN) { |
|
1063 |
free(fullPacket); |
|
1064 |
} |
|
1065 |
return -1; |
|
1066 |
} |
|
1067 |
} |
|
1068 |
||
1069 |
/* receive the packet */ |
|
1070 |
n = recvfrom(fduse, fullPacket, packetBufferLen, MSG_PEEK, |
|
1071 |
(struct sockaddr *)&remote_addr, &remote_addrsize); |
|
1072 |
port = (int) ntohs ((u_short) GET_PORT((SOCKETADDRESS *)&remote_addr)); |
|
1073 |
if (n == JVM_IO_ERR) { |
|
1074 |
if (WSAGetLastError() == WSAECONNRESET) { |
|
1075 |
jboolean connected; |
|
1076 |
||
1077 |
/* |
|
1078 |
* An icmp port unreachable - we must receive this as Windows |
|
1079 |
* does not reset the state of the socket until this has been |
|
1080 |
* received. |
|
1081 |
*/ |
|
1082 |
purgeOutstandingICMP(env, this, fduse); |
|
1083 |
||
1084 |
connected = (*env)->GetBooleanField(env, this, pdsi_connected); |
|
1085 |
if (connected) { |
|
1086 |
JNU_ThrowByName(env, JNU_JAVANETPKG "PortUnreachableException", |
|
1087 |
"ICMP Port Unreachable"); |
|
1088 |
||
1089 |
if (packetBufferLen > MAX_BUFFER_LEN) { |
|
1090 |
free(fullPacket); |
|
1091 |
} |
|
1092 |
return -1; |
|
1093 |
} |
|
1094 |
||
1095 |
/* |
|
1096 |
* If a timeout was specified then we need to adjust it because |
|
1097 |
* we may have used up some of the timeout befor the icmp port |
|
1098 |
* unreachable arrived. |
|
1099 |
*/ |
|
1100 |
if (timeout) { |
|
1101 |
jlong newTime = JVM_CurrentTimeMillis(env, 0); |
|
910 | 1102 |
timeout -= (jint)(newTime - prevTime); |
2 | 1103 |
if (timeout <= 0) { |
1104 |
JNU_ThrowByName(env, JNU_JAVANETPKG "SocketTimeoutException", |
|
1105 |
"Receive timed out"); |
|
1106 |
if (packetBufferLen > MAX_BUFFER_LEN) { |
|
1107 |
free(fullPacket); |
|
1108 |
} |
|
1109 |
return -1; |
|
1110 |
} |
|
1111 |
prevTime = newTime; |
|
1112 |
} |
|
1113 |
retry = TRUE; |
|
1114 |
} |
|
1115 |
} |
|
1116 |
} while (retry); |
|
1117 |
||
1118 |
/* truncate the data if the packet's length is too small */ |
|
1119 |
if (n > packetBufferLen) { |
|
1120 |
n = packetBufferLen; |
|
1121 |
} |
|
1122 |
if (n < 0) { |
|
1123 |
errorCode = WSAGetLastError(); |
|
1124 |
/* check to see if it's because the buffer was too small */ |
|
1125 |
if (errorCode == WSAEMSGSIZE) { |
|
1126 |
/* it is because the buffer is too small. It's UDP, it's |
|
1127 |
* unreliable, it's all good. discard the rest of the |
|
1128 |
* data.. |
|
1129 |
*/ |
|
1130 |
n = packetBufferLen; |
|
1131 |
} else { |
|
1132 |
/* failure */ |
|
1133 |
(*env)->SetIntField(env, packet, dp_lengthID, 0); |
|
1134 |
} |
|
1135 |
} |
|
1136 |
if (n == -1) { |
|
1137 |
JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException", "socket closed"); |
|
1138 |
} else if (n == -2) { |
|
1139 |
JNU_ThrowByName(env, JNU_JAVAIOPKG "InterruptedIOException", |
|
1140 |
"operation interrupted"); |
|
1141 |
} else if (n < 0) { |
|
1142 |
NET_ThrowCurrent(env, "Datagram receive failed"); |
|
1143 |
} else { |
|
1144 |
jobject packetAddress; |
|
1145 |
||
1146 |
/* |
|
1147 |
* Check if there is an InetAddress already associated with this |
|
1148 |
* packet. If so we check if it is the same source address. We |
|
1149 |
* can't update any existing InetAddress because it is immutable |
|
1150 |
*/ |
|
1151 |
packetAddress = (*env)->GetObjectField(env, packet, dp_addressID); |
|
1152 |
if (packetAddress != NULL) { |
|
1153 |
if (!NET_SockaddrEqualsInetAddress(env, (struct sockaddr *) |
|
1154 |
&remote_addr, packetAddress)) { |
|
1155 |
/* force a new InetAddress to be created */ |
|
1156 |
packetAddress = NULL; |
|
1157 |
} |
|
1158 |
} |
|
1159 |
if (packetAddress == NULL) { |
|
1160 |
packetAddress = NET_SockaddrToInetAddress(env, (struct sockaddr *) |
|
1161 |
&remote_addr, &port); |
|
1162 |
/* stuff the new Inetaddress in the packet */ |
|
1163 |
(*env)->SetObjectField(env, packet, dp_addressID, packetAddress); |
|
1164 |
} |
|
1165 |
||
1166 |
/* populate the packet */ |
|
1167 |
(*env)->SetByteArrayRegion(env, packetBuffer, packetBufferOffset, n, |
|
1168 |
(jbyte *)fullPacket); |
|
1169 |
(*env)->SetIntField(env, packet, dp_portID, port); |
|
1170 |
(*env)->SetIntField(env, packet, dp_lengthID, n); |
|
1171 |
} |
|
1172 |
||
1173 |
/* make sure receive() picks up the right fd */ |
|
1174 |
(*env)->SetIntField(env, this, pdsi_fduseID, fduse); |
|
1175 |
||
1176 |
if (packetBufferLen > MAX_BUFFER_LEN) { |
|
1177 |
free(fullPacket); |
|
1178 |
} |
|
1179 |
return port; |
|
1180 |
} |
|
1181 |
||
1182 |
/* |
|
1183 |
* Class: java_net_TwoStacksPlainDatagramSocketImpl |
|
1184 |
* Method: receive |
|
1185 |
* Signature: (Ljava/net/DatagramPacket;)V |
|
1186 |
*/ |
|
1187 |
JNIEXPORT void JNICALL |
|
1188 |
Java_java_net_TwoStacksPlainDatagramSocketImpl_receive0(JNIEnv *env, jobject this, |
|
1189 |
jobject packet) { |
|
1190 |
||
1191 |
char BUF[MAX_BUFFER_LEN]; |
|
1192 |
char *fullPacket; |
|
1193 |
jobject fdObj = (*env)->GetObjectField(env, this, pdsi_fdID); |
|
1194 |
jobject fd1Obj = (*env)->GetObjectField(env, this, pdsi_fd1ID); |
|
1195 |
jint timeout = (*env)->GetIntField(env, this, pdsi_timeoutID); |
|
1196 |
jbyteArray packetBuffer; |
|
1197 |
jint packetBufferOffset, packetBufferLen; |
|
1198 |
int ipv6_supported = ipv6_available(); |
|
1199 |
||
1200 |
/* as a result of the changes for ipv6, peek() or peekData() |
|
1201 |
* must be called prior to receive() so that fduse can be set. |
|
1202 |
*/ |
|
1203 |
int fd, fd1, fduse, errorCode; |
|
1204 |
||
1205 |
int n, nsockets=0; |
|
1206 |
SOCKETADDRESS remote_addr; |
|
1207 |
jint remote_addrsize=sizeof(remote_addr); |
|
1208 |
BOOL retry; |
|
1209 |
jlong prevTime = 0, selectTime=0; |
|
1210 |
jboolean connected; |
|
1211 |
||
1212 |
if (IS_NULL(fdObj) && IS_NULL(fd1Obj)) { |
|
1213 |
JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException", |
|
1214 |
"Socket closed"); |
|
1215 |
return; |
|
1216 |
} |
|
1217 |
||
1218 |
if (!IS_NULL(fdObj)) { |
|
1219 |
fd = (*env)->GetIntField(env, fdObj, IO_fd_fdID); |
|
1220 |
nsockets ++; |
|
1221 |
} |
|
1222 |
if (!IS_NULL(fd1Obj)) { |
|
1223 |
fd1 = (*env)->GetIntField(env, fd1Obj, IO_fd_fdID); |
|
1224 |
nsockets ++; |
|
1225 |
} |
|
1226 |
||
1227 |
if (nsockets == 2) { /* need to choose one of them */ |
|
1228 |
/* was fduse set in peek? */ |
|
1229 |
fduse = (*env)->GetIntField(env, this, pdsi_fduseID); |
|
1230 |
if (fduse == -1) { |
|
1231 |
/* not set in peek(), must select on both sockets */ |
|
1232 |
int ret, t = (timeout == 0) ? -1: timeout; |
|
1233 |
ret = NET_Timeout2 (fd, fd1, t, &fduse); |
|
1234 |
if (ret == 2) { |
|
1235 |
fduse = checkLastFD (env, this, fd, fd1); |
|
1236 |
} else if (ret <= 0) { |
|
1237 |
if (ret == 0) { |
|
1238 |
JNU_ThrowByName(env, JNU_JAVANETPKG "SocketTimeoutException", |
|
1239 |
"Receive timed out"); |
|
1240 |
} else if (ret == JVM_IO_ERR) { |
|
1241 |
JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException", |
|
1242 |
"Socket closed"); |
|
1243 |
} else if (ret == JVM_IO_INTR) { |
|
1244 |
JNU_ThrowByName(env, JNU_JAVAIOPKG "InterruptedIOException", |
|
1245 |
"operation interrupted"); |
|
1246 |
} |
|
1247 |
return; |
|
1248 |
} |
|
1249 |
} |
|
1250 |
} else if (!ipv6_supported) { |
|
1251 |
fduse = fd; |
|
1252 |
} else if (IS_NULL(fdObj)) { |
|
1253 |
/* ipv6 supported: and this socket bound to an IPV6 only address */ |
|
1254 |
fduse = fd1; |
|
1255 |
} else { |
|
1256 |
/* ipv6 supported: and this socket bound to an IPV4 only address */ |
|
1257 |
fduse = fd; |
|
1258 |
} |
|
1259 |
||
1260 |
if (IS_NULL(packet)) { |
|
1261 |
JNU_ThrowNullPointerException(env, "packet"); |
|
1262 |
return; |
|
1263 |
} |
|
1264 |
||
1265 |
packetBuffer = (*env)->GetObjectField(env, packet, dp_bufID); |
|
1266 |
||
1267 |
if (IS_NULL(packetBuffer)) { |
|
1268 |
JNU_ThrowNullPointerException(env, "packet buffer"); |
|
1269 |
return; |
|
1270 |
} |
|
1271 |
||
1272 |
packetBufferOffset = (*env)->GetIntField(env, packet, dp_offsetID); |
|
1273 |
packetBufferLen = (*env)->GetIntField(env, packet, dp_bufLengthID); |
|
1274 |
||
1275 |
if (packetBufferLen > MAX_BUFFER_LEN) { |
|
1276 |
||
1277 |
/* When JNI-ifying the JDK's IO routines, we turned |
|
1278 |
* read's and write's of byte arrays of size greater |
|
1279 |
* than 2048 bytes into several operations of size 2048. |
|
1280 |
* This saves a malloc()/memcpy()/free() for big |
|
1281 |
* buffers. This is OK for file IO and TCP, but that |
|
1282 |
* strategy violates the semantics of a datagram protocol. |
|
1283 |
* (one big send) != (several smaller sends). So here |
|
1284 |
* we *must* alloc the buffer. Note it needn't be bigger |
|
1285 |
* than 65,536 (0xFFFF) the max size of an IP packet. |
|
1286 |
* anything bigger is truncated anyway. |
|
1287 |
*/ |
|
1288 |
fullPacket = (char *)malloc(packetBufferLen); |
|
1289 |
if (!fullPacket) { |
|
13245
7ab3ef5b9520
7181353: Update error message to distinguish native OOM and java OOM in net
zhouyx
parents:
10693
diff
changeset
|
1290 |
JNU_ThrowOutOfMemoryError(env, "Receive buf native heap allocation failed"); |
2 | 1291 |
return; |
1292 |
} |
|
1293 |
} else { |
|
1294 |
fullPacket = &(BUF[0]); |
|
1295 |
} |
|
1296 |
||
1297 |
||
1298 |
||
1299 |
/* |
|
1300 |
* If this Windows edition supports ICMP port unreachable and if we |
|
1301 |
* are not connected then we need to know if a timeout has been specified |
|
1302 |
* and if so we need to pick up the current time. These are required in |
|
1303 |
* order to implement the semantics of timeout, viz :- |
|
1304 |
* timeout set to t1 but ICMP port unreachable arrives in t2 where |
|
1305 |
* t2 < t1. In this case we must discard the ICMP packets and then |
|
1306 |
* wait for the next packet up to a maximum of t1 minus t2. |
|
1307 |
*/ |
|
1308 |
connected = (*env)->GetBooleanField(env, this, pdsi_connected); |
|
1309 |
if (supportPortUnreachable() && !connected && timeout &&!ipv6_supported) { |
|
1310 |
prevTime = JVM_CurrentTimeMillis(env, 0); |
|
1311 |
} |
|
1312 |
||
1313 |
if (timeout && nsockets == 1) { |
|
1314 |
int ret; |
|
1315 |
ret = NET_Timeout(fduse, timeout); |
|
1316 |
if (ret <= 0) { |
|
1317 |
if (ret == 0) { |
|
1318 |
JNU_ThrowByName(env, JNU_JAVANETPKG "SocketTimeoutException", |
|
1319 |
"Receive timed out"); |
|
1320 |
} else if (ret == JVM_IO_ERR) { |
|
1321 |
JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException", |
|
1322 |
"Socket closed"); |
|
1323 |
} else if (ret == JVM_IO_INTR) { |
|
1324 |
JNU_ThrowByName(env, JNU_JAVAIOPKG "InterruptedIOException", |
|
1325 |
"operation interrupted"); |
|
1326 |
} |
|
1327 |
if (packetBufferLen > MAX_BUFFER_LEN) { |
|
1328 |
free(fullPacket); |
|
1329 |
} |
|
1330 |
return; |
|
1331 |
} |
|
1332 |
} |
|
1333 |
||
1334 |
/* |
|
1335 |
* Loop only if we discarding ICMP port unreachable packets |
|
1336 |
*/ |
|
1337 |
do { |
|
1338 |
retry = FALSE; |
|
1339 |
||
1340 |
/* receive the packet */ |
|
1341 |
n = recvfrom(fduse, fullPacket, packetBufferLen, 0, |
|
1342 |
(struct sockaddr *)&remote_addr, &remote_addrsize); |
|
1343 |
||
1344 |
if (n == JVM_IO_ERR) { |
|
1345 |
if (WSAGetLastError() == WSAECONNRESET) { |
|
1346 |
/* |
|
1347 |
* An icmp port unreachable has been received - consume any other |
|
1348 |
* outstanding packets. |
|
1349 |
*/ |
|
1350 |
purgeOutstandingICMP(env, this, fduse); |
|
1351 |
||
1352 |
/* |
|
1353 |
* If connected throw a PortUnreachableException |
|
1354 |
*/ |
|
1355 |
||
1356 |
if (connected) { |
|
1357 |
JNU_ThrowByName(env, JNU_JAVANETPKG "PortUnreachableException", |
|
1358 |
"ICMP Port Unreachable"); |
|
1359 |
||
1360 |
if (packetBufferLen > MAX_BUFFER_LEN) { |
|
1361 |
free(fullPacket); |
|
1362 |
} |
|
1363 |
||
1364 |
return; |
|
1365 |
} |
|
1366 |
||
1367 |
/* |
|
1368 |
* If a timeout was specified then we need to adjust it because |
|
1369 |
* we may have used up some of the timeout before the icmp port |
|
1370 |
* unreachable arrived. |
|
1371 |
*/ |
|
1372 |
if (timeout) { |
|
1373 |
int ret; |
|
1374 |
jlong newTime = JVM_CurrentTimeMillis(env, 0); |
|
910 | 1375 |
timeout -= (jint)(newTime - prevTime); |
2 | 1376 |
prevTime = newTime; |
1377 |
||
1378 |
if (timeout <= 0) { |
|
1379 |
ret = 0; |
|
1380 |
} else { |
|
1381 |
ret = NET_Timeout(fduse, timeout); |
|
1382 |
} |
|
1383 |
||
1384 |
if (ret <= 0) { |
|
1385 |
if (ret == 0) { |
|
1386 |
JNU_ThrowByName(env, JNU_JAVANETPKG "SocketTimeoutException", |
|
1387 |
"Receive timed out"); |
|
1388 |
} else if (ret == JVM_IO_ERR) { |
|
1389 |
JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException", |
|
1390 |
"Socket closed"); |
|
1391 |
} else if (ret == JVM_IO_INTR) { |
|
1392 |
JNU_ThrowByName(env, JNU_JAVAIOPKG "InterruptedIOException", |
|
1393 |
"operation interrupted"); |
|
1394 |
} |
|
1395 |
if (packetBufferLen > MAX_BUFFER_LEN) { |
|
1396 |
free(fullPacket); |
|
1397 |
} |
|
1398 |
return; |
|
1399 |
} |
|
1400 |
} |
|
1401 |
||
1402 |
/* |
|
1403 |
* An ICMP port unreachable was received but we are |
|
1404 |
* not connected so ignore it. |
|
1405 |
*/ |
|
1406 |
retry = TRUE; |
|
1407 |
} |
|
1408 |
} |
|
1409 |
} while (retry); |
|
1410 |
||
1411 |
/* truncate the data if the packet's length is too small */ |
|
1412 |
if (n > packetBufferLen) { |
|
1413 |
n = packetBufferLen; |
|
1414 |
} |
|
1415 |
if (n < 0) { |
|
1416 |
errorCode = WSAGetLastError(); |
|
1417 |
/* check to see if it's because the buffer was too small */ |
|
1418 |
if (errorCode == WSAEMSGSIZE) { |
|
1419 |
/* it is because the buffer is too small. It's UDP, it's |
|
1420 |
* unreliable, it's all good. discard the rest of the |
|
1421 |
* data.. |
|
1422 |
*/ |
|
1423 |
n = packetBufferLen; |
|
1424 |
} else { |
|
1425 |
/* failure */ |
|
1426 |
(*env)->SetIntField(env, packet, dp_lengthID, 0); |
|
1427 |
} |
|
1428 |
} |
|
1429 |
if (n == -1) { |
|
1430 |
JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException", "socket closed"); |
|
1431 |
} else if (n == -2) { |
|
1432 |
JNU_ThrowByName(env, JNU_JAVAIOPKG "InterruptedIOException", |
|
1433 |
"operation interrupted"); |
|
1434 |
} else if (n < 0) { |
|
1435 |
NET_ThrowCurrent(env, "Datagram receive failed"); |
|
1436 |
} else { |
|
1437 |
int port; |
|
1438 |
jobject packetAddress; |
|
1439 |
||
1440 |
/* |
|
1441 |
* Check if there is an InetAddress already associated with this |
|
1442 |
* packet. If so we check if it is the same source address. We |
|
1443 |
* can't update any existing InetAddress because it is immutable |
|
1444 |
*/ |
|
1445 |
packetAddress = (*env)->GetObjectField(env, packet, dp_addressID); |
|
1446 |
||
1447 |
if (packetAddress != NULL) { |
|
1448 |
if (!NET_SockaddrEqualsInetAddress(env, (struct sockaddr *)&remote_addr, packetAddress)) { |
|
1449 |
/* force a new InetAddress to be created */ |
|
1450 |
packetAddress = NULL; |
|
1451 |
} |
|
1452 |
} |
|
1453 |
if (packetAddress == NULL) { |
|
1454 |
packetAddress = NET_SockaddrToInetAddress(env, (struct sockaddr *)&remote_addr, &port); |
|
1455 |
/* stuff the new Inetaddress in the packet */ |
|
1456 |
(*env)->SetObjectField(env, packet, dp_addressID, packetAddress); |
|
1457 |
} else { |
|
1458 |
/* only get the new port number */ |
|
1459 |
port = NET_GetPortFromSockaddr((struct sockaddr *)&remote_addr); |
|
1460 |
} |
|
1461 |
/* populate the packet */ |
|
1462 |
(*env)->SetByteArrayRegion(env, packetBuffer, packetBufferOffset, n, |
|
1463 |
(jbyte *)fullPacket); |
|
1464 |
(*env)->SetIntField(env, packet, dp_portID, port); |
|
1465 |
(*env)->SetIntField(env, packet, dp_lengthID, n); |
|
1466 |
} |
|
1467 |
if (packetBufferLen > MAX_BUFFER_LEN) { |
|
1468 |
free(fullPacket); |
|
1469 |
} |
|
1470 |
} |
|
1471 |
||
1472 |
/* |
|
1473 |
* Class: java_net_TwoStacksPlainDatagramSocketImpl |
|
1474 |
* Method: datagramSocketCreate |
|
1475 |
* Signature: ()V |
|
1476 |
*/ |
|
1477 |
JNIEXPORT void JNICALL |
|
1478 |
Java_java_net_TwoStacksPlainDatagramSocketImpl_datagramSocketCreate(JNIEnv *env, |
|
1479 |
jobject this) { |
|
1480 |
jobject fdObj = (*env)->GetObjectField(env, this, pdsi_fdID); |
|
1481 |
jobject fd1Obj = (*env)->GetObjectField(env, this, pdsi_fd1ID); |
|
1482 |
||
1483 |
int fd, fd1; |
|
1484 |
int t = TRUE; |
|
1485 |
DWORD x1, x2; /* ignored result codes */ |
|
1486 |
int ipv6_supported = ipv6_available(); |
|
1487 |
||
1488 |
int arg = -1; |
|
1489 |
||
1490 |
if (IS_NULL(fdObj) || (ipv6_supported && IS_NULL(fd1Obj))) { |
|
1491 |
JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException", "Socket closed"); |
|
1492 |
return; |
|
1493 |
} else { |
|
1494 |
fd = (int) socket (AF_INET, SOCK_DGRAM, 0); |
|
1495 |
} |
|
1496 |
if (fd == JVM_IO_ERR) { |
|
1497 |
NET_ThrowCurrent(env, "Socket creation failed"); |
|
1498 |
return; |
|
1499 |
} |
|
1500 |
SetHandleInformation((HANDLE)(UINT_PTR)fd, HANDLE_FLAG_INHERIT, FALSE); |
|
1501 |
(*env)->SetIntField(env, fdObj, IO_fd_fdID, fd); |
|
1502 |
NET_SetSockOpt(fd, SOL_SOCKET, SO_BROADCAST, (char*)&t, sizeof(BOOL)); |
|
1503 |
||
1504 |
if (ipv6_supported) { |
|
1505 |
/* SIO_UDP_CONNRESET fixes a bug introduced in Windows 2000, which |
|
1506 |
* returns connection reset errors un connected UDP sockets (as well |
|
1507 |
* as connected sockets. The solution is to only enable this feature |
|
1508 |
* when the socket is connected |
|
1509 |
*/ |
|
1510 |
t = FALSE; |
|
1511 |
WSAIoctl(fd,SIO_UDP_CONNRESET,&t,sizeof(t),&x1,sizeof(x1),&x2,0,0); |
|
1512 |
t = TRUE; |
|
1513 |
fd1 = socket (AF_INET6, SOCK_DGRAM, 0); |
|
1514 |
if (fd1 == JVM_IO_ERR) { |
|
1515 |
NET_ThrowCurrent(env, "Socket creation failed"); |
|
1516 |
return; |
|
1517 |
} |
|
1518 |
NET_SetSockOpt(fd1, SOL_SOCKET, SO_BROADCAST, (char*)&t, sizeof(BOOL)); |
|
1519 |
t = FALSE; |
|
1520 |
WSAIoctl(fd1,SIO_UDP_CONNRESET,&t,sizeof(t),&x1,sizeof(x1),&x2,0,0); |
|
1521 |
(*env)->SetIntField(env, fd1Obj, IO_fd_fdID, fd1); |
|
1522 |
SetHandleInformation((HANDLE)(UINT_PTR)fd1, HANDLE_FLAG_INHERIT, FALSE); |
|
1523 |
} else { |
|
1524 |
/* drop the second fd */ |
|
1525 |
(*env)->SetObjectField(env, this, pdsi_fd1ID, NULL); |
|
1526 |
} |
|
1527 |
} |
|
1528 |
||
1529 |
/* |
|
1530 |
* Class: java_net_TwoStacksPlainDatagramSocketImpl |
|
1531 |
* Method: datagramSocketClose |
|
1532 |
* Signature: ()V |
|
1533 |
*/ |
|
1534 |
JNIEXPORT void JNICALL |
|
1535 |
Java_java_net_TwoStacksPlainDatagramSocketImpl_datagramSocketClose(JNIEnv *env, |
|
1536 |
jobject this) { |
|
1537 |
/* |
|
1538 |
* REMIND: PUT A LOCK AROUND THIS CODE |
|
1539 |
*/ |
|
1540 |
jobject fdObj = (*env)->GetObjectField(env, this, pdsi_fdID); |
|
1541 |
jobject fd1Obj = (*env)->GetObjectField(env, this, pdsi_fd1ID); |
|
1542 |
int ipv6_supported = ipv6_available(); |
|
1543 |
int fd=-1, fd1=-1; |
|
1544 |
||
1545 |
if (IS_NULL(fdObj) && (!ipv6_supported || IS_NULL(fd1Obj))) { |
|
1546 |
return; |
|
1547 |
} |
|
1548 |
||
1549 |
if (!IS_NULL(fdObj)) { |
|
1550 |
fd = (*env)->GetIntField(env, fdObj, IO_fd_fdID); |
|
1551 |
if (fd != -1) { |
|
1552 |
(*env)->SetIntField(env, fdObj, IO_fd_fdID, -1); |
|
1553 |
NET_SocketClose(fd); |
|
1554 |
} |
|
1555 |
} |
|
1556 |
||
1557 |
if (ipv6_supported && fd1Obj != NULL) { |
|
1558 |
fd1 = (*env)->GetIntField(env, fd1Obj, IO_fd_fdID); |
|
1559 |
if (fd1 == -1) { |
|
1560 |
return; |
|
1561 |
} |
|
1562 |
(*env)->SetIntField(env, fd1Obj, IO_fd_fdID, -1); |
|
1563 |
NET_SocketClose(fd1); |
|
1564 |
} |
|
1565 |
} |
|
1566 |
||
1567 |
/* |
|
1568 |
* check the addresses attached to the NetworkInterface object |
|
1569 |
* and return the first one (of the requested family Ipv4 or Ipv6) |
|
1570 |
* in *iaddr |
|
1571 |
*/ |
|
1572 |
||
1573 |
static int getInetAddrFromIf (JNIEnv *env, int family, jobject nif, jobject *iaddr) |
|
1574 |
{ |
|
1575 |
jobjectArray addrArray; |
|
1576 |
static jfieldID ni_addrsID=0; |
|
1577 |
jsize len; |
|
1578 |
jobject addr; |
|
1579 |
int i; |
|
1580 |
||
16870 | 1581 |
if (ni_addrsID == NULL ) { |
2 | 1582 |
jclass c = (*env)->FindClass(env, "java/net/NetworkInterface"); |
1583 |
CHECK_NULL_RETURN (c, -1); |
|
1584 |
ni_addrsID = (*env)->GetFieldID(env, c, "addrs", |
|
1585 |
"[Ljava/net/InetAddress;"); |
|
1586 |
CHECK_NULL_RETURN (ni_addrsID, -1); |
|
1587 |
} |
|
1588 |
||
1589 |
addrArray = (*env)->GetObjectField(env, nif, ni_addrsID); |
|
1590 |
len = (*env)->GetArrayLength(env, addrArray); |
|
1591 |
||
1592 |
/* |
|
1593 |
* Check that there is at least one address bound to this |
|
1594 |
* interface. |
|
1595 |
*/ |
|
1596 |
if (len < 1) { |
|
1597 |
JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException", |
|
1598 |
"bad argument for IP_MULTICAST_IF2: No IP addresses bound to interface"); |
|
1599 |
return -1; |
|
1600 |
} |
|
1601 |
for (i=0; i<len; i++) { |
|
1602 |
int fam; |
|
1603 |
addr = (*env)->GetObjectArrayElement(env, addrArray, i); |
|
16870 | 1604 |
fam = getInetAddress_family(env, addr); |
2 | 1605 |
if (fam == family) { |
1606 |
*iaddr = addr; |
|
1607 |
return 0; |
|
1608 |
} |
|
1609 |
} |
|
1610 |
return -1; |
|
1611 |
} |
|
1612 |
||
1613 |
static int getInet4AddrFromIf (JNIEnv *env, jobject nif, struct in_addr *iaddr) |
|
1614 |
{ |
|
1615 |
jobject addr; |
|
1616 |
||
1617 |
int ret = getInetAddrFromIf (env, IPv4, nif, &addr); |
|
1618 |
if (ret == -1) { |
|
1619 |
return -1; |
|
1620 |
} |
|
1621 |
||
16870 | 1622 |
iaddr->s_addr = htonl(getInetAddress_addr(env, addr)); |
2 | 1623 |
return 0; |
1624 |
} |
|
1625 |
||
1626 |
/* Get the multicasting index from the interface */ |
|
1627 |
||
1628 |
static int getIndexFromIf (JNIEnv *env, jobject nif) { |
|
1629 |
static jfieldID ni_indexID; |
|
1630 |
||
1631 |
if (ni_indexID == NULL) { |
|
1632 |
jclass c = (*env)->FindClass(env, "java/net/NetworkInterface"); |
|
1633 |
CHECK_NULL_RETURN(c, -1); |
|
1634 |
ni_indexID = (*env)->GetFieldID(env, c, "index", "I"); |
|
1635 |
CHECK_NULL_RETURN(ni_indexID, -1); |
|
1636 |
} |
|
1637 |
||
1638 |
return (*env)->GetIntField(env, nif, ni_indexID); |
|
1639 |
} |
|
1640 |
||
1641 |
/* |
|
1642 |
* Sets the multicast interface. |
|
1643 |
* |
|
1644 |
* SocketOptions.IP_MULTICAST_IF (argument is an InetAddress) :- |
|
1645 |
* IPv4: set outgoing multicast interface using |
|
1646 |
* IPPROTO_IP/IP_MULTICAST_IF |
|
1647 |
* |
|
1648 |
* IPv6: Get the interface to which the |
|
1649 |
* InetAddress is bound |
|
1650 |
* and do same as SockOptions.IF_MULTICAST_IF2 |
|
1651 |
* |
|
1652 |
* SockOptions.IF_MULTICAST_IF2 (argument is a NetworkInterface ) :- |
|
1653 |
* For each stack: |
|
1654 |
* IPv4: Obtain IP address bound to network interface |
|
1655 |
* (NetworkInterface.addres[0]) |
|
1656 |
* set outgoing multicast interface using |
|
1657 |
* IPPROTO_IP/IP_MULTICAST_IF |
|
1658 |
* |
|
1659 |
* IPv6: Obtain NetworkInterface.index |
|
1660 |
* Set outgoing multicast interface using |
|
1661 |
* IPPROTO_IPV6/IPV6_MULTICAST_IF |
|
1662 |
* |
|
1663 |
*/ |
|
1664 |
static void setMulticastInterface(JNIEnv *env, jobject this, int fd, int fd1, |
|
1665 |
jint opt, jobject value) |
|
1666 |
{ |
|
1667 |
int ipv6_supported = ipv6_available(); |
|
1668 |
||
1669 |
if (opt == java_net_SocketOptions_IP_MULTICAST_IF) { |
|
1670 |
/* |
|
1671 |
* value is an InetAddress. |
|
1672 |
* On IPv4 system use IP_MULTICAST_IF socket option |
|
1673 |
* On IPv6 system get the NetworkInterface that this IP |
|
1674 |
* address is bound to and use the IPV6_MULTICAST_IF |
|
1675 |
* option instead of IP_MULTICAST_IF |
|
1676 |
*/ |
|
1677 |
if (ipv6_supported) { |
|
1678 |
static jclass ni_class; |
|
1679 |
if (ni_class == NULL) { |
|
1680 |
jclass c = (*env)->FindClass(env, "java/net/NetworkInterface"); |
|
1681 |
CHECK_NULL(c); |
|
1682 |
ni_class = (*env)->NewGlobalRef(env, c); |
|
1683 |
CHECK_NULL(ni_class); |
|
1684 |
} |
|
1685 |
||
1686 |
value = Java_java_net_NetworkInterface_getByInetAddress0(env, ni_class, value); |
|
1687 |
if (value == NULL) { |
|
1688 |
if (!(*env)->ExceptionOccurred(env)) { |
|
1689 |
JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException", |
|
1690 |
"bad argument for IP_MULTICAST_IF" |
|
1691 |
": address not bound to any interface"); |
|
1692 |
} |
|
1693 |
return; |
|
1694 |
} |
|
1695 |
opt = java_net_SocketOptions_IP_MULTICAST_IF2; |
|
1696 |
} else { |
|
1697 |
struct in_addr in; |
|
1698 |
||
16870 | 1699 |
in.s_addr = htonl(getInetAddress_addr(env, value)); |
2 | 1700 |
|
1701 |
if (setsockopt(fd, IPPROTO_IP, IP_MULTICAST_IF, |
|
1702 |
(const char*)&in, sizeof(in)) < 0) { |
|
1703 |
NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "SocketException", |
|
1704 |
"Error setting socket option"); |
|
1705 |
} |
|
1706 |
return; |
|
1707 |
} |
|
1708 |
} |
|
1709 |
||
1710 |
if (opt == java_net_SocketOptions_IP_MULTICAST_IF2) { |
|
1711 |
/* |
|
1712 |
* value is a NetworkInterface. |
|
1713 |
* On IPv6 system get the index of the interface and use the |
|
1714 |
* IPV6_MULTICAST_IF socket option |
|
1715 |
* On IPv4 system extract addr[0] and use the IP_MULTICAST_IF |
|
1716 |
* option. For IPv6 both must be done. |
|
1717 |
*/ |
|
1718 |
if (ipv6_supported) { |
|
1719 |
static jfieldID ni_indexID; |
|
1720 |
struct in_addr in; |
|
1721 |
int index; |
|
1722 |
||
1723 |
if (ni_indexID == NULL) { |
|
1724 |
jclass c = (*env)->FindClass(env, "java/net/NetworkInterface"); |
|
1725 |
CHECK_NULL(c); |
|
1726 |
ni_indexID = (*env)->GetFieldID(env, c, "index", "I"); |
|
1727 |
CHECK_NULL(ni_indexID); |
|
1728 |
} |
|
1729 |
index = (*env)->GetIntField(env, value, ni_indexID); |
|
1730 |
||
1731 |
if (setsockopt(fd1, IPPROTO_IPV6, IPV6_MULTICAST_IF, |
|
1732 |
(const char*)&index, sizeof(index)) < 0) { |
|
1733 |
if (errno == EINVAL && index > 0) { |
|
1734 |
JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException", |
|
1735 |
"IPV6_MULTICAST_IF failed (interface has IPv4 " |
|
1736 |
"address only?)"); |
|
1737 |
} else { |
|
1738 |
NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "SocketException", |
|
1739 |
"Error setting socket option"); |
|
1740 |
} |
|
1741 |
return; |
|
1742 |
} |
|
1743 |
||
1744 |
/* If there are any IPv4 addresses on this interface then |
|
1745 |
* repeat the operation on the IPv4 fd */ |
|
1746 |
||
1747 |
if (getInet4AddrFromIf (env, value, &in) < 0) { |
|
1748 |
return; |
|
1749 |
} |
|
1750 |
if (setsockopt(fd, IPPROTO_IP, IP_MULTICAST_IF, |
|
1751 |
(const char*)&in, sizeof(in)) < 0) { |
|
1752 |
NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "SocketException", |
|
1753 |
"Error setting socket option"); |
|
1754 |
} |
|
1755 |
return; |
|
1756 |
} else { |
|
1757 |
struct in_addr in; |
|
1758 |
||
1759 |
if (getInet4AddrFromIf (env, value, &in) < 0) { |
|
1760 |
if ((*env)->ExceptionOccurred(env)) { |
|
1761 |
return; |
|
1762 |
} |
|
1763 |
JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException", |
|
1764 |
"no InetAddress instances of requested type"); |
|
1765 |
return; |
|
1766 |
} |
|
1767 |
||
1768 |
if (setsockopt(fd, IPPROTO_IP, IP_MULTICAST_IF, |
|
1769 |
(const char*)&in, sizeof(in)) < 0) { |
|
1770 |
NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "SocketException", |
|
1771 |
"Error setting socket option"); |
|
1772 |
} |
|
1773 |
return; |
|
1774 |
} |
|
1775 |
} |
|
1776 |
} |
|
1777 |
||
1778 |
/* |
|
1779 |
* Class: java_net_TwoStacksPlainDatagramSocketImpl |
|
18192 | 1780 |
* Method: socketNativeSetOption |
2 | 1781 |
* Signature: (ILjava/lang/Object;)V |
1782 |
*/ |
|
1783 |
JNIEXPORT void JNICALL |
|
18192 | 1784 |
Java_java_net_TwoStacksPlainDatagramSocketImpl_socketNativeSetOption(JNIEnv *env,jobject this, |
2 | 1785 |
jint opt,jobject value) { |
1786 |
||
1787 |
int fd=-1, fd1=-1; |
|
1788 |
int levelv4, levelv6, optnamev4, optnamev6, optlen; |
|
1789 |
union { |
|
1790 |
int i; |
|
1791 |
char c; |
|
1792 |
} optval; |
|
1793 |
int ipv6_supported = ipv6_available(); |
|
1794 |
||
1795 |
fd = getFD(env, this); |
|
1796 |
||
1797 |
if (ipv6_supported) { |
|
1798 |
fd1 = getFD1(env, this); |
|
1799 |
} |
|
1800 |
if (fd < 0 && fd1 < 0) { |
|
1801 |
JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException", "socket closed"); |
|
1802 |
return; |
|
1803 |
} |
|
1804 |
||
1805 |
if ((opt == java_net_SocketOptions_IP_MULTICAST_IF) || |
|
1806 |
(opt == java_net_SocketOptions_IP_MULTICAST_IF2)) { |
|
1807 |
||
1808 |
setMulticastInterface(env, this, fd, fd1, opt, value); |
|
1809 |
return; |
|
1810 |
} |
|
1811 |
||
1812 |
/* |
|
1813 |
* Map the Java level socket option to the platform specific |
|
1814 |
* level(s) and option name(s). |
|
1815 |
*/ |
|
1816 |
if (fd1 != -1) { |
|
1817 |
if (NET_MapSocketOptionV6(opt, &levelv6, &optnamev6)) { |
|
1818 |
JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException", "Invalid option"); |
|
1819 |
return; |
|
1820 |
} |
|
1821 |
} |
|
1822 |
if (fd != -1) { |
|
1823 |
if (NET_MapSocketOption(opt, &levelv4, &optnamev4)) { |
|
1824 |
JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException", "Invalid option"); |
|
1825 |
return; |
|
1826 |
} |
|
1827 |
} |
|
1828 |
||
1829 |
switch (opt) { |
|
1830 |
case java_net_SocketOptions_SO_SNDBUF : |
|
1831 |
case java_net_SocketOptions_SO_RCVBUF : |
|
1832 |
case java_net_SocketOptions_IP_TOS : |
|
1833 |
{ |
|
1834 |
jclass cls; |
|
1835 |
jfieldID fid; |
|
1836 |
||
1837 |
cls = (*env)->FindClass(env, "java/lang/Integer"); |
|
1838 |
CHECK_NULL(cls); |
|
1839 |
fid = (*env)->GetFieldID(env, cls, "value", "I"); |
|
1840 |
CHECK_NULL(fid); |
|
1841 |
||
1842 |
optval.i = (*env)->GetIntField(env, value, fid); |
|
1843 |
optlen = sizeof(optval.i); |
|
1844 |
} |
|
1845 |
break; |
|
1846 |
||
1847 |
case java_net_SocketOptions_SO_REUSEADDR: |
|
1848 |
case java_net_SocketOptions_SO_BROADCAST: |
|
1849 |
case java_net_SocketOptions_IP_MULTICAST_LOOP: |
|
1850 |
{ |
|
1851 |
jclass cls; |
|
1852 |
jfieldID fid; |
|
1853 |
jboolean on; |
|
1854 |
||
1855 |
cls = (*env)->FindClass(env, "java/lang/Boolean"); |
|
1856 |
CHECK_NULL(cls); |
|
1857 |
fid = (*env)->GetFieldID(env, cls, "value", "Z"); |
|
1858 |
CHECK_NULL(fid); |
|
1859 |
||
1860 |
on = (*env)->GetBooleanField(env, value, fid); |
|
1861 |
optval.i = (on ? 1 : 0); |
|
1862 |
/* |
|
1863 |
* setLoopbackMode (true) disables IP_MULTICAST_LOOP rather |
|
1864 |
* than enabling it. |
|
1865 |
*/ |
|
1866 |
if (opt == java_net_SocketOptions_IP_MULTICAST_LOOP) { |
|
1867 |
optval.i = !optval.i; |
|
1868 |
} |
|
1869 |
optlen = sizeof(optval.i); |
|
1870 |
} |
|
1871 |
break; |
|
1872 |
||
1873 |
default : |
|
1874 |
JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException", |
|
1875 |
"Socket option not supported by PlainDatagramSocketImp"); |
|
1876 |
break; |
|
1877 |
||
1878 |
} |
|
1879 |
||
1880 |
if (fd1 != -1) { |
|
1881 |
if (NET_SetSockOpt(fd1, levelv6, optnamev6, (void *)&optval, optlen) < 0) { |
|
1882 |
NET_ThrowCurrent(env, "setsockopt IPv6"); |
|
1883 |
return; |
|
1884 |
} |
|
1885 |
} |
|
1886 |
if (fd != -1) { |
|
1887 |
if (NET_SetSockOpt(fd, levelv4, optnamev4, (void *)&optval, optlen) < 0) { |
|
1888 |
NET_ThrowCurrent(env, "setsockopt"); |
|
1889 |
return; |
|
1890 |
} |
|
1891 |
} |
|
1892 |
} |
|
1893 |
||
1894 |
/* |
|
1895 |
* Return the multicast interface: |
|
1896 |
* |
|
1897 |
* SocketOptions.IP_MULTICAST_IF |
|
1898 |
* IPv4: Query IPPROTO_IP/IP_MULTICAST_IF |
|
1899 |
* Create InetAddress |
|
1900 |
* IP_MULTICAST_IF returns struct ip_mreqn on 2.2 |
|
1901 |
* kernel but struct in_addr on 2.4 kernel |
|
1902 |
* IPv6: Query IPPROTO_IPV6 / IPV6_MULTICAST_IF or |
|
1903 |
* obtain from impl is Linux 2.2 kernel |
|
1904 |
* If index == 0 return InetAddress representing |
|
1905 |
* anyLocalAddress. |
|
1906 |
* If index > 0 query NetworkInterface by index |
|
1907 |
* and returns addrs[0] |
|
1908 |
* |
|
1909 |
* SocketOptions.IP_MULTICAST_IF2 |
|
1910 |
* IPv4: Query IPPROTO_IP/IP_MULTICAST_IF |
|
1911 |
* Query NetworkInterface by IP address and |
|
1912 |
* return the NetworkInterface that the address |
|
1913 |
* is bound too. |
|
1914 |
* IPv6: Query IPPROTO_IPV6 / IPV6_MULTICAST_IF |
|
1915 |
* (except Linux .2 kernel) |
|
1916 |
* Query NetworkInterface by index and |
|
1917 |
* return NetworkInterface. |
|
1918 |
*/ |
|
1919 |
jobject getMulticastInterface(JNIEnv *env, jobject this, int fd, int fd1, jint opt) { |
|
1920 |
jboolean isIPV4 = !ipv6_available() || fd1 == -1; |
|
1921 |
||
1922 |
/* |
|
1923 |
* IPv4 implementation |
|
1924 |
*/ |
|
1925 |
if (isIPV4) { |
|
1926 |
static jclass inet4_class; |
|
1927 |
static jmethodID inet4_ctrID; |
|
1928 |
||
1929 |
static jclass ni_class; |
|
1930 |
static jmethodID ni_ctrID; |
|
1931 |
static jfieldID ni_indexID; |
|
1932 |
static jfieldID ni_addrsID; |
|
1933 |
||
1934 |
jobjectArray addrArray; |
|
1935 |
jobject addr; |
|
1936 |
jobject ni; |
|
1937 |
||
1938 |
struct in_addr in; |
|
1939 |
struct in_addr *inP = ∈ |
|
1940 |
int len = sizeof(struct in_addr); |
|
1941 |
||
1942 |
if (getsockopt(fd, IPPROTO_IP, IP_MULTICAST_IF, |
|
1943 |
(char *)inP, &len) < 0) { |
|
1944 |
NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "SocketException", |
|
1945 |
"Error getting socket option"); |
|
1946 |
return NULL; |
|
1947 |
} |
|
1948 |
||
1949 |
/* |
|
1950 |
* Construct and populate an Inet4Address |
|
1951 |
*/ |
|
1952 |
if (inet4_class == NULL) { |
|
1953 |
jclass c = (*env)->FindClass(env, "java/net/Inet4Address"); |
|
1954 |
CHECK_NULL_RETURN(c, NULL); |
|
1955 |
inet4_ctrID = (*env)->GetMethodID(env, c, "<init>", "()V"); |
|
1956 |
CHECK_NULL_RETURN(inet4_ctrID, NULL); |
|
1957 |
inet4_class = (*env)->NewGlobalRef(env, c); |
|
1958 |
CHECK_NULL_RETURN(inet4_class, NULL); |
|
1959 |
} |
|
1960 |
addr = (*env)->NewObject(env, inet4_class, inet4_ctrID, 0); |
|
1961 |
CHECK_NULL_RETURN(addr, NULL); |
|
1962 |
||
16870 | 1963 |
setInetAddress_addr(env, addr, ntohl(in.s_addr)); |
2 | 1964 |
|
1965 |
/* |
|
1966 |
* For IP_MULTICAST_IF return InetAddress |
|
1967 |
*/ |
|
1968 |
if (opt == java_net_SocketOptions_IP_MULTICAST_IF) { |
|
1969 |
return addr; |
|
1970 |
} |
|
1971 |
||
1972 |
/* |
|
1973 |
* For IP_MULTICAST_IF2 we get the NetworkInterface for |
|
1974 |
* this address and return it |
|
1975 |
*/ |
|
1976 |
if (ni_class == NULL) { |
|
1977 |
jclass c = (*env)->FindClass(env, "java/net/NetworkInterface"); |
|
1978 |
CHECK_NULL_RETURN(c, NULL); |
|
1979 |
ni_ctrID = (*env)->GetMethodID(env, c, "<init>", "()V"); |
|
1980 |
CHECK_NULL_RETURN(ni_ctrID, NULL); |
|
1981 |
ni_indexID = (*env)->GetFieldID(env, c, "index", "I"); |
|
1982 |
CHECK_NULL_RETURN(ni_indexID, NULL); |
|
1983 |
ni_addrsID = (*env)->GetFieldID(env, c, "addrs", |
|
1984 |
"[Ljava/net/InetAddress;"); |
|
1985 |
CHECK_NULL_RETURN(ni_addrsID, NULL); |
|
1986 |
ni_class = (*env)->NewGlobalRef(env, c); |
|
1987 |
CHECK_NULL_RETURN(ni_class, NULL); |
|
1988 |
} |
|
1989 |
ni = Java_java_net_NetworkInterface_getByInetAddress0(env, ni_class, addr); |
|
1990 |
if (ni) { |
|
1991 |
return ni; |
|
1992 |
} |
|
1993 |
||
1994 |
/* |
|
1995 |
* The address doesn't appear to be bound at any known |
|
1996 |
* NetworkInterface. Therefore we construct a NetworkInterface |
|
1997 |
* with this address. |
|
1998 |
*/ |
|
1999 |
ni = (*env)->NewObject(env, ni_class, ni_ctrID, 0); |
|
2000 |
CHECK_NULL_RETURN(ni, NULL); |
|
2001 |
||
2002 |
(*env)->SetIntField(env, ni, ni_indexID, -1); |
|
2003 |
addrArray = (*env)->NewObjectArray(env, 1, inet4_class, NULL); |
|
2004 |
CHECK_NULL_RETURN(addrArray, NULL); |
|
2005 |
(*env)->SetObjectArrayElement(env, addrArray, 0, addr); |
|
2006 |
(*env)->SetObjectField(env, ni, ni_addrsID, addrArray); |
|
2007 |
return ni; |
|
2008 |
} |
|
2009 |
||
2010 |
||
2011 |
/* |
|
2012 |
* IPv6 implementation |
|
2013 |
*/ |
|
2014 |
if ((opt == java_net_SocketOptions_IP_MULTICAST_IF) || |
|
2015 |
(opt == java_net_SocketOptions_IP_MULTICAST_IF2)) { |
|
2016 |
||
2017 |
static jclass ni_class; |
|
2018 |
static jmethodID ni_ctrID; |
|
2019 |
static jfieldID ni_indexID; |
|
2020 |
static jfieldID ni_addrsID; |
|
2021 |
static jclass ia_class; |
|
2022 |
static jmethodID ia_anyLocalAddressID; |
|
2023 |
||
2024 |
int index; |
|
2025 |
int len = sizeof(index); |
|
2026 |
||
2027 |
jobjectArray addrArray; |
|
2028 |
jobject addr; |
|
2029 |
jobject ni; |
|
2030 |
||
2031 |
{ |
|
2032 |
if (getsockopt(fd1, IPPROTO_IPV6, IPV6_MULTICAST_IF, |
|
2033 |
(char*)&index, &len) < 0) { |
|
2034 |
NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "SocketException", |
|
2035 |
"Error getting socket option"); |
|
2036 |
return NULL; |
|
2037 |
} |
|
2038 |
} |
|
2039 |
||
2040 |
if (ni_class == NULL) { |
|
2041 |
jclass c = (*env)->FindClass(env, "java/net/NetworkInterface"); |
|
2042 |
CHECK_NULL_RETURN(c, NULL); |
|
2043 |
ni_ctrID = (*env)->GetMethodID(env, c, "<init>", "()V"); |
|
2044 |
CHECK_NULL_RETURN(ni_ctrID, NULL); |
|
2045 |
ni_indexID = (*env)->GetFieldID(env, c, "index", "I"); |
|
2046 |
CHECK_NULL_RETURN(ni_indexID, NULL); |
|
2047 |
ni_addrsID = (*env)->GetFieldID(env, c, "addrs", |
|
2048 |
"[Ljava/net/InetAddress;"); |
|
2049 |
CHECK_NULL_RETURN(ni_addrsID, NULL); |
|
2050 |
||
2051 |
ia_class = (*env)->FindClass(env, "java/net/InetAddress"); |
|
2052 |
CHECK_NULL_RETURN(ia_class, NULL); |
|
2053 |
ia_class = (*env)->NewGlobalRef(env, ia_class); |
|
2054 |
CHECK_NULL_RETURN(ia_class, NULL); |
|
2055 |
ia_anyLocalAddressID = (*env)->GetStaticMethodID(env, |
|
2056 |
ia_class, |
|
2057 |
"anyLocalAddress", |
|
2058 |
"()Ljava/net/InetAddress;"); |
|
2059 |
CHECK_NULL_RETURN(ia_anyLocalAddressID, NULL); |
|
2060 |
ni_class = (*env)->NewGlobalRef(env, c); |
|
2061 |
CHECK_NULL_RETURN(ni_class, NULL); |
|
2062 |
} |
|
2063 |
||
2064 |
/* |
|
2065 |
* If multicast to a specific interface then return the |
|
2066 |
* interface (for IF2) or the any address on that interface |
|
2067 |
* (for IF). |
|
2068 |
*/ |
|
2069 |
if (index > 0) { |
|
1097
af4930f761df
6717876: Make java.net.NetworkInterface.getIndex() public
jccollet
parents:
910
diff
changeset
|
2070 |
ni = Java_java_net_NetworkInterface_getByIndex0(env, ni_class, |
2 | 2071 |
index); |
2072 |
if (ni == NULL) { |
|
2073 |
char errmsg[255]; |
|
2074 |
sprintf(errmsg, |
|
2075 |
"IPV6_MULTICAST_IF returned index to unrecognized interface: %d", |
|
2076 |
index); |
|
2077 |
JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException", errmsg); |
|
2078 |
return NULL; |
|
2079 |
} |
|
2080 |
||
2081 |
/* |
|
2082 |
* For IP_MULTICAST_IF2 return the NetworkInterface |
|
2083 |
*/ |
|
2084 |
if (opt == java_net_SocketOptions_IP_MULTICAST_IF2) { |
|
2085 |
return ni; |
|
2086 |
} |
|
2087 |
||
2088 |
/* |
|
2089 |
* For IP_MULTICAST_IF return addrs[0] |
|
2090 |
*/ |
|
2091 |
addrArray = (*env)->GetObjectField(env, ni, ni_addrsID); |
|
2092 |
if ((*env)->GetArrayLength(env, addrArray) < 1) { |
|
2093 |
JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException", |
|
2094 |
"IPV6_MULTICAST_IF returned interface without IP bindings"); |
|
2095 |
return NULL; |
|
2096 |
} |
|
2097 |
||
2098 |
addr = (*env)->GetObjectArrayElement(env, addrArray, 0); |
|
2099 |
return addr; |
|
2100 |
} |
|
2101 |
||
2102 |
/* |
|
2103 |
* Multicast to any address - return anyLocalAddress |
|
2104 |
* or a NetworkInterface with addrs[0] set to anyLocalAddress |
|
2105 |
*/ |
|
2106 |
||
2107 |
addr = (*env)->CallStaticObjectMethod(env, ia_class, ia_anyLocalAddressID, |
|
2108 |
NULL); |
|
2109 |
if (opt == java_net_SocketOptions_IP_MULTICAST_IF) { |
|
2110 |
return addr; |
|
2111 |
} |
|
2112 |
||
2113 |
ni = (*env)->NewObject(env, ni_class, ni_ctrID, 0); |
|
2114 |
CHECK_NULL_RETURN(ni, NULL); |
|
2115 |
(*env)->SetIntField(env, ni, ni_indexID, -1); |
|
2116 |
addrArray = (*env)->NewObjectArray(env, 1, ia_class, NULL); |
|
2117 |
CHECK_NULL_RETURN(addrArray, NULL); |
|
2118 |
(*env)->SetObjectArrayElement(env, addrArray, 0, addr); |
|
2119 |
(*env)->SetObjectField(env, ni, ni_addrsID, addrArray); |
|
2120 |
return ni; |
|
2121 |
} |
|
2122 |
return NULL; |
|
2123 |
} |
|
2124 |
/* |
|
2125 |
* Returns relevant info as a jint. |
|
2126 |
* |
|
2127 |
* Class: java_net_TwoStacksPlainDatagramSocketImpl |
|
2128 |
* Method: socketGetOption |
|
2129 |
* Signature: (I)Ljava/lang/Object; |
|
2130 |
*/ |
|
2131 |
JNIEXPORT jobject JNICALL |
|
2132 |
Java_java_net_TwoStacksPlainDatagramSocketImpl_socketGetOption(JNIEnv *env, jobject this, |
|
2133 |
jint opt) { |
|
2134 |
||
2135 |
int fd=-1, fd1=-1; |
|
2136 |
int level, optname, optlen; |
|
2137 |
union { |
|
2138 |
int i; |
|
2139 |
} optval; |
|
2140 |
int ipv6_supported = ipv6_available(); |
|
2141 |
||
2142 |
fd = getFD(env, this); |
|
2143 |
if (ipv6_supported) { |
|
2144 |
fd1 = getFD1(env, this); |
|
2145 |
} |
|
2146 |
||
2147 |
if (fd < 0 && fd1 < 0) { |
|
2148 |
JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException", |
|
2149 |
"Socket closed"); |
|
2150 |
return NULL; |
|
2151 |
} |
|
2152 |
||
2153 |
/* |
|
2154 |
* Handle IP_MULTICAST_IF separately |
|
2155 |
*/ |
|
2156 |
if (opt == java_net_SocketOptions_IP_MULTICAST_IF || |
|
2157 |
opt == java_net_SocketOptions_IP_MULTICAST_IF2) { |
|
2158 |
return getMulticastInterface(env, this, fd, fd1, opt); |
|
2159 |
} |
|
2160 |
||
2161 |
/* |
|
2162 |
* Map the Java level socket option to the platform specific |
|
2163 |
* level and option name. |
|
2164 |
*/ |
|
2165 |
if (NET_MapSocketOption(opt, &level, &optname)) { |
|
2166 |
JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException", "Invalid option"); |
|
2167 |
return NULL; |
|
2168 |
} |
|
2169 |
||
2170 |
if (fd == -1) { |
|
2171 |
if (NET_MapSocketOptionV6(opt, &level, &optname)) { |
|
2172 |
JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException", "Invalid option"); |
|
2173 |
return NULL; |
|
2174 |
} |
|
2175 |
fd = fd1; /* must be IPv6 only */ |
|
2176 |
} |
|
2177 |
||
2178 |
optlen = sizeof(optval.i); |
|
2179 |
if (NET_GetSockOpt(fd, level, optname, (void *)&optval, &optlen) < 0) { |
|
2180 |
char errmsg[255]; |
|
2181 |
sprintf(errmsg, "error getting socket option: %s\n", strerror(errno)); |
|
2182 |
JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException", errmsg); |
|
2183 |
return NULL; |
|
2184 |
} |
|
2185 |
||
2186 |
switch (opt) { |
|
2187 |
case java_net_SocketOptions_SO_BROADCAST: |
|
2188 |
case java_net_SocketOptions_SO_REUSEADDR: |
|
2189 |
return createBoolean(env, optval.i); |
|
2190 |
||
2191 |
case java_net_SocketOptions_IP_MULTICAST_LOOP: |
|
2192 |
/* getLoopbackMode() returns true if IP_MULTICAST_LOOP is disabled */ |
|
2193 |
return createBoolean(env, !optval.i); |
|
2194 |
||
2195 |
case java_net_SocketOptions_SO_SNDBUF: |
|
2196 |
case java_net_SocketOptions_SO_RCVBUF: |
|
2197 |
case java_net_SocketOptions_IP_TOS: |
|
2198 |
return createInteger(env, optval.i); |
|
2199 |
||
2200 |
default : |
|
2201 |
JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException", |
|
2202 |
"Socket option not supported by TwoStacksPlainDatagramSocketImpl"); |
|
2203 |
return NULL; |
|
2204 |
||
2205 |
} |
|
2206 |
} |
|
2207 |
||
2208 |
/* |
|
10693
6e9ebed2e783
7084030: DatagramSocket.getLocalAddress inconsistent on XP/2003 when IPv6 enabled and socket is connected
chegar
parents:
7668
diff
changeset
|
2209 |
* Returns local address of the socket. |
6e9ebed2e783
7084030: DatagramSocket.getLocalAddress inconsistent on XP/2003 when IPv6 enabled and socket is connected
chegar
parents:
7668
diff
changeset
|
2210 |
* |
6e9ebed2e783
7084030: DatagramSocket.getLocalAddress inconsistent on XP/2003 when IPv6 enabled and socket is connected
chegar
parents:
7668
diff
changeset
|
2211 |
* Class: java_net_TwoStacksPlainDatagramSocketImpl |
6e9ebed2e783
7084030: DatagramSocket.getLocalAddress inconsistent on XP/2003 when IPv6 enabled and socket is connected
chegar
parents:
7668
diff
changeset
|
2212 |
* Method: socketLocalAddress |
6e9ebed2e783
7084030: DatagramSocket.getLocalAddress inconsistent on XP/2003 when IPv6 enabled and socket is connected
chegar
parents:
7668
diff
changeset
|
2213 |
* Signature: (I)Ljava/lang/Object; |
6e9ebed2e783
7084030: DatagramSocket.getLocalAddress inconsistent on XP/2003 when IPv6 enabled and socket is connected
chegar
parents:
7668
diff
changeset
|
2214 |
*/ |
6e9ebed2e783
7084030: DatagramSocket.getLocalAddress inconsistent on XP/2003 when IPv6 enabled and socket is connected
chegar
parents:
7668
diff
changeset
|
2215 |
JNIEXPORT jobject JNICALL |
6e9ebed2e783
7084030: DatagramSocket.getLocalAddress inconsistent on XP/2003 when IPv6 enabled and socket is connected
chegar
parents:
7668
diff
changeset
|
2216 |
Java_java_net_TwoStacksPlainDatagramSocketImpl_socketLocalAddress(JNIEnv *env, jobject this, |
6e9ebed2e783
7084030: DatagramSocket.getLocalAddress inconsistent on XP/2003 when IPv6 enabled and socket is connected
chegar
parents:
7668
diff
changeset
|
2217 |
jint family) { |
6e9ebed2e783
7084030: DatagramSocket.getLocalAddress inconsistent on XP/2003 when IPv6 enabled and socket is connected
chegar
parents:
7668
diff
changeset
|
2218 |
|
6e9ebed2e783
7084030: DatagramSocket.getLocalAddress inconsistent on XP/2003 when IPv6 enabled and socket is connected
chegar
parents:
7668
diff
changeset
|
2219 |
int fd=-1, fd1=-1; |
6e9ebed2e783
7084030: DatagramSocket.getLocalAddress inconsistent on XP/2003 when IPv6 enabled and socket is connected
chegar
parents:
7668
diff
changeset
|
2220 |
SOCKETADDRESS him; |
6e9ebed2e783
7084030: DatagramSocket.getLocalAddress inconsistent on XP/2003 when IPv6 enabled and socket is connected
chegar
parents:
7668
diff
changeset
|
2221 |
int len = 0; |
6e9ebed2e783
7084030: DatagramSocket.getLocalAddress inconsistent on XP/2003 when IPv6 enabled and socket is connected
chegar
parents:
7668
diff
changeset
|
2222 |
int port; |
6e9ebed2e783
7084030: DatagramSocket.getLocalAddress inconsistent on XP/2003 when IPv6 enabled and socket is connected
chegar
parents:
7668
diff
changeset
|
2223 |
jobject iaObj; |
6e9ebed2e783
7084030: DatagramSocket.getLocalAddress inconsistent on XP/2003 when IPv6 enabled and socket is connected
chegar
parents:
7668
diff
changeset
|
2224 |
int ipv6_supported = ipv6_available(); |
6e9ebed2e783
7084030: DatagramSocket.getLocalAddress inconsistent on XP/2003 when IPv6 enabled and socket is connected
chegar
parents:
7668
diff
changeset
|
2225 |
|
6e9ebed2e783
7084030: DatagramSocket.getLocalAddress inconsistent on XP/2003 when IPv6 enabled and socket is connected
chegar
parents:
7668
diff
changeset
|
2226 |
fd = getFD(env, this); |
6e9ebed2e783
7084030: DatagramSocket.getLocalAddress inconsistent on XP/2003 when IPv6 enabled and socket is connected
chegar
parents:
7668
diff
changeset
|
2227 |
if (ipv6_supported) { |
6e9ebed2e783
7084030: DatagramSocket.getLocalAddress inconsistent on XP/2003 when IPv6 enabled and socket is connected
chegar
parents:
7668
diff
changeset
|
2228 |
fd1 = getFD1(env, this); |
6e9ebed2e783
7084030: DatagramSocket.getLocalAddress inconsistent on XP/2003 when IPv6 enabled and socket is connected
chegar
parents:
7668
diff
changeset
|
2229 |
} |
6e9ebed2e783
7084030: DatagramSocket.getLocalAddress inconsistent on XP/2003 when IPv6 enabled and socket is connected
chegar
parents:
7668
diff
changeset
|
2230 |
|
6e9ebed2e783
7084030: DatagramSocket.getLocalAddress inconsistent on XP/2003 when IPv6 enabled and socket is connected
chegar
parents:
7668
diff
changeset
|
2231 |
if (fd < 0 && fd1 < 0) { |
6e9ebed2e783
7084030: DatagramSocket.getLocalAddress inconsistent on XP/2003 when IPv6 enabled and socket is connected
chegar
parents:
7668
diff
changeset
|
2232 |
JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException", |
6e9ebed2e783
7084030: DatagramSocket.getLocalAddress inconsistent on XP/2003 when IPv6 enabled and socket is connected
chegar
parents:
7668
diff
changeset
|
2233 |
"Socket closed"); |
6e9ebed2e783
7084030: DatagramSocket.getLocalAddress inconsistent on XP/2003 when IPv6 enabled and socket is connected
chegar
parents:
7668
diff
changeset
|
2234 |
return NULL; |
6e9ebed2e783
7084030: DatagramSocket.getLocalAddress inconsistent on XP/2003 when IPv6 enabled and socket is connected
chegar
parents:
7668
diff
changeset
|
2235 |
} |
6e9ebed2e783
7084030: DatagramSocket.getLocalAddress inconsistent on XP/2003 when IPv6 enabled and socket is connected
chegar
parents:
7668
diff
changeset
|
2236 |
|
6e9ebed2e783
7084030: DatagramSocket.getLocalAddress inconsistent on XP/2003 when IPv6 enabled and socket is connected
chegar
parents:
7668
diff
changeset
|
2237 |
/* find out local IP address */ |
6e9ebed2e783
7084030: DatagramSocket.getLocalAddress inconsistent on XP/2003 when IPv6 enabled and socket is connected
chegar
parents:
7668
diff
changeset
|
2238 |
|
6e9ebed2e783
7084030: DatagramSocket.getLocalAddress inconsistent on XP/2003 when IPv6 enabled and socket is connected
chegar
parents:
7668
diff
changeset
|
2239 |
len = sizeof (struct sockaddr_in); |
6e9ebed2e783
7084030: DatagramSocket.getLocalAddress inconsistent on XP/2003 when IPv6 enabled and socket is connected
chegar
parents:
7668
diff
changeset
|
2240 |
|
6e9ebed2e783
7084030: DatagramSocket.getLocalAddress inconsistent on XP/2003 when IPv6 enabled and socket is connected
chegar
parents:
7668
diff
changeset
|
2241 |
/* family==-1 when socket is not connected */ |
6e9ebed2e783
7084030: DatagramSocket.getLocalAddress inconsistent on XP/2003 when IPv6 enabled and socket is connected
chegar
parents:
7668
diff
changeset
|
2242 |
if ((family == IPv6) || (family == -1 && fd == -1)) { |
6e9ebed2e783
7084030: DatagramSocket.getLocalAddress inconsistent on XP/2003 when IPv6 enabled and socket is connected
chegar
parents:
7668
diff
changeset
|
2243 |
fd = fd1; /* must be IPv6 only */ |
6e9ebed2e783
7084030: DatagramSocket.getLocalAddress inconsistent on XP/2003 when IPv6 enabled and socket is connected
chegar
parents:
7668
diff
changeset
|
2244 |
len = sizeof (struct SOCKADDR_IN6); |
6e9ebed2e783
7084030: DatagramSocket.getLocalAddress inconsistent on XP/2003 when IPv6 enabled and socket is connected
chegar
parents:
7668
diff
changeset
|
2245 |
} |
6e9ebed2e783
7084030: DatagramSocket.getLocalAddress inconsistent on XP/2003 when IPv6 enabled and socket is connected
chegar
parents:
7668
diff
changeset
|
2246 |
|
6e9ebed2e783
7084030: DatagramSocket.getLocalAddress inconsistent on XP/2003 when IPv6 enabled and socket is connected
chegar
parents:
7668
diff
changeset
|
2247 |
if (fd == -1) { |
6e9ebed2e783
7084030: DatagramSocket.getLocalAddress inconsistent on XP/2003 when IPv6 enabled and socket is connected
chegar
parents:
7668
diff
changeset
|
2248 |
JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException", |
6e9ebed2e783
7084030: DatagramSocket.getLocalAddress inconsistent on XP/2003 when IPv6 enabled and socket is connected
chegar
parents:
7668
diff
changeset
|
2249 |
"Socket closed"); |
6e9ebed2e783
7084030: DatagramSocket.getLocalAddress inconsistent on XP/2003 when IPv6 enabled and socket is connected
chegar
parents:
7668
diff
changeset
|
2250 |
return NULL; |
6e9ebed2e783
7084030: DatagramSocket.getLocalAddress inconsistent on XP/2003 when IPv6 enabled and socket is connected
chegar
parents:
7668
diff
changeset
|
2251 |
} |
6e9ebed2e783
7084030: DatagramSocket.getLocalAddress inconsistent on XP/2003 when IPv6 enabled and socket is connected
chegar
parents:
7668
diff
changeset
|
2252 |
|
6e9ebed2e783
7084030: DatagramSocket.getLocalAddress inconsistent on XP/2003 when IPv6 enabled and socket is connected
chegar
parents:
7668
diff
changeset
|
2253 |
if (getsockname(fd, (struct sockaddr *)&him, &len) == -1) { |
6e9ebed2e783
7084030: DatagramSocket.getLocalAddress inconsistent on XP/2003 when IPv6 enabled and socket is connected
chegar
parents:
7668
diff
changeset
|
2254 |
NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "SocketException", |
6e9ebed2e783
7084030: DatagramSocket.getLocalAddress inconsistent on XP/2003 when IPv6 enabled and socket is connected
chegar
parents:
7668
diff
changeset
|
2255 |
"Error getting socket name"); |
6e9ebed2e783
7084030: DatagramSocket.getLocalAddress inconsistent on XP/2003 when IPv6 enabled and socket is connected
chegar
parents:
7668
diff
changeset
|
2256 |
return NULL; |
6e9ebed2e783
7084030: DatagramSocket.getLocalAddress inconsistent on XP/2003 when IPv6 enabled and socket is connected
chegar
parents:
7668
diff
changeset
|
2257 |
} |
6e9ebed2e783
7084030: DatagramSocket.getLocalAddress inconsistent on XP/2003 when IPv6 enabled and socket is connected
chegar
parents:
7668
diff
changeset
|
2258 |
iaObj = NET_SockaddrToInetAddress(env, (struct sockaddr *)&him, &port); |
6e9ebed2e783
7084030: DatagramSocket.getLocalAddress inconsistent on XP/2003 when IPv6 enabled and socket is connected
chegar
parents:
7668
diff
changeset
|
2259 |
|
6e9ebed2e783
7084030: DatagramSocket.getLocalAddress inconsistent on XP/2003 when IPv6 enabled and socket is connected
chegar
parents:
7668
diff
changeset
|
2260 |
return iaObj; |
6e9ebed2e783
7084030: DatagramSocket.getLocalAddress inconsistent on XP/2003 when IPv6 enabled and socket is connected
chegar
parents:
7668
diff
changeset
|
2261 |
} |
6e9ebed2e783
7084030: DatagramSocket.getLocalAddress inconsistent on XP/2003 when IPv6 enabled and socket is connected
chegar
parents:
7668
diff
changeset
|
2262 |
|
6e9ebed2e783
7084030: DatagramSocket.getLocalAddress inconsistent on XP/2003 when IPv6 enabled and socket is connected
chegar
parents:
7668
diff
changeset
|
2263 |
/* |
2 | 2264 |
* Class: java_net_TwoStacksPlainDatagramSocketImpl |
2265 |
* Method: setTimeToLive |
|
2266 |
* Signature: (I)V |
|
2267 |
*/ |
|
2268 |
JNIEXPORT void JNICALL |
|
2269 |
Java_java_net_TwoStacksPlainDatagramSocketImpl_setTimeToLive(JNIEnv *env, jobject this, |
|
2270 |
jint ttl) { |
|
2271 |
||
2272 |
jobject fdObj = (*env)->GetObjectField(env, this, pdsi_fdID); |
|
2273 |
jobject fd1Obj = (*env)->GetObjectField(env, this, pdsi_fd1ID); |
|
2274 |
int fd = -1, fd1 = -1; |
|
2275 |
int ittl = (int)ttl; |
|
2276 |
||
2277 |
if (IS_NULL(fdObj) && IS_NULL(fd1Obj)) { |
|
2278 |
JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException", |
|
2279 |
"Socket closed"); |
|
2280 |
return; |
|
2281 |
} else { |
|
2282 |
if (!IS_NULL(fdObj)) { |
|
2283 |
fd = (*env)->GetIntField(env, fdObj, IO_fd_fdID); |
|
2284 |
} |
|
2285 |
if (!IS_NULL(fd1Obj)) { |
|
2286 |
fd1 = (*env)->GetIntField(env, fd1Obj, IO_fd_fdID); |
|
2287 |
} |
|
2288 |
} |
|
2289 |
||
2290 |
/* setsockopt to be correct ttl */ |
|
2291 |
if (fd >= 0) { |
|
2292 |
if (NET_SetSockOpt(fd, IPPROTO_IP, IP_MULTICAST_TTL, (char*)&ittl, |
|
2293 |
sizeof (ittl)) < 0) { |
|
2294 |
NET_ThrowCurrent(env, "set IP_MULTICAST_TTL failed"); |
|
2295 |
} |
|
2296 |
} |
|
2297 |
||
2298 |
if (fd1 >= 0) { |
|
2299 |
if (NET_SetSockOpt(fd1, IPPROTO_IPV6, IPV6_MULTICAST_HOPS, (char *)&ittl, |
|
2300 |
sizeof(ittl)) <0) { |
|
2301 |
NET_ThrowCurrent(env, "set IPV6_MULTICAST_HOPS failed"); |
|
2302 |
} |
|
2303 |
} |
|
2304 |
} |
|
2305 |
||
2306 |
/* |
|
2307 |
* Class: java_net_TwoStacksPlainDatagramSocketImpl |
|
2308 |
* Method: setTTL |
|
2309 |
* Signature: (B)V |
|
2310 |
*/ |
|
2311 |
JNIEXPORT void JNICALL |
|
2312 |
Java_java_net_TwoStacksPlainDatagramSocketImpl_setTTL(JNIEnv *env, jobject this, |
|
2313 |
jbyte ttl) { |
|
2314 |
Java_java_net_TwoStacksPlainDatagramSocketImpl_setTimeToLive(env, this, |
|
2315 |
(jint)ttl & 0xFF); |
|
2316 |
} |
|
2317 |
||
2318 |
/* |
|
2319 |
* Class: java_net_TwoStacksPlainDatagramSocketImpl |
|
2320 |
* Method: getTimeToLive |
|
2321 |
* Signature: ()I |
|
2322 |
*/ |
|
2323 |
JNIEXPORT jint JNICALL |
|
2324 |
Java_java_net_TwoStacksPlainDatagramSocketImpl_getTimeToLive(JNIEnv *env, jobject this) { |
|
2325 |
jobject fdObj = (*env)->GetObjectField(env, this, pdsi_fdID); |
|
2326 |
jobject fd1Obj = (*env)->GetObjectField(env, this, pdsi_fd1ID); |
|
2327 |
int fd = -1, fd1 = -1; |
|
2328 |
int ttl = 0; |
|
2329 |
int len = sizeof(ttl); |
|
2330 |
||
2331 |
if (IS_NULL(fdObj) && IS_NULL(fd1Obj)) { |
|
2332 |
JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException", |
|
2333 |
"Socket closed"); |
|
2334 |
return -1; |
|
2335 |
} else { |
|
2336 |
if (!IS_NULL(fdObj)) { |
|
2337 |
fd = (*env)->GetIntField(env, fdObj, IO_fd_fdID); |
|
2338 |
} |
|
2339 |
if (!IS_NULL(fd1Obj)) { |
|
2340 |
fd1 = (*env)->GetIntField(env, fd1Obj, IO_fd_fdID); |
|
2341 |
} |
|
2342 |
} |
|
2343 |
||
2344 |
/* getsockopt of ttl */ |
|
2345 |
if (fd >= 0) { |
|
2346 |
if (NET_GetSockOpt(fd, IPPROTO_IP, IP_MULTICAST_TTL, (char*)&ttl, &len) < 0) { |
|
2347 |
NET_ThrowCurrent(env, "get IP_MULTICAST_TTL failed"); |
|
2348 |
return -1; |
|
2349 |
} |
|
2350 |
return (jint)ttl; |
|
2351 |
} |
|
2352 |
if (fd1 >= 0) { |
|
2353 |
if (NET_GetSockOpt(fd1, IPPROTO_IPV6, IPV6_MULTICAST_HOPS, (char*)&ttl, &len) < 0) { |
|
2354 |
NET_ThrowCurrent(env, "get IP_MULTICAST_TTL failed"); |
|
2355 |
return -1; |
|
2356 |
} |
|
2357 |
return (jint)ttl; |
|
2358 |
} |
|
2359 |
return -1; |
|
2360 |
} |
|
2361 |
||
2362 |
/* |
|
2363 |
* Class: java_net_TwoStacksPlainDatagramSocketImpl |
|
2364 |
* Method: getTTL |
|
2365 |
* Signature: ()B |
|
2366 |
*/ |
|
2367 |
JNIEXPORT jbyte JNICALL |
|
2368 |
Java_java_net_TwoStacksPlainDatagramSocketImpl_getTTL(JNIEnv *env, jobject this) { |
|
2369 |
int result = Java_java_net_TwoStacksPlainDatagramSocketImpl_getTimeToLive(env, this); |
|
2370 |
||
2371 |
return (jbyte)result; |
|
2372 |
} |
|
2373 |
||
2374 |
/* join/leave the named group on the named interface, or if no interface specified |
|
2375 |
* then the interface set with setInterfac(), or the default interface otherwise */ |
|
2376 |
||
2377 |
static void mcast_join_leave(JNIEnv *env, jobject this, |
|
2378 |
jobject iaObj, jobject niObj, |
|
2379 |
jboolean join) |
|
2380 |
{ |
|
2381 |
jobject fdObj = (*env)->GetObjectField(env, this, pdsi_fdID); |
|
2382 |
jobject fd1Obj = (*env)->GetObjectField(env, this, pdsi_fd1ID); |
|
2383 |
jint fd = -1, fd1 = -1; |
|
2384 |
||
2385 |
SOCKETADDRESS name; |
|
2386 |
struct ip_mreq mname; |
|
2387 |
struct ipv6_mreq mname6; |
|
2388 |
||
2389 |
struct in_addr in; |
|
2390 |
DWORD ifindex; |
|
2391 |
||
2392 |
int len, family; |
|
2393 |
int ipv6_supported = ipv6_available(); |
|
2394 |
int cmd ; |
|
2395 |
||
2396 |
if (IS_NULL(fdObj) && IS_NULL(fd1Obj)) { |
|
2397 |
JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException", |
|
2398 |
"Socket closed"); |
|
2399 |
return; |
|
2400 |
} |
|
2401 |
if (!IS_NULL(fdObj)) { |
|
2402 |
fd = (*env)->GetIntField(env, fdObj, IO_fd_fdID); |
|
2403 |
} |
|
2404 |
if (ipv6_supported && !IS_NULL(fd1Obj)) { |
|
2405 |
fd1 = (*env)->GetIntField(env, fd1Obj, IO_fd_fdID); |
|
2406 |
} |
|
2407 |
||
2408 |
if (IS_NULL(iaObj)) { |
|
2409 |
JNU_ThrowNullPointerException(env, "address"); |
|
2410 |
return; |
|
2411 |
} |
|
2412 |
||
2413 |
if (NET_InetAddressToSockaddr(env, iaObj, 0, (struct sockaddr *)&name, &len, JNI_FALSE) != 0) { |
|
2414 |
return; |
|
2415 |
} |
|
2416 |
||
2417 |
/* Set the multicast group address in the ip_mreq field |
|
2418 |
* eventually this check should be done by the security manager |
|
2419 |
*/ |
|
2420 |
family = name.him.sa_family; |
|
2421 |
||
2422 |
if (family == AF_INET) { |
|
2423 |
int address = name.him4.sin_addr.s_addr; |
|
2424 |
if (!IN_MULTICAST(ntohl(address))) { |
|
2425 |
JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException", "not in multicast"); |
|
2426 |
return; |
|
2427 |
} |
|
2428 |
mname.imr_multiaddr.s_addr = address; |
|
2429 |
if (fd < 0) { |
|
2430 |
JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException", "Can't join an IPv4 group on an IPv6 only socket"); |
|
2431 |
return; |
|
2432 |
} |
|
2433 |
if (IS_NULL(niObj)) { |
|
2434 |
len = sizeof (in); |
|
2435 |
if (NET_GetSockOpt(fd, IPPROTO_IP, IP_MULTICAST_IF, |
|
2436 |
(char *)&in, &len) < 0) { |
|
2437 |
NET_ThrowCurrent(env, "get IP_MULTICAST_IF failed"); |
|
2438 |
return; |
|
2439 |
} |
|
2440 |
mname.imr_interface.s_addr = in.s_addr; |
|
2441 |
} else { |
|
2442 |
if (getInet4AddrFromIf (env, niObj, &mname.imr_interface) != 0) { |
|
2443 |
NET_ThrowCurrent(env, "no Inet4Address associated with interface"); |
|
2444 |
return; |
|
2445 |
} |
|
2446 |
} |
|
2447 |
||
2448 |
cmd = join ? IP_ADD_MEMBERSHIP: IP_DROP_MEMBERSHIP; |
|
2449 |
||
2450 |
/* Join the multicast group */ |
|
2451 |
if (NET_SetSockOpt(fd, IPPROTO_IP, cmd, (char *) &mname, sizeof (mname)) < 0) { |
|
2452 |
if (WSAGetLastError() == WSAENOBUFS) { |
|
2453 |
JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException", |
|
2454 |
"IP_ADD_MEMBERSHIP failed (out of hardware filters?)"); |
|
2455 |
} else { |
|
2456 |
JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException","error setting options"); |
|
2457 |
} |
|
2458 |
} |
|
2459 |
} else /* AF_INET6 */ { |
|
2460 |
if (ipv6_supported) { |
|
2461 |
struct in6_addr *address; |
|
2462 |
address = &name.him6.sin6_addr; |
|
2463 |
if (!IN6_IS_ADDR_MULTICAST(address)) { |
|
2464 |
JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException", "not in6 multicast"); |
|
2465 |
return; |
|
2466 |
} |
|
2467 |
mname6.ipv6mr_multiaddr = *address; |
|
2468 |
} else { |
|
2469 |
JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException", "IPv6 not supported"); |
|
2470 |
return; |
|
2471 |
} |
|
2472 |
if (fd1 < 0) { |
|
2473 |
JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException", "Can't join an IPv6 group on a IPv4 socket"); |
|
2474 |
return; |
|
2475 |
} |
|
2476 |
if (IS_NULL(niObj)) { |
|
2477 |
len = sizeof (ifindex); |
|
2478 |
if (NET_GetSockOpt(fd1, IPPROTO_IPV6, IPV6_MULTICAST_IF, &ifindex, &len) < 0) { |
|
2479 |
NET_ThrowCurrent(env, "get IPV6_MULTICAST_IF failed"); |
|
2480 |
return; |
|
2481 |
} |
|
2482 |
} else { |
|
2483 |
ifindex = getIndexFromIf (env, niObj); |
|
2484 |
if (ifindex == -1) { |
|
2485 |
NET_ThrowCurrent(env, "get ifindex failed"); |
|
2486 |
return; |
|
2487 |
} |
|
2488 |
} |
|
2489 |
mname6.ipv6mr_interface = ifindex; |
|
2490 |
cmd = join ? IPV6_ADD_MEMBERSHIP: IPV6_DROP_MEMBERSHIP; |
|
2491 |
||
2492 |
/* Join the multicast group */ |
|
2493 |
if (NET_SetSockOpt(fd1, IPPROTO_IPV6, cmd, (char *) &mname6, sizeof (mname6)) < 0) { |
|
2494 |
if (WSAGetLastError() == WSAENOBUFS) { |
|
2495 |
JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException", |
|
2496 |
"IP_ADD_MEMBERSHIP failed (out of hardware filters?)"); |
|
2497 |
} else { |
|
2498 |
JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException","error setting options"); |
|
2499 |
} |
|
2500 |
} |
|
2501 |
} |
|
2502 |
||
2503 |
return; |
|
2504 |
} |
|
2505 |
||
2506 |
/* |
|
2507 |
* Class: java_net_TwoStacksPlainDatagramSocketImpl |
|
2508 |
* Method: join |
|
2509 |
* Signature: (Ljava/net/InetAddress;)V |
|
2510 |
*/ |
|
2511 |
JNIEXPORT void JNICALL |
|
2512 |
Java_java_net_TwoStacksPlainDatagramSocketImpl_join(JNIEnv *env, jobject this, |
|
2513 |
jobject iaObj, jobject niObj) |
|
2514 |
{ |
|
2515 |
mcast_join_leave (env, this, iaObj, niObj, JNI_TRUE); |
|
2516 |
} |
|
2517 |
||
2518 |
/* |
|
2519 |
* Class: java_net_TwoStacksPlainDatagramSocketImpl |
|
2520 |
* Method: leave |
|
2521 |
* Signature: (Ljava/net/InetAddress;)V |
|
2522 |
*/ |
|
2523 |
JNIEXPORT void JNICALL |
|
2524 |
Java_java_net_TwoStacksPlainDatagramSocketImpl_leave(JNIEnv *env, jobject this, |
|
2525 |
jobject iaObj, jobject niObj) |
|
2526 |
{ |
|
2527 |
mcast_join_leave (env, this, iaObj, niObj, JNI_FALSE); |
|
2528 |
} |