1 /* VMInetAddress.c - Native methods for InetAddress class
2 Copyright (C) 1998, 2002, 2005, 2006 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
38 /* do not move; needed here because of some macro definitions */
52 #include "java_net_VMInetAddress.h"
54 /*************************************************************************/
57 * Function to return the local hostname
59 JNIEXPORT jstring JNICALL
60 Java_java_net_VMInetAddress_getLocalHostname (JNIEnv
* env
,
62 __attribute__ ((__unused__
)))
68 #ifndef WITHOUT_NETWORK
69 result
= cpnet_getHostname (env
, hostname
, sizeof (hostname
));
70 if (result
!= CPNATIVE_OK
)
72 strcpy (hostname
, "localhost");
74 #else /* not WITHOUT_NETWORK */
75 strcpy (hostname
, "localhost");
76 #endif /* not WITHOUT_NETWORK */
78 retval
= (*env
)->NewStringUTF (env
, hostname
);
83 /*************************************************************************/
86 * Returns the value of the special IP address INADDR_ANY
88 JNIEXPORT jarray JNICALL
89 Java_java_net_VMInetAddress_lookupInaddrAny (JNIEnv
* env
,
91 __attribute__ ((__unused__
)))
97 /* Allocate an array for the IP address */
98 IParray
= (*env
)->NewByteArray (env
, 4);
101 JCL_ThrowException (env
, UNKNOWN_HOST_EXCEPTION
, "Internal Error");
102 return (jarray
) NULL
;
105 /* Copy in the values */
106 octets
= (*env
)->GetByteArrayElements (env
, IParray
, 0);
108 #ifndef WITHOUT_NETWORK
109 addr
= cpnet_newIPV4Address (env
);
110 cpnet_setIPV4Any (addr
);
111 cpnet_IPV4AddressToBytes (addr
, octets
);
112 cpnet_freeAddress (env
, addr
);
113 #else /* not WITHOUT_NETWORK */
118 #endif /* not WITHOUT_NETWORK */
120 (*env
)->ReleaseByteArrayElements (env
, IParray
, octets
, 0);
125 /*************************************************************************/
128 * Function to return the canonical hostname for a given IP address passed
131 JNIEXPORT jstring JNICALL
132 Java_java_net_VMInetAddress_getHostByAddr (JNIEnv
* env
,
134 __attribute__ ((__unused__
)),
137 #ifndef WITHOUT_NETWORK
145 /* Grab the byte[] array with the IP out of the input data */
146 len
= (*env
)->GetArrayLength (env
, arr
);
147 if (len
!= 4 && len
!= 16)
149 JCL_ThrowException (env
, UNKNOWN_HOST_EXCEPTION
, "Bad IP Address");
150 return (jstring
) NULL
;
153 octets
= (*env
)->GetByteArrayElements (env
, arr
, 0);
156 JCL_ThrowException (env
, UNKNOWN_HOST_EXCEPTION
, "Bad IP Address");
157 return (jstring
) NULL
;
163 addr
= cpnet_newIPV4Address(env
);
164 cpnet_bytesToIPV4Address (addr
, octets
);
168 addr
= cpnet_newIPV6Address(env
);
169 cpnet_bytesToIPV6Address (addr
, octets
);
173 JCL_ThrowException (env
, UNKNOWN_HOST_EXCEPTION
, "Bad IP Address");
174 return (jstring
) NULL
;
178 /* Release some memory */
179 (*env
)->ReleaseByteArrayElements (env
, arr
, octets
, 0);
181 /* Resolve the address and return the name */
182 result
= cpnet_getHostByAddr (env
, addr
, hostname
, sizeof (hostname
));
183 if (result
!= CPNATIVE_OK
)
185 JCL_ThrowException (env
, UNKNOWN_HOST_EXCEPTION
,
186 cpnative_getErrorString (result
));
187 return (jstring
) NULL
;
190 retval
= (*env
)->NewStringUTF (env
, hostname
);
193 #else /* not WITHOUT_NETWORK */
194 return (jstring
) NULL
;
195 #endif /* not WITHOUT_NETWORK */
198 /*************************************************************************/
200 JNIEXPORT jobjectArray JNICALL
201 Java_java_net_VMInetAddress_getHostByName (JNIEnv
* env
,
203 __attribute__ ((__unused__
)),
206 #ifndef WITHOUT_NETWORK
207 const char *hostname
;
208 cpnet_address
**addresses
;
209 jsize addresses_count
;
217 /* Grab the hostname string */
218 hostname
= (*env
)->GetStringUTFChars (env
, host
, 0);
221 JCL_ThrowException (env
, UNKNOWN_HOST_EXCEPTION
, "Null hostname");
222 return (jobjectArray
) NULL
;
225 result
= cpnet_getHostByName (env
, hostname
, &addresses
, &addresses_count
);
226 if (result
!= CPNATIVE_OK
|| addresses_count
== 0)
228 JCL_ThrowException (env
, UNKNOWN_HOST_EXCEPTION
, (char *) hostname
);
229 return (jobjectArray
) NULL
;
231 (*env
)->ReleaseStringUTFChars (env
, host
, hostname
);
233 arr_class
= (*env
)->FindClass (env
, "[B");
236 JCL_ThrowException (env
, UNKNOWN_HOST_EXCEPTION
, "Internal Error");
237 return (jobjectArray
) NULL
;
240 addrs
= (*env
)->NewObjectArray (env
, addresses_count
, arr_class
, 0);
243 JCL_ThrowException (env
, UNKNOWN_HOST_EXCEPTION
, "Internal Error");
244 return (jobjectArray
) NULL
;
247 /* Now loop and copy in each address */
248 for (i
= 0; i
< addresses_count
; i
++)
250 if (cpnet_isIPV4Address (addresses
[i
]))
252 ret_octets
= (*env
)->NewByteArray (env
, 4);
256 JCL_ThrowException (env
, UNKNOWN_HOST_EXCEPTION
, "Internal Error");
257 cpnet_freeAddresses (env
, addresses
, addresses_count
);
258 return (jobjectArray
) NULL
;
261 octets
= (*env
)->GetByteArrayElements (env
, ret_octets
, 0);
263 cpnet_IPV4AddressToBytes (addresses
[i
], octets
);
265 (*env
)->ReleaseByteArrayElements (env
, ret_octets
, octets
, 0);
267 (*env
)->SetObjectArrayElement (env
, addrs
, i
, ret_octets
);
270 else if (cpnet_isIPV6Address (addresses
[i
]))
272 ret_octets
= (*env
)->NewByteArray (env
, 16);
276 JCL_ThrowException (env
, UNKNOWN_HOST_EXCEPTION
, "Internal Error");
277 cpnet_freeAddresses (env
, addresses
, addresses_count
);
278 return (jobjectArray
) NULL
;
281 octets
= (*env
)->GetByteArrayElements (env
, ret_octets
, 0);
283 cpnet_IPV6AddressToBytes (addresses
[i
], octets
);
285 (*env
)->ReleaseByteArrayElements (env
, ret_octets
, octets
, 0);
287 (*env
)->SetObjectArrayElement (env
, addrs
, i
, ret_octets
);
292 JCL_ThrowException (env
, UNKNOWN_HOST_EXCEPTION
, "Internal Error");
293 cpnet_freeAddresses (env
, addresses
, addresses_count
);
294 return (jobjectArray
) NULL
;
298 cpnet_freeAddresses (env
, addresses
, addresses_count
);
301 #else /* not WITHOUT_NETWORK */
302 return (jobjectArray
) NULL
;
303 #endif /* not WITHOUT_NETWORK */
306 /*************************************************************************/
309 * Return the IP address represented by a literal address.
310 * Will return null if the literal address is not valid.
312 JNIEXPORT jbyteArray JNICALL
313 Java_java_net_VMInetAddress_aton (JNIEnv
*env
,
315 __attribute__ ((__unused__
)),
318 #ifndef WITHOUT_NETWORK
319 const char *hostname
;
320 cpnet_address
*address
;
323 jbyteArray ret_octets
;
325 hostname
= (*env
)->GetStringUTFChars (env
, host
, 0);
328 JCL_ThrowException (env
, UNKNOWN_HOST_EXCEPTION
, "Null hostname");
329 return (jbyteArray
) NULL
;
332 result
= cpnet_aton (env
, hostname
, &address
);
333 if (result
!= CPNATIVE_OK
)
335 JCL_ThrowException (env
, UNKNOWN_HOST_EXCEPTION
, "Internal Error");
337 cpnet_freeAddress (env
, address
);
338 return (jbyteArray
) NULL
;
341 return (jbyteArray
) NULL
;
343 if (cpnet_isIPV4Address (address
))
345 ret_octets
= (jbyteArray
) (*env
)->NewByteArray (env
, 4);
349 JCL_ThrowException (env
, UNKNOWN_HOST_EXCEPTION
, "Internal Error");
350 cpnet_freeAddress (env
, address
);
351 return (jbyteArray
) NULL
;
354 octets
= (*env
)->GetByteArrayElements (env
, ret_octets
, 0);
356 cpnet_IPV4AddressToBytes (address
, octets
);
358 (*env
)->ReleaseByteArrayElements (env
, ret_octets
, octets
, 0);
361 else if (cpnet_isIPV6Address (address
))
363 ret_octets
= (jbyteArray
) (*env
)->NewByteArray (env
, 16);
367 JCL_ThrowException (env
, UNKNOWN_HOST_EXCEPTION
, "Internal Error");
368 cpnet_freeAddress (env
, address
);
369 return (jbyteArray
) NULL
;
372 octets
= (*env
)->GetByteArrayElements (env
, ret_octets
, 0);
374 cpnet_IPV6AddressToBytes (address
, octets
);
376 (*env
)->ReleaseByteArrayElements (env
, ret_octets
, octets
, 0);
381 JCL_ThrowException (env
, UNKNOWN_HOST_EXCEPTION
, "Internal Error");
382 cpnet_freeAddress (env
, address
);
383 return (jbyteArray
) NULL
;
386 cpnet_freeAddress (env
, address
);
390 #else /* not WITHOUT_NETWORK */
391 return (jbyteArray
) NULL
;
392 #endif /* not WITHOUT_NETWORK */