author | michaelm |
Wed, 13 Feb 2013 10:40:31 +0000 | |
changeset 16870 | f35b2bd19761 |
parent 13043 | 10fa4f21ea0f |
child 19035 | 129954ce8d08 |
child 20821 | e0d0a585aa49 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
13043
10fa4f21ea0f
7123896: Unexpected behavior due to Solaris using separate IPv4 and IPv6 port spaces
coffeys
parents:
7668
diff
changeset
|
2 |
* Copyright (c) 1998, 2012, Oracle and/or its affiliates. All rights reserved. |
2 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
5506 | 7 |
* published by the Free Software Foundation. Oracle designates this |
2 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
2 | 10 |
* |
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
5506 | 21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
22 |
* or visit www.oracle.com if you need additional information or have any |
|
23 |
* questions. |
|
2 | 24 |
*/ |
25 |
||
26 |
#include "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); |
|
70 |
initLocalAddrTable (); |
|
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 |
||
78 |
void init(JNIEnv *env) { |
|
79 |
if (!initialized) { |
|
80 |
Java_java_net_InetAddress_init(env, 0); |
|
81 |
Java_java_net_Inet4Address_init(env, 0); |
|
82 |
Java_java_net_Inet6Address_init(env, 0); |
|
83 |
initialized = 1; |
|
84 |
} |
|
85 |
} |
|
86 |
||
16870 | 87 |
/* The address, and family fields used to be in InetAddress |
88 |
* but are now in an implementation object. So, there is an extra |
|
89 |
* level of indirection to access them now. |
|
90 |
*/ |
|
91 |
||
92 |
extern jclass iac_class; |
|
93 |
extern jfieldID ia_holderID; |
|
94 |
extern jfieldID iac_addressID; |
|
95 |
extern jfieldID iac_familyID; |
|
96 |
||
97 |
void setInetAddress_addr(JNIEnv *env, jobject iaObj, int address) { |
|
98 |
jobject holder; |
|
99 |
init(env); |
|
100 |
holder = (*env)->GetObjectField(env, iaObj, ia_holderID); |
|
101 |
(*env)->SetIntField(env, holder, iac_addressID, address); |
|
102 |
} |
|
103 |
||
104 |
void setInetAddress_family(JNIEnv *env, jobject iaObj, int family) { |
|
105 |
jobject holder; |
|
106 |
init(env); |
|
107 |
holder = (*env)->GetObjectField(env, iaObj, ia_holderID); |
|
108 |
(*env)->SetIntField(env, holder, iac_familyID, family); |
|
109 |
} |
|
110 |
||
111 |
void setInetAddress_hostName(JNIEnv *env, jobject iaObj, jobject host) { |
|
112 |
jobject holder; |
|
113 |
init(env); |
|
114 |
holder = (*env)->GetObjectField(env, iaObj, ia_holderID); |
|
115 |
(*env)->SetObjectField(env, holder, iac_hostNameID, host); |
|
116 |
} |
|
117 |
||
118 |
int getInetAddress_addr(JNIEnv *env, jobject iaObj) { |
|
119 |
jobject holder; |
|
120 |
init(env); |
|
121 |
holder = (*env)->GetObjectField(env, iaObj, ia_holderID); |
|
122 |
return (*env)->GetIntField(env, holder, iac_addressID); |
|
123 |
} |
|
124 |
||
125 |
int getInetAddress_family(JNIEnv *env, jobject iaObj) { |
|
126 |
jobject holder; |
|
127 |
init(env); |
|
128 |
holder = (*env)->GetObjectField(env, iaObj, ia_holderID); |
|
129 |
return (*env)->GetIntField(env, holder, iac_familyID); |
|
130 |
} |
|
131 |
||
132 |
jobject getInetAddress_hostName(JNIEnv *env, jobject iaObj) { |
|
133 |
jobject holder; |
|
134 |
init(env); |
|
135 |
holder = (*env)->GetObjectField(env, iaObj, ia_holderID); |
|
136 |
return (*env)->GetObjectField(env, holder, iac_hostNameID); |
|
137 |
} |
|
138 |
||
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
715
diff
changeset
|
139 |
JNIEXPORT jobject JNICALL |
2 | 140 |
NET_SockaddrToInetAddress(JNIEnv *env, struct sockaddr *him, int *port) { |
141 |
jobject iaObj; |
|
142 |
init(env); |
|
143 |
#ifdef AF_INET6 |
|
144 |
if (him->sa_family == AF_INET6) { |
|
145 |
jbyteArray ipaddress; |
|
146 |
#ifdef WIN32 |
|
147 |
struct SOCKADDR_IN6 *him6 = (struct SOCKADDR_IN6 *)him; |
|
148 |
#else |
|
149 |
struct sockaddr_in6 *him6 = (struct sockaddr_in6 *)him; |
|
150 |
#endif |
|
151 |
jbyte *caddr = (jbyte *)&(him6->sin6_addr); |
|
152 |
if (NET_IsIPv4Mapped(caddr)) { |
|
153 |
int address; |
|
154 |
static jclass inet4Cls = 0; |
|
155 |
if (inet4Cls == 0) { |
|
156 |
jclass c = (*env)->FindClass(env, "java/net/Inet4Address"); |
|
157 |
CHECK_NULL_RETURN(c, NULL); |
|
158 |
inet4Cls = (*env)->NewGlobalRef(env, c); |
|
159 |
CHECK_NULL_RETURN(inet4Cls, NULL); |
|
160 |
(*env)->DeleteLocalRef(env, c); |
|
161 |
} |
|
162 |
iaObj = (*env)->NewObject(env, inet4Cls, ia4_ctrID); |
|
163 |
CHECK_NULL_RETURN(iaObj, NULL); |
|
164 |
address = NET_IPv4MappedToIPv4(caddr); |
|
16870 | 165 |
setInetAddress_addr(env, iaObj, address); |
166 |
setInetAddress_family(env, iaObj, IPv4); |
|
2 | 167 |
} else { |
168 |
static jclass inet6Cls = 0; |
|
482
9cb089744f91
6558853: getHostAddress() on connections using IPv6 link-local addrs should have zone id
jccollet
parents:
2
diff
changeset
|
169 |
jint scope; |
2 | 170 |
if (inet6Cls == 0) { |
171 |
jclass c = (*env)->FindClass(env, "java/net/Inet6Address"); |
|
172 |
CHECK_NULL_RETURN(c, NULL); |
|
173 |
inet6Cls = (*env)->NewGlobalRef(env, c); |
|
174 |
CHECK_NULL_RETURN(inet6Cls, NULL); |
|
175 |
(*env)->DeleteLocalRef(env, c); |
|
176 |
} |
|
177 |
iaObj = (*env)->NewObject(env, inet6Cls, ia6_ctrID); |
|
178 |
CHECK_NULL_RETURN(iaObj, NULL); |
|
179 |
ipaddress = (*env)->NewByteArray(env, 16); |
|
180 |
CHECK_NULL_RETURN(ipaddress, NULL); |
|
181 |
(*env)->SetByteArrayRegion(env, ipaddress, 0, 16, |
|
182 |
(jbyte *)&(him6->sin6_addr)); |
|
183 |
||
184 |
(*env)->SetObjectField(env, iaObj, ia6_ipaddressID, ipaddress); |
|
185 |
||
16870 | 186 |
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
|
187 |
scope = getScopeID(him); |
9cb089744f91
6558853: getHostAddress() on connections using IPv6 link-local addrs should have zone id
jccollet
parents:
2
diff
changeset
|
188 |
(*env)->SetIntField(env, iaObj, ia6_scopeidID, scope); |
9cb089744f91
6558853: getHostAddress() on connections using IPv6 link-local addrs should have zone id
jccollet
parents:
2
diff
changeset
|
189 |
if (scope > 0) |
9cb089744f91
6558853: getHostAddress() on connections using IPv6 link-local addrs should have zone id
jccollet
parents:
2
diff
changeset
|
190 |
(*env)->SetBooleanField(env, iaObj, ia6_scopeidsetID, JNI_TRUE); |
2 | 191 |
} |
192 |
*port = ntohs(him6->sin6_port); |
|
193 |
} else |
|
194 |
#endif /* AF_INET6 */ |
|
195 |
{ |
|
196 |
struct sockaddr_in *him4 = (struct sockaddr_in *)him; |
|
197 |
static jclass inet4Cls = 0; |
|
198 |
||
199 |
if (inet4Cls == 0) { |
|
200 |
jclass c = (*env)->FindClass(env, "java/net/Inet4Address"); |
|
201 |
CHECK_NULL_RETURN(c, NULL); |
|
202 |
inet4Cls = (*env)->NewGlobalRef(env, c); |
|
203 |
CHECK_NULL_RETURN(inet4Cls, NULL); |
|
204 |
(*env)->DeleteLocalRef(env, c); |
|
205 |
} |
|
206 |
iaObj = (*env)->NewObject(env, inet4Cls, ia4_ctrID); |
|
207 |
CHECK_NULL_RETURN(iaObj, NULL); |
|
16870 | 208 |
setInetAddress_family(env, iaObj, IPv4); |
209 |
setInetAddress_addr(env, iaObj, ntohl(him4->sin_addr.s_addr)); |
|
2 | 210 |
*port = ntohs(him4->sin_port); |
211 |
} |
|
212 |
return iaObj; |
|
213 |
} |
|
214 |
||
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
715
diff
changeset
|
215 |
JNIEXPORT jint JNICALL |
2 | 216 |
NET_SockaddrEqualsInetAddress(JNIEnv *env, struct sockaddr *him, jobject iaObj) |
217 |
{ |
|
4814 | 218 |
jint family = AF_INET; |
2 | 219 |
|
220 |
#ifdef AF_INET6 |
|
16870 | 221 |
family = getInetAddress_family(env, iaObj) == IPv4? AF_INET : AF_INET6; |
2 | 222 |
if (him->sa_family == AF_INET6) { |
223 |
#ifdef WIN32 |
|
224 |
struct SOCKADDR_IN6 *him6 = (struct SOCKADDR_IN6 *)him; |
|
225 |
#else |
|
226 |
struct sockaddr_in6 *him6 = (struct sockaddr_in6 *)him; |
|
227 |
#endif |
|
228 |
jbyte *caddrNew = (jbyte *)&(him6->sin6_addr); |
|
229 |
if (NET_IsIPv4Mapped(caddrNew)) { |
|
230 |
int addrNew; |
|
231 |
int addrCur; |
|
232 |
if (family == AF_INET6) { |
|
233 |
return JNI_FALSE; |
|
234 |
} |
|
235 |
addrNew = NET_IPv4MappedToIPv4(caddrNew); |
|
16870 | 236 |
addrCur = getInetAddress_addr(env, iaObj); |
2 | 237 |
if (addrNew == addrCur) { |
238 |
return JNI_TRUE; |
|
239 |
} else { |
|
240 |
return JNI_FALSE; |
|
241 |
} |
|
242 |
} else { |
|
243 |
jbyteArray ipaddress; |
|
244 |
jbyte caddrCur[16]; |
|
245 |
int scope; |
|
246 |
||
247 |
if (family == AF_INET) { |
|
248 |
return JNI_FALSE; |
|
249 |
} |
|
250 |
ipaddress = (*env)->GetObjectField(env, iaObj, ia6_ipaddressID); |
|
251 |
scope = (*env)->GetIntField(env, iaObj, ia6_scopeidID); |
|
252 |
(*env)->GetByteArrayRegion(env, ipaddress, 0, 16, caddrCur); |
|
253 |
if (NET_IsEqual(caddrNew, caddrCur) && cmpScopeID(scope, him)) { |
|
254 |
return JNI_TRUE; |
|
255 |
} else { |
|
256 |
return JNI_FALSE; |
|
257 |
} |
|
258 |
} |
|
259 |
} else |
|
260 |
#endif /* AF_INET6 */ |
|
261 |
{ |
|
262 |
struct sockaddr_in *him4 = (struct sockaddr_in *)him; |
|
263 |
int addrNew, addrCur; |
|
264 |
if (family != AF_INET) { |
|
265 |
return JNI_FALSE; |
|
266 |
} |
|
267 |
addrNew = ntohl(him4->sin_addr.s_addr); |
|
16870 | 268 |
addrCur = getInetAddress_addr(env, iaObj); |
2 | 269 |
if (addrNew == addrCur) { |
270 |
return JNI_TRUE; |
|
271 |
} else { |
|
272 |
return JNI_FALSE; |
|
273 |
} |
|
274 |
} |
|
275 |
} |
|
276 |
||
277 |
unsigned short |
|
278 |
in_cksum(unsigned short *addr, int len) { |
|
279 |
int nleft = len; |
|
280 |
int sum = 0; |
|
281 |
unsigned short *w = addr; |
|
282 |
unsigned short answer = 0; |
|
283 |
while(nleft > 1) { |
|
284 |
sum += *w++; |
|
285 |
nleft -= 2; |
|
286 |
} |
|
287 |
||
288 |
if (nleft == 1) { |
|
289 |
*(unsigned char *) (&answer) = *(unsigned char *)w; |
|
290 |
sum += answer; |
|
291 |
} |
|
292 |
||
293 |
sum = (sum >> 16) + (sum & 0xffff); |
|
294 |
sum += (sum >> 16); |
|
295 |
answer = ~sum; |
|
296 |
return (answer); |
|
297 |
} |