/[classpath]/classpath/native/jni/java-net/java_net_InetAddress.c
ViewVC logotype

Diff of /classpath/native/jni/java-net/java_net_InetAddress.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1.5 by mark, Mon Dec 2 00:28:09 2002 UTC revision 1.6 by rupp, Tue Jul 15 14:35:02 2003 UTC
# Line 35  this exception to your version of the li Line 35  this exception to your version of the li
35  obligated to do so.  If you do not wish to do so, delete this  obligated to do so.  If you do not wish to do so, delete this
36  exception statement from your version. */  exception statement from your version. */
37    
38    /* do not move; needed here because of some macro definitions */
39    #include <config.h>
40    
 #include <stdio.h>  
41  #include <stdlib.h>  #include <stdlib.h>
42    #include <stdio.h>
43  #include <string.h>  #include <string.h>
44  #include <unistd.h>  #include <assert.h>
 #include <netdb.h>  
 #include <sys/socket.h>  
 #include <netinet/in.h>  
45    
46  #include <jni.h>  #include <jni.h>
47  #include <jcl.h>  #include <jcl.h>
48    
 #include "java_net_InetAddress.h"  
   
49  #include "javanet.h"  #include "javanet.h"
50    
51    #include "target_native.h"
52    #ifndef WITHOUT_NETWORK
53      #include "target_native_network.h"
54    #endif /* WITHOUT_NETWORK */
55    
56    #include "java_net_InetAddress.h"
57    
58  /*************************************************************************/  /*************************************************************************/
59    
60  /*  /*
# Line 59  exception statement from your version. * Line 63  exception statement from your version. *
63  JNIEXPORT jstring JNICALL  JNIEXPORT jstring JNICALL
64  Java_java_net_InetAddress_getLocalHostName(JNIEnv *env, jclass class)  Java_java_net_InetAddress_getLocalHostName(JNIEnv *env, jclass class)
65  {  {
66    char buf[255];    char    hostname[256];
67      int     result;
68    jstring retval;    jstring retval;
69    
70    if (gethostname(buf, sizeof(buf) - 1) == -1)    assert(env!=NULL);
71      strcpy(buf, "localhost");    assert((*env)!=NULL);
72    
73    #ifndef WITHOUT_NETWORK
74      TARGET_NATIVE_NETWORK_GET_HOSTNAME(hostname,sizeof(hostname),result);
75      if (result != TARGET_NATIVE_OK)
76        {
77          strcpy(hostname,"localhost");
78        }
79    #else /* not WITHOUT_NETWORK */
80      strcpy(hostname, "localhost");
81    #endif /* not WITHOUT_NETWORK */
82    
83    retval = (*env)->NewStringUTF(env, buf);    retval = (*env)->NewStringUTF(env, hostname);
84    
85    return(retval);    return(retval);
86  }  }
# Line 78  Java_java_net_InetAddress_getLocalHostNa Line 93  Java_java_net_InetAddress_getLocalHostNa
93  JNIEXPORT jarray JNICALL  JNIEXPORT jarray JNICALL
94  Java_java_net_InetAddress_lookupInaddrAny(JNIEnv *env, jclass class)  Java_java_net_InetAddress_lookupInaddrAny(JNIEnv *env, jclass class)
95  {  {
96    jarray arr;    jarray IParray;
97    jbyte *octets;    jbyte  *octets;
98    
99      assert(env!=NULL);
100      assert((*env)!=NULL);
101    
102    /* Allocate an array for the IP address */    /* Allocate an array for the IP address */
103    arr = (*env)->NewByteArray(env, 4);    IParray = (*env)->NewByteArray(env, 4);
104    if (!arr)          if (IParray == NULL)      
105      {      {
106        JCL_ThrowException(env, UNKNOWN_HOST_EXCEPTION, "Internal Error");        JCL_ThrowException(env, UNKNOWN_HOST_EXCEPTION, "Internal Error");
107        return (jarray)NULL;        return (jarray)NULL;
108      }      }
109    
110    /* Copy in the values */    /* Copy in the values */
111    octets = (*env)->GetByteArrayElements(env, arr, 0);    octets = (*env)->GetByteArrayElements(env, IParray, 0);
112    
113    octets[0] = (INADDR_ANY & 0xFF000000) >> 24;  #ifndef WITHOUT_NETWORK
114    octets[1] = (INADDR_ANY & 0x00FF0000) >> 16;    TARGET_NATIVE_NETWORK_INT_TO_IPADDRESS_BYTES(INADDR_ANY,
115    octets[2] = (INADDR_ANY & 0x0000FF00) >> 8;                                                 octets[0],
116    octets[3] = (INADDR_ANY & 0x000000FF);                                                 octets[1],
117                                                   octets[2],
118                                                   octets[3]
119                                                  );
120      (*env)->ReleaseByteArrayElements(env, IParray, octets, 0);
121    #else /* not WITHOUT_NETWORK */
122      octets[0]=0;
123      octets[1]=0;
124      octets[2]=0;
125      octets[3]=0;
126    #endif /* not WITHOUT_NETWORK */
127    
128    (*env)->ReleaseByteArrayElements(env, arr, octets, 0);    return(IParray);
   
   return(arr);  
129  }  }
130    
131  /*************************************************************************/  /*************************************************************************/
# Line 111  Java_java_net_InetAddress_lookupInaddrAn Line 137  Java_java_net_InetAddress_lookupInaddrAn
137  JNIEXPORT jstring JNICALL  JNIEXPORT jstring JNICALL
138  Java_java_net_InetAddress_getHostByAddr(JNIEnv *env, jclass class, jarray arr)  Java_java_net_InetAddress_getHostByAddr(JNIEnv *env, jclass class, jarray arr)
139  {  {
140    jbyte *octets;  #ifndef WITHOUT_NETWORK
141    jsize len;    jbyte   *octets;
142    int addr;    jsize   len;
143    struct hostent *hp;    int     addr;
144      char    hostname[255];
145      int     result;
146    jstring retval;    jstring retval;
147    
148      assert(env!=NULL);
149      assert((*env)!=NULL);
150    
151    /* Grab the byte[] array with the IP out of the input data */    /* Grab the byte[] array with the IP out of the input data */
152    len = (*env)->GetArrayLength(env, arr);    len = (*env)->GetArrayLength(env, arr);
153    if (len != 4)    if (len != 4)
# Line 133  Java_java_net_InetAddress_getHostByAddr( Line 164  Java_java_net_InetAddress_getHostByAddr(
164      }      }
165    
166    /* Convert it to a 32 bit address */    /* Convert it to a 32 bit address */
167    addr = (octets[0] << 24) + (octets[1] << 16) + (octets[2] << 8) + octets[3];    TARGET_NATIVE_NETWORK_IPADDRESS_BYTES_TO_INT(octets[0],
168    addr = htonl(addr);                                                 octets[1],
169                                                   octets[2],
170                                                   octets[3],
171                                                   addr
172                                                  );
173    
174    /* Release some memory */    /* Release some memory */
175    (*env)->ReleaseByteArrayElements(env, arr, octets, 0);    (*env)->ReleaseByteArrayElements(env, arr, octets, 0);
176    
177    /* Resolve the address and return the name */    /* Resolve the address and return the name */
178    hp = gethostbyaddr((char*)&addr, sizeof(addr), AF_INET);    TARGET_NATIVE_NETWORK_GET_HOSTNAME_BY_ADDRESS(addr,hostname,sizeof(hostname),result);
179    if (!hp)    if (result != TARGET_NATIVE_OK)
180      {      {
181        JCL_ThrowException(env, UNKNOWN_HOST_EXCEPTION, "Bad IP Address");        JCL_ThrowException(env, UNKNOWN_HOST_EXCEPTION, "Bad IP address");
182        return (jstring)NULL;        return (jstring)NULL;
183      }      }
184    
185    retval = (*env)->NewStringUTF(env, hp->h_name);    retval = (*env)->NewStringUTF(env, hostname);
186    
187    return(retval);    return(retval);
188    #else /* not WITHOUT_NETWORK */
189      return (jstring)NULL;
190    #endif /* not WITHOUT_NETWORK */
191  }  }
192    
193  /*************************************************************************/  /*************************************************************************/
# Line 157  Java_java_net_InetAddress_getHostByAddr( Line 195  Java_java_net_InetAddress_getHostByAddr(
195  JNIEXPORT jobjectArray JNICALL  JNIEXPORT jobjectArray JNICALL
196  Java_java_net_InetAddress_getHostByName(JNIEnv *env, jclass class, jstring host)  Java_java_net_InetAddress_getHostByName(JNIEnv *env, jclass class, jstring host)
197  {  {
198    const char *hostname;  #ifndef WITHOUT_NETWORK
199    struct hostent *hp;    const char     *hostname;
200    int i, ip;  /* FIXME: limitation of max. 64 addresses - how to make it more flexibale? */
201    jbyte *octets;    int            addresses[64];
202    jsize num_addrs;    jsize          addresses_count;
203    jclass arr_class;    int            result;
204    jobjectArray addrs;    jclass         arr_class;
205    jarray ret_octets;    jobjectArray   addrs;
206      int            i;
207      jbyte          *octets;
208      jarray         ret_octets;
209    
210      assert(env!=NULL);
211      assert((*env)!=NULL);
212    
213    /* Grab the hostname string */    /* Grab the hostname string */
214    hostname = (*env)->GetStringUTFChars(env, host, 0);    hostname = (*env)->GetStringUTFChars(env, host, 0);
# Line 175  Java_java_net_InetAddress_getHostByName( Line 219  Java_java_net_InetAddress_getHostByName(
219      }      }
220    
221    /* Look up the host */    /* Look up the host */
222    hp = gethostbyname(hostname);    TARGET_NATIVE_NETWORK_GET_HOSTNAME_BY_NAME(hostname,
223    if (!hp)                                               addresses,
224                                                 sizeof(addresses)/sizeof(addresses[0]),
225                                                 addresses_count,
226                                                 result
227                                                );
228      if (result != TARGET_NATIVE_OK)
229      {      {
230        JCL_ThrowException(env, UNKNOWN_HOST_EXCEPTION, hostname);        JCL_ThrowException(env, UNKNOWN_HOST_EXCEPTION, (char*)hostname);
231        return (jobjectArray)NULL;        return (jobjectArray)NULL;
232      }      }
233    (*env)->ReleaseStringUTFChars(env, host, hostname);    (*env)->ReleaseStringUTFChars(env, host, hostname);
234    
   /* Figure out how many addresses there are and allocate a return array */  
   for (num_addrs = 0, i = 0; hp->h_addr_list[i] ; i++)  
     ++num_addrs;  
   
235    arr_class = (*env)->FindClass(env,"[B");    arr_class = (*env)->FindClass(env,"[B");
236    if (!arr_class)    if (!arr_class)
237      {      {
# Line 194  Java_java_net_InetAddress_getHostByName( Line 239  Java_java_net_InetAddress_getHostByName(
239        return (jobjectArray)NULL;        return (jobjectArray)NULL;
240      }      }
241    
242    addrs = (*env)->NewObjectArray(env, num_addrs, arr_class, 0);    addrs = (*env)->NewObjectArray(env, addresses_count, arr_class, 0);
243    if (!addrs)    if (!addrs)
244      {      {
245        JCL_ThrowException(env, UNKNOWN_HOST_EXCEPTION, "Internal Error");        JCL_ThrowException(env, UNKNOWN_HOST_EXCEPTION, "Internal Error");
# Line 202  Java_java_net_InetAddress_getHostByName( Line 247  Java_java_net_InetAddress_getHostByName(
247      }      }
248    
249    /* Now loop and copy in each address */    /* Now loop and copy in each address */
250    for (i = 0; i < num_addrs; i++)    for (i = 0; i < addresses_count; i++)
251      {      {
252        ret_octets = (*env)->NewByteArray(env, 4);        ret_octets = (*env)->NewByteArray(env, 4);
253        if (!ret_octets)              if (!ret_octets)      
# Line 213  Java_java_net_InetAddress_getHostByName( Line 258  Java_java_net_InetAddress_getHostByName(
258    
259        octets = (*env)->GetByteArrayElements(env, ret_octets, 0);        octets = (*env)->GetByteArrayElements(env, ret_octets, 0);
260    
261        ip = ntohl(*(int*)(hp->h_addr_list[i]));        TARGET_NATIVE_NETWORK_INT_TO_IPADDRESS_BYTES(addresses[i],
262        octets[0] = (ip & 0xFF000000) >> 24;                                                     octets[0],
263        octets[1] = (ip & 0x00FF0000) >> 16;                                                     octets[1],
264        octets[2] = (ip & 0x0000FF00) >> 8;                                                     octets[2],
265        octets[3] = (ip & 0x000000FF);                                                     octets[3]
266                                                      );
267    
268        (*env)->ReleaseByteArrayElements(env, ret_octets, octets, 0);        (*env)->ReleaseByteArrayElements(env, ret_octets, octets, 0);
269    
270        (*env)->SetObjectArrayElement(env, addrs, i, ret_octets);        (*env)->SetObjectArrayElement(env, addrs, i, ret_octets);
271      }      }
272    
273    return(addrs);    return(addrs);
274    #else /* not WITHOUT_NETWORK */
275      return (jobjectArray)NULL;
276    #endif /* not WITHOUT_NETWORK */
277  }  }
278    
279    /* end of file */

Legend:
Removed from v.1.5  
changed lines
  Added in v.1.6

savannah-hackers-public@gnu.org
ViewVC Help
Powered by ViewVC 1.1.26