java.lang.Object
java.net.SocketAddress
java.net.InetSocketAddress
org.newsclub.net.unix.AFSocketAddress
- All Implemented Interfaces:
Serializable
- Direct Known Subclasses:
AFGenericSocketAddress
,AFSYSTEMSocketAddress
,AFTIPCSocketAddress
,AFUNIXSocketAddress
,AFVSOCKSocketAddress
Some
SocketAddress
that is supported by junixsocket, such as AFUNIXSocketAddress
.- Author:
- Christian Kohlschütter
- See Also:
-
Nested Class Summary
Modifier and TypeClassDescriptionprotected static interface
A reference to the constructor of an AFSocketAddress subclass. -
Constructor Summary
ModifierConstructorDescriptionprotected
AFSocketAddress
(int port, byte[] socketAddress, org.newsclub.net.unix.pool.ObjectPool.Lease<ByteBuffer> nativeAddress, AFAddressFamily<?> af) Creates a new socket address. -
Method Summary
Modifier and TypeMethodDescriptionstatic boolean
canMap
(SocketAddress addr) Checks if the givenSocketAddress
can be mapped to anAFSocketAddress
.static boolean
canMap
(SocketAddress addr, Class<? extends AFSocketAddress> targetAddressClass) Checks if the givenSocketAddress
can be mapped to a specificAFSocketAddress
subclass.boolean
covers
(AFSocketAddress other) Checks if the given address could cover another address.final AFAddressFamily
<?> Returns the correspondingAFAddressFamily
.protected final byte[]
getBytes()
Returns the (non-native) byte-level representation of this address.abstract File
getFile()
Returns theFile
corresponding with this address, if possible.protected final InetAddress
Wraps this address as anInetAddress
.protected static final InetAddress
getInetAddress
(FileDescriptor fdesc, boolean peerName, AFAddressFamily<?> af) Wraps the socket name/peer name of a file descriptor as anInetAddress
.protected final InetAddress
getInetAddress
(AFAddressFamily<?> af) Wraps an address as anInetAddress
.protected static final <A extends AFSocketAddress>
@Nullable AgetSocketAddress
(FileDescriptor fdesc, boolean requestPeerName, int port, AFAddressFamily<A> af) Gets the socket name/peer name of a file descriptor as anAFSocketAddress
.abstract boolean
Checks if the address can be resolved to aFile
.protected static final boolean
isSupportedAddress
(InetAddress addr, AFAddressFamily<?> af) Checks if the given address is supported by this address family.protected static boolean
Checks ifAFSocketAddress
instantiation should be performed via deserialization.static AFSocketAddress
mapOrFail
(SocketAddress addr) Maps the given address to anAFSocketAddress
.static <A extends AFSocketAddress>
AmapOrFail
(SocketAddress addr, Class<A> targetAddressClass) Maps the given address to a specificAFSocketAddress
type.Creates a new server socket bound to this address.AFSocket
<?> Creates a new socket connected to this address.protected static <A extends AFSocketAddress>
AnewDeserializedAFSocketAddress
(int port, byte[] socketAddress, org.newsclub.net.unix.pool.ObjectPool.Lease<ByteBuffer> nativeAddress, AFAddressFamily<A> af, AFSocketAddress.AFSocketAddressConstructor<A> constructor) Returns a newAFSocketAddress
instance via deserialization.Creates a new server socket force-bound to this address (i.e., any additional call toServerSocket.bind(SocketAddress)
will ignore the passed address and use this one instead.static AFSocketAddress
Tries to parse the given URI and return a correspondingAFSocketAddress
for it.static AFSocketAddress
Tries to parse the given URI and return a correspondingAFSocketAddress
for it.protected static int
parseUnsignedInt
(String s, int radix) Parses the string argument as an unsigned integer in the radix specified by the second argument.protected static final <A extends AFSocketAddress>
AresolveAddress
(byte[] socketAddress, int port, AFAddressFamily<A> af) Resolves a junixsocket-specific byte-array representation of anAFSocketAddress
to an actualAFSocketAddress
instance, possibly reusing a cached instance.@Nullable String
toSocatAddressString
(AFSocketType socketType, AFSocketProtocol socketProtocol) Returns a address string that can be used withsocat
'sSOCKET-CONNECT
,SOCKET-LISTEN
,SOCKET-DATAGRAM
, etc., address types, ornull
if the address type is not natively supported by this platform.Tries to create a URI based on thisAFSocketAddress
.protected static final <A extends AFSocketAddress>
@NonNull Aunwrap
(String hostname, int port, AFAddressFamily<A> af) Returns anAFSocketAddress
given a specialInetAddress
hostname that encodes the byte sequence of an AF_UNIX etc. socket address, like those returned bywrapAddress()
.protected static final <A extends AFSocketAddress>
@NonNull Aunwrap
(InetAddress address, int port, AFAddressFamily<A> af) Returns anAFSocketAddress
given a specialInetAddress
that encodes the byte sequence of an AF_UNIX etc. socket address, like those returned bywrapAddress()
.final InetAddress
Returns a "special"InetAddress
that contains information about thisAFSocketAddress
.final void
Writes the native (system-level) representation of this address to the given buffer.Methods inherited from class java.net.InetSocketAddress
createUnresolved, equals, getAddress, getHostName, getHostString, getPort, hashCode, isUnresolved, toString
-
Constructor Details
-
AFSocketAddress
protected AFSocketAddress(int port, byte[] socketAddress, org.newsclub.net.unix.pool.ObjectPool.Lease<ByteBuffer> nativeAddress, AFAddressFamily<?> af) throws SocketException Creates a new socket address.- Parameters:
port
- The port.socketAddress
- The socket address in junixsocket-specific byte-array representation.nativeAddress
- The socket address in system-native representation.af
- The address family.- Throws:
SocketException
- on error.
-
-
Method Details
-
newDeserializedAFSocketAddress
protected static <A extends AFSocketAddress> A newDeserializedAFSocketAddress(int port, byte[] socketAddress, org.newsclub.net.unix.pool.ObjectPool.Lease<ByteBuffer> nativeAddress, AFAddressFamily<A> af, AFSocketAddress.AFSocketAddressConstructor<A> constructor) throws SocketException Returns a newAFSocketAddress
instance via deserialization. This is a trick to workaround certain environments that do not allow the construction ofInetSocketAddress
instances without trying DNS resolution.- Type Parameters:
A
- The subclass (must be a direct subclass ofAFSocketAddress
).- Parameters:
port
- The port to use.socketAddress
- The junixsocket representation of the socket address.nativeAddress
- The system-native representation of the socket address, ornull
.af
- The address family, corresponding to the subclassconstructor
- The constructor to use as fallback- Returns:
- The new instance.
- Throws:
SocketException
- on error.
-
isUseDeserializationForInit
protected static boolean isUseDeserializationForInit()Checks ifAFSocketAddress
instantiation should be performed via deserialization.- Returns:
true
if so.- See Also:
-
hasFilename
public abstract boolean hasFilename()Checks if the address can be resolved to aFile
.- Returns:
true
if the address has a filename.
-
getFile
Returns theFile
corresponding with this address, if possible. AFileNotFoundException
is thrown if there is no filename associated with the address, which applies to addresses in the abstract namespace, for example.- Returns:
- The filename.
- Throws:
FileNotFoundException
- if the address is not associated with a filename.
-
getAddressFamily
Returns the correspondingAFAddressFamily
.- Returns:
- The address family instance.
-
getInetAddress
protected static final InetAddress getInetAddress(FileDescriptor fdesc, boolean peerName, AFAddressFamily<?> af) Wraps the socket name/peer name of a file descriptor as anInetAddress
.- Parameters:
fdesc
- The file descriptor.peerName
- Iftrue
, the remote peer name (instead of the local name) is retrieved.af
- The address family.- Returns:
- The
InetAddress
.
-
getSocketAddress
protected static final <A extends AFSocketAddress> @Nullable A getSocketAddress(FileDescriptor fdesc, boolean requestPeerName, int port, AFAddressFamily<A> af) Gets the socket name/peer name of a file descriptor as anAFSocketAddress
.- Type Parameters:
A
- The corresponding address type.- Parameters:
fdesc
- The file descriptor.requestPeerName
- Iftrue
, the remote peer name (instead of the local name) is retrieved.port
- The port.af
- The address family.- Returns:
- The
InetAddress
.
-
getBytes
protected final byte[] getBytes()Returns the (non-native) byte-level representation of this address.- Returns:
- The byte array.
-
wrapAddress
Returns a "special"InetAddress
that contains information about thisAFSocketAddress
. IMPORTANT: ThisInetAddress
does not properly compare (usingInetAddress.equals(Object)
andInetAddress.hashCode()
). It should be used exclusively to circumvent existing APIs likeDatagramSocket
that only accept/returnInetAddress
and not arbitrarySocketAddress
types.- Returns:
- The "special"
InetAddress
.
-
resolveAddress
protected static final <A extends AFSocketAddress> A resolveAddress(byte[] socketAddress, int port, AFAddressFamily<A> af) throws SocketException Resolves a junixsocket-specific byte-array representation of anAFSocketAddress
to an actualAFSocketAddress
instance, possibly reusing a cached instance.- Type Parameters:
A
- The concreteAFSocketAddress
that is supported by this type.- Parameters:
socketAddress
- The socket address in junixsocket-specific byte-array representation.port
- The port.af
- The address family.- Returns:
- The instance.
- Throws:
SocketException
- on error.
-
getInetAddress
Wraps an address as anInetAddress
.- Parameters:
af
- The address family.- Returns:
- The
InetAddress
.
-
getInetAddress
Wraps this address as anInetAddress
.- Returns:
- The
InetAddress
.
-
unwrap
protected static final <A extends AFSocketAddress> @NonNull A unwrap(InetAddress address, int port, AFAddressFamily<A> af) throws SocketException Returns anAFSocketAddress
given a specialInetAddress
that encodes the byte sequence of an AF_UNIX etc. socket address, like those returned bywrapAddress()
.- Type Parameters:
A
- The corresponding address type.- Parameters:
address
- The "special"InetAddress
.port
- The port (use 0 for "none").af
- The address family.- Returns:
- The
AFSocketAddress
instance. - Throws:
SocketException
- if the operation fails, for example when an unsupported address is specified.
-
unwrap
protected static final <A extends AFSocketAddress> @NonNull A unwrap(String hostname, int port, AFAddressFamily<A> af) throws SocketException Returns anAFSocketAddress
given a specialInetAddress
hostname that encodes the byte sequence of an AF_UNIX etc. socket address, like those returned bywrapAddress()
.- Type Parameters:
A
- The corresponding address type.- Parameters:
hostname
- The "special" hostname, as provided byInetAddress.getHostName()
.port
- The port (use 0 for "none").af
- The address family.- Returns:
- The
AFSocketAddress
instance. - Throws:
SocketException
- if the operation fails, for example when an unsupported address is specified.
-
isSupportedAddress
Checks if the given address is supported by this address family.- Parameters:
addr
- The address.af
- The address family.- Returns:
true
if supported.
-
writeNativeAddressTo
Writes the native (system-level) representation of this address to the given buffer. The position of the target buffer will be at the end (i.e., after) the written data.- Parameters:
buf
- The target buffer.- Throws:
IOException
- on error.
-
newConnectedSocket
Creates a new socket connected to this address.- Returns:
- The socket instance.
- Throws:
IOException
- on error.
-
newBoundServerSocket
Creates a new server socket bound to this address.- Returns:
- The server socket instance.
- Throws:
IOException
- on error.
-
newForceBoundServerSocket
Creates a new server socket force-bound to this address (i.e., any additional call toServerSocket.bind(SocketAddress)
will ignore the passed address and use this one instead.- Returns:
- The server socket instance.
- Throws:
IOException
- on error.
-
of
Tries to parse the given URI and return a correspondingAFSocketAddress
for it. NOTE: Only certain URI schemes are supported, such asunix://
(forAFUNIXSocketAddress
) andtipc://
forAFTIPCSocketAddress
.- Parameters:
u
- The URI.- Returns:
- The address.
- Throws:
SocketException
- on error.- See Also:
-
of
Tries to parse the given URI and return a correspondingAFSocketAddress
for it. NOTE: Only certain URI schemes are supported, such asunix://
(forAFUNIXSocketAddress
) andtipc://
forAFTIPCSocketAddress
.- Parameters:
u
- The URI.overridePort
- The port to forcibly use, or-1
for "don't override".- Returns:
- The address.
- Throws:
SocketException
- on error.- See Also:
-
toURI
Tries to create a URI based on thisAFSocketAddress
.- Parameters:
scheme
- The target scheme.template
- An optional template to reuse certain parameters (e.g., the "path" component for anhttp
request), ornull
.- Returns:
- The URI.
- Throws:
IOException
- on error.
-
toSocatAddressString
public @Nullable String toSocatAddressString(AFSocketType socketType, AFSocketProtocol socketProtocol) throws IOException Returns a address string that can be used withsocat
'sSOCKET-CONNECT
,SOCKET-LISTEN
,SOCKET-DATAGRAM
, etc., address types, ornull
if the address type is not natively supported by this platform. This call is mostly suited for debugging purposes. The resulting string is specific to the platform the code is executed on, and thus may be different among platforms.- Parameters:
socketType
- The socket type, ornull
to omit from string.socketProtocol
- The socket protocol, ornull
to omit from string.- Returns:
- The string (such as 1:0:x2f746d702f796f).
- Throws:
IOException
- on error (aSocketException
is thrown if the native address cannot be accessed).
-
covers
Checks if the given address could cover another address. By default, this is only true if both addresses are regarded equal usingInetSocketAddress.equals(Object)
. However, implementations may support "wildcard" addresses, and this method would compare a wildcard address against some non-wildcard address, for example.- Parameters:
other
- The other address that could be covered by this address.- Returns:
true
if the other address could be covered.
-
parseUnsignedInt
Parses the string argument as an unsigned integer in the radix specified by the second argument. Works likeInteger.parseUnsignedInt(String, int)
; added to allow execution on Java 1.7.- Parameters:
s
- The string.radix
- The radix.- Returns:
- The integer.
- Throws:
NumberFormatException
- on parse error.
-
canMap
Checks if the givenSocketAddress
can be mapped to anAFSocketAddress
. This is the case if the address either already is anAFSocketAddress
,null
, or something that has an equivalent representation, such asUnixDomainSocketAddress
.- Parameters:
addr
- The address.- Returns:
true
if mappable.
-
canMap
public static boolean canMap(SocketAddress addr, Class<? extends AFSocketAddress> targetAddressClass) Checks if the givenSocketAddress
can be mapped to a specificAFSocketAddress
subclass. This is the case if the address either already is such anAFSocketAddress
,null
, or something that has an equivalent representation, such asUnixDomainSocketAddress
.- Parameters:
addr
- The address.targetAddressClass
- The target address class to map to.- Returns:
true
if mappable.
-
mapOrFail
Maps the given address to anAFSocketAddress
.- Parameters:
addr
- The address.- Returns:
- The
AFSocketAddress
. - Throws:
IllegalArgumentException
- if the address could not be mapped.- See Also:
-
mapOrFail
public static <A extends AFSocketAddress> A mapOrFail(SocketAddress addr, Class<A> targetAddressClass) Maps the given address to a specificAFSocketAddress
type.- Type Parameters:
A
- The target address type.- Parameters:
addr
- The address.targetAddressClass
- The target address class.- Returns:
- The
AFSocketAddress
. - Throws:
IllegalArgumentException
- if the address could not be mapped.- See Also:
-