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::lookup (jstring host
, java::net::InetAddress
* iaddr
,
56 struct hostent
*hptr
= 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
);
68 jbyteArray bytes
= iaddr
->addr
;
69 char *chars
= (char*) elements (bytes
);
70 int len
= bytes
->length
;
76 type
= iaddr
->family
= AF_INET
;
81 val
= (char *) &chars
;
82 type
= iaddr
->family
= AF_INET6
;
84 #endif /* HAVE_INET6 */
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
);
96 host
= JvNewStringUTF (hptr
->h_name
);
97 java::lang::SecurityException
*ex
= checkConnect (host
);
100 if (iaddr
== NULL
|| iaddr
->addr
== NULL
)
107 if (iaddr
!= NULL
&& iaddr
->addr
!= NULL
)
109 iaddr
->hostName
= iaddr
->getHostAddress();
113 throw new java::net::UnknownHostException(host
);
119 char** ptr
= hptr
->h_addr_list
;
121 while (*ptr
++) count
++;
126 JArray
<java::net::InetAddress
*> *result
;
127 java::net::InetAddress
** iaddrs
;
130 result
= java::net::InetAddress::allocArray (count
);
131 iaddrs
= elements (result
);
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
);
158 java::net::InetAddress::getLocalHostname ()
161 if (gethostname (buffer
, sizeof(buffer
)))
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
);