2012-01-30 22:29:59 +01:00
|
|
|
/****************************************************************************
|
2021-03-18 16:06:01 +01:00
|
|
|
* apps/netutils/telnetd/telnetd_daemon.c
|
2012-01-30 22:29:59 +01:00
|
|
|
*
|
2021-06-11 06:22:42 +02:00
|
|
|
* Licensed to the Apache Software Foundation (ASF) under one or more
|
|
|
|
* contributor license agreements. See the NOTICE file distributed with
|
|
|
|
* this work for additional information regarding copyright ownership. The
|
|
|
|
* ASF licenses this file to you under the Apache License, Version 2.0 (the
|
|
|
|
* "License"); you may not use this file except in compliance with the
|
|
|
|
* License. You may obtain a copy of the License at
|
2012-01-30 22:29:59 +01:00
|
|
|
*
|
2021-06-11 06:22:42 +02:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2012-01-30 22:29:59 +01:00
|
|
|
*
|
2021-06-11 06:22:42 +02:00
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
|
|
* License for the specific language governing permissions and limitations
|
|
|
|
* under the License.
|
2012-01-30 22:29:59 +01:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
|
|
|
#include <sys/socket.h>
|
2015-12-07 20:51:37 +01:00
|
|
|
#include <sys/ioctl.h>
|
2012-01-30 22:29:59 +01:00
|
|
|
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <fcntl.h>
|
2015-01-22 13:33:55 +01:00
|
|
|
#include <signal.h>
|
2012-01-30 22:29:59 +01:00
|
|
|
#include <sched.h>
|
2022-10-16 13:44:38 +02:00
|
|
|
#include <spawn.h>
|
2012-01-30 22:29:59 +01:00
|
|
|
#include <errno.h>
|
|
|
|
#include <debug.h>
|
2014-10-09 00:23:48 +02:00
|
|
|
|
2015-12-07 16:29:29 +01:00
|
|
|
#include <nuttx/net/telnet.h>
|
|
|
|
|
2016-07-11 18:11:18 +02:00
|
|
|
#include "netutils/telnetd.h"
|
|
|
|
#include "netutils/netlib.h"
|
2012-01-30 22:29:59 +01:00
|
|
|
|
|
|
|
/****************************************************************************
|
2022-10-16 13:44:38 +02:00
|
|
|
* Public Functions
|
2022-08-05 07:34:49 +02:00
|
|
|
****************************************************************************/
|
|
|
|
|
2012-01-30 22:29:59 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: telnetd_daemon
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* This function is the Telnet daemon. It does not return (unless an
|
|
|
|
* error occurs).
|
|
|
|
*
|
|
|
|
* Parameters:
|
|
|
|
* Standard task start up arguments.
|
|
|
|
*
|
|
|
|
* Return:
|
|
|
|
* Does not return unless an error occurs.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2022-10-16 13:44:38 +02:00
|
|
|
int telnetd_daemon(FAR const struct telnetd_config_s *config)
|
2012-01-30 22:29:59 +01:00
|
|
|
{
|
2017-06-27 17:14:13 +02:00
|
|
|
union
|
|
|
|
{
|
|
|
|
struct sockaddr generic;
|
|
|
|
#ifdef CONFIG_NET_IPv4
|
|
|
|
struct sockaddr_in ipv4;
|
|
|
|
#endif
|
|
|
|
#ifdef CONFIG_NET_IPv6
|
|
|
|
struct sockaddr_in6 ipv6;
|
|
|
|
#endif
|
|
|
|
} addr;
|
2020-07-11 19:23:09 +02:00
|
|
|
|
2015-01-22 13:33:55 +01:00
|
|
|
#ifdef CONFIG_SCHED_HAVE_PARENT
|
|
|
|
struct sigaction sa;
|
|
|
|
sigset_t blockset;
|
2015-01-22 13:51:31 +01:00
|
|
|
#endif
|
2012-01-30 22:29:59 +01:00
|
|
|
socklen_t addrlen;
|
|
|
|
int listensd;
|
|
|
|
int acceptsd;
|
2020-02-08 17:24:16 +01:00
|
|
|
#ifdef CONFIG_NET_SOCKOPTS
|
2012-01-30 22:29:59 +01:00
|
|
|
int optval;
|
2020-02-08 17:24:16 +01:00
|
|
|
#endif
|
2012-01-30 22:29:59 +01:00
|
|
|
|
2015-01-22 13:33:55 +01:00
|
|
|
#ifdef CONFIG_SCHED_HAVE_PARENT
|
|
|
|
/* Call sigaction with the SA_NOCLDWAIT flag so that we do not transform
|
|
|
|
* children into "zombies" when they terminate: Child exit status will
|
|
|
|
* not be retained.
|
|
|
|
*
|
|
|
|
* NOTE: If the SA_NOCLDWAIT flag is set when establishing a handler for
|
|
|
|
* SIGCHLD, POSIX.1 leaves it unspecified whether a SIGCHLD signal is
|
|
|
|
* generated when a child process terminates. On both Linux and NuttX, a
|
|
|
|
* SIGCHLD signal will be generated in this case.
|
|
|
|
*/
|
|
|
|
|
|
|
|
sa.sa_handler = SIG_IGN;
|
|
|
|
sa.sa_flags = SA_NOCLDWAIT;
|
|
|
|
if (sigaction(SIGCHLD, &sa, NULL) < 0)
|
|
|
|
{
|
2020-02-03 10:16:49 +01:00
|
|
|
nerr("ERROR: sigaction failed: %d\n", errno);
|
2022-10-16 13:44:38 +02:00
|
|
|
goto errout;
|
2015-01-22 13:33:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Block receipt of the SIGCHLD signal */
|
|
|
|
|
|
|
|
sigemptyset(&blockset);
|
|
|
|
sigaddset(&blockset, SIGCHLD);
|
|
|
|
if (sigprocmask(SIG_BLOCK, &blockset, NULL) < 0)
|
|
|
|
{
|
2020-02-03 10:16:49 +01:00
|
|
|
nerr("ERROR: sigprocmask failed: %d\n", errno);
|
2022-10-16 13:44:38 +02:00
|
|
|
goto errout;
|
2015-01-22 13:33:55 +01:00
|
|
|
}
|
|
|
|
#endif /* CONFIG_SCHED_HAVE_PARENT */
|
|
|
|
|
2012-01-30 22:29:59 +01:00
|
|
|
/* Create a new TCP socket to use to listen for connections */
|
|
|
|
|
2022-10-16 13:44:38 +02:00
|
|
|
listensd = socket(config->d_family, SOCK_STREAM | SOCK_CLOEXEC, 0);
|
2012-01-30 22:29:59 +01:00
|
|
|
if (listensd < 0)
|
|
|
|
{
|
2017-06-27 17:14:13 +02:00
|
|
|
nerr("ERROR: socket() failed for family %u: %d\n",
|
2022-10-16 13:44:38 +02:00
|
|
|
config->d_family, errno);
|
|
|
|
goto errout;
|
2012-01-30 22:29:59 +01:00
|
|
|
}
|
|
|
|
|
2020-02-08 17:16:13 +01:00
|
|
|
#ifdef CONFIG_NET_SOCKOPTS
|
2012-01-30 22:29:59 +01:00
|
|
|
/* Set socket to reuse address */
|
|
|
|
|
|
|
|
optval = 1;
|
2020-02-03 14:35:37 +01:00
|
|
|
if (setsockopt(listensd, SOL_SOCKET, SO_REUSEADDR,
|
2022-10-16 13:44:38 +02:00
|
|
|
&optval, sizeof(int)) < 0)
|
2012-01-30 22:29:59 +01:00
|
|
|
{
|
2016-06-11 23:51:27 +02:00
|
|
|
nerr("ERROR: setsockopt SO_REUSEADDR failure: %d\n", errno);
|
2012-01-30 22:29:59 +01:00
|
|
|
goto errout_with_socket;
|
|
|
|
}
|
2020-02-08 17:16:13 +01:00
|
|
|
#endif
|
2012-01-30 22:29:59 +01:00
|
|
|
|
|
|
|
/* Bind the socket to a local address */
|
|
|
|
|
2017-06-27 17:14:13 +02:00
|
|
|
#ifdef CONFIG_NET_IPv4
|
2022-10-16 13:44:38 +02:00
|
|
|
if (config->d_family == AF_INET)
|
2017-06-27 17:14:13 +02:00
|
|
|
{
|
|
|
|
addr.ipv4.sin_family = AF_INET;
|
2022-10-16 13:44:38 +02:00
|
|
|
addr.ipv4.sin_port = config->d_port;
|
2017-06-27 17:14:13 +02:00
|
|
|
addr.ipv4.sin_addr.s_addr = INADDR_ANY;
|
|
|
|
addrlen = sizeof(struct sockaddr_in);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
#endif
|
|
|
|
#ifdef CONFIG_NET_IPv6
|
2022-10-16 13:44:38 +02:00
|
|
|
if (config->d_family == AF_INET6)
|
2017-06-27 17:14:13 +02:00
|
|
|
{
|
|
|
|
addr.ipv6.sin6_family = AF_INET6;
|
2022-10-16 13:44:38 +02:00
|
|
|
addr.ipv6.sin6_port = config->d_port;
|
2017-06-27 17:14:13 +02:00
|
|
|
addrlen = sizeof(struct sockaddr_in6);
|
|
|
|
|
2020-07-11 19:23:09 +02:00
|
|
|
memset(addr.ipv6.sin6_addr.s6_addr, 0,
|
|
|
|
sizeof(addr.ipv6.sin6_addr.s6_addr));
|
2017-06-27 17:14:13 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
#endif
|
|
|
|
{
|
2022-10-16 13:44:38 +02:00
|
|
|
nerr("ERROR: Unsupported address family: %u", config->d_family);
|
2017-06-27 17:14:13 +02:00
|
|
|
goto errout_with_socket;
|
|
|
|
}
|
2012-01-30 22:29:59 +01:00
|
|
|
|
2017-06-27 17:14:13 +02:00
|
|
|
if (bind(listensd, &addr.generic, addrlen) < 0)
|
2012-01-30 22:29:59 +01:00
|
|
|
{
|
2016-06-11 23:51:27 +02:00
|
|
|
nerr("ERROR: bind failure: %d\n", errno);
|
2012-01-30 22:29:59 +01:00
|
|
|
goto errout_with_socket;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Listen for connections on the bound TCP socket */
|
|
|
|
|
|
|
|
if (listen(listensd, 5) < 0)
|
|
|
|
{
|
2016-06-11 23:51:27 +02:00
|
|
|
nerr("ERROR: listen failure %d\n", errno);
|
2012-01-30 22:29:59 +01:00
|
|
|
goto errout_with_socket;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Begin accepting connections */
|
|
|
|
|
2020-02-03 14:35:37 +01:00
|
|
|
for (; ; )
|
2012-01-30 22:29:59 +01:00
|
|
|
{
|
2022-10-16 13:44:38 +02:00
|
|
|
struct telnet_session_s session;
|
|
|
|
#ifdef CONFIG_NET_SOLINGER
|
|
|
|
struct linger ling;
|
|
|
|
#endif
|
|
|
|
int drvrfd;
|
2019-06-03 17:07:07 +02:00
|
|
|
|
2022-10-16 13:44:38 +02:00
|
|
|
/* Now go silent. */
|
2012-01-30 22:29:59 +01:00
|
|
|
|
2022-10-16 13:44:38 +02:00
|
|
|
close(0);
|
|
|
|
close(1);
|
|
|
|
close(2);
|
|
|
|
|
|
|
|
ninfo("Accepting connections on port %d\n", ntohs(config->d_port));
|
|
|
|
|
|
|
|
addrlen = sizeof(addr);
|
|
|
|
acceptsd = accept(listensd, &addr.generic, &addrlen);
|
2012-01-30 22:29:59 +01:00
|
|
|
if (acceptsd < 0)
|
|
|
|
{
|
2015-01-22 13:33:55 +01:00
|
|
|
/* Just continue if a signal was received */
|
|
|
|
|
2020-02-03 10:11:41 +01:00
|
|
|
if (errno == EINTR)
|
2015-01-22 13:33:55 +01:00
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-02-03 10:11:41 +01:00
|
|
|
nerr("ERROR: accept failed: %d\n", errno);
|
2015-01-22 13:33:55 +01:00
|
|
|
goto errout_with_socket;
|
|
|
|
}
|
2012-01-30 22:29:59 +01:00
|
|
|
}
|
|
|
|
|
2020-02-08 17:16:13 +01:00
|
|
|
#ifdef CONFIG_NET_SOLINGER
|
2020-07-11 19:23:09 +02:00
|
|
|
/* Configure to "linger" until all data is sent when the socket is
|
|
|
|
* closed
|
|
|
|
*/
|
2012-01-30 22:29:59 +01:00
|
|
|
|
|
|
|
ling.l_onoff = 1;
|
|
|
|
ling.l_linger = 30; /* timeout is seconds */
|
2020-07-11 19:23:09 +02:00
|
|
|
if (setsockopt(acceptsd, SOL_SOCKET, SO_LINGER,
|
|
|
|
&ling, sizeof(struct linger)) < 0)
|
2012-01-30 22:29:59 +01:00
|
|
|
{
|
2016-06-20 20:39:46 +02:00
|
|
|
nerr("ERROR: setsockopt failed: %d\n", errno);
|
2012-01-30 22:29:59 +01:00
|
|
|
goto errout_with_acceptsd;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2015-12-07 20:51:37 +01:00
|
|
|
/* Open the Telnet factory */
|
|
|
|
|
2022-10-16 13:44:38 +02:00
|
|
|
drvrfd = open("/dev/telnet", O_RDONLY);
|
|
|
|
if (drvrfd < 0)
|
2015-12-07 20:51:37 +01:00
|
|
|
{
|
2016-06-20 20:39:46 +02:00
|
|
|
nerr("ERROR: open(/dev/telnet) failed: %d\n", errno);
|
2015-12-07 20:51:37 +01:00
|
|
|
goto errout_with_acceptsd;
|
|
|
|
}
|
|
|
|
|
2012-01-30 22:29:59 +01:00
|
|
|
/* Create a character device to "wrap" the accepted socket descriptor */
|
|
|
|
|
2016-06-20 19:48:15 +02:00
|
|
|
ninfo("Creating the telnet driver\n");
|
2015-12-07 20:51:37 +01:00
|
|
|
|
|
|
|
session.ts_sd = acceptsd;
|
|
|
|
session.ts_devpath[0] = '\0';
|
|
|
|
|
2022-10-16 13:44:38 +02:00
|
|
|
if (ioctl(drvrfd, SIOCTELNET,
|
|
|
|
(unsigned long)((uintptr_t)&session)) < 0)
|
2012-01-30 22:29:59 +01:00
|
|
|
{
|
2016-06-20 20:39:46 +02:00
|
|
|
nerr("ERROR: open(/dev/telnet) failed: %d\n", errno);
|
2022-10-16 13:44:38 +02:00
|
|
|
close(drvrfd);
|
2012-01-30 22:29:59 +01:00
|
|
|
goto errout_with_acceptsd;
|
|
|
|
}
|
|
|
|
|
2022-10-16 13:44:38 +02:00
|
|
|
close(drvrfd);
|
|
|
|
|
2012-01-30 22:29:59 +01:00
|
|
|
/* Open the driver */
|
|
|
|
|
2016-06-20 19:48:15 +02:00
|
|
|
ninfo("Opening the telnet driver at %s\n", session.ts_devpath);
|
2015-12-07 20:51:37 +01:00
|
|
|
drvrfd = open(session.ts_devpath, O_RDWR);
|
2012-01-30 22:29:59 +01:00
|
|
|
if (drvrfd < 0)
|
|
|
|
{
|
2016-06-20 20:39:46 +02:00
|
|
|
nerr("ERROR: Failed to open %s: %d\n", session.ts_devpath, errno);
|
2020-02-03 10:11:41 +01:00
|
|
|
goto errout_with_socket;
|
2012-01-30 22:29:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Use this driver as stdin, stdout, and stderror */
|
|
|
|
|
2020-01-02 13:09:50 +01:00
|
|
|
dup2(drvrfd, 0);
|
|
|
|
dup2(drvrfd, 1);
|
|
|
|
dup2(drvrfd, 2);
|
2012-01-30 22:29:59 +01:00
|
|
|
|
|
|
|
/* And we can close our original driver fd */
|
|
|
|
|
|
|
|
if (drvrfd > 2)
|
|
|
|
{
|
|
|
|
close(drvrfd);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Create a task to handle the connection. The created task
|
|
|
|
* will inherit the new stdin, stdout, and stderr.
|
|
|
|
*/
|
|
|
|
|
2016-06-20 19:48:15 +02:00
|
|
|
ninfo("Starting the telnet session\n");
|
2022-10-16 13:44:38 +02:00
|
|
|
|
|
|
|
#ifndef CONFIG_BUILD_KERNEL
|
|
|
|
if (config->t_entry)
|
2012-01-30 22:29:59 +01:00
|
|
|
{
|
2022-10-16 13:44:38 +02:00
|
|
|
pid_t pid = task_create("Telnet session",
|
|
|
|
config->t_priority, config->t_stacksize,
|
|
|
|
config->t_entry, NULL);
|
|
|
|
if (pid >= 0)
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
2012-01-30 22:29:59 +01:00
|
|
|
}
|
2022-10-16 13:44:38 +02:00
|
|
|
#endif
|
2012-01-30 22:29:59 +01:00
|
|
|
|
2022-10-16 13:44:38 +02:00
|
|
|
#ifdef CONFIG_LIBC_EXECFUNCS
|
|
|
|
if (config->t_path)
|
|
|
|
{
|
|
|
|
struct sched_param param;
|
|
|
|
posix_spawnattr_t attr;
|
|
|
|
pid_t pid;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
sched_getparam(0, ¶m);
|
|
|
|
param.sched_priority = config->t_priority;
|
|
|
|
|
|
|
|
posix_spawnattr_init(&attr);
|
|
|
|
posix_spawnattr_setflags(&attr, POSIX_SPAWN_SETSCHEDPARAM);
|
|
|
|
posix_spawnattr_setschedparam(&attr, ¶m);
|
|
|
|
posix_spawnattr_setstacksize(&attr, config->t_stacksize);
|
|
|
|
|
|
|
|
ret = posix_spawnp(&pid, config->t_path, NULL,
|
|
|
|
&attr, config->t_argv, NULL);
|
|
|
|
if (ret > 0)
|
|
|
|
{
|
|
|
|
nerr("ERROR: Failed start the telnet session: %d\n", ret);
|
|
|
|
errno = ret;
|
|
|
|
goto errout_with_socket;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
2012-01-30 22:29:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
errout_with_acceptsd:
|
|
|
|
close(acceptsd);
|
|
|
|
|
|
|
|
errout_with_socket:
|
|
|
|
close(listensd);
|
2022-10-16 13:44:38 +02:00
|
|
|
errout:
|
|
|
|
return errno;
|
2012-01-30 22:29:59 +01:00
|
|
|
}
|