📄 inetaddress.java
字号:
/* * @(#)InetAddress.java 1.89 06/10/10 * * Copyright 1990-2008 Sun Microsystems, Inc. All Rights Reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License version * 2 only, as published by the Free Software Foundation. * * This program 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 at /legal/license.txt). * * 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 Sun Microsystems, Inc., 4150 Network Circle, Santa * Clara, CA 95054 or visit www.sun.com if you need additional * information or have any questions. * */package java.net;import java.util.HashMap;import java.util.LinkedHashMap;import java.util.Random;import java.util.Iterator;import java.util.LinkedList;import java.security.AccessController;import java.io.ObjectStreamException;import sun.security.action.*;import sun.net.InetAddressCachePolicy;import sun.misc.Service;import sun.net.spi.nameservice.*;/** * This class represents an Internet Protocol (IP) address. * * <p> An IP address is either a 32-bit or 128-bit unsigned number * used by IP, a lower-level protocol on which protocols like UDP and * TCP are built. The IP address architecture is defined by <a * href="http://www.ietf.org/rfc/rfc790.txt"><i>RFC 790: * Assigned Numbers</i></a>, <a * href="http://www.ietf.org/rfc/rfc1918.txt"> <i>RFC 1918: * Address Allocation for Private Internets</i></a>, <a * href="http://www.ietf.org/rfc/rfc2365.txt"><i>RFC 2365: * Administratively Scoped IP Multicast</i></a>, and <a * href="http://www.ietf.org/rfc/rfc2373.txt"><i>RFC 2373: IP * Version 6 Addressing Architecture</i></a>. An instance of an * InetAddress consists of an IP address and possibly its * corresponding host name (depending on whether it is constructed * with a host name or whether it has already done reverse host name * resolution). * * <h4> Address types </h4> * * <blockquote><table cellspacing=2 summary="Description of unicast and multicast address types"> * <tr><th valign=top><i>unicast</i></th> * <td>An identifier for a single interface. A packet sent to * a unicast address is delivered to the interface identified by * that address. * * <p> The Unspecified Address -- Also called anylocal or wildcard * address. It must never be assigned to any node. It indicates the * absence of an address. One example of its use is as the target of * bind, which allows a server to accept a client connection on any * interface, in case the server host has multiple interfaces. * * <p> The <i>unspecified</i> address must not be used as * the destination address of an IP packet. * * <p> The <i>Loopback</i> Addresses -- This is the address * assigned to the loopback interface. Anything sent to this * IP address loops around and becomes IP input on the local * host. This address is often used when testing a * client.</td></tr> * <tr><th valign=top><i>multicast</i></th> * <td>An identifier for a set of interfaces (typically belonging * to different nodes). A packet sent to a multicast address is * delivered to all interfaces identified by that address.</td></tr> * </table></blockquote> * * <h4> IP address scope </h4> * * <p> <i>Link-local</i> addresses are designed to be used for addressing * on a single link for purposes such as auto-address configuration, * neighbor discovery, or when no routers are present. * * <p> <i>Site-local</i> addresses are designed to be used for addressing * inside of a site without the need for a global prefix. * * <p> <i>Global</i> addresses are unique across the internet. * * <h4> Textual representation of IP addresses </h4> * * The textual representation of an IP address is address family specific. * * <p> * * For IPv4 address format, please refer to <A * HREF="Inet4Address.html#format">Inet4Address#format</A>; For IPv6 * address format, please refer to <A * HREF="Inet6Address.html#format">Inet6Address#format</A>. * * <h4> Host Name Resolution </h4> * * Host name-to-IP address <i>resolution</i> is accomplished through * the use of a combination of local machine configuration information * and network naming services such as the Domain Name System (DNS) * and Network Information Service(NIS). The particular naming * services(s) being used is by default the local machine configured * one. For any host name, its corresponding IP address is returned. * * <p> <i>Reverse name resolution</i> means that for any IP address, * the host associated with the IP address is returned. * * <p> The InetAddress class provides methods to resolve host names to * their IP addresses and vise versa. * * <h4> InetAddress Caching </h4> * * The InetAddress class has a cache to store successful as well as * unsuccessful host name resolutions. The positive caching is there * to guard against DNS spoofing attacks; while the negative caching * is used to improve performance. * * <p> By default, the result of positive host name resolutions are * cached forever, because there is no general rule to decide when it * is safe to remove cache entries. The result of unsuccessful host * name resolution is cached for a very short period of time (10 * seconds) to improve performance. * * <p> Under certain circumstances where it can be determined that DNS * spoofing attacks are not possible, a Java security property can be * set to a different Time-to-live (TTL) value for positive * caching. Likewise, a system admin can configure a different * negative caching TTL value when needed. * * <p> Two Java security properties control the TTL values used for * positive and negative host name resolution caching: * * <blockquote> * <dl> * <dt><b>networkaddress.cache.ttl</b> (default: -1)</dt> * <dd>Indicates the caching policy for successful name lookups from * the name service. The value is specified as as integer to indicate * the number of seconds to cache the successful lookup. * <p> * A value of -1 indicates "cache forever". * </dt><p> * <p> * <dt><b>networkaddress.cache.negative.ttl</b> (default: 10)</dt> * <dd>Indicates the caching policy for un-successful name lookups * from the name service. The value is specified as as integer to * indicate the number of seconds to cache the failure for * un-successful lookups. * <p> * A value of 0 indicates "never cache". * A value of -1 indicates "cache forever". * </dd> * </dl> * </blockquote> * * @author Chris Warth * @version 1.89, 10/10/06 * @see java.net.InetAddress#getByAddress(byte[]) * @see java.net.InetAddress#getByAddress(java.lang.String, byte[]) * @see java.net.InetAddress#getAllByName(java.lang.String) * @see java.net.InetAddress#getByName(java.lang.String) * @see java.net.InetAddress#getLocalHost() * @since JDK1.0 */publicclass InetAddress implements java.io.Serializable { /** * Specify the address family: Internet Protocol, Version 4 * @since 1.4 */ static final int IPv4 = 1; /** * Specify the address family: Internet Protocol, Version 6 * @since 1.4 */ static final int IPv6 = 2; /* Specify address family preference */ static transient boolean preferIPv6Address = false; /** * @serial */ String hostName; /** * Holds a 32-bit IPv4 address. * * @serial */ int address; /** * Specifies the address family type, for instance, '1' for IPv4 * addresses, and '2' for IPv6 addresses. * * @serial */ int family; /* Used to store the name service provider */ private static NameService nameService = null; /* Used to store the best available hostname */ private transient String canonicalHostName = null; /** use serialVersionUID from JDK 1.0.2 for interoperability */ private static final long serialVersionUID = 3286316764910316507L; /* * Load net library into runtime, and perform initializations. */ static { preferIPv6Address = ((Boolean)java.security.AccessController.doPrivileged( new GetBooleanAction("java.net.preferIPv6Addresses"))).booleanValue(); AccessController.doPrivileged(new LoadLibraryAction("net")); /* * Wrap the init() method in a privileged block so that it * can read system properties. In j2se1.4.2 this is done * in the InetAddress OnLoad() function & this is executed * by the system class loader. */ java.security.AccessController.doPrivileged( new java.security.PrivilegedAction() { public Object run() { init(); return null; } } ); } /** * Constructor for the Socket.accept() method. * This creates an empty InetAddress, which is filled in by * the accept() method. This InetAddress, however, is not * put in the address cache, since it is not created by name. */ InetAddress() { } /** * Replaces the de-serialized object with an Inet4Address object. * * @return the alternate object to the de-serialized object. * * @throws ObjectStreamException if a new object replacing this * object could not be created */ private Object readResolve() throws ObjectStreamException { // will replace the deserialized 'this' object return new Inet4Address(this.hostName, this.address); } /** * Utility routine to check if the InetAddress is an * IP multicast address. * @return a <code>boolean</code> indicating if the InetAddress is * an IP multicast address * @since JDK1.1 */ public boolean isMulticastAddress() { return false; } /** * Utility routine to check if the InetAddress in a wildcard address. * @return a <code>boolean</code> indicating if the Inetaddress is * a wildcard address. * @since 1.4 */ public boolean isAnyLocalAddress() { return false; } /** * Utility routine to check if the InetAddress is a loopback address. * * @return a <code>boolean</code> indicating if the InetAddress is * a loopback address; or false otherwise. * @since 1.4 */ public boolean isLoopbackAddress() { return false; } /** * Utility routine to check if the InetAddress is an link local address. * * @return a <code>boolean</code> indicating if the InetAddress is * a link local address; or false if address is not a link local unicast address. * @since 1.4 */ public boolean isLinkLocalAddress() { return false; } /** * Utility routine to check if the InetAddress is a site local address. * * @return a <code>boolean</code> indicating if the InetAddress is * a site local address; or false if address is not a site local unicast address. * @since 1.4 */ public boolean isSiteLocalAddress() { return false; } /** * Utility routine to check if the multicast address has global scope. * * @return a <code>boolean</code> indicating if the address has * is a multicast address of global scope, false if it is not * of global scope or it is not a multicast address * @since 1.4 */ public boolean isMCGlobal() { return false; } /** * Utility routine to check if the multicast address has node scope. * * @return a <code>boolean</code> indicating if the address has * is a multicast address of node-local scope, false if it is not * of node-local scope or it is not a multicast address * @since 1.4 */ public boolean isMCNodeLocal() { return false; } /** * Utility routine to check if the multicast address has link scope. * * @return a <code>boolean</code> indicating if the address has * is a multicast address of link-local scope, false if it is not * of link-local scope or it is not a multicast address * @since 1.4 */ public boolean isMCLinkLocal() { return false; } /** * Utility routine to check if the multicast address has site scope. * * @return a <code>boolean</code> indicating if the address has * is a multicast address of site-local scope, false if it is not * of site-local scope or it is not a multicast address * @since 1.4 */ public boolean isMCSiteLocal() { return false; } /** * Utility routine to check if the multicast address has organization scope. * * @return a <code>boolean</code> indicating if the address has * is a multicast address of organization-local scope, * false if it is not of organization-local scope * or it is not a multicast address * @since 1.4 */ public boolean isMCOrgLocal() { return false; } /** * Gets the host name for this IP address. * * <p>If this InetAddress was created with a host name, * this host name will be remembered and returned; * otherwise, a reverse name lookup will be performed * and the result will be returned based on the system * configured name lookup service. If a lookup of the name service * is required, call * {@link #getCanonicalHostName() getCanonicalHostName}. * * <p>If there is a security manager, its * <code>checkConnect</code> method is first called * with the hostname and <code>-1</code> * as its arguments to see if the operation is allowed. * If the operation is not allowed, it will return * the textual representation of the IP address. * * @return the host name for this IP address, or if the operation * is not allowed by the security check, the textual * representation of the IP address. * * @see InetAddress#getCanonicalHostName * @see SecurityManager#checkConnect */ public String getHostName() { return getHostName(true); } /** * Returns the hostname for this address. * If the host is equal to null, then this address refers to any * of the local machine's available network addresses. * this is package private so SocketPermission can make calls into * here without a security check. * * <p>If there is a security manager, this method first * calls its <code>checkConnect</code> method * with the hostname and <code>-1</code> * as its arguments to see if the calling code is allowed to know * the hostname for this IP address, i.e., to connect to the host. * If the operation is not allowed, it will return * the textual representation of the IP address. * * @return the host name for this IP address, or if the operation * is not allowed by the security check, the textual * representation of the IP address. * * @param check make security check if true * * @see SecurityManager#checkConnect */ String getHostName(boolean check) { if (hostName == null) { hostName = InetAddress.getHostFromNameService(this, check); }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -