Merged with mainline at revision 128810.
[official-gcc.git] / libjava / classpath / native / jni / java-net / java_net_VMInetAddress.c
blob43f2d9146dd4f454e11219f11b9429649318986e
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)
9 any later version.
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
19 02110-1301 USA.
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
24 combination.
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 */
39 #include <config.h>
41 #include <stdlib.h>
42 #include <stdio.h>
43 #include <string.h>
45 #include <jni.h>
46 #include <jcl.h>
48 #include "cpnative.h"
49 #include "cpnet.h"
50 #include "javanet.h"
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,
61 jclass class
62 __attribute__ ((__unused__)))
64 char hostname[256];
65 int result;
66 jstring retval;
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);
80 return (retval);
83 /*************************************************************************/
86 * Returns the value of the special IP address INADDR_ANY
88 JNIEXPORT jarray JNICALL
89 Java_java_net_VMInetAddress_lookupInaddrAny (JNIEnv * env,
90 jclass class
91 __attribute__ ((__unused__)))
93 jarray IParray;
94 cpnet_address *addr;
95 jbyte *octets;
97 /* Allocate an array for the IP address */
98 IParray = (*env)->NewByteArray (env, 4);
99 if (IParray == NULL)
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 */
114 octets[0] = 0;
115 octets[1] = 0;
116 octets[2] = 0;
117 octets[3] = 0;
118 #endif /* not WITHOUT_NETWORK */
120 (*env)->ReleaseByteArrayElements (env, IParray, octets, 0);
122 return (IParray);
125 /*************************************************************************/
128 * Function to return the canonical hostname for a given IP address passed
129 * in as a byte array
131 JNIEXPORT jstring JNICALL
132 Java_java_net_VMInetAddress_getHostByAddr (JNIEnv * env,
133 jclass class
134 __attribute__ ((__unused__)),
135 jarray arr)
137 #ifndef WITHOUT_NETWORK
138 jbyte *octets;
139 jsize len;
140 cpnet_address *addr;
141 char hostname[255];
142 int result;
143 jstring retval;
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);
154 if (!octets)
156 JCL_ThrowException (env, UNKNOWN_HOST_EXCEPTION, "Bad IP Address");
157 return (jstring) NULL;
160 switch (len)
162 case 4:
163 addr = cpnet_newIPV4Address(env);
164 cpnet_bytesToIPV4Address (addr, octets);
165 break;
166 #ifdef HAVE_INET6
167 case 16:
168 addr = cpnet_newIPV6Address(env);
169 cpnet_bytesToIPV6Address (addr, octets);
170 break;
171 #endif
172 default:
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);
192 return (retval);
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,
202 jclass class
203 __attribute__ ((__unused__)),
204 jstring host)
206 #ifndef WITHOUT_NETWORK
207 const char *hostname;
208 cpnet_address **addresses;
209 jsize addresses_count;
210 int result;
211 jclass arr_class;
212 jobjectArray addrs;
213 jint i;
214 jbyte *octets;
215 jarray ret_octets;
217 /* Grab the hostname string */
218 hostname = (*env)->GetStringUTFChars (env, host, 0);
219 if (!hostname)
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");
234 if (!arr_class)
236 JCL_ThrowException (env, UNKNOWN_HOST_EXCEPTION, "Internal Error");
237 return (jobjectArray) NULL;
240 addrs = (*env)->NewObjectArray (env, addresses_count, arr_class, 0);
241 if (!addrs)
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);
254 if (!ret_octets)
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);
269 #ifdef HAVE_INET6
270 else if (cpnet_isIPV6Address (addresses[i]))
272 ret_octets = (*env)->NewByteArray (env, 16);
274 if (!ret_octets)
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);
289 #endif
290 else
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);
300 return (addrs);
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,
314 jclass class
315 __attribute__ ((__unused__)),
316 jstring host)
318 #ifndef WITHOUT_NETWORK
319 const char *hostname;
320 cpnet_address *address;
321 int result;
322 jbyte *octets;
323 jbyteArray ret_octets;
325 hostname = (*env)->GetStringUTFChars (env, host, 0);
326 if (!hostname)
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");
336 if (address)
337 cpnet_freeAddress (env, address);
338 return (jbyteArray) NULL;
340 if (!address)
341 return (jbyteArray) NULL;
343 if (cpnet_isIPV4Address (address))
345 ret_octets = (jbyteArray) (*env)->NewByteArray (env, 4);
347 if (!ret_octets)
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);
360 #ifdef HAVE_INET6
361 else if (cpnet_isIPV6Address (address))
363 ret_octets = (jbyteArray) (*env)->NewByteArray (env, 16);
365 if (!ret_octets)
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);
378 #endif
379 else
381 JCL_ThrowException (env, UNKNOWN_HOST_EXCEPTION, "Internal Error");
382 cpnet_freeAddress (env, address);
383 return (jbyteArray) NULL;
386 cpnet_freeAddress (env, address);
388 return (ret_octets);
390 #else /* not WITHOUT_NETWORK */
391 return (jbyteArray) NULL;
392 #endif /* not WITHOUT_NETWORK */
395 /* end of file */