java.lang.Object
java.net.SocketAddress
java.net.InetSocketAddress
org.newsclub.net.unix.AFSocketAddress
- All Implemented Interfaces:
Serializable
- Direct Known Subclasses:
AFTIPCSocketAddress
,AFUNIXSocketAddress
Some
SocketAddress
that is supported by junixsocket, such as AFUNIXSocketAddress
.- Author:
- Christian Kohlschütter
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprotected static interface
A reference to the constructor of an AFSocketAddress subclass. -
Constructor Summary
ConstructorsModifierConstructorDescriptionprotected
AFSocketAddress
(int port, byte[] socketAddress, ByteBuffer nativeAddress, AFAddressFamily<?> af) Creates a new socket address.protected
AFSocketAddress
(Class<org.newsclub.net.unix.SentinelSocketAddress> clazz, int port) Only forSentinelSocketAddress
. -
Method Summary
Modifier and TypeMethodDescriptionfinal 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 peerName, 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.Creates a new server socket bound to this address.AFSocket<?>
Creates a new socket connected to this address.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 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, 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.
-
AFSocketAddress
Only forSentinelSocketAddress
.- Parameters:
clazz
- TheSentinelSocketAddress
class.port
- A sentinel port number.
-
-
Method Details
-
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 peerName, 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.peerName
- 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 (ornull
).- 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), or
null
if unable to retrieve. - Throws:
IOException
- on error.
-