📄 inetaddress.java
字号:
/* InetAddress.java -- Class to model an Internet address Copyright (C) 1998, 1999, 2002, 2004, 2005 Free Software Foundation, Inc.This file is part of GNU Classpath.GNU Classpath is free software; you can redistribute it and/or modifyit under the terms of the GNU General Public License as published bythe Free Software Foundation; either version 2, or (at your option)any later version.GNU Classpath is distributed in the hope that it will be useful, butWITHOUT ANY WARRANTY; without even the implied warranty ofMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNUGeneral Public License for more details.You should have received a copy of the GNU General Public Licensealong with GNU Classpath; see the file COPYING. If not, write to theFree Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA02110-1301 USA.Linking this library statically or dynamically with other modules ismaking a combined work based on this library. Thus, the terms andconditions of the GNU General Public License cover the wholecombination.As a special exception, the copyright holders of this library give youpermission to link this library with independent modules to produce anexecutable, regardless of the license terms of these independentmodules, and to copy and distribute the resulting executable underterms of your choice, provided that you also meet, for each linkedindependent module, the terms and conditions of the license of thatmodule. An independent module is a module which is not derived fromor based on this library. If you modify this library, you may extendthis exception to your version of the library, but you are notobligated to do so. If you do not wish to do so, delete thisexception statement from your version. */package java.net;import gnu.classpath.Configuration;import java.io.IOException;import java.io.ObjectInputStream;import java.io.ObjectOutputStream;import java.io.ObjectStreamException;import java.io.Serializable;/** * This class models an Internet address. It does not have a public * constructor. Instead, new instances of this objects are created * using the static methods getLocalHost(), getByName(), and * getAllByName(). * * <p>This class fulfills the function of the C style functions gethostname(), * gethostbyname(), and gethostbyaddr(). It resolves Internet DNS names * into their corresponding numeric addresses and vice versa.</p> * * @author Aaron M. Renn (arenn@urbanophile.com) * @author Per Bothner * * @specnote This class is not final since JK 1.4 */public class InetAddress implements Serializable{ private static final long serialVersionUID = 3286316764910316507L; /** * Dummy InetAddress, used to bind socket to any (all) network interfaces. */ static InetAddress ANY_IF; private static final byte[] loopbackAddress = { 127, 0, 0, 1 }; private static final InetAddress loopback = new Inet4Address(loopbackAddress, "localhost"); private static InetAddress localhost = null; static { // load the shared library needed for name resolution if (Configuration.INIT_LOAD_LIBRARY) System.loadLibrary("javanet"); byte[] zeros = { 0, 0, 0, 0 }; ANY_IF = new Inet4Address(zeros, "0.0.0.0"); } /** * The Serialized Form specifies that an int 'address' is saved/restored. * This class uses a byte array internally so we'll just do the conversion * at serialization time and leave the rest of the algorithm as is. */ private int address; /** * An array of octets representing an IP address. */ transient byte[] addr; /** * The name of the host for this address. */ String hostName; /** * The field 'family' seems to be the AF_ value. * FIXME: Much of the code in the other java.net classes does not make * use of this family field. A better implementation would be to make * use of getaddrinfo() and have other methods just check the family * field rather than examining the length of the address each time. */ int family; /** * Initializes this object's addr instance variable from the passed in * byte array. Note that this constructor is protected and is called * only by static methods in this class. * * @param ipaddr The IP number of this address as an array of bytes * @param hostname The hostname of this IP address. */ InetAddress(byte[] ipaddr, String hostname) { addr = (null == ipaddr) ? null : (byte[]) ipaddr.clone(); hostName = hostname; if (ipaddr != null) family = getFamily(ipaddr); } /** * Returns true if this address is a multicast address, false otherwise. * An address is multicast if the high four bits are "1110". These are * also known as "Class D" addresses. * * @return true if mulitcast, false if not * * @since 1.1 */ public boolean isMulticastAddress() { // Mask against high order bits of 1110 if (addr.length == 4) return (addr[0] & 0xf0) == 0xe0; // Mask against high order bits of 11111111 if (addr.length == 16) return addr [0] == (byte) 0xFF; return false; } /** * Utility routine to check if the InetAddress in a wildcard address * * @since 1.4 */ public boolean isAnyLocalAddress() { // This is the IPv4 implementation. // Any class derived from InetAddress should override this. return equals(ANY_IF); } /** * Utility routine to check if the InetAddress is a loopback address * * @since 1.4 */ public boolean isLoopbackAddress() { // This is the IPv4 implementation. // Any class derived from InetAddress should override this. return (addr[0] & 0xff) == 0x7f; } /** * Utility routine to check if InetAddress is a link local address * * @since 1.4 */ public boolean isLinkLocalAddress() { // This is the IPv4 implementation. // Any class derived from InetAddress should override this. // XXX: This seems to not exist with IPv4 addresses return false; } /** * Utility routine to check if InetAddress is a site local address * * @since 1.4 */ public boolean isSiteLocalAddress() { // This is the IPv4 implementation. // Any class derived from InetAddress should override this. // 10.0.0.0/8 if ((addr[0] & 0xff) == 0x0a) return true; // 172.16.0.0/12 if ((addr[0] & 0xff) == 0xac && (addr[1] & 0xf0) == 0x10) return true; // 192.168.0.0/16 if ((addr[0] & 0xff) == 0xc0 && (addr[1] & 0xff) == 0xa8) return true; // XXX: Do we need to check more addresses here ? return false; } /** * Utility routine to check if InetAddress is a global multicast address * * @since 1.4 */ public boolean isMCGlobal() { // This is the IPv4 implementation. // Any class derived from InetAddress should override this. // XXX: This seems to not exist with IPv4 addresses return false; } /** * Utility routine to check if InetAddress is a node local multicast address. * * @since 1.4 */ public boolean isMCNodeLocal() { // This is the IPv4 implementation. // Any class derived from InetAddress should override this. // XXX: This seems to not exist with IPv4 addresses return false; } /** * Utility routine to check if InetAddress is a link local multicast address. * * @since 1.4 */ public boolean isMCLinkLocal() { // This is the IPv4 implementation. // Any class derived from InetAddress should override this. if (! isMulticastAddress()) return false; return ((addr[0] & 0xff) == 0xe0 && (addr[1] & 0xff) == 0x00 && (addr[2] & 0xff) == 0x00); } /** * Utility routine to check if InetAddress is a site local multicast address. * * @since 1.4 */ public boolean isMCSiteLocal() { // This is the IPv4 implementation. // Any class derived from InetAddress should override this. // XXX: This seems to not exist with IPv4 addresses return false; } /** * Utility routine to check if InetAddress is a organization local * multicast address. * * @since 1.4 */ public boolean isMCOrgLocal() { // This is the IPv4 implementation. // Any class derived from InetAddress should override this. // XXX: This seems to not exist with IPv4 addresses return false; } /** * Returns the hostname for this address. This will return the IP address * as a String if there is no hostname available for this address * * @return The hostname for this address */ public String getHostName() { if (hostName != null) return hostName; // Lookup hostname and set field. lookup (null, this, false); return hostName; } /** * Returns the canonical hostname represented by this InetAddress * * @since 1.4 */ public String getCanonicalHostName() { SecurityManager sm = System.getSecurityManager(); if (sm != null) { try { sm.checkConnect(hostName, -1); } catch (SecurityException e) { return getHostAddress(); } } // Try to find the FDQN now InetAddress address; byte[] ipaddr = getAddress(); if (ipaddr.length == 16) address = new Inet6Address(getAddress(), null); else address = new Inet4Address(getAddress(), null); return address.getHostName(); } /** * Returns the IP address of this object as a byte array. * * @return IP address */ public byte[] getAddress() { // An experiment shows that JDK1.2 returns a different byte array each // time. This makes sense, in terms of security. return (byte[]) addr.clone(); } /* Helper function due to a CNI limitation. */ private static InetAddress[] allocArray (int count) { return new InetAddress [count]; } /* Helper function due to a CNI limitation. */ private static SecurityException checkConnect (String hostname) { SecurityManager s = System.getSecurityManager(); if (s == null) return null; try { s.checkConnect (hostname, -1); return null; } catch (SecurityException ex) { return ex; } } /** * Returns the IP address of this object as a String. The address is in * the dotted octet notation, for example, "127.0.0.1". * * @return The IP address of this object in String form * * @since 1.0.2 */ public String getHostAddress() { StringBuffer sb = new StringBuffer(40); int len = addr.length; int i = 0; if (len == 16) { // An IPv6 address. for ( ; ; i += 2) { if (i >= 16) return sb.toString(); int x = ((addr [i] & 0xFF) << 8) | (addr [i + 1] & 0xFF); boolean empty = sb.length() == 0;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -