Class UnifiedServerSocket.UnifiedSocket
- java.lang.Object
-
- java.net.Socket
-
- org.apache.zookeeper.server.quorum.UnifiedServerSocket.UnifiedSocket
-
- All Implemented Interfaces:
java.io.Closeable
,java.lang.AutoCloseable
- Enclosing class:
- UnifiedServerSocket
public static class UnifiedServerSocket.UnifiedSocket extends java.net.Socket
The result of calling accept() on a UnifiedServerSocket. This is a Socket that doesn't know if it's using plaintext or SSL/TLS at the time when it is created. Calling a method that indicates a desire to read or write from the socket will cause the socket to detect if the connected client is attempting to establish a TLS or plaintext connection. This is done by doing a blocking read of 5 bytes off the socket and checking if the bytes look like the start of a TLS ClientHello message. If it looks like the client is attempting to connect with TLS, the internal socket is upgraded to a SSLSocket. If not, any bytes read from the socket are pushed back to the input stream, and the socket continues to be treated as a plaintext socket. The methods that trigger this behavior are: Calling other socket methods (i.e option setters such asSocket.setTcpNoDelay(boolean)
) does not trigger mode detection. Because detecting the mode is a potentially blocking operation, it should not be done in the accepting thread. Attempting to read from or write to the socket in the accepting thread opens the caller up to a denial-of-service attack, in which a client connects and then does nothing. This would prevent any other clients from connecting. Passing the socket returned by accept() to a separate thread which handles all read and write operations protects against this DoS attack. Callers can check if the socket has been upgraded to TLS by callingisSecureSocket()
, and can get the underlying SSLSocket by callinggetSslSocket()
.
-
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
bind(java.net.SocketAddress bindpoint)
SeeSocket.bind(SocketAddress)
.void
close()
SeeSocket.close()
.void
connect(java.net.SocketAddress endpoint)
SeeSocket.connect(SocketAddress)
.void
connect(java.net.SocketAddress endpoint, int timeout)
SeeSocket.connect(SocketAddress, int)
.java.nio.channels.SocketChannel
getChannel()
SeeSocket.getChannel()
.java.net.InetAddress
getInetAddress()
SeeSocket.getInetAddress()
.java.io.InputStream
getInputStream()
SeeSocket.getInputStream()
.boolean
getKeepAlive()
SeeSocket.getKeepAlive()
.java.net.InetAddress
getLocalAddress()
SeeSocket.getLocalAddress()
.int
getLocalPort()
SeeSocket.getLocalPort()
.java.net.SocketAddress
getLocalSocketAddress()
SeeSocket.getLocalSocketAddress()
.boolean
getOOBInline()
SeeSocket.getOOBInline()
.java.io.OutputStream
getOutputStream()
SeeSocket.getOutputStream()
.int
getPort()
SeeSocket.getPort()
.int
getReceiveBufferSize()
SeeSocket.getReceiveBufferSize()
.java.net.SocketAddress
getRemoteSocketAddress()
SeeSocket.getRemoteSocketAddress()
.boolean
getReuseAddress()
SeeSocket.getReuseAddress()
.int
getSendBufferSize()
SeeSocket.getSendBufferSize()
.int
getSoLinger()
SeeSocket.getSoLinger()
.int
getSoTimeout()
SeeSocket.getSoTimeout()
.javax.net.ssl.SSLSocket
getSslSocket()
Returns the underlying SSLSocket if the mode is TLS.boolean
getTcpNoDelay()
SeeSocket.getTcpNoDelay()
.int
getTrafficClass()
SeeSocket.getTrafficClass()
.boolean
isBound()
SeeSocket.isBound()
.boolean
isClosed()
SeeSocket.isClosed()
.boolean
isConnected()
SeeSocket.isConnected()
.boolean
isInputShutdown()
SeeSocket.isInputShutdown()
.boolean
isModeKnown()
Returns true if the socket mode is not yet known.boolean
isOutputShutdown()
SeeSocket.isOutputShutdown()
.boolean
isPlaintextSocket()
Returns true if the socket mode has been determined to be PLAINTEXT.boolean
isSecureSocket()
Returns true if the socket mode has been determined to be TLS.void
sendUrgentData(int data)
SeeSocket.sendUrgentData(int)
.void
setKeepAlive(boolean on)
SeeSocket.setKeepAlive(boolean)
.void
setOOBInline(boolean on)
SeeSocket.setOOBInline(boolean)
.void
setPerformancePreferences(int connectionTime, int latency, int bandwidth)
SeeSocket.setPerformancePreferences(int, int, int)
.void
setReceiveBufferSize(int size)
SeeSocket.setReceiveBufferSize(int)
.void
setReuseAddress(boolean on)
SeeSocket.setReuseAddress(boolean)
.void
setSendBufferSize(int size)
SeeSocket.setSendBufferSize(int)
.void
setSoLinger(boolean on, int linger)
SeeSocket.setSoLinger(boolean, int)
.void
setSoTimeout(int timeout)
SeeSocket.setSoTimeout(int)
.void
setTcpNoDelay(boolean on)
SeeSocket.setTcpNoDelay(boolean)
.void
setTrafficClass(int tc)
SeeSocket.setTrafficClass(int)
.void
shutdownInput()
SeeSocket.shutdownInput()
.void
shutdownOutput()
SeeSocket.shutdownOutput()
.java.lang.String
toString()
SeeSocket.toString()
.
-
-
-
Method Detail
-
isSecureSocket
public boolean isSecureSocket()
Returns true if the socket mode has been determined to be TLS.- Returns:
- true if the mode is TLS, false if it is UNKNOWN or PLAINTEXT.
-
isPlaintextSocket
public boolean isPlaintextSocket()
Returns true if the socket mode has been determined to be PLAINTEXT.- Returns:
- true if the mode is PLAINTEXT, false if it is UNKNOWN or TLS.
-
isModeKnown
public boolean isModeKnown()
Returns true if the socket mode is not yet known.- Returns:
- true if the mode is UNKNOWN, false if it is PLAINTEXT or TLS.
-
getSslSocket
public javax.net.ssl.SSLSocket getSslSocket() throws java.io.IOException
Returns the underlying SSLSocket if the mode is TLS. If the mode is UNKNOWN, causes mode detection which is a potentially blocking operation. If the mode ends up being PLAINTEXT, this will throw a SocketException, so callers are advised to only call this method after checking thatisSecureSocket()
returned true.- Returns:
- the underlying SSLSocket if the mode is known to be TLS.
- Throws:
java.io.IOException
- if detecting the socket mode failsjava.net.SocketException
- if the mode is PLAINTEXT.
-
connect
public void connect(java.net.SocketAddress endpoint) throws java.io.IOException
SeeSocket.connect(SocketAddress)
. Calling this method does not trigger mode detection.- Overrides:
connect
in classjava.net.Socket
- Throws:
java.io.IOException
-
connect
public void connect(java.net.SocketAddress endpoint, int timeout) throws java.io.IOException
SeeSocket.connect(SocketAddress, int)
. Calling this method does not trigger mode detection.- Overrides:
connect
in classjava.net.Socket
- Throws:
java.io.IOException
-
bind
public void bind(java.net.SocketAddress bindpoint) throws java.io.IOException
SeeSocket.bind(SocketAddress)
. Calling this method does not trigger mode detection.- Overrides:
bind
in classjava.net.Socket
- Throws:
java.io.IOException
-
getInetAddress
public java.net.InetAddress getInetAddress()
SeeSocket.getInetAddress()
. Calling this method does not trigger mode detection.- Overrides:
getInetAddress
in classjava.net.Socket
-
getLocalAddress
public java.net.InetAddress getLocalAddress()
SeeSocket.getLocalAddress()
. Calling this method does not trigger mode detection.- Overrides:
getLocalAddress
in classjava.net.Socket
-
getPort
public int getPort()
SeeSocket.getPort()
. Calling this method does not trigger mode detection.- Overrides:
getPort
in classjava.net.Socket
-
getLocalPort
public int getLocalPort()
SeeSocket.getLocalPort()
. Calling this method does not trigger mode detection.- Overrides:
getLocalPort
in classjava.net.Socket
-
getRemoteSocketAddress
public java.net.SocketAddress getRemoteSocketAddress()
SeeSocket.getRemoteSocketAddress()
. Calling this method does not trigger mode detection.- Overrides:
getRemoteSocketAddress
in classjava.net.Socket
-
getLocalSocketAddress
public java.net.SocketAddress getLocalSocketAddress()
SeeSocket.getLocalSocketAddress()
. Calling this method does not trigger mode detection.- Overrides:
getLocalSocketAddress
in classjava.net.Socket
-
getChannel
public java.nio.channels.SocketChannel getChannel()
SeeSocket.getChannel()
. Calling this method does not trigger mode detection.- Overrides:
getChannel
in classjava.net.Socket
-
getInputStream
public java.io.InputStream getInputStream() throws java.io.IOException
SeeSocket.getInputStream()
. If the socket mode has not yet been detected, the first read from the returned input stream will trigger mode detection, which is a potentially blocking operation. This means the accept() thread should avoid reading from this input stream if possible.- Overrides:
getInputStream
in classjava.net.Socket
- Throws:
java.io.IOException
-
getOutputStream
public java.io.OutputStream getOutputStream() throws java.io.IOException
SeeSocket.getOutputStream()
. If the socket mode has not yet been detected, the first read from the returned input stream will trigger mode detection, which is a potentially blocking operation. This means the accept() thread should avoid reading from this input stream if possible.- Overrides:
getOutputStream
in classjava.net.Socket
- Throws:
java.io.IOException
-
setTcpNoDelay
public void setTcpNoDelay(boolean on) throws java.net.SocketException
SeeSocket.setTcpNoDelay(boolean)
. Calling this method does not trigger mode detection.- Overrides:
setTcpNoDelay
in classjava.net.Socket
- Throws:
java.net.SocketException
-
getTcpNoDelay
public boolean getTcpNoDelay() throws java.net.SocketException
SeeSocket.getTcpNoDelay()
. Calling this method does not trigger mode detection.- Overrides:
getTcpNoDelay
in classjava.net.Socket
- Throws:
java.net.SocketException
-
setSoLinger
public void setSoLinger(boolean on, int linger) throws java.net.SocketException
SeeSocket.setSoLinger(boolean, int)
. Calling this method does not trigger mode detection.- Overrides:
setSoLinger
in classjava.net.Socket
- Throws:
java.net.SocketException
-
getSoLinger
public int getSoLinger() throws java.net.SocketException
SeeSocket.getSoLinger()
. Calling this method does not trigger mode detection.- Overrides:
getSoLinger
in classjava.net.Socket
- Throws:
java.net.SocketException
-
sendUrgentData
public void sendUrgentData(int data) throws java.io.IOException
SeeSocket.sendUrgentData(int)
. Calling this method triggers mode detection, which is a potentially blocking operation, so it should not be done in the accept() thread.- Overrides:
sendUrgentData
in classjava.net.Socket
- Throws:
java.io.IOException
-
setOOBInline
public void setOOBInline(boolean on) throws java.net.SocketException
SeeSocket.setOOBInline(boolean)
. Calling this method does not trigger mode detection.- Overrides:
setOOBInline
in classjava.net.Socket
- Throws:
java.net.SocketException
-
getOOBInline
public boolean getOOBInline() throws java.net.SocketException
SeeSocket.getOOBInline()
. Calling this method does not trigger mode detection.- Overrides:
getOOBInline
in classjava.net.Socket
- Throws:
java.net.SocketException
-
setSoTimeout
public void setSoTimeout(int timeout) throws java.net.SocketException
SeeSocket.setSoTimeout(int)
. Calling this method does not trigger mode detection.- Overrides:
setSoTimeout
in classjava.net.Socket
- Throws:
java.net.SocketException
-
getSoTimeout
public int getSoTimeout() throws java.net.SocketException
SeeSocket.getSoTimeout()
. Calling this method does not trigger mode detection.- Overrides:
getSoTimeout
in classjava.net.Socket
- Throws:
java.net.SocketException
-
setSendBufferSize
public void setSendBufferSize(int size) throws java.net.SocketException
SeeSocket.setSendBufferSize(int)
. Calling this method does not trigger mode detection.- Overrides:
setSendBufferSize
in classjava.net.Socket
- Throws:
java.net.SocketException
-
getSendBufferSize
public int getSendBufferSize() throws java.net.SocketException
SeeSocket.getSendBufferSize()
. Calling this method does not trigger mode detection.- Overrides:
getSendBufferSize
in classjava.net.Socket
- Throws:
java.net.SocketException
-
setReceiveBufferSize
public void setReceiveBufferSize(int size) throws java.net.SocketException
SeeSocket.setReceiveBufferSize(int)
. Calling this method does not trigger mode detection.- Overrides:
setReceiveBufferSize
in classjava.net.Socket
- Throws:
java.net.SocketException
-
getReceiveBufferSize
public int getReceiveBufferSize() throws java.net.SocketException
SeeSocket.getReceiveBufferSize()
. Calling this method does not trigger mode detection.- Overrides:
getReceiveBufferSize
in classjava.net.Socket
- Throws:
java.net.SocketException
-
setKeepAlive
public void setKeepAlive(boolean on) throws java.net.SocketException
SeeSocket.setKeepAlive(boolean)
. Calling this method does not trigger mode detection.- Overrides:
setKeepAlive
in classjava.net.Socket
- Throws:
java.net.SocketException
-
getKeepAlive
public boolean getKeepAlive() throws java.net.SocketException
SeeSocket.getKeepAlive()
. Calling this method does not trigger mode detection.- Overrides:
getKeepAlive
in classjava.net.Socket
- Throws:
java.net.SocketException
-
setTrafficClass
public void setTrafficClass(int tc) throws java.net.SocketException
SeeSocket.setTrafficClass(int)
. Calling this method does not trigger mode detection.- Overrides:
setTrafficClass
in classjava.net.Socket
- Throws:
java.net.SocketException
-
getTrafficClass
public int getTrafficClass() throws java.net.SocketException
SeeSocket.getTrafficClass()
. Calling this method does not trigger mode detection.- Overrides:
getTrafficClass
in classjava.net.Socket
- Throws:
java.net.SocketException
-
setReuseAddress
public void setReuseAddress(boolean on) throws java.net.SocketException
SeeSocket.setReuseAddress(boolean)
. Calling this method does not trigger mode detection.- Overrides:
setReuseAddress
in classjava.net.Socket
- Throws:
java.net.SocketException
-
getReuseAddress
public boolean getReuseAddress() throws java.net.SocketException
SeeSocket.getReuseAddress()
. Calling this method does not trigger mode detection.- Overrides:
getReuseAddress
in classjava.net.Socket
- Throws:
java.net.SocketException
-
close
public void close() throws java.io.IOException
SeeSocket.close()
. Calling this method does not trigger mode detection.- Specified by:
close
in interfacejava.lang.AutoCloseable
- Specified by:
close
in interfacejava.io.Closeable
- Overrides:
close
in classjava.net.Socket
- Throws:
java.io.IOException
-
shutdownInput
public void shutdownInput() throws java.io.IOException
SeeSocket.shutdownInput()
. Calling this method does not trigger mode detection.- Overrides:
shutdownInput
in classjava.net.Socket
- Throws:
java.io.IOException
-
shutdownOutput
public void shutdownOutput() throws java.io.IOException
SeeSocket.shutdownOutput()
. Calling this method does not trigger mode detection.- Overrides:
shutdownOutput
in classjava.net.Socket
- Throws:
java.io.IOException
-
toString
public java.lang.String toString()
SeeSocket.toString()
. Calling this method does not trigger mode detection.- Overrides:
toString
in classjava.net.Socket
-
isConnected
public boolean isConnected()
SeeSocket.isConnected()
. Calling this method does not trigger mode detection.- Overrides:
isConnected
in classjava.net.Socket
-
isBound
public boolean isBound()
SeeSocket.isBound()
. Calling this method does not trigger mode detection.- Overrides:
isBound
in classjava.net.Socket
-
isClosed
public boolean isClosed()
SeeSocket.isClosed()
. Calling this method does not trigger mode detection.- Overrides:
isClosed
in classjava.net.Socket
-
isInputShutdown
public boolean isInputShutdown()
SeeSocket.isInputShutdown()
. Calling this method does not trigger mode detection.- Overrides:
isInputShutdown
in classjava.net.Socket
-
isOutputShutdown
public boolean isOutputShutdown()
SeeSocket.isOutputShutdown()
. Calling this method does not trigger mode detection.- Overrides:
isOutputShutdown
in classjava.net.Socket
-
setPerformancePreferences
public void setPerformancePreferences(int connectionTime, int latency, int bandwidth)
SeeSocket.setPerformancePreferences(int, int, int)
. Calling this method does not trigger mode detection.- Overrides:
setPerformancePreferences
in classjava.net.Socket
-
-