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
14 #include <java/net/InetAddress.h>
15 #include <java/net/UnknownHostException.h>
16 #include <java/lang/SecurityException.h>
19 java::net::InetAddress::aton (jstring host
)
21 JV_TEMP_UTF_STRING (hostname
, host
);
24 unsigned long laddr
= inet_addr (hostname
);
25 if (laddr
!= INADDR_NONE
)
27 bytes
= (char*) &laddr
;
32 jbyteArray result
= JvNewByteArray (blen
);
33 memcpy (elements (result
), bytes
, blen
);
38 java::net::InetAddress::getFamily (jbyteArray bytes
)
40 int len
= bytes
->length
;
46 #endif /* HAVE_INET6 */
48 JvFail ("unrecognized size");
52 JArray
<java::net::InetAddress
*> *
53 java::net::InetAddress::implLookup (jstring host
,
54 java::net::InetAddress
* iaddr
,
57 struct hostent
*hptr
= 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
);
69 jbyteArray bytes
= iaddr
->addr
;
70 char *chars
= (char*) elements (bytes
);
71 int len
= bytes
->length
;
77 type
= iaddr
->family
= AF_INET
;
82 val
= (char *) &chars
;
83 type
= iaddr
->family
= AF_INET6
;
85 #endif /* HAVE_INET6 */
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
);
97 host
= JvNewStringUTF (hptr
->h_name
);
98 java::lang::SecurityException
*ex
= checkConnect (host
);
101 if (iaddr
== NULL
|| iaddr
->addr
== NULL
)
108 if (iaddr
!= NULL
&& iaddr
->addr
!= NULL
)
110 iaddr
->hostName
= iaddr
->getHostAddress();
114 throw new java::net::UnknownHostException(host
);
120 char** ptr
= hptr
->h_addr_list
;
122 while (*ptr
++) count
++;
127 JArray
<java::net::InetAddress
*> *result
;
128 java::net::InetAddress
** iaddrs
;
131 result
= java::net::InetAddress::allocArray (count
);
132 iaddrs
= elements (result
);
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
);
159 java::net::InetAddress::getLocalHostname ()
162 if (gethostname (buffer
, sizeof(buffer
)))
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
);