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