author | alanb |
Tue, 23 Feb 2016 17:41:00 +0000 | |
changeset 36115 | 0676e37a0b9c |
parent 33660 | 781823826ae0 |
child 41771 | 18c9669e76ca |
permissions | -rw-r--r-- |
2 | 1 |
/* |
36115 | 2 |
* Copyright (c) 1998, 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 |
*/ |
25 |
||
26 |
#include "jni.h" |
|
27 |
#include "jvm.h" |
|
28 |
#include "jni_util.h" |
|
29 |
#include "net_util.h" |
|
30 |
||
31 |
int IPv6_supported() ; |
|
36115 | 32 |
int reuseport_supported() ; |
2 | 33 |
|
34 |
static int IPv6_available; |
|
36115 | 35 |
static int REUSEPORT_available; |
2 | 36 |
|
37 |
JNIEXPORT jint JNICALL ipv6_available() |
|
38 |
{ |
|
39 |
return IPv6_available ; |
|
40 |
} |
|
41 |
||
36115 | 42 |
JNIEXPORT jint JNICALL reuseport_available() |
43 |
{ |
|
44 |
return REUSEPORT_available; |
|
45 |
} |
|
46 |
||
2 | 47 |
JNIEXPORT jint JNICALL |
33653
c1ee09fe3274
8136556: Add the ability to perform static builds of MacOSX x64 binaries
bobv
parents:
25859
diff
changeset
|
48 |
DEF_JNI_OnLoad(JavaVM *vm, void *reserved) |
2 | 49 |
{ |
50 |
JNIEnv *env; |
|
51 |
jclass iCls; |
|
52 |
jmethodID mid; |
|
53 |
jstring s; |
|
54 |
jint preferIPv4Stack; |
|
23015
73b21ab36615
8034174: Remove use of JVM_* functions from java.net code
chegar
parents:
22956
diff
changeset
|
55 |
if ((*vm)->GetEnv(vm, (void**) &env, JNI_VERSION_1_2) != JNI_OK) { |
73b21ab36615
8034174: Remove use of JVM_* functions from java.net code
chegar
parents:
22956
diff
changeset
|
56 |
return JNI_EVERSION; /* JNI version not supported */ |
2 | 57 |
} |
23015
73b21ab36615
8034174: Remove use of JVM_* functions from java.net code
chegar
parents:
22956
diff
changeset
|
58 |
|
2 | 59 |
iCls = (*env)->FindClass(env, "java/lang/Boolean"); |
60 |
CHECK_NULL_RETURN(iCls, JNI_VERSION_1_2); |
|
61 |
mid = (*env)->GetStaticMethodID(env, iCls, "getBoolean", "(Ljava/lang/String;)Z"); |
|
62 |
CHECK_NULL_RETURN(mid, JNI_VERSION_1_2); |
|
63 |
s = (*env)->NewStringUTF(env, "java.net.preferIPv4Stack"); |
|
64 |
CHECK_NULL_RETURN(s, JNI_VERSION_1_2); |
|
65 |
preferIPv4Stack = (*env)->CallStaticBooleanMethod(env, iCls, mid, s); |
|
66 |
||
67 |
/* |
|
68 |
Since we have initialized and loaded the Socket library we will |
|
69 |
check now to whether we have IPv6 on this platform and if the |
|
70 |
supporting socket APIs are available |
|
71 |
*/ |
|
72 |
IPv6_available = IPv6_supported() & (!preferIPv4Stack); |
|
36115 | 73 |
|
74 |
/* check if SO_REUSEPORT is supported on this platform */ |
|
75 |
REUSEPORT_available = reuseport_supported(); |
|
22597
7515a991bb37
8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
20839
diff
changeset
|
76 |
platformInit(); |
13043
10fa4f21ea0f
7123896: Unexpected behavior due to Solaris using separate IPv4 and IPv6 port spaces
coffeys
parents:
7668
diff
changeset
|
77 |
parseExclusiveBindProperty(env); |
10fa4f21ea0f
7123896: Unexpected behavior due to Solaris using separate IPv4 and IPv6 port spaces
coffeys
parents:
7668
diff
changeset
|
78 |
|
2 | 79 |
return JNI_VERSION_1_2; |
80 |
} |
|
81 |
||
82 |
static int initialized = 0; |
|
83 |
||
22646
5fa3669fd35d
8025306: Inet[4|6]Address class and fieldID initialization in networking native code
chegar
parents:
22597
diff
changeset
|
84 |
JNIEXPORT void JNICALL initInetAddressIDs(JNIEnv *env) { |
2 | 85 |
if (!initialized) { |
86 |
Java_java_net_InetAddress_init(env, 0); |
|
22646
5fa3669fd35d
8025306: Inet[4|6]Address class and fieldID initialization in networking native code
chegar
parents:
22597
diff
changeset
|
87 |
JNU_CHECK_EXCEPTION(env); |
2 | 88 |
Java_java_net_Inet4Address_init(env, 0); |
22646
5fa3669fd35d
8025306: Inet[4|6]Address class and fieldID initialization in networking native code
chegar
parents:
22597
diff
changeset
|
89 |
JNU_CHECK_EXCEPTION(env); |
2 | 90 |
Java_java_net_Inet6Address_init(env, 0); |
22646
5fa3669fd35d
8025306: Inet[4|6]Address class and fieldID initialization in networking native code
chegar
parents:
22597
diff
changeset
|
91 |
JNU_CHECK_EXCEPTION(env); |
2 | 92 |
initialized = 1; |
93 |
} |
|
94 |
} |
|
95 |
||
16870 | 96 |
/* The address, and family fields used to be in InetAddress |
97 |
* but are now in an implementation object. So, there is an extra |
|
98 |
* level of indirection to access them now. |
|
99 |
*/ |
|
100 |
||
101 |
extern jclass iac_class; |
|
102 |
extern jfieldID ia_holderID; |
|
103 |
extern jfieldID iac_addressID; |
|
104 |
extern jfieldID iac_familyID; |
|
105 |
||
20821 | 106 |
/** |
107 |
* set_ methods return JNI_TRUE on success JNI_FALSE on error |
|
108 |
* get_ methods that return +ve int return -1 on error |
|
109 |
* get_ methods that return objects return NULL on error. |
|
110 |
*/ |
|
111 |
jobject getInet6Address_scopeifname(JNIEnv *env, jobject iaObj) { |
|
22646
5fa3669fd35d
8025306: Inet[4|6]Address class and fieldID initialization in networking native code
chegar
parents:
22597
diff
changeset
|
112 |
jobject holder = (*env)->GetObjectField(env, iaObj, ia6_holder6ID); |
20821 | 113 |
CHECK_NULL_RETURN(holder, NULL); |
114 |
return (*env)->GetObjectField(env, holder, ia6_scopeifnameID); |
|
115 |
} |
|
116 |
||
22956 | 117 |
jboolean setInet6Address_scopeifname(JNIEnv *env, jobject iaObj, jobject scopeifname) { |
22646
5fa3669fd35d
8025306: Inet[4|6]Address class and fieldID initialization in networking native code
chegar
parents:
22597
diff
changeset
|
118 |
jobject holder = (*env)->GetObjectField(env, iaObj, ia6_holder6ID); |
20821 | 119 |
CHECK_NULL_RETURN(holder, JNI_FALSE); |
120 |
(*env)->SetObjectField(env, holder, ia6_scopeifnameID, scopeifname); |
|
121 |
return JNI_TRUE; |
|
122 |
} |
|
123 |
||
124 |
int getInet6Address_scopeid_set(JNIEnv *env, jobject iaObj) { |
|
22646
5fa3669fd35d
8025306: Inet[4|6]Address class and fieldID initialization in networking native code
chegar
parents:
22597
diff
changeset
|
125 |
jobject holder = (*env)->GetObjectField(env, iaObj, ia6_holder6ID); |
20821 | 126 |
CHECK_NULL_RETURN(holder, -1); |
127 |
return (*env)->GetBooleanField(env, holder, ia6_scopeidsetID); |
|
128 |
} |
|
129 |
||
130 |
int getInet6Address_scopeid(JNIEnv *env, jobject iaObj) { |
|
22646
5fa3669fd35d
8025306: Inet[4|6]Address class and fieldID initialization in networking native code
chegar
parents:
22597
diff
changeset
|
131 |
jobject holder = (*env)->GetObjectField(env, iaObj, ia6_holder6ID); |
20821 | 132 |
CHECK_NULL_RETURN(holder, -1); |
133 |
return (*env)->GetIntField(env, holder, ia6_scopeidID); |
|
134 |
} |
|
135 |
||
22956 | 136 |
jboolean setInet6Address_scopeid(JNIEnv *env, jobject iaObj, int scopeid) { |
22646
5fa3669fd35d
8025306: Inet[4|6]Address class and fieldID initialization in networking native code
chegar
parents:
22597
diff
changeset
|
137 |
jobject holder = (*env)->GetObjectField(env, iaObj, ia6_holder6ID); |
20821 | 138 |
CHECK_NULL_RETURN(holder, JNI_FALSE); |
139 |
(*env)->SetIntField(env, holder, ia6_scopeidID, scopeid); |
|
140 |
if (scopeid > 0) { |
|
22956 | 141 |
(*env)->SetBooleanField(env, holder, ia6_scopeidsetID, JNI_TRUE); |
20821 | 142 |
} |
143 |
return JNI_TRUE; |
|
144 |
} |
|
145 |
||
22956 | 146 |
jboolean getInet6Address_ipaddress(JNIEnv *env, jobject iaObj, char *dest) { |
20821 | 147 |
jobject holder, addr; |
148 |
||
149 |
holder = (*env)->GetObjectField(env, iaObj, ia6_holder6ID); |
|
150 |
CHECK_NULL_RETURN(holder, JNI_FALSE); |
|
151 |
addr = (*env)->GetObjectField(env, holder, ia6_ipaddressID); |
|
152 |
CHECK_NULL_RETURN(addr, JNI_FALSE); |
|
153 |
(*env)->GetByteArrayRegion(env, addr, 0, 16, (jbyte *)dest); |
|
154 |
return JNI_TRUE; |
|
155 |
} |
|
156 |
||
22956 | 157 |
jboolean setInet6Address_ipaddress(JNIEnv *env, jobject iaObj, char *address) { |
20821 | 158 |
jobject holder; |
159 |
jbyteArray addr; |
|
160 |
||
161 |
holder = (*env)->GetObjectField(env, iaObj, ia6_holder6ID); |
|
162 |
CHECK_NULL_RETURN(holder, JNI_FALSE); |
|
163 |
addr = (jbyteArray)(*env)->GetObjectField(env, holder, ia6_ipaddressID); |
|
164 |
if (addr == NULL) { |
|
165 |
addr = (*env)->NewByteArray(env, 16); |
|
166 |
CHECK_NULL_RETURN(addr, JNI_FALSE); |
|
167 |
(*env)->SetObjectField(env, holder, ia6_ipaddressID, addr); |
|
168 |
} |
|
169 |
(*env)->SetByteArrayRegion(env, addr, 0, 16, (jbyte *)address); |
|
170 |
return JNI_TRUE; |
|
171 |
} |
|
172 |
||
16870 | 173 |
void setInetAddress_addr(JNIEnv *env, jobject iaObj, int address) { |
22646
5fa3669fd35d
8025306: Inet[4|6]Address class and fieldID initialization in networking native code
chegar
parents:
22597
diff
changeset
|
174 |
jobject holder = (*env)->GetObjectField(env, iaObj, ia_holderID); |
16870 | 175 |
(*env)->SetIntField(env, holder, iac_addressID, address); |
176 |
} |
|
177 |
||
178 |
void setInetAddress_family(JNIEnv *env, jobject iaObj, int family) { |
|
22646
5fa3669fd35d
8025306: Inet[4|6]Address class and fieldID initialization in networking native code
chegar
parents:
22597
diff
changeset
|
179 |
jobject holder = (*env)->GetObjectField(env, iaObj, ia_holderID); |
16870 | 180 |
(*env)->SetIntField(env, holder, iac_familyID, family); |
181 |
} |
|
182 |
||
183 |
void setInetAddress_hostName(JNIEnv *env, jobject iaObj, jobject host) { |
|
22646
5fa3669fd35d
8025306: Inet[4|6]Address class and fieldID initialization in networking native code
chegar
parents:
22597
diff
changeset
|
184 |
jobject holder = (*env)->GetObjectField(env, iaObj, ia_holderID); |
16870 | 185 |
(*env)->SetObjectField(env, holder, iac_hostNameID, host); |
33301
9ee33abf5b4c
8133196: HTTPS hostname invalid issue with InetAddress
coffeys
parents:
25859
diff
changeset
|
186 |
(*env)->SetObjectField(env, holder, iac_origHostNameID, host); |
16870 | 187 |
} |
188 |
||
189 |
int getInetAddress_addr(JNIEnv *env, jobject iaObj) { |
|
22646
5fa3669fd35d
8025306: Inet[4|6]Address class and fieldID initialization in networking native code
chegar
parents:
22597
diff
changeset
|
190 |
jobject holder = (*env)->GetObjectField(env, iaObj, ia_holderID); |
16870 | 191 |
return (*env)->GetIntField(env, holder, iac_addressID); |
192 |
} |
|
193 |
||
194 |
int getInetAddress_family(JNIEnv *env, jobject iaObj) { |
|
22646
5fa3669fd35d
8025306: Inet[4|6]Address class and fieldID initialization in networking native code
chegar
parents:
22597
diff
changeset
|
195 |
jobject holder = (*env)->GetObjectField(env, iaObj, ia_holderID); |
16870 | 196 |
return (*env)->GetIntField(env, holder, iac_familyID); |
197 |
} |
|
198 |
||
199 |
jobject getInetAddress_hostName(JNIEnv *env, jobject iaObj) { |
|
22646
5fa3669fd35d
8025306: Inet[4|6]Address class and fieldID initialization in networking native code
chegar
parents:
22597
diff
changeset
|
200 |
jobject holder = (*env)->GetObjectField(env, iaObj, ia_holderID); |
16870 | 201 |
return (*env)->GetObjectField(env, holder, iac_hostNameID); |
202 |
} |
|
203 |
||
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
715
diff
changeset
|
204 |
JNIEXPORT jobject JNICALL |
2 | 205 |
NET_SockaddrToInetAddress(JNIEnv *env, struct sockaddr *him, int *port) { |
206 |
jobject iaObj; |
|
207 |
#ifdef AF_INET6 |
|
208 |
if (him->sa_family == AF_INET6) { |
|
209 |
#ifdef WIN32 |
|
210 |
struct SOCKADDR_IN6 *him6 = (struct SOCKADDR_IN6 *)him; |
|
211 |
#else |
|
212 |
struct sockaddr_in6 *him6 = (struct sockaddr_in6 *)him; |
|
213 |
#endif |
|
214 |
jbyte *caddr = (jbyte *)&(him6->sin6_addr); |
|
215 |
if (NET_IsIPv4Mapped(caddr)) { |
|
216 |
int address; |
|
22646
5fa3669fd35d
8025306: Inet[4|6]Address class and fieldID initialization in networking native code
chegar
parents:
22597
diff
changeset
|
217 |
iaObj = (*env)->NewObject(env, ia4_class, ia4_ctrID); |
2 | 218 |
CHECK_NULL_RETURN(iaObj, NULL); |
219 |
address = NET_IPv4MappedToIPv4(caddr); |
|
16870 | 220 |
setInetAddress_addr(env, iaObj, address); |
221 |
setInetAddress_family(env, iaObj, IPv4); |
|
2 | 222 |
} else { |
482
9cb089744f91
6558853: getHostAddress() on connections using IPv6 link-local addrs should have zone id
jccollet
parents:
2
diff
changeset
|
223 |
jint scope; |
22956 | 224 |
jboolean ret; |
22646
5fa3669fd35d
8025306: Inet[4|6]Address class and fieldID initialization in networking native code
chegar
parents:
22597
diff
changeset
|
225 |
iaObj = (*env)->NewObject(env, ia6_class, ia6_ctrID); |
2 | 226 |
CHECK_NULL_RETURN(iaObj, NULL); |
20821 | 227 |
ret = setInet6Address_ipaddress(env, iaObj, (char *)&(him6->sin6_addr)); |
22956 | 228 |
if (ret == JNI_FALSE) |
229 |
return NULL; |
|
16870 | 230 |
setInetAddress_family(env, iaObj, IPv6); |
482
9cb089744f91
6558853: getHostAddress() on connections using IPv6 link-local addrs should have zone id
jccollet
parents:
2
diff
changeset
|
231 |
scope = getScopeID(him); |
20821 | 232 |
setInet6Address_scopeid(env, iaObj, scope); |
2 | 233 |
} |
234 |
*port = ntohs(him6->sin6_port); |
|
235 |
} else |
|
236 |
#endif /* AF_INET6 */ |
|
237 |
{ |
|
238 |
struct sockaddr_in *him4 = (struct sockaddr_in *)him; |
|
22646
5fa3669fd35d
8025306: Inet[4|6]Address class and fieldID initialization in networking native code
chegar
parents:
22597
diff
changeset
|
239 |
iaObj = (*env)->NewObject(env, ia4_class, ia4_ctrID); |
2 | 240 |
CHECK_NULL_RETURN(iaObj, NULL); |
16870 | 241 |
setInetAddress_family(env, iaObj, IPv4); |
242 |
setInetAddress_addr(env, iaObj, ntohl(him4->sin_addr.s_addr)); |
|
2 | 243 |
*port = ntohs(him4->sin_port); |
244 |
} |
|
245 |
return iaObj; |
|
246 |
} |
|
247 |
||
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
715
diff
changeset
|
248 |
JNIEXPORT jint JNICALL |
2 | 249 |
NET_SockaddrEqualsInetAddress(JNIEnv *env, struct sockaddr *him, jobject iaObj) |
250 |
{ |
|
4814 | 251 |
jint family = AF_INET; |
2 | 252 |
|
253 |
#ifdef AF_INET6 |
|
16870 | 254 |
family = getInetAddress_family(env, iaObj) == IPv4? AF_INET : AF_INET6; |
2 | 255 |
if (him->sa_family == AF_INET6) { |
256 |
#ifdef WIN32 |
|
257 |
struct SOCKADDR_IN6 *him6 = (struct SOCKADDR_IN6 *)him; |
|
258 |
#else |
|
259 |
struct sockaddr_in6 *him6 = (struct sockaddr_in6 *)him; |
|
260 |
#endif |
|
261 |
jbyte *caddrNew = (jbyte *)&(him6->sin6_addr); |
|
262 |
if (NET_IsIPv4Mapped(caddrNew)) { |
|
263 |
int addrNew; |
|
264 |
int addrCur; |
|
265 |
if (family == AF_INET6) { |
|
266 |
return JNI_FALSE; |
|
267 |
} |
|
268 |
addrNew = NET_IPv4MappedToIPv4(caddrNew); |
|
16870 | 269 |
addrCur = getInetAddress_addr(env, iaObj); |
2 | 270 |
if (addrNew == addrCur) { |
271 |
return JNI_TRUE; |
|
272 |
} else { |
|
273 |
return JNI_FALSE; |
|
274 |
} |
|
275 |
} else { |
|
276 |
jbyte caddrCur[16]; |
|
277 |
int scope; |
|
278 |
||
279 |
if (family == AF_INET) { |
|
280 |
return JNI_FALSE; |
|
281 |
} |
|
20821 | 282 |
scope = getInet6Address_scopeid(env, iaObj); |
283 |
getInet6Address_ipaddress(env, iaObj, (char *)caddrCur); |
|
2 | 284 |
if (NET_IsEqual(caddrNew, caddrCur) && cmpScopeID(scope, him)) { |
285 |
return JNI_TRUE; |
|
286 |
} else { |
|
287 |
return JNI_FALSE; |
|
288 |
} |
|
289 |
} |
|
290 |
} else |
|
291 |
#endif /* AF_INET6 */ |
|
292 |
{ |
|
293 |
struct sockaddr_in *him4 = (struct sockaddr_in *)him; |
|
294 |
int addrNew, addrCur; |
|
295 |
if (family != AF_INET) { |
|
296 |
return JNI_FALSE; |
|
297 |
} |
|
298 |
addrNew = ntohl(him4->sin_addr.s_addr); |
|
16870 | 299 |
addrCur = getInetAddress_addr(env, iaObj); |
2 | 300 |
if (addrNew == addrCur) { |
301 |
return JNI_TRUE; |
|
302 |
} else { |
|
303 |
return JNI_FALSE; |
|
304 |
} |
|
305 |
} |
|
306 |
} |
|
307 |
||
308 |
unsigned short |
|
309 |
in_cksum(unsigned short *addr, int len) { |
|
310 |
int nleft = len; |
|
311 |
int sum = 0; |
|
312 |
unsigned short *w = addr; |
|
313 |
unsigned short answer = 0; |
|
314 |
while(nleft > 1) { |
|
315 |
sum += *w++; |
|
316 |
nleft -= 2; |
|
317 |
} |
|
318 |
||
319 |
if (nleft == 1) { |
|
320 |
*(unsigned char *) (&answer) = *(unsigned char *)w; |
|
321 |
sum += answer; |
|
322 |
} |
|
323 |
||
324 |
sum = (sum >> 16) + (sum & 0xffff); |
|
325 |
sum += (sum >> 16); |
|
326 |
answer = ~sum; |
|
327 |
return (answer); |
|
328 |
} |