Java Code Examples for java.net.InetAddress.isAnyLocalAddress()

The following are Jave code examples for showing how to use isAnyLocalAddress() of the java.net.InetAddress class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
Example 1
Project: Elasticsearch   File: NetworkUtils.java   Source Code and License Vote up 6 votes
/** Sorts an address by preference. This way code like publishing can just pick the first one */
static int sortKey(InetAddress address, boolean prefer_v6) {
    int key = address.getAddress().length;
    if (prefer_v6) {
        key = -key;
    }
    
    if (address.isAnyLocalAddress()) {
        key += 5;
    }
    if (address.isMulticastAddress()) {
        key += 4;
    }
    if (address.isLoopbackAddress()) {
        key += 3;
    }
    if (address.isLinkLocalAddress()) {
        key += 2;
    }
    if (address.isSiteLocalAddress()) {
        key += 1;
    }

    return key;
}
 
Example 2
Project: ditb   File: Addressing.java   Source Code and License Vote up 6 votes
/**
 * Given an InetAddress, checks to see if the address is a local address, by comparing the address
 * with all the interfaces on the node.
 * @param addr address to check if it is local node's address
 * @return true if the address corresponds to the local node
 */
public static boolean isLocalAddress(InetAddress addr) {
  // Check if the address is any local or loop back
  boolean local = addr.isAnyLocalAddress() || addr.isLoopbackAddress();

  // Check if the address is defined on any interface
  if (!local) {
    try {
      local = NetworkInterface.getByInetAddress(addr) != null;
    } catch (SocketException e) {
      local = false;
    }
  }
  return local;
}
 
Example 3
Project: dlock   File: NetUtils.java   Source Code and License Vote up 6 votes
/**
 * Retrieve the first validated local ip address(the Public and LAN ip addresses are validated).
 *
 * @return the local address
 * @throws SocketException the socket exception
 */
public static InetAddress getLocalInetAddress() throws SocketException {
    // enumerates all network interfaces
    Enumeration<NetworkInterface> enu = NetworkInterface.getNetworkInterfaces();

    while (enu.hasMoreElements()) {
        NetworkInterface ni = enu.nextElement();
        if (ni.isLoopback()) {
            continue;
        }

        Enumeration<InetAddress> addressEnumeration = ni.getInetAddresses();
        while (addressEnumeration.hasMoreElements()) {
            InetAddress address = addressEnumeration.nextElement();

            // ignores all invalidated addresses
            if (address.isLinkLocalAddress() || address.isLoopbackAddress() || address.isAnyLocalAddress()) {
                continue;
            }

            return address;
        }
    }

    throw new RuntimeException("No validated local address!");
}
 
Example 4
Project: burstcoin   File: Peers.java   Source Code and License Vote up 6 votes
static String normalizeHostAndPort(String address) {
    try {
        if (address == null) {
            return null;
        }
        URI uri = new URI("http://" + address.trim());
        String host = uri.getHost();
        if (host == null || host.equals("") || host.equals("localhost") ||
                            host.equals("127.0.0.1") || host.equals("[0:0:0:0:0:0:0:1]")) {
            return null;
        }
        InetAddress inetAddress = InetAddress.getByName(host);
        if (inetAddress.isAnyLocalAddress() || inetAddress.isLoopbackAddress() ||
                                               inetAddress.isLinkLocalAddress()) {
            return null;
        }
        int port = uri.getPort();
        return port == -1 ? host : host + ':' + port;
    } catch (URISyntaxException |UnknownHostException e) {
        return null;
    }
}
 
Example 5
Project: uid-generator   File: NetUtils.java   Source Code and License Vote up 6 votes
/**
 * Retrieve the first validated local ip address(the Public and LAN ip addresses are validated).
 *
 * @return the local address
 * @throws SocketException the socket exception
 */
public static InetAddress getLocalInetAddress() throws SocketException {
    // enumerates all network interfaces
    Enumeration<NetworkInterface> enu = NetworkInterface.getNetworkInterfaces();

    while (enu.hasMoreElements()) {
        NetworkInterface ni = enu.nextElement();
        if (ni.isLoopback()) {
            continue;
        }

        Enumeration<InetAddress> addressEnumeration = ni.getInetAddresses();
        while (addressEnumeration.hasMoreElements()) {
            InetAddress address = addressEnumeration.nextElement();

            // ignores all invalidated addresses
            if (address.isLinkLocalAddress() || address.isLoopbackAddress() || address.isAnyLocalAddress()) {
                continue;
            }

            return address;
        }
    }

    throw new RuntimeException("No validated local address!");
}
 
Example 6
Project: hadoop   File: DFSUtil.java   Source Code and License Vote up 6 votes
/**
 * Substitute a default host in the case that an address has been configured
 * with a wildcard. This is used, for example, when determining the HTTP
 * address of the NN -- if it's configured to bind to 0.0.0.0, we want to
 * substitute the hostname from the filesystem URI rather than trying to
 * connect to 0.0.0.0.
 * @param configuredAddress the address found in the configuration
 * @param defaultHost the host to substitute with, if configuredAddress
 * is a local/wildcard address.
 * @return the substituted address
 * @throws IOException if it is a wildcard address and security is enabled
 */
@VisibleForTesting
static String substituteForWildcardAddress(String configuredAddress,
  String defaultHost) throws IOException {
  InetSocketAddress sockAddr = NetUtils.createSocketAddr(configuredAddress);
  InetSocketAddress defaultSockAddr = NetUtils.createSocketAddr(defaultHost
      + ":0");
  final InetAddress addr = sockAddr.getAddress();
  if (addr != null && addr.isAnyLocalAddress()) {
    if (UserGroupInformation.isSecurityEnabled() &&
        defaultSockAddr.getAddress().isAnyLocalAddress()) {
      throw new IOException("Cannot use a wildcard address with security. " +
          "Must explicitly set bind address for Kerberos");
    }
    return defaultHost + ":" + sockAddr.getPort();
  } else {
    return configuredAddress;
  }
}
 
Example 7
Project: elasticsearch_my   File: NetworkUtils.java   Source Code and License Vote up 6 votes
/** Sorts an address by preference. This way code like publishing can just pick the first one */
static int sortKey(InetAddress address, boolean prefer_v6) {
    int key = address.getAddress().length;
    if (prefer_v6) {
        key = -key;
    }
    
    if (address.isAnyLocalAddress()) {
        key += 5;
    }
    if (address.isMulticastAddress()) {
        key += 4;
    }
    if (address.isLoopbackAddress()) {
        key += 3;
    }
    if (address.isLinkLocalAddress()) {
        key += 2;
    }
    if (address.isSiteLocalAddress()) {
        key += 1;
    }

    return key;
}
 
Example 8
Project: hadoop   File: WebAppUtils.java   Source Code and License Vote up 6 votes
private static String getResolvedAddress(InetSocketAddress address) {
  address = NetUtils.getConnectAddress(address);
  StringBuilder sb = new StringBuilder();
  InetAddress resolved = address.getAddress();
  if (resolved == null || resolved.isAnyLocalAddress() ||
      resolved.isLoopbackAddress()) {
    String lh = address.getHostName();
    try {
      lh = InetAddress.getLocalHost().getCanonicalHostName();
    } catch (UnknownHostException e) {
      //Ignore and fallback.
    }
    sb.append(lh);
  } else {
    sb.append(address.getHostName());
  }
  sb.append(":").append(address.getPort());
  return sb.toString();
}
 
Example 9
Project: hadoop   File: MiniMRYarnCluster.java   Source Code and License Vote up 5 votes
public static String getResolvedMRHistoryWebAppURLWithoutScheme(
    Configuration conf, boolean isSSLEnabled) {
  InetSocketAddress address = null;
  if (isSSLEnabled) {
    address =
        conf.getSocketAddr(JHAdminConfig.MR_HISTORY_WEBAPP_HTTPS_ADDRESS,
            JHAdminConfig.DEFAULT_MR_HISTORY_WEBAPP_HTTPS_ADDRESS,
            JHAdminConfig.DEFAULT_MR_HISTORY_WEBAPP_HTTPS_PORT);
  } else {
    address =
        conf.getSocketAddr(JHAdminConfig.MR_HISTORY_WEBAPP_ADDRESS,
            JHAdminConfig.DEFAULT_MR_HISTORY_WEBAPP_ADDRESS,
            JHAdminConfig.DEFAULT_MR_HISTORY_WEBAPP_PORT);    }
  address = NetUtils.getConnectAddress(address);
  StringBuffer sb = new StringBuffer();
  InetAddress resolved = address.getAddress();
  if (resolved == null || resolved.isAnyLocalAddress() || 
      resolved.isLoopbackAddress()) {
    String lh = address.getHostName();
    try {
      lh = InetAddress.getLocalHost().getCanonicalHostName();
    } catch (UnknownHostException e) {
      //Ignore and fallback.
    }
    sb.append(lh);
  } else {
    sb.append(address.getHostName());
  }
  sb.append(":").append(address.getPort());
  return sb.toString();
}
 
Example 10
Project: abhot   File: Util.java   Source Code and License Vote up 5 votes
private static boolean isGoodV4Address(InetAddress address)
{
	return address instanceof Inet4Address &&
			!address.isAnyLocalAddress() &&
			!address.isLoopbackAddress() &&
			!address.isMulticastAddress();
}
 
Example 11
Project: webtrekk-android-sdk   File: NanoHTTPD.java   Source Code and License Vote up 5 votes
public HTTPSession(TempFileManager tempFileManager, InputStream inputStream, OutputStream outputStream, InetAddress inetAddress) {
    this.tempFileManager = tempFileManager;
    this.inputStream = new BufferedInputStream(inputStream, HTTPSession.BUFSIZE);
    this.outputStream = outputStream;
    this.remoteIp = inetAddress.isLoopbackAddress() || inetAddress.isAnyLocalAddress() ? "127.0.0.1" : inetAddress.getHostAddress().toString();
    this.remoteHostname = inetAddress.isLoopbackAddress() || inetAddress.isAnyLocalAddress() ? "localhost" : inetAddress.getHostName().toString();
    this.headers = new HashMap<String, String>();
}
 
Example 12
Project: AgentX   File: UdpServer.java   Source Code and License Vote up 5 votes
private void initAddr() throws UnknownHostException {
    InetAddress host = InetAddress.getByName(config.getHost());
    if (!host.isAnyLocalAddress()) {
        throw new RuntimeException("<host> is not local");
    }
    udpHost = host.getHostAddress();
    udpPort = config.getPort();
    udpAddr = new InetSocketAddress(udpHost, udpPort);
}
 
Example 13
Project: hadoop-oss   File: NetUtils.java   Source Code and License Vote up 5 votes
/**
 * Given an InetAddress, checks to see if the address is a local address, by
 * comparing the address with all the interfaces on the node.
 * @param addr address to check if it is local node's address
 * @return true if the address corresponds to the local node
 */
public static boolean isLocalAddress(InetAddress addr) {
  // Check if the address is any local or loop back
  boolean local = addr.isAnyLocalAddress() || addr.isLoopbackAddress();

  // Check if the address is defined on any interface
  if (!local) {
    try {
      local = NetworkInterface.getByInetAddress(addr) != null;
    } catch (SocketException e) {
      local = false;
    }
  }
  return local;
}
 
Example 14
Project: shabdiz   File: NetworkUtil.java   Source Code and License Vote up 5 votes
/**
 * Tests whether a given address is a valid local address.
 *
 * @param address an address
 * @return true if the address is a valid local address
 */
public static boolean isValidLocalAddress(final InetAddress address) {

    boolean local = address.isAnyLocalAddress() || address.isLoopbackAddress();
    if (!local) {
        try {
            local = NetworkInterface.getByInetAddress(address) != null;
        } catch (final SocketException e) {
            local = false;
        }
    }
    return local;
}
 
Example 15
Project: alfresco-repository   File: HostConfigurableSocketFactory.java   Source Code and License Vote up 5 votes
public void setHost(String host)
{
    try
    {
        InetAddress hostAddress = InetAddress.getByName(host);
        if (!hostAddress.isAnyLocalAddress())
        {
            this.host = hostAddress;
        }
    }
    catch (UnknownHostException e)
    {
        throw new RuntimeException(e.toString());
    }
}
 
Example 16
Project: open-rmbt   File: Helperfunctions.java   Source Code and License Vote up 5 votes
public static String IpType(InetAddress inetAddress)
{
    try
    {
        final String ipVersion;
        if (inetAddress instanceof Inet4Address)
            ipVersion = "ipv4";
        else if (inetAddress instanceof Inet6Address)
            ipVersion = "ipv6";
        else
            ipVersion = "ipv?";
        
        if (inetAddress.isAnyLocalAddress())
            return "wildcard_" + ipVersion;
        if (inetAddress.isSiteLocalAddress())
            return "site_local_" + ipVersion;
        if (inetAddress.isLinkLocalAddress())
            return "link_local_" + ipVersion;
        if (inetAddress.isLoopbackAddress())
            return "loopback_" + ipVersion;
        return "public_" + ipVersion;

    }
    catch (final IllegalArgumentException e)
    {
        return "illegal_ip";
    }
}
 
Example 17
Project: open-rmbt   File: Helperfunctions.java   Source Code and License Vote up 5 votes
public static String filterIP(final InetAddress inetAddress)
{
    try
    {
        final String ipVersion;
        if (inetAddress instanceof Inet4Address)
            ipVersion = "ipv4";
        else if (inetAddress instanceof Inet6Address)
            ipVersion = "ipv6";
        else
            ipVersion = "ipv?";
        
        if (inetAddress.isAnyLocalAddress())
            return "wildcard";
        if (inetAddress.isSiteLocalAddress())
            return "site_local_" + ipVersion;
        if (inetAddress.isLinkLocalAddress())
            return "link_local_" + ipVersion;
        if (inetAddress.isLoopbackAddress())
            return "loopback_" + ipVersion;
        return inetAddress.getHostAddress();
    }
    catch (final IllegalArgumentException e)
    {
        return "illegal_ip";
    }
}
 
Example 18
Project: yacy_grid_mcp   File: Domains.java   Source Code and License Vote up 4 votes
public static boolean isThisHostIP(final InetAddress clientAddress) {
    if (clientAddress == null) return false;
    if (clientAddress.isAnyLocalAddress() || clientAddress.isLoopbackAddress()) return true;
    return myHostAddresses.contains(clientAddress); // includes localHostAddresses
}
 
Example 19
Project: BiglyBT   File: RSSGeneratorPlugin.java   Source Code and License Vote up 4 votes
public String
getURL()
{
	InetAddress bind_ip = getServerBindIP();

	String 	ip;

	if ( bind_ip.isAnyLocalAddress()){

		ip = "127.0.0.1";

	}else{

		ip = bind_ip.getHostAddress();
	}

	return( getProtocol() + "://" + ip + ":" + getPort() + "/" );
}
 
Example 20
Project: BiglyBT   File: AESocksProxyPlugableConnectionDefault.java   Source Code and License Vote up 2 votes
protected
proxyStateRelayConnect(
	InetSocketAddress	_address )

	throws IOException
{
	super( socks_connection );

	address			= _address;

		// OK, we're almost ready to roll. Unregister the read select until we're
		// connected

	connection.cancelReadSelect( source_channel );

	connection.setConnectState( this );

	target_channel = SocketChannel.open();

    InetAddress bindIP = NetworkAdmin.getSingleton().getMultiHomedOutgoingRoundRobinBindAddress(address.getAddress());

       if ( bindIP != null ){

       	try{
       		target_channel.socket().bind( new InetSocketAddress( bindIP, 0 ) );

       	}catch( IOException e ){

       		if ( bindIP.isAnyLocalAddress()){

       			// no point in moaning here about this

       		}else{

        			// if the address is unresolved then the calculated bind address can be invalid
        			// (might pick an IPv6 address for example when this is unbindable). In this case
        			// carry on and attempt to connect as this will fail anyway

        		if ( ! ( e.getMessage().contains( "not supported" ) && address.isUnresolved())){

        			throw( e );
        		}
       		}
       	}
       }

       target_channel.configureBlocking( false );

       target_channel.connect( address );

       connection.requestConnectSelect( target_channel );
}