- All Implemented Interfaces:
Closeable
,AutoCloseable
,AFSocketExtensions
,AFSomeSocket
,AFSomeSocketThing
,AFUNIXSocketExtensions
,FileDescriptorAccess
public final class AFUNIXSocket
extends AFSocket<AFUNIXSocketAddress>
implements AFUNIXSocketExtensions
Implementation of an AF_UNIX domain socket.
- Author:
- Christian Kohlschütter
-
Nested Class Summary
Nested classes/interfaces inherited from class org.newsclub.net.unix.AFSocket
AFSocket.Constructor<A extends AFSocketAddress>
-
Method Summary
Modifier and TypeMethodDescriptionvoid
Clears the queue of incomingFileDescriptor
s that were sent as ancillary messages.static AFUNIXSocket
connectTo
(AFUNIXSocketAddress addr) Creates a newAFSocket
and connects it to the givenAFUNIXSocketAddress
.Retrieves the "peer credentials" for this connection.Retrieves an array of incomingFileDescriptor
s that were sent as ancillary messages, along with a call toInputStream.read()
, etc.boolean
Returnstrue
if there are pending file descriptors to be sent as part of an ancillary message.static boolean
Returnstrue
iffAFUNIXSocket
s are supported by the current Java VM.static void
Very basic self-test function.protected AFUNIXSocketChannel
Creates a newAFSocketChannel
for this socket.static AFUNIXSocket
Creates a new, unboundAFSocket
.static AFUNIXSocket
Creates a new, unbound, "strict"AFSocket
.void
setOutboundFileDescriptors
(FileDescriptor... fdescs) Sets a list ofFileDescriptor
s that should be sent as an ancillary message along with the next write.Methods inherited from class org.newsclub.net.unix.AFSocket
addCloseable, bind, checkConnectionClosed, close, connect, connect, connectHook, connectTo, connectTo, ensureAncillaryReceiveBufferSize, ensureSupported, ensureUnsafeSupported, forceConnectAddress, getAncillaryReceiveBufferSize, getFileDescriptor, getImplExtensions, getInputStream, getLoadedLibrary, getLocalSocketAddress, getOutputStream, getRemoteSocketAddress, getVersion, isBound, isClosed, isConnected, isRunningOnAndroid, newInstance, removeCloseable, setAncillaryReceiveBufferSize, setShutdownOnClose, socketAddressClass, supports, supports, toString
Methods inherited from class java.net.Socket
getInetAddress, getKeepAlive, getLocalAddress, getLocalPort, getOOBInline, getOption, getPort, getReceiveBufferSize, getReuseAddress, getSendBufferSize, getSoLinger, getSoTimeout, getTcpNoDelay, getTrafficClass, isInputShutdown, isOutputShutdown, sendUrgentData, setKeepAlive, setOOBInline, setOption, setPerformancePreferences, setReceiveBufferSize, setReuseAddress, setSendBufferSize, setSocketImplFactory, setSoLinger, setSoTimeout, setTcpNoDelay, setTrafficClass, shutdownInput, shutdownOutput, supportedOptions
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
Methods inherited from interface org.newsclub.net.unix.AFSocketExtensions
ensureAncillaryReceiveBufferSize, getAncillaryReceiveBufferSize, setAncillaryReceiveBufferSize
-
Method Details
-
newChannel
Description copied from class:AFSocket
Creates a newAFSocketChannel
for this socket.- Specified by:
newChannel
in classAFSocket<AFUNIXSocketAddress>
- Returns:
- The new instance.
-
newInstance
Creates a new, unboundAFSocket
. This "default" implementation is a bit "lenient" with respect to the specification. In particular, we ignore calls toSocket.getTcpNoDelay()
andSocket.setTcpNoDelay(boolean)
.- Returns:
- A new, unbound socket.
- Throws:
IOException
- if the operation fails.
-
newStrictInstance
Creates a new, unbound, "strict"AFSocket
. This call uses an implementation that tries to be closer to the specification thannewInstance()
, at least for some cases.- Returns:
- A new, unbound socket.
- Throws:
IOException
- if the operation fails.
-
connectTo
Creates a newAFSocket
and connects it to the givenAFUNIXSocketAddress
.- Parameters:
addr
- The address to connect to.- Returns:
- A new, connected socket.
- Throws:
IOException
- if the operation fails.
-
getChannel
- Overrides:
getChannel
in classAFSocket<AFUNIXSocketAddress>
-
getPeerCredentials
Description copied from interface:AFUNIXSocketExtensions
Retrieves the "peer credentials" for this connection. These credentials may be useful to authenticate the other end of the socket (client or server). Depending on the socket/connection/environment, you may not receive any or all credentials. For example, on Linux,AFUNIXDatagramSocket
andAFUNIXDatagramChannel
may not be able to retrieve credentials at all.- Specified by:
getPeerCredentials
in interfaceAFUNIXSocketExtensions
- Returns:
- The peer's credentials, or
null
if they couldn't be retrieved. - Throws:
IOException
- If there was an error returning these credentials.
-
getReceivedFileDescriptors
Description copied from interface:AFUNIXSocketExtensions
Retrieves an array of incomingFileDescriptor
s that were sent as ancillary messages, along with a call toInputStream.read()
, etc. NOTE: Another call to this method will not return the same file descriptors again (most likely, an empty array will be returned).- Specified by:
getReceivedFileDescriptors
in interfaceAFUNIXSocketExtensions
- Returns:
- The file descriptors, or an empty array if none were available.
- Throws:
IOException
- if the operation fails.
-
clearReceivedFileDescriptors
public void clearReceivedFileDescriptors()Description copied from interface:AFUNIXSocketExtensions
Clears the queue of incomingFileDescriptor
s that were sent as ancillary messages.- Specified by:
clearReceivedFileDescriptors
in interfaceAFUNIXSocketExtensions
-
setOutboundFileDescriptors
Description copied from interface:AFUNIXSocketExtensions
Sets a list ofFileDescriptor
s that should be sent as an ancillary message along with the next write. Important: There can only be one set of file descriptors active until the write completes. The socket also needs to be connected for this operation to succeed. It is also important to know that there may be an upper limit imposed by the operation system as to how many file descriptors can be sent at once. Linux, for example, may support up to 253. If the number of file descriptors exceeds the limit, an exception may be thrown when sending data along with the ancillary message containing the file descriptors.- Specified by:
setOutboundFileDescriptors
in interfaceAFUNIXSocketExtensions
- Parameters:
fdescs
- The file descriptors, ornull
if none.- Throws:
IOException
- if the operation fails.
-
hasOutboundFileDescriptors
public boolean hasOutboundFileDescriptors()Description copied from interface:AFUNIXSocketExtensions
Returnstrue
if there are pending file descriptors to be sent as part of an ancillary message.- Specified by:
hasOutboundFileDescriptors
in interfaceAFUNIXSocketExtensions
- Returns:
true
if there are file descriptors pending.
-
isSupported
public static boolean isSupported()Returnstrue
iffAFUNIXSocket
s are supported by the current Java VM. To supportAFSocket
s, a custom JNI library must be loaded that is supplied with junixsocket, and the system must support AF_UNIX sockets. This call is equivalent to checkingAFSocket.isSupported()
andAFSocket.supports(AFSocketCapability)
withAFSocketCapability.CAPABILITY_UNIX_DOMAIN
.- Returns:
true
iff supported.
-
main
Very basic self-test function. Prints "supported" and "capabilities" status to System.out.- Parameters:
args
- ignored.
-