Brooklyn

brooklyn.util
[Java] Class NetworkUtils

java.lang.Object
  brooklyn.util.NetworkUtils

public class NetworkUtils

Field Summary
static int MAX_PORT_NUMBER

static int MIN_PORT_NUMBER

static java.util.List PRIVATE_NETWORKS

static java.util.regex.Pattern VALID_IP_ADDRESS_PATTERN

static java.lang.String VALID_IP_ADDRESS_REGEX

 
Method Summary
static int checkPortValid(java.lang.Integer port, java.lang.String errorMessage)

static void checkPortsValid(java.util.Map ports)

static Cidr cidr(java.lang.String cidr)

returns a CIDR object for the given string, e.g. "10.0.0.0/8"

static java.lang.String getAddressOfUnresolvableHostname()

returns null in a sane DNS environment, but if DNS provides a bogus address for made-up hostnames, this returns that address

static java.net.InetAddress getInetAddressWithFixedName(byte[] ip)

Gets an InetAddress using the given IP, and using that IP as the hostname (i.e. avoids any hostname resolution).

static java.net.InetAddress getInetAddressWithFixedName(java.lang.String hostnameOrIp)

Gets an InetAddress using the given hostname or IP.

static java.util.Map getLocalAddresses()

returns all local addresses

static java.net.InetAddress getLocalHost()

returns local IP address, or 127.0.0.1 if it cannot be parsed

static java.lang.String getLocalhostExternalIp()

returns the externally-facing IP address from which this host comes

static Cidr getPrivateNetwork(java.lang.String ip)

returns any well-known private network (e.g. 10.0.0.0/8 or 192.168.0.0/16) which the given IP is in, or the /32 of local address if none

static Cidr getPrivateNetwork(java.net.InetAddress address)

static boolean isPortAvailable(int port)

static boolean isPortAvailable(java.net.InetAddress localAddress, int port)

static boolean isPortValid(java.lang.Integer port)

static boolean isPrivateSubnet(java.net.InetAddress address)

Check if this is a private address, not exposed on the public Internet.

static boolean isPublicIp(java.lang.String ipAddress)

returns whether the IP is _not_ in any private subnet

static int nextAvailablePort(int port)

returns the first port available on the local machine >= the port supplied

static java.net.InetAddress resolve(java.lang.String hostname)

resolves the given hostname to an IP address, returning null if unresolvable or if the resolution is bogus (eg 169.* subnet or a "catch-all" IP resolution supplied by some miscreant DNS services)

 
Methods inherited from class java.lang.Object
java.lang.Object#wait(), java.lang.Object#wait(long), java.lang.Object#wait(long, int), java.lang.Object#equals(java.lang.Object), java.lang.Object#toString(), java.lang.Object#hashCode(), java.lang.Object#getClass(), java.lang.Object#notify(), java.lang.Object#notifyAll()
 

Field Detail

MAX_PORT_NUMBER

public static final int MAX_PORT_NUMBER


MIN_PORT_NUMBER

public static final int MIN_PORT_NUMBER


PRIVATE_NETWORKS

public static final java.util.List PRIVATE_NETWORKS


VALID_IP_ADDRESS_PATTERN

public static final java.util.regex.Pattern VALID_IP_ADDRESS_PATTERN


VALID_IP_ADDRESS_REGEX

public static final java.lang.String VALID_IP_ADDRESS_REGEX


 
Method Detail

checkPortValid

public static int checkPortValid(java.lang.Integer port, java.lang.String errorMessage)


checkPortsValid

public static void checkPortsValid(@SuppressWarnings("rawtypes") java.util.Map ports)


cidr

public static Cidr cidr(java.lang.String cidr)
returns a CIDR object for the given string, e.g. "10.0.0.0/8"


getAddressOfUnresolvableHostname

public static java.lang.String getAddressOfUnresolvableHostname()
returns null in a sane DNS environment, but if DNS provides a bogus address for made-up hostnames, this returns that address


getInetAddressWithFixedName

public static java.net.InetAddress getInetAddressWithFixedName(byte[] ip)
Gets an InetAddress using the given IP, and using that IP as the hostname (i.e. avoids any hostname resolution).

This is very useful if using the InetAddress for updating config files on remote machines, because then it will not be pickup a hostname from the local /etc/hosts file, which might not be known on the remote machine.


getInetAddressWithFixedName

public static java.net.InetAddress getInetAddressWithFixedName(java.lang.String hostnameOrIp)
Gets an InetAddress using the given hostname or IP. If it is an IPv4 address, then this is equivalent to getInetAddressWithFixedName(byte[]). If it is a hostname, then this hostname will be used in the returned InetAddress.


getLocalAddresses

public static java.util.Map getLocalAddresses()
returns all local addresses


getLocalHost

public static java.net.InetAddress getLocalHost()
returns local IP address, or 127.0.0.1 if it cannot be parsed


getLocalhostExternalIp

public static java.lang.String getLocalhostExternalIp()
returns the externally-facing IP address from which this host comes


getPrivateNetwork

public static Cidr getPrivateNetwork(java.lang.String ip)
returns any well-known private network (e.g. 10.0.0.0/8 or 192.168.0.0/16) which the given IP is in, or the /32 of local address if none


getPrivateNetwork

public static Cidr getPrivateNetwork(java.net.InetAddress address)


isPortAvailable

public static boolean isPortAvailable(int port)


isPortAvailable

public static boolean isPortAvailable(java.net.InetAddress localAddress, int port)


isPortValid

public static boolean isPortValid(java.lang.Integer port)


isPrivateSubnet

public static boolean isPrivateSubnet(java.net.InetAddress address)
Check if this is a private address, not exposed on the public Internet. For IPV4 addresses this is an RFC1918 subnet address ({code 10.0.0.0/8}, 172.16.0.0/12 and 192.168.0.0/16), a link-local address (169.254.0.0/16) or a loopback address (127.0.0.1/0).

For IPV6 addresses this is the RFC3514 link local block (fe80::/10) and site local block (feco::/10) or the loopback block (::1/128).

Returns:
true if the address is private


isPublicIp

public static boolean isPublicIp(java.lang.String ipAddress)
returns whether the IP is _not_ in any private subnet


nextAvailablePort

public static int nextAvailablePort(int port)
returns the first port available on the local machine >= the port supplied


resolve

public static java.net.InetAddress resolve(java.lang.String hostname)
resolves the given hostname to an IP address, returning null if unresolvable or if the resolution is bogus (eg 169.* subnet or a "catch-all" IP resolution supplied by some miscreant DNS services)


 

Brooklyn Multi-Cloud Application Management Platform
brooklyncentral.github.com. Apache License. © 2012.