Dead
[official-gcc.git] / gomp-20050608-branch / libjava / classpath / native / jni / java-net / java_net_VMInetAddress.c
blob86ac06e6f79d32c3a39bee5c57417aa51e057cd7
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 "javanet.h"
50 #include "target_native.h"
51 #ifndef WITHOUT_NETWORK
52 #include "target_native_network.h"
53 #endif /* WITHOUT_NETWORK */
55 #include "java_net_VMInetAddress.h"
57 /*************************************************************************/
60 * Function to return the local hostname
62 JNIEXPORT jstring JNICALL
63 Java_java_net_VMInetAddress_getLocalHostname (JNIEnv * env,
64 jclass class
65 __attribute__ ((__unused__)))
67 char hostname[256];
68 int result;
69 jstring retval;
71 #ifndef WITHOUT_NETWORK
72 TARGET_NATIVE_NETWORK_GET_HOSTNAME (hostname, sizeof (hostname), result);
73 if (result != TARGET_NATIVE_OK)
75 strcpy (hostname, "localhost");
77 #else /* not WITHOUT_NETWORK */
78 strcpy (hostname, "localhost");
79 #endif /* not WITHOUT_NETWORK */
81 retval = (*env)->NewStringUTF (env, hostname);
83 return (retval);
86 /*************************************************************************/
89 * Returns the value of the special IP address INADDR_ANY
91 JNIEXPORT jarray JNICALL
92 Java_java_net_VMInetAddress_lookupInaddrAny (JNIEnv * env,
93 jclass class
94 __attribute__ ((__unused__)))
96 jarray IParray;
97 jbyte *octets;
99 /* Allocate an array for the IP address */
100 IParray = (*env)->NewByteArray (env, 4);
101 if (IParray == NULL)
103 JCL_ThrowException (env, UNKNOWN_HOST_EXCEPTION, "Internal Error");
104 return (jarray) NULL;
107 /* Copy in the values */
108 octets = (*env)->GetByteArrayElements (env, IParray, 0);
110 #ifndef WITHOUT_NETWORK
111 TARGET_NATIVE_NETWORK_INT_TO_IPADDRESS_BYTES (INADDR_ANY,
112 octets[0],
113 octets[1],
114 octets[2], octets[3]);
115 (*env)->ReleaseByteArrayElements (env, IParray, octets, 0);
116 #else /* not WITHOUT_NETWORK */
117 octets[0] = 0;
118 octets[1] = 0;
119 octets[2] = 0;
120 octets[3] = 0;
121 #endif /* not WITHOUT_NETWORK */
123 return (IParray);
126 /*************************************************************************/
129 * Function to return the canonical hostname for a given IP address passed
130 * in as a byte array
132 JNIEXPORT jstring JNICALL
133 Java_java_net_VMInetAddress_getHostByAddr (JNIEnv * env,
134 jclass class
135 __attribute__ ((__unused__)),
136 jarray arr)
138 #ifndef WITHOUT_NETWORK
139 jbyte *octets;
140 jsize len;
141 int addr;
142 char hostname[255];
143 int result;
144 jstring retval;
146 /* Grab the byte[] array with the IP out of the input data */
147 len = (*env)->GetArrayLength (env, arr);
148 if (len != 4)
150 JCL_ThrowException (env, UNKNOWN_HOST_EXCEPTION, "Bad IP Address");
151 return (jstring) NULL;
154 octets = (*env)->GetByteArrayElements (env, arr, 0);
155 if (!octets)
157 JCL_ThrowException (env, UNKNOWN_HOST_EXCEPTION, "Bad IP Address");
158 return (jstring) NULL;
161 /* Convert it to a 32 bit address */
162 TARGET_NATIVE_NETWORK_IPADDRESS_BYTES_TO_INT (octets[0],
163 octets[1],
164 octets[2], octets[3], addr);
166 /* Release some memory */
167 (*env)->ReleaseByteArrayElements (env, arr, octets, 0);
169 /* Resolve the address and return the name */
170 TARGET_NATIVE_NETWORK_GET_HOSTNAME_BY_ADDRESS (addr, hostname,
171 sizeof (hostname), result);
172 if (result != TARGET_NATIVE_OK)
174 JCL_ThrowException (env, UNKNOWN_HOST_EXCEPTION,
175 TARGET_NATIVE_LAST_ERROR_STRING ());
176 return (jstring) NULL;
179 retval = (*env)->NewStringUTF (env, hostname);
181 return (retval);
182 #else /* not WITHOUT_NETWORK */
183 return (jstring) NULL;
184 #endif /* not WITHOUT_NETWORK */
187 /*************************************************************************/
189 JNIEXPORT jobjectArray JNICALL
190 Java_java_net_VMInetAddress_getHostByName (JNIEnv * env,
191 jclass class
192 __attribute__ ((__unused__)),
193 jstring host)
195 #ifndef WITHOUT_NETWORK
196 const char *hostname;
197 /* FIXME: limitation of max. 64 addresses - how to make it more flexibale? */
198 int addresses[64];
199 jsize addresses_count;
200 int result;
201 jclass arr_class;
202 jobjectArray addrs;
203 int i;
204 jbyte *octets;
205 jarray ret_octets;
206 int max_addresses;
208 /* Grab the hostname string */
209 hostname = (*env)->GetStringUTFChars (env, host, 0);
210 if (!hostname)
212 JCL_ThrowException (env, UNKNOWN_HOST_EXCEPTION, "Null hostname");
213 return (jobjectArray) NULL;
216 max_addresses = sizeof (addresses) / sizeof (addresses[0]);
217 TARGET_NATIVE_NETWORK_GET_HOSTNAME_BY_NAME (hostname,
218 addresses,
219 max_addresses,
220 addresses_count, result);
221 if (result != TARGET_NATIVE_OK)
223 JCL_ThrowException (env, UNKNOWN_HOST_EXCEPTION, (char *) hostname);
224 return (jobjectArray) NULL;
226 (*env)->ReleaseStringUTFChars (env, host, hostname);
228 arr_class = (*env)->FindClass (env, "[B");
229 if (!arr_class)
231 JCL_ThrowException (env, UNKNOWN_HOST_EXCEPTION, "Internal Error");
232 return (jobjectArray) NULL;
235 addrs = (*env)->NewObjectArray (env, addresses_count, arr_class, 0);
236 if (!addrs)
238 JCL_ThrowException (env, UNKNOWN_HOST_EXCEPTION, "Internal Error");
239 return (jobjectArray) NULL;
242 /* Now loop and copy in each address */
243 for (i = 0; i < addresses_count; i++)
245 ret_octets = (*env)->NewByteArray (env, 4);
246 if (!ret_octets)
248 JCL_ThrowException (env, UNKNOWN_HOST_EXCEPTION, "Internal Error");
249 return (jobjectArray) NULL;
252 octets = (*env)->GetByteArrayElements (env, ret_octets, 0);
254 TARGET_NATIVE_NETWORK_INT_TO_IPADDRESS_BYTES (addresses[i],
255 octets[0],
256 octets[1],
257 octets[2], octets[3]);
259 (*env)->ReleaseByteArrayElements (env, ret_octets, octets, 0);
261 (*env)->SetObjectArrayElement (env, addrs, i, ret_octets);
264 return (addrs);
265 #else /* not WITHOUT_NETWORK */
266 return (jobjectArray) NULL;
267 #endif /* not WITHOUT_NETWORK */
270 /* end of file */