author | robm |
Tue, 27 Sep 2016 12:07:33 +0100 | |
changeset 41137 | cbc639238d91 |
parent 41126 | 7a2fc45a887d |
child 41771 | 18c9669e76ca |
permissions | -rw-r--r-- |
2 | 1 |
/* |
23010
6dadb192ad81
8029235: Update copyright year to match last edit in jdk8 jdk repository for 2013
lana
parents:
22646
diff
changeset
|
2 |
* Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved. |
2 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
5506 | 7 |
* published by the Free Software Foundation. Oracle designates this |
2 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
2 | 10 |
* |
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
5506 | 21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
22 |
* or visit www.oracle.com if you need additional information or have any |
|
23 |
* questions. |
|
2 | 24 |
*/ |
25 |
||
26 |
#include <windows.h> |
|
27 |
#include <winsock2.h> |
|
28 |
#include <ctype.h> |
|
29 |
#include <stdio.h> |
|
30 |
#include <stdlib.h> |
|
31 |
#include <malloc.h> |
|
32 |
#include <sys/types.h> |
|
910 | 33 |
#include <process.h> |
32266
e0a235a11254
8133015: InetAddress.isReachable(tmout) returning wrong value on Windows for IPv6
robm
parents:
25859
diff
changeset
|
34 |
#include <iphlpapi.h> |
e0a235a11254
8133015: InetAddress.isReachable(tmout) returning wrong value on Windows for IPv6
robm
parents:
25859
diff
changeset
|
35 |
#include <icmpapi.h> |
41126
7a2fc45a887d
8159410: InetAddress.isReachable returns true for non existing IP adresses
robm
parents:
37362
diff
changeset
|
36 |
#include <WinError.h> |
2 | 37 |
|
38 |
#include "java_net_InetAddress.h" |
|
39 |
#include "java_net_Inet4AddressImpl.h" |
|
40 |
#include "net_util.h" |
|
41 |
#include "icmp.h" |
|
42 |
||
43 |
||
44 |
/* |
|
45 |
* Returns true if hostname is in dotted IP address format. Note that this |
|
46 |
* function performs a syntax check only. For each octet it just checks that |
|
47 |
* the octet is at most 3 digits. |
|
48 |
*/ |
|
49 |
jboolean isDottedIPAddress(const char *hostname, unsigned int *addrp) { |
|
50 |
char *c = (char *)hostname; |
|
51 |
int octets = 0; |
|
52 |
unsigned int cur = 0; |
|
53 |
int digit_cnt = 0; |
|
54 |
||
55 |
while (*c) { |
|
56 |
if (*c == '.') { |
|
57 |
if (digit_cnt == 0) { |
|
58 |
return JNI_FALSE; |
|
59 |
} else { |
|
60 |
if (octets < 4) { |
|
61 |
addrp[octets++] = cur; |
|
62 |
cur = 0; |
|
63 |
digit_cnt = 0; |
|
64 |
} else { |
|
65 |
return JNI_FALSE; |
|
66 |
} |
|
67 |
} |
|
68 |
c++; |
|
69 |
continue; |
|
70 |
} |
|
71 |
||
72 |
if ((*c < '0') || (*c > '9')) { |
|
73 |
return JNI_FALSE; |
|
74 |
} |
|
75 |
||
76 |
digit_cnt++; |
|
77 |
if (digit_cnt > 3) { |
|
78 |
return JNI_FALSE; |
|
79 |
} |
|
80 |
||
81 |
/* don't check if current octet > 255 */ |
|
82 |
cur = cur*10 + (*c - '0'); |
|
83 |
||
84 |
/* Move onto next character and check for EOF */ |
|
85 |
c++; |
|
86 |
if (*c == '\0') { |
|
87 |
if (octets < 4) { |
|
88 |
addrp[octets++] = cur; |
|
89 |
} else { |
|
90 |
return JNI_FALSE; |
|
91 |
} |
|
92 |
} |
|
93 |
} |
|
94 |
||
95 |
return (jboolean)(octets == 4); |
|
96 |
} |
|
97 |
||
98 |
/* |
|
99 |
* Inet4AddressImpl |
|
100 |
*/ |
|
101 |
||
102 |
/* |
|
103 |
* Class: java_net_Inet4AddressImpl |
|
104 |
* Method: getLocalHostName |
|
105 |
* Signature: ()Ljava/lang/String; |
|
106 |
*/ |
|
107 |
JNIEXPORT jstring JNICALL |
|
108 |
Java_java_net_Inet4AddressImpl_getLocalHostName (JNIEnv *env, jobject this) { |
|
109 |
char hostname[256]; |
|
110 |
||
111 |
if (gethostname(hostname, sizeof hostname) == -1) { |
|
112 |
strcpy(hostname, "localhost"); |
|
113 |
} |
|
114 |
return JNU_NewStringPlatform(env, hostname); |
|
115 |
} |
|
116 |
||
117 |
/* |
|
118 |
* Find an internet address for a given hostname. Not this this |
|
119 |
* code only works for addresses of type INET. The translation |
|
120 |
* of %d.%d.%d.%d to an address (int) occurs in java now, so the |
|
121 |
* String "host" shouldn't be a %d.%d.%d.%d string. The only |
|
122 |
* exception should be when any of the %d are out of range and |
|
123 |
* we fallback to a lookup. |
|
124 |
* |
|
125 |
* Class: java_net_Inet4AddressImpl |
|
126 |
* Method: lookupAllHostAddr |
|
127 |
* Signature: (Ljava/lang/String;)[[B |
|
128 |
* |
|
129 |
* This is almost shared code |
|
130 |
*/ |
|
131 |
||
132 |
JNIEXPORT jobjectArray JNICALL |
|
133 |
Java_java_net_Inet4AddressImpl_lookupAllHostAddr(JNIEnv *env, jobject this, |
|
134 |
jstring host) { |
|
135 |
const char *hostname; |
|
136 |
struct hostent *hp; |
|
137 |
unsigned int addr[4]; |
|
138 |
||
139 |
jobjectArray ret = NULL; |
|
140 |
||
22646
5fa3669fd35d
8025306: Inet[4|6]Address class and fieldID initialization in networking native code
chegar
parents:
21620
diff
changeset
|
141 |
initInetAddressIDs(env); |
5fa3669fd35d
8025306: Inet[4|6]Address class and fieldID initialization in networking native code
chegar
parents:
21620
diff
changeset
|
142 |
JNU_CHECK_EXCEPTION_RETURN(env, NULL); |
2 | 143 |
|
144 |
if (IS_NULL(host)) { |
|
145 |
JNU_ThrowNullPointerException(env, "host argument"); |
|
146 |
return NULL; |
|
147 |
} |
|
148 |
hostname = JNU_GetStringPlatformChars(env, host, JNI_FALSE); |
|
149 |
CHECK_NULL_RETURN(hostname, NULL); |
|
150 |
||
151 |
/* |
|
152 |
* The NT/2000 resolver tolerates a space in front of localhost. This |
|
153 |
* is not consistent with other implementations of gethostbyname. |
|
154 |
* In addition we must do a white space check on Solaris to avoid a |
|
155 |
* bug whereby 0.0.0.0 is returned if any host name has a white space. |
|
156 |
*/ |
|
157 |
if (isspace(hostname[0])) { |
|
158 |
JNU_ThrowByName(env, JNU_JAVANETPKG "UnknownHostException", hostname); |
|
159 |
goto cleanupAndReturn; |
|
160 |
} |
|
161 |
||
162 |
/* |
|
163 |
* If the format is x.x.x.x then don't use gethostbyname as Windows |
|
164 |
* is unable to handle octets which are out of range. |
|
165 |
*/ |
|
166 |
if (isDottedIPAddress(hostname, &addr[0])) { |
|
167 |
unsigned int address; |
|
168 |
jobject iaObj; |
|
169 |
||
170 |
/* |
|
171 |
* Are any of the octets out of range? |
|
172 |
*/ |
|
173 |
if (addr[0] > 255 || addr[1] > 255 || addr[2] > 255 || addr[3] > 255) { |
|
174 |
JNU_ThrowByName(env, JNU_JAVANETPKG "UnknownHostException", hostname); |
|
175 |
goto cleanupAndReturn; |
|
176 |
} |
|
177 |
||
178 |
/* |
|
179 |
* Return an byte array with the populated address. |
|
180 |
*/ |
|
181 |
address = (addr[3]<<24) & 0xff000000; |
|
182 |
address |= (addr[2]<<16) & 0xff0000; |
|
183 |
address |= (addr[1]<<8) & 0xff00; |
|
184 |
address |= addr[0]; |
|
185 |
||
22646
5fa3669fd35d
8025306: Inet[4|6]Address class and fieldID initialization in networking native code
chegar
parents:
21620
diff
changeset
|
186 |
ret = (*env)->NewObjectArray(env, 1, ia_class, NULL); |
2 | 187 |
|
188 |
if (IS_NULL(ret)) { |
|
189 |
goto cleanupAndReturn; |
|
190 |
} |
|
191 |
||
22646
5fa3669fd35d
8025306: Inet[4|6]Address class and fieldID initialization in networking native code
chegar
parents:
21620
diff
changeset
|
192 |
iaObj = (*env)->NewObject(env, ia4_class, ia4_ctrID); |
2 | 193 |
if (IS_NULL(iaObj)) { |
194 |
ret = NULL; |
|
195 |
goto cleanupAndReturn; |
|
196 |
} |
|
16870 | 197 |
setInetAddress_addr(env, iaObj, ntohl(address)); |
2 | 198 |
(*env)->SetObjectArrayElement(env, ret, 0, iaObj); |
199 |
JNU_ReleaseStringPlatformChars(env, host, hostname); |
|
200 |
return ret; |
|
201 |
} |
|
202 |
||
203 |
/* |
|
204 |
* Perform the lookup |
|
205 |
*/ |
|
206 |
if ((hp = gethostbyname((char*)hostname)) != NULL) { |
|
207 |
struct in_addr **addrp = (struct in_addr **) hp->h_addr_list; |
|
208 |
int len = sizeof(struct in_addr); |
|
209 |
int i = 0; |
|
210 |
||
211 |
while (*addrp != (struct in_addr *) 0) { |
|
212 |
i++; |
|
213 |
addrp++; |
|
214 |
} |
|
215 |
||
22646
5fa3669fd35d
8025306: Inet[4|6]Address class and fieldID initialization in networking native code
chegar
parents:
21620
diff
changeset
|
216 |
ret = (*env)->NewObjectArray(env, i, ia_class, NULL); |
2 | 217 |
|
218 |
if (IS_NULL(ret)) { |
|
219 |
goto cleanupAndReturn; |
|
220 |
} |
|
221 |
||
222 |
addrp = (struct in_addr **) hp->h_addr_list; |
|
223 |
i = 0; |
|
224 |
while (*addrp != (struct in_addr *) 0) { |
|
22646
5fa3669fd35d
8025306: Inet[4|6]Address class and fieldID initialization in networking native code
chegar
parents:
21620
diff
changeset
|
225 |
jobject iaObj = (*env)->NewObject(env, ia4_class, ia4_ctrID); |
2 | 226 |
if (IS_NULL(iaObj)) { |
227 |
ret = NULL; |
|
228 |
goto cleanupAndReturn; |
|
229 |
} |
|
16870 | 230 |
setInetAddress_addr(env, iaObj, ntohl((*addrp)->s_addr)); |
231 |
setInetAddress_hostName(env, iaObj, host); |
|
2 | 232 |
(*env)->SetObjectArrayElement(env, ret, i, iaObj); |
233 |
addrp++; |
|
234 |
i++; |
|
235 |
} |
|
20857
734dc47ee277
8010371: getaddrinfo can fail with EAI_SYSTEM/EAGAIN, causes UnknownHostException to be thrown
bpb
parents:
16870
diff
changeset
|
236 |
} else if (WSAGetLastError() == WSATRY_AGAIN) { |
734dc47ee277
8010371: getaddrinfo can fail with EAI_SYSTEM/EAGAIN, causes UnknownHostException to be thrown
bpb
parents:
16870
diff
changeset
|
237 |
NET_ThrowByNameWithLastError(env, |
734dc47ee277
8010371: getaddrinfo can fail with EAI_SYSTEM/EAGAIN, causes UnknownHostException to be thrown
bpb
parents:
16870
diff
changeset
|
238 |
JNU_JAVANETPKG "UnknownHostException", |
734dc47ee277
8010371: getaddrinfo can fail with EAI_SYSTEM/EAGAIN, causes UnknownHostException to be thrown
bpb
parents:
16870
diff
changeset
|
239 |
hostname); |
2 | 240 |
} else { |
241 |
JNU_ThrowByName(env, JNU_JAVANETPKG "UnknownHostException", hostname); |
|
242 |
} |
|
243 |
||
244 |
cleanupAndReturn: |
|
245 |
JNU_ReleaseStringPlatformChars(env, host, hostname); |
|
246 |
return ret; |
|
247 |
} |
|
248 |
||
249 |
/* |
|
250 |
* Class: java_net_Inet4AddressImpl |
|
251 |
* Method: getHostByAddr |
|
252 |
* Signature: (I)Ljava/lang/String; |
|
253 |
*/ |
|
254 |
JNIEXPORT jstring JNICALL |
|
255 |
Java_java_net_Inet4AddressImpl_getHostByAddr(JNIEnv *env, jobject this, |
|
256 |
jbyteArray addrArray) { |
|
257 |
struct hostent *hp; |
|
258 |
jbyte caddr[4]; |
|
259 |
jint addr; |
|
260 |
(*env)->GetByteArrayRegion(env, addrArray, 0, 4, caddr); |
|
261 |
addr = ((caddr[0]<<24) & 0xff000000); |
|
262 |
addr |= ((caddr[1] <<16) & 0xff0000); |
|
263 |
addr |= ((caddr[2] <<8) & 0xff00); |
|
264 |
addr |= (caddr[3] & 0xff); |
|
265 |
addr = htonl(addr); |
|
266 |
||
267 |
hp = gethostbyaddr((char *)&addr, sizeof(addr), AF_INET); |
|
268 |
if (hp == NULL) { |
|
269 |
JNU_ThrowByName(env, JNU_JAVANETPKG "UnknownHostException", 0); |
|
270 |
return NULL; |
|
271 |
} |
|
272 |
if (hp->h_name == NULL) { /* Deal with bug in Windows XP */ |
|
273 |
JNU_ThrowByName(env, JNU_JAVANETPKG "UnknownHostException", 0); |
|
274 |
return NULL; |
|
275 |
} |
|
276 |
return JNU_NewStringPlatform(env, hp->h_name); |
|
277 |
} |
|
278 |
||
37362
9b3486880437
8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents:
34529
diff
changeset
|
279 |
static jboolean |
9b3486880437
8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents:
34529
diff
changeset
|
280 |
tcp_ping4(JNIEnv *env, |
9b3486880437
8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents:
34529
diff
changeset
|
281 |
jbyteArray addrArray, |
9b3486880437
8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents:
34529
diff
changeset
|
282 |
jint timeout, |
9b3486880437
8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents:
34529
diff
changeset
|
283 |
jbyteArray ifArray, |
9b3486880437
8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents:
34529
diff
changeset
|
284 |
jint ttl) |
9b3486880437
8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents:
34529
diff
changeset
|
285 |
{ |
9b3486880437
8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents:
34529
diff
changeset
|
286 |
jint addr; |
9b3486880437
8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents:
34529
diff
changeset
|
287 |
jbyte caddr[4]; |
9b3486880437
8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents:
34529
diff
changeset
|
288 |
jint fd; |
9b3486880437
8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents:
34529
diff
changeset
|
289 |
struct sockaddr_in him; |
9b3486880437
8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents:
34529
diff
changeset
|
290 |
struct sockaddr_in* netif = NULL; |
9b3486880437
8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents:
34529
diff
changeset
|
291 |
struct sockaddr_in inf; |
9b3486880437
8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents:
34529
diff
changeset
|
292 |
int len = 0; |
9b3486880437
8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents:
34529
diff
changeset
|
293 |
WSAEVENT hEvent; |
9b3486880437
8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents:
34529
diff
changeset
|
294 |
int connect_rv = -1; |
9b3486880437
8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents:
34529
diff
changeset
|
295 |
int sz; |
9b3486880437
8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents:
34529
diff
changeset
|
296 |
|
9b3486880437
8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents:
34529
diff
changeset
|
297 |
/** |
9b3486880437
8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents:
34529
diff
changeset
|
298 |
* Convert IP address from byte array to integer |
9b3486880437
8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents:
34529
diff
changeset
|
299 |
*/ |
9b3486880437
8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents:
34529
diff
changeset
|
300 |
sz = (*env)->GetArrayLength(env, addrArray); |
9b3486880437
8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents:
34529
diff
changeset
|
301 |
if (sz != 4) { |
9b3486880437
8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents:
34529
diff
changeset
|
302 |
return JNI_FALSE; |
9b3486880437
8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents:
34529
diff
changeset
|
303 |
} |
9b3486880437
8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents:
34529
diff
changeset
|
304 |
memset((char *) &him, 0, sizeof(him)); |
9b3486880437
8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents:
34529
diff
changeset
|
305 |
memset((char *) caddr, 0, sizeof(caddr)); |
9b3486880437
8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents:
34529
diff
changeset
|
306 |
(*env)->GetByteArrayRegion(env, addrArray, 0, 4, caddr); |
9b3486880437
8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents:
34529
diff
changeset
|
307 |
addr = ((caddr[0]<<24) & 0xff000000); |
9b3486880437
8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents:
34529
diff
changeset
|
308 |
addr |= ((caddr[1] <<16) & 0xff0000); |
9b3486880437
8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents:
34529
diff
changeset
|
309 |
addr |= ((caddr[2] <<8) & 0xff00); |
9b3486880437
8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents:
34529
diff
changeset
|
310 |
addr |= (caddr[3] & 0xff); |
9b3486880437
8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents:
34529
diff
changeset
|
311 |
addr = htonl(addr); |
9b3486880437
8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents:
34529
diff
changeset
|
312 |
/** |
9b3486880437
8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents:
34529
diff
changeset
|
313 |
* Socket address |
9b3486880437
8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents:
34529
diff
changeset
|
314 |
*/ |
9b3486880437
8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents:
34529
diff
changeset
|
315 |
him.sin_addr.s_addr = addr; |
9b3486880437
8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents:
34529
diff
changeset
|
316 |
him.sin_family = AF_INET; |
9b3486880437
8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents:
34529
diff
changeset
|
317 |
len = sizeof(him); |
9b3486880437
8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents:
34529
diff
changeset
|
318 |
|
9b3486880437
8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents:
34529
diff
changeset
|
319 |
/** |
9b3486880437
8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents:
34529
diff
changeset
|
320 |
* If a network interface was specified, let's convert its address |
9b3486880437
8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents:
34529
diff
changeset
|
321 |
* as well. |
9b3486880437
8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents:
34529
diff
changeset
|
322 |
*/ |
9b3486880437
8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents:
34529
diff
changeset
|
323 |
if (!(IS_NULL(ifArray))) { |
9b3486880437
8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents:
34529
diff
changeset
|
324 |
memset((char *) caddr, 0, sizeof(caddr)); |
9b3486880437
8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents:
34529
diff
changeset
|
325 |
(*env)->GetByteArrayRegion(env, ifArray, 0, 4, caddr); |
9b3486880437
8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents:
34529
diff
changeset
|
326 |
addr = ((caddr[0]<<24) & 0xff000000); |
9b3486880437
8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents:
34529
diff
changeset
|
327 |
addr |= ((caddr[1] <<16) & 0xff0000); |
9b3486880437
8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents:
34529
diff
changeset
|
328 |
addr |= ((caddr[2] <<8) & 0xff00); |
9b3486880437
8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents:
34529
diff
changeset
|
329 |
addr |= (caddr[3] & 0xff); |
9b3486880437
8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents:
34529
diff
changeset
|
330 |
addr = htonl(addr); |
9b3486880437
8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents:
34529
diff
changeset
|
331 |
inf.sin_addr.s_addr = addr; |
9b3486880437
8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents:
34529
diff
changeset
|
332 |
inf.sin_family = AF_INET; |
9b3486880437
8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents:
34529
diff
changeset
|
333 |
inf.sin_port = 0; |
9b3486880437
8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents:
34529
diff
changeset
|
334 |
netif = &inf; |
9b3486880437
8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents:
34529
diff
changeset
|
335 |
} |
9b3486880437
8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents:
34529
diff
changeset
|
336 |
|
9b3486880437
8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents:
34529
diff
changeset
|
337 |
/* |
9b3486880437
8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents:
34529
diff
changeset
|
338 |
* Can't create a raw socket, so let's try a TCP socket |
9b3486880437
8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents:
34529
diff
changeset
|
339 |
*/ |
9b3486880437
8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents:
34529
diff
changeset
|
340 |
fd = NET_Socket(AF_INET, SOCK_STREAM, 0); |
9b3486880437
8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents:
34529
diff
changeset
|
341 |
if (fd == -1) { |
9b3486880437
8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents:
34529
diff
changeset
|
342 |
/* note: if you run out of fds, you may not be able to load |
9b3486880437
8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents:
34529
diff
changeset
|
343 |
* the exception class, and get a NoClassDefFoundError |
9b3486880437
8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents:
34529
diff
changeset
|
344 |
* instead. |
9b3486880437
8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents:
34529
diff
changeset
|
345 |
*/ |
9b3486880437
8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents:
34529
diff
changeset
|
346 |
NET_ThrowNew(env, WSAGetLastError(), "Can't create socket"); |
9b3486880437
8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents:
34529
diff
changeset
|
347 |
return JNI_FALSE; |
9b3486880437
8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents:
34529
diff
changeset
|
348 |
} |
9b3486880437
8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents:
34529
diff
changeset
|
349 |
if (ttl > 0) { |
9b3486880437
8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents:
34529
diff
changeset
|
350 |
setsockopt(fd, IPPROTO_IP, IP_TTL, (const char *)&ttl, sizeof(ttl)); |
9b3486880437
8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents:
34529
diff
changeset
|
351 |
} |
9b3486880437
8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents:
34529
diff
changeset
|
352 |
/* |
9b3486880437
8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents:
34529
diff
changeset
|
353 |
* A network interface was specified, so let's bind to it. |
9b3486880437
8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents:
34529
diff
changeset
|
354 |
*/ |
9b3486880437
8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents:
34529
diff
changeset
|
355 |
if (netif != NULL) { |
9b3486880437
8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents:
34529
diff
changeset
|
356 |
if (bind(fd, (struct sockaddr*)netif, sizeof(struct sockaddr_in)) < 0) { |
9b3486880437
8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents:
34529
diff
changeset
|
357 |
NET_ThrowNew(env, WSAGetLastError(), "Can't bind socket"); |
9b3486880437
8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents:
34529
diff
changeset
|
358 |
closesocket(fd); |
9b3486880437
8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents:
34529
diff
changeset
|
359 |
return JNI_FALSE; |
9b3486880437
8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents:
34529
diff
changeset
|
360 |
} |
9b3486880437
8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents:
34529
diff
changeset
|
361 |
} |
9b3486880437
8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents:
34529
diff
changeset
|
362 |
|
9b3486880437
8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents:
34529
diff
changeset
|
363 |
/* |
9b3486880437
8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents:
34529
diff
changeset
|
364 |
* Make the socket non blocking so we can use select/poll. |
9b3486880437
8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents:
34529
diff
changeset
|
365 |
*/ |
9b3486880437
8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents:
34529
diff
changeset
|
366 |
hEvent = WSACreateEvent(); |
9b3486880437
8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents:
34529
diff
changeset
|
367 |
WSAEventSelect(fd, hEvent, FD_READ|FD_CONNECT|FD_CLOSE); |
9b3486880437
8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents:
34529
diff
changeset
|
368 |
|
9b3486880437
8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents:
34529
diff
changeset
|
369 |
/* no need to use NET_Connect as non-blocking */ |
9b3486880437
8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents:
34529
diff
changeset
|
370 |
him.sin_port = htons(7); /* Echo */ |
9b3486880437
8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents:
34529
diff
changeset
|
371 |
connect_rv = connect(fd, (struct sockaddr *)&him, len); |
9b3486880437
8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents:
34529
diff
changeset
|
372 |
|
9b3486880437
8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents:
34529
diff
changeset
|
373 |
/** |
9b3486880437
8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents:
34529
diff
changeset
|
374 |
* connection established or refused immediately, either way it means |
9b3486880437
8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents:
34529
diff
changeset
|
375 |
* we were able to reach the host! |
9b3486880437
8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents:
34529
diff
changeset
|
376 |
*/ |
9b3486880437
8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents:
34529
diff
changeset
|
377 |
if (connect_rv == 0 || WSAGetLastError() == WSAECONNREFUSED) { |
9b3486880437
8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents:
34529
diff
changeset
|
378 |
WSACloseEvent(hEvent); |
9b3486880437
8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents:
34529
diff
changeset
|
379 |
closesocket(fd); |
9b3486880437
8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents:
34529
diff
changeset
|
380 |
return JNI_TRUE; |
9b3486880437
8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents:
34529
diff
changeset
|
381 |
} else { |
9b3486880437
8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents:
34529
diff
changeset
|
382 |
int optlen; |
9b3486880437
8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents:
34529
diff
changeset
|
383 |
|
9b3486880437
8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents:
34529
diff
changeset
|
384 |
switch (WSAGetLastError()) { |
9b3486880437
8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents:
34529
diff
changeset
|
385 |
case WSAEHOSTUNREACH: /* Host Unreachable */ |
9b3486880437
8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents:
34529
diff
changeset
|
386 |
case WSAENETUNREACH: /* Network Unreachable */ |
9b3486880437
8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents:
34529
diff
changeset
|
387 |
case WSAENETDOWN: /* Network is down */ |
9b3486880437
8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents:
34529
diff
changeset
|
388 |
case WSAEPFNOSUPPORT: /* Protocol Family unsupported */ |
9b3486880437
8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents:
34529
diff
changeset
|
389 |
WSACloseEvent(hEvent); |
9b3486880437
8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents:
34529
diff
changeset
|
390 |
closesocket(fd); |
9b3486880437
8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents:
34529
diff
changeset
|
391 |
return JNI_FALSE; |
9b3486880437
8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents:
34529
diff
changeset
|
392 |
} |
9b3486880437
8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents:
34529
diff
changeset
|
393 |
|
9b3486880437
8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents:
34529
diff
changeset
|
394 |
if (WSAGetLastError() != WSAEWOULDBLOCK) { |
9b3486880437
8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents:
34529
diff
changeset
|
395 |
NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "ConnectException", |
9b3486880437
8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents:
34529
diff
changeset
|
396 |
"connect failed"); |
9b3486880437
8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents:
34529
diff
changeset
|
397 |
WSACloseEvent(hEvent); |
9b3486880437
8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents:
34529
diff
changeset
|
398 |
closesocket(fd); |
9b3486880437
8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents:
34529
diff
changeset
|
399 |
return JNI_FALSE; |
9b3486880437
8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents:
34529
diff
changeset
|
400 |
} |
9b3486880437
8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents:
34529
diff
changeset
|
401 |
|
9b3486880437
8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents:
34529
diff
changeset
|
402 |
timeout = NET_Wait(env, fd, NET_WAIT_CONNECT, timeout); |
9b3486880437
8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents:
34529
diff
changeset
|
403 |
|
9b3486880437
8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents:
34529
diff
changeset
|
404 |
/* has connection been established */ |
9b3486880437
8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents:
34529
diff
changeset
|
405 |
|
9b3486880437
8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents:
34529
diff
changeset
|
406 |
if (timeout >= 0) { |
9b3486880437
8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents:
34529
diff
changeset
|
407 |
optlen = sizeof(connect_rv); |
9b3486880437
8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents:
34529
diff
changeset
|
408 |
if (getsockopt(fd, SOL_SOCKET, SO_ERROR, (void*)&connect_rv, |
9b3486880437
8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents:
34529
diff
changeset
|
409 |
&optlen) <0) { |
9b3486880437
8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents:
34529
diff
changeset
|
410 |
connect_rv = WSAGetLastError(); |
9b3486880437
8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents:
34529
diff
changeset
|
411 |
} |
9b3486880437
8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents:
34529
diff
changeset
|
412 |
|
9b3486880437
8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents:
34529
diff
changeset
|
413 |
if (connect_rv == 0 || connect_rv == WSAECONNREFUSED) { |
9b3486880437
8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents:
34529
diff
changeset
|
414 |
WSACloseEvent(hEvent); |
9b3486880437
8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents:
34529
diff
changeset
|
415 |
closesocket(fd); |
9b3486880437
8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents:
34529
diff
changeset
|
416 |
return JNI_TRUE; |
9b3486880437
8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents:
34529
diff
changeset
|
417 |
} |
9b3486880437
8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents:
34529
diff
changeset
|
418 |
} |
9b3486880437
8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents:
34529
diff
changeset
|
419 |
} |
9b3486880437
8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents:
34529
diff
changeset
|
420 |
WSACloseEvent(hEvent); |
9b3486880437
8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents:
34529
diff
changeset
|
421 |
closesocket(fd); |
9b3486880437
8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents:
34529
diff
changeset
|
422 |
return JNI_FALSE; |
9b3486880437
8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents:
34529
diff
changeset
|
423 |
} |
2 | 424 |
|
425 |
/** |
|
426 |
* ping implementation. |
|
427 |
* Send a ICMP_ECHO_REQUEST packet every second until either the timeout |
|
428 |
* expires or a answer is received. |
|
429 |
* Returns true is an ECHO_REPLY is received, otherwise, false. |
|
430 |
*/ |
|
431 |
static jboolean |
|
32845
e630c3008f2c
8135305: InetAddress.isReachable reports true when loopback interface is specified
robm
parents:
32266
diff
changeset
|
432 |
ping4(JNIEnv *env, |
e630c3008f2c
8135305: InetAddress.isReachable reports true when loopback interface is specified
robm
parents:
32266
diff
changeset
|
433 |
unsigned long src_addr, |
e630c3008f2c
8135305: InetAddress.isReachable reports true when loopback interface is specified
robm
parents:
32266
diff
changeset
|
434 |
unsigned long dest_addr, |
37362
9b3486880437
8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents:
34529
diff
changeset
|
435 |
jint timeout, |
9b3486880437
8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents:
34529
diff
changeset
|
436 |
HANDLE hIcmpFile) |
32845
e630c3008f2c
8135305: InetAddress.isReachable reports true when loopback interface is specified
robm
parents:
32266
diff
changeset
|
437 |
{ |
32266
e0a235a11254
8133015: InetAddress.isReachable(tmout) returning wrong value on Windows for IPv6
robm
parents:
25859
diff
changeset
|
438 |
// See https://msdn.microsoft.com/en-us/library/aa366050%28VS.85%29.aspx |
2 | 439 |
|
32266
e0a235a11254
8133015: InetAddress.isReachable(tmout) returning wrong value on Windows for IPv6
robm
parents:
25859
diff
changeset
|
440 |
DWORD dwRetVal = 0; |
e0a235a11254
8133015: InetAddress.isReachable(tmout) returning wrong value on Windows for IPv6
robm
parents:
25859
diff
changeset
|
441 |
char SendData[32] = {0}; |
e0a235a11254
8133015: InetAddress.isReachable(tmout) returning wrong value on Windows for IPv6
robm
parents:
25859
diff
changeset
|
442 |
LPVOID ReplyBuffer = NULL; |
e0a235a11254
8133015: InetAddress.isReachable(tmout) returning wrong value on Windows for IPv6
robm
parents:
25859
diff
changeset
|
443 |
DWORD ReplySize = 0; |
34529
3a49a6c8b989
8143397: It looks like InetAddress.isReachable(timeout) works incorrectly
robm
parents:
32845
diff
changeset
|
444 |
jboolean ret = JNI_FALSE; |
2 | 445 |
|
41126
7a2fc45a887d
8159410: InetAddress.isReachable returns true for non existing IP adresses
robm
parents:
37362
diff
changeset
|
446 |
// https://msdn.microsoft.com/en-us/library/windows/desktop/aa366051%28v=vs.85%29.aspx |
7a2fc45a887d
8159410: InetAddress.isReachable returns true for non existing IP adresses
robm
parents:
37362
diff
changeset
|
447 |
ReplySize = sizeof(ICMP_ECHO_REPLY) // The buffer should be large enough |
7a2fc45a887d
8159410: InetAddress.isReachable returns true for non existing IP adresses
robm
parents:
37362
diff
changeset
|
448 |
// to hold at least one ICMP_ECHO_REPLY |
7a2fc45a887d
8159410: InetAddress.isReachable returns true for non existing IP adresses
robm
parents:
37362
diff
changeset
|
449 |
// structure |
7a2fc45a887d
8159410: InetAddress.isReachable returns true for non existing IP adresses
robm
parents:
37362
diff
changeset
|
450 |
+ sizeof(SendData) // plus RequestSize bytes of data. |
7a2fc45a887d
8159410: InetAddress.isReachable returns true for non existing IP adresses
robm
parents:
37362
diff
changeset
|
451 |
+ 8; // This buffer should also be large enough |
7a2fc45a887d
8159410: InetAddress.isReachable returns true for non existing IP adresses
robm
parents:
37362
diff
changeset
|
452 |
// to also hold 8 more bytes of data |
7a2fc45a887d
8159410: InetAddress.isReachable returns true for non existing IP adresses
robm
parents:
37362
diff
changeset
|
453 |
// (the size of an ICMP error message) |
7a2fc45a887d
8159410: InetAddress.isReachable returns true for non existing IP adresses
robm
parents:
37362
diff
changeset
|
454 |
|
32266
e0a235a11254
8133015: InetAddress.isReachable(tmout) returning wrong value on Windows for IPv6
robm
parents:
25859
diff
changeset
|
455 |
ReplyBuffer = (VOID*) malloc(ReplySize); |
e0a235a11254
8133015: InetAddress.isReachable(tmout) returning wrong value on Windows for IPv6
robm
parents:
25859
diff
changeset
|
456 |
if (ReplyBuffer == NULL) { |
e0a235a11254
8133015: InetAddress.isReachable(tmout) returning wrong value on Windows for IPv6
robm
parents:
25859
diff
changeset
|
457 |
IcmpCloseHandle(hIcmpFile); |
e0a235a11254
8133015: InetAddress.isReachable(tmout) returning wrong value on Windows for IPv6
robm
parents:
25859
diff
changeset
|
458 |
NET_ThrowNew(env, WSAGetLastError(), "Unable to allocate memory"); |
e0a235a11254
8133015: InetAddress.isReachable(tmout) returning wrong value on Windows for IPv6
robm
parents:
25859
diff
changeset
|
459 |
return JNI_FALSE; |
e0a235a11254
8133015: InetAddress.isReachable(tmout) returning wrong value on Windows for IPv6
robm
parents:
25859
diff
changeset
|
460 |
} |
2 | 461 |
|
32845
e630c3008f2c
8135305: InetAddress.isReachable reports true when loopback interface is specified
robm
parents:
32266
diff
changeset
|
462 |
if (src_addr == 0) { |
e630c3008f2c
8135305: InetAddress.isReachable reports true when loopback interface is specified
robm
parents:
32266
diff
changeset
|
463 |
dwRetVal = IcmpSendEcho(hIcmpFile, // HANDLE IcmpHandle, |
e630c3008f2c
8135305: InetAddress.isReachable reports true when loopback interface is specified
robm
parents:
32266
diff
changeset
|
464 |
dest_addr, // IPAddr DestinationAddress, |
e630c3008f2c
8135305: InetAddress.isReachable reports true when loopback interface is specified
robm
parents:
32266
diff
changeset
|
465 |
SendData, // LPVOID RequestData, |
e630c3008f2c
8135305: InetAddress.isReachable reports true when loopback interface is specified
robm
parents:
32266
diff
changeset
|
466 |
sizeof(SendData), // WORD RequestSize, |
e630c3008f2c
8135305: InetAddress.isReachable reports true when loopback interface is specified
robm
parents:
32266
diff
changeset
|
467 |
NULL, // PIP_OPTION_INFORMATION RequestOptions, |
e630c3008f2c
8135305: InetAddress.isReachable reports true when loopback interface is specified
robm
parents:
32266
diff
changeset
|
468 |
ReplyBuffer,// LPVOID ReplyBuffer, |
e630c3008f2c
8135305: InetAddress.isReachable reports true when loopback interface is specified
robm
parents:
32266
diff
changeset
|
469 |
ReplySize, // DWORD ReplySize, |
34529
3a49a6c8b989
8143397: It looks like InetAddress.isReachable(timeout) works incorrectly
robm
parents:
32845
diff
changeset
|
470 |
// Note: IcmpSendEcho and its derivatives |
3a49a6c8b989
8143397: It looks like InetAddress.isReachable(timeout) works incorrectly
robm
parents:
32845
diff
changeset
|
471 |
// seem to have an undocumented minimum |
3a49a6c8b989
8143397: It looks like InetAddress.isReachable(timeout) works incorrectly
robm
parents:
32845
diff
changeset
|
472 |
// timeout of 1000ms below which the |
3a49a6c8b989
8143397: It looks like InetAddress.isReachable(timeout) works incorrectly
robm
parents:
32845
diff
changeset
|
473 |
// api behaves inconsistently. |
3a49a6c8b989
8143397: It looks like InetAddress.isReachable(timeout) works incorrectly
robm
parents:
32845
diff
changeset
|
474 |
(timeout < 1000) ? 1000 : timeout); // DWORD Timeout |
32845
e630c3008f2c
8135305: InetAddress.isReachable reports true when loopback interface is specified
robm
parents:
32266
diff
changeset
|
475 |
} else { |
e630c3008f2c
8135305: InetAddress.isReachable reports true when loopback interface is specified
robm
parents:
32266
diff
changeset
|
476 |
dwRetVal = IcmpSendEcho2Ex(hIcmpFile, // HANDLE IcmpHandle, |
e630c3008f2c
8135305: InetAddress.isReachable reports true when loopback interface is specified
robm
parents:
32266
diff
changeset
|
477 |
NULL, // HANDLE Event |
e630c3008f2c
8135305: InetAddress.isReachable reports true when loopback interface is specified
robm
parents:
32266
diff
changeset
|
478 |
NULL, // PIO_APC_ROUTINE ApcRoutine |
e630c3008f2c
8135305: InetAddress.isReachable reports true when loopback interface is specified
robm
parents:
32266
diff
changeset
|
479 |
NULL, // ApcContext |
e630c3008f2c
8135305: InetAddress.isReachable reports true when loopback interface is specified
robm
parents:
32266
diff
changeset
|
480 |
src_addr, // IPAddr SourceAddress, |
e630c3008f2c
8135305: InetAddress.isReachable reports true when loopback interface is specified
robm
parents:
32266
diff
changeset
|
481 |
dest_addr, // IPAddr DestinationAddress, |
e630c3008f2c
8135305: InetAddress.isReachable reports true when loopback interface is specified
robm
parents:
32266
diff
changeset
|
482 |
SendData, // LPVOID RequestData, |
e630c3008f2c
8135305: InetAddress.isReachable reports true when loopback interface is specified
robm
parents:
32266
diff
changeset
|
483 |
sizeof(SendData), // WORD RequestSize, |
e630c3008f2c
8135305: InetAddress.isReachable reports true when loopback interface is specified
robm
parents:
32266
diff
changeset
|
484 |
NULL, // PIP_OPTION_INFORMATION RequestOptions, |
e630c3008f2c
8135305: InetAddress.isReachable reports true when loopback interface is specified
robm
parents:
32266
diff
changeset
|
485 |
ReplyBuffer,// LPVOID ReplyBuffer, |
e630c3008f2c
8135305: InetAddress.isReachable reports true when loopback interface is specified
robm
parents:
32266
diff
changeset
|
486 |
ReplySize, // DWORD ReplySize, |
34529
3a49a6c8b989
8143397: It looks like InetAddress.isReachable(timeout) works incorrectly
robm
parents:
32845
diff
changeset
|
487 |
(timeout < 1000) ? 1000 : timeout); // DWORD Timeout |
3a49a6c8b989
8143397: It looks like InetAddress.isReachable(timeout) works incorrectly
robm
parents:
32845
diff
changeset
|
488 |
} |
3a49a6c8b989
8143397: It looks like InetAddress.isReachable(timeout) works incorrectly
robm
parents:
32845
diff
changeset
|
489 |
|
41126
7a2fc45a887d
8159410: InetAddress.isReachable returns true for non existing IP adresses
robm
parents:
37362
diff
changeset
|
490 |
if (dwRetVal == 0) { // if the call failed |
7a2fc45a887d
8159410: InetAddress.isReachable returns true for non existing IP adresses
robm
parents:
37362
diff
changeset
|
491 |
TCHAR *buf; |
7a2fc45a887d
8159410: InetAddress.isReachable returns true for non existing IP adresses
robm
parents:
37362
diff
changeset
|
492 |
DWORD err = WSAGetLastError(); |
7a2fc45a887d
8159410: InetAddress.isReachable returns true for non existing IP adresses
robm
parents:
37362
diff
changeset
|
493 |
switch (err) { |
7a2fc45a887d
8159410: InetAddress.isReachable returns true for non existing IP adresses
robm
parents:
37362
diff
changeset
|
494 |
case ERROR_NO_NETWORK: |
7a2fc45a887d
8159410: InetAddress.isReachable returns true for non existing IP adresses
robm
parents:
37362
diff
changeset
|
495 |
case ERROR_NETWORK_UNREACHABLE: |
7a2fc45a887d
8159410: InetAddress.isReachable returns true for non existing IP adresses
robm
parents:
37362
diff
changeset
|
496 |
case ERROR_HOST_UNREACHABLE: |
7a2fc45a887d
8159410: InetAddress.isReachable returns true for non existing IP adresses
robm
parents:
37362
diff
changeset
|
497 |
case ERROR_PROTOCOL_UNREACHABLE: |
7a2fc45a887d
8159410: InetAddress.isReachable returns true for non existing IP adresses
robm
parents:
37362
diff
changeset
|
498 |
case ERROR_PORT_UNREACHABLE: |
7a2fc45a887d
8159410: InetAddress.isReachable returns true for non existing IP adresses
robm
parents:
37362
diff
changeset
|
499 |
case ERROR_REQUEST_ABORTED: |
7a2fc45a887d
8159410: InetAddress.isReachable returns true for non existing IP adresses
robm
parents:
37362
diff
changeset
|
500 |
case ERROR_INCORRECT_ADDRESS: |
7a2fc45a887d
8159410: InetAddress.isReachable returns true for non existing IP adresses
robm
parents:
37362
diff
changeset
|
501 |
case ERROR_HOST_DOWN: |
41137
cbc639238d91
8166747: Add invalid network / computer name cases to isReachable known failure switch
robm
parents:
41126
diff
changeset
|
502 |
case ERROR_INVALID_COMPUTERNAME: |
cbc639238d91
8166747: Add invalid network / computer name cases to isReachable known failure switch
robm
parents:
41126
diff
changeset
|
503 |
case ERROR_INVALID_NETNAME: |
41126
7a2fc45a887d
8159410: InetAddress.isReachable returns true for non existing IP adresses
robm
parents:
37362
diff
changeset
|
504 |
case WSAEHOSTUNREACH: /* Host Unreachable */ |
7a2fc45a887d
8159410: InetAddress.isReachable returns true for non existing IP adresses
robm
parents:
37362
diff
changeset
|
505 |
case WSAENETUNREACH: /* Network Unreachable */ |
7a2fc45a887d
8159410: InetAddress.isReachable returns true for non existing IP adresses
robm
parents:
37362
diff
changeset
|
506 |
case WSAENETDOWN: /* Network is down */ |
7a2fc45a887d
8159410: InetAddress.isReachable returns true for non existing IP adresses
robm
parents:
37362
diff
changeset
|
507 |
case WSAEPFNOSUPPORT: /* Protocol Family unsupported */ |
7a2fc45a887d
8159410: InetAddress.isReachable returns true for non existing IP adresses
robm
parents:
37362
diff
changeset
|
508 |
case IP_REQ_TIMED_OUT: |
7a2fc45a887d
8159410: InetAddress.isReachable returns true for non existing IP adresses
robm
parents:
37362
diff
changeset
|
509 |
break; |
7a2fc45a887d
8159410: InetAddress.isReachable returns true for non existing IP adresses
robm
parents:
37362
diff
changeset
|
510 |
default: |
7a2fc45a887d
8159410: InetAddress.isReachable returns true for non existing IP adresses
robm
parents:
37362
diff
changeset
|
511 |
FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM, |
7a2fc45a887d
8159410: InetAddress.isReachable returns true for non existing IP adresses
robm
parents:
37362
diff
changeset
|
512 |
NULL, err, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), |
7a2fc45a887d
8159410: InetAddress.isReachable returns true for non existing IP adresses
robm
parents:
37362
diff
changeset
|
513 |
(LPTSTR)&buf, 0, NULL); |
7a2fc45a887d
8159410: InetAddress.isReachable returns true for non existing IP adresses
robm
parents:
37362
diff
changeset
|
514 |
NET_ThrowNew(env, err, buf); |
7a2fc45a887d
8159410: InetAddress.isReachable returns true for non existing IP adresses
robm
parents:
37362
diff
changeset
|
515 |
LocalFree(buf); |
7a2fc45a887d
8159410: InetAddress.isReachable returns true for non existing IP adresses
robm
parents:
37362
diff
changeset
|
516 |
break; |
7a2fc45a887d
8159410: InetAddress.isReachable returns true for non existing IP adresses
robm
parents:
37362
diff
changeset
|
517 |
} |
7a2fc45a887d
8159410: InetAddress.isReachable returns true for non existing IP adresses
robm
parents:
37362
diff
changeset
|
518 |
} else { |
34529
3a49a6c8b989
8143397: It looks like InetAddress.isReachable(timeout) works incorrectly
robm
parents:
32845
diff
changeset
|
519 |
PICMP_ECHO_REPLY pEchoReply = (PICMP_ECHO_REPLY)ReplyBuffer; |
41126
7a2fc45a887d
8159410: InetAddress.isReachable returns true for non existing IP adresses
robm
parents:
37362
diff
changeset
|
520 |
|
7a2fc45a887d
8159410: InetAddress.isReachable returns true for non existing IP adresses
robm
parents:
37362
diff
changeset
|
521 |
// This is to take into account the undocumented minimum |
7a2fc45a887d
8159410: InetAddress.isReachable returns true for non existing IP adresses
robm
parents:
37362
diff
changeset
|
522 |
// timeout mentioned in the IcmpSendEcho call above. |
7a2fc45a887d
8159410: InetAddress.isReachable returns true for non existing IP adresses
robm
parents:
37362
diff
changeset
|
523 |
// We perform an extra check to make sure that our |
7a2fc45a887d
8159410: InetAddress.isReachable returns true for non existing IP adresses
robm
parents:
37362
diff
changeset
|
524 |
// roundtrip time was less than our desired timeout |
7a2fc45a887d
8159410: InetAddress.isReachable returns true for non existing IP adresses
robm
parents:
37362
diff
changeset
|
525 |
// for cases where that timeout is < 1000ms. |
7a2fc45a887d
8159410: InetAddress.isReachable returns true for non existing IP adresses
robm
parents:
37362
diff
changeset
|
526 |
if (pEchoReply->Status == IP_SUCCESS |
7a2fc45a887d
8159410: InetAddress.isReachable returns true for non existing IP adresses
robm
parents:
37362
diff
changeset
|
527 |
&& (int)pEchoReply->RoundTripTime <= timeout) |
7a2fc45a887d
8159410: InetAddress.isReachable returns true for non existing IP adresses
robm
parents:
37362
diff
changeset
|
528 |
{ |
34529
3a49a6c8b989
8143397: It looks like InetAddress.isReachable(timeout) works incorrectly
robm
parents:
32845
diff
changeset
|
529 |
ret = JNI_TRUE; |
41126
7a2fc45a887d
8159410: InetAddress.isReachable returns true for non existing IP adresses
robm
parents:
37362
diff
changeset
|
530 |
} |
32845
e630c3008f2c
8135305: InetAddress.isReachable reports true when loopback interface is specified
robm
parents:
32266
diff
changeset
|
531 |
} |
32266
e0a235a11254
8133015: InetAddress.isReachable(tmout) returning wrong value on Windows for IPv6
robm
parents:
25859
diff
changeset
|
532 |
|
e0a235a11254
8133015: InetAddress.isReachable(tmout) returning wrong value on Windows for IPv6
robm
parents:
25859
diff
changeset
|
533 |
free(ReplyBuffer); |
e0a235a11254
8133015: InetAddress.isReachable(tmout) returning wrong value on Windows for IPv6
robm
parents:
25859
diff
changeset
|
534 |
IcmpCloseHandle(hIcmpFile); |
e0a235a11254
8133015: InetAddress.isReachable(tmout) returning wrong value on Windows for IPv6
robm
parents:
25859
diff
changeset
|
535 |
|
34529
3a49a6c8b989
8143397: It looks like InetAddress.isReachable(timeout) works incorrectly
robm
parents:
32845
diff
changeset
|
536 |
return ret; |
2 | 537 |
} |
538 |
||
539 |
/* |
|
540 |
* Class: java_net_Inet4AddressImpl |
|
541 |
* Method: isReachable0 |
|
542 |
* Signature: ([bI[bI)Z |
|
543 |
*/ |
|
544 |
JNIEXPORT jboolean JNICALL |
|
545 |
Java_java_net_Inet4AddressImpl_isReachable0(JNIEnv *env, jobject this, |
|
546 |
jbyteArray addrArray, |
|
547 |
jint timeout, |
|
548 |
jbyteArray ifArray, |
|
549 |
jint ttl) { |
|
32845
e630c3008f2c
8135305: InetAddress.isReachable reports true when loopback interface is specified
robm
parents:
32266
diff
changeset
|
550 |
jint src_addr = 0; |
e630c3008f2c
8135305: InetAddress.isReachable reports true when loopback interface is specified
robm
parents:
32266
diff
changeset
|
551 |
jint dest_addr = 0; |
2 | 552 |
jbyte caddr[4]; |
553 |
int sz; |
|
37362
9b3486880437
8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents:
34529
diff
changeset
|
554 |
HANDLE hIcmpFile; |
2 | 555 |
|
556 |
/** |
|
557 |
* Convert IP address from byte array to integer |
|
558 |
*/ |
|
559 |
sz = (*env)->GetArrayLength(env, addrArray); |
|
560 |
if (sz != 4) { |
|
561 |
return JNI_FALSE; |
|
562 |
} |
|
563 |
memset((char *) caddr, 0, sizeof(caddr)); |
|
564 |
(*env)->GetByteArrayRegion(env, addrArray, 0, 4, caddr); |
|
32845
e630c3008f2c
8135305: InetAddress.isReachable reports true when loopback interface is specified
robm
parents:
32266
diff
changeset
|
565 |
dest_addr = ((caddr[0]<<24) & 0xff000000); |
e630c3008f2c
8135305: InetAddress.isReachable reports true when loopback interface is specified
robm
parents:
32266
diff
changeset
|
566 |
dest_addr |= ((caddr[1] <<16) & 0xff0000); |
e630c3008f2c
8135305: InetAddress.isReachable reports true when loopback interface is specified
robm
parents:
32266
diff
changeset
|
567 |
dest_addr |= ((caddr[2] <<8) & 0xff00); |
e630c3008f2c
8135305: InetAddress.isReachable reports true when loopback interface is specified
robm
parents:
32266
diff
changeset
|
568 |
dest_addr |= (caddr[3] & 0xff); |
e630c3008f2c
8135305: InetAddress.isReachable reports true when loopback interface is specified
robm
parents:
32266
diff
changeset
|
569 |
dest_addr = htonl(dest_addr); |
2 | 570 |
|
32845
e630c3008f2c
8135305: InetAddress.isReachable reports true when loopback interface is specified
robm
parents:
32266
diff
changeset
|
571 |
/** |
e630c3008f2c
8135305: InetAddress.isReachable reports true when loopback interface is specified
robm
parents:
32266
diff
changeset
|
572 |
* If a network interface was specified, let's convert its address |
e630c3008f2c
8135305: InetAddress.isReachable reports true when loopback interface is specified
robm
parents:
32266
diff
changeset
|
573 |
* as well. |
e630c3008f2c
8135305: InetAddress.isReachable reports true when loopback interface is specified
robm
parents:
32266
diff
changeset
|
574 |
*/ |
e630c3008f2c
8135305: InetAddress.isReachable reports true when loopback interface is specified
robm
parents:
32266
diff
changeset
|
575 |
if (!(IS_NULL(ifArray))) { |
e630c3008f2c
8135305: InetAddress.isReachable reports true when loopback interface is specified
robm
parents:
32266
diff
changeset
|
576 |
memset((char *) caddr, 0, sizeof(caddr)); |
e630c3008f2c
8135305: InetAddress.isReachable reports true when loopback interface is specified
robm
parents:
32266
diff
changeset
|
577 |
(*env)->GetByteArrayRegion(env, ifArray, 0, 4, caddr); |
e630c3008f2c
8135305: InetAddress.isReachable reports true when loopback interface is specified
robm
parents:
32266
diff
changeset
|
578 |
src_addr = ((caddr[0]<<24) & 0xff000000); |
e630c3008f2c
8135305: InetAddress.isReachable reports true when loopback interface is specified
robm
parents:
32266
diff
changeset
|
579 |
src_addr |= ((caddr[1] <<16) & 0xff0000); |
e630c3008f2c
8135305: InetAddress.isReachable reports true when loopback interface is specified
robm
parents:
32266
diff
changeset
|
580 |
src_addr |= ((caddr[2] <<8) & 0xff00); |
e630c3008f2c
8135305: InetAddress.isReachable reports true when loopback interface is specified
robm
parents:
32266
diff
changeset
|
581 |
src_addr |= (caddr[3] & 0xff); |
e630c3008f2c
8135305: InetAddress.isReachable reports true when loopback interface is specified
robm
parents:
32266
diff
changeset
|
582 |
src_addr = htonl(src_addr); |
e630c3008f2c
8135305: InetAddress.isReachable reports true when loopback interface is specified
robm
parents:
32266
diff
changeset
|
583 |
} |
e630c3008f2c
8135305: InetAddress.isReachable reports true when loopback interface is specified
robm
parents:
32266
diff
changeset
|
584 |
|
37362
9b3486880437
8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents:
34529
diff
changeset
|
585 |
hIcmpFile = IcmpCreateFile(); |
9b3486880437
8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents:
34529
diff
changeset
|
586 |
if (hIcmpFile == INVALID_HANDLE_VALUE) { |
9b3486880437
8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents:
34529
diff
changeset
|
587 |
int err = WSAGetLastError(); |
9b3486880437
8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents:
34529
diff
changeset
|
588 |
if (err == ERROR_ACCESS_DENIED) { |
9b3486880437
8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents:
34529
diff
changeset
|
589 |
// fall back to TCP echo if access is denied to ICMP |
9b3486880437
8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents:
34529
diff
changeset
|
590 |
return tcp_ping4(env, addrArray, timeout, ifArray, ttl); |
9b3486880437
8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents:
34529
diff
changeset
|
591 |
} else { |
9b3486880437
8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents:
34529
diff
changeset
|
592 |
NET_ThrowNew(env, err, "Unable to create ICMP file handle"); |
9b3486880437
8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents:
34529
diff
changeset
|
593 |
return JNI_FALSE; |
9b3486880437
8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents:
34529
diff
changeset
|
594 |
} |
9b3486880437
8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents:
34529
diff
changeset
|
595 |
} else { |
9b3486880437
8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents:
34529
diff
changeset
|
596 |
return ping4(env, src_addr, dest_addr, timeout, hIcmpFile); |
9b3486880437
8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents:
34529
diff
changeset
|
597 |
} |
2 | 598 |
} |
32845
e630c3008f2c
8135305: InetAddress.isReachable reports true when loopback interface is specified
robm
parents:
32266
diff
changeset
|
599 |