OSDN Git Service

2005-10-22 Erik Edelmann <eedelman@gcc.gnu.org>
[pf3gnuchains/gcc-fork.git] / libjava / java / net / natInetAddressPosix.cc
1 /* Copyright (C) 2003  Free Software Foundation
2
3    This file is part of libgcj.
4
5 This software is copyrighted work licensed under the terms of the
6 Libgcj License.  Please consult the file "LIBGCJ_LICENSE" for
7 details.  */
8
9 #include <config.h>
10
11 #ifdef HAVE_UNISTD_H
12 #include <unistd.h>
13 #endif
14 #include <string.h>
15 #include <errno.h>
16
17 #include <sys/param.h>
18 #include <sys/types.h>
19 #ifdef HAVE_SYS_SOCKET_H
20 #include <sys/socket.h>
21 #endif
22 #ifdef HAVE_NETINET_IN_H
23 #include <netinet/in.h>
24 #endif
25 #ifdef HAVE_ARPA_INET_H
26 #include <arpa/inet.h>
27 #endif
28 #ifdef HAVE_NETDB_H
29 #include <netdb.h>
30 #endif
31
32 #include <gcj/cni.h>
33 #include <jvm.h>
34 #include <java/net/InetAddress.h>
35 #include <java/net/UnknownHostException.h>
36 #include <java/lang/SecurityException.h>
37
38 #if defined(HAVE_UNAME) && ! defined(HAVE_GETHOSTNAME)
39 #include <sys/utsname.h>
40 #endif
41
42 #ifndef HAVE_GETHOSTNAME_DECL
43 extern "C" int gethostname (char *name, int namelen);
44 #endif
45
46 jbyteArray
47 java::net::InetAddress::aton (jstring host)
48 {
49   char *hostname;
50   char buf[100];
51   int len = JvGetStringUTFLength(host);
52   if (len < 100)
53     hostname = buf;
54   else
55     hostname = (char*) _Jv_AllocBytes (len+1);
56   JvGetStringUTFRegion (host, 0, host->length(), hostname);
57   buf[len] = '\0';
58   char* bytes = NULL;
59   int blen = 0;
60 #ifdef HAVE_INET_ATON
61   struct in_addr laddr;
62   if (inet_aton (hostname, &laddr))
63     {
64       bytes = (char*) &laddr;
65       blen = 4;
66     }
67 #elif defined(HAVE_INET_ADDR)
68 #if ! HAVE_IN_ADDR_T
69   typedef jint in_addr_t;
70 #endif
71   in_addr_t laddr = inet_addr (hostname);
72   if (laddr != (in_addr_t)(-1))
73     {
74       bytes = (char*) &laddr;
75       blen = 4;
76     }
77 #endif
78 #if defined (HAVE_INET_PTON) && defined (HAVE_INET6)
79   char inet6_addr[16];
80   if (len != 0 && inet_pton (AF_INET6, hostname, inet6_addr) > 0)
81     {
82       bytes = inet6_addr;
83       blen = 16;
84     }
85 #endif
86   if (blen == 0)
87     return NULL;
88   jbyteArray result = JvNewByteArray (blen);
89   memcpy (elements (result), bytes, blen);
90   return result;
91 }
92
93 jint
94 java::net::InetAddress::getFamily (jbyteArray bytes)
95 {
96   int len = bytes->length;
97   if (len == 4)
98     return AF_INET;
99 #ifdef HAVE_INET6
100   else if (len == 16)
101     return AF_INET6;
102 #endif /* HAVE_INET6 */
103   else
104     JvFail ("unrecognized size");
105 }
106
107
108 JArray<java::net::InetAddress*> *
109 java::net::InetAddress::lookup (jstring host, java::net::InetAddress* iaddr,
110                                 jboolean all)
111 {
112   struct hostent *hptr = NULL;
113 #if defined (HAVE_GETHOSTBYNAME_R) || defined (HAVE_GETHOSTBYADDR_R)
114   struct hostent hent_r;
115 #if HAVE_STRUCT_HOSTENT_DATA
116   struct hostent_data fixed_buffer, *buffer_r = &fixed_buffer;
117 #else
118 #if defined (__GLIBC__) 
119   // FIXME: in glibc, gethostbyname_r returns NETDB_INTERNAL to herr and
120   // ERANGE to errno if the buffer size is too small, rather than what is 
121   // expected here. We work around this by setting a bigger buffer size and 
122   // hoping that it is big enough.
123   char fixed_buffer[1024];
124 #else
125   char fixed_buffer[200];
126 #endif
127   char *buffer_r = fixed_buffer;
128   int size_r = sizeof (fixed_buffer);
129 #endif
130 #endif
131
132   if (host != NULL)
133     {
134       char *hostname;
135       char buf[100];
136       int len = JvGetStringUTFLength(host);
137       if (len < 100)
138         hostname = buf;
139       else
140         hostname = (char*) _Jv_AllocBytes (len+1);
141       JvGetStringUTFRegion (host, 0, host->length(), hostname);
142       buf[len] = '\0';
143 #ifdef HAVE_GETHOSTBYNAME_R
144       while (true)
145         {
146           int ok;
147 #if HAVE_STRUCT_HOSTENT_DATA
148           ok = ! gethostbyname_r (hostname, &hent_r, buffer_r);
149 #else
150           int herr = 0;
151 #ifdef GETHOSTBYNAME_R_RETURNS_INT
152           ok = ! gethostbyname_r (hostname, &hent_r, buffer_r, size_r,
153                                   &hptr, &herr);
154 #else
155           hptr = gethostbyname_r (hostname, &hent_r, buffer_r, size_r, &herr);
156           ok = hptr != NULL;
157 #endif /* GETHOSTNAME_R_RETURNS_INT */
158           if (! ok && herr == ERANGE)
159             {
160               size_r *= 2;
161               buffer_r = (char *) _Jv_AllocBytes (size_r);
162             }
163           else
164 #endif /* HAVE_STRUCT_HOSTENT_DATA */
165             break;
166         }
167 #else
168       // FIXME: this is insufficient if some other piece of code calls
169       // this gethostbyname.
170       JvSynchronize sync (java::net::InetAddress::loopbackAddress);
171       hptr = gethostbyname (hostname);
172 #endif /* HAVE_GETHOSTBYNAME_R */
173     }
174   else
175     {
176       jbyteArray bytes = iaddr->addr;
177       char *chars = (char*) elements (bytes);
178       int len = bytes->length;
179       int type;
180       char *val;
181       if (len == 4)
182         {
183           val = chars;
184           type = iaddr->family = AF_INET;
185         }
186 #ifdef HAVE_INET6
187       else if (len == 16)
188         {
189           val = (char *) &chars;
190           type = iaddr->family = AF_INET6;
191         }
192 #endif /* HAVE_INET6 */
193       else
194         JvFail ("unrecognized size");
195
196 #ifdef HAVE_GETHOSTBYADDR_R
197       while (true)
198         {
199           int ok;
200 #if HAVE_STRUCT_HOSTENT_DATA
201           ok = ! gethostbyaddr_r (val, len, type, &hent_r, buffer_r);
202 #else
203           int herr = 0;
204 #ifdef GETHOSTBYADDR_R_RETURNS_INT
205           ok = ! gethostbyaddr_r (val, len, type, &hent_r,
206                                   buffer_r, size_r, &hptr, &herr);
207 #else
208           hptr = gethostbyaddr_r (val, len, type, &hent_r,
209                                   buffer_r, size_r, &herr);
210           ok = hptr != NULL;
211 #endif /* GETHOSTBYADDR_R_RETURNS_INT */
212           if (! ok && herr == ERANGE)
213             {
214               size_r *= 2;
215               buffer_r = (char *) _Jv_AllocBytes (size_r);
216             }
217           else 
218 #endif /* HAVE_STRUCT_HOSTENT_DATA */
219             break;
220         }
221 #else /* HAVE_GETHOSTBYADDR_R */
222       // FIXME: this is insufficient if some other piece of code calls
223       // this gethostbyaddr.
224       JvSynchronize sync (java::net::InetAddress::loopbackAddress);
225       hptr = gethostbyaddr (val, len, type);
226 #endif /* HAVE_GETHOSTBYADDR_R */
227     }
228   if (hptr != NULL)
229     {
230       if (!all)
231         host = JvNewStringUTF (hptr->h_name);
232     }
233   if (hptr == NULL)
234     {
235       if (iaddr != NULL && iaddr->addr != NULL)
236         {
237           iaddr->hostName = iaddr->getHostAddress();
238           return NULL;
239         }
240       else
241         throw new java::net::UnknownHostException(host);
242     }
243   int count;
244   if (all)
245     {
246       char** ptr = hptr->h_addr_list;
247       count = 0;
248       while (*ptr++)  count++;
249     }
250   else
251     count = 1;
252   JArray<java::net::InetAddress*> *result;
253   java::net::InetAddress** iaddrs;
254   if (all)
255     {
256       result = java::net::InetAddress::allocArray (count);
257       iaddrs = elements (result);
258     }
259   else
260     {
261       result = NULL;
262       iaddrs = &iaddr;
263     }
264
265   for (int i = 0;  i < count;  i++)
266     {
267       if (iaddrs[i] == NULL)
268         iaddrs[i] = new java::net::InetAddress (NULL, NULL);
269       if (iaddrs[i]->hostName == NULL)
270         iaddrs[i]->hostName = host;
271       if (iaddrs[i]->addr == NULL)
272         {
273           char *bytes = hptr->h_addr_list[i];
274           iaddrs[i]->addr = JvNewByteArray (hptr->h_length);
275           iaddrs[i]->family = getFamily (iaddrs[i]->addr);
276           memcpy (elements (iaddrs[i]->addr), bytes, hptr->h_length);
277         }
278     }
279   return result;
280 }
281
282 jstring
283 java::net::InetAddress::getLocalHostname ()
284 {
285   char *chars;
286 #ifdef HAVE_GETHOSTNAME
287   char buffer[MAXHOSTNAMELEN];
288   if (gethostname (buffer, MAXHOSTNAMELEN))
289     return NULL;
290   chars = buffer;
291 #elif HAVE_UNAME
292   struct utsname stuff;
293   if (uname (&stuff) != 0)
294     return NULL;
295   chars = stuff.nodename;
296 #else
297   return NULL;
298 #endif
299   // It is admittedly non-optimal to convert the hostname to Unicode
300   // only to convert it back in getByName, but simplicity wins.  Note
301   // that unless there is a SecurityManager, we only get called once
302   // anyway, thanks to the InetAddress.localhost cache.
303   return JvNewStringUTF (chars);
304 }