Class SecureNioChannel
java.lang.Object
org.apache.tomcat.util.net.NioChannel
org.apache.tomcat.util.net.SecureNioChannel
- All Implemented Interfaces:
Closeable, AutoCloseable, ByteChannel, Channel, GatheringByteChannel, ReadableByteChannel, ScatteringByteChannel, WritableByteChannel
Implementation of a secure socket channel
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected booleanTrue when the channel is closedprotected booleanTrue when the channel is in the process of closingprotected booleanTrue when the SSL handshake is completeprotected SSLEngineResult.HandshakeStatusCurrent handshake status, updated during handshake processingprotected booleanTrue when a handshake wrap is pendingprotected ByteBufferNetwork input buffer for encrypted dataprotected ByteBufferNetwork output buffer for encrypted dataprotected booleanTrue when SNI processing is completeprotected SSLEngineSSL engine for this channelFields inherited from class NioChannel
bufHandler, emptyBuf, sc, socketWrapper -
Constructor Summary
ConstructorsConstructorDescriptionSecureNioChannel(SocketBufferHandler bufHandler, NioEndpoint endpoint) Creates a new secure NIO channel. -
Method Summary
Modifier and TypeMethodDescriptionvoidclose()Sends an SSL close message, will not physically close the connection here.voidclose(boolean force) Close the connection.protected booleanflush(ByteBuffer buf) Flushes the buffer to the network, non-blockingbooleanReturn true if the buffer wrote data.voidfree()Free the channel memoryReturns an empty byte buffer used for SSL wrap operations.intReturns the number of bytes remaining in the outbound buffer.Returns the SSL engine for this channel.Returns the SSL support object for this channel, ornullif the SSL engine has not been initialized.inthandshake(boolean read, boolean write) Performs SSL handshake, non-blocking, but performs NEED_TASK on the same thread.protected SSLEngineResulthandshakeUnwrap(boolean doread) Perform handshake unwrapprotected SSLEngineResulthandshakeWrap(boolean doWrite) Performs the WRAP functionbooleanIndicates whether the channel is in the process of closing.booleanIndicates whether the SSL handshake is complete.intread(ByteBuffer dst) Reads a sequence of bytes from this channel into the given buffer.longread(ByteBuffer[] dsts, int offset, int length) voidrehandshake(long timeout) Force a blocking handshake to take place for this key.voidreset(SocketChannel channel, NioEndpoint.NioSocketWrapper socketWrapper) Reset the channelprotected SSLEngineResult.HandshakeStatustasks()Executes all the tasks needed on the same thread.intwrite(ByteBuffer src) Writes a sequence of bytes to this channel from the given buffer.longwrite(ByteBuffer[] srcs, int offset, int length) Methods inherited from class NioChannel
checkInterruptStatus, getAppReadBufHandler, getBufHandler, getIOChannel, isOpen, read, setAppReadBufHandler, toString, write
-
Field Details
-
netInBuffer
Network input buffer for encrypted data -
netOutBuffer
Network output buffer for encrypted data -
sslEngine
SSL engine for this channel -
sniComplete
protected boolean sniCompleteTrue when SNI processing is complete -
handshakeComplete
protected boolean handshakeCompleteTrue when the SSL handshake is complete -
needHandshakeWrap
protected boolean needHandshakeWrapTrue when a handshake wrap is pending -
handshakeStatus
Current handshake status, updated during handshake processing -
closed
protected boolean closedTrue when the channel is closed -
closing
protected boolean closingTrue when the channel is in the process of closing
-
-
Constructor Details
-
SecureNioChannel
Creates a new secure NIO channel.- Parameters:
bufHandler- Buffer handler for the application buffersendpoint- The NIO endpoint managing this channel
-
-
Method Details
-
reset
public void reset(SocketChannel channel, NioEndpoint.NioSocketWrapper socketWrapper) throws IOException Description copied from class:NioChannelReset the channel- Overrides:
resetin classNioChannel- Parameters:
channel- the socket channelsocketWrapper- the socket wrapper- Throws:
IOException- If a problem was encountered resetting the channel
-
free
public void free()Description copied from class:NioChannelFree the channel memory- Overrides:
freein classNioChannel
-
flush
Flushes the buffer to the network, non-blocking- Parameters:
buf- ByteBuffer- Returns:
- boolean true if the buffer has been emptied out, false otherwise
- Throws:
IOException- An IO error occurred writing data
-
handshake
Performs SSL handshake, non-blocking, but performs NEED_TASK on the same thread. Hence, you should never call this method using your Acceptor thread, as you would slow down your system significantly. If the return value from this method is positive, the selection key should be registered interestOps given by the return value.- Overrides:
handshakein classNioChannel- Parameters:
read- boolean - true if the underlying channel is readablewrite- boolean - true if the underlying channel is writable- Returns:
- 0 if handshake is complete, -1 if an error (other than an IOException) occurred, otherwise it returns a SelectionKey interestOps value
- Throws:
IOException- If an I/O error occurs during the handshake or if the handshake fails during wrapping or unwrapping
-
rehandshake
Force a blocking handshake to take place for this key. This requires that both network and application buffers have been emptied out prior to this call taking place, or a IOException will be thrown.- Parameters:
timeout- - timeout in milliseconds for each socket operation- Throws:
IOException- - if an IO exception occurs or if application or network buffers contain dataSocketTimeoutException- - if a socket operation timed out
-
tasks
Executes all the tasks needed on the same thread.- Returns:
- the status
-
handshakeWrap
Performs the WRAP function- Parameters:
doWrite- boolean- Returns:
- the result
- Throws:
IOException- An IO error occurred
-
handshakeUnwrap
Perform handshake unwrap- Parameters:
doread- boolean- Returns:
- the result
- Throws:
IOException- An IO error occurred
-
getSSLSupport
Returns the SSL support object for this channel, ornullif the SSL engine has not been initialized.- Returns:
- the SSL support object, or
null
-
close
Sends an SSL close message, will not physically close the connection here.
To close the connection, you could do something likeclose(); while (isOpen() && !myTimeoutFunction()) Thread.sleep(25); if ( isOpen() ) close(true); //forces a close if you timed out- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceChannel- Specified by:
closein interfaceCloseable- Overrides:
closein classNioChannel- Throws:
IOException- if an I/O error occursIOException- if there is data on the outgoing network buffer, and we are unable to flush it
-
close
Description copied from class:NioChannelClose the connection.- Overrides:
closein classNioChannel- Parameters:
force- Should the underlying socket be forcibly closed?- Throws:
IOException- If closing the secure channel fails.
-
read
Reads a sequence of bytes from this channel into the given buffer.- Specified by:
readin interfaceReadableByteChannel- Overrides:
readin classNioChannel- Parameters:
dst- The buffer into which bytes are to be transferred- Returns:
- The number of bytes read, possibly zero, or
-1if the channel has reached end-of-stream - Throws:
IOException- If some other I/O error occursIllegalStateException- if the handshake was not completed
-
read
- Specified by:
readin interfaceScatteringByteChannel- Overrides:
readin classNioChannel- Throws:
IOException
-
write
Writes a sequence of bytes to this channel from the given buffer.- Specified by:
writein interfaceWritableByteChannel- Overrides:
writein classNioChannel- Parameters:
src- The buffer from which bytes are to be retrieved- Returns:
- The number of bytes written, possibly zero
- Throws:
IOException- If some other I/O error occurs
-
write
- Specified by:
writein interfaceGatheringByteChannel- Overrides:
writein classNioChannel- Throws:
IOException
-
getOutboundRemaining
public int getOutboundRemaining()Description copied from class:NioChannelReturns the number of bytes remaining in the outbound buffer.- Overrides:
getOutboundRemainingin classNioChannel- Returns:
- Always returns 0 for non-secure channels
-
flushOutbound
Description copied from class:NioChannelReturn true if the buffer wrote data. NO-OP for non-secure channel.- Overrides:
flushOutboundin classNioChannel- Returns:
- Always returns
falsefor non-secure channel - Throws:
IOException- Never for non-secure channel
-
isHandshakeComplete
public boolean isHandshakeComplete()Description copied from class:NioChannelIndicates whether the SSL handshake is complete.- Overrides:
isHandshakeCompletein classNioChannel- Returns:
truefor non-secure channels (no handshake required)
-
isClosing
public boolean isClosing()Description copied from class:NioChannelIndicates whether the channel is in the process of closing.- Overrides:
isClosingin classNioChannel- Returns:
falsefor non-secure channels
-
getSslEngine
Returns the SSL engine for this channel.- Returns:
- the SSL engine, or
nullif not yet created
-
getEmptyBuf
Returns an empty byte buffer used for SSL wrap operations.- Returns:
- an empty byte buffer
-