3658N/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 * 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 * 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. 2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 4632N/A/* Use getaddrinfo(3), which is thread safe */ 4632N/A/************************************************************************ 4632N/A * Class: java_net_Inet4AddressImpl 4632N/A /* Something went wrong, maybe networking is not setup? */ 4632N/A /* host is known to name service */ 4632N/A /* if getnameinfo fails hostname is still the value 4632N/A * Find an internet address for a given hostname. Note that this 4632N/A * code only works for addresses of type INET. The translation 4632N/A * of %d.%d.%d.%d to an address (int) occurs in java now, so the 4632N/A * String "host" shouldn't *ever* be a %d.%d.%d.%d string 4632N/A * Class: java_net_Inet4AddressImpl 4632N/A * Method: lookupAllHostAddr 4632N/A * Workaround for Solaris bug 4160367 - if a hostname contains a 4632N/A * white space then 0.0.0.0 is returned 4632N/A /* we may have memory to free at the end of this */ 4632N/A /* We need 4 bytes to store ipv4 address; */ 4632N/A /* we may have memory to free at the end of this */ 4632N/A * Class: java_net_Inet4AddressImpl 4632N/A * For IPv4 addresses construct a sockaddr_in structure. 4632N/A#
else /* defined(_ALLBSD_SOURCE) && !defined(HAS_GLIBC_GETHOSTBY_R) */ 0N/A/* the initial size of our hostent buffers */ 0N/A/************************************************************************ 0N/A * Class: java_net_Inet4AddressImpl 0N/A * Method: getLocalHostName 0N/A /* Something went wrong, maybe networking is not setup? */ 0N/A * Solaris gethostname() says "host", so extra work is needed. 0N/A /* Solaris doesn't want to give us a fully qualified domain name. 0N/A * We do a reverse lookup to try and get one. This works 0N/A * if NIS comes first (it still gets only a partial name). 0N/A * We use thread-safe system calls. 0N/A#
endif /* __linux__ */ 3658N/A // these buffers must be pointer-aligned so they are declared 0N/A * If gethostbyaddr_r() found a fully qualified host name, 0N/A * returns that name. Otherwise, returns the hostname 0N/A * found by gethostname(). 0N/A * Find an internet address for a given hostname. Note that 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 *ever* be a %d.%d.%d.%d string 0N/A * Class: java_net_Inet4AddressImpl 0N/A * Method: lookupAllHostAddr 3658N/A // this buffer must be pointer-aligned so is declared 0N/A /* temporary buffer, on the off chance we need to expand */ 0N/A * Workaround for Solaris bug 4160367 - if a hostname contains a 0N/A * white space then 0.0.0.0 is returned 0N/A /* Try once, with our static buffer. */ 0N/A /* With the re-entrant system calls, it's possible that the buffer 0N/A * we pass to it is not large enough to hold an exceptionally 0N/A * large DNS entry. This is signaled by errno->ERANGE. We try once 0N/A * more, with a very big size. 0N/A /* we may have memory to free at the end of this */ 0N/A * Class: java_net_Inet4AddressImpl 0N/A * Method: getHostByAddr 3658N/A // this buffer must be pointer-aligned so is declared 0N/A * We are careful here to use the reentrant version of 0N/A * gethostbyname because at the Java level this routine is not 0N/A * protected by any synchronization. 0N/A * Still keeping the reentrant platform dependent calls temporarily 0N/A * We should probably conform to one interface later. 0N/A /* With the re-entrant system calls, it's possible that the buffer 0N/A * we pass to it is not large enough to hold an exceptionally 0N/A * large DNS entry. This is signaled by errno->ERANGE. We try once 0N/A * more, with a very big size. 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 /* icmp_id is a 16 bit data type, therefore down cast the pid */ 0N/A * sets the ttl (max number of hops) 0N/A * a specific interface was specified, so let's bind the socket 0N/A * to that interface to ensure the requests are sent only through it. 0N/A * Make the socket non blocking so we can use select 0N/A * create the ICMP request 2983N/A * On some Linuxes, when bound to the loopback interface, sendto 5201N/A * will fail and errno will be set to EINVAL or EHOSTUNREACH. 5201N/A * When that happens, don't throw an exception, just return false. 0N/A * We did receive something, but is it what we were expecting? 0N/A * I.E.: A ICMP_ECHOREPLY packet with the proper PID. 0N/A * Class: java_net_Inet4AddressImpl 0N/A * Method: isReachable0 0N/A * Signature: ([bI[bI)Z 0N/A * If a network interface was specified, let's create the address 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 * On some Linuxes, when bound to the loopback interface, connect 5201N/A * will fail and errno will be set to EINVAL or EHOSTUNREACH. 5201N/A * When that happens, don't throw an exception, just return false. 0N/A#
endif /* __linux__ */ 0N/A /* has connection been established? */