2003-11-28 Michael Koch <konqueror@gmx.de>
[official-gcc.git] / libjava / java / net / natInetAddressWin32.cc
blobd32f45c3c6d5a0064fc04e1f5c79955d85224ffa
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::implLookup (jstring host,
54 java::net::InetAddress* iaddr,
55 jboolean all)
57 struct hostent *hptr = NULL;
58 if (host != NULL)
60 JV_TEMP_UTF_STRING (hostname, host);
62 // FIXME: this is insufficient if some other piece of code calls
63 // this gethostbyname.
64 JvSynchronize sync (java::net::InetAddress::localhostAddress);
65 hptr = gethostbyname (hostname);
67 else
69 jbyteArray bytes = iaddr->addr;
70 char *chars = (char*) elements (bytes);
71 int len = bytes->length;
72 int type;
73 char *val;
74 if (len == 4)
76 val = chars;
77 type = iaddr->family = AF_INET;
79 #ifdef HAVE_INET6
80 else if (len == 16)
82 val = (char *) &chars;
83 type = iaddr->family = AF_INET6;
85 #endif /* HAVE_INET6 */
86 else
87 JvFail ("unrecognized size");
89 // FIXME: this is insufficient if some other piece of code calls
90 // this gethostbyaddr.
91 JvSynchronize sync (java::net::InetAddress::localhostAddress);
92 hptr = gethostbyaddr (val, len, type);
94 if (hptr != NULL)
96 if (!all)
97 host = JvNewStringUTF (hptr->h_name);
98 java::lang::SecurityException *ex = checkConnect (host);
99 if (ex != NULL)
101 if (iaddr == NULL || iaddr->addr == NULL)
102 throw ex;
103 hptr = NULL;
106 if (hptr == NULL)
108 if (iaddr != NULL && iaddr->addr != NULL)
110 iaddr->hostName = iaddr->getHostAddress();
111 return NULL;
113 else
114 throw new java::net::UnknownHostException(host);
117 int count;
118 if (all)
120 char** ptr = hptr->h_addr_list;
121 count = 0;
122 while (*ptr++) count++;
124 else
125 count = 1;
127 JArray<java::net::InetAddress*> *result;
128 java::net::InetAddress** iaddrs;
129 if (all)
131 result = java::net::InetAddress::allocArray (count);
132 iaddrs = elements (result);
134 else
136 result = NULL;
137 iaddrs = &iaddr;
140 for (int i = 0; i < count; i++)
142 if (iaddrs[i] == NULL)
143 iaddrs[i] = new java::net::InetAddress (NULL, NULL);
144 if (iaddrs[i]->hostName == NULL)
145 iaddrs[i]->hostName = host;
146 if (iaddrs[i]->addr == NULL)
148 char *bytes = hptr->h_addr_list[i];
149 iaddrs[i]->addr = JvNewByteArray (hptr->h_length);
150 iaddrs[i]->family = getFamily (iaddrs[i]->addr);
151 memcpy (elements (iaddrs[i]->addr), bytes, hptr->h_length);
155 return result;
158 jstring
159 java::net::InetAddress::getLocalHostname ()
161 char buffer[400];
162 if (gethostname (buffer, sizeof(buffer)))
163 return NULL;
164 // It is admittedly non-optimal to convert the hostname to Unicode
165 // only to convert it back in getByName, but simplicity wins. Note
166 // that unless there is a SecurityManager, we only get called once
167 // anyway, thanks to the InetAddress.localhost cache.
168 return JvNewStringUTF (buffer);