author | alanb |
Sun, 31 Aug 2008 18:39:01 +0100 | |
changeset 1152 | 29d6145d1097 |
parent 715 | f16baef3a20e |
child 4814 | dbf72872f8d2 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
715 | 2 |
* Copyright 1998-2008 Sun Microsystems, Inc. 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 |
|
7 |
* published by the Free Software Foundation. Sun designates this |
|
8 |
* particular file as subject to the "Classpath" exception as provided |
|
9 |
* by Sun in the LICENSE file that accompanied this code. |
|
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 |
* |
|
21 |
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
|
22 |
* CA 95054 USA or visit www.sun.com if you need additional information or |
|
23 |
* have any questions. |
|
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 (); |
|
71 |
return JNI_VERSION_1_2; |
|
72 |
} |
|
73 |
||
74 |
static int initialized = 0; |
|
75 |
||
76 |
void init(JNIEnv *env) { |
|
77 |
if (!initialized) { |
|
78 |
Java_java_net_InetAddress_init(env, 0); |
|
79 |
Java_java_net_Inet4Address_init(env, 0); |
|
80 |
Java_java_net_Inet6Address_init(env, 0); |
|
81 |
initialized = 1; |
|
82 |
} |
|
83 |
} |
|
84 |
||
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
715
diff
changeset
|
85 |
JNIEXPORT jobject JNICALL |
2 | 86 |
NET_SockaddrToInetAddress(JNIEnv *env, struct sockaddr *him, int *port) { |
87 |
jobject iaObj; |
|
88 |
init(env); |
|
89 |
#ifdef AF_INET6 |
|
90 |
if (him->sa_family == AF_INET6) { |
|
91 |
jbyteArray ipaddress; |
|
92 |
#ifdef WIN32 |
|
93 |
struct SOCKADDR_IN6 *him6 = (struct SOCKADDR_IN6 *)him; |
|
94 |
#else |
|
95 |
struct sockaddr_in6 *him6 = (struct sockaddr_in6 *)him; |
|
96 |
#endif |
|
97 |
jbyte *caddr = (jbyte *)&(him6->sin6_addr); |
|
98 |
if (NET_IsIPv4Mapped(caddr)) { |
|
99 |
int address; |
|
100 |
static jclass inet4Cls = 0; |
|
101 |
if (inet4Cls == 0) { |
|
102 |
jclass c = (*env)->FindClass(env, "java/net/Inet4Address"); |
|
103 |
CHECK_NULL_RETURN(c, NULL); |
|
104 |
inet4Cls = (*env)->NewGlobalRef(env, c); |
|
105 |
CHECK_NULL_RETURN(inet4Cls, NULL); |
|
106 |
(*env)->DeleteLocalRef(env, c); |
|
107 |
} |
|
108 |
iaObj = (*env)->NewObject(env, inet4Cls, ia4_ctrID); |
|
109 |
CHECK_NULL_RETURN(iaObj, NULL); |
|
110 |
address = NET_IPv4MappedToIPv4(caddr); |
|
111 |
(*env)->SetIntField(env, iaObj, ia_addressID, address); |
|
112 |
(*env)->SetIntField(env, iaObj, ia_familyID, IPv4); |
|
113 |
} else { |
|
114 |
static jclass inet6Cls = 0; |
|
482
9cb089744f91
6558853: getHostAddress() on connections using IPv6 link-local addrs should have zone id
jccollet
parents:
2
diff
changeset
|
115 |
jint scope; |
2 | 116 |
if (inet6Cls == 0) { |
117 |
jclass c = (*env)->FindClass(env, "java/net/Inet6Address"); |
|
118 |
CHECK_NULL_RETURN(c, NULL); |
|
119 |
inet6Cls = (*env)->NewGlobalRef(env, c); |
|
120 |
CHECK_NULL_RETURN(inet6Cls, NULL); |
|
121 |
(*env)->DeleteLocalRef(env, c); |
|
122 |
} |
|
123 |
iaObj = (*env)->NewObject(env, inet6Cls, ia6_ctrID); |
|
124 |
CHECK_NULL_RETURN(iaObj, NULL); |
|
125 |
ipaddress = (*env)->NewByteArray(env, 16); |
|
126 |
CHECK_NULL_RETURN(ipaddress, NULL); |
|
127 |
(*env)->SetByteArrayRegion(env, ipaddress, 0, 16, |
|
128 |
(jbyte *)&(him6->sin6_addr)); |
|
129 |
||
130 |
(*env)->SetObjectField(env, iaObj, ia6_ipaddressID, ipaddress); |
|
131 |
||
132 |
(*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
|
133 |
scope = getScopeID(him); |
9cb089744f91
6558853: getHostAddress() on connections using IPv6 link-local addrs should have zone id
jccollet
parents:
2
diff
changeset
|
134 |
(*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
|
135 |
if (scope > 0) |
9cb089744f91
6558853: getHostAddress() on connections using IPv6 link-local addrs should have zone id
jccollet
parents:
2
diff
changeset
|
136 |
(*env)->SetBooleanField(env, iaObj, ia6_scopeidsetID, JNI_TRUE); |
2 | 137 |
} |
138 |
*port = ntohs(him6->sin6_port); |
|
139 |
} else |
|
140 |
#endif /* AF_INET6 */ |
|
141 |
{ |
|
142 |
struct sockaddr_in *him4 = (struct sockaddr_in *)him; |
|
143 |
static jclass inet4Cls = 0; |
|
144 |
||
145 |
if (inet4Cls == 0) { |
|
146 |
jclass c = (*env)->FindClass(env, "java/net/Inet4Address"); |
|
147 |
CHECK_NULL_RETURN(c, NULL); |
|
148 |
inet4Cls = (*env)->NewGlobalRef(env, c); |
|
149 |
CHECK_NULL_RETURN(inet4Cls, NULL); |
|
150 |
(*env)->DeleteLocalRef(env, c); |
|
151 |
} |
|
152 |
iaObj = (*env)->NewObject(env, inet4Cls, ia4_ctrID); |
|
153 |
CHECK_NULL_RETURN(iaObj, NULL); |
|
154 |
(*env)->SetIntField(env, iaObj, ia_familyID, IPv4); |
|
155 |
(*env)->SetIntField(env, iaObj, ia_addressID, |
|
156 |
ntohl(him4->sin_addr.s_addr)); |
|
157 |
*port = ntohs(him4->sin_port); |
|
158 |
} |
|
159 |
return iaObj; |
|
160 |
} |
|
161 |
||
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
715
diff
changeset
|
162 |
JNIEXPORT jint JNICALL |
2 | 163 |
NET_SockaddrEqualsInetAddress(JNIEnv *env, struct sockaddr *him, jobject iaObj) |
164 |
{ |
|
165 |
jint family = (*env)->GetIntField(env, iaObj, ia_familyID) == IPv4? |
|
166 |
AF_INET : AF_INET6; |
|
167 |
||
168 |
#ifdef AF_INET6 |
|
169 |
if (him->sa_family == AF_INET6) { |
|
170 |
#ifdef WIN32 |
|
171 |
struct SOCKADDR_IN6 *him6 = (struct SOCKADDR_IN6 *)him; |
|
172 |
#else |
|
173 |
struct sockaddr_in6 *him6 = (struct sockaddr_in6 *)him; |
|
174 |
#endif |
|
175 |
jbyte *caddrNew = (jbyte *)&(him6->sin6_addr); |
|
176 |
if (NET_IsIPv4Mapped(caddrNew)) { |
|
177 |
int addrNew; |
|
178 |
int addrCur; |
|
179 |
if (family == AF_INET6) { |
|
180 |
return JNI_FALSE; |
|
181 |
} |
|
182 |
addrNew = NET_IPv4MappedToIPv4(caddrNew); |
|
183 |
addrCur = (*env)->GetIntField(env, iaObj, ia_addressID); |
|
184 |
if (addrNew == addrCur) { |
|
185 |
return JNI_TRUE; |
|
186 |
} else { |
|
187 |
return JNI_FALSE; |
|
188 |
} |
|
189 |
} else { |
|
190 |
jbyteArray ipaddress; |
|
191 |
jbyte caddrCur[16]; |
|
192 |
int scope; |
|
193 |
||
194 |
if (family == AF_INET) { |
|
195 |
return JNI_FALSE; |
|
196 |
} |
|
197 |
ipaddress = (*env)->GetObjectField(env, iaObj, ia6_ipaddressID); |
|
198 |
scope = (*env)->GetIntField(env, iaObj, ia6_scopeidID); |
|
199 |
(*env)->GetByteArrayRegion(env, ipaddress, 0, 16, caddrCur); |
|
200 |
if (NET_IsEqual(caddrNew, caddrCur) && cmpScopeID(scope, him)) { |
|
201 |
return JNI_TRUE; |
|
202 |
} else { |
|
203 |
return JNI_FALSE; |
|
204 |
} |
|
205 |
} |
|
206 |
} else |
|
207 |
#endif /* AF_INET6 */ |
|
208 |
{ |
|
209 |
struct sockaddr_in *him4 = (struct sockaddr_in *)him; |
|
210 |
int addrNew, addrCur; |
|
211 |
if (family != AF_INET) { |
|
212 |
return JNI_FALSE; |
|
213 |
} |
|
214 |
addrNew = ntohl(him4->sin_addr.s_addr); |
|
215 |
addrCur = (*env)->GetIntField(env, iaObj, ia_addressID); |
|
216 |
if (addrNew == addrCur) { |
|
217 |
return JNI_TRUE; |
|
218 |
} else { |
|
219 |
return JNI_FALSE; |
|
220 |
} |
|
221 |
} |
|
222 |
} |
|
223 |
||
224 |
unsigned short |
|
225 |
in_cksum(unsigned short *addr, int len) { |
|
226 |
int nleft = len; |
|
227 |
int sum = 0; |
|
228 |
unsigned short *w = addr; |
|
229 |
unsigned short answer = 0; |
|
230 |
while(nleft > 1) { |
|
231 |
sum += *w++; |
|
232 |
nleft -= 2; |
|
233 |
} |
|
234 |
||
235 |
if (nleft == 1) { |
|
236 |
*(unsigned char *) (&answer) = *(unsigned char *)w; |
|
237 |
sum += answer; |
|
238 |
} |
|
239 |
||
240 |
sum = (sum >> 16) + (sum & 0xffff); |
|
241 |
sum += (sum >> 16); |
|
242 |
answer = ~sum; |
|
243 |
return (answer); |
|
244 |
} |