0N/A/*
3909N/A * Copyright (c) 2000, 2011, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/A#include <windows.h>
0N/A#include <winsock2.h>
0N/A#include <ctype.h>
0N/A#include <stdio.h>
0N/A#include <stdlib.h>
0N/A#include <malloc.h>
0N/A#include <sys/types.h>
453N/A#include <process.h>
0N/A
0N/A#include "java_net_InetAddress.h"
0N/A#include "java_net_Inet4AddressImpl.h"
0N/A#include "net_util.h"
0N/A#include "icmp.h"
0N/A
0N/A
0N/A/*
0N/A * Returns true if hostname is in dotted IP address format. Note that this
0N/A * function performs a syntax check only. For each octet it just checks that
0N/A * the octet is at most 3 digits.
0N/A */
0N/Ajboolean isDottedIPAddress(const char *hostname, unsigned int *addrp) {
0N/A char *c = (char *)hostname;
0N/A int octets = 0;
0N/A unsigned int cur = 0;
0N/A int digit_cnt = 0;
0N/A
0N/A while (*c) {
0N/A if (*c == '.') {
0N/A if (digit_cnt == 0) {
0N/A return JNI_FALSE;
0N/A } else {
0N/A if (octets < 4) {
0N/A addrp[octets++] = cur;
0N/A cur = 0;
0N/A digit_cnt = 0;
0N/A } else {
0N/A return JNI_FALSE;
0N/A }
0N/A }
0N/A c++;
0N/A continue;
0N/A }
0N/A
0N/A if ((*c < '0') || (*c > '9')) {
0N/A return JNI_FALSE;
0N/A }
0N/A
0N/A digit_cnt++;
0N/A if (digit_cnt > 3) {
0N/A return JNI_FALSE;
0N/A }
0N/A
0N/A /* don't check if current octet > 255 */
0N/A cur = cur*10 + (*c - '0');
0N/A
0N/A /* Move onto next character and check for EOF */
0N/A c++;
0N/A if (*c == '\0') {
0N/A if (octets < 4) {
0N/A addrp[octets++] = cur;
0N/A } else {
0N/A return JNI_FALSE;
0N/A }
0N/A }
0N/A }
0N/A
0N/A return (jboolean)(octets == 4);
0N/A}
0N/A
0N/A/*
0N/A * Inet4AddressImpl
0N/A */
0N/A
0N/A/*
0N/A * Class: java_net_Inet4AddressImpl
0N/A * Method: getLocalHostName
0N/A * Signature: ()Ljava/lang/String;
0N/A */
0N/AJNIEXPORT jstring JNICALL
0N/AJava_java_net_Inet4AddressImpl_getLocalHostName (JNIEnv *env, jobject this) {
0N/A char hostname[256];
0N/A
0N/A if (gethostname(hostname, sizeof hostname) == -1) {
0N/A strcpy(hostname, "localhost");
0N/A }
0N/A return JNU_NewStringPlatform(env, hostname);
0N/A}
0N/A
0N/Astatic jclass ni_iacls;
0N/Astatic jclass ni_ia4cls;
0N/Astatic jmethodID ni_ia4ctrID;
0N/Astatic int initialized = 0;
0N/A
0N/A/*
0N/A * Find an internet address for a given hostname. Not this this
0N/A * code only works for addresses of type INET. The translation
0N/A * of %d.%d.%d.%d to an address (int) occurs in java now, so the
0N/A * String "host" shouldn't be a %d.%d.%d.%d string. The only
0N/A * exception should be when any of the %d are out of range and
0N/A * we fallback to a lookup.
0N/A *
0N/A * Class: java_net_Inet4AddressImpl
0N/A * Method: lookupAllHostAddr
0N/A * Signature: (Ljava/lang/String;)[[B
0N/A *
0N/A * This is almost shared code
0N/A */
0N/A
0N/AJNIEXPORT jobjectArray JNICALL
0N/AJava_java_net_Inet4AddressImpl_lookupAllHostAddr(JNIEnv *env, jobject this,
0N/A jstring host) {
0N/A const char *hostname;
0N/A struct hostent *hp;
0N/A unsigned int addr[4];
0N/A
0N/A jobjectArray ret = NULL;
0N/A
0N/A if (!initialized) {
0N/A ni_iacls = (*env)->FindClass(env, "java/net/InetAddress");
0N/A ni_iacls = (*env)->NewGlobalRef(env, ni_iacls);
0N/A ni_ia4cls = (*env)->FindClass(env, "java/net/Inet4Address");
0N/A ni_ia4cls = (*env)->NewGlobalRef(env, ni_ia4cls);
0N/A ni_ia4ctrID = (*env)->GetMethodID(env, ni_ia4cls, "<init>", "()V");
0N/A initialized = 1;
0N/A }
0N/A
0N/A if (IS_NULL(host)) {
0N/A JNU_ThrowNullPointerException(env, "host argument");
0N/A return NULL;
0N/A }
0N/A hostname = JNU_GetStringPlatformChars(env, host, JNI_FALSE);
0N/A CHECK_NULL_RETURN(hostname, NULL);
0N/A
0N/A /*
0N/A * The NT/2000 resolver tolerates a space in front of localhost. This
0N/A * is not consistent with other implementations of gethostbyname.
0N/A * In addition we must do a white space check on Solaris to avoid a
0N/A * bug whereby 0.0.0.0 is returned if any host name has a white space.
0N/A */
0N/A if (isspace(hostname[0])) {
0N/A JNU_ThrowByName(env, JNU_JAVANETPKG "UnknownHostException", hostname);
0N/A goto cleanupAndReturn;
0N/A }
0N/A
0N/A /*
0N/A * If the format is x.x.x.x then don't use gethostbyname as Windows
0N/A * is unable to handle octets which are out of range.
0N/A */
0N/A if (isDottedIPAddress(hostname, &addr[0])) {
0N/A unsigned int address;
0N/A jobject iaObj;
0N/A
0N/A /*
0N/A * Are any of the octets out of range?
0N/A */
0N/A if (addr[0] > 255 || addr[1] > 255 || addr[2] > 255 || addr[3] > 255) {
0N/A JNU_ThrowByName(env, JNU_JAVANETPKG "UnknownHostException", hostname);
0N/A goto cleanupAndReturn;
0N/A }
0N/A
0N/A /*
0N/A * Return an byte array with the populated address.
0N/A */
0N/A address = (addr[3]<<24) & 0xff000000;
0N/A address |= (addr[2]<<16) & 0xff0000;
0N/A address |= (addr[1]<<8) & 0xff00;
0N/A address |= addr[0];
0N/A
0N/A ret = (*env)->NewObjectArray(env, 1, ni_iacls, NULL);
0N/A
0N/A if (IS_NULL(ret)) {
0N/A goto cleanupAndReturn;
0N/A }
0N/A
0N/A iaObj = (*env)->NewObject(env, ni_ia4cls, ni_ia4ctrID);
0N/A if (IS_NULL(iaObj)) {
0N/A ret = NULL;
0N/A goto cleanupAndReturn;
0N/A }
5888N/A setInetAddress_addr(env, iaObj, ntohl(address));
0N/A (*env)->SetObjectArrayElement(env, ret, 0, iaObj);
0N/A JNU_ReleaseStringPlatformChars(env, host, hostname);
0N/A return ret;
0N/A }
0N/A
0N/A /*
0N/A * Perform the lookup
0N/A */
0N/A if ((hp = gethostbyname((char*)hostname)) != NULL) {
0N/A struct in_addr **addrp = (struct in_addr **) hp->h_addr_list;
0N/A int len = sizeof(struct in_addr);
0N/A int i = 0;
0N/A
0N/A while (*addrp != (struct in_addr *) 0) {
0N/A i++;
0N/A addrp++;
0N/A }
0N/A
0N/A ret = (*env)->NewObjectArray(env, i, ni_iacls, NULL);
0N/A
0N/A if (IS_NULL(ret)) {
0N/A goto cleanupAndReturn;
0N/A }
0N/A
0N/A addrp = (struct in_addr **) hp->h_addr_list;
0N/A i = 0;
0N/A while (*addrp != (struct in_addr *) 0) {
0N/A jobject iaObj = (*env)->NewObject(env, ni_ia4cls, ni_ia4ctrID);
0N/A if (IS_NULL(iaObj)) {
0N/A ret = NULL;
0N/A goto cleanupAndReturn;
0N/A }
5888N/A setInetAddress_addr(env, iaObj, ntohl((*addrp)->s_addr));
5888N/A setInetAddress_hostName(env, iaObj, host);
0N/A (*env)->SetObjectArrayElement(env, ret, i, iaObj);
0N/A addrp++;
0N/A i++;
0N/A }
0N/A } else {
0N/A JNU_ThrowByName(env, JNU_JAVANETPKG "UnknownHostException", hostname);
0N/A }
0N/A
0N/AcleanupAndReturn:
0N/A JNU_ReleaseStringPlatformChars(env, host, hostname);
0N/A return ret;
0N/A}
0N/A
0N/A/*
0N/A * Class: java_net_Inet4AddressImpl
0N/A * Method: getHostByAddr
0N/A * Signature: (I)Ljava/lang/String;
0N/A */
0N/AJNIEXPORT jstring JNICALL
0N/AJava_java_net_Inet4AddressImpl_getHostByAddr(JNIEnv *env, jobject this,
0N/A jbyteArray addrArray) {
0N/A struct hostent *hp;
0N/A jbyte caddr[4];
0N/A jint addr;
0N/A (*env)->GetByteArrayRegion(env, addrArray, 0, 4, caddr);
0N/A addr = ((caddr[0]<<24) & 0xff000000);
0N/A addr |= ((caddr[1] <<16) & 0xff0000);
0N/A addr |= ((caddr[2] <<8) & 0xff00);
0N/A addr |= (caddr[3] & 0xff);
0N/A addr = htonl(addr);
0N/A
0N/A hp = gethostbyaddr((char *)&addr, sizeof(addr), AF_INET);
0N/A if (hp == NULL) {
0N/A JNU_ThrowByName(env, JNU_JAVANETPKG "UnknownHostException", 0);
0N/A return NULL;
0N/A }
0N/A if (hp->h_name == NULL) { /* Deal with bug in Windows XP */
0N/A JNU_ThrowByName(env, JNU_JAVANETPKG "UnknownHostException", 0);
0N/A return NULL;
0N/A }
0N/A return JNU_NewStringPlatform(env, hp->h_name);
0N/A}
0N/A
0N/A
0N/A/**
0N/A * ping implementation.
0N/A * Send a ICMP_ECHO_REQUEST packet every second until either the timeout
0N/A * expires or a answer is received.
0N/A * Returns true is an ECHO_REPLY is received, otherwise, false.
0N/A */
0N/Astatic jboolean
0N/Aping4(JNIEnv *env, jint fd, struct sockaddr_in* him, jint timeout,
0N/A struct sockaddr_in* netif, jint ttl) {
0N/A jint size;
0N/A jint n, len, hlen1, icmplen;
0N/A char sendbuf[1500];
0N/A char recvbuf[1500];
0N/A struct icmp *icmp;
0N/A struct ip *ip;
0N/A WSAEVENT hEvent;
0N/A struct sockaddr sa_recv;
0N/A jint tmout2;
0N/A u_short pid, seq;
0N/A int read_rv = 0;
0N/A
0N/A /* Initialize the sequence number to a suitable random number and
0N/A shift right one place to allow sufficient room for increamenting. */
0N/A seq = ((unsigned short)rand()) >> 1;
0N/A
0N/A /* icmp_id is a 16 bit data type, therefore down cast the pid */
453N/A pid = (u_short) _getpid();
0N/A size = 60*1024;
0N/A setsockopt(fd, SOL_SOCKET, SO_RCVBUF, (const char *) &size, sizeof(size));
0N/A /**
0N/A * A TTL was specified, let's set the socket option.
0N/A */
0N/A if (ttl > 0) {
0N/A setsockopt(fd, IPPROTO_IP, IP_TTL, (const char *) &ttl, sizeof(ttl));
0N/A }
0N/A
0N/A /**
0N/A * A network interface was specified, let's bind to it.
0N/A */
0N/A if (netif != NULL) {
0N/A if (bind(fd, (struct sockaddr*)netif, sizeof(struct sockaddr_in)) < 0) {
0N/A NET_ThrowNew(env, WSAGetLastError(), "Can't bind socket");
0N/A closesocket(fd);
0N/A return JNI_FALSE;
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Let's make the socket non blocking
0N/A */
0N/A hEvent = WSACreateEvent();
0N/A WSAEventSelect(fd, hEvent, FD_READ|FD_CONNECT|FD_CLOSE);
0N/A
0N/A /**
0N/A * send 1 ICMP REQUEST every second until either we get a valid reply
0N/A * or the timeout expired.
0N/A */
0N/A do {
0N/A /**
0N/A * construct the ICMP header
0N/A */
0N/A memset(sendbuf, 0, 1500);
0N/A icmp = (struct icmp *) sendbuf;
0N/A icmp->icmp_type = ICMP_ECHO;
0N/A icmp->icmp_code = 0;
0N/A icmp->icmp_id = htons(pid);
0N/A icmp->icmp_seq = htons(seq);
0N/A /**
0N/A * checksum has to be set to zero before we can calculate the
0N/A * real checksum!
0N/A */
0N/A icmp->icmp_cksum = 0;
0N/A icmp->icmp_cksum = in_cksum((u_short *)icmp, 64);
0N/A /**
0N/A * Ping!
0N/A */
0N/A n = sendto(fd, sendbuf, 64, 0, (struct sockaddr *)him,
0N/A sizeof(struct sockaddr));
0N/A if (n < 0 && WSAGetLastError() != WSAEWOULDBLOCK) {
0N/A NET_ThrowNew(env, WSAGetLastError(), "Can't send ICMP packet");
0N/A closesocket(fd);
0N/A WSACloseEvent(hEvent);
0N/A return JNI_FALSE;
0N/A }
0N/A
0N/A /*
0N/A * wait for 1 second at most
0N/A */
0N/A tmout2 = timeout > 1000 ? 1000 : timeout;
0N/A do {
0N/A tmout2 = NET_Wait(env, fd, NET_WAIT_READ, tmout2);
0N/A if (tmout2 >= 0) {
0N/A len = sizeof(sa_recv);
0N/A n = recvfrom(fd, recvbuf, sizeof(recvbuf), 0, &sa_recv, &len);
0N/A ip = (struct ip*) recvbuf;
0N/A hlen1 = (ip->ip_hl) << 2;
0N/A icmp = (struct icmp *) (recvbuf + hlen1);
0N/A icmplen = n - hlen1;
0N/A /**
0N/A * Is that a proper ICMP reply?
0N/A */
0N/A if (icmplen >= 8 && icmp->icmp_type == ICMP_ECHOREPLY &&
0N/A (ntohs(icmp->icmp_seq) == seq) && (ntohs(icmp->icmp_id) == pid)) {
0N/A closesocket(fd);
0N/A WSACloseEvent(hEvent);
0N/A return JNI_TRUE;
0N/A }
0N/A }
0N/A } while (tmout2 > 0);
0N/A timeout -= 1000;
0N/A seq++;
0N/A } while (timeout > 0);
0N/A closesocket(fd);
0N/A WSACloseEvent(hEvent);
0N/A return JNI_FALSE;
0N/A}
0N/A
0N/A/*
0N/A * Class: java_net_Inet4AddressImpl
0N/A * Method: isReachable0
0N/A * Signature: ([bI[bI)Z
0N/A */
0N/AJNIEXPORT jboolean JNICALL
0N/AJava_java_net_Inet4AddressImpl_isReachable0(JNIEnv *env, jobject this,
0N/A jbyteArray addrArray,
0N/A jint timeout,
0N/A jbyteArray ifArray,
0N/A jint ttl) {
0N/A jint addr;
0N/A jbyte caddr[4];
0N/A jint fd;
0N/A struct sockaddr_in him;
0N/A struct sockaddr_in* netif = NULL;
0N/A struct sockaddr_in inf;
0N/A int len = 0;
0N/A WSAEVENT hEvent;
0N/A int connect_rv = -1;
0N/A int sz;
0N/A
0N/A /**
0N/A * Convert IP address from byte array to integer
0N/A */
0N/A sz = (*env)->GetArrayLength(env, addrArray);
0N/A if (sz != 4) {
0N/A return JNI_FALSE;
0N/A }
0N/A memset((char *) &him, 0, sizeof(him));
0N/A memset((char *) caddr, 0, sizeof(caddr));
0N/A (*env)->GetByteArrayRegion(env, addrArray, 0, 4, caddr);
0N/A addr = ((caddr[0]<<24) & 0xff000000);
0N/A addr |= ((caddr[1] <<16) & 0xff0000);
0N/A addr |= ((caddr[2] <<8) & 0xff00);
0N/A addr |= (caddr[3] & 0xff);
0N/A addr = htonl(addr);
0N/A /**
0N/A * Socket address
0N/A */
0N/A him.sin_addr.s_addr = addr;
0N/A him.sin_family = AF_INET;
0N/A len = sizeof(him);
0N/A
0N/A /**
0N/A * If a network interface was specified, let's convert its address
0N/A * as well.
0N/A */
0N/A if (!(IS_NULL(ifArray))) {
0N/A memset((char *) caddr, 0, sizeof(caddr));
0N/A (*env)->GetByteArrayRegion(env, ifArray, 0, 4, caddr);
0N/A addr = ((caddr[0]<<24) & 0xff000000);
0N/A addr |= ((caddr[1] <<16) & 0xff0000);
0N/A addr |= ((caddr[2] <<8) & 0xff00);
0N/A addr |= (caddr[3] & 0xff);
0N/A addr = htonl(addr);
0N/A inf.sin_addr.s_addr = addr;
0N/A inf.sin_family = AF_INET;
0N/A inf.sin_port = 0;
0N/A netif = &inf;
0N/A }
0N/A
0N/A#if 0
0N/A /*
0N/A * Windows implementation of ICMP & RAW sockets is too unreliable for now.
0N/A * Therefore it's best not to try it at all and rely only on TCP
0N/A * We may revisit and enable this code in the future.
0N/A */
0N/A
0N/A /*
0N/A * Let's try to create a RAW socket to send ICMP packets
0N/A * This usually requires "root" privileges, so it's likely to fail.
0N/A */
0N/A fd = NET_Socket(AF_INET, SOCK_RAW, IPPROTO_ICMP);
0N/A if (fd != -1) {
0N/A /*
0N/A * It didn't fail, so we can use ICMP_ECHO requests.
0N/A */
0N/A return ping4(env, fd, &him, timeout, netif, ttl);
0N/A }
0N/A#endif
0N/A
0N/A /*
0N/A * Can't create a raw socket, so let's try a TCP socket
0N/A */
0N/A fd = NET_Socket(AF_INET, SOCK_STREAM, 0);
0N/A if (fd == JVM_IO_ERR) {
0N/A /* note: if you run out of fds, you may not be able to load
0N/A * the exception class, and get a NoClassDefFoundError
0N/A * instead.
0N/A */
0N/A NET_ThrowNew(env, WSAGetLastError(), "Can't create socket");
0N/A return JNI_FALSE;
0N/A }
0N/A if (ttl > 0) {
0N/A setsockopt(fd, IPPROTO_IP, IP_TTL, (const char *)&ttl, sizeof(ttl));
0N/A }
0N/A /*
0N/A * A network interface was specified, so let's bind to it.
0N/A */
0N/A if (netif != NULL) {
0N/A if (bind(fd, (struct sockaddr*)netif, sizeof(struct sockaddr_in)) < 0) {
0N/A NET_ThrowNew(env, WSAGetLastError(), "Can't bind socket");
0N/A closesocket(fd);
0N/A return JNI_FALSE;
0N/A }
0N/A }
0N/A
0N/A /*
0N/A * Make the socket non blocking so we can use select/poll.
0N/A */
0N/A hEvent = WSACreateEvent();
0N/A WSAEventSelect(fd, hEvent, FD_READ|FD_CONNECT|FD_CLOSE);
0N/A
0N/A /* no need to use NET_Connect as non-blocking */
0N/A him.sin_port = htons(7); /* Echo */
0N/A connect_rv = connect(fd, (struct sockaddr *)&him, len);
0N/A
0N/A /**
0N/A * connection established or refused immediately, either way it means
0N/A * we were able to reach the host!
0N/A */
0N/A if (connect_rv == 0 || WSAGetLastError() == WSAECONNREFUSED) {
0N/A WSACloseEvent(hEvent);
0N/A closesocket(fd);
0N/A return JNI_TRUE;
0N/A } else {
0N/A int optlen;
0N/A
0N/A switch (WSAGetLastError()) {
0N/A case WSAEHOSTUNREACH: /* Host Unreachable */
0N/A case WSAENETUNREACH: /* Network Unreachable */
0N/A case WSAENETDOWN: /* Network is down */
0N/A case WSAEPFNOSUPPORT: /* Protocol Family unsupported */
0N/A WSACloseEvent(hEvent);
0N/A closesocket(fd);
0N/A return JNI_FALSE;
0N/A }
0N/A
0N/A if (WSAGetLastError() != WSAEWOULDBLOCK) {
0N/A NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "ConnectException",
0N/A "connect failed");
0N/A WSACloseEvent(hEvent);
0N/A closesocket(fd);
0N/A return JNI_FALSE;
0N/A }
0N/A
0N/A timeout = NET_Wait(env, fd, NET_WAIT_CONNECT, timeout);
0N/A
0N/A /* has connection been established */
0N/A
0N/A if (timeout >= 0) {
0N/A optlen = sizeof(connect_rv);
3334N/A if (getsockopt(fd, SOL_SOCKET, SO_ERROR, (void*)&connect_rv,
3334N/A &optlen) <0) {
0N/A connect_rv = WSAGetLastError();
0N/A }
0N/A
0N/A if (connect_rv == 0 || connect_rv == WSAECONNREFUSED) {
0N/A WSACloseEvent(hEvent);
0N/A closesocket(fd);
0N/A return JNI_TRUE;
0N/A }
0N/A }
0N/A }
0N/A WSACloseEvent(hEvent);
0N/A closesocket(fd);
0N/A return JNI_FALSE;
0N/A}