MityDSP Documentation Index

Using the MityDSP Telnet Server Daemon

The MityDSP telnet server deamon allows you to easily add a telnet interface to your MityDSP based aplication. A telnet interface can be used for MityDSP device configuration and/or as a diagnostic aid.

The MityDSP telnet server class (tcTelnetd) provides a low level foundation for your application's command shell. The tcTelnetd class handles all of the low TCP socket interfaces and optionally can provide help in user authentication when a telnet client connects to your MityDSP device.

The tcTelnetd server class supports RFC 854 (support for TELNET external commands) and RFC 857 (support for remote echo control). Hooks are provided for handling commands, options set/request commands, and subnegotiation extraction according to RFC 854.

Once started, the tcTelnetd object will notify your application through callback functions when a new connection is established, when a user name needs verification, when a password needs verification, when data is received and when the client closes the telnet connection. Functions are also provided for your application to send text data to connected telnet clients.

Example

The following simple example will start a telnet server which will support up to four simultaneous independant connections. Since writing a command shell is beyond the scope of this document (see the telnet server demo program) this simple example will simply echo characters back to the telnet client. When the telnet client sends a 'Z' the example will close the connection and when the telnet client sends a 'U' it will reply with a jovial message.

 #include "net/telnetd/telnetd.h"

 tcTelnetd *telnetd;
 
 void start_telnet_server ()
 {
     // Create the telnet server daemon
     telnetd = new tcTelnetd (4);
 
     // Start the telnet server daemon
     tsTelnetdParams tp;
 
     memset (&tp, 0, sizeof (tp));        
     tp.connect_func  = telnet_connect;
     tp.chk_user_func = telnet_chk_user;
     tp.chk_pswd_func = telnet_chk_pswd;
     tp.close_func    = telnet_close;
     tp.in_char_func  = telnet_in_char;
     tp.user1         = NULL;
     
     telnetd->run (&tp);   // Will not return...
 }
 
 int telnet_connect (tsTelnetConn* conn)
 {
     // If we were maintaing some application specific information
     // in an application provided structure for each telnet connection 
     // we would grab that structure from a pool here. Then initialize 
     // the structure and set the user2 parameter of this tsTelnetConn
     // to point to it. That way we can easily reference the data in any 
     // of the callbacks since they all have a pointer to the same 
     // tsTelnetConn connection structure we have a pointer to right now
     // and they're unique for each telnet connection.

     // This could also be a good place for some security if you wanted 
     // it in the form of allowing only certain telnet client machines 
     // to have access, you could take a look at the client's TCP/IP
     // address in the tsTelnetConn structure, then reject the connection
     // by returning -1 instead of 0 if you don't like the client address.

     // Put up our own welcome message, NOTE: This is optional.       
     // If we specified a user name or a password check function
     // then the deamon will take care of prompting for those
     // entries. We just need to verify them when the checks come in.

     telnetd->send_str (conn, "Please log in...\r\n\n");

     return 0;
 }

 int telnet_chk_user (tsTelnetConn* conn, const char *user)
 {
     // Performing a user name check is optional. If you supply
     // a NULL in the tsTelnetdParams structure, no user check 
     // will be performed.

     // Allow only users "admin" & "Joe".

     int retv = -1;
     if (strcmp (user, "admin") == 0 || strcmp (user, "Joe") == 0)
     {
         retv = 0;
     }
     return retv;
 }
 
 int telnet_chk_pswd (tsTelnetConn* conn, const char *pswd)
 {
     // Performing a password check is optional. If you supply
     // a NULL in the tsTelnetdParams structure, no password check 
     // will be performed.

     // We can find the username from this connection by looking
     // in the user field of the tsTelnetConn structure.

     // User admin's password will be "password" & user Joe's 
     // password will be "Cool".

     int retv = -1;
     if ((strcmp (conn->user, "admin") == 0 && strcmp (pswd, "password") == 0) ||
         (strcmp (conn->user, "Joe")   == 0 && strcmp (pswd, "Cool")     == 0))
     {
         retv = 0;
     }
     return retv;
 }
 
 void telnet_close (tsTelnetConn* conn)
 {
     // Nothing to do for this simple example
     // If we were maintaing some application specific information
     // in an application provided structure for each telnet connection 
     // we would free that structure back into the available pool here.
 }

 int telnet_in_char (tsTelnetConn *conn, char c)
 {
     // Echo everything except 'Z' in which case we close the connection
     // and 'U' in which case we send a silly message.
     int retv = 0;
     switch (c)
     {
     case 'Z':
         telnetd->send_str (conn, "\r\nBye ...\r\n");
         telnetd->close_conn (conn);
         retv = -1;
         break;
     case 'U':
         telnetd->send_str (conn, "\r\nI see U ...\r\n");
         break;
     default:
         if (conn->echo)
         {
             telnetd->send_char (conn, c);
         }
         break;
     }
     return 0;
 }

  
Generated on Fri Sep 23 16:34:01 2011 for MityDSP Net by  Doxygen Version 1.6.1
Copyright © 2009, Critical Link LLC, All rights reserved.