From 804de7cec2dadf8f4dd364cc162a2d3530245c86 Mon Sep 17 00:00:00 2001 From: Travis Geiselbrecht Date: Fri, 28 May 2021 17:17:01 -0700 Subject: [PATCH] WIP telnetd --- app/telnetd/rules.mk | 12 ++++ app/telnetd/telnetd.cpp | 72 ++++++++++++++++++++ project/qemu-virt-riscv64-supervisor-test.mk | 2 +- project/virtual/inetapps.mk | 8 +++ project/virtual/minip.mk | 4 +- 5 files changed, 94 insertions(+), 4 deletions(-) create mode 100644 app/telnetd/rules.mk create mode 100644 app/telnetd/telnetd.cpp create mode 100644 project/virtual/inetapps.mk diff --git a/app/telnetd/rules.mk b/app/telnetd/rules.mk new file mode 100644 index 00000000..2252ec7f --- /dev/null +++ b/app/telnetd/rules.mk @@ -0,0 +1,12 @@ +LOCAL_DIR := $(GET_LOCAL_DIR) + +MODULE := $(LOCAL_DIR) + +MODULE_SRCS += $(LOCAL_DIR)/telnetd.cpp + +MODULE_DEPS := \ + lib/cksum \ + lib/libcpp \ + lib/minip + +include make/module.mk diff --git a/app/telnetd/telnetd.cpp b/app/telnetd/telnetd.cpp new file mode 100644 index 00000000..1d418146 --- /dev/null +++ b/app/telnetd/telnetd.cpp @@ -0,0 +1,72 @@ +/* + * Copyright (c) 2014 Travis Geiselbrecht + * + * Use of this source code is governed by a MIT-style + * license that can be found in the LICENSE file or at + * https://opensource.org/licenses/MIT + */ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define LOCAL_TRACE 1 + +static int telnet_worker(void *arg) { + tcp_socket_t *s = static_cast(arg); + + char buf[128]; + for (;;) { + ssize_t err = tcp_read(s, buf, sizeof(buf)); + if (err < 0) { + printf("TELENT: error from read, exiting\n"); + return err; + } + + hexdump8(buf, err); + } + + return 0; +} + +static void telnetd_entry(const struct app_descriptor *app, void *args) { + printf("TELNET: waiting for network configuration\n"); + minip_wait_for_configured(INFINITE_TIME); + printf("TELNET: starting telnet server\n"); + + // starting telnet stack + tcp_socket_t *listen_socket; + status_t err = tcp_open_listen(&listen_socket, 23); + if (err < 0) { + printf("tcp_open_listen returns %d\n", err); + } + + for (;;) { + tcp_socket_t *accept_socket; + + err = tcp_accept(listen_socket, &accept_socket); + LTRACEF("tcp_accept returns returns %d, handle %p\n", err, accept_socket); + if (err < 0) { + TRACEF("error accepting socket, retrying\n"); + continue; + } + + printf("TELNET: starting worker\n"); + thread_detach_and_resume(thread_create("chargen_worker", &telnet_worker, accept_socket, DEFAULT_PRIORITY, DEFAULT_STACK_SIZE)); + } +} + +APP_START(inetsrv) +.init = nullptr, +.entry = telnetd_entry, +.flags = 0, +.stack_size = 0, +APP_END diff --git a/project/qemu-virt-riscv64-supervisor-test.mk b/project/qemu-virt-riscv64-supervisor-test.mk index a2e9a1a7..b16ae7cf 100644 --- a/project/qemu-virt-riscv64-supervisor-test.mk +++ b/project/qemu-virt-riscv64-supervisor-test.mk @@ -7,6 +7,6 @@ RISCV_MODE := supervisor include project/virtual/test.mk include project/virtual/fs.mk -include project/virtual/minip.mk +include project/virtual/inetapps.mk include project/target/qemu-virt-riscv.mk diff --git a/project/virtual/inetapps.mk b/project/virtual/inetapps.mk new file mode 100644 index 00000000..6eb42b8a --- /dev/null +++ b/project/virtual/inetapps.mk @@ -0,0 +1,8 @@ +# some internet apps that depend on minip + +MODULES += \ + app/inetsrv \ + app/irc \ + app/telnetd \ + +include project/virtual/minip.mk diff --git a/project/virtual/minip.mk b/project/virtual/minip.mk index 8654fccc..46b8f072 100644 --- a/project/virtual/minip.mk +++ b/project/virtual/minip.mk @@ -1,6 +1,4 @@ # modules related to the minip stack MODULES += \ - lib/minip \ - app/inetsrv - + lib/minip