Print this page
Split |
Close |
Expand all |
Collapse all |
--- old/src/share/native/java/net/net_util.c
+++ new/src/share/native/java/net/net_util.c
1 1 /*
2 2 * Copyright 1998-2008 Sun Microsystems, Inc. All Rights Reserved.
3 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 4 *
5 5 * This code is free software; you can redistribute it and/or modify it
6 6 * under the terms of the GNU General Public License version 2 only, as
7 7 * published by the Free Software Foundation. Sun designates this
8 8 * particular file as subject to the "Classpath" exception as provided
9 9 * by Sun in the LICENSE file that accompanied this code.
10 10 *
11 11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 14 * version 2 for more details (a copy is included in the LICENSE file that
15 15 * accompanied this code).
16 16 *
17 17 * You should have received a copy of the GNU General Public License version
18 18 * 2 along with this work; if not, write to the Free Software Foundation,
19 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 20 *
21 21 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
22 22 * CA 95054 USA or visit www.sun.com if you need additional information or
23 23 * have any questions.
24 24 */
25 25
26 26 #include "jni.h"
27 27 #include "jvm.h"
28 28 #include "jni_util.h"
29 29 #include "net_util.h"
30 30
31 31 int IPv6_supported() ;
32 32
33 33 static int IPv6_available;
34 34
35 35 JNIEXPORT jint JNICALL ipv6_available()
36 36 {
37 37 return IPv6_available ;
38 38 }
39 39
40 40 JNIEXPORT jint JNICALL
41 41 JNI_OnLoad(JavaVM *vm, void *reserved)
42 42 {
43 43 JNIEnv *env;
44 44 jclass iCls;
45 45 jmethodID mid;
46 46 jstring s;
47 47 jint preferIPv4Stack;
48 48
49 49 if ((*vm)->GetEnv(vm, (void **)&env, JNI_VERSION_1_2) == JNI_OK) {
50 50 if (JVM_InitializeSocketLibrary() < 0) {
51 51 JNU_ThrowByName(env, "java/lang/UnsatisfiedLinkError",
52 52 "failed to initialize net library.");
53 53 return JNI_VERSION_1_2;
54 54 }
55 55 }
56 56 iCls = (*env)->FindClass(env, "java/lang/Boolean");
57 57 CHECK_NULL_RETURN(iCls, JNI_VERSION_1_2);
58 58 mid = (*env)->GetStaticMethodID(env, iCls, "getBoolean", "(Ljava/lang/String;)Z");
59 59 CHECK_NULL_RETURN(mid, JNI_VERSION_1_2);
60 60 s = (*env)->NewStringUTF(env, "java.net.preferIPv4Stack");
61 61 CHECK_NULL_RETURN(s, JNI_VERSION_1_2);
62 62 preferIPv4Stack = (*env)->CallStaticBooleanMethod(env, iCls, mid, s);
63 63
64 64 /*
65 65 Since we have initialized and loaded the Socket library we will
66 66 check now to whether we have IPv6 on this platform and if the
67 67 supporting socket APIs are available
68 68 */
69 69 IPv6_available = IPv6_supported() & (!preferIPv4Stack);
70 70 initLocalAddrTable ();
71 71 return JNI_VERSION_1_2;
72 72 }
73 73
74 74 static int initialized = 0;
75 75
76 76 void init(JNIEnv *env) {
77 77 if (!initialized) {
78 78 Java_java_net_InetAddress_init(env, 0);
79 79 Java_java_net_Inet4Address_init(env, 0);
80 80 Java_java_net_Inet6Address_init(env, 0);
81 81 initialized = 1;
82 82 }
83 83 }
84 84
85 85 JNIEXPORT jobject JNICALL
86 86 NET_SockaddrToInetAddress(JNIEnv *env, struct sockaddr *him, int *port) {
87 87 jobject iaObj;
88 88 init(env);
89 89 #ifdef AF_INET6
↓ open down ↓ |
89 lines elided |
↑ open up ↑ |
90 90 if (him->sa_family == AF_INET6) {
91 91 jbyteArray ipaddress;
92 92 #ifdef WIN32
93 93 struct SOCKADDR_IN6 *him6 = (struct SOCKADDR_IN6 *)him;
94 94 #else
95 95 struct sockaddr_in6 *him6 = (struct sockaddr_in6 *)him;
96 96 #endif
97 97 jbyte *caddr = (jbyte *)&(him6->sin6_addr);
98 98 if (NET_IsIPv4Mapped(caddr)) {
99 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);
100 + iaObj = (*env)->NewObject(env, ia4_class, ia4_ctrID);
109 101 CHECK_NULL_RETURN(iaObj, NULL);
110 102 address = NET_IPv4MappedToIPv4(caddr);
111 103 (*env)->SetIntField(env, iaObj, ia_addressID, address);
112 104 (*env)->SetIntField(env, iaObj, ia_familyID, IPv4);
113 105 } else {
114 - static jclass inet6Cls = 0;
115 106 jint scope;
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);
107 + iaObj = (*env)->NewObject(env, ia6_class, ia6_ctrID);
124 108 CHECK_NULL_RETURN(iaObj, NULL);
125 109 ipaddress = (*env)->NewByteArray(env, 16);
126 110 CHECK_NULL_RETURN(ipaddress, NULL);
127 111 (*env)->SetByteArrayRegion(env, ipaddress, 0, 16,
128 112 (jbyte *)&(him6->sin6_addr));
129 113
130 114 (*env)->SetObjectField(env, iaObj, ia6_ipaddressID, ipaddress);
131 115
132 116 (*env)->SetIntField(env, iaObj, ia_familyID, IPv6);
133 117 scope = getScopeID(him);
134 118 (*env)->SetIntField(env, iaObj, ia6_scopeidID, scope);
135 119 if (scope > 0)
136 120 (*env)->SetBooleanField(env, iaObj, ia6_scopeidsetID, JNI_TRUE);
137 121 }
138 122 *port = ntohs(him6->sin6_port);
139 123 } else
140 124 #endif /* AF_INET6 */
141 125 {
142 126 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);
127 + iaObj = (*env)->NewObject(env, ia4_class, ia4_ctrID);
153 128 CHECK_NULL_RETURN(iaObj, NULL);
154 129 (*env)->SetIntField(env, iaObj, ia_familyID, IPv4);
155 130 (*env)->SetIntField(env, iaObj, ia_addressID,
156 131 ntohl(him4->sin_addr.s_addr));
157 132 *port = ntohs(him4->sin_port);
158 133 }
159 134 return iaObj;
160 135 }
161 136
162 137 JNIEXPORT jint JNICALL
163 138 NET_SockaddrEqualsInetAddress(JNIEnv *env, struct sockaddr *him, jobject iaObj)
164 139 {
165 140 jint family = AF_INET;
166 141
167 142 #ifdef AF_INET6
168 143 family = (*env)->GetIntField(env, iaObj, ia_familyID) == IPv4?
169 144 AF_INET : AF_INET6;
170 145 if (him->sa_family == AF_INET6) {
171 146 #ifdef WIN32
172 147 struct SOCKADDR_IN6 *him6 = (struct SOCKADDR_IN6 *)him;
173 148 #else
174 149 struct sockaddr_in6 *him6 = (struct sockaddr_in6 *)him;
175 150 #endif
176 151 jbyte *caddrNew = (jbyte *)&(him6->sin6_addr);
177 152 if (NET_IsIPv4Mapped(caddrNew)) {
178 153 int addrNew;
179 154 int addrCur;
180 155 if (family == AF_INET6) {
181 156 return JNI_FALSE;
182 157 }
183 158 addrNew = NET_IPv4MappedToIPv4(caddrNew);
184 159 addrCur = (*env)->GetIntField(env, iaObj, ia_addressID);
185 160 if (addrNew == addrCur) {
186 161 return JNI_TRUE;
187 162 } else {
188 163 return JNI_FALSE;
189 164 }
190 165 } else {
191 166 jbyteArray ipaddress;
192 167 jbyte caddrCur[16];
193 168 int scope;
194 169
195 170 if (family == AF_INET) {
196 171 return JNI_FALSE;
197 172 }
198 173 ipaddress = (*env)->GetObjectField(env, iaObj, ia6_ipaddressID);
199 174 scope = (*env)->GetIntField(env, iaObj, ia6_scopeidID);
200 175 (*env)->GetByteArrayRegion(env, ipaddress, 0, 16, caddrCur);
201 176 if (NET_IsEqual(caddrNew, caddrCur) && cmpScopeID(scope, him)) {
202 177 return JNI_TRUE;
203 178 } else {
204 179 return JNI_FALSE;
205 180 }
206 181 }
207 182 } else
208 183 #endif /* AF_INET6 */
209 184 {
210 185 struct sockaddr_in *him4 = (struct sockaddr_in *)him;
211 186 int addrNew, addrCur;
212 187 if (family != AF_INET) {
213 188 return JNI_FALSE;
214 189 }
215 190 addrNew = ntohl(him4->sin_addr.s_addr);
216 191 addrCur = (*env)->GetIntField(env, iaObj, ia_addressID);
217 192 if (addrNew == addrCur) {
218 193 return JNI_TRUE;
219 194 } else {
220 195 return JNI_FALSE;
221 196 }
222 197 }
223 198 }
224 199
225 200 unsigned short
226 201 in_cksum(unsigned short *addr, int len) {
227 202 int nleft = len;
228 203 int sum = 0;
229 204 unsigned short *w = addr;
230 205 unsigned short answer = 0;
231 206 while(nleft > 1) {
232 207 sum += *w++;
233 208 nleft -= 2;
234 209 }
235 210
236 211 if (nleft == 1) {
237 212 *(unsigned char *) (&answer) = *(unsigned char *)w;
238 213 sum += answer;
239 214 }
240 215
241 216 sum = (sum >> 16) + (sum & 0xffff);
242 217 sum += (sum >> 16);
243 218 answer = ~sum;
244 219 return (answer);
245 220 }
↓ open down ↓ |
83 lines elided |
↑ open up ↑ |
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX