public class Networking
extends java.lang.Object
Modifier and Type | Field and Description |
---|---|
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<Cidr> |
PRIVATE_NETWORKS |
static java.util.regex.Pattern |
VALID_IP_ADDRESS_PATTERN |
static java.lang.String |
VALID_IP_ADDRESS_REGEX |
Constructor and Description |
---|
Networking() |
Modifier and Type | Method and Description |
---|---|
static byte[] |
asByteArray(long... bytes)
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.
|
static void |
checkPortsValid(java.util.Map<?,?> ports) |
static int |
checkPortValid(java.lang.Integer port,
java.lang.String errorMessage) |
static Cidr |
cidr(java.lang.String cidr)
returns a CIDR object for the given string, e.g.
|
static void |
closeQuietly(java.net.DatagramSocket s) |
static void |
closeQuietly(java.net.ServerSocket s) |
static void |
closeQuietly(java.net.Socket s) |
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.
|
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) |
static java.net.InetAddress |
getInetAddressWithFixedName(java.lang.String hostnameOrIp)
Gets an InetAddress using the given hostname or IP.
|
static java.util.Map<java.lang.String,java.net.InetAddress> |
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.net.InetAddress address) |
static Cidr |
getPrivateNetwork(java.lang.String ip)
returns any well-known private network (e.g.
|
static void |
init()
does nothing, but forces the class to be loaded and do static initialization
|
static void |
installTlsForHttpsIfAppropriate() |
static void |
installTlsOnlyForHttpsForcing()
force use of TLSv1, fixing:
http://stackoverflow.com/questions/9828414/receiving-sslhandshakeexception-handshake-failure-despite-my-client-ignoring-al
|
static boolean |
isLocalhost(java.lang.String remoteAddress)
returns true if the supplied string matches any known IP (v4 or v6) for this machine,
or if it can be resolved to any such address
|
static boolean |
isLocalOnly(java.net.InetAddress address)
Check whether this address is definitely not going to be usable on any other machine;
i.e.
|
static boolean |
isLocalOnly(java.lang.String hostnameOrIp)
As
isLocalOnly(InetAddress) but taking a string;
does not require the string to be resolvable, and generally treats non-resolvable hostnames as NOT local-only
(although they are treated as private by isPrivateSubnet(String) ),
although certain well-known hostnames are recognised as local-only |
static boolean |
isPortAvailable(java.net.InetAddress localAddress,
int port) |
static boolean |
isPortAvailable(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)
returns whether the IP is _not_ in any private subnet
|
static boolean |
isReachable(com.google.common.net.HostAndPort endpoint) |
static boolean |
isValidIp4(java.lang.String input)
checks whether given string matches a valid numeric IP (v4) address, e.g.
|
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)
|
public static final int MIN_PORT_NUMBER
public static final int MAX_PORT_NUMBER
public static final java.lang.String VALID_IP_ADDRESS_REGEX
public static final java.util.regex.Pattern VALID_IP_ADDRESS_PATTERN
public static final java.util.List<Cidr> PRIVATE_NETWORKS
public static java.net.InetAddress ANY_NIC
public static java.net.InetAddress LOOPBACK
public static boolean isPortAvailable(int port)
public static boolean isPortAvailable(java.net.InetAddress localAddress, int port)
public static int nextAvailablePort(int port)
public static boolean isPortValid(java.lang.Integer port)
public static int checkPortValid(java.lang.Integer port, java.lang.String errorMessage)
public static void checkPortsValid(java.util.Map<?,?> ports)
public static boolean isPrivateSubnet(java.net.InetAddress 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
).
public static boolean isLocalOnly(java.net.InetAddress address)
public static boolean isLocalOnly(java.lang.String hostnameOrIp)
isLocalOnly(InetAddress)
but taking a string;
does not require the string to be resolvable, and generally treats non-resolvable hostnames as NOT local-only
(although they are treated as private by isPrivateSubnet(String)
),
although certain well-known hostnames are recognised as local-only
note however InetAddress.getByName(String)
can ignore settings in /etc/hosts, on OS X at least,
and give different values than the system
public static boolean isPrivateSubnet(java.lang.String hostnameOrIp)
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-onlypublic static java.lang.String getAddressOfUnresolvableHostname()
public static java.net.InetAddress resolve(java.lang.String hostname)
public static java.net.InetAddress getInetAddressWithFixedName(byte[] ip)
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.
public static java.net.InetAddress getInetAddressWithFixedName(int ip1, int ip2, int ip3, int ip4)
public static java.net.InetAddress getInetAddressWithFixedName(int ip1, int ip2, int ip3, int ip4, int ip5, int ip6)
public static byte[] asByteArray(long... bytes)
public static boolean isValidIp4(java.lang.String input)
public static java.net.InetAddress getInetAddressWithFixedName(java.lang.String hostnameOrIp)
getInetAddressWithFixedName(byte[])
. If it is a hostname, then this hostname will be used
in the returned InetAddress.public static java.net.InetAddress getLocalHost()
public static java.util.Map<java.lang.String,java.net.InetAddress> getLocalAddresses()
public static Cidr cidr(java.lang.String cidr)
public static Cidr getPrivateNetwork(java.lang.String ip)
public static Cidr getPrivateNetwork(java.net.InetAddress address)
public static boolean isPublicIp(java.lang.String ipAddress)
public static boolean isLocalhost(java.lang.String remoteAddress)
public static boolean isReachable(com.google.common.net.HostAndPort endpoint)
public static void closeQuietly(java.net.Socket s)
public static void closeQuietly(java.net.ServerSocket s)
public static void closeQuietly(java.net.DatagramSocket s)
public static void installTlsOnlyForHttpsForcing()
public static void installTlsForHttpsIfAppropriate()
public static void init()