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