Class TelnetIO


  • public class TelnetIO
    extends java.lang.Object
    Class that represents the TelnetIO implementation. It contains an inner IACHandler class to handle the telnet protocol level communication.

    Although supposed to work full-duplex, we only process the telnet protocol layer communication in case of reading requests from the higher levels. This is the only way to meet the one thread per connection requirement.

    The output is done via byte-oriented streams, definately suitable for the telnet protocol. The format of the output is UTF-8 (Unicode), which is a standard and supported by any telnet client, including the ones included in Microsoft OS's.

    Notes:
    • The underlying output is buffered, to ensure that all bytes written are send, the flush() method has to be called.
    • This low-level routines ensure nice multithreading behaviour on I/O. Neither large outputs, nor input sequences excuted by the connection thread can hog the system.
    Version:
    2.0 (16/07/2006)
    • Field Summary

      Fields 
      Modifier and Type Field Description
      protected static int AO
      Abort Output
      protected static int AUTHENTICATION  
      protected static int AYT
      Are You There
      protected static int BRK
      Break
      private Connection connection  
      private ConnectionData connectionData  
      private boolean crFlag  
      private static int DEFAULT_HEIGHT  
      private static int DEFAULT_WIDTH  
      protected static int DM
      Data mark its the data part of a SYNCH which helps to clean up the buffers between Telnet Server <-> Telnet Client.
      protected static int DO
      Negotiation: Do option
      protected static int DONT
      Negotiation: Dont do option
      protected static int EC
      Erase Char
      protected static int ECHO
      Telnet Option: ECHO
      protected static int EL
      Erase Line
      protected static int ENCRYPT  
      protected static int EXT_ASCII
      Unused
      protected static int GA
      Go Ahead
      Newer Telnets do not make use of this option that allows a specific communication mode.
      protected static int IAC
      Interpret As Command
      private TelnetIO.IACHandler iacHandler  
      private java.io.DataInputStream in  
      private boolean initializing  
      protected static int IP
      Interrupt Process
      protected static int IS
      TTYPE subnegotiation: IS
      protected static int LINEMODE
      Telnet Option: Linemode
      protected static int LM_EDIT  
      protected static int LM_FORWARDMASK  
      protected static int LM_MODE  
      protected static int LM_MODEACK
      Following methods implement init/request/answer procedures for telnet protocol level communication.
      protected static int LM_SLC  
      protected static int LM_SLC_ABORT
      The following implement the NVT (network virtual terminal) which offers the concept of a simple "printer".
      protected static int LM_SLC_ACK  
      protected static int LM_SLC_AO  
      protected static int LM_SLC_AYT  
      protected static int LM_SLC_BRK  
      protected static int LM_SLC_CANTCHANGE
      Constants declaration
      protected static int LM_SLC_DEFAULT  
      protected static int LM_SLC_EOF  
      protected static int LM_SLC_EOR  
      protected static int LM_SLC_FLUSHIN  
      protected static int LM_SLC_FLUSHOUT  
      protected static int LM_SLC_IP  
      protected static int LM_SLC_LEVELBITS  
      protected static int LM_SLC_NOSUPPORT  
      protected static int LM_SLC_SUSP  
      protected static int LM_SLC_SYNCH  
      protected static int LM_SLC_VALUE
      End telnet protocol level communication methods
      protected static int LM_TRAPSIG  
      private java.net.InetAddress localAddress  
      private static java.util.logging.Logger LOG  
      protected static int LOGOUT
      Telnet Option: Logout
      This allows nice goodbye to time-outed or unwanted clients.
      protected static int NAWS
      Telnet Option: Negotiate About Window Size
      Server request is IAC DO NAWS Client response contains subnegotiation with data (columns, rows).
      protected static int NE_ESC
      The following options are options for which we also support subnegotiation based upon the telnet protocol specification.
      protected static int NE_IN_END  
      protected static int NE_IN_ERROR  
      protected static int NE_INFO  
      protected static int NE_USERVAR  
      protected static int NE_VALUE  
      protected static int NE_VAR
      The following are constants for supported options, which can be negotiated based upon the telnet protocol specification.
      protected static int NE_VAR_DEFINED  
      protected static int NE_VAR_DEFINED_EMPTY  
      protected static int NE_VAR_NAME_MAXLENGTH  
      protected static int NE_VAR_OK  
      protected static int NE_VAR_UNDEFINED  
      protected static int NE_VAR_VALUE_MAXLENGTH  
      protected static int NEWENV
      Telnet Option: Environment
      private boolean noIac  
      protected static int NOP
      No operation
      private java.io.DataOutputStream out  
      protected static int SB
      Marks start of a subnegotiation.
      protected static int SE
      Marks end of subnegotiation.
      protected static int SEND
      TTYPE subnegotiation: SEND
      protected static int SEND_LOC  
      private static int SMALLEST_BELIEVABLE_HEIGHT  
      private static int SMALLEST_BELIEVABLE_WIDTH
      Window Size Constants
      protected static int SUPGA
      Telnet Option: SUPress Go Ahead
      This will be negotiated, all new telnet protocol implementations are recommended to do this.
      protected static int TTYPE
      Telnet Option: Terminal TYPE
      Server request contains subnegotiation SEND Client response contains subnegotiation with data IS,terminal type string
      protected static int WILL
      Negotiation: Will do option
      protected static int WONT
      Negotiation: Wont do option
    • Constructor Summary

      Constructors 
      Constructor Description
      TelnetIO()
      Creates a TelnetIO object for the given connection.
      Input- and OutputStreams are properly set and the primary telnet protocol initialization is carried out by the inner IACHandler class.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void closeInput()
      Method to close the underlying inputstream to free system resources.
      Most likely only to be called by the ConnectionManager upon clean up of connections that ended or died.
      void closeOutput()
      Method to close the underlying output stream to free system resources.
      Most likely only to be called by the ConnectionManager upon clean up of connections that ended or died.
      void flush()
      Method to flush all buffered output.
      private void IamHere()
      Method that represents the answer to the AreYouThere question of the telnet protocol specification
      void initIO()  
      private void initTelnetCommunication()
      Method that initializes the telnet communication layer.
      private void nvtBreak()
      Network virtual terminal break.
      private int rawread()
      Method to read a raw byte from the InputStream.
      Telnet protocol layer communication is filtered and processed here.
      private void rawWrite​(int i)  
      int read()
      Method to read a byte from the InputStream.
      private int read16int()
      This method reads an unsigned 16bit Integer from the stream, its here for getting the NAWS Data Values for height and width.
      void setConnection​(Connection con)  
      void setEcho​(boolean b)  
      private void setTerminalGeometry​(int width, int height)
      Method that checks reported terminal sizes and sets the asserted values in the ConnectionData instance associated with the connection.
      private int stripCRSeq​(int input)
      Checks for the telnet protocol specified CR followed by NULL or LF
      Subsequently reads for the next byte and forwards only a ENTER represented by LF internally.
      void write​(byte b)
      Method to output a byte.
      void write​(byte[] sequence)
      Method to write an array of bytes.
      void write​(char ch)
      Method to write a char.
      void write​(int i)
      Method to output an int.
      void write​(int[] sequence)
      Method to output an array of int' s.
      void write​(java.lang.String str)
      Method to output a string.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • GA

        protected static final int GA
        Go Ahead
        Newer Telnets do not make use of this option that allows a specific communication mode.
        See Also:
        Constant Field Values
      • DM

        protected static final int DM
        Data mark its the data part of a SYNCH which helps to clean up the buffers between Telnet Server <-> Telnet Client.
        It should work like this we send a TCP urgent package and <IAC> <DM> the receiver should get the urgent package (SYNCH) and just discard everything until he receives our <IAC> <DM>.
        Remark:
        1. can we send a TCP urgent package?
        2. can we make use of the thing at all?
        See Also:
        Constant Field Values
      • SUPGA

        protected static final int SUPGA
        Telnet Option: SUPress Go Ahead
        This will be negotiated, all new telnet protocol implementations are recommended to do this.
        See Also:
        Constant Field Values
      • NAWS

        protected static final int NAWS
        Telnet Option: Negotiate About Window Size
        • Server request is IAC DO NAWS
        • Client response contains subnegotiation with data (columns, rows).
        See Also:
        Constant Field Values
      • TTYPE

        protected static final int TTYPE
        Telnet Option: Terminal TYPE
        • Server request contains subnegotiation SEND
        • Client response contains subnegotiation with data IS,terminal type string
        See Also:
        Constant Field Values
      • LOGOUT

        protected static final int LOGOUT
        Telnet Option: Logout
        This allows nice goodbye to time-outed or unwanted clients.
        See Also:
        Constant Field Values
      • LINEMODE

        protected static final int LINEMODE
        Telnet Option: Linemode

        The infamous line mode option.

        See Also:
        Constant Field Values
      • LM_MODEACK

        protected static final int LM_MODEACK
        Following methods implement init/request/answer procedures for telnet protocol level communication.
        See Also:
        Constant Field Values
      • LM_SLC_VALUE

        protected static final int LM_SLC_VALUE
        End telnet protocol level communication methods
        See Also:
        Constant Field Values
      • LM_SLC_CANTCHANGE

        protected static final int LM_SLC_CANTCHANGE
        Constants declaration
        See Also:
        Constant Field Values
      • LM_SLC_ABORT

        protected static final int LM_SLC_ABORT
        The following implement the NVT (network virtual terminal) which offers the concept of a simple "printer". They are the basical meanings of control possibilities on a standard telnet implementation.
        See Also:
        Constant Field Values
      • NE_VAR

        protected static final int NE_VAR
        The following are constants for supported options, which can be negotiated based upon the telnet protocol specification.
        See Also:
        Constant Field Values
      • NE_ESC

        protected static final int NE_ESC
        The following options are options for which we also support subnegotiation based upon the telnet protocol specification.
        See Also:
        Constant Field Values
      • NE_VAR_NAME_MAXLENGTH

        protected static final int NE_VAR_NAME_MAXLENGTH
        See Also:
        Constant Field Values
      • NE_VAR_VALUE_MAXLENGTH

        protected static final int NE_VAR_VALUE_MAXLENGTH
        See Also:
        Constant Field Values
      • LOG

        private static final java.util.logging.Logger LOG
      • SMALLEST_BELIEVABLE_WIDTH

        private static final int SMALLEST_BELIEVABLE_WIDTH
        Window Size Constants
        See Also:
        Constant Field Values
      • SMALLEST_BELIEVABLE_HEIGHT

        private static final int SMALLEST_BELIEVABLE_HEIGHT
        See Also:
        Constant Field Values
      • out

        private java.io.DataOutputStream out
      • in

        private java.io.DataInputStream in
      • localAddress

        private java.net.InetAddress localAddress
      • noIac

        private boolean noIac
      • initializing

        private boolean initializing
      • crFlag

        private boolean crFlag
    • Constructor Detail

      • TelnetIO

        public TelnetIO()
        Creates a TelnetIO object for the given connection.
        Input- and OutputStreams are properly set and the primary telnet protocol initialization is carried out by the inner IACHandler class.
    • Method Detail

      • initIO

        public void initIO()
                    throws java.io.IOException
        Throws:
        java.io.IOException
      • setConnection

        public void setConnection​(Connection con)
      • write

        public void write​(byte b)
                   throws java.io.IOException
        Method to output a byte. Ensures that CR(\r) is never send alone,but CRLF(\r\n), which is a rule of the telnet protocol.
        Parameters:
        b - Byte to be written.
        Throws:
        java.io.IOException - if an error occurs
      • write

        public void write​(int i)
                   throws java.io.IOException
        Method to output an int.
        Parameters:
        i - Integer to be written.
        Throws:
        java.io.IOException - if an error occurs
      • write

        public void write​(byte[] sequence)
                   throws java.io.IOException
        Method to write an array of bytes.
        Parameters:
        sequence - byte[] to be written.
        Throws:
        java.io.IOException - if an error occurs
      • write

        public void write​(int[] sequence)
                   throws java.io.IOException
        Method to output an array of int' s.
        Parameters:
        sequence - int [] to write
        Throws:
        java.io.IOException - if an error occurs
      • write

        public void write​(char ch)
                   throws java.io.IOException
        Method to write a char.
        Parameters:
        ch - char to be written.
        Throws:
        java.io.IOException - if an error occurs
      • write

        public void write​(java.lang.String str)
                   throws java.io.IOException
        Method to output a string.
        Parameters:
        str - String to be written.
        Throws:
        java.io.IOException - if an error occurs
      • flush

        public void flush()
                   throws java.io.IOException
        Method to flush all buffered output.
        Throws:
        java.io.IOException - if an error occurs
      • closeOutput

        public void closeOutput()
        Method to close the underlying output stream to free system resources.
        Most likely only to be called by the ConnectionManager upon clean up of connections that ended or died.
      • rawWrite

        private void rawWrite​(int i)
                       throws java.io.IOException
        Throws:
        java.io.IOException
      • read

        public int read()
                 throws java.io.IOException
        Method to read a byte from the InputStream. Invokes the IACHandler upon IAC (Byte=255).
        Returns:
        int read from stream.
        Throws:
        java.io.IOException - if an error occurs
      • closeInput

        public void closeInput()
        Method to close the underlying inputstream to free system resources.
        Most likely only to be called by the ConnectionManager upon clean up of connections that ended or died.
      • read16int

        private int read16int()
                       throws java.io.IOException
        This method reads an unsigned 16bit Integer from the stream, its here for getting the NAWS Data Values for height and width.
        Throws:
        java.io.IOException - if an error occurs
      • rawread

        private int rawread()
                     throws java.io.IOException
        Method to read a raw byte from the InputStream.
        Telnet protocol layer communication is filtered and processed here.
        Returns:
        int read from stream.
        Throws:
        java.io.IOException - if an error occurs
      • stripCRSeq

        private int stripCRSeq​(int input)
                        throws java.io.IOException
        Checks for the telnet protocol specified CR followed by NULL or LF
        Subsequently reads for the next byte and forwards only a ENTER represented by LF internally.
        Throws:
        java.io.IOException - if an error occurs
      • initTelnetCommunication

        private void initTelnetCommunication()
        Method that initializes the telnet communication layer.
      • IamHere

        private void IamHere()
        Method that represents the answer to the AreYouThere question of the telnet protocol specification

        Output of the String [HostAdress:Yes]

      • nvtBreak

        private void nvtBreak()
        Network virtual terminal break.
      • setTerminalGeometry

        private void setTerminalGeometry​(int width,
                                         int height)
        Method that checks reported terminal sizes and sets the asserted values in the ConnectionData instance associated with the connection.
        Parameters:
        width - Integer that represents the Window width in chars
        height - Integer that represents the Window height in chars
      • setEcho

        public void setEcho​(boolean b)