Inet4AddressImpl.c revision 0
/*
* Copyright 2000-2007 Sun Microsystems, Inc. All Rights Reserved.
* 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. Sun designates this
* particular file as subject to the "Classpath" exception as provided
* by Sun 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
* CA 95054 USA or visit www.sun.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>
#include "jvm.h"
#include "jni_util.h"
#include "net_util.h"
#include "java_net_Inet4AddressImpl.h"
/* the initial size of our hostent buffers */
#define HENT_BUF_SIZE 1024
#ifndef __GLIBC__
/* gethostname() is in libc.so but I can't find a header file for it */
#endif
/************************************************************************
* 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__ */
char buf[HENT_BUF_SIZE];
char buf2[HENT_BUF_SIZE];
int h_error=0;
#ifdef __GLIBC__
#else
#endif
if (hp) {
#ifdef __GLIBC__
#else
#endif
if (hp) {
/*
* If gethostbyaddr_r() found a fully qualified host name,
* returns that name. Otherwise, returns the hostname
* found by gethostname().
*/
}
}
}
}
static jmethodID ni_ia4ctrID;
static jfieldID ni_iaaddressID;
static jfieldID ni_iahostID;
static jfieldID ni_iafamilyID;
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;
jobjectArray ret = 0;
char buf[HENT_BUF_SIZE];
/* temporary buffer, on the off chance we need to expand */
int h_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;
}
/* Try once, with our static buffer. */
#ifdef __GLIBC__
#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 __GLIBC__
#else
&h_error);
#endif
}
}
int i = 0;
i++;
addrp++;
}
goto cleanupAndReturn;
}
/* 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
*/
char buf[HENT_BUF_SIZE];
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 __GLIBC__
#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 __GLIBC__
#else
#endif
} else {
}
}
} else {
}
if (tmp) {
}
return ret;
}
#define SET_NONBLOCKING(fd) { \
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
char sendbuf[1500];
char recvbuf[1500];
struct sockaddr_in sa_recv;
/* 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 ) {
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;
}
}
} while (tmout2 > 0);
timeout -= 1000;
} while (timeout >0);
return JNI_FALSE;
}
/*
* Class: java_net_Inet4AddressImpl
* Method: isReachable0
* Signature: ([bI[bI)Z
*/
struct sockaddr_in him;
struct sockaddr_in inf;
int len = 0;
int connect_rv = -1;
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:
/*
* On some Linuxes, when bound to the loopback interface, connect
* will fail and errno will be set to EINVAL. 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;
}
}