1 // natClass.cc - Implementation of java.lang.Class native methods.
3 /* Copyright (C) 1998, 1999 Cygnus Solutions
5 This file is part of libgcj.
7 This software is copyrighted work licensed under the terms of the
8 Libgcj License. Please consult the file "LIBGCJ_LICENSE" for
19 #include <sys/param.h>
20 #include <sys/types.h>
21 #ifdef HAVE_SYS_SOCKET_H
22 #include <sys/socket.h>
24 #ifdef HAVE_NETINET_IN_H
25 #include <netinet/in.h>
27 #ifdef HAVE_ARPA_INET_H
28 #include <arpa/inet.h>
36 #include <java/net/InetAddress.h>
37 #include <java/net/UnknownHostException.h>
38 #include <java/lang/SecurityException.h>
40 #if defined(HAVE_UNAME) && ! defined(HAVE_GETHOSTNAME)
41 #include <sys/utsname.h>
44 #ifndef HAVE_GETHOSTNAME_DECL
45 extern "C" int gethostname (char *name, int namelen);
49 java::net::InetAddress::aton (jstring host)
53 int len = JvGetStringUTFLength(host);
57 hostname = (char*) _Jv_AllocBytesChecked (len+1);
58 JvGetStringUTFRegion (host, 0, host->length(), hostname);
64 if (inet_aton (hostname, &laddr))
66 bytes = (char*) &laddr;
69 #elif defined(HAVE_INET_ADDR)
70 in_addr_t laddr = inet_addr (hostname);
71 if (laddr != (in_addr_t)(-1))
73 bytes = (char*) &laddr;
79 if (len == 0 && inet_pton (AF_INET6, hostname, inet6_addr) > 0)
87 jbyteArray result = JvNewByteArray (len);
88 memcpy (elements (result), bytes, blen);
93 JArray<java::net::InetAddress*> *
94 java::net::InetAddress::lookup (jstring host, java::net::InetAddress* iaddr,
97 struct hostent *hptr = NULL;
98 #if defined (HAVE_GETHOSTBYNAME_R) || defined (HAVE_GETHOSTBYADDR_R)
99 struct hostent hent_r;
100 char fixed_buffer[200];
101 char *buffer_r = fixed_buffer;
102 int size_r = sizeof (fixed_buffer);
109 int len = JvGetStringUTFLength(host);
113 hostname = (char*) _Jv_AllocBytesChecked (len+1);
114 JvGetStringUTFRegion (host, 0, host->length(), hostname);
116 #ifdef HAVE_GETHOSTBYNAME_R
118 while (hptr == NULL && herr == ERANGE)
121 #ifdef GETHOSTBYNAME_R_RETURNS_INT
122 ok = ! gethostbyname_r (hostname, &hent_r, buffer_r, size_r,
125 hptr = gethostbyname_r (hostname, &hent_r, buffer_r, size_r, &herr);
127 #endif /* GETHOSTNAME_R_RETURNS_INT */
128 if (! ok && herr == ERANGE)
131 buffer_r = (char *) _Jv_AllocBytesChecked (size_r);
135 // FIXME: this is insufficient if some other piece of code calls
136 // this gethostbyname.
137 JvSynchronize sync (java::net::InetAddress::localhostAddress);
138 hptr = gethostbyname (hostname);
139 #endif /* HAVE_GETHOSTBYNAME_R */
143 jbyteArray bytes = iaddr->address;
144 char *chars = (char*) elements (bytes);
145 int len = bytes->length;
156 val = (char *) &chars;
159 #endif /* HAVE_INET6 */
161 JvFail ("unrecognized size");
163 #ifdef HAVE_GETHOSTBYADDR_R
165 while (hptr == NULL && herr == ERANGE)
168 #ifdef GETHOSTBYADDR_R_RETURNS_INT
169 ok = ! gethostbyaddr_r (val, len, type, &hent_r,
170 buffer_r, size_r, &hptr, &herr);
172 hptr = gethostbyaddr_r (val, len, type, &hent_r,
173 buffer_r, size_r, &herr);
175 #endif /* GETHOSTBYADDR_R_RETURNS_INT */
176 if (! ok && herr == ERANGE)
179 buffer_r = (char *) _Jv_AllocBytesChecked (size_r);
182 #else /* HAVE_GETHOSTBYADDR_R */
183 // FIXME: this is insufficient if some other piece of code calls
184 // this gethostbyaddr.
185 JvSynchronize sync (java::net::InetAddress::localhostAddress);
186 hptr = gethostbyaddr (val, len, type);
187 #endif /* HAVE_GETHOSTBYADDR_R */
191 host = JvNewStringUTF (hptr->h_name);
192 java::lang::SecurityException *ex = checkConnect (host);
195 if (iaddr == NULL || iaddr->address == NULL)
202 if (iaddr != NULL && iaddr->address != NULL)
204 iaddr->hostname = iaddr->getHostAddress();
208 JvThrow (new java::net::UnknownHostException(host));
213 char** ptr = hptr->h_addr_list;
215 while (*ptr++) count++;
219 JArray<java::net::InetAddress*> *result;
220 java::net::InetAddress** iaddrs;
223 result = java::net::InetAddress::allocArray (count);
224 iaddrs = elements (result);
232 for (int i = 0; i < count; i++)
234 if (iaddrs[i] == NULL)
235 iaddrs[i] = new java::net::InetAddress (NULL, NULL);
237 iaddrs[0]->hostname = host;
238 if (iaddrs[i]->address == NULL)
240 char *bytes = hptr->h_addr_list[i];
241 iaddr->address = JvNewByteArray (hptr->h_length);
242 memcpy (elements (iaddr->address), bytes, hptr->h_length);
249 java::net::InetAddress::getLocalHostname ()
252 #ifdef HAVE_GETHOSTNAME
253 char buffer[MAXHOSTNAMELEN];
254 if (gethostname (buffer, MAXHOSTNAMELEN))
258 struct utsname stuff;
259 if (uname (&stuff) != 0)
261 chars = stuff.nodename;
265 // It is admittedly non-optimal to convert the hostname to Unicode
266 // only to convert it back in getByName, but simplicity wins. Note
267 // that unless there is a SecurityManager, we only get called once
268 // anyway, thanks to the InetAddress.localhost cache.
269 return JvNewStringUTF (chars);