50 |
|
|
51 |
#include "target_native.h" |
#include "target_native.h" |
52 |
#ifndef WITHOUT_NETWORK |
#ifndef WITHOUT_NETWORK |
53 |
#include "target_native_network.h" |
#include "target_native_network.h" |
54 |
#endif /* WITHOUT_NETWORK */ |
#endif /* WITHOUT_NETWORK */ |
55 |
|
|
56 |
#include "java_net_InetAddress.h" |
#include "java_net_InetAddress.h" |
61 |
* Function to return the local hostname |
* Function to return the local hostname |
62 |
*/ |
*/ |
63 |
JNIEXPORT jstring JNICALL |
JNIEXPORT jstring JNICALL |
64 |
Java_java_net_InetAddress_getLocalHostname(JNIEnv *env, |
Java_java_net_InetAddress_getLocalHostname (JNIEnv * env, |
65 |
jclass class |
jclass class |
66 |
__attribute__ ((__unused__))) |
__attribute__ ((__unused__))) |
67 |
{ |
{ |
68 |
char hostname[256]; |
char hostname[256]; |
69 |
int result; |
int result; |
70 |
jstring retval; |
jstring retval; |
71 |
|
|
72 |
assert(env!=NULL); |
assert (env != NULL); |
73 |
assert((*env)!=NULL); |
assert ((*env) != NULL); |
74 |
|
|
75 |
#ifndef WITHOUT_NETWORK |
#ifndef WITHOUT_NETWORK |
76 |
TARGET_NATIVE_NETWORK_GET_HOSTNAME(hostname,sizeof(hostname),result); |
TARGET_NATIVE_NETWORK_GET_HOSTNAME (hostname, sizeof (hostname), result); |
77 |
if (result != TARGET_NATIVE_OK) |
if (result != TARGET_NATIVE_OK) |
78 |
{ |
{ |
79 |
strcpy(hostname,"localhost"); |
strcpy (hostname, "localhost"); |
80 |
} |
} |
81 |
#else /* not WITHOUT_NETWORK */ |
#else /* not WITHOUT_NETWORK */ |
82 |
strcpy(hostname, "localhost"); |
strcpy (hostname, "localhost"); |
83 |
#endif /* not WITHOUT_NETWORK */ |
#endif /* not WITHOUT_NETWORK */ |
84 |
|
|
85 |
retval = (*env)->NewStringUTF(env, hostname); |
retval = (*env)->NewStringUTF (env, hostname); |
86 |
|
|
87 |
return(retval); |
return (retval); |
88 |
} |
} |
89 |
|
|
90 |
/*************************************************************************/ |
/*************************************************************************/ |
93 |
* Returns the value of the special IP address INADDR_ANY |
* Returns the value of the special IP address INADDR_ANY |
94 |
*/ |
*/ |
95 |
JNIEXPORT jarray JNICALL |
JNIEXPORT jarray JNICALL |
96 |
Java_java_net_InetAddress_lookupInaddrAny(JNIEnv *env, |
Java_java_net_InetAddress_lookupInaddrAny (JNIEnv * env, |
97 |
jclass class |
jclass class |
98 |
__attribute__ ((__unused__))) |
__attribute__ ((__unused__))) |
99 |
{ |
{ |
100 |
jarray IParray; |
jarray IParray; |
101 |
jbyte *octets; |
jbyte *octets; |
102 |
|
|
103 |
assert(env!=NULL); |
assert (env != NULL); |
104 |
assert((*env)!=NULL); |
assert ((*env) != NULL); |
105 |
|
|
106 |
/* Allocate an array for the IP address */ |
/* Allocate an array for the IP address */ |
107 |
IParray = (*env)->NewByteArray(env, 4); |
IParray = (*env)->NewByteArray (env, 4); |
108 |
if (IParray == NULL) |
if (IParray == NULL) |
109 |
{ |
{ |
110 |
JCL_ThrowException(env, UNKNOWN_HOST_EXCEPTION, "Internal Error"); |
JCL_ThrowException (env, UNKNOWN_HOST_EXCEPTION, "Internal Error"); |
111 |
return (jarray)NULL; |
return (jarray) NULL; |
112 |
} |
} |
113 |
|
|
114 |
/* Copy in the values */ |
/* Copy in the values */ |
115 |
octets = (*env)->GetByteArrayElements(env, IParray, 0); |
octets = (*env)->GetByteArrayElements (env, IParray, 0); |
116 |
|
|
117 |
#ifndef WITHOUT_NETWORK |
#ifndef WITHOUT_NETWORK |
118 |
TARGET_NATIVE_NETWORK_INT_TO_IPADDRESS_BYTES(INADDR_ANY, |
TARGET_NATIVE_NETWORK_INT_TO_IPADDRESS_BYTES (INADDR_ANY, |
119 |
octets[0], |
octets[0], |
120 |
octets[1], |
octets[1], |
121 |
octets[2], |
octets[2], octets[3]); |
122 |
octets[3] |
(*env)->ReleaseByteArrayElements (env, IParray, octets, 0); |
|
); |
|
|
(*env)->ReleaseByteArrayElements(env, IParray, octets, 0); |
|
123 |
#else /* not WITHOUT_NETWORK */ |
#else /* not WITHOUT_NETWORK */ |
124 |
octets[0]=0; |
octets[0] = 0; |
125 |
octets[1]=0; |
octets[1] = 0; |
126 |
octets[2]=0; |
octets[2] = 0; |
127 |
octets[3]=0; |
octets[3] = 0; |
128 |
#endif /* not WITHOUT_NETWORK */ |
#endif /* not WITHOUT_NETWORK */ |
129 |
|
|
130 |
return(IParray); |
return (IParray); |
131 |
} |
} |
132 |
|
|
133 |
/*************************************************************************/ |
/*************************************************************************/ |
137 |
* in as a byte array |
* in as a byte array |
138 |
*/ |
*/ |
139 |
JNIEXPORT jstring JNICALL |
JNIEXPORT jstring JNICALL |
140 |
Java_java_net_InetAddress_getHostByAddr(JNIEnv *env, |
Java_java_net_InetAddress_getHostByAddr (JNIEnv * env, |
141 |
jclass class |
jclass class |
142 |
__attribute__ ((__unused__)), |
__attribute__ ((__unused__)), |
143 |
jarray arr) |
jarray arr) |
144 |
{ |
{ |
145 |
#ifndef WITHOUT_NETWORK |
#ifndef WITHOUT_NETWORK |
146 |
jbyte *octets; |
jbyte *octets; |
147 |
jsize len; |
jsize len; |
148 |
int addr; |
int addr; |
149 |
char hostname[255]; |
char hostname[255]; |
150 |
int result; |
int result; |
151 |
jstring retval; |
jstring retval; |
152 |
|
|
153 |
assert(env!=NULL); |
assert (env != NULL); |
154 |
assert((*env)!=NULL); |
assert ((*env) != NULL); |
155 |
|
|
156 |
/* Grab the byte[] array with the IP out of the input data */ |
/* Grab the byte[] array with the IP out of the input data */ |
157 |
len = (*env)->GetArrayLength(env, arr); |
len = (*env)->GetArrayLength (env, arr); |
158 |
if (len != 4) |
if (len != 4) |
159 |
{ |
{ |
160 |
JCL_ThrowException(env, UNKNOWN_HOST_EXCEPTION, "Bad IP Address"); |
JCL_ThrowException (env, UNKNOWN_HOST_EXCEPTION, "Bad IP Address"); |
161 |
return (jstring)NULL; |
return (jstring) NULL; |
162 |
} |
} |
163 |
|
|
164 |
octets = (*env)->GetByteArrayElements(env, arr, 0); |
octets = (*env)->GetByteArrayElements (env, arr, 0); |
165 |
if (!octets) |
if (!octets) |
166 |
{ |
{ |
167 |
JCL_ThrowException(env, UNKNOWN_HOST_EXCEPTION, "Bad IP Address"); |
JCL_ThrowException (env, UNKNOWN_HOST_EXCEPTION, "Bad IP Address"); |
168 |
return (jstring)NULL; |
return (jstring) NULL; |
169 |
} |
} |
170 |
|
|
171 |
/* Convert it to a 32 bit address */ |
/* Convert it to a 32 bit address */ |
172 |
TARGET_NATIVE_NETWORK_IPADDRESS_BYTES_TO_INT(octets[0], |
TARGET_NATIVE_NETWORK_IPADDRESS_BYTES_TO_INT (octets[0], |
173 |
octets[1], |
octets[1], |
174 |
octets[2], |
octets[2], octets[3], addr); |
|
octets[3], |
|
|
addr |
|
|
); |
|
175 |
|
|
176 |
/* Release some memory */ |
/* Release some memory */ |
177 |
(*env)->ReleaseByteArrayElements(env, arr, octets, 0); |
(*env)->ReleaseByteArrayElements (env, arr, octets, 0); |
178 |
|
|
179 |
/* Resolve the address and return the name */ |
/* Resolve the address and return the name */ |
180 |
TARGET_NATIVE_NETWORK_GET_HOSTNAME_BY_ADDRESS(addr,hostname,sizeof(hostname),result); |
TARGET_NATIVE_NETWORK_GET_HOSTNAME_BY_ADDRESS (addr, hostname, |
181 |
|
sizeof (hostname), result); |
182 |
if (result != TARGET_NATIVE_OK) |
if (result != TARGET_NATIVE_OK) |
183 |
{ |
{ |
184 |
JCL_ThrowException(env, UNKNOWN_HOST_EXCEPTION, "Bad IP address"); |
JCL_ThrowException (env, UNKNOWN_HOST_EXCEPTION, "Bad IP address"); |
185 |
return (jstring)NULL; |
return (jstring) NULL; |
186 |
} |
} |
187 |
|
|
188 |
retval = (*env)->NewStringUTF(env, hostname); |
retval = (*env)->NewStringUTF (env, hostname); |
189 |
|
|
190 |
return(retval); |
return (retval); |
191 |
#else /* not WITHOUT_NETWORK */ |
#else /* not WITHOUT_NETWORK */ |
192 |
return (jstring)NULL; |
return (jstring) NULL; |
193 |
#endif /* not WITHOUT_NETWORK */ |
#endif /* not WITHOUT_NETWORK */ |
194 |
} |
} |
195 |
|
|
196 |
/*************************************************************************/ |
/*************************************************************************/ |
197 |
|
|
198 |
JNIEXPORT jobjectArray JNICALL |
JNIEXPORT jobjectArray JNICALL |
199 |
Java_java_net_InetAddress_getHostByName(JNIEnv *env, |
Java_java_net_InetAddress_getHostByName (JNIEnv * env, |
200 |
jclass class |
jclass class |
201 |
__attribute__ ((__unused__)), |
__attribute__ ((__unused__)), |
202 |
jstring host) |
jstring host) |
203 |
{ |
{ |
204 |
#ifndef WITHOUT_NETWORK |
#ifndef WITHOUT_NETWORK |
205 |
const char *hostname; |
const char *hostname; |
206 |
/* FIXME: limitation of max. 64 addresses - how to make it more flexibale? */ |
/* FIXME: limitation of max. 64 addresses - how to make it more flexibale? */ |
207 |
int addresses[64]; |
int addresses[64]; |
208 |
jsize addresses_count; |
jsize addresses_count; |
209 |
int result; |
int result; |
210 |
jclass arr_class; |
jclass arr_class; |
211 |
jobjectArray addrs; |
jobjectArray addrs; |
212 |
int i; |
int i; |
213 |
jbyte *octets; |
jbyte *octets; |
214 |
jarray ret_octets; |
jarray ret_octets; |
215 |
int max_addresses; |
int max_addresses; |
216 |
|
|
217 |
assert(env!=NULL); |
assert (env != NULL); |
218 |
assert((*env)!=NULL); |
assert ((*env) != NULL); |
219 |
|
|
220 |
/* Grab the hostname string */ |
/* Grab the hostname string */ |
221 |
hostname = (*env)->GetStringUTFChars(env, host, 0); |
hostname = (*env)->GetStringUTFChars (env, host, 0); |
222 |
if (!hostname) |
if (!hostname) |
223 |
{ |
{ |
224 |
JCL_ThrowException(env, UNKNOWN_HOST_EXCEPTION, "Null hostname"); |
JCL_ThrowException (env, UNKNOWN_HOST_EXCEPTION, "Null hostname"); |
225 |
return (jobjectArray)NULL; |
return (jobjectArray) NULL; |
226 |
} |
} |
227 |
|
|
228 |
max_addresses = sizeof(addresses) / sizeof(addresses[0]); |
max_addresses = sizeof (addresses) / sizeof (addresses[0]); |
229 |
TARGET_NATIVE_NETWORK_GET_HOSTNAME_BY_NAME(hostname, |
TARGET_NATIVE_NETWORK_GET_HOSTNAME_BY_NAME (hostname, |
230 |
addresses, |
addresses, |
231 |
max_addresses, |
max_addresses, |
232 |
addresses_count, |
addresses_count, result); |
|
result); |
|
233 |
if (result != TARGET_NATIVE_OK) |
if (result != TARGET_NATIVE_OK) |
234 |
{ |
{ |
235 |
JCL_ThrowException(env, UNKNOWN_HOST_EXCEPTION, (char*)hostname); |
JCL_ThrowException (env, UNKNOWN_HOST_EXCEPTION, (char *) hostname); |
236 |
return (jobjectArray)NULL; |
return (jobjectArray) NULL; |
237 |
} |
} |
238 |
(*env)->ReleaseStringUTFChars(env, host, hostname); |
(*env)->ReleaseStringUTFChars (env, host, hostname); |
239 |
|
|
240 |
arr_class = (*env)->FindClass(env,"[B"); |
arr_class = (*env)->FindClass (env, "[B"); |
241 |
if (!arr_class) |
if (!arr_class) |
242 |
{ |
{ |
243 |
JCL_ThrowException(env, UNKNOWN_HOST_EXCEPTION, "Internal Error"); |
JCL_ThrowException (env, UNKNOWN_HOST_EXCEPTION, "Internal Error"); |
244 |
return (jobjectArray)NULL; |
return (jobjectArray) NULL; |
245 |
} |
} |
246 |
|
|
247 |
addrs = (*env)->NewObjectArray(env, addresses_count, arr_class, 0); |
addrs = (*env)->NewObjectArray (env, addresses_count, arr_class, 0); |
248 |
if (!addrs) |
if (!addrs) |
249 |
{ |
{ |
250 |
JCL_ThrowException(env, UNKNOWN_HOST_EXCEPTION, "Internal Error"); |
JCL_ThrowException (env, UNKNOWN_HOST_EXCEPTION, "Internal Error"); |
251 |
return (jobjectArray)NULL; |
return (jobjectArray) NULL; |
252 |
} |
} |
253 |
|
|
254 |
/* Now loop and copy in each address */ |
/* Now loop and copy in each address */ |
255 |
for (i = 0; i < addresses_count; i++) |
for (i = 0; i < addresses_count; i++) |
256 |
{ |
{ |
257 |
ret_octets = (*env)->NewByteArray(env, 4); |
ret_octets = (*env)->NewByteArray (env, 4); |
258 |
if (!ret_octets) |
if (!ret_octets) |
259 |
{ |
{ |
260 |
JCL_ThrowException(env, UNKNOWN_HOST_EXCEPTION, "Internal Error"); |
JCL_ThrowException (env, UNKNOWN_HOST_EXCEPTION, "Internal Error"); |
261 |
return (jobjectArray)NULL; |
return (jobjectArray) NULL; |
262 |
} |
} |
263 |
|
|
264 |
octets = (*env)->GetByteArrayElements(env, ret_octets, 0); |
octets = (*env)->GetByteArrayElements (env, ret_octets, 0); |
265 |
|
|
266 |
TARGET_NATIVE_NETWORK_INT_TO_IPADDRESS_BYTES(addresses[i], |
TARGET_NATIVE_NETWORK_INT_TO_IPADDRESS_BYTES (addresses[i], |
267 |
octets[0], |
octets[0], |
268 |
octets[1], |
octets[1], |
269 |
octets[2], |
octets[2], octets[3]); |
|
octets[3] |
|
|
); |
|
270 |
|
|
271 |
(*env)->ReleaseByteArrayElements(env, ret_octets, octets, 0); |
(*env)->ReleaseByteArrayElements (env, ret_octets, octets, 0); |
272 |
|
|
273 |
(*env)->SetObjectArrayElement(env, addrs, i, ret_octets); |
(*env)->SetObjectArrayElement (env, addrs, i, ret_octets); |
274 |
} |
} |
275 |
|
|
276 |
return(addrs); |
return (addrs); |
277 |
#else /* not WITHOUT_NETWORK */ |
#else /* not WITHOUT_NETWORK */ |
278 |
return (jobjectArray)NULL; |
return (jobjectArray) NULL; |
279 |
#endif /* not WITHOUT_NETWORK */ |
#endif /* not WITHOUT_NETWORK */ |
280 |
} |
} |
281 |
|
|