home *** CD-ROM | disk | FTP | other *** search
/ Java 1996 August / Java - Summer 1996.iso / kaffe-0.2 / lib / net / java.net / java.net.InetAddress.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-02-19  |  2.7 KB  |  127 lines

  1. /*
  2.  * java.net.InetAddress.c
  3.  *
  4.  * Copyright (c) 1996 Systems Architecture Research Centre,
  5.  *           City University, London, UK.
  6.  *
  7.  * See the file "license.terms" for information on usage and redistribution
  8.  * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
  9.  *
  10.  * Written by Tim Wilkinson <tim@sarc.city.ac.uk>, February 1996.
  11.  */
  12.  
  13. #include <assert.h>
  14. #include <sys/types.h>
  15. #include <netinet/in.h>
  16. #include <sys/socket.h>
  17. #include <netdb.h>
  18. #include <native.h>
  19. #include "../../kaffe/itypes.h"
  20. #include "java.net.InetAddress.h"
  21. #include "nets.h"
  22.  
  23. /*
  24.  * Get localhost name.
  25.  *  Is this my name or "localhost" ?
  26.  */
  27. struct Hjava_lang_String*
  28. java_net_InetAddress_getLocalHostName()
  29. {
  30.     return (makeJavaString("localhost", 9));
  31. }
  32.  
  33. /*
  34.  * Provide one of my local address (I guess).
  35.  */
  36. void
  37. java_net_InetAddress_makeAnyLocalAddress(struct Hjava_net_InetAddress* this)
  38. {
  39.     unhand(this)->hostName = 0;
  40.     unhand(this)->address = htonl(INADDR_ANY);
  41.     unhand(this)->family = AF_INET;
  42. }
  43.  
  44. /*
  45.  * Convert a hostname to the primary host address.
  46.  */
  47. HArray* /* HArrayOfBytes */
  48. java_net_InetAddress_lookupHostAddr(struct Hjava_lang_String* str)
  49. {
  50.     char name[MAXHOSTNAME];
  51.     struct hostent* ent;
  52.     object* obj;
  53.  
  54.     javaString2CString(str, name, sizeof(name));
  55.  
  56.     ent = gethostbyname(name);
  57.     if (ent == 0) {
  58.         SignalError(0, "java.net.UnknownHostException", SYS_HERROR);
  59.     }
  60.  
  61.     /* Copy in the network address */
  62.     obj = (object*)alloc_array(4, TYPE_Byte);
  63.     assert(obj != 0);
  64.     *(long*)obj->data = *(long*)ent->h_addr_list[0];
  65.  
  66.     return (obj);
  67. }
  68.  
  69. /*
  70.  * Convert a hostname to an array of host addresses.
  71.  */
  72. HArray* /* HArrayOfArrayOfBytes */
  73. java_net_InetAddress_lookupAllHostAddr(struct Hjava_lang_String* str)
  74. {
  75.     char name[MAXHOSTNAME];
  76.     struct hostent* ent;
  77.     object* obj;
  78.     object* array;
  79.     int i;
  80.  
  81.     javaString2CString(str, name, sizeof(name));
  82.  
  83.     ent = gethostbyname(name);
  84.     if (ent == 0) {
  85.         SignalError(0, "java.net.UnknownHostException", SYS_HERROR);
  86.     }
  87.  
  88.     array = (object*)alloc_objectarray(ent->h_length, "[[B");
  89.     assert(array != 0);
  90.  
  91.     for (i = 0; i < ent->h_length; i++) {
  92.         /* Copy in the network address */
  93.         obj = (object*)alloc_array(4, TYPE_Byte);
  94.         assert(obj != 0);
  95.         *(long*)obj->data = *(long*)ent->h_addr_list[i];
  96.         ((object**)array->data)[i] = obj;
  97.     }
  98.  
  99.     return (obj);
  100. }
  101.  
  102. /*
  103.  * Convert a network order address into the hostname.
  104.  */
  105. struct Hjava_lang_String*
  106. java_net_InetAddress_getHostByAddr(long addr)
  107. {
  108.     struct hostent* ent;
  109.  
  110.     abort(); /* DONT KNOW THE ARGUMENTS */
  111.     ent = gethostbyaddr(0, 0, 0);
  112.     if (ent == 0) {
  113.         SignalError(0, "java.net.UnknownHostException", SYS_HERROR);
  114.     }
  115.  
  116.     return (makeJavaString((char*)ent->h_name, strlen(ent->h_name)));
  117. }
  118.  
  119. /*
  120.  * Return the inet address family.
  121.  */
  122. long
  123. java_net_InetAddress_getInetFamily()
  124. {
  125.     return (AF_INET);
  126. }
  127.