/*
* 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 <windows.h>
#include <winsock2.h>
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <process.h>
#include "java_net_InetAddress.h"
#include "java_net_Inet4AddressImpl.h"
#include "net_util.h"
#include "icmp.h"
/*
* Returns true if hostname is in dotted IP address format. Note that this
* function performs a syntax check only. For each octet it just checks that
* the octet is at most 3 digits.
*/
char *c = (char *)hostname;
int octets = 0;
unsigned int cur = 0;
int digit_cnt = 0;
while (*c) {
if (*c == '.') {
if (digit_cnt == 0) {
return JNI_FALSE;
} else {
if (octets < 4) {
cur = 0;
digit_cnt = 0;
} else {
return JNI_FALSE;
}
}
c++;
continue;
}
if ((*c < '0') || (*c > '9')) {
return JNI_FALSE;
}
digit_cnt++;
if (digit_cnt > 3) {
return JNI_FALSE;
}
/* don't check if current octet > 255 */
/* Move onto next character and check for EOF */
c++;
if (*c == '\0') {
if (octets < 4) {
} else {
return JNI_FALSE;
}
}
}
}
/*
* Inet4AddressImpl
*/
/*
* Class: java_net_Inet4AddressImpl
* Method: getLocalHostName
*/
}
}
static int initialized = 0;
/*
* Find an internet address for a given hostname. Not this 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 be a %d.%d.%d.%d string. The only
* exception should be when any of the %d are out of range and
* we fallback to a lookup.
*
* Class: java_net_Inet4AddressImpl
* Method: lookupAllHostAddr
*
* This is almost shared code
*/
const char *hostname;
if (!initialized) {
initialized = 1;
}
return NULL;
}
/*
* The NT/2000 resolver tolerates a space in front of localhost. This
* is not consistent with other implementations of gethostbyname.
* In addition we must do a white space check on Solaris to avoid a
* bug whereby 0.0.0.0 is returned if any host name has a white space.
*/
goto cleanupAndReturn;
}
/*
* If the format is x.x.x.x then don't use gethostbyname as Windows
* is unable to handle octets which are out of range.
*/
unsigned int address;
/*
* Are any of the octets out of range?
*/
goto cleanupAndReturn;
}
/*
* Return an byte array with the populated address.
*/
goto cleanupAndReturn;
}
goto cleanupAndReturn;
}
return ret;
}
/*
* Perform the lookup
*/
int i = 0;
i++;
addrp++;
}
goto cleanupAndReturn;
}
i = 0;
goto cleanupAndReturn;
}
addrp++;
i++;
}
} else {
}
return ret;
}
/*
* Class: java_net_Inet4AddressImpl
* Method: getHostByAddr
*/
return NULL;
}
return NULL;
}
}
/**
* 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
int read_rv = 0;
/* Initialize the sequence number to a suitable random number and
shift right one place to allow sufficient room for increamenting. */
/* icmp_id is a 16 bit data type, therefore down cast the pid */
/**
* A TTL was specified, let's set the socket option.
*/
if (ttl > 0) {
}
/**
* A network interface was specified, let's bind to it.
*/
return JNI_FALSE;
}
}
/**
* Let's make the socket non blocking
*/
hEvent = WSACreateEvent();
/**
* send 1 ICMP REQUEST every second until either we get a valid reply
* or the timeout expired.
*/
do {
/**
* construct the ICMP header
*/
/**
* checksum has to be set to zero before we can calculate the
* real checksum!
*/
icmp->icmp_cksum = 0;
/**
* Ping!
*/
sizeof(struct sockaddr));
if (n < 0 && WSAGetLastError() != WSAEWOULDBLOCK) {
return JNI_FALSE;
}
/*
* wait for 1 second at most
*/
do {
if (tmout2 >= 0) {
/**
* Is that a proper ICMP reply?
*/
return JNI_TRUE;
}
}
} while (tmout2 > 0);
timeout -= 1000;
seq++;
} while (timeout > 0);
return JNI_FALSE;
}
/*
* Class: java_net_Inet4AddressImpl
* Method: isReachable0
* Signature: ([bI[bI)Z
*/
int len = 0;
int sz;
/**
* Convert IP address from byte array to integer
*/
if (sz != 4) {
return JNI_FALSE;
}
/**
* Socket address
*/
/**
* If a network interface was specified, let's convert its address
* as well.
*/
}
#if 0
/*
* Windows implementation of ICMP & RAW sockets is too unreliable for now.
* Therefore it's best not to try it at all and rely only on TCP
* We may revisit and enable this code in the future.
*/
/*
* 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.
*/
}
#endif
/*
* 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;
}
}
/*
*/
hEvent = WSACreateEvent();
/* 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 (WSAGetLastError()) {
case WSAEHOSTUNREACH: /* Host Unreachable */
case WSAENETUNREACH: /* Network Unreachable */
case WSAENETDOWN: /* Network is down */
case WSAEPFNOSUPPORT: /* Protocol Family unsupported */
return JNI_FALSE;
}
if (WSAGetLastError() != WSAEWOULDBLOCK) {
"connect failed");
return JNI_FALSE;
}
/* has connection been established */
if (timeout >= 0) {
optlen = sizeof(connect_rv);
&optlen) <0) {
}
return JNI_TRUE;
}
}
}
return JNI_FALSE;
}