public class IpHelper extends Object
Modifier and Type | Field | Description |
---|---|---|
static Inet4Address |
BROADCAST_ADDRESS |
The IPv4 broadcast address:
255.255.255.255 |
static Inet6Address |
IPV6_ZERO |
The IPv6 equivalent of the "Quad Zero" IPv4 address (0.0.0.0).
|
static Inet4Address |
QUAD_ZERO |
The "Quad Zero" IPv4 address:
0.0.0.0 |
Modifier and Type | Method | Description |
---|---|---|
static int |
aton(byte[] addr) |
Converts an InetAddress' byte[] representation to a numeric address
This only works for IPv4 (obviously) |
static int |
aton(String ip) |
An equivalent of the C
inet_aton function |
static int |
aton(Inet4Address ip) |
Converts an InetAddress to a numeric address
|
static int |
aton(InetAddress ip) |
Converts an InetAddress to a numeric address only in the case of IPv4 (Inet4Address) addresses.
|
static NetworkInterface |
getInterfaceForLocalIp(InetAddress addr) |
Given a local IP address, returns the Interface it corresponds to
|
static String |
getLocalhost() |
Gets the hostname of localhost
|
static String |
getLocalIp() |
Gets the local IP address
|
static InetAddress |
getLocalIpAddress() |
Returns the primary InetAddress of localhost
|
static InetAddress |
getLocalIpAddress(String iface) |
Returns the IP address associated with iface
|
static List<InetAddress> |
getLocalIpAddresses() |
Returns a list of local InetAddresses for this machine
|
static List<InetAddress> |
getLocalIpAddresses(boolean pruneSiteLocal) |
Returns a list of local InetAddresses for this machine
|
static List<InetAddress> |
getLocalIpAddresses(boolean pruneSiteLocal,
boolean pruneDown) |
Returns a list of local InetAddresses for this machine
|
static List<InetAddress> |
getLocalIpAddresses(String ifaceName,
boolean pruneSiteLocal) |
|
static String |
getMacFor(NetworkInterface iface) |
Given a network interface, determines its mac address
|
static String |
getMacForLocalIp(InetAddress addr) |
Given an InetAddress, determines the MAC Address (hardware address) of the corresponding interface
|
static boolean |
isLocalAddress(String addr) |
Determines if a specified host or IP refers to the local machine
|
static boolean |
isLocalAddress(InetAddress addr) |
Determines if a specified host or IP refers to the local machine
|
static boolean |
isPublicallyRoutable(InetAddress addrIP) |
Deprecated.
use isPubliclyRoutable(java.net.InetAddress)
|
static boolean |
isPubliclyRoutable(InetAddress addrIP) |
Determines whether a particular IP address is publicly routable on the internet
|
static boolean |
isValidCiscoWildcard(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(String netmask) |
Validates a netmask
|
static boolean |
isValidNetmask(InetAddress netmask) |
Validates a netmask
|
static int |
netmaskToPrefix(InetAddress netmask) |
Determines the prefix for a given netmask (ie.
|
static InetAddress |
ntoa(byte[] addr) |
|
static InetAddress |
ntoa(int address) |
Converts numeric address to an InetAddress
|
static InetAddress |
parse(String ip) |
Parses an IP address, throwing an
IllegalArgumentException (rather than an UnknownHostException ) if it is
invalid |
static 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 InetAddress |
prefixToNetmask(int prefix) |
Turns an IPv4 prefix into a netmask
|
static InetAddress |
stoa(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 Inet4Address QUAD_ZERO
0.0.0.0
public static final Inet4Address BROADCAST_ADDRESS
255.255.255.255
public static final Inet6Address IPV6_ZERO
::
public static String getLocalhost() throws RuntimeException
RuntimeException
- When we cannot determine the local machine's hostnamepublic static String getLocalIp() throws RuntimeException
RuntimeException
- On error (eg. when the local host cannot be determined)public static InetAddress getLocalIpAddress() throws RuntimeException
RuntimeException
- On any errorpublic static InetAddress getLocalIpAddress(String iface) throws RuntimeException
iface
- The interface nameRuntimeException
- On any errorpublic static List<InetAddress> getLocalIpAddresses() throws RuntimeException
RuntimeException
- If there is an error retrieving the InetAddressespublic static List<InetAddress> getLocalIpAddresses(boolean pruneSiteLocal) throws RuntimeException
pruneSiteLocal
- boolean Set to true if site local (10/8, for example) addresses should be prunedRuntimeException
- If there is an error retrieving the InetAddressespublic static List<InetAddress> getLocalIpAddresses(boolean pruneSiteLocal, boolean pruneDown) throws 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 falseRuntimeException
- If there is an error retrieving the InetAddressespublic static String getMacForLocalIp(InetAddress addr) throws SocketException, NoMacAddressException, NoInterfaceException
addr
- The IP addressSocketException
- If we couldn't get the interfaceNoMacAddressException
- If the interface doesn't have a mac address specifiedNoInterfaceException
public static String getMacFor(NetworkInterface iface) throws SocketException, NoMacAddressException
iface
- the interfaceSocketException
NoMacAddressException
- If the interface doesn't have a mac addresspublic static NetworkInterface getInterfaceForLocalIp(InetAddress addr) throws SocketException, NoInterfaceException
addr
- The addressSocketException
NoInterfaceException
- If there's no corresponding interface for the given IPpublic static String physicalAddressToString(byte[] addr)
public static List<InetAddress> getLocalIpAddresses(String ifaceName, boolean pruneSiteLocal) throws RuntimeException
RuntimeException
public static boolean isValidNetmask(String netmask)
netmask
- String A netmask to testpublic static boolean isValidNetmask(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(InetAddress netmask)
netmask
- An IP netmask (eg. 255.255.252.0)IllegalArgumentException
- if an invalid netmask is passedpublic static InetAddress prefixToNetmask(int prefix)
prefix
- public static int prefixToMask(int prefix)
IpHelper.aton(prefixToNetmask(prefix))
but
significantly faster)prefix
- public static boolean isValidCiscoWildcard(InetAddress wildcard)
wildcard
- InetAddresspublic static boolean isLocalAddress(String addr)
addr
- String The host/IPpublic static boolean isLocalAddress(InetAddress addr)
addr
- String The host/IPpublic static InetAddress stoa(String ip)
ip
- a valid IP address (IPv4 or IPv6)InetAddress.getByName
on the IP
(but without having to catch UnknownHostException)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(String ip)
inet_aton
functionip
- public static int aton(InetAddress ip)
ip
- public static int aton(Inet4Address ip)
ip
- public static int aton(byte[] addr)
addr
- public static InetAddress ntoa(byte[] addr)
public static InetAddress ntoa(int address)
address
- public static InetAddress parse(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(InetAddress addrIP)
addrIP
- public static boolean isPubliclyRoutable(InetAddress addrIP)
addrIP
- Copyright © 2018. All rights reserved.