Package org.onlab.packet
Class IpAddress
- java.lang.Object
-
- org.onlab.packet.IpAddress
-
- All Implemented Interfaces:
Comparable<IpAddress>
- Direct Known Subclasses:
Ip4Address,Ip6Address
public class IpAddress extends Object implements Comparable<IpAddress>
A class representing an IP address. This class is immutable.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classIpAddress.Version
-
Field Summary
Fields Modifier and Type Field Description static intINET_BIT_LENGTHstatic intINET_BYTE_LENGTHstatic intINET6_BIT_LENGTHstatic intINET6_BYTE_LENGTH
-
Constructor Summary
Constructors Modifier Constructor Description protectedIpAddress()Default constructor for Kryo serialization.protectedIpAddress(IpAddress.Version version, byte[] value)Constructor for given IP address version and address octets.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description static intbyteLength(IpAddress.Version version)Computes the IP address byte length for a given IP version.intcompareTo(IpAddress o)booleanequals(Object obj)Ip4AddressgetIp4Address()Gets theIp4Addressview of the IP address.Ip6AddressgetIp6Address()Gets theIp6Addressview of the IP address.inthashCode()booleanisIp4()Tests whether the IP version of this address is IPv4.booleanisIp6()Tests whether the IP version of this address is IPv6.booleanisLinkLocal()Check if this IP address is a link-local address.booleanisMulticast()Check if this IP address is a multicast address.booleanisSelfAssigned()Check if this IP address is self-assigned.booleanisZero()Check if this IP address is zero.static IpAddressmakeMaskedAddress(IpAddress address, int prefixLength)Creates an IP address by masking it with a network mask of given mask length.static IpAddressmakeMaskPrefix(IpAddress.Version version, int prefixLength)Creates an IP network mask prefix.InetAddresstoInetAddress()Returns the IP address as InetAddress.IpPrefixtoIpPrefix()Generates an IP prefix.byte[]toOctets()Returns the IP address as a byte array.StringtoString()static IpAddressvalueOf(int value)Converts an integer into an IPv4 address.static IpAddressvalueOf(String value)Converts an IPv4 or IPv6 string literal (e.g., "10.2.3.4" or "1111:2222::8888") into an IP address.static IpAddressvalueOf(InetAddress inetAddress)Converts an InetAddress into an IP address.static IpAddressvalueOf(IpAddress.Version version, byte[] value)Converts a byte array into an IP address.static IpAddressvalueOf(IpAddress.Version version, byte[] value, int offset)Converts a byte array and a given offset from the beginning of the array into an IP address.IpAddress.Versionversion()Returns the IP version of this address.
-
-
-
Field Detail
-
INET_BYTE_LENGTH
public static final int INET_BYTE_LENGTH
- See Also:
- Constant Field Values
-
INET_BIT_LENGTH
public static final int INET_BIT_LENGTH
- See Also:
- Constant Field Values
-
INET6_BYTE_LENGTH
public static final int INET6_BYTE_LENGTH
- See Also:
- Constant Field Values
-
INET6_BIT_LENGTH
public static final int INET6_BIT_LENGTH
- See Also:
- Constant Field Values
-
-
Constructor Detail
-
IpAddress
protected IpAddress(IpAddress.Version version, byte[] value)
Constructor for given IP address version and address octets.- Parameters:
version- the IP address versionvalue- the IP address value stored in network byte order (i.e., the most significant byte first)- Throws:
IllegalArgumentException- if the arguments are invalid
-
IpAddress
protected IpAddress()
Default constructor for Kryo serialization.
-
-
Method Detail
-
version
public IpAddress.Version version()
Returns the IP version of this address.- Returns:
- the version
-
isIp4
public boolean isIp4()
Tests whether the IP version of this address is IPv4.- Returns:
- true if the IP version of this address is IPv4, otherwise false.
-
isIp6
public boolean isIp6()
Tests whether the IP version of this address is IPv6.- Returns:
- true if the IP version of this address is IPv6, otherwise false.
-
getIp4Address
public Ip4Address getIp4Address()
Gets theIp4Addressview of the IP address.- Returns:
- the
Ip4Addressview of the IP address if it is IPv4, otherwise null
-
getIp6Address
public Ip6Address getIp6Address()
Gets theIp6Addressview of the IP address.- Returns:
- the
Ip6Addressview of the IP address if it is IPv6, otherwise null
-
toOctets
public byte[] toOctets()
Returns the IP address as a byte array.- Returns:
- a byte array
-
toInetAddress
public InetAddress toInetAddress()
Returns the IP address as InetAddress.- Returns:
- InetAddress
-
byteLength
public static int byteLength(IpAddress.Version version)
Computes the IP address byte length for a given IP version.- Parameters:
version- the IP version- Returns:
- the IP address byte length for the IP version
- Throws:
IllegalArgumentException- if the IP version is invalid
-
valueOf
public static IpAddress valueOf(int value)
Converts an integer into an IPv4 address.- Parameters:
value- an integer representing an IPv4 address value- Returns:
- an IP address
-
valueOf
public static IpAddress valueOf(IpAddress.Version version, byte[] value)
Converts a byte array into an IP address.- Parameters:
version- the IP address versionvalue- the IP address value stored in network byte order (i.e., the most significant byte first)- Returns:
- an IP address
- Throws:
IllegalArgumentException- if the arguments are invalid
-
valueOf
public static IpAddress valueOf(IpAddress.Version version, byte[] value, int offset)
Converts a byte array and a given offset from the beginning of the array into an IP address.The IP address is stored in network byte order (i.e., the most significant byte first).
- Parameters:
version- the IP address versionvalue- the value to useoffset- the offset in bytes from the beginning of the byte array- Returns:
- an IP address
- Throws:
IllegalArgumentException- if the arguments are invalid
-
valueOf
public static IpAddress valueOf(InetAddress inetAddress)
Converts an InetAddress into an IP address.- Parameters:
inetAddress- the InetAddress value to use- Returns:
- an IP address
- Throws:
IllegalArgumentException- if the argument is invalid
-
valueOf
public static IpAddress valueOf(String value)
Converts an IPv4 or IPv6 string literal (e.g., "10.2.3.4" or "1111:2222::8888") into an IP address.- Parameters:
value- an IP address value in string form- Returns:
- an IP address
- Throws:
IllegalArgumentException- if the argument is invalid
-
makeMaskPrefix
public static IpAddress makeMaskPrefix(IpAddress.Version version, int prefixLength)
Creates an IP network mask prefix.- Parameters:
version- the IP address versionprefixLength- the length of the mask prefix. Must be in the interval [0, 32] for IPv4, or [0, 128] for IPv6- Returns:
- a new IP address that contains a mask prefix of the specified length
- Throws:
IllegalArgumentException- if the arguments are invalid
-
makeMaskedAddress
public static IpAddress makeMaskedAddress(IpAddress address, int prefixLength)
Creates an IP address by masking it with a network mask of given mask length.- Parameters:
address- the address to maskprefixLength- the length of the mask prefix. Must be in the interval [0, 32] for IPv4, or [0, 128] for IPv6- Returns:
- a new IP address that is masked with a mask prefix of the specified length
- Throws:
IllegalArgumentException- if the prefix length is invalid
-
isZero
public boolean isZero()
Check if this IP address is zero.- Returns:
- true if this address is zero
-
isSelfAssigned
public boolean isSelfAssigned()
Check if this IP address is self-assigned.- Returns:
- true if this address is self-assigned
-
isMulticast
public boolean isMulticast()
Check if this IP address is a multicast address.- Returns:
- true if this address is a multicast address
-
isLinkLocal
public boolean isLinkLocal()
Check if this IP address is a link-local address.- Returns:
- true if this address is a link-local address
-
compareTo
public int compareTo(IpAddress o)
- Specified by:
compareToin interfaceComparable<IpAddress>
-
toIpPrefix
public IpPrefix toIpPrefix()
Generates an IP prefix.- Returns:
- the IP prefix of the IP address
-
-