Package org.apache.sshd.common.util.net
Class SshdSocketAddress
- java.lang.Object
-
- java.net.SocketAddress
-
- org.apache.sshd.common.util.net.SshdSocketAddress
-
- All Implemented Interfaces:
Serializable
- Direct Known Subclasses:
LocalForwardingEntry
public class SshdSocketAddress extends SocketAddress
A simple socket address holding the host name and port number. The reason it does not extend
InetSocketAddress
is twofold:The
InetSocketAddress
performs a DNS resolution on the provided host name - which we don't want do use until we want to create a connection using this address (thus thetoInetSocketAddress()
call which executes this queryIf empty host name is provided we replace it with the any address of 0.0.0.0
- Author:
- Apache MINA SSHD Project
- See Also:
- Serialized Form
-
-
Field Summary
-
Constructor Summary
Constructors Constructor Description SshdSocketAddress(int port)
SshdSocketAddress(String hostName, int port)
SshdSocketAddress(InetSocketAddress addr)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description boolean
equals(Object o)
static List<InetAddress>
getExternalNetwork4Addresses()
static InetAddress
getFirstExternalNetwork4Address()
Returns the first external network address assigned to this machine or null if one is not found.String
getHostName()
int
getPort()
int
hashCode()
static boolean
isCarrierGradeNatIPv4Address(String addr)
protected boolean
isEquivalent(SshdSocketAddress that)
static boolean
isIPv4Address(String addr)
static boolean
isIPv6Address(String address)
static boolean
isLoopback(String ip)
static boolean
isLoopback(InetAddress addr)
static boolean
isPrivateIPv4Address(String addr)
Checks if the address is one of the allocated private blocksstatic boolean
isValidHostAddress(InetAddress addr)
static boolean
isValidIPv4AddressComponent(CharSequence c)
Checks if the provided argument is a valid IPv4 address component:static int
toAddressPort(SocketAddress addr)
Attempts to resolve the port valuestatic String
toAddressString(InetAddress addr)
static String
toAddressString(SocketAddress addr)
InetSocketAddress
toInetSocketAddress()
static InetSocketAddress
toInetSocketAddress(SocketAddress remoteAddress)
Converts aSocketAddress
into anInetSocketAddress
if possible:static SshdSocketAddress
toSshdSocketAddress(SocketAddress addr)
String
toString()
-
-
-
Field Detail
-
LOCALHOST_NAME
public static final String LOCALHOST_NAME
- See Also:
- Constant Field Values
-
LOCALHOST_IPV4
public static final String LOCALHOST_IPV4
- See Also:
- Constant Field Values
-
IPV4_ANYADDR
public static final String IPV4_ANYADDR
- See Also:
- Constant Field Values
-
PRIVATE_CLASS_A_PREFIX
public static final String PRIVATE_CLASS_A_PREFIX
- See Also:
- Constant Field Values
-
PRIVATE_CLASS_B_PREFIX
public static final String PRIVATE_CLASS_B_PREFIX
- See Also:
- Constant Field Values
-
PRIVATE_CLASS_C_PREFIX
public static final String PRIVATE_CLASS_C_PREFIX
- See Also:
- Constant Field Values
-
CARRIER_GRADE_NAT_PREFIX
public static final String CARRIER_GRADE_NAT_PREFIX
- See Also:
- Constant Field Values
-
BROADCAST_ADDRESS
public static final String BROADCAST_ADDRESS
- See Also:
- Constant Field Values
-
IPV6_MAX_HEX_GROUPS
public static final int IPV6_MAX_HEX_GROUPS
Max. number of hex groups (separated by ":") in an IPV6 address- See Also:
- Constant Field Values
-
IPV6_MAX_HEX_DIGITS_PER_GROUP
public static final int IPV6_MAX_HEX_DIGITS_PER_GROUP
Max. hex digits in each IPv6 group- See Also:
- Constant Field Values
-
IPV6_LONG_ANY_ADDRESS
public static final String IPV6_LONG_ANY_ADDRESS
- See Also:
- Constant Field Values
-
IPV6_SHORT_ANY_ADDRESS
public static final String IPV6_SHORT_ANY_ADDRESS
- See Also:
- Constant Field Values
-
IPV6_LONG_LOCALHOST
public static final String IPV6_LONG_LOCALHOST
- See Also:
- Constant Field Values
-
IPV6_SHORT_LOCALHOST
public static final String IPV6_SHORT_LOCALHOST
- See Also:
- Constant Field Values
-
LOCALHOST_ADDRESS
public static final SshdSocketAddress LOCALHOST_ADDRESS
A dummy placeholder that can be used instead ofnull
s
-
BY_HOST_ADDRESS
public static final Comparator<InetAddress> BY_HOST_ADDRESS
ComparesInetAddress
-es according to theirInetAddress.getHostAddress()
value case insensitive- See Also:
toAddressString(InetAddress)
-
BY_HOST_AND_PORT
public static final Comparator<SocketAddress> BY_HOST_AND_PORT
ComparesSocketAddress
-es according to their host case insensitive and if equals, then according to their port value (if any)
-
-
Constructor Detail
-
SshdSocketAddress
public SshdSocketAddress(int port)
-
SshdSocketAddress
public SshdSocketAddress(InetSocketAddress addr)
-
SshdSocketAddress
public SshdSocketAddress(String hostName, int port)
-
-
Method Detail
-
getHostName
public String getHostName()
-
getPort
public int getPort()
-
toInetSocketAddress
public InetSocketAddress toInetSocketAddress()
-
isEquivalent
protected boolean isEquivalent(SshdSocketAddress that)
-
getFirstExternalNetwork4Address
public static InetAddress getFirstExternalNetwork4Address()
Returns the first external network address assigned to this machine or null if one is not found.- Returns:
- Inet4Address associated with an external interface DevNote: We actually return InetAddress here, as Inet4Addresses are final and cannot be mocked.
-
getExternalNetwork4Addresses
public static List<InetAddress> getExternalNetwork4Addresses()
- Returns:
- a
List
of local network addresses which are not multicast or localhost sorted according toBY_HOST_ADDRESS
-
isValidHostAddress
public static boolean isValidHostAddress(InetAddress addr)
- Parameters:
addr
- TheInetAddress
to be verified- Returns:
true
if the address is:- Not
null
- An
Inet4Address
- Not link local
- Not a multicast
- Not a loopback
- Not
- See Also:
InetAddress.isLinkLocalAddress()
,InetAddress.isMulticastAddress()
,InetAddress.isMulticastAddress()
-
isLoopback
public static boolean isLoopback(InetAddress addr)
- Parameters:
addr
- TheInetAddress
to be considered- Returns:
true
if the address is a loopback one. Note: ifInetAddress.isLoopbackAddress()
returnsfalse
the address string is checked- See Also:
toAddressString(InetAddress)
,isLoopback(String)
-
isLoopback
public static boolean isLoopback(String ip)
- Parameters:
ip
- IP value to be tested- Returns:
true
if the IP is "localhost" or "127.x.x.x".
-
toSshdSocketAddress
public static SshdSocketAddress toSshdSocketAddress(SocketAddress addr)
-
toAddressString
public static String toAddressString(SocketAddress addr)
-
toAddressPort
public static int toAddressPort(SocketAddress addr)
Attempts to resolve the port value- Parameters:
addr
- TheSocketAddress
to examine- Returns:
- The associated port value - negative if failed to resolve
-
toInetSocketAddress
public static InetSocketAddress toInetSocketAddress(SocketAddress remoteAddress)
Converts a
SocketAddress
into anInetSocketAddress
if possible:- If already an
InetSocketAddress
then cast it as such - If an
SshdSocketAddress
then invoketoInetSocketAddress()
- Otherwise, throw an exception
- Parameters:
remoteAddress
- TheSocketAddress
- ignored ifnull
- Returns:
- The
InetSocketAddress
instance - Throws:
ClassCastException
- if argument is not already anInetSocketAddress
or aSshdSocketAddress
- If already an
-
toAddressString
public static String toAddressString(InetAddress addr)
-
isIPv4Address
public static boolean isIPv4Address(String addr)
-
isPrivateIPv4Address
public static boolean isPrivateIPv4Address(String addr)
Checks if the address is one of the allocated private blocks- Parameters:
addr
- The address string- Returns:
true
if this is one of the allocated private blocks. Note: it assumes that the address string is indeed an IPv4 address- See Also:
isIPv4Address(String)
,PRIVATE_CLASS_A_PREFIX
,PRIVATE_CLASS_B_PREFIX
,PRIVATE_CLASS_C_PREFIX
, Wiki page
-
isCarrierGradeNatIPv4Address
public static boolean isCarrierGradeNatIPv4Address(String addr)
- Parameters:
addr
- The address to be checked- Returns:
true
if the address is in the 100.64.0.0/10 range- See Also:
- RFC6598
-
isValidIPv4AddressComponent
public static boolean isValidIPv4AddressComponent(CharSequence c)
Checks if the provided argument is a valid IPv4 address component:
- Not
null
/empty - Has at most 3 digits
- Its value is ≤ 255
- Parameters:
c
- TheCharSequence
to be validate- Returns:
true
if valid IPv4 address component
- Not
-
isIPv6Address
public static boolean isIPv6Address(String address)
-
-