2015-12-07 16:26:57 +01:00
|
|
|
/****************************************************************************
|
2015-12-07 20:48:06 +01:00
|
|
|
* drivers/net/telnet.c
|
2015-12-07 16:26:57 +01:00
|
|
|
*
|
2020-02-07 14:16:21 +01:00
|
|
|
* Copyright (C) 2007, 2009, 2011-2013, 2017, 2019, 2020 Gregory Nutt. All
|
2019-01-05 19:14:05 +01:00
|
|
|
* rights reserved.
|
2015-12-07 16:26:57 +01:00
|
|
|
* Author: Gregory Nutt <gnutt@nuttx.org>
|
|
|
|
*
|
2020-02-23 09:50:23 +01:00
|
|
|
* This derives remotely from some Telnet logic from uIP which has a
|
2019-05-21 17:57:46 +02:00
|
|
|
* compatible BSD license:
|
2015-12-07 16:26:57 +01:00
|
|
|
*
|
|
|
|
* Author: Adam Dunkels <adam@sics.se>
|
|
|
|
* Copyright (c) 2003, Adam Dunkels.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
*
|
|
|
|
* 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.
|
2019-01-05 19:14:05 +01:00
|
|
|
* 3. Neither the name of the Institute, NuttX nor the names of its
|
|
|
|
* contributors may be used to endorse or promote products derived
|
|
|
|
* from this software without specific prior written permission.
|
2015-12-07 16:26:57 +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>
|
|
|
|
|
2020-02-05 10:52:26 +01:00
|
|
|
#include <assert.h>
|
2015-12-07 16:26:57 +01:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <poll.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <debug.h>
|
|
|
|
|
2020-02-05 10:52:26 +01:00
|
|
|
#include <nuttx/kmalloc.h>
|
2019-05-21 17:57:46 +02:00
|
|
|
#include <nuttx/kthread.h>
|
2020-02-05 10:52:26 +01:00
|
|
|
#include <nuttx/signal.h>
|
2019-05-21 17:57:46 +02:00
|
|
|
#include <nuttx/semaphore.h>
|
2015-12-07 16:26:57 +01:00
|
|
|
#include <nuttx/fs/fs.h>
|
|
|
|
#include <nuttx/net/net.h>
|
|
|
|
#include <nuttx/net/telnet.h>
|
|
|
|
|
2015-12-07 20:48:06 +01:00
|
|
|
#ifdef CONFIG_NETDEV_TELNET
|
|
|
|
|
2015-12-07 16:26:57 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Pre-processor Definitions
|
|
|
|
****************************************************************************/
|
2019-01-05 19:14:05 +01:00
|
|
|
|
2015-12-07 16:26:57 +01:00
|
|
|
/* Configuration ************************************************************/
|
|
|
|
|
|
|
|
#ifndef CONFIG_TELNET_RXBUFFER_SIZE
|
|
|
|
# define CONFIG_TELNET_RXBUFFER_SIZE 256
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef CONFIG_TELNET_TXBUFFER_SIZE
|
|
|
|
# define CONFIG_TELNET_TXBUFFER_SIZE 256
|
|
|
|
#endif
|
|
|
|
|
2019-05-21 17:57:46 +02:00
|
|
|
#ifndef CONFIG_TELNET_MAXLCLIENTS
|
|
|
|
# define CONFIG_TELNET_MAXLCLIENTS 8
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef CONFIG_TELNET_IOTHREAD_PRIORITY
|
|
|
|
# define CONFIG_TELNET_IOTHREAD_PRIORITY 100
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef CONFIG_TELNET_IOTHREAD_STACKSIZE
|
|
|
|
# define CONFIG_TELNET_IOTHREAD_STACKSIZE 1024
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#undef HAVE_SIGNALS
|
|
|
|
#if defined(CONFIG_TTY_SIGINT) || defined(CONFIG_TTY_SIGSTP)
|
|
|
|
# define HAVE_SIGNALS
|
|
|
|
#endif
|
|
|
|
|
2015-12-07 16:26:57 +01:00
|
|
|
/* Telnet protocol stuff ****************************************************/
|
|
|
|
|
2020-02-05 10:52:26 +01:00
|
|
|
#define TELNET_NL 0x0a
|
|
|
|
#define TELNET_CR 0x0d
|
2019-01-05 19:14:05 +01:00
|
|
|
|
|
|
|
/* Telnet commands */
|
|
|
|
|
|
|
|
#define TELNET_ECHO 1
|
|
|
|
#define TELNET_SGA 3 /* Suppress Go Ahead */
|
|
|
|
#define TELNET_NAWS 31 /* Negotiate about window size */
|
|
|
|
|
|
|
|
/* Telnet control */
|
2015-12-07 16:26:57 +01:00
|
|
|
|
2019-01-05 19:14:05 +01:00
|
|
|
#define TELNET_IAC 255
|
|
|
|
#define TELNET_WILL 251
|
|
|
|
#define TELNET_WONT 252
|
|
|
|
#define TELNET_DO 253
|
|
|
|
#define TELNET_DONT 254
|
|
|
|
#define TELNET_SB 250
|
|
|
|
#define TELNET_SE 240
|
2018-01-09 12:16:02 +01:00
|
|
|
|
2015-12-07 16:26:57 +01:00
|
|
|
/* Device stuff *************************************************************/
|
|
|
|
|
2020-02-05 10:52:26 +01:00
|
|
|
#define TELNET_DEVFMT "/dev/telnet%d"
|
2015-12-07 16:26:57 +01:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Types
|
|
|
|
****************************************************************************/
|
2019-01-05 19:14:05 +01:00
|
|
|
|
2015-12-07 16:26:57 +01:00
|
|
|
/* The state of the telnet parser */
|
|
|
|
|
|
|
|
enum telnet_state_e
|
|
|
|
{
|
|
|
|
STATE_NORMAL = 0,
|
|
|
|
STATE_IAC,
|
|
|
|
STATE_WILL,
|
|
|
|
STATE_WONT,
|
|
|
|
STATE_DO,
|
2019-01-05 19:14:05 +01:00
|
|
|
STATE_DONT,
|
|
|
|
STATE_SB,
|
|
|
|
STATE_SB_NAWS,
|
|
|
|
STATE_SE
|
2015-12-07 16:26:57 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
/* This structure describes the internal state of the driver */
|
|
|
|
|
|
|
|
struct telnet_dev_s
|
|
|
|
{
|
2019-01-05 19:14:05 +01:00
|
|
|
sem_t td_exclsem; /* Enforces mutually exclusive access */
|
2019-05-22 15:46:40 +02:00
|
|
|
sem_t td_iosem; /* I/O thread will notify that data is available */
|
2019-01-05 19:14:05 +01:00
|
|
|
uint8_t td_state; /* (See telnet_state_e) */
|
|
|
|
uint8_t td_offset; /* Offset to the valid, pending bytes in the rxbuffer */
|
|
|
|
uint8_t td_crefs; /* The number of open references to the session */
|
2020-02-05 10:57:58 +01:00
|
|
|
uint8_t td_minor; /* Minor device number */
|
2020-04-12 17:52:56 +02:00
|
|
|
uint16_t td_pending; /* Number of valid, pending bytes in the rxbuffer */
|
2019-01-05 19:14:05 +01:00
|
|
|
#ifdef CONFIG_TELNET_SUPPORT_NAWS
|
|
|
|
uint16_t td_rows; /* Number of NAWS rows */
|
|
|
|
uint16_t td_cols; /* Number of NAWS cols */
|
|
|
|
int td_sb_count; /* Count of TELNET_SB bytes received */
|
|
|
|
#endif
|
2019-05-21 17:57:46 +02:00
|
|
|
#ifdef HAVE_SIGNALS
|
2020-02-05 10:52:26 +01:00
|
|
|
pid_t td_pid;
|
2019-05-21 17:57:46 +02:00
|
|
|
#endif
|
2020-02-05 10:52:26 +01:00
|
|
|
struct pollfd td_fds;
|
2019-01-05 19:14:05 +01:00
|
|
|
FAR struct socket td_psock; /* A clone of the internal socket structure */
|
2015-12-07 16:26:57 +01:00
|
|
|
char td_rxbuffer[CONFIG_TELNET_RXBUFFER_SIZE];
|
|
|
|
char td_txbuffer[CONFIG_TELNET_TXBUFFER_SIZE];
|
|
|
|
};
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Function Prototypes
|
|
|
|
****************************************************************************/
|
2019-01-05 19:14:05 +01:00
|
|
|
|
2015-12-07 16:26:57 +01:00
|
|
|
/* Support functions */
|
|
|
|
|
|
|
|
#ifdef CONFIG_TELNET_DUMPBUFFER
|
|
|
|
static inline void telnet_dumpbuffer(FAR const char *msg,
|
|
|
|
FAR const char *buffer, unsigned int nbytes);
|
|
|
|
#else
|
|
|
|
# define telnet_dumpbuffer(msg,buffer,nbytes)
|
|
|
|
#endif
|
|
|
|
static void telnet_getchar(FAR struct telnet_dev_s *priv, uint8_t ch,
|
|
|
|
FAR char *dest, int *nread);
|
|
|
|
static ssize_t telnet_receive(FAR struct telnet_dev_s *priv,
|
|
|
|
FAR const char *src, size_t srclen, FAR char *dest,
|
|
|
|
size_t destlen);
|
|
|
|
static bool telnet_putchar(FAR struct telnet_dev_s *priv, uint8_t ch,
|
|
|
|
int *nwritten);
|
|
|
|
static void telnet_sendopt(FAR struct telnet_dev_s *priv, uint8_t option,
|
|
|
|
uint8_t value);
|
2020-02-05 10:52:26 +01:00
|
|
|
static int telnet_io_main(int argc, FAR char** argv);
|
2015-12-07 16:26:57 +01:00
|
|
|
|
2015-12-07 20:48:06 +01:00
|
|
|
/* Telnet character driver methods */
|
2015-12-07 16:26:57 +01:00
|
|
|
|
|
|
|
static int telnet_open(FAR struct file *filep);
|
|
|
|
static int telnet_close(FAR struct file *filep);
|
2019-01-05 19:14:05 +01:00
|
|
|
static ssize_t telnet_read(FAR struct file *filep, FAR char *buffer,
|
|
|
|
size_t len);
|
|
|
|
static ssize_t telnet_write(FAR struct file *filep, FAR const char *buffer,
|
|
|
|
size_t len);
|
2020-02-05 09:12:53 +01:00
|
|
|
static int telnet_ioctl(FAR struct file *filep, int cmd,
|
|
|
|
unsigned long arg);
|
2019-01-05 19:14:05 +01:00
|
|
|
static int telnet_poll(FAR struct file *filep, FAR struct pollfd *fds,
|
|
|
|
bool setup);
|
2015-12-07 20:48:06 +01:00
|
|
|
|
|
|
|
/* Telnet session creation */
|
|
|
|
|
|
|
|
static int telnet_session(FAR struct telnet_session_s *session);
|
|
|
|
|
|
|
|
/* Telnet factory driver methods */
|
|
|
|
|
|
|
|
static ssize_t factory_read(FAR struct file *filep, FAR char *buffer,
|
|
|
|
size_t buflen);
|
|
|
|
static ssize_t factory_write(FAR struct file *filep, FAR const char *buffer,
|
|
|
|
size_t buflen);
|
2020-02-05 09:12:53 +01:00
|
|
|
static int factory_ioctl(FAR struct file *filep, int cmd,
|
2015-12-07 16:26:57 +01:00
|
|
|
unsigned long arg);
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Data
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static const struct file_operations g_telnet_fops =
|
|
|
|
{
|
2015-12-07 20:48:06 +01:00
|
|
|
telnet_open, /* open */
|
|
|
|
telnet_close, /* close */
|
|
|
|
telnet_read, /* read */
|
|
|
|
telnet_write, /* write */
|
2019-01-13 21:04:20 +01:00
|
|
|
NULL, /* seek */
|
2020-02-05 09:12:53 +01:00
|
|
|
telnet_ioctl, /* ioctl */
|
2019-05-22 02:57:54 +02:00
|
|
|
telnet_poll /* poll */
|
2019-01-13 21:04:20 +01:00
|
|
|
#ifndef CONFIG_DISABLE_PSEUDOFS_OPERATIONS
|
|
|
|
, NULL /* unlink */
|
|
|
|
#endif
|
2015-12-07 20:48:06 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
static const struct file_operations g_factory_fops =
|
|
|
|
{
|
2019-01-13 21:04:20 +01:00
|
|
|
NULL, /* open */
|
|
|
|
NULL, /* close */
|
2015-12-07 20:48:06 +01:00
|
|
|
factory_read, /* read */
|
|
|
|
factory_write, /* write */
|
2019-01-13 21:04:20 +01:00
|
|
|
NULL, /* seek */
|
2020-02-05 09:12:53 +01:00
|
|
|
factory_ioctl, /* ioctl */
|
|
|
|
NULL /* poll */
|
2019-01-13 21:04:20 +01:00
|
|
|
#ifndef CONFIG_DISABLE_PSEUDOFS_OPERATIONS
|
|
|
|
, NULL /* unlink */
|
|
|
|
#endif
|
2015-12-07 16:26:57 +01:00
|
|
|
};
|
|
|
|
|
2019-05-21 17:57:46 +02:00
|
|
|
/* This is an global data set of all of all active Telnet drivers. This
|
|
|
|
* additional logic in included to handle killing of task via control
|
|
|
|
* characters received via Telenet (via Ctrl-C SIGINT, in particular).
|
|
|
|
*/
|
|
|
|
|
|
|
|
static pid_t g_telnet_io_kthread;
|
|
|
|
static struct telnet_dev_s *g_telnet_clients[CONFIG_TELNET_MAXLCLIENTS];
|
|
|
|
static sem_t g_iosem = SEM_INITIALIZER(0);
|
|
|
|
static sem_t g_clients_sem = SEM_INITIALIZER(1);
|
|
|
|
|
2015-12-07 16:26:57 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Private Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: telnet_dumpbuffer
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Dump a buffer of data (debug only)
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#ifdef CONFIG_TELNET_DUMPBUFFER
|
|
|
|
static inline void telnet_dumpbuffer(FAR const char *msg,
|
|
|
|
FAR const char *buffer,
|
|
|
|
unsigned int nbytes)
|
|
|
|
{
|
2020-02-07 14:16:21 +01:00
|
|
|
/* CONFIG_DEBUG_FEATURES, CONFIG_DEBUG_INFO, and CONFIG_DEBUG_NET have to
|
|
|
|
* be defined or the following does nothing.
|
2019-01-05 19:14:05 +01:00
|
|
|
*/
|
2015-12-07 16:26:57 +01:00
|
|
|
|
2019-01-05 19:14:05 +01:00
|
|
|
ninfodumpbuffer(msg, (FAR const uint8_t *)buffer, nbytes);
|
2015-12-07 16:26:57 +01:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2019-05-21 17:57:46 +02:00
|
|
|
/****************************************************************************
|
2020-02-05 10:14:30 +01:00
|
|
|
* Name: telnet_check_ctrlchar
|
2019-05-21 17:57:46 +02:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Check if an incoming control character should generate a signal.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#ifdef HAVE_SIGNALS
|
2020-02-05 10:14:30 +01:00
|
|
|
static void telnet_check_ctrlchar(FAR struct telnet_dev_s *priv,
|
|
|
|
FAR char *buffer, size_t len)
|
2019-05-21 17:57:46 +02:00
|
|
|
{
|
|
|
|
int signo = 0;
|
|
|
|
|
2020-02-05 10:52:26 +01:00
|
|
|
for (; priv->td_pid >= 0 && len > 0; buffer++, len--)
|
2020-02-05 10:14:30 +01:00
|
|
|
{
|
2019-05-21 17:57:46 +02:00
|
|
|
#ifdef CONFIG_TTY_SIGINT
|
2020-04-12 18:09:04 +02:00
|
|
|
/* Is this the special character that will generate the SIGINT
|
|
|
|
* signal?
|
|
|
|
*/
|
2019-05-21 17:57:46 +02:00
|
|
|
|
2020-02-05 10:14:30 +01:00
|
|
|
if (*buffer == CONFIG_TTY_SIGINT_CHAR)
|
|
|
|
{
|
|
|
|
/* Yes.. note that the kill is needed and do not put the character
|
|
|
|
* into the Rx buffer. It should not be read as normal data.
|
|
|
|
*/
|
2019-05-21 17:57:46 +02:00
|
|
|
|
2020-02-05 10:14:30 +01:00
|
|
|
signo = SIGINT;
|
|
|
|
break;
|
|
|
|
}
|
2019-05-21 17:57:46 +02:00
|
|
|
#endif
|
2020-02-10 15:49:00 +01:00
|
|
|
|
2019-05-21 17:57:46 +02:00
|
|
|
#ifdef CONFIG_TTY_SIGSTP
|
2020-04-12 18:09:04 +02:00
|
|
|
/* Is this the special character that will generate the SIGSTP
|
|
|
|
* signal?
|
|
|
|
*/
|
2019-05-21 17:57:46 +02:00
|
|
|
|
2020-02-05 10:14:30 +01:00
|
|
|
if (*buffer == CONFIG_TTY_SIGSTP_CHAR)
|
2019-05-21 17:57:46 +02:00
|
|
|
{
|
|
|
|
/* Note that the kill is needed and do not put the character
|
|
|
|
* into the Rx buffer. It should not be read as normal data.
|
|
|
|
*/
|
|
|
|
|
|
|
|
signo = SIGSTP;
|
2020-02-10 16:05:17 +01:00
|
|
|
#ifndef CONFIG_TTY_SIGINT
|
|
|
|
break;
|
|
|
|
#endif
|
2019-05-21 17:57:46 +02:00
|
|
|
}
|
|
|
|
#endif
|
2020-02-05 10:14:30 +01:00
|
|
|
}
|
2019-05-21 17:57:46 +02:00
|
|
|
|
|
|
|
/* Send the signal if necessary */
|
|
|
|
|
|
|
|
if (signo != 0)
|
|
|
|
{
|
2020-02-05 10:52:26 +01:00
|
|
|
nxsig_kill(priv->td_pid, signo);
|
2019-05-21 17:57:46 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2015-12-07 16:26:57 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: telnet_getchar
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Get another character for the user received buffer from the RX buffer
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static void telnet_getchar(FAR struct telnet_dev_s *priv, uint8_t ch,
|
|
|
|
FAR char *dest, int *nread)
|
|
|
|
{
|
|
|
|
register int index;
|
|
|
|
|
2018-01-09 12:16:02 +01:00
|
|
|
#ifndef CONFIG_TELNET_CHARACTER_MODE
|
2015-12-07 16:26:57 +01:00
|
|
|
/* Ignore carriage returns */
|
|
|
|
|
2020-02-05 10:52:26 +01:00
|
|
|
if (ch != TELNET_CR)
|
2018-01-09 12:16:02 +01:00
|
|
|
#endif
|
2015-12-07 16:26:57 +01:00
|
|
|
{
|
|
|
|
/* Add all other characters to the destination buffer */
|
|
|
|
|
|
|
|
index = *nread;
|
|
|
|
dest[index++] = ch;
|
|
|
|
*nread = index;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: telnet_receive
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Process a received Telnet buffer
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2019-01-05 19:14:05 +01:00
|
|
|
static ssize_t telnet_receive(FAR struct telnet_dev_s *priv,
|
|
|
|
FAR const char *src, size_t srclen,
|
|
|
|
FAR char *dest, size_t destlen)
|
2015-12-07 16:26:57 +01:00
|
|
|
{
|
|
|
|
int nread;
|
|
|
|
uint8_t ch;
|
|
|
|
|
2016-06-20 19:59:15 +02:00
|
|
|
ninfo("srclen: %d destlen: %d\n", srclen, destlen);
|
2015-12-07 16:26:57 +01:00
|
|
|
|
|
|
|
for (nread = 0; srclen > 0 && nread < destlen; srclen--)
|
|
|
|
{
|
|
|
|
ch = *src++;
|
2016-06-20 19:59:15 +02:00
|
|
|
ninfo("ch=%02x state=%d\n", ch, priv->td_state);
|
2015-12-07 16:26:57 +01:00
|
|
|
|
|
|
|
switch (priv->td_state)
|
|
|
|
{
|
|
|
|
case STATE_IAC:
|
|
|
|
if (ch == TELNET_IAC)
|
|
|
|
{
|
|
|
|
telnet_getchar(priv, ch, dest, &nread);
|
|
|
|
priv->td_state = STATE_NORMAL;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
switch (ch)
|
|
|
|
{
|
|
|
|
case TELNET_WILL:
|
|
|
|
priv->td_state = STATE_WILL;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case TELNET_WONT:
|
|
|
|
priv->td_state = STATE_WONT;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case TELNET_DO:
|
|
|
|
priv->td_state = STATE_DO;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case TELNET_DONT:
|
|
|
|
priv->td_state = STATE_DONT;
|
|
|
|
break;
|
|
|
|
|
2019-01-05 19:14:05 +01:00
|
|
|
#ifdef CONFIG_TELNET_SUPPORT_NAWS
|
|
|
|
case TELNET_SB:
|
|
|
|
priv->td_state = STATE_SB;
|
|
|
|
priv->td_sb_count = 0;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case TELNET_SE:
|
|
|
|
priv->td_state = STATE_NORMAL;
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
|
2015-12-07 16:26:57 +01:00
|
|
|
default:
|
|
|
|
priv->td_state = STATE_NORMAL;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case STATE_WILL:
|
2019-01-05 19:14:05 +01:00
|
|
|
#ifdef CONFIG_TELNET_SUPPORT_NAWS
|
|
|
|
/* For NAWS, Reply with a DO */
|
|
|
|
|
|
|
|
if (ch == TELNET_NAWS)
|
|
|
|
{
|
|
|
|
telnet_sendopt(priv, TELNET_DO, ch);
|
|
|
|
}
|
|
|
|
|
2020-02-22 19:31:14 +01:00
|
|
|
/* Reply with a DON'T */
|
2015-12-07 16:26:57 +01:00
|
|
|
|
2019-01-05 19:14:05 +01:00
|
|
|
else
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
telnet_sendopt(priv, TELNET_DONT, ch);
|
|
|
|
ninfo("Suppress: 0x%02X (%d)\n", ch, ch);
|
|
|
|
}
|
|
|
|
|
2015-12-07 16:26:57 +01:00
|
|
|
priv->td_state = STATE_NORMAL;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case STATE_WONT:
|
|
|
|
telnet_sendopt(priv, TELNET_DONT, ch);
|
|
|
|
priv->td_state = STATE_NORMAL;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case STATE_DO:
|
2018-01-09 12:16:02 +01:00
|
|
|
#ifdef CONFIG_TELNET_CHARACTER_MODE
|
2020-02-05 16:52:37 +01:00
|
|
|
if (ch == TELNET_SGA || ch == TELNET_ECHO)
|
2018-01-09 12:16:02 +01:00
|
|
|
{
|
2020-04-12 18:09:04 +02:00
|
|
|
/* If it received 'ECHO' or 'Suppress Go Ahead', then do
|
|
|
|
* nothing.
|
|
|
|
*/
|
2018-01-09 12:16:02 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Reply with a WONT */
|
|
|
|
|
|
|
|
telnet_sendopt(priv, TELNET_WONT, ch);
|
2019-01-05 19:14:05 +01:00
|
|
|
ninfo("WONT: 0x%02X\n", ch);
|
2018-01-09 12:16:02 +01:00
|
|
|
}
|
|
|
|
#else
|
2015-12-07 16:26:57 +01:00
|
|
|
/* Reply with a WONT */
|
|
|
|
|
|
|
|
telnet_sendopt(priv, TELNET_WONT, ch);
|
2018-01-09 12:16:02 +01:00
|
|
|
#endif
|
2015-12-07 16:26:57 +01:00
|
|
|
priv->td_state = STATE_NORMAL;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case STATE_DONT:
|
2019-12-05 21:54:50 +01:00
|
|
|
|
2015-12-07 16:26:57 +01:00
|
|
|
/* Reply with a WONT */
|
|
|
|
|
|
|
|
telnet_sendopt(priv, TELNET_WONT, ch);
|
|
|
|
priv->td_state = STATE_NORMAL;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case STATE_NORMAL:
|
|
|
|
if (ch == TELNET_IAC)
|
|
|
|
{
|
|
|
|
priv->td_state = STATE_IAC;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
telnet_getchar(priv, ch, dest, &nread);
|
|
|
|
}
|
|
|
|
break;
|
2019-01-05 19:14:05 +01:00
|
|
|
|
|
|
|
#ifdef CONFIG_TELNET_SUPPORT_NAWS
|
2019-05-21 17:57:46 +02:00
|
|
|
/* Handle Telnet Sub negotiation request */
|
2019-01-05 19:14:05 +01:00
|
|
|
|
|
|
|
case STATE_SB:
|
|
|
|
switch (ch)
|
|
|
|
{
|
|
|
|
case TELNET_NAWS:
|
|
|
|
priv->td_state = STATE_SB_NAWS;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
priv->td_state = STATE_NORMAL;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
/* Handle NAWS sub-option negotiation */
|
|
|
|
|
|
|
|
case STATE_SB_NAWS:
|
2019-12-05 21:54:50 +01:00
|
|
|
|
2019-01-05 19:14:05 +01:00
|
|
|
/* Update cols / rows based on received byte count */
|
|
|
|
|
|
|
|
switch (priv->td_sb_count)
|
|
|
|
{
|
|
|
|
case 0:
|
2019-02-24 18:51:25 +01:00
|
|
|
priv->td_cols = (priv->td_cols & 0x00ff) | (ch << 8);
|
2019-01-05 19:14:05 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 1:
|
2019-02-24 18:51:25 +01:00
|
|
|
priv->td_cols = (priv->td_cols & 0xff00) | ch;
|
2019-01-05 19:14:05 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 2:
|
2019-02-24 18:51:25 +01:00
|
|
|
priv->td_rows = (priv->td_rows & 0x00ff) | (ch << 8);
|
2019-01-05 19:14:05 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 3:
|
2019-02-24 18:51:25 +01:00
|
|
|
priv->td_rows = (priv->td_rows & 0xff00) | ch;
|
2019-01-05 19:14:05 +01:00
|
|
|
ninfo("NAWS: %d,%d", priv->td_cols, priv->td_rows);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Increment SB count and switch to NORMAL when complete */
|
|
|
|
|
|
|
|
if (++priv->td_sb_count == 4)
|
|
|
|
{
|
|
|
|
priv->td_state = STATE_NORMAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
#endif
|
2015-12-07 16:26:57 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* We get here if (1) all of the received bytes have been processed, or
|
|
|
|
* (2) if the user's buffer has become full.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (srclen > 0)
|
|
|
|
{
|
|
|
|
/* Remember where we left off. These bytes will be returned the next
|
|
|
|
* time that telnet_read() is called.
|
|
|
|
*/
|
|
|
|
|
|
|
|
priv->td_pending = srclen;
|
|
|
|
priv->td_offset = (src - priv->td_rxbuffer);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* All of the received bytes were consumed */
|
|
|
|
|
|
|
|
priv->td_pending = 0;
|
|
|
|
priv->td_offset = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return nread;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: telnet_putchar
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Put another character from the user buffer to the TX buffer.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static bool telnet_putchar(FAR struct telnet_dev_s *priv, uint8_t ch,
|
|
|
|
int *nread)
|
|
|
|
{
|
|
|
|
register int index;
|
|
|
|
bool ret = false;
|
|
|
|
|
2020-04-12 18:09:04 +02:00
|
|
|
/* Ignore carriage returns (we will put these in automatically as
|
|
|
|
* necessary).
|
|
|
|
*/
|
2015-12-07 16:26:57 +01:00
|
|
|
|
2020-02-05 10:52:26 +01:00
|
|
|
if (ch != TELNET_CR)
|
2015-12-07 16:26:57 +01:00
|
|
|
{
|
|
|
|
/* Add all other characters to the destination buffer */
|
|
|
|
|
|
|
|
index = *nread;
|
|
|
|
priv->td_txbuffer[index++] = ch;
|
|
|
|
|
|
|
|
/* Check for line feeds */
|
|
|
|
|
2020-02-05 10:52:26 +01:00
|
|
|
if (ch == TELNET_NL)
|
2015-12-07 16:26:57 +01:00
|
|
|
{
|
|
|
|
/* Now add the carriage return */
|
|
|
|
|
2020-02-05 10:52:26 +01:00
|
|
|
priv->td_txbuffer[index++] = TELNET_CR;
|
2015-12-07 16:26:57 +01:00
|
|
|
|
|
|
|
/* End of line */
|
|
|
|
|
|
|
|
ret = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
*nread = index;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: telnet_sendopt
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Send the telnet option bytes
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static void telnet_sendopt(FAR struct telnet_dev_s *priv, uint8_t option,
|
|
|
|
uint8_t value)
|
|
|
|
{
|
2020-02-05 09:24:54 +01:00
|
|
|
uint8_t optbuf[3];
|
2017-09-30 01:48:15 +02:00
|
|
|
int ret;
|
|
|
|
|
2015-12-07 16:26:57 +01:00
|
|
|
optbuf[0] = TELNET_IAC;
|
|
|
|
optbuf[1] = option;
|
|
|
|
optbuf[2] = value;
|
|
|
|
|
2020-02-05 09:24:54 +01:00
|
|
|
telnet_dumpbuffer("Send optbuf", optbuf, 3);
|
2017-09-30 01:48:15 +02:00
|
|
|
|
2020-02-05 09:24:54 +01:00
|
|
|
ret = psock_send(&priv->td_psock, optbuf, 3, 0);
|
2017-09-30 01:48:15 +02:00
|
|
|
if (ret < 0)
|
2015-12-07 16:26:57 +01:00
|
|
|
{
|
2017-09-30 01:48:15 +02:00
|
|
|
nerr("ERROR: Failed to send TELNET_IAC: %d\n", ret);
|
2015-12-07 16:26:57 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: telnet_open
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int telnet_open(FAR struct file *filep)
|
|
|
|
{
|
|
|
|
FAR struct inode *inode = filep->f_inode;
|
|
|
|
FAR struct telnet_dev_s *priv = inode->i_private;
|
|
|
|
int tmp;
|
|
|
|
int ret;
|
|
|
|
|
2016-06-20 19:59:15 +02:00
|
|
|
ninfo("td_crefs: %d\n", priv->td_crefs);
|
2015-12-07 16:26:57 +01:00
|
|
|
|
|
|
|
/* Get exclusive access to the device structures */
|
|
|
|
|
2017-10-04 23:22:27 +02:00
|
|
|
ret = nxsem_wait(&priv->td_exclsem);
|
2015-12-07 16:26:57 +01:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
2017-10-04 23:22:27 +02:00
|
|
|
nerr("ERROR: nxsem_wait failed: %d\n", ret);
|
2015-12-07 16:26:57 +01:00
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Increment the count of references to the device. If this the first
|
|
|
|
* time that the driver has been opened for this device, then initialize
|
|
|
|
* the device.
|
|
|
|
*/
|
|
|
|
|
|
|
|
tmp = priv->td_crefs + 1;
|
|
|
|
if (tmp > 255)
|
|
|
|
{
|
|
|
|
/* More than 255 opens; uint8_t would overflow to zero */
|
|
|
|
|
|
|
|
ret = -EMFILE;
|
|
|
|
goto errout_with_sem;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Save the new open count on success */
|
|
|
|
|
|
|
|
priv->td_crefs = tmp;
|
|
|
|
ret = OK;
|
|
|
|
|
|
|
|
errout_with_sem:
|
2017-10-03 23:35:24 +02:00
|
|
|
nxsem_post(&priv->td_exclsem);
|
2015-12-07 16:26:57 +01:00
|
|
|
|
|
|
|
errout:
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: telnet_close
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int telnet_close(FAR struct file *filep)
|
|
|
|
{
|
|
|
|
FAR struct inode *inode = filep->f_inode;
|
|
|
|
FAR struct telnet_dev_s *priv = inode->i_private;
|
|
|
|
FAR char *devpath;
|
|
|
|
int ret;
|
2019-05-21 17:57:46 +02:00
|
|
|
int i;
|
2015-12-07 16:26:57 +01:00
|
|
|
|
2016-06-20 19:59:15 +02:00
|
|
|
ninfo("td_crefs: %d\n", priv->td_crefs);
|
2015-12-07 16:26:57 +01:00
|
|
|
|
|
|
|
/* Get exclusive access to the device structures */
|
|
|
|
|
2017-10-04 23:22:27 +02:00
|
|
|
ret = nxsem_wait(&priv->td_exclsem);
|
2015-12-07 16:26:57 +01:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
2017-10-04 23:22:27 +02:00
|
|
|
nerr("ERROR: nxsem_wait failed: %d\n", ret);
|
2015-12-07 16:26:57 +01:00
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Decrement the references to the driver. If the reference count will
|
|
|
|
* decrement to 0, then uninitialize the driver.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (priv->td_crefs > 1)
|
|
|
|
{
|
|
|
|
/* Just decrement the reference count and release the semaphore */
|
|
|
|
|
|
|
|
priv->td_crefs--;
|
2017-10-03 23:35:24 +02:00
|
|
|
nxsem_post(&priv->td_exclsem);
|
2015-12-07 16:26:57 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Re-create the path to the driver. */
|
|
|
|
|
2020-02-05 10:52:26 +01:00
|
|
|
ret = asprintf(&devpath, TELNET_DEVFMT, priv->td_minor);
|
2015-12-07 16:26:57 +01:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
2016-06-20 20:44:38 +02:00
|
|
|
nerr("ERROR: Failed to allocate the driver path\n");
|
2015-12-07 16:26:57 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Un-register the character driver */
|
|
|
|
|
|
|
|
ret = unregister_driver(devpath);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
/* NOTE: a return value of -EBUSY is not an error, it simply
|
|
|
|
* means that the Telnet driver is busy now and cannot be
|
|
|
|
* registered now because there are other sessions using the
|
|
|
|
* connection. The driver will be properly unregistered when
|
|
|
|
* the final session terminates.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (ret != -EBUSY)
|
|
|
|
{
|
2016-06-20 20:44:38 +02:00
|
|
|
nerr("ERROR: Failed to unregister the driver %s: %d\n",
|
|
|
|
devpath, ret);
|
2015-12-07 16:26:57 +01:00
|
|
|
}
|
2020-02-05 10:52:26 +01:00
|
|
|
else
|
|
|
|
{
|
|
|
|
ret = OK;
|
|
|
|
}
|
2015-12-07 16:26:57 +01:00
|
|
|
}
|
|
|
|
|
2020-02-05 10:52:26 +01:00
|
|
|
kmm_free(devpath);
|
2015-12-07 16:26:57 +01:00
|
|
|
}
|
|
|
|
|
2019-05-21 17:57:46 +02:00
|
|
|
/* Remove ourself from the clients list */
|
|
|
|
|
|
|
|
nxsem_wait(&g_clients_sem);
|
|
|
|
for (i = 0; i < CONFIG_TELNET_MAXLCLIENTS; i++)
|
|
|
|
{
|
|
|
|
if (g_telnet_clients[i] == priv)
|
|
|
|
{
|
2020-02-05 10:52:26 +01:00
|
|
|
g_telnet_clients[i] = NULL;
|
2019-05-21 17:57:46 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-22 15:46:40 +02:00
|
|
|
/* If the socket is still polling */
|
|
|
|
|
2020-02-05 10:52:26 +01:00
|
|
|
if (priv->td_fds.events)
|
2019-05-21 17:57:46 +02:00
|
|
|
{
|
|
|
|
/* Tear down the poll */
|
|
|
|
|
2020-02-05 10:52:26 +01:00
|
|
|
psock_poll(&priv->td_psock, &priv->td_fds, FALSE);
|
|
|
|
priv->td_fds.events = 0;
|
2019-05-21 17:57:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
nxsem_post(&g_clients_sem);
|
|
|
|
|
2019-05-22 15:46:40 +02:00
|
|
|
/* Notify the I/O thread that a client was removed */
|
2019-05-21 17:57:46 +02:00
|
|
|
|
|
|
|
nxsem_post(&g_iosem);
|
|
|
|
|
2015-12-07 16:26:57 +01:00
|
|
|
/* Close the socket */
|
|
|
|
|
|
|
|
psock_close(&priv->td_psock);
|
|
|
|
|
|
|
|
/* Release the driver memory. What if there are threads waiting on
|
|
|
|
* td_exclsem? They will never be awakened! How could this happen?
|
|
|
|
* crefs == 1 so there are no other open references to the driver.
|
|
|
|
* But this could have if someone were trying to re-open the driver
|
|
|
|
* after every other thread has closed it. That really should not
|
|
|
|
* happen in the intended usage model.
|
|
|
|
*/
|
|
|
|
|
|
|
|
DEBUGASSERT(priv->td_exclsem.semcount == 0);
|
2017-10-03 23:35:24 +02:00
|
|
|
nxsem_destroy(&priv->td_exclsem);
|
2020-02-05 10:52:26 +01:00
|
|
|
kmm_free(priv);
|
2015-12-07 16:26:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
errout:
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: telnet_read
|
|
|
|
****************************************************************************/
|
|
|
|
|
2019-01-05 19:14:05 +01:00
|
|
|
static ssize_t telnet_read(FAR struct file *filep, FAR char *buffer,
|
|
|
|
size_t len)
|
2015-12-07 16:26:57 +01:00
|
|
|
{
|
|
|
|
FAR struct inode *inode = filep->f_inode;
|
|
|
|
FAR struct telnet_dev_s *priv = inode->i_private;
|
2020-03-31 16:05:48 +02:00
|
|
|
ssize_t nread = 0;
|
|
|
|
int ret;
|
2015-12-07 16:26:57 +01:00
|
|
|
|
2016-06-20 19:59:15 +02:00
|
|
|
ninfo("len: %d\n", len);
|
2015-12-07 16:26:57 +01:00
|
|
|
|
|
|
|
/* First, handle the case where there are still valid bytes left in the
|
|
|
|
* I/O buffer from the last time that read was called. NOTE: Much of
|
|
|
|
* what we read may be protocol stuff and may not correspond to user
|
2019-05-22 15:46:40 +02:00
|
|
|
* data. Hence we need the loop and we need may need to wait for data
|
2015-12-07 16:26:57 +01:00
|
|
|
* multiple times in order to get data that the client is interested in.
|
|
|
|
*/
|
|
|
|
|
|
|
|
do
|
|
|
|
{
|
2019-06-03 16:20:21 +02:00
|
|
|
FAR const char *src;
|
|
|
|
|
2019-05-22 15:46:40 +02:00
|
|
|
if (priv->td_pending == 0)
|
2015-12-07 16:26:57 +01:00
|
|
|
{
|
2020-02-05 15:04:34 +01:00
|
|
|
/* poll fds.revents contains last poll status in case of error */
|
|
|
|
|
2020-02-05 10:52:26 +01:00
|
|
|
if ((priv->td_fds.revents & (POLLHUP | POLLERR)) != 0)
|
2020-02-05 15:04:34 +01:00
|
|
|
{
|
|
|
|
return -EPIPE;
|
|
|
|
}
|
|
|
|
|
2019-01-05 19:14:05 +01:00
|
|
|
if (filep->f_oflags & O_NONBLOCK)
|
|
|
|
{
|
2020-02-05 15:04:34 +01:00
|
|
|
return -EAGAIN;
|
2019-01-05 19:14:05 +01:00
|
|
|
}
|
|
|
|
|
2020-03-31 16:05:48 +02:00
|
|
|
/* Wait for new data, interrupt, or thread cancellation */
|
|
|
|
|
|
|
|
ret = nxsem_wait(&priv->td_iosem);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
nerr("ERROR: nxsem_wait failed: %d\n", ret);
|
|
|
|
return (ssize_t)ret;
|
|
|
|
}
|
2015-12-07 16:26:57 +01:00
|
|
|
|
2020-02-05 15:04:34 +01:00
|
|
|
continue;
|
2015-12-07 16:26:57 +01:00
|
|
|
}
|
2019-05-22 15:46:40 +02:00
|
|
|
|
|
|
|
/* Take exclusive access to data buffer */
|
|
|
|
|
2020-03-31 16:05:48 +02:00
|
|
|
ret = nxsem_wait(&priv->td_exclsem);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
nerr("ERROR: nxsem_wait failed: %d\n", ret);
|
|
|
|
return (ssize_t)ret;
|
|
|
|
}
|
2019-05-22 15:46:40 +02:00
|
|
|
|
|
|
|
/* Process the buffered telnet data */
|
|
|
|
|
2019-06-03 16:20:21 +02:00
|
|
|
src = &priv->td_rxbuffer[priv->td_offset];
|
2020-03-31 16:05:48 +02:00
|
|
|
nread = telnet_receive(priv, src, priv->td_pending, buffer, len);
|
2019-05-22 15:46:40 +02:00
|
|
|
|
|
|
|
nxsem_post(&priv->td_exclsem);
|
2015-12-07 16:26:57 +01:00
|
|
|
}
|
2020-03-31 16:05:48 +02:00
|
|
|
while (nread == 0);
|
2015-12-07 16:26:57 +01:00
|
|
|
|
2018-02-01 17:00:02 +01:00
|
|
|
/* Returned Value:
|
2015-12-07 16:26:57 +01:00
|
|
|
*
|
2020-03-31 16:05:48 +02:00
|
|
|
* nread > 0: The number of characters copied into the user buffer by
|
|
|
|
* telnet_receive().
|
|
|
|
* nread <= 0: Loss of connection or error events reported by recv().
|
2015-12-07 16:26:57 +01:00
|
|
|
*/
|
|
|
|
|
2020-03-31 16:05:48 +02:00
|
|
|
return nread;
|
2015-12-07 16:26:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: telnet_write
|
|
|
|
****************************************************************************/
|
|
|
|
|
2019-01-05 19:14:05 +01:00
|
|
|
static ssize_t telnet_write(FAR struct file *filep, FAR const char *buffer,
|
|
|
|
size_t len)
|
2015-12-07 16:26:57 +01:00
|
|
|
{
|
|
|
|
FAR struct inode *inode = filep->f_inode;
|
|
|
|
FAR struct telnet_dev_s *priv = inode->i_private;
|
|
|
|
FAR const char *src = buffer;
|
|
|
|
ssize_t nsent;
|
|
|
|
ssize_t ret;
|
|
|
|
int ncopied;
|
|
|
|
char ch;
|
|
|
|
bool eol;
|
|
|
|
|
2016-06-20 19:59:15 +02:00
|
|
|
ninfo("len: %d\n", len);
|
2015-12-07 16:26:57 +01:00
|
|
|
|
|
|
|
/* Process each character from the user buffer */
|
|
|
|
|
|
|
|
for (nsent = 0, ncopied = 0; nsent < len; nsent++)
|
|
|
|
{
|
|
|
|
/* Get the next character from the user buffer */
|
|
|
|
|
|
|
|
ch = *src++;
|
|
|
|
|
|
|
|
/* Add the character to the TX buffer */
|
|
|
|
|
|
|
|
eol = telnet_putchar(priv, ch, &ncopied);
|
|
|
|
|
|
|
|
/* Was that the end of a line? Or is the buffer too full to hold the
|
2020-02-05 09:24:54 +01:00
|
|
|
* next largest character sequence ("\r\n")?
|
2015-12-07 16:26:57 +01:00
|
|
|
*/
|
|
|
|
|
2020-02-05 09:24:54 +01:00
|
|
|
if (eol || ncopied > CONFIG_TELNET_TXBUFFER_SIZE - 2)
|
2015-12-07 16:26:57 +01:00
|
|
|
{
|
|
|
|
/* Yes... send the data now */
|
|
|
|
|
|
|
|
ret = psock_send(&priv->td_psock, priv->td_txbuffer, ncopied, 0);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
2019-01-05 19:14:05 +01:00
|
|
|
nerr("ERROR: psock_send failed '%s': %d\n",
|
|
|
|
priv->td_txbuffer, ret);
|
2015-12-07 16:26:57 +01:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Reset the index to the beginning of the TX buffer. */
|
|
|
|
|
|
|
|
ncopied = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Send anything remaining in the TX buffer */
|
|
|
|
|
|
|
|
if (ncopied > 0)
|
|
|
|
{
|
|
|
|
ret = psock_send(&priv->td_psock, priv->td_txbuffer, ncopied, 0);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
2020-02-07 14:16:21 +01:00
|
|
|
nerr("ERROR: psock_send failed '%s': %d\n",
|
|
|
|
priv->td_txbuffer, ret);
|
2015-12-07 16:26:57 +01:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Notice that we don't actually return the number of bytes sent, but
|
|
|
|
* rather, the number of bytes that the caller asked us to send. We may
|
2020-02-05 09:24:54 +01:00
|
|
|
* have sent more bytes (because of CR-LF expansion). But it confuses
|
|
|
|
* some logic if you report that you sent more than you were requested to.
|
2015-12-07 16:26:57 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
return len;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
2015-12-07 20:48:06 +01:00
|
|
|
* Name: telnet_session
|
2015-12-07 16:26:57 +01:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Create a character driver to "wrap" the telnet session. This function
|
|
|
|
* will select and return a unique path for the new telnet device.
|
|
|
|
*
|
2018-03-13 16:52:27 +01:00
|
|
|
* Input Parameters:
|
2015-12-07 20:48:06 +01:00
|
|
|
* session - On input, contains the socket descriptor that represents the
|
2019-01-05 19:14:05 +01:00
|
|
|
* new telnet connection. On output, it holds the path to the new Telnet
|
|
|
|
* driver.
|
2015-12-07 16:26:57 +01:00
|
|
|
*
|
2018-02-01 17:00:02 +01:00
|
|
|
* Returned Value:
|
2015-12-07 20:48:06 +01:00
|
|
|
* Zero (OK) on success; a negated errno value on failure.
|
2015-12-07 16:26:57 +01:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2015-12-07 20:48:06 +01:00
|
|
|
static int telnet_session(FAR struct telnet_session_s *session)
|
2015-12-07 16:26:57 +01:00
|
|
|
{
|
|
|
|
FAR struct telnet_dev_s *priv;
|
|
|
|
FAR struct socket *psock;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* Allocate instance data for this driver */
|
|
|
|
|
2020-02-05 10:52:26 +01:00
|
|
|
priv = (FAR struct telnet_dev_s *)kmm_zalloc(sizeof(struct telnet_dev_s));
|
2015-12-07 16:26:57 +01:00
|
|
|
if (!priv)
|
|
|
|
{
|
2016-06-20 20:44:38 +02:00
|
|
|
nerr("ERROR: Failed to allocate the driver data structure\n");
|
2015-12-07 20:48:06 +01:00
|
|
|
return -ENOMEM;
|
2015-12-07 16:26:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Initialize the allocated driver instance */
|
|
|
|
|
2017-10-03 20:51:15 +02:00
|
|
|
nxsem_init(&priv->td_exclsem, 0, 1);
|
2019-05-22 15:46:40 +02:00
|
|
|
nxsem_init(&priv->td_iosem, 0, 0);
|
|
|
|
|
|
|
|
/* td_iosem is used for signaling and, hence, must not participate in
|
|
|
|
* priority inheritance.
|
|
|
|
*/
|
|
|
|
|
2020-05-17 15:56:21 +02:00
|
|
|
nxsem_set_protocol(&priv->td_iosem, SEM_PRIO_NONE);
|
2015-12-07 16:26:57 +01:00
|
|
|
|
2019-01-05 19:14:05 +01:00
|
|
|
priv->td_state = STATE_NORMAL;
|
|
|
|
priv->td_crefs = 0;
|
2020-02-05 10:57:58 +01:00
|
|
|
priv->td_minor = 0;
|
2019-01-05 19:14:05 +01:00
|
|
|
priv->td_pending = 0;
|
|
|
|
priv->td_offset = 0;
|
2019-05-21 17:57:46 +02:00
|
|
|
#ifdef HAVE_SIGNALS
|
2020-02-05 10:52:26 +01:00
|
|
|
priv->td_pid = -1;
|
2019-05-21 17:57:46 +02:00
|
|
|
#endif
|
2019-01-05 19:14:05 +01:00
|
|
|
#ifdef CONFIG_TELNET_SUPPORT_NAWS
|
|
|
|
priv->td_rows = 25;
|
|
|
|
priv->td_cols = 80;
|
|
|
|
priv->td_sb_count = 0;
|
|
|
|
#endif
|
2015-12-07 16:26:57 +01:00
|
|
|
|
|
|
|
/* Clone the internal socket structure. We do this so that it will be
|
|
|
|
* independent of threads and of socket descriptors (the original socket
|
|
|
|
* instance resided in the daemon's task group`).
|
|
|
|
*/
|
|
|
|
|
2015-12-07 20:48:06 +01:00
|
|
|
psock = sockfd_socket(session->ts_sd);
|
2015-12-07 16:26:57 +01:00
|
|
|
if (!psock)
|
|
|
|
{
|
2019-01-05 19:14:05 +01:00
|
|
|
nerr("ERROR: Failed to convert sd=%d to a socket structure\n",
|
|
|
|
session->ts_sd);
|
2015-12-07 20:48:06 +01:00
|
|
|
ret = -EINVAL;
|
2015-12-07 16:26:57 +01:00
|
|
|
goto errout_with_dev;
|
|
|
|
}
|
|
|
|
|
2020-01-31 09:44:28 +01:00
|
|
|
ret = psock_dup2(psock, &priv->td_psock);
|
2015-12-07 16:26:57 +01:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
2020-01-31 09:44:28 +01:00
|
|
|
nerr("ERROR: psock_dup2 failed: %d\n", ret);
|
2015-12-07 16:26:57 +01:00
|
|
|
goto errout_with_dev;
|
|
|
|
}
|
|
|
|
|
2020-02-23 09:50:23 +01:00
|
|
|
/* Allocate a unique minor device number of the telnet driver.
|
2015-12-07 20:48:06 +01:00
|
|
|
* Get exclusive access to the minor counter.
|
|
|
|
*/
|
2015-12-07 16:26:57 +01:00
|
|
|
|
2020-02-05 10:57:58 +01:00
|
|
|
ret = nxsem_wait_uninterruptible(&g_clients_sem);
|
2020-01-02 17:49:34 +01:00
|
|
|
if (ret < 0)
|
2015-12-07 16:26:57 +01:00
|
|
|
{
|
2020-01-02 17:49:34 +01:00
|
|
|
nerr("ERROR: nxsem_wait failed: %d\n", ret);
|
|
|
|
goto errout_with_clone;
|
2015-12-07 16:26:57 +01:00
|
|
|
}
|
|
|
|
|
2015-12-07 20:48:06 +01:00
|
|
|
/* Loop until the device name is verified to be unique. */
|
2015-12-07 16:26:57 +01:00
|
|
|
|
2020-02-05 10:57:58 +01:00
|
|
|
while (priv->td_minor < CONFIG_TELNET_MAXLCLIENTS)
|
2015-12-07 20:48:06 +01:00
|
|
|
{
|
2020-02-05 10:57:58 +01:00
|
|
|
if (g_telnet_clients[priv->td_minor] == NULL)
|
|
|
|
{
|
|
|
|
snprintf(session->ts_devpath, TELNET_DEVPATH_MAX,
|
2020-02-05 10:52:26 +01:00
|
|
|
TELNET_DEVFMT, priv->td_minor);
|
2020-02-05 10:57:58 +01:00
|
|
|
break;
|
|
|
|
}
|
2020-02-07 14:16:21 +01:00
|
|
|
|
2020-02-05 10:57:58 +01:00
|
|
|
priv->td_minor++;
|
2015-12-07 20:48:06 +01:00
|
|
|
}
|
|
|
|
|
2020-02-05 10:57:58 +01:00
|
|
|
if (priv->td_minor >= CONFIG_TELNET_MAXLCLIENTS)
|
2015-12-07 16:26:57 +01:00
|
|
|
{
|
2016-06-20 20:44:38 +02:00
|
|
|
nerr("ERROR: Too many sessions\n");
|
2015-12-07 20:48:06 +01:00
|
|
|
ret = -ENFILE;
|
|
|
|
goto errout_with_semaphore;
|
2015-12-07 16:26:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Register the driver */
|
|
|
|
|
2015-12-07 20:48:06 +01:00
|
|
|
ret = register_driver(session->ts_devpath, &g_telnet_fops, 0666, priv);
|
2015-12-07 16:26:57 +01:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
2016-06-20 20:44:38 +02:00
|
|
|
nerr("ERROR: Failed to register the driver %s: %d\n",
|
|
|
|
session->ts_devpath, ret);
|
2015-12-07 20:48:06 +01:00
|
|
|
goto errout_with_semaphore;
|
2015-12-07 16:26:57 +01:00
|
|
|
}
|
|
|
|
|
2015-12-07 20:48:06 +01:00
|
|
|
/* Close the original psoock (keeping the clone) */
|
|
|
|
|
|
|
|
psock_close(psock);
|
|
|
|
|
2019-01-19 13:43:21 +01:00
|
|
|
#ifdef CONFIG_TELNET_SUPPORT_NAWS
|
2019-01-05 19:14:05 +01:00
|
|
|
telnet_sendopt(priv, TELNET_DO, TELNET_NAWS);
|
2019-01-19 13:43:21 +01:00
|
|
|
#endif
|
2019-01-05 19:14:05 +01:00
|
|
|
|
2020-02-05 16:52:37 +01:00
|
|
|
#ifdef CONFIG_TELNET_CHARACTER_MODE
|
|
|
|
telnet_sendopt(priv, TELNET_WILL, TELNET_SGA);
|
|
|
|
telnet_sendopt(priv, TELNET_WILL, TELNET_ECHO);
|
|
|
|
#endif
|
|
|
|
|
2019-05-21 17:57:46 +02:00
|
|
|
/* Has the I/O thread been started? */
|
|
|
|
|
|
|
|
if (g_telnet_io_kthread == (pid_t)0)
|
|
|
|
{
|
|
|
|
/* g_iosem is used for signaling and, hence, must not participate in
|
|
|
|
* priority inheritance.
|
|
|
|
*/
|
|
|
|
|
2020-05-17 15:56:21 +02:00
|
|
|
nxsem_set_protocol(&g_iosem, SEM_PRIO_NONE);
|
2019-05-21 17:57:46 +02:00
|
|
|
|
|
|
|
/* Start the I/O thread */
|
|
|
|
|
|
|
|
g_telnet_io_kthread =
|
2019-05-22 15:46:40 +02:00
|
|
|
kthread_create("telnet_io", CONFIG_TELNET_IOTHREAD_PRIORITY,
|
2020-02-07 14:16:21 +01:00
|
|
|
CONFIG_TELNET_IOTHREAD_STACKSIZE, telnet_io_main,
|
|
|
|
NULL);
|
2019-05-21 17:57:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Save ourself in the list of Telnet client threads */
|
|
|
|
|
2020-02-05 10:57:58 +01:00
|
|
|
g_telnet_clients[priv->td_minor] = priv;
|
2019-05-21 17:57:46 +02:00
|
|
|
nxsem_post(&g_clients_sem);
|
|
|
|
nxsem_post(&g_iosem);
|
|
|
|
|
2015-12-07 20:48:06 +01:00
|
|
|
return OK;
|
|
|
|
|
|
|
|
errout_with_semaphore:
|
2020-02-05 10:57:58 +01:00
|
|
|
nxsem_post(&g_clients_sem);
|
2015-12-07 20:48:06 +01:00
|
|
|
|
|
|
|
errout_with_clone:
|
|
|
|
psock_close(&priv->td_psock);
|
2015-12-07 16:26:57 +01:00
|
|
|
|
|
|
|
errout_with_dev:
|
|
|
|
free(priv);
|
2015-12-07 20:48:06 +01:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: factory_read
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static ssize_t factory_read(FAR struct file *filep, FAR char *buffer,
|
|
|
|
size_t len)
|
|
|
|
{
|
|
|
|
return 0; /* Return EOF */
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: factory_write
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static ssize_t factory_write(FAR struct file *filep, FAR const char *buffer,
|
|
|
|
size_t len)
|
|
|
|
{
|
|
|
|
return len; /* Say that everything was written */
|
|
|
|
}
|
|
|
|
|
2020-02-05 09:12:53 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: telnet_ioctl
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int telnet_ioctl(FAR struct file *filep, int cmd, unsigned long arg)
|
|
|
|
{
|
|
|
|
FAR struct inode *inode = filep->f_inode;
|
|
|
|
FAR struct telnet_dev_s *priv = inode->i_private;
|
|
|
|
int ret = OK;
|
|
|
|
|
|
|
|
switch (cmd)
|
|
|
|
{
|
|
|
|
#ifdef HAVE_SIGNALS
|
2020-04-12 18:09:04 +02:00
|
|
|
/* Make the given terminal the controlling terminal of the calling
|
|
|
|
* process.
|
|
|
|
*/
|
2020-02-05 09:12:53 +01:00
|
|
|
|
|
|
|
case TIOCSCTTY:
|
|
|
|
{
|
|
|
|
/* Check if the ISIG flag is set in the termios c_lflag to enable
|
|
|
|
* this feature. This flag is set automatically for a serial console
|
|
|
|
* device.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* Save the PID of the recipient of the SIGINT signal. */
|
|
|
|
|
|
|
|
priv->td_pid = (pid_t)arg;
|
|
|
|
DEBUGASSERT((unsigned long)(priv->td_pid) == arg);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef CONFIG_TELNET_SUPPORT_NAWS
|
|
|
|
case TIOCGWINSZ:
|
|
|
|
{
|
|
|
|
FAR struct winsize *pw = (FAR struct winsize *)((uintptr_t)arg);
|
|
|
|
|
|
|
|
/* Get row/col from the private data */
|
|
|
|
|
|
|
|
pw->ws_row = priv->td_rows;
|
|
|
|
pw->ws_col = priv->td_cols;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
default:
|
|
|
|
ret = -ENOTTY;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
UNUSED(priv); /* Avoid warning if not used */
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2019-01-05 19:14:05 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: telnet_poll
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* The standard poll() operation redirects operations on socket descriptors
|
|
|
|
* to this function.
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* fd - The socket descriptor of interest
|
|
|
|
* fds - The structure describing the events to be monitored, OR NULL if
|
|
|
|
* this is a request to stop monitoring events.
|
|
|
|
* setup - true: Setup up the poll; false: Teardown the poll
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* 0: Success; Negated errno on failure
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int telnet_poll(FAR struct file *filep, FAR struct pollfd *fds,
|
|
|
|
bool setup)
|
|
|
|
{
|
|
|
|
FAR struct inode *inode = filep->f_inode;
|
|
|
|
FAR struct telnet_dev_s *priv = inode->i_private;
|
|
|
|
FAR struct socket *psock;
|
|
|
|
|
|
|
|
DEBUGASSERT(fds != NULL);
|
|
|
|
|
|
|
|
/* Get the underlying socket structure and verify that the sockfd
|
|
|
|
* corresponds to valid, allocated socket
|
|
|
|
*/
|
|
|
|
|
|
|
|
psock = &priv->td_psock;
|
|
|
|
if (!psock || psock->s_crefs <= 0)
|
|
|
|
{
|
|
|
|
return -EBADF;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Test if we have cached data waiting to be read */
|
|
|
|
|
|
|
|
if (priv->td_pending > 0)
|
|
|
|
{
|
|
|
|
/* Yes.. then signal the poll logic */
|
|
|
|
|
2019-01-08 15:16:43 +01:00
|
|
|
fds->revents |= (POLLRDNORM & fds->events);
|
2020-02-05 15:04:34 +01:00
|
|
|
if (fds->revents)
|
|
|
|
{
|
|
|
|
nxsem_post(fds->sem);
|
|
|
|
}
|
2019-01-05 19:14:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Then let psock_poll() do the heavy lifting */
|
|
|
|
|
|
|
|
return psock_poll(psock, fds, setup);
|
|
|
|
}
|
|
|
|
|
2019-05-21 17:57:46 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: telnet_io_main
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int telnet_io_main(int argc, FAR char** argv)
|
|
|
|
{
|
|
|
|
FAR struct telnet_dev_s *priv;
|
|
|
|
FAR char *buffer;
|
|
|
|
int i;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
while (1)
|
|
|
|
{
|
|
|
|
nxsem_reset(&g_iosem, 0);
|
|
|
|
|
|
|
|
/* Poll each client in the g_telnet_clients[] array. */
|
|
|
|
|
|
|
|
nxsem_wait(&g_clients_sem);
|
|
|
|
for (i = 0; i < CONFIG_TELNET_MAXLCLIENTS; i++)
|
|
|
|
{
|
2020-02-05 10:57:58 +01:00
|
|
|
priv = g_telnet_clients[i];
|
2020-02-05 10:52:26 +01:00
|
|
|
if (priv != NULL && !(priv->td_fds.revents & (POLLHUP | POLLERR)))
|
2019-05-21 17:57:46 +02:00
|
|
|
{
|
2020-02-05 10:52:26 +01:00
|
|
|
priv->td_fds.sem = &g_iosem;
|
|
|
|
priv->td_fds.events = POLLIN | POLLHUP | POLLERR;
|
|
|
|
priv->td_fds.revents = 0;
|
2019-05-21 17:57:46 +02:00
|
|
|
|
2020-02-05 10:52:26 +01:00
|
|
|
psock_poll(&priv->td_psock, &priv->td_fds, TRUE);
|
2019-05-21 17:57:46 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
nxsem_post(&g_clients_sem);
|
|
|
|
|
2020-02-05 10:52:26 +01:00
|
|
|
/* Wait for any Telnet connect/disconnect events
|
2019-05-22 15:46:40 +02:00
|
|
|
* to include/remove client sockets from polling
|
|
|
|
*/
|
2019-05-21 17:57:46 +02:00
|
|
|
|
2020-01-02 17:49:34 +01:00
|
|
|
nxsem_wait(&g_iosem);
|
2019-05-21 17:57:46 +02:00
|
|
|
|
|
|
|
/* Revisit each client in the g_telnet_clients[] array */
|
|
|
|
|
|
|
|
nxsem_wait(&g_clients_sem);
|
|
|
|
for (i = 0; i < CONFIG_TELNET_MAXLCLIENTS; i++)
|
|
|
|
{
|
2020-02-05 10:57:58 +01:00
|
|
|
priv = g_telnet_clients[i];
|
|
|
|
|
|
|
|
/* If poll was setup previously (events != 0) */
|
|
|
|
|
2020-02-05 10:52:26 +01:00
|
|
|
if (priv != NULL && priv->td_fds.events)
|
2019-05-21 17:57:46 +02:00
|
|
|
{
|
|
|
|
/* Check for a pending poll() */
|
|
|
|
|
2020-02-05 10:52:26 +01:00
|
|
|
if (priv->td_fds.revents & POLLIN)
|
2019-05-21 17:57:46 +02:00
|
|
|
{
|
|
|
|
if (priv->td_pending < CONFIG_TELNET_RXBUFFER_SIZE)
|
|
|
|
{
|
2019-05-22 15:46:40 +02:00
|
|
|
/* Take exclusive access to data buffer */
|
|
|
|
|
|
|
|
nxsem_wait(&priv->td_exclsem);
|
2019-05-21 17:57:46 +02:00
|
|
|
buffer = priv->td_rxbuffer + priv->td_pending +
|
|
|
|
priv->td_offset;
|
|
|
|
|
|
|
|
ret = psock_recv(&priv->td_psock, buffer,
|
|
|
|
CONFIG_TELNET_RXBUFFER_SIZE -
|
|
|
|
priv->td_pending - priv->td_offset,
|
|
|
|
0);
|
|
|
|
|
|
|
|
priv->td_pending += ret;
|
2019-05-22 15:46:40 +02:00
|
|
|
nxsem_post(&priv->td_exclsem);
|
|
|
|
|
|
|
|
/* Notify the client thread that data is available */
|
|
|
|
|
|
|
|
nxsem_post(&priv->td_iosem);
|
2019-05-21 17:57:46 +02:00
|
|
|
|
|
|
|
#ifdef HAVE_SIGNALS
|
|
|
|
/* Check if any of the received characters is a
|
|
|
|
* control that should generate a signal.
|
|
|
|
*/
|
|
|
|
|
2020-02-05 10:14:30 +01:00
|
|
|
telnet_check_ctrlchar(priv, buffer, ret);
|
2019-05-21 17:57:46 +02:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-05 10:57:58 +01:00
|
|
|
/* Tear it down */
|
2019-05-21 17:57:46 +02:00
|
|
|
|
2020-02-05 10:52:26 +01:00
|
|
|
psock_poll(&priv->td_psock, &priv->td_fds, FALSE);
|
|
|
|
priv->td_fds.events = 0;
|
2019-05-21 17:57:46 +02:00
|
|
|
|
2019-05-22 15:46:40 +02:00
|
|
|
/* POLLHUP (or POLLERR) indicates that this session has
|
|
|
|
* terminated.
|
|
|
|
*/
|
2019-05-21 17:57:46 +02:00
|
|
|
|
2020-02-05 10:52:26 +01:00
|
|
|
if (priv->td_fds.revents & (POLLHUP | POLLERR))
|
2019-05-21 17:57:46 +02:00
|
|
|
{
|
2019-05-22 15:46:40 +02:00
|
|
|
/* notify the client thread */
|
|
|
|
|
|
|
|
nxsem_post(&priv->td_iosem);
|
2019-05-21 17:57:46 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
nxsem_post(&g_clients_sem);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-12-07 20:48:06 +01:00
|
|
|
/****************************************************************************
|
2020-02-05 09:12:53 +01:00
|
|
|
* Name: factory_ioctl
|
2015-12-07 20:48:06 +01:00
|
|
|
****************************************************************************/
|
|
|
|
|
2020-02-05 09:12:53 +01:00
|
|
|
static int factory_ioctl(FAR struct file *filep, int cmd, unsigned long arg)
|
2015-12-07 20:48:06 +01:00
|
|
|
{
|
2020-02-05 09:12:53 +01:00
|
|
|
int ret = OK;
|
2015-12-07 20:48:06 +01:00
|
|
|
|
|
|
|
switch (cmd)
|
|
|
|
{
|
|
|
|
/* Command: SIOCTELNET
|
|
|
|
* Description: Create a Telnet sessions.
|
|
|
|
* Argument: A pointer to a write-able instance of struct
|
|
|
|
* telnet_session_s.
|
|
|
|
* Dependencies: CONFIG_NETDEV_TELNET
|
|
|
|
*/
|
|
|
|
|
|
|
|
case SIOCTELNET:
|
|
|
|
{
|
2019-05-21 17:57:46 +02:00
|
|
|
FAR struct telnet_session_s *session =
|
2020-02-05 09:12:53 +01:00
|
|
|
(FAR struct telnet_session_s *)((uintptr_t)arg);
|
2015-12-07 20:48:06 +01:00
|
|
|
|
|
|
|
if (session == NULL)
|
|
|
|
{
|
|
|
|
ret = -EINVAL;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ret = telnet_session(session);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
ret = -ENOTTY;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
2015-12-07 16:26:57 +01:00
|
|
|
}
|
2015-12-07 20:48:06 +01:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: telnet_initialize
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Create the Telnet factory at /dev/telnet.
|
|
|
|
*
|
2018-03-13 16:52:27 +01:00
|
|
|
* Input Parameters:
|
2015-12-07 20:48:06 +01:00
|
|
|
* None
|
|
|
|
*
|
2018-02-01 17:00:02 +01:00
|
|
|
* Returned Value:
|
2015-12-07 20:48:06 +01:00
|
|
|
* Zero (OK) on success; a negated errno value on failure.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
int telnet_initialize(void)
|
|
|
|
{
|
|
|
|
return register_driver("/dev/telnet", &g_factory_fops, 0666, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* CONFIG_NETDEV_TELNET */
|