1999-05-26 19:00:06 +02:00
|
|
|
// natInetAddress.cc
|
1999-04-07 16:42:40 +02:00
|
|
|
|
2000-04-21 22:38:43 +02:00
|
|
|
/* Copyright (C) 1998, 1999, 2000 Free Software Foundation
|
1999-04-07 16:42:40 +02:00
|
|
|
|
|
|
|
This file is part of libgcj.
|
|
|
|
|
|
|
|
This software is copyrighted work licensed under the terms of the
|
|
|
|
Libgcj License. Please consult the file "LIBGCJ_LICENSE" for
|
|
|
|
details. */
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
|
2000-03-15 23:03:19 +01:00
|
|
|
#ifdef USE_WINSOCK
|
|
|
|
|
|
|
|
#include <windows.h>
|
|
|
|
#include <winsock.h>
|
|
|
|
|
|
|
|
#ifndef MAXHOSTNAMELEN
|
|
|
|
#define MAXHOSTNAMELEN 64
|
|
|
|
#endif /* MAXHOSTNAMELEN */
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
1999-04-07 16:42:40 +02:00
|
|
|
#ifdef HAVE_UNISTD_H
|
|
|
|
#include <unistd.h>
|
|
|
|
#endif
|
|
|
|
#include <string.h>
|
|
|
|
#include <errno.h>
|
|
|
|
|
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#ifdef HAVE_SYS_SOCKET_H
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_NETINET_IN_H
|
|
|
|
#include <netinet/in.h>
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_ARPA_INET_H
|
|
|
|
#include <arpa/inet.h>
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_NETDB_H
|
|
|
|
#include <netdb.h>
|
|
|
|
#endif
|
|
|
|
|
2000-03-15 23:03:19 +01:00
|
|
|
#endif /* USE_WINSOCK */
|
|
|
|
|
1999-09-11 00:03:10 +02:00
|
|
|
#include <gcj/cni.h>
|
1999-04-07 16:42:40 +02:00
|
|
|
#include <jvm.h>
|
|
|
|
#include <java/net/InetAddress.h>
|
|
|
|
#include <java/net/UnknownHostException.h>
|
|
|
|
#include <java/lang/SecurityException.h>
|
|
|
|
|
|
|
|
#if defined(HAVE_UNAME) && ! defined(HAVE_GETHOSTNAME)
|
|
|
|
#include <sys/utsname.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef HAVE_GETHOSTNAME_DECL
|
|
|
|
extern "C" int gethostname (char *name, int namelen);
|
|
|
|
#endif
|
|
|
|
|
1999-10-02 02:19:01 +02:00
|
|
|
#ifdef DISABLE_JAVA_NET
|
|
|
|
|
|
|
|
jbyteArray
|
|
|
|
java::net::InetAddress::aton (jstring)
|
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
JArray<java::net::InetAddress*> *
|
|
|
|
java::net::InetAddress::lookup (jstring, java::net::InetAddress *, jboolean)
|
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
jstring
|
|
|
|
java::net::InetAddress::getLocalHostname ()
|
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
#else /* DISABLE_JAVA_NET */
|
|
|
|
|
1999-04-07 16:42:40 +02:00
|
|
|
jbyteArray
|
|
|
|
java::net::InetAddress::aton (jstring host)
|
|
|
|
{
|
|
|
|
char *hostname;
|
|
|
|
char buf[100];
|
|
|
|
int len = JvGetStringUTFLength(host);
|
|
|
|
if (len < 100)
|
|
|
|
hostname = buf;
|
|
|
|
else
|
|
|
|
hostname = (char*) _Jv_AllocBytesChecked (len+1);
|
|
|
|
JvGetStringUTFRegion (host, 0, host->length(), hostname);
|
|
|
|
buf[len] = '\0';
|
|
|
|
char* bytes = NULL;
|
|
|
|
int blen = 0;
|
|
|
|
#ifdef HAVE_INET_ATON
|
|
|
|
struct in_addr laddr;
|
|
|
|
if (inet_aton (hostname, &laddr))
|
|
|
|
{
|
|
|
|
bytes = (char*) &laddr;
|
1999-06-11 03:38:08 +02:00
|
|
|
blen = 4;
|
1999-04-07 16:42:40 +02:00
|
|
|
}
|
|
|
|
#elif defined(HAVE_INET_ADDR)
|
1999-08-21 16:26:44 +02:00
|
|
|
#if ! HAVE_IN_ADDR_T
|
|
|
|
typedef jint in_addr_t;
|
|
|
|
#endif
|
1999-04-07 16:42:40 +02:00
|
|
|
in_addr_t laddr = inet_addr (hostname);
|
|
|
|
if (laddr != (in_addr_t)(-1))
|
|
|
|
{
|
|
|
|
bytes = (char*) &laddr;
|
1999-06-11 03:38:08 +02:00
|
|
|
blen = 4;
|
1999-04-07 16:42:40 +02:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_INET_PTON
|
|
|
|
char inet6_addr[16];
|
|
|
|
if (len == 0 && inet_pton (AF_INET6, hostname, inet6_addr) > 0)
|
|
|
|
{
|
|
|
|
bytes = inet6_addr;
|
1999-06-11 03:38:08 +02:00
|
|
|
blen = 16;
|
1999-04-07 16:42:40 +02:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
if (blen == 0)
|
|
|
|
return NULL;
|
1999-06-11 03:38:08 +02:00
|
|
|
jbyteArray result = JvNewByteArray (blen);
|
1999-04-07 16:42:40 +02:00
|
|
|
memcpy (elements (result), bytes, blen);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
JArray<java::net::InetAddress*> *
|
|
|
|
java::net::InetAddress::lookup (jstring host, java::net::InetAddress* iaddr,
|
|
|
|
jboolean all)
|
|
|
|
{
|
|
|
|
struct hostent *hptr = NULL;
|
|
|
|
#if defined (HAVE_GETHOSTBYNAME_R) || defined (HAVE_GETHOSTBYADDR_R)
|
|
|
|
struct hostent hent_r;
|
1999-08-01 02:14:32 +02:00
|
|
|
#if HAVE_STRUCT_HOSTENT_DATA
|
|
|
|
struct hostent_data fixed_buffer, *buffer_r = &fixed_buffer;
|
|
|
|
#else
|
1999-06-11 03:38:08 +02:00
|
|
|
#if defined (__GLIBC__)
|
|
|
|
// FIXME: in glibc, gethostbyname_r returns NETDB_INTERNAL to herr and
|
|
|
|
// ERANGE to errno if the buffer size is too small, rather than what is
|
|
|
|
// expected here. We work around this by setting a bigger buffer size and
|
|
|
|
// hoping that it is big enough.
|
1999-04-14 13:07:51 +02:00
|
|
|
char fixed_buffer[1024];
|
|
|
|
#else
|
1999-04-07 16:42:40 +02:00
|
|
|
char fixed_buffer[200];
|
1999-04-14 13:07:51 +02:00
|
|
|
#endif
|
1999-04-07 16:42:40 +02:00
|
|
|
char *buffer_r = fixed_buffer;
|
|
|
|
int size_r = sizeof (fixed_buffer);
|
1999-08-01 02:14:32 +02:00
|
|
|
#endif
|
1999-04-07 16:42:40 +02:00
|
|
|
#endif
|
|
|
|
|
|
|
|
if (host != NULL)
|
|
|
|
{
|
|
|
|
char *hostname;
|
|
|
|
char buf[100];
|
|
|
|
int len = JvGetStringUTFLength(host);
|
|
|
|
if (len < 100)
|
|
|
|
hostname = buf;
|
|
|
|
else
|
|
|
|
hostname = (char*) _Jv_AllocBytesChecked (len+1);
|
|
|
|
JvGetStringUTFRegion (host, 0, host->length(), hostname);
|
|
|
|
buf[len] = '\0';
|
|
|
|
#ifdef HAVE_GETHOSTBYNAME_R
|
1999-06-11 03:38:08 +02:00
|
|
|
while (true)
|
1999-04-07 16:42:40 +02:00
|
|
|
{
|
|
|
|
int ok;
|
1999-08-01 02:14:32 +02:00
|
|
|
#if HAVE_STRUCT_HOSTENT_DATA
|
|
|
|
ok = ! gethostbyname_r (hostname, &hent_r, buffer_r);
|
|
|
|
#else
|
|
|
|
int herr = 0;
|
1999-04-07 16:42:40 +02:00
|
|
|
#ifdef GETHOSTBYNAME_R_RETURNS_INT
|
|
|
|
ok = ! gethostbyname_r (hostname, &hent_r, buffer_r, size_r,
|
|
|
|
&hptr, &herr);
|
|
|
|
#else
|
|
|
|
hptr = gethostbyname_r (hostname, &hent_r, buffer_r, size_r, &herr);
|
|
|
|
ok = hptr != NULL;
|
|
|
|
#endif /* GETHOSTNAME_R_RETURNS_INT */
|
|
|
|
if (! ok && herr == ERANGE)
|
|
|
|
{
|
|
|
|
size_r *= 2;
|
|
|
|
buffer_r = (char *) _Jv_AllocBytesChecked (size_r);
|
|
|
|
}
|
1999-06-11 03:38:08 +02:00
|
|
|
else
|
1999-08-01 02:14:32 +02:00
|
|
|
#endif /* HAVE_STRUCT_HOSTENT_DATA */
|
1999-06-11 03:38:08 +02:00
|
|
|
break;
|
1999-04-07 16:42:40 +02:00
|
|
|
}
|
|
|
|
#else
|
|
|
|
// FIXME: this is insufficient if some other piece of code calls
|
|
|
|
// this gethostbyname.
|
|
|
|
JvSynchronize sync (java::net::InetAddress::localhostAddress);
|
|
|
|
hptr = gethostbyname (hostname);
|
|
|
|
#endif /* HAVE_GETHOSTBYNAME_R */
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
jbyteArray bytes = iaddr->address;
|
|
|
|
char *chars = (char*) elements (bytes);
|
|
|
|
int len = bytes->length;
|
|
|
|
int type;
|
|
|
|
char *val;
|
|
|
|
if (len == 4)
|
|
|
|
{
|
|
|
|
val = chars;
|
|
|
|
type = AF_INET;
|
|
|
|
}
|
|
|
|
#ifdef HAVE_INET6
|
|
|
|
else if (len == 16)
|
|
|
|
{
|
|
|
|
val = (char *) &chars;
|
1999-04-08 15:22:59 +02:00
|
|
|
type = AF_INET6;
|
1999-04-07 16:42:40 +02:00
|
|
|
}
|
|
|
|
#endif /* HAVE_INET6 */
|
|
|
|
else
|
|
|
|
JvFail ("unrecognized size");
|
|
|
|
|
|
|
|
#ifdef HAVE_GETHOSTBYADDR_R
|
1999-06-11 03:38:08 +02:00
|
|
|
while (true)
|
1999-04-07 16:42:40 +02:00
|
|
|
{
|
|
|
|
int ok;
|
1999-08-01 02:14:32 +02:00
|
|
|
#if HAVE_STRUCT_HOSTENT_DATA
|
|
|
|
ok = ! gethostbyaddr_r (val, len, type, &hent_r, buffer_r);
|
|
|
|
#else
|
|
|
|
int herr = 0;
|
1999-04-07 16:42:40 +02:00
|
|
|
#ifdef GETHOSTBYADDR_R_RETURNS_INT
|
|
|
|
ok = ! gethostbyaddr_r (val, len, type, &hent_r,
|
|
|
|
buffer_r, size_r, &hptr, &herr);
|
|
|
|
#else
|
|
|
|
hptr = gethostbyaddr_r (val, len, type, &hent_r,
|
|
|
|
buffer_r, size_r, &herr);
|
|
|
|
ok = hptr != NULL;
|
|
|
|
#endif /* GETHOSTBYADDR_R_RETURNS_INT */
|
|
|
|
if (! ok && herr == ERANGE)
|
|
|
|
{
|
|
|
|
size_r *= 2;
|
|
|
|
buffer_r = (char *) _Jv_AllocBytesChecked (size_r);
|
|
|
|
}
|
1999-06-11 03:38:08 +02:00
|
|
|
else
|
1999-08-01 02:14:32 +02:00
|
|
|
#endif /* HAVE_STRUCT_HOSTENT_DATA */
|
1999-06-11 03:38:08 +02:00
|
|
|
break;
|
1999-04-07 16:42:40 +02:00
|
|
|
}
|
|
|
|
#else /* HAVE_GETHOSTBYADDR_R */
|
|
|
|
// FIXME: this is insufficient if some other piece of code calls
|
|
|
|
// this gethostbyaddr.
|
|
|
|
JvSynchronize sync (java::net::InetAddress::localhostAddress);
|
|
|
|
hptr = gethostbyaddr (val, len, type);
|
|
|
|
#endif /* HAVE_GETHOSTBYADDR_R */
|
|
|
|
}
|
|
|
|
if (hptr != NULL)
|
1999-06-18 03:17:28 +02:00
|
|
|
{
|
|
|
|
if (!all)
|
1999-06-11 03:38:08 +02:00
|
|
|
host = JvNewStringUTF (hptr->h_name);
|
1999-04-07 16:42:40 +02:00
|
|
|
java::lang::SecurityException *ex = checkConnect (host);
|
|
|
|
if (ex != NULL)
|
|
|
|
{
|
|
|
|
if (iaddr == NULL || iaddr->address == NULL)
|
|
|
|
JvThrow (ex);
|
|
|
|
hptr = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (hptr == NULL)
|
|
|
|
{
|
|
|
|
if (iaddr != NULL && iaddr->address != NULL)
|
|
|
|
{
|
|
|
|
iaddr->hostname = iaddr->getHostAddress();
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
JvThrow (new java::net::UnknownHostException(host));
|
|
|
|
}
|
|
|
|
int count;
|
|
|
|
if (all)
|
|
|
|
{
|
|
|
|
char** ptr = hptr->h_addr_list;
|
|
|
|
count = 0;
|
|
|
|
while (*ptr++) count++;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
count = 1;
|
|
|
|
JArray<java::net::InetAddress*> *result;
|
|
|
|
java::net::InetAddress** iaddrs;
|
|
|
|
if (all)
|
|
|
|
{
|
|
|
|
result = java::net::InetAddress::allocArray (count);
|
|
|
|
iaddrs = elements (result);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
result = NULL;
|
|
|
|
iaddrs = &iaddr;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int i = 0; i < count; i++)
|
|
|
|
{
|
|
|
|
if (iaddrs[i] == NULL)
|
|
|
|
iaddrs[i] = new java::net::InetAddress (NULL, NULL);
|
1999-06-18 03:17:28 +02:00
|
|
|
if (iaddrs[i]->hostname == NULL)
|
|
|
|
iaddrs[i]->hostname = host;
|
1999-04-07 16:42:40 +02:00
|
|
|
if (iaddrs[i]->address == NULL)
|
|
|
|
{
|
|
|
|
char *bytes = hptr->h_addr_list[i];
|
1999-06-11 03:38:08 +02:00
|
|
|
iaddrs[i]->address = JvNewByteArray (hptr->h_length);
|
|
|
|
memcpy (elements (iaddrs[i]->address), bytes, hptr->h_length);
|
1999-04-07 16:42:40 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
jstring
|
|
|
|
java::net::InetAddress::getLocalHostname ()
|
|
|
|
{
|
|
|
|
char *chars;
|
|
|
|
#ifdef HAVE_GETHOSTNAME
|
|
|
|
char buffer[MAXHOSTNAMELEN];
|
|
|
|
if (gethostname (buffer, MAXHOSTNAMELEN))
|
|
|
|
return NULL;
|
|
|
|
chars = buffer;
|
|
|
|
#elif HAVE_UNAME
|
|
|
|
struct utsname stuff;
|
|
|
|
if (uname (&stuff) != 0)
|
1999-06-18 03:17:28 +02:00
|
|
|
return NULL;
|
1999-04-07 16:42:40 +02:00
|
|
|
chars = stuff.nodename;
|
|
|
|
#else
|
|
|
|
return NULL;
|
|
|
|
#endif
|
|
|
|
// It is admittedly non-optimal to convert the hostname to Unicode
|
|
|
|
// only to convert it back in getByName, but simplicity wins. Note
|
|
|
|
// that unless there is a SecurityManager, we only get called once
|
|
|
|
// anyway, thanks to the InetAddress.localhost cache.
|
|
|
|
return JvNewStringUTF (chars);
|
|
|
|
}
|
1999-10-02 02:19:01 +02:00
|
|
|
|
|
|
|
#endif /* DISABLE_JAVA_NET */
|