/*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
#include <errno.h>
#include <netinet/in_systm.h>
#include <netdb.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#ifdef _ALLBSD_SOURCE
#include <unistd.h>
#endif
#include "jvm.h"
#include "jni_util.h"
#include "net_util.h"
#include "java_net_Inet4AddressImpl.h"
#endif
#if defined(_ALLBSD_SOURCE) && !defined(HAS_GLIBC_GETHOSTBY_R)
/* Use getaddrinfo(3), which is thread safe */
/************************************************************************
* Inet4AddressImpl
*/
/*
* Class: java_net_Inet4AddressImpl
* Method: getLocalHostName
*/
hostname[0] = '\0';
/* Something went wrong, maybe networking is not setup? */
} else {
int error;
if (error == 0) {
/* host is known to name service */
NULL,
0,
/* if getnameinfo fails hostname is still the value
from gethostname */
}
}
}
static int initialized = 0;
/*
* Find an internet address for a given hostname. Note that this
* code only works for addresses of type INET. The translation
* of %d.%d.%d.%d to an address (int) occurs in java now, so the
* String "host" shouldn't *ever* be a %d.%d.%d.%d string
*
* Class: java_net_Inet4AddressImpl
* Method: lookupAllHostAddr
*/
const char *hostname;
int retLen = 0;
int error=0;
if (!initialized) {
initialized = 1;
}
return 0;
}
/*
* Workaround for Solaris bug 4160367 - if a hostname contains a
* white space then 0.0.0.0 is returned
*/
(char *)hostname);
return NULL;
}
if (error) {
/* report error */
(char *)hostname);
return NULL;
} else {
int i = 0;
int skip = 0;
skip = 1;
break;
}
}
if (!skip) {
if (!next) {
goto cleanupAndReturn;
}
} else {
}
i++;
}
}
retLen = i;
i = 0;
goto cleanupAndReturn;
}
/* we may have memory to free at the end of this */
goto cleanupAndReturn;
}
/* We need 4 bytes to store ipv4 address; */
/* we may have memory to free at the end of this */
goto cleanupAndReturn;
}
setInetAddress_addr(env, iaObj, ntohl(((struct sockaddr_in*)(iterator->ai_addr))->sin_addr.s_addr));
i++;
}
}
{
}
}
return ret;
}
/*
* Class: java_net_Inet4AddressImpl
* Method: getHostByAddr
*/
int error = 0;
int len = 0;
/*
* For IPv4 addresses construct a sockaddr_in structure.
*/
if (!error) {
}
}
return ret;
}
#else /* defined(_ALLBSD_SOURCE) && !defined(HAS_GLIBC_GETHOSTBY_R) */
/* the initial size of our hostent buffers */
/************************************************************************
* Inet4AddressImpl
*/
/*
* Class: java_net_Inet4AddressImpl
* Method: getLocalHostName
*/
hostname[0] = '\0';
/* Something went wrong, maybe networking is not setup? */
} else {
#ifdef __linux__
/* On Linux gethostname() says "host.domain.sun.com". On
* Solaris gethostname() says "host", so extra work is needed.
*/
#else
/* Solaris doesn't want to give us a fully qualified domain name.
* We do a reverse lookup to try and get one. This works
* if DNS occurs before NIS in /etc/resolv.conf, but fails
* if NIS comes first (it still gets only a partial name).
* We use thread-safe system calls.
*/
#endif /* __linux__ */
// these buffers must be pointer-aligned so they are declared
// with pointer type
int h_error=0;
// ensure null-terminated
#ifdef HAS_GLIBC_GETHOSTBY_R
#else
#endif
if (hp) {
#ifdef HAS_GLIBC_GETHOSTBY_R
#else
#endif
if (hp) {
/*
* If gethostbyaddr_r() found a fully qualified host name,
* returns that name. Otherwise, returns the hostname
* found by gethostname().
*/
}
}
}
}
static int initialized = 0;
/*
* Find an internet address for a given hostname. Note that this
* code only works for addresses of type INET. The translation
* of %d.%d.%d.%d to an address (int) occurs in java now, so the
* String "host" shouldn't *ever* be a %d.%d.%d.%d string
*
* Class: java_net_Inet4AddressImpl
* Method: lookupAllHostAddr
*/
const char *hostname;
// this buffer must be pointer-aligned so is declared
// with pointer type
/* temporary buffer, on the off chance we need to expand */
int h_error=0;
if (!initialized) {
initialized = 1;
}
return 0;
}
#ifdef __solaris__
/*
* Workaround for Solaris bug 4160367 - if a hostname contains a
* white space then 0.0.0.0 is returned
*/
(char *)hostname);
return NULL;
}
#endif
/* Try once, with our static buffer. */
#ifdef HAS_GLIBC_GETHOSTBY_R
#else
#endif
/* With the re-entrant system calls, it's possible that the buffer
* we pass to it is not large enough to hold an exceptionally
* large DNS entry. This is signaled by errno->ERANGE. We try once
* more, with a very big size.
*/
#ifdef HAS_GLIBC_GETHOSTBY_R
#else
&h_error);
#endif
}
}
int i = 0;
i++;
addrp++;
}
/* we may have memory to free at the end of this */
goto cleanupAndReturn;
}
i = 0;
while (*addrp) {
goto cleanupAndReturn;
}
addrp++;
i++;
}
} else {
(char *)hostname);
}
}
return ret;
}
/*
* Class: java_net_Inet4AddressImpl
* Method: getHostByAddr
*/
// this buffer must be pointer-aligned so is declared
// with pointer type
int h_error = 0;
/*
* We are careful here to use the reentrant version of
* gethostbyname because at the Java level this routine is not
* protected by any synchronization.
*
* Still keeping the reentrant platform dependent calls temporarily
* We should probably conform to one interface later.
*
*/
#ifdef HAS_GLIBC_GETHOSTBY_R
#else
#endif
/* With the re-entrant system calls, it's possible that the buffer
* we pass to it is not large enough to hold an exceptionally
* large DNS entry. This is signaled by errno->ERANGE. We try once
* more, with a very big size.
*/
#ifdef HAS_GLIBC_GETHOSTBY_R
#else
#endif
} else {
}
}
} else {
}
if (tmp) {
}
return ret;
}
#endif /* _ALLBSD_SOURCE */
flags |= O_NONBLOCK; \
}
/**
* ping implementation.
* Send a ICMP_ECHO_REQUEST packet every second until either the timeout
* expires or a answer is received.
* Returns true is an ECHO_REPLY is received, otherwise, false.
*/
static jboolean
/* icmp_id is a 16 bit data type, therefore down cast the pid */
/*
* sets the ttl (max number of hops)
*/
if (ttl > 0) {
}
/*
* a specific interface was specified, so let's bind the socket
* to that interface to ensure the requests are sent only through it.
*/
return JNI_FALSE;
}
}
/*
* Make the socket non blocking so we can use select
*/
do {
/*
* create the ICMP request
*/
seq++;
icmp->icmp_cksum = 0;
/*
* send it
*/
sizeof(struct sockaddr));
if (n < 0 && errno != EINPROGRESS ) {
#ifdef __linux__
/*
* On some Linuxes, when bound to the loopback interface, sendto
* will fail and errno will be set to EINVAL or EHOSTUNREACH.
* When that happens, don't throw an exception, just return false.
*/
#endif /*__linux__ */
return JNI_FALSE;
}
do {
if (tmout2 >= 0) {
/*
* We did receive something, but is it what we were expecting?
* I.E.: A ICMP_ECHOREPLY packet with the proper PID.
*/
return JNI_TRUE;
}
return JNI_TRUE;
}
}
}
} while (tmout2 > 0);
timeout -= 1000;
} while (timeout >0);
return JNI_FALSE;
}
/*
* Class: java_net_Inet4AddressImpl
* Method: isReachable0
* Signature: ([bI[bI)Z
*/
int len = 0;
int sz;
if (sz != 4) {
return JNI_FALSE;
}
/*
* If a network interface was specified, let's create the address
* for it.
*/
}
/*
* Let's try to create a RAW socket to send ICMP packets
* This usually requires "root" privileges, so it's likely to fail.
*/
if (fd != -1) {
/*
* It didn't fail, so we can use ICMP_ECHO requests.
*/
}
/*
* Can't create a raw socket, so let's try a TCP socket
*/
if (fd == JVM_IO_ERR) {
/* note: if you run out of fds, you may not be able to load
* the exception class, and get a NoClassDefFoundError
* instead.
*/
return JNI_FALSE;
}
if (ttl > 0) {
}
/*
* A network interface was specified, so let's bind to it.
*/
return JNI_FALSE;
}
}
/*
*/
/* no need to use NET_Connect as non-blocking */
/**
* connection established or refused immediately, either way it means
* we were able to reach the host!
*/
return JNI_TRUE;
} else {
int optlen;
switch (errno) {
case ENETUNREACH: /* Network Unreachable */
case EAFNOSUPPORT: /* Address Family not supported */
case EADDRNOTAVAIL: /* address is not available on the remote machine */
#ifdef __linux__
case EINVAL:
case EHOSTUNREACH:
/*
* On some Linuxes, when bound to the loopback interface, connect
* will fail and errno will be set to EINVAL or EHOSTUNREACH.
* When that happens, don't throw an exception, just return false.
*/
#endif /* __linux__ */
return JNI_FALSE;
}
if (errno != EINPROGRESS) {
"connect failed");
return JNI_FALSE;
}
if (timeout >= 0) {
/* has connection been established? */
optlen = sizeof(connect_rv);
&optlen) <0) {
connect_rv = errno;
}
return JNI_TRUE;
}
}
return JNI_FALSE;
}
}