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
Nested ClassesModifier and TypeClassDescriptionprotected static interfaceA reference to the constructor of an AFSocketAddress subclass. -
Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedAFSocketAddress(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 booleancanMap(SocketAddress addr) Checks if the givenSocketAddresscan be mapped to anAFSocketAddress.static booleancanMap(SocketAddress addr, Class<? extends AFSocketAddress> targetAddressClass) Checks if the givenSocketAddresscan be mapped to a specificAFSocketAddresssubclass.booleancovers(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 FilegetFile()Returns theFilecorresponding with this address, if possible.protected final InetAddressWraps this address as anInetAddress.protected static final InetAddressgetInetAddress(FileDescriptor fdesc, boolean peerName, AFAddressFamily<?> af) Wraps the socket name/peer name of a file descriptor as anInetAddress.protected final InetAddressgetInetAddress(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 booleanChecks if the address can be resolved to aFile.protected static final booleanisSupportedAddress(InetAddress addr, AFAddressFamily<?> af) Checks if the given address is supported by this address family.protected static booleanChecks ifAFSocketAddressinstantiation should be performed via deserialization.static AFSocketAddressmapOrFail(SocketAddress addr) Maps the given address to anAFSocketAddress.static <A extends AFSocketAddress>
AmapOrFail(SocketAddress addr, Class<A> targetAddressClass) Maps the given address to a specificAFSocketAddresstype.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 newAFSocketAddressinstance 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 AFSocketAddressTries to parse the given URI and return a correspondingAFSocketAddressfor it.static AFSocketAddressTries to parse the given URI and return a correspondingAFSocketAddressfor it.protected static intparseUnsignedInt(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 anAFSocketAddressto an actualAFSocketAddressinstance, possibly reusing a cached instance.@Nullable StringtoSocatAddressString(AFSocketType socketType, AFSocketProtocol socketProtocol) Returns a address string that can be used withsocat'sSOCKET-CONNECT,SOCKET-LISTEN,SOCKET-DATAGRAM, etc., address types, ornullif 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 anAFSocketAddressgiven a specialInetAddresshostname 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 anAFSocketAddressgiven a specialInetAddressthat encodes the byte sequence of an AF_UNIX etc. socket address, like those returned bywrapAddress().final InetAddressReturns a "special"InetAddressthat contains information about thisAFSocketAddress.final voidWrites 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 newAFSocketAddressinstance via deserialization. This is a trick to workaround certain environments that do not allow the construction ofInetSocketAddressinstances 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 ifAFSocketAddressinstantiation should be performed via deserialization.- Returns:
trueif so.- See Also:
-
hasFilename
public abstract boolean hasFilename()Checks if the address can be resolved to aFile.- Returns:
trueif the address has a filename.
-
getFile
Returns theFilecorresponding with this address, if possible. AFileNotFoundExceptionis 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"InetAddressthat contains information about thisAFSocketAddress. IMPORTANT: ThisInetAddressdoes not properly compare (usingInetAddress.equals(Object)andInetAddress.hashCode()). It should be used exclusively to circumvent existing APIs likeDatagramSocketthat only accept/returnInetAddressand not arbitrarySocketAddresstypes.- 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 anAFSocketAddressto an actualAFSocketAddressinstance, possibly reusing a cached instance.- Type Parameters:
A- The concreteAFSocketAddressthat 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 anAFSocketAddressgiven a specialInetAddressthat 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
AFSocketAddressinstance. - 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 anAFSocketAddressgiven a specialInetAddresshostname 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
AFSocketAddressinstance. - 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:
trueif 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 correspondingAFSocketAddressfor 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 correspondingAFSocketAddressfor it. NOTE: Only certain URI schemes are supported, such asunix://(forAFUNIXSocketAddress) andtipc://forAFTIPCSocketAddress.- Parameters:
u- The URI.overridePort- The port to forcibly use, or-1for "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 anhttprequest), 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, ornullif 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, ornullto omit from string.socketProtocol- The socket protocol, ornullto omit from string.- Returns:
- The string (such as 1:0:x2f746d702f796f).
- Throws:
IOException- on error (aSocketExceptionis 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:
trueif 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 givenSocketAddresscan 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:
trueif mappable.
-
canMap
public static boolean canMap(SocketAddress addr, Class<? extends AFSocketAddress> targetAddressClass) Checks if the givenSocketAddresscan be mapped to a specificAFSocketAddresssubclass. 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:
trueif 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 specificAFSocketAddresstype.- 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:
-