Package org.eclipse.remote.telnet.core
Class TelnetProtocol
java.lang.Object
java.lang.Thread
org.eclipse.remote.telnet.core.TelnetProtocol
- All Implemented Interfaces:
Runnable
This class encapsulates a TELNET connection to a remote server. It processes
incoming TELNET protocol data and generates outbound TELNET protocol data. It
also manages two sets of TelnetOption objects: one for the local endpoint and
one for the remote endpoint.
IMPORTANT: Understanding this code requires understanding the TELNET protocol and TELNET option processing, as defined in the RFCs listed below.
- See Also:
-
Nested Class Summary
Nested classes/interfaces inherited from class java.lang.Thread
Thread.Builder, Thread.State, Thread.UncaughtExceptionHandler -
Field Summary
FieldsModifier and TypeFieldDescriptionprotected static final intSize of buffer for processing data received from remote endpoint.protected OutputStreamThis field holds a reference to anOutputStreamobject used to send data to the client.protected intThis field holds the height of the Terminal screen in rows.protected booleanThis field is true if an error occurs while processing a subnegotiation command.protected ReadableByteChannelThis field holds a reference to anReadableByteChannelobject used to receive data from the remote endpoint.protected booleanUNDER CONSTRUCTIONprotected org.eclipse.remote.telnet.core.TelnetOption[]An array of TelnetOption objects representing the local endpoint's TELNET options.protected intThis field holds the index into arrayreceivedSubnegotiationof the next unused byte.protected byte[]Holds incoming network data after the TELNET protocol bytes have been processed and removed.protected StringBufferThis field holds a StringBuffer containing text recently received from the remote endpoint (after all TELNET protocol bytes have been processed and removed).protected ByteBufferHolds raw bytes received from the remote endpoint, prior to any TELNET protocol processing.protected byte[]An array of bytes that holds the TELNET subnegotiation command most recently received from the remote endpoint.protected booleanThis field is true if the remote endpoint is a TELNET server, false if not.protected org.eclipse.remote.telnet.core.TelnetOption[]An array of TelnetOption objects representing the remote endpoint's TELNET options.protected OutputStreamThis field holds a reference to anOutputStreamobject used to send data to the remote endpoint.protected TelnetCommandShellThis field holds a reference to theTelnetCommandShell.protected SocketThis method holds the Socket object for the TELNET connection.protected static final intTELNET connection state: Last byte processed was DO code.protected static final intTELNET connection state: Last byte processed was DONT code.protected static final intTELNET connection state: Last byte processed was IAC code.protected static final intTELNET connection state: Initial state.protected static final intTELNET connection state: Currently receiving sub-negotiation data.protected static final intTELNET connection state: Last byte processed was SB.protected static final intTELNET connection state: Last byte processed was WILL code.protected static final intTELNET connection state: Last byte processed was WONT code.static final byteCommand code: Abort Output.static final byteCommand code: Are You There.static final byteCommand code: Break.static final byteCommand code: Data Mark.static final byteCommand code: Do.static final byteCommand code: Don't.static final byteCommand code: Erase Character.static final byteCommand code: Erase Line.static final byteCommand code: Go Ahead.static final byteCommand code: Interpret As Command.static final byteCommand code: Interrupt Process.static final byteCommand code: IS.static final byteCommand code: No-op.static final byteOption code: Echo option.static final byteOption code: Negotitate About Window Size (NAWS)static final byteOption code: Suppress Go Ahead option.static final byteOption code: Terminal Typestatic final byteOption code: Transmit Binary option.static final byteCommand code: Subnegotiation Begin.static final byteCommand code: Subnegotiation End.static final byteCommand code: SEND.static final byteCommand code: Will.static final byteCommand code: Won't.protected intHolds the current state of the TELNET protocol processor.protected intThis field holds the width of the Terminal screen in columns.Fields inherited from class java.lang.Thread
MAX_PRIORITY, MIN_PRIORITY, NORM_PRIORITY -
Constructor Summary
ConstructorsConstructorDescriptionTelnetProtocol(Socket socket, TelnetCommandShell shell) This constructor just initializes some internal object state from its arguments. -
Method Summary
Modifier and TypeMethodDescriptionprotected voidThis method initializes the localOptions[] and remoteOptions[] arrays so that they contain references to TelnetOption objects representing our desired state for each option.booleanReturns true if the TCP connection represented by this object is connected, false otherwise.booleanReturns true if the TCP connection represented by this object is connected and the remote endpoint is a TELNET server, false otherwise.booleanReturns true if local echoing is enabled for this TCP connection, false otherwise.protected intprocessTelnetProtocol(int count) Process TELNET protocol data contained in the first count bytes of rawBytes.voidrun()This method runs in its own thread.voidsetClientOutputStream(OutputStream stream) voidsetTerminalSize(int newWidth, int newHeight) This method sets the terminal width and height to the supplied values.protected voidThis method is called whenever we receive a valid TELNET protocol command from the remote endpoint.Methods inherited from class java.lang.Thread
activeCount, checkAccess, clone, countStackFrames, currentThread, dumpStack, enumerate, getAllStackTraces, getContextClassLoader, getDefaultUncaughtExceptionHandler, getId, getName, getPriority, getStackTrace, getState, getThreadGroup, getUncaughtExceptionHandler, holdsLock, interrupt, interrupted, isAlive, isDaemon, isInterrupted, isVirtual, join, join, join, join, ofPlatform, ofVirtual, onSpinWait, resume, setContextClassLoader, setDaemon, setDefaultUncaughtExceptionHandler, setName, setPriority, setUncaughtExceptionHandler, sleep, sleep, sleep, start, startVirtualThread, stop, suspend, threadId, toString, yield
-
Field Details
-
STATE_INITIAL
protected static final int STATE_INITIALTELNET connection state: Initial state.- See Also:
-
STATE_IAC_RECEIVED
protected static final int STATE_IAC_RECEIVEDTELNET connection state: Last byte processed was IAC code. code.- See Also:
-
STATE_WILL_RECEIVED
protected static final int STATE_WILL_RECEIVEDTELNET connection state: Last byte processed was WILL code. code.- See Also:
-
STATE_WONT_RECEIVED
protected static final int STATE_WONT_RECEIVEDTELNET connection state: Last byte processed was WONT code.- See Also:
-
STATE_DO_RECEIVED
protected static final int STATE_DO_RECEIVEDTELNET connection state: Last byte processed was DO code.- See Also:
-
STATE_DONT_RECEIVED
protected static final int STATE_DONT_RECEIVEDTELNET connection state: Last byte processed was DONT code.- See Also:
-
STATE_SUBNEGOTIATION_STARTED
protected static final int STATE_SUBNEGOTIATION_STARTEDTELNET connection state: Last byte processed was SB.- See Also:
-
STATE_RECEIVING_SUBNEGOTIATION
protected static final int STATE_RECEIVING_SUBNEGOTIATIONTELNET connection state: Currently receiving sub-negotiation data.- See Also:
-
BUFFER_SIZE
protected static final int BUFFER_SIZESize of buffer for processing data received from remote endpoint.- See Also:
-
rawBytes
Holds raw bytes received from the remote endpoint, prior to any TELNET protocol processing. -
processedBytes
protected byte[] processedBytesHolds incoming network data after the TELNET protocol bytes have been processed and removed. -
processedStringBuffer
This field holds a StringBuffer containing text recently received from the remote endpoint (after all TELNET protocol bytes have been processed and removed). -
telnetState
protected int telnetStateHolds the current state of the TELNET protocol processor. -
remoteIsTelnetServer
protected boolean remoteIsTelnetServerThis field is true if the remote endpoint is a TELNET server, false if not. We set this to true if and only if the remote endpoint sends recognizable TELNET protocol data. We do not assume that the remote endpoint is a TELNET server just because it is listening on port 23. This allows us to successfully connect to a TELNET server listening on a port other than 23.When this field first changes from false to true, we send all WILL or DO commands to the remote endpoint.
- See Also:
-
localOptions
protected org.eclipse.remote.telnet.core.TelnetOption[] localOptionsAn array of TelnetOption objects representing the local endpoint's TELNET options. The array is indexed by the numeric TELNET option code. -
remoteOptions
protected org.eclipse.remote.telnet.core.TelnetOption[] remoteOptionsAn array of TelnetOption objects representing the remote endpoint's TELNET options. The array is indexed by the numeric TELNET option code. -
receivedSubnegotiation
protected byte[] receivedSubnegotiationAn array of bytes that holds the TELNET subnegotiation command most recently received from the remote endpoint. This array does _not_ include the leading IAC SB bytes, nor does it include the trailing IAC SE bytes. The first byte of this array is always a TELNET option code. -
nextSubnegotiationByteIndex
protected int nextSubnegotiationByteIndexThis field holds the index into arrayreceivedSubnegotiationof the next unused byte. This is used by methodprocessTelnetProtocol(int)when the state machine is in statesSTATE_SUBNEGOTIATION_STARTEDandSTATE_RECEIVING_SUBNEGOTIATION. -
ignoreSubnegotiation
protected boolean ignoreSubnegotiationThis field is true if an error occurs while processing a subnegotiation command.- See Also:
-
width
protected int widthThis field holds the width of the Terminal screen in columns. -
height
protected int heightThis field holds the height of the Terminal screen in rows. -
shell
This field holds a reference to theTelnetCommandShell. -
socket
This method holds the Socket object for the TELNET connection. -
inputChannel
This field holds a reference to anReadableByteChannelobject used to receive data from the remote endpoint. -
serverOutputStream
This field holds a reference to anOutputStreamobject used to send data to the remote endpoint. -
clientOutputStream
This field holds a reference to anOutputStreamobject used to send data to the client. -
localEcho
protected boolean localEchoUNDER CONSTRUCTION -
TELNET_SE
static final byte TELNET_SECommand code: Subnegotiation End.- See Also:
-
TELNET_NOP
static final byte TELNET_NOPCommand code: No-op.- See Also:
-
TELNET_DM
static final byte TELNET_DMCommand code: Data Mark.- See Also:
-
TELNET_BREAK
static final byte TELNET_BREAKCommand code: Break.- See Also:
-
TELNET_IP
static final byte TELNET_IPCommand code: Interrupt Process.- See Also:
-
TELNET_AO
static final byte TELNET_AOCommand code: Abort Output.- See Also:
-
TELNET_AYT
static final byte TELNET_AYTCommand code: Are You There.- See Also:
-
TELNET_EC
static final byte TELNET_ECCommand code: Erase Character.- See Also:
-
TELNET_EL
static final byte TELNET_ELCommand code: Erase Line.- See Also:
-
TELNET_GA
static final byte TELNET_GACommand code: Go Ahead.- See Also:
-
TELNET_SB
static final byte TELNET_SBCommand code: Subnegotiation Begin.- See Also:
-
TELNET_WILL
static final byte TELNET_WILLCommand code: Will.- See Also:
-
TELNET_WONT
static final byte TELNET_WONTCommand code: Won't.- See Also:
-
TELNET_DO
static final byte TELNET_DOCommand code: Do.- See Also:
-
TELNET_DONT
static final byte TELNET_DONTCommand code: Don't.- See Also:
-
TELNET_IAC
static final byte TELNET_IACCommand code: Interpret As Command.- See Also:
-
TELNET_IS
static final byte TELNET_ISCommand code: IS.- See Also:
-
TELNET_SEND
static final byte TELNET_SENDCommand code: SEND.- See Also:
-
TELNET_OPTION_TRANSMIT_BINARY
static final byte TELNET_OPTION_TRANSMIT_BINARYOption code: Transmit Binary option.- See Also:
-
TELNET_OPTION_ECHO
static final byte TELNET_OPTION_ECHOOption code: Echo option.- See Also:
-
TELNET_OPTION_SUPPRESS_GA
static final byte TELNET_OPTION_SUPPRESS_GAOption code: Suppress Go Ahead option.- See Also:
-
TELNET_OPTION_TERMINAL_TYPE
static final byte TELNET_OPTION_TERMINAL_TYPEOption code: Terminal Type- See Also:
-
TELNET_OPTION_NAWS
static final byte TELNET_OPTION_NAWSOption code: Negotitate About Window Size (NAWS)- See Also:
-
-
Constructor Details
-
TelnetProtocol
This constructor just initializes some internal object state from its arguments.- Throws:
IOException
-
-
Method Details
-
getOutputStream
-
setClientOutputStream
-
isConnected
public boolean isConnected()Returns true if the TCP connection represented by this object is connected, false otherwise. -
isRemoteTelnetServer
public boolean isRemoteTelnetServer()Returns true if the TCP connection represented by this object is connected and the remote endpoint is a TELNET server, false otherwise. -
setTerminalSize
public void setTerminalSize(int newWidth, int newHeight) This method sets the terminal width and height to the supplied values. If either new value differs from the corresponding old value, we initiate a NAWS subnegotiation to inform the remote endpoint of the new terminal size. -
localEcho
public boolean localEcho()Returns true if local echoing is enabled for this TCP connection, false otherwise. -
run
public void run()This method runs in its own thread. It reads raw bytes from the TELNET connection socket, processes any TELNET protocol bytes (and removes them), and passes the remaining bytes to a TerminalDisplay object for display. -
initializeOptions
protected void initializeOptions()This method initializes the localOptions[] and remoteOptions[] arrays so that they contain references to TelnetOption objects representing our desired state for each option. The goal is to achieve server-side echoing, suppression of Go Aheads, and to send the local terminal type and size to the remote endpoint. -
processTelnetProtocol
protected int processTelnetProtocol(int count) Process TELNET protocol data contained in the first count bytes of rawBytes. This function preserves its state between calls, because a multi-byte TELNET command might be split between two (or more) calls to this function. The state is preserved in field telnetState. This function implements an FSA that recognizes TELNET option codes. TELNET option sub-negotiation is delegated to instances of TelnetOption.- Returns:
- The number of bytes remaining in the buffer after removing all TELNET protocol bytes.
-
telnetServerDetected
protected void telnetServerDetected()This method is called whenever we receive a valid TELNET protocol command from the remote endpoint. When it is called for the first time for this connection, we negotiate all options that we desire to be enabled.This method does not negotiate options that we do not desire to be enabled, because all options are initially disabled.
-