author | coffeys |
Thu, 17 May 2012 12:21:16 +0100 | |
changeset 13043 | 10fa4f21ea0f |
parent 7668 | d4a77089c587 |
child 16870 | f35b2bd19761 |
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 |
||
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
715
diff
changeset
|
87 |
JNIEXPORT jobject JNICALL |
2 | 88 |
NET_SockaddrToInetAddress(JNIEnv *env, struct sockaddr *him, int *port) { |
89 |
jobject iaObj; |
|
90 |
init(env); |
|
91 |
#ifdef AF_INET6 |
|
92 |
if (him->sa_family == AF_INET6) { |
|
93 |
jbyteArray ipaddress; |
|
94 |
#ifdef WIN32 |
|
95 |
struct SOCKADDR_IN6 *him6 = (struct SOCKADDR_IN6 *)him; |
|
96 |
#else |
|
97 |
struct sockaddr_in6 *him6 = (struct sockaddr_in6 *)him; |
|
98 |
#endif |
|
99 |
jbyte *caddr = (jbyte *)&(him6->sin6_addr); |
|
100 |
if (NET_IsIPv4Mapped(caddr)) { |
|
101 |
int address; |
|
102 |
static jclass inet4Cls = 0; |
|
103 |
if (inet4Cls == 0) { |
|
104 |
jclass c = (*env)->FindClass(env, "java/net/Inet4Address"); |
|
105 |
CHECK_NULL_RETURN(c, NULL); |
|
106 |
inet4Cls = (*env)->NewGlobalRef(env, c); |
|
107 |
CHECK_NULL_RETURN(inet4Cls, NULL); |
|
108 |
(*env)->DeleteLocalRef(env, c); |
|
109 |
} |
|
110 |
iaObj = (*env)->NewObject(env, inet4Cls, ia4_ctrID); |
|
111 |
CHECK_NULL_RETURN(iaObj, NULL); |
|
112 |
address = NET_IPv4MappedToIPv4(caddr); |
|
113 |
(*env)->SetIntField(env, iaObj, ia_addressID, address); |
|
114 |
(*env)->SetIntField(env, iaObj, ia_familyID, IPv4); |
|
115 |
} else { |
|
116 |
static jclass inet6Cls = 0; |
|
482
9cb089744f91
6558853: getHostAddress() on connections using IPv6 link-local addrs should have zone id
jccollet
parents:
2
diff
changeset
|
117 |
jint scope; |
2 | 118 |
if (inet6Cls == 0) { |
119 |
jclass c = (*env)->FindClass(env, "java/net/Inet6Address"); |
|
120 |
CHECK_NULL_RETURN(c, NULL); |
|
121 |
inet6Cls = (*env)->NewGlobalRef(env, c); |
|
122 |
CHECK_NULL_RETURN(inet6Cls, NULL); |
|
123 |
(*env)->DeleteLocalRef(env, c); |
|
124 |
} |
|
125 |
iaObj = (*env)->NewObject(env, inet6Cls, ia6_ctrID); |
|
126 |
CHECK_NULL_RETURN(iaObj, NULL); |
|
127 |
ipaddress = (*env)->NewByteArray(env, 16); |
|
128 |
CHECK_NULL_RETURN(ipaddress, NULL); |
|
129 |
(*env)->SetByteArrayRegion(env, ipaddress, 0, 16, |
|
130 |
(jbyte *)&(him6->sin6_addr)); |
|
131 |
||
132 |
(*env)->SetObjectField(env, iaObj, ia6_ipaddressID, ipaddress); |
|
133 |
||
134 |
(*env)->SetIntField(env, iaObj, ia_familyID, IPv6); |
|
482
9cb089744f91
6558853: getHostAddress() on connections using IPv6 link-local addrs should have zone id
jccollet
parents:
2
diff
changeset
|
135 |
scope = getScopeID(him); |
9cb089744f91
6558853: getHostAddress() on connections using IPv6 link-local addrs should have zone id
jccollet
parents:
2
diff
changeset
|
136 |
(*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
|
137 |
if (scope > 0) |
9cb089744f91
6558853: getHostAddress() on connections using IPv6 link-local addrs should have zone id
jccollet
parents:
2
diff
changeset
|
138 |
(*env)->SetBooleanField(env, iaObj, ia6_scopeidsetID, JNI_TRUE); |
2 | 139 |
} |
140 |
*port = ntohs(him6->sin6_port); |
|
141 |
} else |
|
142 |
#endif /* AF_INET6 */ |
|
143 |
{ |
|
144 |
struct sockaddr_in *him4 = (struct sockaddr_in *)him; |
|
145 |
static jclass inet4Cls = 0; |
|
146 |
||
147 |
if (inet4Cls == 0) { |
|
148 |
jclass c = (*env)->FindClass(env, "java/net/Inet4Address"); |
|
149 |
CHECK_NULL_RETURN(c, NULL); |
|
150 |
inet4Cls = (*env)->NewGlobalRef(env, c); |
|
151 |
CHECK_NULL_RETURN(inet4Cls, NULL); |
|
152 |
(*env)->DeleteLocalRef(env, c); |
|
153 |
} |
|
154 |
iaObj = (*env)->NewObject(env, inet4Cls, ia4_ctrID); |
|
155 |
CHECK_NULL_RETURN(iaObj, NULL); |
|
156 |
(*env)->SetIntField(env, iaObj, ia_familyID, IPv4); |
|
157 |
(*env)->SetIntField(env, iaObj, ia_addressID, |
|
158 |
ntohl(him4->sin_addr.s_addr)); |
|
159 |
*port = ntohs(him4->sin_port); |
|
160 |
} |
|
161 |
return iaObj; |
|
162 |
} |
|
163 |
||
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
715
diff
changeset
|
164 |
JNIEXPORT jint JNICALL |
2 | 165 |
NET_SockaddrEqualsInetAddress(JNIEnv *env, struct sockaddr *him, jobject iaObj) |
166 |
{ |
|
4814 | 167 |
jint family = AF_INET; |
2 | 168 |
|
169 |
#ifdef AF_INET6 |
|
4814 | 170 |
family = (*env)->GetIntField(env, iaObj, ia_familyID) == IPv4? |
171 |
AF_INET : AF_INET6; |
|
2 | 172 |
if (him->sa_family == AF_INET6) { |
173 |
#ifdef WIN32 |
|
174 |
struct SOCKADDR_IN6 *him6 = (struct SOCKADDR_IN6 *)him; |
|
175 |
#else |
|
176 |
struct sockaddr_in6 *him6 = (struct sockaddr_in6 *)him; |
|
177 |
#endif |
|
178 |
jbyte *caddrNew = (jbyte *)&(him6->sin6_addr); |
|
179 |
if (NET_IsIPv4Mapped(caddrNew)) { |
|
180 |
int addrNew; |
|
181 |
int addrCur; |
|
182 |
if (family == AF_INET6) { |
|
183 |
return JNI_FALSE; |
|
184 |
} |
|
185 |
addrNew = NET_IPv4MappedToIPv4(caddrNew); |
|
186 |
addrCur = (*env)->GetIntField(env, iaObj, ia_addressID); |
|
187 |
if (addrNew == addrCur) { |
|
188 |
return JNI_TRUE; |
|
189 |
} else { |
|
190 |
return JNI_FALSE; |
|
191 |
} |
|
192 |
} else { |
|
193 |
jbyteArray ipaddress; |
|
194 |
jbyte caddrCur[16]; |
|
195 |
int scope; |
|
196 |
||
197 |
if (family == AF_INET) { |
|
198 |
return JNI_FALSE; |
|
199 |
} |
|
200 |
ipaddress = (*env)->GetObjectField(env, iaObj, ia6_ipaddressID); |
|
201 |
scope = (*env)->GetIntField(env, iaObj, ia6_scopeidID); |
|
202 |
(*env)->GetByteArrayRegion(env, ipaddress, 0, 16, caddrCur); |
|
203 |
if (NET_IsEqual(caddrNew, caddrCur) && cmpScopeID(scope, him)) { |
|
204 |
return JNI_TRUE; |
|
205 |
} else { |
|
206 |
return JNI_FALSE; |
|
207 |
} |
|
208 |
} |
|
209 |
} else |
|
210 |
#endif /* AF_INET6 */ |
|
211 |
{ |
|
212 |
struct sockaddr_in *him4 = (struct sockaddr_in *)him; |
|
213 |
int addrNew, addrCur; |
|
214 |
if (family != AF_INET) { |
|
215 |
return JNI_FALSE; |
|
216 |
} |
|
217 |
addrNew = ntohl(him4->sin_addr.s_addr); |
|
218 |
addrCur = (*env)->GetIntField(env, iaObj, ia_addressID); |
|
219 |
if (addrNew == addrCur) { |
|
220 |
return JNI_TRUE; |
|
221 |
} else { |
|
222 |
return JNI_FALSE; |
|
223 |
} |
|
224 |
} |
|
225 |
} |
|
226 |
||
227 |
unsigned short |
|
228 |
in_cksum(unsigned short *addr, int len) { |
|
229 |
int nleft = len; |
|
230 |
int sum = 0; |
|
231 |
unsigned short *w = addr; |
|
232 |
unsigned short answer = 0; |
|
233 |
while(nleft > 1) { |
|
234 |
sum += *w++; |
|
235 |
nleft -= 2; |
|
236 |
} |
|
237 |
||
238 |
if (nleft == 1) { |
|
239 |
*(unsigned char *) (&answer) = *(unsigned char *)w; |
|
240 |
sum += answer; |
|
241 |
} |
|
242 |
||
243 |
sum = (sum >> 16) + (sum & 0xffff); |
|
244 |
sum += (sum >> 16); |
|
245 |
answer = ~sum; |
|
246 |
return (answer); |
|
247 |
} |