Package io.netty.util
Class NetUtil
- java.lang.Object
-
- io.netty.util.NetUtil
-
public final class NetUtil extends java.lang.ObjectA 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.
-
-
Field Summary
Fields Modifier and Type Field Description private static intIPV4_MAX_CHAR_BETWEEN_SEPARATORMaximum amount of value adding characters in between IPV4 separatorsprivate static booleanIPV4_PREFERREDtrueif IPv4 should be used even if the system supports both IPv4 and IPv6.private static intIPV4_SEPARATORSNumber of separators that must be present in an IPv4 stringprivate static booleanIPV6_ADDRESSES_PREFERREDtrueif an IPv6 address should be preferred when a host has both an IPv4 address and an IPv6 address.private static intIPV6_BYTE_COUNTNumber of bytes needed to represent and IPV6 valueprivate static intIPV6_MAX_CHAR_BETWEEN_SEPARATORMaximum amount of value adding characters in between IPV6 separatorsprivate static intIPV6_MAX_CHAR_COUNTThe maximum number of characters for an IPV6 string with no scopeprivate static intIPV6_MAX_SEPARATORSMaximum number of separators that must be present in an IPv6 stringprivate static intIPV6_MIN_SEPARATORSMinimum number of separators that must be present in an IPv6 stringprivate static intIPV6_WORD_COUNTThis defines how many words (represented as ints) are needed to represent an IPv6 addressstatic java.net.InetAddressLOCALHOSTTheInetAddressthat represents the loopback address.static java.net.Inet4AddressLOCALHOST4TheInet4Addressthat represents the IPv4 loopback address '127.0.0.1'static java.net.Inet6AddressLOCALHOST6TheInet6Addressthat represents the IPv6 loopback address '::1'private static InternalLoggerloggerThe logger being used by this classstatic java.net.NetworkInterfaceLOOPBACK_IFThe loopbackNetworkInterfaceof the current machinestatic intSOMAXCONNThe SOMAXCONN value of the current machine.
-
Constructor Summary
Constructors Modifier Constructor Description privateNetUtil()A constructor to stop this class being constructed.
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static java.lang.StringbytesToIpAddress(byte[] bytes)Converts 4-byte or 16-byte data into an IPv4 or IPv6 string respectively.static java.lang.StringbytesToIpAddress(byte[] bytes, int offset, int length)Converts 4-byte or 16-byte data into an IPv4 or IPv6 string respectively.static byte[]createByteArrayFromIpAddressString(java.lang.String ipAddressString)Creates an byte[] based on an ipAddressString.private static intdecimalDigit(java.lang.String str, int pos)static java.net.Inet6AddressgetByName(java.lang.CharSequence ip)Returns theInet6Addressrepresentation of aCharSequenceIP address.static java.net.Inet6AddressgetByName(java.lang.CharSequence ip, boolean ipv4Mapped)Returns theInet6Addressrepresentation of aCharSequenceIP address.static java.lang.StringgetHostname(java.net.InetSocketAddress addr)ReturnsInetSocketAddress.getHostString()if Java >= 7, orInetSocketAddress.getHostName()otherwise.private static byte[]getIPv6ByName(java.lang.CharSequence ip, boolean ipv4Mapped)Returns the byte array representation of aCharSequenceIP address.private static booleaninRangeEndExclusive(int value, int start, int end)Does a range check onvalueif is withinstart(inclusive) andend(exclusive).static java.lang.StringintToIpAddress(int i)Converts a 32-bit integer into an IPv4 address.private static byteipv4WordToByte(java.lang.String ip, int from, int toExclusive)static booleanisIpV4StackPreferred()Returnstrueif IPv4 should be used even if the system supports both IPv4 and IPv6.static booleanisIpV6AddressesPreferred()Returnstrueif an IPv6 address should be preferred when a host has both an IPv4 address and an IPv6 address.private static booleanisValidHexChar(char c)private static booleanisValidIpV4Address(AsciiString ip, int from, int toExcluded)static booleanisValidIpV4Address(java.lang.CharSequence ip)Takes aCharSequenceand parses it to see if it is a valid IPV4 address.private static booleanisValidIpV4Address(java.lang.CharSequence ip, int from, int toExcluded)static booleanisValidIpV4Address(java.lang.String ip)Takes aStringand parses it to see if it is a valid IPV4 address.private static booleanisValidIpV4Address(java.lang.String ip, int from, int toExcluded)private static booleanisValidIpV4Address0(java.lang.CharSequence ip, int from, int toExcluded)private static booleanisValidIPv4Mapped(byte[] bytes, int currentIndex, int compressBegin, int compressLength)private static booleanisValidIPv4MappedChar(char c)private static booleanisValidIPv4MappedSeparators(byte b0, byte b1, boolean mustBeZero)private static booleanisValidIpV4Word(java.lang.CharSequence word, int from, int toExclusive)static booleanisValidIpV6Address(java.lang.CharSequence ip)static booleanisValidIpV6Address(java.lang.String ip)private static booleanisValidNumericChar(char c)private static java.lang.StringBuildernewSocketAddressStringBuilder(java.lang.String host, java.lang.String port, boolean ipv4)private static java.lang.IntegersysctlGetInt(java.lang.String sysctlKey)This will execute sysctl with thesysctlKeywhich is expected to return the numeric value for forsysctlKey.private static java.lang.StringtoAddressString(byte[] bytes, int offset, boolean ipv4Mapped)static java.lang.StringtoAddressString(java.net.InetAddress ip)Returns theStringrepresentation of anInetAddress.static java.lang.StringtoAddressString(java.net.InetAddress ip, boolean ipv4Mapped)Returns theStringrepresentation of anInetAddress.static java.lang.StringtoSocketAddressString(java.lang.String host, int port)Returns theStringrepresentation of a host port combo.static java.lang.StringtoSocketAddressString(java.net.InetSocketAddress addr)Returns theStringrepresentation of anInetSocketAddress.(package private) static byte[]validIpV4ToBytes(java.lang.String ip)
-
-
-
Field Detail
-
LOCALHOST4
public static final java.net.Inet4Address LOCALHOST4
TheInet4Addressthat represents the IPv4 loopback address '127.0.0.1'
-
LOCALHOST6
public static final java.net.Inet6Address LOCALHOST6
TheInet6Addressthat represents the IPv6 loopback address '::1'
-
LOCALHOST
public static final java.net.InetAddress LOCALHOST
TheInetAddressthat represents the loopback address. If IPv6 stack is available, it will refer toLOCALHOST6. Otherwise,LOCALHOST4.
-
LOOPBACK_IF
public static final java.net.NetworkInterface LOOPBACK_IF
The loopbackNetworkInterfaceof the current machine
-
SOMAXCONN
public static final int SOMAXCONN
The SOMAXCONN value of the current machine. If failed to get the value,200is used as a default value for Windows or128for others.
-
IPV6_WORD_COUNT
private static final int IPV6_WORD_COUNT
This defines how many words (represented as ints) are needed to represent an IPv6 address- See Also:
- Constant Field Values
-
IPV6_MAX_CHAR_COUNT
private static final int IPV6_MAX_CHAR_COUNT
The maximum number of characters for an IPV6 string with no scope- See Also:
- Constant Field Values
-
IPV6_BYTE_COUNT
private static final int IPV6_BYTE_COUNT
Number of bytes needed to represent and IPV6 value- See Also:
- Constant Field Values
-
IPV6_MAX_CHAR_BETWEEN_SEPARATOR
private static final int IPV6_MAX_CHAR_BETWEEN_SEPARATOR
Maximum amount of value adding characters in between IPV6 separators- See Also:
- Constant Field Values
-
IPV6_MIN_SEPARATORS
private static final int IPV6_MIN_SEPARATORS
Minimum number of separators that must be present in an IPv6 string- See Also:
- Constant Field Values
-
IPV6_MAX_SEPARATORS
private static final int IPV6_MAX_SEPARATORS
Maximum number of separators that must be present in an IPv6 string- See Also:
- Constant Field Values
-
IPV4_MAX_CHAR_BETWEEN_SEPARATOR
private static final int IPV4_MAX_CHAR_BETWEEN_SEPARATOR
Maximum amount of value adding characters in between IPV4 separators- See Also:
- Constant Field Values
-
IPV4_SEPARATORS
private static final int IPV4_SEPARATORS
Number of separators that must be present in an IPv4 string- See Also:
- Constant Field Values
-
IPV4_PREFERRED
private static final boolean IPV4_PREFERRED
trueif IPv4 should be used even if the system supports both IPv4 and IPv6.
-
IPV6_ADDRESSES_PREFERRED
private static final boolean IPV6_ADDRESSES_PREFERRED
trueif an IPv6 address should be preferred when a host has both an IPv4 address and an IPv6 address.
-
logger
private static final InternalLogger logger
The logger being used by this class
-
-
Method Detail
-
sysctlGetInt
private static java.lang.Integer sysctlGetInt(java.lang.String sysctlKey) throws java.io.IOExceptionThis will execute sysctl with thesysctlKeywhich 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:
java.io.IOException
-
isIpV4StackPreferred
public static boolean isIpV4StackPreferred()
Returnstrueif IPv4 should be used even if the system supports both IPv4 and IPv6. Setting this property totruewill disable IPv6 support. The default value of this property isfalse.- See Also:
- Java SE networking properties
-
isIpV6AddressesPreferred
public static boolean isIpV6AddressesPreferred()
Returnstrueif 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:
- Java SE networking properties
-
createByteArrayFromIpAddressString
public static byte[] createByteArrayFromIpAddressString(java.lang.String ipAddressString)
Creates an byte[] based on an ipAddressString. No error handling is performed here.
-
decimalDigit
private static int decimalDigit(java.lang.String str, int pos)
-
ipv4WordToByte
private static byte ipv4WordToByte(java.lang.String ip, int from, int toExclusive)
-
validIpV4ToBytes
static byte[] validIpV4ToBytes(java.lang.String ip)
-
intToIpAddress
public static java.lang.String intToIpAddress(int i)
Converts a 32-bit integer into an IPv4 address.
-
bytesToIpAddress
public static java.lang.String bytesToIpAddress(byte[] bytes)
Converts 4-byte or 16-byte data into an IPv4 or IPv6 string respectively.- Throws:
java.lang.IllegalArgumentException- iflengthis not4nor16
-
bytesToIpAddress
public static java.lang.String bytesToIpAddress(byte[] bytes, int offset, int length)Converts 4-byte or 16-byte data into an IPv4 or IPv6 string respectively.- Throws:
java.lang.IllegalArgumentException- iflengthis not4nor16
-
isValidIpV6Address
public static boolean isValidIpV6Address(java.lang.String ip)
-
isValidIpV6Address
public static boolean isValidIpV6Address(java.lang.CharSequence ip)
-
isValidIpV4Word
private static boolean isValidIpV4Word(java.lang.CharSequence word, int from, int toExclusive)
-
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
public static boolean isValidIpV4Address(java.lang.CharSequence ip)
Takes aCharSequenceand 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
public static boolean isValidIpV4Address(java.lang.String ip)
Takes aStringand 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
private static boolean isValidIpV4Address(java.lang.CharSequence ip, int from, int toExcluded)
-
isValidIpV4Address
private static boolean isValidIpV4Address(java.lang.String ip, int from, int toExcluded)
-
isValidIpV4Address
private static boolean isValidIpV4Address(AsciiString ip, int from, int toExcluded)
-
isValidIpV4Address0
private static boolean isValidIpV4Address0(java.lang.CharSequence ip, int from, int toExcluded)
-
getByName
public static java.net.Inet6Address getByName(java.lang.CharSequence ip)
Returns theInet6Addressrepresentation of aCharSequenceIP address.This method will treat all IPv4 type addresses as "IPv4 mapped" (see
getByName(CharSequence, boolean))- Parameters:
ip-CharSequenceIP address to be converted to aInet6Address- Returns:
Inet6Addressrepresentation of theipornullif not a valid IP address.
-
getByName
public static java.net.Inet6Address getByName(java.lang.CharSequence ip, boolean ipv4Mapped)Returns theInet6Addressrepresentation of aCharSequenceIP address.The
ipv4Mappedparameter specifies how IPv4 addresses should be treated. "IPv4 mapped" format as defined in rfc 4291 section 2 is supported.- Parameters:
ip-CharSequenceIP address to be converted to aInet6Addressipv4Mapped-trueTo allow IPv4 mapped inputs to be translated intoInet6AddressfalseConsider IPv4 mapped addresses as invalid.
- Returns:
Inet6Addressrepresentation of theipornullif not a valid IP address.
-
getIPv6ByName
private static byte[] getIPv6ByName(java.lang.CharSequence ip, boolean ipv4Mapped)Returns the byte array representation of aCharSequenceIP address.The
ipv4Mappedparameter specifies how IPv4 addresses should be treated. "IPv4 mapped" format as defined in rfc 4291 section 2 is supported.- Parameters:
ip-CharSequenceIP address to be converted to aInet6Addressipv4Mapped-trueTo allow IPv4 mapped inputs to be translated intoInet6AddressfalseConsider IPv4 mapped addresses as invalid.
- Returns:
- byte array representation of the
ipornullif not a valid IP address.
-
toSocketAddressString
public static java.lang.String toSocketAddressString(java.net.InetSocketAddress addr)
Returns theStringrepresentation of anInetSocketAddress.The output does not include Scope ID.
- Parameters:
addr-InetSocketAddressto be converted to an address string- Returns:
Stringcontaining the text-formatted IP address
-
toSocketAddressString
public static java.lang.String toSocketAddressString(java.lang.String host, int port)Returns theStringrepresentation of a host port combo.
-
newSocketAddressStringBuilder
private static java.lang.StringBuilder newSocketAddressStringBuilder(java.lang.String host, java.lang.String port, boolean ipv4)
-
toAddressString
public static java.lang.String toAddressString(java.net.InetAddress ip)
Returns theStringrepresentation 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-InetAddressto be converted to an address string- Returns:
Stringcontaining the text-formatted IP address
- Inet4Address results are identical to
-
toAddressString
public static java.lang.String toAddressString(java.net.InetAddress ip, boolean ipv4Mapped)Returns theStringrepresentation of anInetAddress.- Inet4Address results are identical to
InetAddress.getHostAddress() - Inet6Address results adhere to
rfc 5952 section 4 if
ipv4Mappedis false. Ifipv4Mappedis 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-InetAddressto be converted to an address stringipv4Mapped-trueto 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 4falseto strictly follow rfc 5952
- Returns:
Stringcontaining the text-formatted IP address
- Inet4Address results are identical to
-
toAddressString
private static java.lang.String toAddressString(byte[] bytes, int offset, boolean ipv4Mapped)
-
getHostname
public static java.lang.String getHostname(java.net.InetSocketAddress addr)
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 onvalueif 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:
trueifvalueif is withinstart(inclusive) andend(exclusive)falseotherwise
-
-