2003-12-26 Guilhem Lavaux <guilhem@kaffe.org>
[official-gcc.git] / libjava / java / net / natInetAddressWin32.cc
blob42c7d7db9e81c4b8ec086f4cd19bf191a2eede56
1 /* Copyright (C) 2003 Free Software Foundation
3 This file is part of libgcj.
5 This software is copyrighted work licensed under the terms of the
6 Libgcj License. Please consult the file "LIBGCJ_LICENSE" for
7 details. */
9 #include <config.h>
10 #include <platform.h>
12 #undef STRICT
14 #include <java/net/InetAddress.h>
15 #include <java/net/UnknownHostException.h>
16 #include <java/lang/SecurityException.h>
18 jbyteArray
19 java::net::InetAddress::aton (jstring host)
21 JV_TEMP_UTF_STRING (hostname, host);
22 char* bytes = NULL;
23 int blen = 0;
24 unsigned long laddr = inet_addr (hostname);
25 if (laddr != INADDR_NONE)
27 bytes = (char*) &laddr;
28 blen = 4;
30 if (blen == 0)
31 return NULL;
32 jbyteArray result = JvNewByteArray (blen);
33 memcpy (elements (result), bytes, blen);
34 return result;
37 jint
38 java::net::InetAddress::getFamily (jbyteArray bytes)
40 int len = bytes->length;
41 if (len == 4)
42 return AF_INET;
43 #ifdef HAVE_INET6
44 else if (len == 16)
45 return AF_INET6;
46 #endif /* HAVE_INET6 */
47 else
48 JvFail ("unrecognized size");
52 JArray<java::net::InetAddress*> *
53 java::net::InetAddress::lookup (jstring host, java::net::InetAddress* iaddr,
54 jboolean all)
56 struct hostent *hptr = NULL;
57 if (host != NULL)
59 JV_TEMP_UTF_STRING (hostname, host);
61 // FIXME: this is insufficient if some other piece of code calls
62 // this gethostbyname.
63 JvSynchronize sync (java::net::InetAddress::localhostAddress);
64 hptr = gethostbyname (hostname);
66 else
68 jbyteArray bytes = iaddr->addr;
69 char *chars = (char*) elements (bytes);
70 int len = bytes->length;
71 int type;
72 char *val;
73 if (len == 4)
75 val = chars;
76 type = iaddr->family = AF_INET;
78 #ifdef HAVE_INET6
79 else if (len == 16)
81 val = (char *) &chars;
82 type = iaddr->family = AF_INET6;
84 #endif /* HAVE_INET6 */
85 else
86 JvFail ("unrecognized size");
88 // FIXME: this is insufficient if some other piece of code calls
89 // this gethostbyaddr.
90 JvSynchronize sync (java::net::InetAddress::localhostAddress);
91 hptr = gethostbyaddr (val, len, type);
93 if (hptr != NULL)
95 if (!all)
96 host = JvNewStringUTF (hptr->h_name);
97 java::lang::SecurityException *ex = checkConnect (host);
98 if (ex != NULL)
100 if (iaddr == NULL || iaddr->addr == NULL)
101 throw ex;
102 hptr = NULL;
105 if (hptr == NULL)
107 if (iaddr != NULL && iaddr->addr != NULL)
109 iaddr->hostName = iaddr->getHostAddress();
110 return NULL;
112 else
113 throw new java::net::UnknownHostException(host);
116 int count;
117 if (all)
119 char** ptr = hptr->h_addr_list;
120 count = 0;
121 while (*ptr++) count++;
123 else
124 count = 1;
126 JArray<java::net::InetAddress*> *result;
127 java::net::InetAddress** iaddrs;
128 if (all)
130 result = java::net::InetAddress::allocArray (count);
131 iaddrs = elements (result);
133 else
135 result = NULL;
136 iaddrs = &iaddr;
139 for (int i = 0; i < count; i++)
141 if (iaddrs[i] == NULL)
142 iaddrs[i] = new java::net::InetAddress (NULL, NULL);
143 if (iaddrs[i]->hostName == NULL)
144 iaddrs[i]->hostName = host;
145 if (iaddrs[i]->addr == NULL)
147 char *bytes = hptr->h_addr_list[i];
148 iaddrs[i]->addr = JvNewByteArray (hptr->h_length);
149 iaddrs[i]->family = getFamily (iaddrs[i]->addr);
150 memcpy (elements (iaddrs[i]->addr), bytes, hptr->h_length);
154 return result;
157 jstring
158 java::net::InetAddress::getLocalHostname ()
160 char buffer[400];
161 if (gethostname (buffer, sizeof(buffer)))
162 return NULL;
163 // It is admittedly non-optimal to convert the hostname to Unicode
164 // only to convert it back in getByName, but simplicity wins. Note
165 // that unless there is a SecurityManager, we only get called once
166 // anyway, thanks to the InetAddress.localhost cache.
167 return JvNewStringUTF (buffer);