Embedded Telnet Client


Topics:

Overview
Constants
Enumerations
Functions


Overview

The embedded Telnet client is a cross platform Telnet implementation used with applications that require use of the Telnet protocol as a keyboard and screen interface to the TCP stack. The gxsTelnet Client class is derived from the gxSocket class and supports NVT (Network Virtual Terminal) terminal negotiation. The character set is restricted to 7 bits sent as a byte value with the most significant bit cleared. All terminal emulations other then NVT are handled by terminal type set in the environment by the TERM variable. This implementation works well in a UNIX to UNIX environment. However, in a Windows to UNIX environment your application must provide the API to perform specific terminal emulations.


Constants

// Telnet command codes
const unsigned char gxsTEL_IAC  = 255; // Interpret as command escape sequence
                                       // Prefix to all telnet commands 
const unsigned char gxsTEL_DONT = 254; // You are not to use this option
const unsigned char gxsTEL_DO   = 253; // Please, you use this option
const unsigned char gxsTEL_WONT = 252; // I won't use option 
const unsigned char gxsTEL_WILL = 251; // I will use option 
const unsigned char gxsTEL_SB   = 250; // Subnegotiate
const unsigned char gxsTEL_GA   = 249; // Go ahead
const unsigned char gxsTEL_EL   = 248; // Erase line          
const unsigned char gxsTEL_EC   = 247; // Erase character
const unsigned char gxsTEL_AYT  = 246; // Are you there
const unsigned char gxsTEL_AO   = 245; // Abort output
const unsigned char gxsTEL_IP   = 244; // Interrupt process
const unsigned char gxsTEL_BRK  = 243; // Break
const unsigned char gxsTEL_DM   = 242; // Data mark
const unsigned char gxsTEL_NOP  = 241; // No operation.
const unsigned char gxsTEL_SE   = 240; // End of subnegotiation
const unsigned char gxsTEL_EOR  = 239; // End of record
const unsigned char gxsTEL_ABORT = 238; // About process
const unsigned char gxsTEL_SUSP  = 237; // Suspend process
const unsigned char gxsTEL_xEOF  = 236; // End of file: EOF already used

// Telnet options 
const unsigned char gxsTELOPT_BIN    = 0;   // Binary transmission
const unsigned char gxsTELOPT_ECHO   = 1;   // Echo
const unsigned char gxsTELOPT_RECN   = 2;   // Reconnection
const unsigned char gxsTELOPT_SUPP   = 3;   // Suppress go ahead
const unsigned char gxsTELOPT_APRX   = 4;   // Approx message size negotiation
const unsigned char gxsTELOPT_STAT   = 5;   // Status
const unsigned char gxsTELOPT_TIM    = 6;   // Timing mark
const unsigned char gxsTELOPT_REM    = 7;   // Remote controlled trans/echo
const unsigned char gxsTELOPT_OLW    = 8;   // Output line width
const unsigned char gxsTELOPT_OPS    = 9;   // Output page size
const unsigned char gxsTELOPT_OCRD   = 10;  // Out carriage-return disposition
const unsigned char gxsTELOPT_OHT    = 11;  // Output horizontal tabstops
const unsigned char gxsTELOPT_OHTD   = 12;  // Out horizontal tab disposition
const unsigned char gxsTELOPT_OFD    = 13;  // Output formfeed disposition
const unsigned char gxsTELOPT_OVT    = 14;  // Output vertical tabstops
const unsigned char gxsTELOPT_OVTD   = 15;  // Output vertical tab disposition
const unsigned char gxsTELOPT_OLD    = 16;  // Output linefeed disposition
const unsigned char gxsTELOPT_EXT    = 17;  // Extended ascii character set
const unsigned char gxsTELOPT_LOGO   = 18;  // Logout
const unsigned char gxsTELOPT_BYTE   = 19;  // Byte macro
const unsigned char gxsTELOPT_DATA   = 20;  // Data entry terminal
const unsigned char gxsTELOPT_SUP    = 21;  // supdup protocol
const unsigned char gxsTELOPT_SUPO   = 22;  // supdup output
const unsigned char gxsTELOPT_SNDL   = 23;  // Send location
const unsigned char gxsTELOPT_TERM   = 24;  // Terminal type
const unsigned char gxsTELOPT_EOR    = 25;  // End of record
const unsigned char gxsTELOPT_TACACS = 26;  // Tacacs user identification
const unsigned char gxsTELOPT_OM     = 27;  // Output marking
const unsigned char gxsTELOPT_TLN    = 28;  // Terminal location number
const unsigned char gxsTELOPT_3270   = 29;  // Telnet 3270 regime
const unsigned char gxsTELOPT_X3     = 30;  // X.3 PAD
const unsigned char gxsTELOPT_NAWS   = 31;  // Negotiate about window size
const unsigned char gxsTELOPT_TS     = 32;  // Terminal speed
const unsigned char gxsTELOPT_RFC    = 33;  // Remote flow control
const unsigned char gxsTELOPT_LINE   = 34;  // Linemode
const unsigned char gxsTELOPT_XDL    = 35;  // X display location
const unsigned char gxsTELOPT_ENVIR  = 36;  // Telnet environment option
const unsigned char gxsTELOPT_AUTH   = 37;  // Telnet authentication option
const unsigned char gxsTELOPT_NENVIR = 39;  // Telnet environment option
const unsigned char gxsTELOPT_EXTOP  = 255; // Extended-options-list


Enumerations

enum gxsTelnetTermTypes { // Terminal types
  gxsTEL_NVT_TERM,    // NVT - Network Virtual Terminal
  gxsTEL_VT100F_TERM, // Filtered VT100/ANSI terminal emulation
                      // NOTE: This implementation will only 
                      // filter-out VT100 and ANSI escape 
                      // sequences. This terminal type should  
                      // only be used by embedded telnet clients 
                      // that do not support VT100/ANSI emulations 
                      // but must talk to servers that will not 
                      // support other emulations.
  gxsTEL_ENV_TERM     // Use term type set in environment space
};

enum gxsTelnetState { // Telnet transition state enumeration
  telnet_data,   // Telnet data byte
  telnet_code,   // Telnet code
  telnet_option  // Telnet option
};


Functions

gxsTelnetClient::gxsTelnetClient()
gxsTelnetClient::~gxsTelnetClient()
gxsTelnetClient::ConnectClient()
gxsTelnetClient::GetChar()
gxsTelnetClient::GetTermType()
gxsTelnetClient::OutputCharWaiting()
gxsTelnetClient::ReadTelnetData()
gxsTelnetClient::RecvString()
gxsTelnetClient::SendString()
gxsTelnetClient::SetTermType()
gxsTelnetClient::SetTimeOut()
gxsTelnetClient::WaitForReply()


gxsTelnetClient::gxsTelnetClient() - Default class constructor.

gxsTelnetClient::~gxsTelnetClient() - Class destructor.

gxSocketError gxsTelnetClient::ConnectClient(const char *host, int port = gxSOCKET_TELNET_PORT) - Public member function used to connect a telnet client to a server. Returns zero if no errors occur.

int gxsTelnetClient::GetChar(unsigned char &c) - Public member function used to extract a single character from the telnet input stream if a character is waiting to be read. Passes back the character in the "c" variable. Returns true if a character is waiting to be read or false if no characters are waiting.

char *gxsTelnetClient::GetTermType() - Public member function that returns the current terminal type name.

int gxsTelnetClient::OutputCharWaiting() - Public member function that returns true if any characters are waiting to be read.

gxSocketError gxsTelnetClient::ReadTelnetData(unsigned char data) - Public member function used to read a single character from the telnet input stream and interpret any telnet command sequences. Returns zero if no errors occur.

gxSocketError gxsTelnetClient::RecvString(char *buf, int bytes, const char *str) - Public blocking receive function used to read a string from a telnet server following a connect or send call. If the specified string is not received within the preset timeout period this function will return a non-zero value to indicate an error. Returns zero if no errors occur. The calling function must allocate a buffer large enough to hold the telnet reply. If the number of bytes received exceed the number of bytes allocated for the "buf" variable this function will return a buffer overflow.

gxSocketError gxsTelnetClient::SendString(const char *buf, int bytes) - Public blocking write function used to send a string to a telnet server. Returns zero if no errors occur.

void gxsTelnetClient::SetTermType(gxsTelnetTermTypes ttype = gxsTEL_NVT_TERM) - Public member function used to set the terminal type for this client session.

void gxsTelnetClient::SetTermType(const char *s) - Public member function used to set a custom terminal type for this client session.

void gxsTelnetClient::SetTimeOut(int seconds, int useconds) - Public member function used to set telnet timeout values for this client session.

int gxsTelnetClient::WaitForReply() - Public member function that returns false if a reply time is longer then the timeout values.


End Of Document