Telnet Server - telnetd
#include <posh_init.h>
#include < sys.h >
The telnet server provides a network telnet protocol and utilizes the vtty server technology and the posh shell server for user interaction via telnet. Typically, when initialized, it allows users to query files, examine the file structure, create new files and look at the content of files, all over the network. It is a minimal implementation for memory reasons. It does not support terminal formating of any kind and only supports the absolute minimal set of commands.
The telnet server inputs characters and outputs results of command requests via the network. The first character data negotiates the telnet protocol. The first command request is a login request. Unlike many implementations, the login support is contained within posh - it does not have a separate thread for this purpose. The .login file is specified at posh creation time in the initialization structure.
The posh_init initialization structure also specifies the mount points for two file systems. These mount points are important to avoid attempts to list non-existent files. Setup these mount points at creation time for proper operation. The final elements of the initalization structure are the input and output streams. These two are specified as files. The structure is shown below.
Posh initialization structure.
struct set_posh
{
char *stdinstring; // set the input stream
char *stdoutstring; // set output stream
char *mountpoint; // set first file mount point
char *mountpoint2; // set second file mount point
char *loginfile;
};
This partial example shows the creation and registration of the telnetd server and its associated vtty file I/O. After creation, posh is created which interacts via the vtty file I/O ports for input and output.
int start_posh(void);
THREAD telnet_shell()
{
telnetd(start_posh);
}
int start_posh(void)
{
struct set_posh *set_posh_tty;
pthread_t poshTid;
pthread_attr_t poshattr;
struct sched_param poshPriority;
if( (set_posh_tty = malloc(sizeof(struct set_posh)))==NULL)
{
xprintf("posh create - no memory\n");
return 0;
}
set_posh_tty->stdinstring = "/dev/vtty";
set_posh_tty->stdoutstring = "/dev/vtty";
set_posh_tty->mountpoint = "/dev/rd/d1";
set_posh_tty->mountpoint2 = NULL;
set_posh_tty->loginfile = "/dev/rd/d1/.login";
pthread_attr_init(&poshattr);
pthread_attr_setstacksize(&poshattr, 1400);
poshPriority.sched_priority = 5;
pthread_attr_setdetachstate(&poshattr, PTHREAD_CREATE_DETACHED);
pthread_attr_setschedparam(&poshattr, &poshPriority);
pthread_create(&poshTid, &poshattr, (void *)(&posh), set_posh_tty);
pthread_attr_destroy(&poshattr);
return 1;
}
/*
* Start telnetd example
*/
...
pthread_attr_init(&attr);
myNewPriority.sched_priority = 6;
pthread_attr_setschedparam(&attr, &myNewPriority);
pthread_attr_setstacksize(&attr, 1024);
if(pthread_create(πd, &attr, &telnet_shell, 0)!=0)
{
xprintf("pthread_create = %d\n", errno);
pthread_exit(0);
}
pthread_attr_destroy(&attr);
if(dir_register("/dev/vtty", pid, TYPE_SERVER)==0)
{
xprintf("dir_register = %d\n", errno);
pthread_exit(0);
}
xprintf("\ntelnetd started!\n");
...
help - help listing
alias [alias_name="value"] - setup an alias for an expression
cat - concatinate output
cd <dir> - change directory
chmod <mode> <dir | file> - change a file's mode
cp <src> <dst> - copy a file
exit - exit the posh shell
logout - logout of posh
ls [option] - list directory contents
-l for long list
-d lists directories, not content
mkdir <dir> - make a directory
mv <src> <dst> - move a file
printenv [env_name] - print all or part of environment - printenv [VARIABLE]...
or printenv OPTION
pwd - present working directory
rm <file> - remove a file
rmdir - remove a directory
setenv [env_name="value"] - set an environment variable
unalias <alias_name> - delete an alias
useradd <username> <password> - add a user with a password
userdel <username> - delete a user
userpasswd <username> <oldpassword> <newpassword> - set (or reset) a user password
There is a demo available for the Unison and DSPnano telnetd server which is found in installdir/demos.