Inet4AddressImpl.c revision 373
0N/A * Copyright 2000-2006 Sun Microsystems, Inc. All Rights Reserved. 0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 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 0N/A * published by the Free Software Foundation. Sun designates this 0N/A * particular file as subject to the "Classpath" exception as provided 0N/A * by Sun in the LICENSE file that accompanied this code. 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 * 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 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, 0N/A * CA 95054 USA or visit www.sun.com if you need additional information or 0N/A * have any questions. 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 if ((*c <
'0') || (*c >
'9')) {
0N/A /* don't check if current octet > 255 */ 0N/A /* Move onto next character and check for EOF */ 0N/A * Class: java_net_Inet4AddressImpl 0N/A * Method: getLocalHostName 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 * Class: java_net_Inet4AddressImpl 0N/A * Method: lookupAllHostAddr 0N/A * This is almost shared code 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 * 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 * Are any of the octets out of range? 0N/A * Return an byte array with the populated address. 0N/A * Perform the lookup 0N/A * Class: java_net_Inet4AddressImpl 0N/A * Method: getHostByAddr 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 /* Initialize the sequence number to a suitable random number and 0N/A shift right one place to allow sufficient room for increamenting. */ 0N/A /* icmp_id is a 16 bit data type, therefore down cast the pid */ 0N/A * A TTL was specified, let's set the socket option. 0N/A * A network interface was specified, let's bind to it. 0N/A * Let's make the socket non blocking 0N/A * send 1 ICMP REQUEST every second until either we get a valid reply 0N/A * or the timeout expired. 0N/A * construct the ICMP header 0N/A * checksum has to be set to zero before we can calculate the 0N/A * wait for 1 second at most 0N/A * Is that a proper ICMP reply? 0N/A * Class: java_net_Inet4AddressImpl 0N/A * Method: isReachable0 0N/A * Signature: ([bI[bI)Z 0N/A * Convert IP address from byte array to integer 0N/A * If a network interface was specified, let's convert its address 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 * 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 * It didn't fail, so we can use ICMP_ECHO requests. 0N/A * Can't create a raw socket, so let's try a TCP socket 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 * A network interface was specified, so let's bind to it. 0N/A /* no need to use NET_Connect as non-blocking */ 0N/A * connection established or refused immediately, either way it means 0N/A * we were able to reach the host! 0N/A /* has connection been established */