public class IpHelper
extends java.lang.Object
| Modifier and Type | Field and Description |
|---|---|
static java.net.Inet4Address |
BROADCAST_ADDRESS
The IPv4 broadcast address:
255.255.255.255 |
static java.net.Inet6Address |
IPV6_ZERO
The IPv6 equivalent of the "Quad Zero" IPv4 address (0.0.0.0).
|
static java.net.Inet4Address |
QUAD_ZERO
The "Quad Zero" IPv4 address:
0.0.0.0 |
| Modifier and Type | Method and Description |
|---|---|
static int |
aton(byte[] addr)
Converts an InetAddress' byte[] representation to a numeric address
This only works for IPv4 (obviously) |
static int |
aton(java.net.Inet4Address ip)
Converts an InetAddress to a numeric address
|
static int |
aton(java.net.InetAddress ip)
Converts an InetAddress to a numeric address only in the case of IPv4 (Inet4Address) addresses.
|
static int |
aton(java.lang.String ip)
An equivalent of the C
inet_aton function |
static java.net.NetworkInterface |
getInterfaceForLocalIp(java.net.InetAddress addr)
Given a local IP address, returns the Interface it corresponds to
|
static java.lang.String |
getLocalhost()
Gets the hostname of localhost
|
static java.lang.String |
getLocalIp()
Gets the local IP address
|
static java.net.InetAddress |
getLocalIpAddress()
Returns the primary InetAddress of localhost
|
static java.net.InetAddress |
getLocalIpAddress(java.lang.String iface)
Returns the IP address associated with iface
|
static java.util.List<java.net.InetAddress> |
getLocalIpAddresses()
Returns a list of local InetAddresses for this machine
|
static java.util.List<java.net.InetAddress> |
getLocalIpAddresses(boolean pruneSiteLocal)
Returns a list of local InetAddresses for this machine
|
static java.util.List<java.net.InetAddress> |
getLocalIpAddresses(boolean pruneSiteLocal,
boolean pruneDown)
Returns a list of local InetAddresses for this machine
|
static java.util.List<java.net.InetAddress> |
getLocalIpAddresses(java.lang.String ifaceName,
boolean pruneSiteLocal) |
static java.lang.String |
getMacFor(java.net.NetworkInterface iface)
Given a network interface, determines its mac address
|
static java.lang.String |
getMacForLocalIp(java.net.InetAddress addr)
Given an InetAddress, determines the MAC Address (hardware address) of the corresponding interface
|
static boolean |
isLocalAddress(java.net.InetAddress addr)
Determines if a specified host or IP refers to the local machine
|
static boolean |
isLocalAddress(java.lang.String addr)
Determines if a specified host or IP refers to the local machine
|
static boolean |
isPublicallyRoutable(java.net.InetAddress addrIP)
Deprecated.
use isPubliclyRoutable(java.net.InetAddress)
|
static boolean |
isPubliclyRoutable(java.net.InetAddress addrIP)
Determines whether a particular IP address is publicly routable on the internet
|
static boolean |
isValidCiscoWildcard(java.net.InetAddress wildcard)
Determines if a specified address is a valid Cisco Wildcard (cisco's representation of a netmask)
|
static boolean |
isValidNetmask(byte[] segments)
Validates a netmask
|
static boolean |
isValidNetmask(java.net.InetAddress netmask)
Validates a netmask
|
static boolean |
isValidNetmask(java.lang.String netmask)
Validates a netmask
|
static int |
netmaskToPrefix(java.net.InetAddress netmask)
Determines the prefix for a given netmask (ie.
|
static java.net.InetAddress |
ntoa(byte[] addr) |
static java.net.InetAddress |
ntoa(int address)
Converts numeric address to an InetAddress
|
static java.net.InetAddress |
parse(java.lang.String ip)
Parses an IP address, throwing an
IllegalArgumentException (rather than an UnknownHostException) if it is
invalid |
static java.lang.String |
physicalAddressToString(byte[] addr) |
static int |
prefixToMask(int prefix)
Turns an IPv4 prefix into a numeric mask (the equivalent of running
IpHelper.aton(prefixToNetmask(prefix)) but
significantly faster) |
static java.net.InetAddress |
prefixToNetmask(int prefix)
Turns an IPv4 prefix into a netmask
|
static java.net.InetAddress |
stoa(java.lang.String ip)
Parses an IP address, returning it as an InetAddress
This method exists to prevent code which is handling valid IP addresses from having to catch UnknownHostException excessively (when there is often no choice but to totally fail out anyway) |
public static final java.net.Inet4Address QUAD_ZERO
0.0.0.0public static final java.net.Inet4Address BROADCAST_ADDRESS
255.255.255.255public static final java.net.Inet6Address IPV6_ZERO
::public static java.lang.String getLocalhost()
throws java.lang.RuntimeException
java.lang.RuntimeException - When we cannot determine the local machine's hostnamepublic static java.lang.String getLocalIp()
throws java.lang.RuntimeException
java.lang.RuntimeException - On error (eg. when the local host cannot be determined)public static java.net.InetAddress getLocalIpAddress()
throws java.lang.RuntimeException
java.lang.RuntimeException - On any errorpublic static java.net.InetAddress getLocalIpAddress(java.lang.String iface)
throws java.lang.RuntimeException
iface - The interface namejava.lang.RuntimeException - On any errorpublic static java.util.List<java.net.InetAddress> getLocalIpAddresses()
throws java.lang.RuntimeException
java.lang.RuntimeException - If there is an error retrieving the InetAddressespublic static java.util.List<java.net.InetAddress> getLocalIpAddresses(boolean pruneSiteLocal)
throws java.lang.RuntimeException
pruneSiteLocal - boolean Set to true if site local (10/8, for example) addresses should be prunedjava.lang.RuntimeException - If there is an error retrieving the InetAddressespublic static java.util.List<java.net.InetAddress> getLocalIpAddresses(boolean pruneSiteLocal,
boolean pruneDown)
throws java.lang.RuntimeException
pruneSiteLocal - boolean Set to true if site local (10/8, for example) addresses should be prunedpruneDown - boolean Set to true to prune out interfaces whose .isUp() return falsejava.lang.RuntimeException - If there is an error retrieving the InetAddressespublic static java.lang.String getMacForLocalIp(java.net.InetAddress addr)
throws java.net.SocketException,
NoMacAddressException,
NoInterfaceException
addr - The IP addressjava.net.SocketException - If we couldn't get the interfaceNoMacAddressException - If the interface doesn't have a mac address specifiedNoInterfaceExceptionpublic static java.lang.String getMacFor(java.net.NetworkInterface iface)
throws java.net.SocketException,
NoMacAddressException
iface - the interfacejava.net.SocketExceptionNoMacAddressException - If the interface doesn't have a mac addresspublic static java.net.NetworkInterface getInterfaceForLocalIp(java.net.InetAddress addr)
throws java.net.SocketException,
NoInterfaceException
addr - The addressjava.net.SocketExceptionNoInterfaceException - If there's no corresponding interface for the given IPpublic static java.lang.String physicalAddressToString(byte[] addr)
public static java.util.List<java.net.InetAddress> getLocalIpAddresses(java.lang.String ifaceName,
boolean pruneSiteLocal)
throws java.lang.RuntimeException
java.lang.RuntimeExceptionpublic static boolean isValidNetmask(java.lang.String netmask)
netmask - String A netmask to testpublic static boolean isValidNetmask(java.net.InetAddress netmask)
netmask - InetAddress A netmask to testpublic static boolean isValidNetmask(byte[] segments)
segments - byte[] A signed byte array whose binary values represent the addresspublic static int netmaskToPrefix(java.net.InetAddress netmask)
netmask - An IP netmask (eg. 255.255.252.0)java.lang.IllegalArgumentException - if an invalid netmask is passedpublic static java.net.InetAddress prefixToNetmask(int prefix)
prefix - public static int prefixToMask(int prefix)
IpHelper.aton(prefixToNetmask(prefix)) but
significantly faster)prefix - public static boolean isValidCiscoWildcard(java.net.InetAddress wildcard)
wildcard - InetAddresspublic static boolean isLocalAddress(java.lang.String addr)
addr - String The host/IPpublic static boolean isLocalAddress(java.net.InetAddress addr)
addr - String The host/IPpublic static java.net.InetAddress stoa(java.lang.String ip)
ip - a valid IP address (IPv4 or IPv6)InetAddress.getByName on the IP
(but without having to catch UnknownHostException)java.lang.IllegalArgumentException - if the IP address is invalid (eg. null, an empty string or otherwise not in the valid IP address format)public static int aton(java.lang.String ip)
inet_aton functionip - public static int aton(java.net.InetAddress ip)
ip - public static int aton(java.net.Inet4Address ip)
ip - public static int aton(byte[] addr)
addr - public static java.net.InetAddress ntoa(byte[] addr)
public static java.net.InetAddress ntoa(int address)
address - public static java.net.InetAddress parse(java.lang.String ip)
IllegalArgumentException (rather than an UnknownHostException) if it is
invalidip - the IP - must not be null or an empty string. should be a valid IP address (eg. 1.2.3.4)@Deprecated public static boolean isPublicallyRoutable(java.net.InetAddress addrIP)
addrIP - public static boolean isPubliclyRoutable(java.net.InetAddress addrIP)
addrIP - Copyright © 2014. All Rights Reserved.