Package io.netty.util
Class NetUtil
java.lang.Object
io.netty.util.NetUtil
A class that holds a number of network-related constants.
This class borrowed some of its methods from a modified fork of the
Inet6Util class which was part of Apache Harmony.
-
Nested Class Summary
Nested Classes -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate static final int
Maximum amount of value adding characters in between IPV4 separatorsprivate static final boolean
true
if IPv4 should be used even if the system supports both IPv4 and IPv6.private static final int
Number of separators that must be present in an IPv4 stringprivate static final boolean
true
if an IPv6 address should be preferred when a host has both an IPv4 address and an IPv6 address.private static final int
Number of bytes needed to represent an IPV6 valueprivate static final int
Maximum amount of value adding characters in between IPV6 separatorsprivate static final int
The maximum number of characters for an IPV6 string with no scopeprivate static final int
Maximum number of separators that must be present in an IPv6 stringprivate static final int
Minimum number of separators that must be present in an IPv6 stringprivate static final int
This defines how many words (represented as ints) are needed to represent an IPv6 addressstatic final InetAddress
TheInetAddress
that represents the loopback address.static final Inet4Address
TheInet4Address
that represents the IPv4 loopback address '127.0.0.1'static final Inet6Address
TheInet6Address
that represents the IPv6 loopback address '::1'private static final InternalLogger
The logger being used by this classstatic final NetworkInterface
The loopbackNetworkInterface
of the current machinestatic final Collection
<NetworkInterface> An unmodifiable Collection of all the interfaces on this machine.static final int
The SOMAXCONN value of the current machine. -
Constructor Summary
ConstructorsModifierConstructorDescriptionprivate
NetUtil()
A constructor to stop this class being constructed. -
Method Summary
Modifier and TypeMethodDescriptionstatic String
bytesToIpAddress
(byte[] bytes) Converts 4-byte or 16-byte data into an IPv4 or IPv6 string respectively.static String
bytesToIpAddress
(byte[] bytes, int offset, int length) Converts 4-byte or 16-byte data into an IPv4 or IPv6 string respectively.static byte[]
createByteArrayFromIpAddressString
(String ipAddressString) Creates an byte[] based on an ipAddressString.static InetAddress
createInetAddressFromIpAddressString
(String ipAddressString) Creates anInetAddress
based on an ipAddressString or might return null if it can't be parsed.private static int
decimalDigit
(String str, int pos) static Inet6Address
Returns theInet6Address
representation of aCharSequence
IP address.static Inet6Address
getByName
(CharSequence ip, boolean ipv4Mapped) Returns theInet6Address
representation of aCharSequence
IP address.static String
getHostname
(InetSocketAddress addr) ReturnsInetSocketAddress.getHostString()
if Java >= 7, orInetSocketAddress.getHostName()
otherwise.(package private) static byte[]
getIPv6ByName
(CharSequence ip, boolean ipv4Mapped) Returns the byte array representation of aCharSequence
IP address.private static boolean
inRangeEndExclusive
(int value, int start, int end) Does a range check onvalue
if is withinstart
(inclusive) andend
(exclusive).static String
intToIpAddress
(int i) Converts a 32-bit integer into an IPv4 address.static int
ipv4AddressToInt
(Inet4Address ipAddress) ConvertInet4Address
intoint
private static byte
ipv4WordToByte
(String ip, int from, int toExclusive) static boolean
Returnstrue
if IPv4 should be used even if the system supports both IPv4 and IPv6.static boolean
Returnstrue
if an IPv6 address should be preferred when a host has both an IPv4 address and an IPv6 address.private static boolean
isValidHexChar
(char c) private static boolean
isValidIpV4Address
(AsciiString ip, int from, int toExcluded) static boolean
Takes aCharSequence
and parses it to see if it is a valid IPV4 address.private static boolean
isValidIpV4Address
(CharSequence ip, int from, int toExcluded) static boolean
Takes aString
and parses it to see if it is a valid IPV4 address.private static boolean
isValidIpV4Address
(String ip, int from, int toExcluded) private static boolean
isValidIpV4Address0
(CharSequence ip, int from, int toExcluded) private static boolean
isValidIPv4Mapped
(byte[] bytes, int currentIndex, int compressBegin, int compressLength) private static boolean
isValidIPv4MappedChar
(char c) private static boolean
isValidIPv4MappedSeparators
(byte b0, byte b1, boolean mustBeZero) private static boolean
isValidIpV4Word
(CharSequence word, int from, int toExclusive) static boolean
static boolean
private static boolean
isValidNumericChar
(char c) private static StringBuilder
newSocketAddressStringBuilder
(String host, String port, boolean ipv4) private static Integer
sysctlGetInt
(String sysctlKey) This will execute sysctl with thesysctlKey
which is expected to return the numeric value for forsysctlKey
.private static String
toAddressString
(byte[] bytes, int offset, boolean ipv4Mapped) static String
Returns theString
representation of anInetAddress
.static String
toAddressString
(InetAddress ip, boolean ipv4Mapped) Returns theString
representation of anInetAddress
.static String
toSocketAddressString
(String host, int port) Returns theString
representation of a host port combo.static String
Returns theString
representation of anInetSocketAddress
.(package private) static byte[]
-
Field Details
-
LOCALHOST4
TheInet4Address
that represents the IPv4 loopback address '127.0.0.1' -
LOCALHOST6
TheInet6Address
that represents the IPv6 loopback address '::1' -
LOCALHOST
TheInetAddress
that represents the loopback address. If IPv6 stack is available, it will refer toLOCALHOST6
. Otherwise,LOCALHOST4
. -
LOOPBACK_IF
The loopbackNetworkInterface
of the current machine -
NETWORK_INTERFACES
An unmodifiable Collection of all the interfaces on this machine. -
SOMAXCONN
public static final int SOMAXCONNThe SOMAXCONN value of the current machine. If failed to get the value,200
is used as a default value for Windows and128
for others. -
IPV6_WORD_COUNT
private static final int IPV6_WORD_COUNTThis defines how many words (represented as ints) are needed to represent an IPv6 address- See Also:
-
IPV6_MAX_CHAR_COUNT
private static final int IPV6_MAX_CHAR_COUNTThe maximum number of characters for an IPV6 string with no scope- See Also:
-
IPV6_BYTE_COUNT
private static final int IPV6_BYTE_COUNTNumber of bytes needed to represent an IPV6 value- See Also:
-
IPV6_MAX_CHAR_BETWEEN_SEPARATOR
private static final int IPV6_MAX_CHAR_BETWEEN_SEPARATORMaximum amount of value adding characters in between IPV6 separators- See Also:
-
IPV6_MIN_SEPARATORS
private static final int IPV6_MIN_SEPARATORSMinimum number of separators that must be present in an IPv6 string- See Also:
-
IPV6_MAX_SEPARATORS
private static final int IPV6_MAX_SEPARATORSMaximum number of separators that must be present in an IPv6 string- See Also:
-
IPV4_MAX_CHAR_BETWEEN_SEPARATOR
private static final int IPV4_MAX_CHAR_BETWEEN_SEPARATORMaximum amount of value adding characters in between IPV4 separators- See Also:
-
IPV4_SEPARATORS
private static final int IPV4_SEPARATORSNumber of separators that must be present in an IPv4 string- See Also:
-
IPV4_PREFERRED
private static final boolean IPV4_PREFERREDtrue
if IPv4 should be used even if the system supports both IPv4 and IPv6. -
IPV6_ADDRESSES_PREFERRED
private static final boolean IPV6_ADDRESSES_PREFERREDtrue
if an IPv6 address should be preferred when a host has both an IPv4 address and an IPv6 address. -
logger
The logger being used by this class
-
-
Constructor Details
-
NetUtil
private NetUtil()A constructor to stop this class being constructed.
-
-
Method Details
-
sysctlGetInt
This will execute sysctl with thesysctlKey
which is expected to return the numeric value for forsysctlKey
.- Parameters:
sysctlKey
- The key which the return value corresponds to.- Returns:
- The sysctl value for
sysctlKey
. - Throws:
IOException
-
isIpV4StackPreferred
public static boolean isIpV4StackPreferred()Returnstrue
if IPv4 should be used even if the system supports both IPv4 and IPv6. Setting this property totrue
will disable IPv6 support. The default value of this property isfalse
.- See Also:
-
isIpV6AddressesPreferred
public static boolean isIpV6AddressesPreferred()Returnstrue
if an IPv6 address should be preferred when a host has both an IPv4 address and an IPv6 address. The default value of this property isfalse
.- See Also:
-
createByteArrayFromIpAddressString
Creates an byte[] based on an ipAddressString. No error handling is performed here. -
createInetAddressFromIpAddressString
Creates anInetAddress
based on an ipAddressString or might return null if it can't be parsed. No error handling is performed here. -
decimalDigit
-
ipv4WordToByte
-
validIpV4ToBytes
-
ipv4AddressToInt
ConvertInet4Address
intoint
-
intToIpAddress
Converts a 32-bit integer into an IPv4 address. -
bytesToIpAddress
Converts 4-byte or 16-byte data into an IPv4 or IPv6 string respectively.- Throws:
IllegalArgumentException
- iflength
is not4
nor16
-
bytesToIpAddress
Converts 4-byte or 16-byte data into an IPv4 or IPv6 string respectively.- Throws:
IllegalArgumentException
- iflength
is not4
nor16
-
isValidIpV6Address
-
isValidIpV6Address
-
isValidIpV4Word
-
isValidHexChar
private static boolean isValidHexChar(char c) -
isValidNumericChar
private static boolean isValidNumericChar(char c) -
isValidIPv4MappedChar
private static boolean isValidIPv4MappedChar(char c) -
isValidIPv4MappedSeparators
private static boolean isValidIPv4MappedSeparators(byte b0, byte b1, boolean mustBeZero) -
isValidIPv4Mapped
private static boolean isValidIPv4Mapped(byte[] bytes, int currentIndex, int compressBegin, int compressLength) -
isValidIpV4Address
Takes aCharSequence
and parses it to see if it is a valid IPV4 address.- Returns:
- true, if the string represents an IPV4 address in dotted notation, false otherwise
-
isValidIpV4Address
Takes aString
and parses it to see if it is a valid IPV4 address.- Returns:
- true, if the string represents an IPV4 address in dotted notation, false otherwise
-
isValidIpV4Address
-
isValidIpV4Address
-
isValidIpV4Address
-
isValidIpV4Address0
-
getByName
Returns theInet6Address
representation of aCharSequence
IP address.This method will treat all IPv4 type addresses as "IPv4 mapped" (see
getByName(CharSequence, boolean)
)- Parameters:
ip
-CharSequence
IP address to be converted to aInet6Address
- Returns:
Inet6Address
representation of theip
ornull
if not a valid IP address.
-
getByName
Returns theInet6Address
representation of aCharSequence
IP address.The
ipv4Mapped
parameter specifies how IPv4 addresses should be treated. "IPv4 mapped" format as defined in rfc 4291 section 2 is supported.- Parameters:
ip
-CharSequence
IP address to be converted to aInet6Address
ipv4Mapped
-true
To allow IPv4 mapped inputs to be translated intoInet6Address
false
Consider IPv4 mapped addresses as invalid.
- Returns:
Inet6Address
representation of theip
ornull
if not a valid IP address.
-
getIPv6ByName
Returns the byte array representation of aCharSequence
IP address.The
ipv4Mapped
parameter specifies how IPv4 addresses should be treated. "IPv4 mapped" format as defined in rfc 4291 section 2 is supported.- Parameters:
ip
-CharSequence
IP address to be converted to aInet6Address
ipv4Mapped
-true
To allow IPv4 mapped inputs to be translated intoInet6Address
false
Consider IPv4 mapped addresses as invalid.
- Returns:
- byte array representation of the
ip
ornull
if not a valid IP address.
-
toSocketAddressString
- Parameters:
addr
-InetSocketAddress
to be converted to an address string- Returns:
String
containing the text-formatted IP address
-
toSocketAddressString
Returns theString
representation of a host port combo. -
newSocketAddressStringBuilder
-
toAddressString
Returns theString
representation of anInetAddress
.- Inet4Address results are identical to
InetAddress.getHostAddress()
- Inet6Address results adhere to rfc 5952 section 4
The output does not include Scope ID.
- Parameters:
ip
-InetAddress
to be converted to an address string- Returns:
String
containing the text-formatted IP address
- Inet4Address results are identical to
-
toAddressString
Returns theString
representation of anInetAddress
.- Inet4Address results are identical to
InetAddress.getHostAddress()
- Inet6Address results adhere to
rfc 5952 section 4 if
ipv4Mapped
is false. Ifipv4Mapped
is true then "IPv4 mapped" format from rfc 4291 section 2 will be supported. The compressed result will always obey the compression rules defined in rfc 5952 section 4
The output does not include Scope ID.
- Parameters:
ip
-InetAddress
to be converted to an address stringipv4Mapped
-true
to stray from strict rfc 5952 and support the "IPv4 mapped" format defined in rfc 4291 section 2 while still following the updated guidelines in rfc 5952 section 4false
to strictly follow rfc 5952
- Returns:
String
containing the text-formatted IP address
- Inet4Address results are identical to
-
toAddressString
-
getHostname
ReturnsInetSocketAddress.getHostString()
if Java >= 7, orInetSocketAddress.getHostName()
otherwise.- Parameters:
addr
- The address- Returns:
- the host string
-
inRangeEndExclusive
private static boolean inRangeEndExclusive(int value, int start, int end) Does a range check onvalue
if is withinstart
(inclusive) andend
(exclusive).- Parameters:
value
- The value to checked if is withinstart
(inclusive) andend
(exclusive)start
- The start of the range (inclusive)end
- The end of the range (exclusive)- Returns:
true
ifvalue
if is withinstart
(inclusive) andend
(exclusive)false
otherwise
-