Package org.onlab.packet
Class IpAddress
- java.lang.Object
-
- org.onlab.packet.IpAddress
-
- All Implemented Interfaces:
java.lang.Comparable<IpAddress>
- Direct Known Subclasses:
Ip4Address
,Ip6Address
public class IpAddress extends java.lang.Object implements java.lang.Comparable<IpAddress>
A class representing an IP address. This class is immutable.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
IpAddress.Version
-
Field Summary
Fields Modifier and Type Field Description static int
INET_BIT_LENGTH
static int
INET_BYTE_LENGTH
static int
INET6_BIT_LENGTH
static int
INET6_BYTE_LENGTH
-
Constructor Summary
Constructors Modifier Constructor Description protected
IpAddress()
Default constructor for Kryo serialization.protected
IpAddress(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 int
byteLength(IpAddress.Version version)
Computes the IP address byte length for a given IP version.int
compareTo(IpAddress o)
boolean
equals(java.lang.Object obj)
Ip4Address
getIp4Address()
Gets theIp4Address
view of the IP address.Ip6Address
getIp6Address()
Gets theIp6Address
view of the IP address.int
hashCode()
boolean
isIp4()
Tests whether the IP version of this address is IPv4.boolean
isIp6()
Tests whether the IP version of this address is IPv6.boolean
isLinkLocal()
Check if this IP address is a link-local address.boolean
isMulticast()
Check if this IP address is a multicast address.boolean
isSelfAssigned()
Check if this IP address is self-assigned.boolean
isZero()
Check if this IP address is zero.static IpAddress
makeMaskedAddress(IpAddress address, int prefixLength)
Creates an IP address by masking it with a network mask of given mask length.static IpAddress
makeMaskPrefix(IpAddress.Version version, int prefixLength)
Creates an IP network mask prefix.java.net.InetAddress
toInetAddress()
Returns the IP address as InetAddress.IpPrefix
toIpPrefix()
Generates an IP prefix.byte[]
toOctets()
Returns the IP address as a byte array.java.lang.String
toString()
static IpAddress
valueOf(int value)
Converts an integer into an IPv4 address.static IpAddress
valueOf(java.lang.String value)
Converts an IPv4 or IPv6 string literal (e.g., "10.2.3.4" or "1111:2222::8888") into an IP address.static IpAddress
valueOf(java.net.InetAddress inetAddress)
Converts an InetAddress into an IP address.static IpAddress
valueOf(IpAddress.Version version, byte[] value)
Converts a byte array into an IP address.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.IpAddress.Version
version()
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:
java.lang.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 theIp4Address
view of the IP address.- Returns:
- the
Ip4Address
view of the IP address if it is IPv4, otherwise null
-
getIp6Address
public Ip6Address getIp6Address()
Gets theIp6Address
view of the IP address.- Returns:
- the
Ip6Address
view 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 java.net.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:
java.lang.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:
java.lang.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:
java.lang.IllegalArgumentException
- if the arguments are invalid
-
valueOf
public static IpAddress valueOf(java.net.InetAddress inetAddress)
Converts an InetAddress into an IP address.- Parameters:
inetAddress
- the InetAddress value to use- Returns:
- an IP address
- Throws:
java.lang.IllegalArgumentException
- if the argument is invalid
-
valueOf
public static IpAddress valueOf(java.lang.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:
java.lang.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:
java.lang.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:
java.lang.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:
compareTo
in interfacejava.lang.Comparable<IpAddress>
-
hashCode
public int hashCode()
- Overrides:
hashCode
in classjava.lang.Object
-
equals
public boolean equals(java.lang.Object obj)
- Overrides:
equals
in classjava.lang.Object
-
toString
public java.lang.String toString()
- Overrides:
toString
in classjava.lang.Object
-
toIpPrefix
public IpPrefix toIpPrefix()
Generates an IP prefix.- Returns:
- the IP prefix of the IP address
-
-