Brooklyn

brooklyn.util.net
[Java] Class Networking

java.lang.Object
  brooklyn.util.net.Networking

public class Networking

Field Summary
static java.net.InetAddress ANY_NIC

static java.net.InetAddress LOOPBACK

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 byte[] asByteArray(long... bytes)

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()

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(int ip1, int ip2, int ip3, int ip4)

static java.net.InetAddress getInetAddressWithFixedName(int ip1, int ip2, int ip3, int ip4, int ip5, int ip6)

creates a byte array given a var-arg number of (or bytes or longs); checks that all values are valid as _unsigned_ bytes (i.e. in [0,255] )

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

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 Cidr getPrivateNetwork(java.lang.String ip)

static Cidr getPrivateNetwork(java.net.InetAddress address)

returns whether the IP is _not_ in any private subnet

static boolean isLocalOnly(java.net.InetAddress address)

static boolean isLocalOnly(java.lang.String hostnameOrIp)

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 isPrivateSubnet(java.lang.String hostnameOrIp)

As isPrivateSubnet(InetAddress) but taking a string; sepcifically local-only address ARE treated as private.

static boolean isPublicIp(java.lang.String ipAddress)

static boolean isValidIp4(java.lang.String input)

checks whether given string matches a valid numeric IP (v4) address, e.g. 127.0.0.1, but not localhost or 1.2.3.256

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

ANY_NIC

public static java.net.InetAddress ANY_NIC


LOOPBACK

public static java.net.InetAddress LOOPBACK


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

asByteArray

public static byte[] asByteArray(long... bytes)


checkPortValid

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


checkPortsValid

public static void checkPortsValid(Integer)) { 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()


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(int ip1, int ip2, int ip3, int ip4)


getInetAddressWithFixedName

public static java.net.InetAddress getInetAddressWithFixedName(int ip1, int ip2, int ip3, int ip4, int ip5, int ip6)
creates a byte array given a var-arg number of (or bytes or longs); checks that all values are valid as _unsigned_ bytes (i.e. in [0,255] )


getInetAddressWithFixedName

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


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


getPrivateNetwork

public static Cidr getPrivateNetwork(java.lang.String ip)


getPrivateNetwork

public static Cidr getPrivateNetwork(java.net.InetAddress address)
returns whether the IP is _not_ in any private subnet


isLocalOnly

public static boolean isLocalOnly(java.net.InetAddress address)


isLocalOnly

public static boolean isLocalOnly(java.lang.String hostnameOrIp)


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 (site local) address (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


isPrivateSubnet

public static boolean isPrivateSubnet(java.lang.String hostnameOrIp)
As isPrivateSubnet(InetAddress) but taking a string; sepcifically local-only address ARE treated as private. does not require the string to be resolvable, and things which aren't resolvable are treated as private unless they are known to be local-only


isPublicIp

public static boolean isPublicIp(java.lang.String ipAddress)


isValidIp4

public static boolean isValidIp4(java.lang.String input)
checks whether given string matches a valid numeric IP (v4) address, e.g. 127.0.0.1, but not localhost or 1.2.3.256


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.