2007-11-25 17:50:16 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* netutils/telnetd/telnetd.c
|
2007-09-16 00:45:45 +02:00
|
|
|
*
|
2007-11-25 17:50:16 +01:00
|
|
|
* Copyright (C) 2007 Gregory Nutt. All rights reserved.
|
|
|
|
* Author: Gregory Nutt <spudmonkey@racsa.co.cr>
|
|
|
|
*
|
|
|
|
* This is a leverage of similar logic from uIP:
|
|
|
|
*
|
|
|
|
* Author: Adam Dunkels <adam@sics.se>
|
|
|
|
* Copyright (c) 2003, Adam Dunkels.
|
|
|
|
* All rights reserved.
|
2007-08-27 01:12:17 +02:00
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
2007-11-25 17:50:16 +01:00
|
|
|
*
|
2007-08-27 01:12:17 +02:00
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
2007-11-25 17:50:16 +01:00
|
|
|
* 3. Neither the name of the Institute nor the names of its contributors
|
|
|
|
* may be used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
2007-08-27 01:12:17 +02:00
|
|
|
*
|
2007-11-25 17:50:16 +01:00
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
2007-08-27 01:12:17 +02:00
|
|
|
|
|
|
|
#include <sys/types.h>
|
2007-11-25 17:50:16 +01:00
|
|
|
#include <sys/socket.h>
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <unistd.h>
|
2007-08-27 01:12:17 +02:00
|
|
|
#include <stdlib.h>
|
2007-11-25 17:50:16 +01:00
|
|
|
#include <stdarg.h>
|
2007-08-27 01:12:17 +02:00
|
|
|
#include <string.h>
|
2007-11-25 17:50:16 +01:00
|
|
|
#include <pthread.h>
|
|
|
|
#include <debug.h>
|
2007-08-27 01:12:17 +02:00
|
|
|
|
2007-11-25 17:50:16 +01:00
|
|
|
#include <net/uip/telnetd.h>
|
|
|
|
#include <net/uip/uip-lib.h>
|
2007-08-27 01:12:17 +02:00
|
|
|
|
|
|
|
#include "shell.h"
|
|
|
|
|
2007-11-25 17:50:16 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Definitions
|
|
|
|
****************************************************************************/
|
|
|
|
|
2007-08-27 01:12:17 +02:00
|
|
|
#define ISO_nl 0x0a
|
|
|
|
#define ISO_cr 0x0d
|
|
|
|
|
|
|
|
#define STATE_NORMAL 0
|
|
|
|
#define STATE_IAC 1
|
|
|
|
#define STATE_WILL 2
|
|
|
|
#define STATE_WONT 3
|
|
|
|
#define STATE_DO 4
|
|
|
|
#define STATE_DONT 5
|
|
|
|
#define STATE_CLOSE 6
|
|
|
|
|
|
|
|
#define TELNET_IAC 255
|
|
|
|
#define TELNET_WILL 251
|
|
|
|
#define TELNET_WONT 252
|
|
|
|
#define TELNET_DO 253
|
|
|
|
#define TELNET_DONT 254
|
|
|
|
|
2007-11-25 17:50:16 +01:00
|
|
|
/* Configurable settings */
|
2007-08-27 01:12:17 +02:00
|
|
|
|
2007-11-25 17:50:16 +01:00
|
|
|
#ifndef CONFIG_NETUTILS_IOBUFFER_SIZE
|
|
|
|
# define CONFIG_NETUTILS_IOBUFFER_SIZE 512
|
|
|
|
#endif
|
2007-08-27 01:12:17 +02:00
|
|
|
|
2007-11-25 17:50:16 +01:00
|
|
|
#ifndef CONFIG_NETUTILS_CMD_SIZE
|
|
|
|
# define CONFIG_NETUTILS_CMD_SIZE 40
|
|
|
|
#endif
|
2007-09-16 00:45:45 +02:00
|
|
|
|
2007-11-25 17:50:16 +01:00
|
|
|
/* As threads are created to handle each request, a stack must be allocated
|
|
|
|
* for the thread. Use a default if the user provided no stacksize.
|
|
|
|
*/
|
2007-09-16 00:45:45 +02:00
|
|
|
|
2007-11-25 17:50:16 +01:00
|
|
|
#ifndef CONFIG_NETUTILS_TELNETDSTACKSIZE
|
|
|
|
# define CONFIG_NETUTILS_TELNETDSTACKSIZE 4096
|
|
|
|
#endif
|
2007-08-27 01:12:17 +02:00
|
|
|
|
2007-11-25 17:50:16 +01:00
|
|
|
/* Enabled dumping of all input/output buffers */
|
2007-09-16 00:45:45 +02:00
|
|
|
|
2007-11-25 17:50:16 +01:00
|
|
|
#undef CONFIG_NETUTILS_TELNETD_DUMPBUFFER
|
2007-09-16 00:45:45 +02:00
|
|
|
|
2007-11-25 17:50:16 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Private Types
|
|
|
|
****************************************************************************/
|
2007-09-16 00:45:45 +02:00
|
|
|
|
2007-11-25 17:50:16 +01:00
|
|
|
struct telnetd_s
|
|
|
|
{
|
|
|
|
int tn_sockfd;
|
|
|
|
char tn_iobuffer[CONFIG_NETUTILS_IOBUFFER_SIZE];
|
|
|
|
char tn_cmd[CONFIG_NETUTILS_CMD_SIZE];
|
|
|
|
uint8 tn_bufndx;
|
|
|
|
uint8 tn_state;
|
|
|
|
};
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: telnetd_dumpbuffer
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Dump a buffer of data (debug only)
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
2007-09-16 00:45:45 +02:00
|
|
|
|
2007-11-25 17:50:16 +01:00
|
|
|
#ifdef CONFIG_NETUTILS_TELNETD_DUMPBUFFER
|
2009-06-25 19:44:35 +02:00
|
|
|
static inline void telnetd_dumpbuffer(FAR const char *msg, FAR const char *buffer, unsigned int nbytes)
|
2007-08-27 01:12:17 +02:00
|
|
|
{
|
2009-06-25 19:44:35 +02:00
|
|
|
/* CONFIG_DEBUG, CONFIG_DEBUG_VERBOSE, and CONFIG_DEBUG_NET have to be
|
|
|
|
* defined or the following does nothing.
|
|
|
|
*/
|
|
|
|
|
|
|
|
nvdbgdumpbuffer(msg, (FAR const ubyte*)buffer, nbytes);
|
2007-08-27 01:12:17 +02:00
|
|
|
}
|
2007-11-25 17:50:16 +01:00
|
|
|
#else
|
|
|
|
# define telnetd_dumpbuffer(msg,buffer,nbytes)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: telnetd_putchar
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Add another parsed character to the TELNET command string
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
2007-09-16 00:45:45 +02:00
|
|
|
|
2007-11-25 17:50:16 +01:00
|
|
|
static void telnetd_putchar(struct telnetd_s *pstate, uint8 ch)
|
2007-08-27 01:12:17 +02:00
|
|
|
{
|
2007-11-25 17:50:16 +01:00
|
|
|
/* Ignore carriage returns */
|
|
|
|
|
|
|
|
if (ch == ISO_cr)
|
2007-09-16 00:45:45 +02:00
|
|
|
{
|
2007-08-27 01:12:17 +02:00
|
|
|
return;
|
|
|
|
}
|
2007-09-16 00:45:45 +02:00
|
|
|
|
2007-11-25 17:50:16 +01:00
|
|
|
/* Add all other characters to the cmd buffer */
|
|
|
|
|
|
|
|
pstate->tn_cmd[pstate->tn_bufndx] = ch;
|
|
|
|
|
|
|
|
/* If a newline was added or if the buffer is full, then process it now */
|
|
|
|
|
|
|
|
if (ch == ISO_nl || pstate->tn_bufndx == (CONFIG_NETUTILS_CMD_SIZE - 1))
|
2007-09-16 00:45:45 +02:00
|
|
|
{
|
2007-11-25 17:50:16 +01:00
|
|
|
if (pstate->tn_bufndx > 0)
|
2007-09-16 00:45:45 +02:00
|
|
|
{
|
2007-11-25 17:50:16 +01:00
|
|
|
pstate->tn_cmd[pstate->tn_bufndx] = '\0';
|
2007-09-16 00:45:45 +02:00
|
|
|
}
|
2007-11-25 17:50:16 +01:00
|
|
|
|
|
|
|
telnetd_dumpbuffer("TELNET CMD", pstate->tn_cmd, strlen(pstate->tn_cmd));
|
|
|
|
shell_input(pstate, pstate->tn_cmd);
|
|
|
|
pstate->tn_bufndx = 0;
|
2007-09-16 00:45:45 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2007-11-25 17:50:16 +01:00
|
|
|
pstate->tn_bufndx++;
|
|
|
|
vdbg("Add '%c', bufndx=%d\n", ch, pstate->tn_bufndx);
|
2007-08-27 01:12:17 +02:00
|
|
|
}
|
|
|
|
}
|
2007-09-16 00:45:45 +02:00
|
|
|
|
2007-11-25 17:50:16 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: telnetd_sendopt
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static void telnetd_sendopt(struct telnetd_s *pstate, uint8 option, uint8 value)
|
2007-08-27 01:12:17 +02:00
|
|
|
{
|
2007-11-25 17:50:16 +01:00
|
|
|
uint8 optbuf[4];
|
|
|
|
optbuf[0] = TELNET_IAC;
|
|
|
|
optbuf[1] = option;
|
|
|
|
optbuf[2] = value;
|
|
|
|
optbuf[3] = 0;
|
|
|
|
|
|
|
|
telnetd_dumpbuffer("Send optbuf", optbuf, 4);
|
|
|
|
if (send(pstate->tn_sockfd, optbuf, 4, 0) < 0)
|
2007-09-16 00:45:45 +02:00
|
|
|
{
|
2007-11-25 17:50:16 +01:00
|
|
|
dbg("[%d] Failed to send TELNET_IAC\n", pstate->tn_sockfd);
|
2007-09-16 00:45:45 +02:00
|
|
|
}
|
2007-08-27 01:12:17 +02:00
|
|
|
}
|
2007-09-16 00:45:45 +02:00
|
|
|
|
2007-11-25 17:50:16 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: telnetd_receive
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Process a received TELENET buffer
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
2007-09-16 00:45:45 +02:00
|
|
|
|
2007-11-25 17:50:16 +01:00
|
|
|
static int telnetd_receive(struct telnetd_s *pstate, size_t len)
|
|
|
|
{
|
|
|
|
char *ptr = pstate->tn_iobuffer;
|
|
|
|
uint8 ch;
|
2007-09-16 00:45:45 +02:00
|
|
|
|
2007-11-25 17:50:16 +01:00
|
|
|
while (len > 0)
|
2007-09-16 00:45:45 +02:00
|
|
|
{
|
2007-11-25 17:50:16 +01:00
|
|
|
ch = *ptr++;
|
|
|
|
len--;
|
|
|
|
|
|
|
|
vdbg("ch=%02x state=%d\n", ch, pstate->tn_state);
|
|
|
|
switch (pstate->tn_state)
|
2007-09-16 00:45:45 +02:00
|
|
|
{
|
|
|
|
case STATE_IAC:
|
2007-11-25 17:50:16 +01:00
|
|
|
if (ch == TELNET_IAC)
|
2007-09-16 00:45:45 +02:00
|
|
|
{
|
2007-11-25 17:50:16 +01:00
|
|
|
telnetd_putchar(pstate, ch);
|
|
|
|
pstate->tn_state = STATE_NORMAL;
|
2007-09-16 00:45:45 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2007-11-25 17:50:16 +01:00
|
|
|
switch (ch)
|
2007-09-16 00:45:45 +02:00
|
|
|
{
|
|
|
|
case TELNET_WILL:
|
2007-11-25 17:50:16 +01:00
|
|
|
pstate->tn_state = STATE_WILL;
|
2007-09-16 00:45:45 +02:00
|
|
|
break;
|
2007-11-25 17:50:16 +01:00
|
|
|
|
2007-09-16 00:45:45 +02:00
|
|
|
case TELNET_WONT:
|
2007-11-25 17:50:16 +01:00
|
|
|
pstate->tn_state = STATE_WONT;
|
2007-09-16 00:45:45 +02:00
|
|
|
break;
|
2007-11-25 17:50:16 +01:00
|
|
|
|
2007-09-16 00:45:45 +02:00
|
|
|
case TELNET_DO:
|
2007-11-25 17:50:16 +01:00
|
|
|
pstate->tn_state = STATE_DO;
|
2007-09-16 00:45:45 +02:00
|
|
|
break;
|
2007-11-25 17:50:16 +01:00
|
|
|
|
2007-09-16 00:45:45 +02:00
|
|
|
case TELNET_DONT:
|
2007-11-25 17:50:16 +01:00
|
|
|
pstate->tn_state = STATE_DONT;
|
2007-09-16 00:45:45 +02:00
|
|
|
break;
|
2007-11-25 17:50:16 +01:00
|
|
|
|
2007-09-16 00:45:45 +02:00
|
|
|
default:
|
2007-11-25 17:50:16 +01:00
|
|
|
pstate->tn_state = STATE_NORMAL;
|
2007-09-16 00:45:45 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
2007-11-25 17:50:16 +01:00
|
|
|
|
2007-09-16 00:45:45 +02:00
|
|
|
case STATE_WILL:
|
|
|
|
/* Reply with a DONT */
|
2007-11-25 17:50:16 +01:00
|
|
|
|
|
|
|
telnetd_sendopt(pstate, TELNET_DONT, ch);
|
|
|
|
pstate->tn_state = STATE_NORMAL;
|
2007-09-16 00:45:45 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case STATE_WONT:
|
|
|
|
/* Reply with a DONT */
|
2007-11-25 17:50:16 +01:00
|
|
|
|
|
|
|
telnetd_sendopt(pstate, TELNET_DONT, ch);
|
|
|
|
pstate->tn_state = STATE_NORMAL;
|
2007-09-16 00:45:45 +02:00
|
|
|
break;
|
2007-11-25 17:50:16 +01:00
|
|
|
|
2007-09-16 00:45:45 +02:00
|
|
|
case STATE_DO:
|
|
|
|
/* Reply with a WONT */
|
2007-11-25 17:50:16 +01:00
|
|
|
|
|
|
|
telnetd_sendopt(pstate, TELNET_WONT, ch);
|
|
|
|
pstate->tn_state = STATE_NORMAL;
|
2007-09-16 00:45:45 +02:00
|
|
|
break;
|
2007-11-25 17:50:16 +01:00
|
|
|
|
2007-09-16 00:45:45 +02:00
|
|
|
case STATE_DONT:
|
|
|
|
/* Reply with a WONT */
|
2007-11-25 17:50:16 +01:00
|
|
|
|
|
|
|
telnetd_sendopt(pstate, TELNET_WONT, ch);
|
|
|
|
pstate->tn_state = STATE_NORMAL;
|
2007-09-16 00:45:45 +02:00
|
|
|
break;
|
2007-11-25 17:50:16 +01:00
|
|
|
|
2007-09-16 00:45:45 +02:00
|
|
|
case STATE_NORMAL:
|
2007-11-25 17:50:16 +01:00
|
|
|
if (ch == TELNET_IAC)
|
2007-09-16 00:45:45 +02:00
|
|
|
{
|
2007-11-25 17:50:16 +01:00
|
|
|
pstate->tn_state = STATE_IAC;
|
2007-09-16 00:45:45 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2007-11-25 17:50:16 +01:00
|
|
|
telnetd_putchar(pstate, ch);
|
2007-09-16 00:45:45 +02:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2007-08-27 01:12:17 +02:00
|
|
|
}
|
2007-11-25 17:50:16 +01:00
|
|
|
return OK;
|
2007-08-27 01:12:17 +02:00
|
|
|
}
|
|
|
|
|
2007-11-25 17:50:16 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: telnetd_handler
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Each time a new connection to port 23 is made, a new thread is created
|
|
|
|
* that begins at this entry point. There should be exactly one argument
|
|
|
|
* and it should be the socket descriptor (+1).
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
2007-08-27 01:12:17 +02:00
|
|
|
|
2007-11-25 17:50:16 +01:00
|
|
|
static void *telnetd_handler(void *arg)
|
2007-08-27 01:12:17 +02:00
|
|
|
{
|
2007-11-25 17:50:16 +01:00
|
|
|
struct telnetd_s *pstate = (struct telnetd_s *)malloc(sizeof(struct telnetd_s));
|
|
|
|
int sockfd = (int)arg;
|
|
|
|
int ret = ERROR;
|
2007-11-08 17:01:18 +01:00
|
|
|
|
2007-11-25 17:50:16 +01:00
|
|
|
dbg("[%d] Started\n", sockfd);
|
|
|
|
|
|
|
|
/* Verify that the state structure was successfully allocated */
|
|
|
|
|
|
|
|
if (pstate)
|
2007-09-16 00:45:45 +02:00
|
|
|
{
|
2007-11-25 17:50:16 +01:00
|
|
|
/* Initialize the thread state structure */
|
2007-09-16 00:45:45 +02:00
|
|
|
|
2007-11-25 17:50:16 +01:00
|
|
|
memset(pstate, 0, sizeof(struct telnetd_s));
|
|
|
|
pstate->tn_sockfd = sockfd;
|
|
|
|
pstate->tn_state = STATE_NORMAL;
|
2007-09-16 00:45:45 +02:00
|
|
|
|
2007-11-25 17:50:16 +01:00
|
|
|
/* Start up the shell */
|
2007-09-16 00:45:45 +02:00
|
|
|
|
2007-11-25 17:50:16 +01:00
|
|
|
shell_init(pstate);
|
|
|
|
shell_start(pstate);
|
2007-09-16 00:45:45 +02:00
|
|
|
|
2007-11-25 17:50:16 +01:00
|
|
|
/* Loop processing each TELNET command */
|
|
|
|
do
|
|
|
|
{
|
|
|
|
/* Read a buffer of data from the TELNET client */
|
|
|
|
|
|
|
|
ret = recv(pstate->tn_sockfd, pstate->tn_iobuffer, CONFIG_NETUTILS_IOBUFFER_SIZE, 0);
|
|
|
|
if (ret > 0)
|
|
|
|
{
|
|
|
|
|
|
|
|
/* Process the received TELNET data */
|
|
|
|
|
|
|
|
telnetd_dumpbuffer("Received buffer", pstate->tn_iobuffer, ret);
|
|
|
|
ret = telnetd_receive(pstate, ret);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
while (ret >= 0 && pstate->tn_state != STATE_CLOSE);
|
|
|
|
dbg("[%d] ret=%d tn_state=%d\n", sockfd, ret, pstate->tn_state);
|
2007-11-15 23:38:32 +01:00
|
|
|
|
2007-11-25 17:50:16 +01:00
|
|
|
/* End of command processing -- Clean up and exit */
|
|
|
|
|
|
|
|
free(pstate);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Exit the task */
|
|
|
|
|
|
|
|
dbg("[%d] Exitting\n", sockfd);
|
|
|
|
close(sockfd);
|
2008-12-12 19:01:25 +01:00
|
|
|
return NULL;
|
2007-08-27 01:12:17 +02:00
|
|
|
}
|
2007-11-25 17:50:16 +01:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: telnetd_init
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* This is the main processing thread for telnetd. It never returns
|
|
|
|
* unless an error occurs
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
void telnetd_init(void)
|
|
|
|
{
|
|
|
|
/* Execute telnetd_handler on each connection to port 23 */
|
|
|
|
|
|
|
|
uip_server(HTONS(23), telnetd_handler, CONFIG_NETUTILS_TELNETDSTACKSIZE);
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: shell_prompt
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Print a prompt to the shell window.
|
|
|
|
*
|
|
|
|
* This function can be used by the shell back-end to print out a prompt
|
|
|
|
* to the shell window.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
void shell_prompt(void *handle, char *str)
|
|
|
|
{
|
|
|
|
struct telnetd_s *pstate = (struct telnetd_s *)handle;
|
|
|
|
int len = strlen(str);
|
|
|
|
|
|
|
|
strncpy(pstate->tn_iobuffer, str, len);
|
|
|
|
telnetd_dumpbuffer("Shell prompt", pstate->tn_iobuffer, len);
|
|
|
|
if (send(pstate->tn_sockfd, pstate->tn_iobuffer, len, 0) < 0)
|
|
|
|
{
|
|
|
|
dbg("[%d] Failed to send prompt\n", pstate->tn_sockfd);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: shell_output
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Print a string to the shell window.
|
|
|
|
*
|
|
|
|
* This function is implemented by the shell GUI / telnet server and
|
|
|
|
* can be called by the shell back-end to output a string in the
|
|
|
|
* shell window. The string is automatically appended with a linebreak.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
void shell_output(void *handle, const char *fmt, ...)
|
|
|
|
{
|
|
|
|
struct telnetd_s *pstate = (struct telnetd_s *)handle;
|
|
|
|
unsigned len;
|
|
|
|
va_list ap;
|
|
|
|
|
|
|
|
va_start(ap, fmt);
|
|
|
|
vsnprintf(pstate->tn_iobuffer, CONFIG_NETUTILS_IOBUFFER_SIZE, fmt, ap);
|
|
|
|
va_end(ap);
|
|
|
|
|
|
|
|
len = strlen(pstate->tn_iobuffer);
|
|
|
|
if (len < CONFIG_NETUTILS_IOBUFFER_SIZE - 2)
|
|
|
|
{
|
|
|
|
pstate->tn_iobuffer[len] = ISO_cr;
|
|
|
|
pstate->tn_iobuffer[len+1] = ISO_nl;
|
|
|
|
pstate->tn_iobuffer[len+2] = '\0';
|
|
|
|
}
|
|
|
|
|
|
|
|
telnetd_dumpbuffer("Shell output", pstate->tn_iobuffer, len+2);
|
|
|
|
if (send(pstate->tn_sockfd, pstate->tn_iobuffer, len+2, 0) < 0)
|
|
|
|
{
|
2007-12-02 19:18:59 +01:00
|
|
|
dbg("[%d] Failed to send response\n", pstate->tn_sockfd);
|
2007-11-25 17:50:16 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: shell_quit
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Quit the shell
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
void shell_quit(void *handle, char *str)
|
|
|
|
{
|
|
|
|
struct telnetd_s *pstate = (struct telnetd_s *)handle;
|
|
|
|
pstate->tn_state = STATE_CLOSE;
|
|
|
|
}
|
|
|
|
|