Added telnetd to raspberry pico port
All checks were successful
/ unit_test (push) Successful in 44s

This commit is contained in:
Daniele Lacamera 2024-11-27 21:14:02 +01:00
parent 9997f3e295
commit 44deb02802
8 changed files with 147 additions and 31 deletions

View file

@ -4,10 +4,10 @@
#define ETHERNET
#define LINK_MTU 1536
#define MAX_TCPSOCKETS 4
#define MAX_UDPSOCKETS 2
#define RXBUF_SIZE LINK_MTU * 8
#define TXBUF_SIZE LINK_MTU * 2
#define MAX_TCPSOCKETS 20
#define MAX_UDPSOCKETS 1
#define RXBUF_SIZE LINK_MTU * 4
#define TXBUF_SIZE LINK_MTU * 16
#define MAX_NEIGHBORS 16

View file

@ -98,7 +98,6 @@ void ipstack_ipconfig_set(struct ipstack *s, ip4 ip, ip4 mask, ip4 gw);
void ipstack_ipconfig_get(struct ipstack *s, ip4 *ip, ip4 *mask, ip4 *gw);
struct ll *ipstack_getdev(struct ipstack *s);
ip4 atoip4(const char *ip);
/* Callback flags */
#define CB_EVENT_READABLE 0x01 /* Accepted connection or data available */
@ -110,6 +109,43 @@ void ipstack_register_callback(struct ipstack *s, int sock_fd, void (*cb)(int so
/* External requirements */
uint32_t ipstack_getrandom(void);
/* Inline utility functions */
static inline uint32_t atou(const char *s)
{
uint32_t ret = 0;
while (*s >= '0' && *s <= '9') {
ret = ret * 10 + (*s - '0');
s++;
}
return ret;
}
static inline ip4 atoip4(const char *ip)
{
ip4 ret = 0;
int i = 0;
int j = 0;
for (i = 0; i < 4; i++) {
ret |= (atou(ip + j) << (24 - i * 8));
while (ip[j] != '.' && ip[j] != '\0') j++;
if (ip[j] == '\0') break;
j++;
}
return ret;
}
static inline void iptoa(ip4 ip, char *buf)
{
int i, j = 0;
buf[0] = 0;
for (i = 0; i < 4; i++) {
uint8_t x = (ip >> (24 - i * 8)) & 0xFF;
if (x > 99) buf[j++] = x / 100 + '0';
if (x > 9) buf[j++] = (x / 10) % 10 + '0';
buf[j++] = x % 10 + '0';
if (i < 3) buf[j++] = '.';
}
buf[j] = 0;
}
#endif

View file

@ -2106,26 +2106,3 @@ void ipstack_ipconfig_get(struct ipstack *s, ip4 *ip, ip4 *mask, ip4 *gw)
*gw = s->ipconf.gw;
}
static uint32_t atou(const char *s)
{
uint32_t ret = 0;
while (*s >= '0' && *s <= '9') {
ret = ret * 10 + (*s - '0');
s++;
}
return ret;
}
ip4 atoip4(const char *ip)
{
ip4 ret = 0;
int i = 0;
int j = 0;
for (i = 0; i < 4; i++) {
ret |= (atou(ip + j) << (24 - i * 8));
while (ip[j] != '.' && ip[j] != '\0') j++;
if (ip[j] == '\0') break;
j++;
}
return ret;
}

View file

@ -17,7 +17,7 @@
static __thread int in_the_stack = 1;
static struct ipstack *IPSTACK = NULL;
pthread_mutex_t ipstack_mutex = PTHREAD_MUTEX_INITIALIZER;
pthread_mutex_t ipstack_mutex;
/* host_ functions are the original functions from the libc */
static int (*host_socket ) (int domain, int type, int protocol) = NULL;
@ -558,6 +558,7 @@ void __attribute__((constructor)) init_femto_posix() {
swap_socketcall(select, "select");
swap_socketcall(fcntl, "fcntl");
pthread_mutex_init(&ipstack_mutex, NULL);
ipstack_init_static(&IPSTACK);
tapdev = ipstack_getdev(IPSTACK);
if (tap_init(tapdev, "femt0", linux_ip.s_addr) < 0) {

View file

@ -18,6 +18,7 @@ add_executable(${PROJECT})
target_sources(${PROJECT} PUBLIC
${CMAKE_CURRENT_LIST_DIR}/src/main.c
${CMAKE_CURRENT_LIST_DIR}/src/rand.c
${CMAKE_CURRENT_LIST_DIR}/src/motd.c
${CMAKE_CURRENT_LIST_DIR}/src/usb_descriptors.c
${CMAKE_CURRENT_LIST_DIR}/../../femtotcp.c
)

View file

@ -1964,6 +1964,30 @@ src/main.c.s:
$(MAKE) $(MAKESILENT) -f CMakeFiles/raspberry-pico-usb-server.dir/build.make CMakeFiles/raspberry-pico-usb-server.dir/src/main.c.s
.PHONY : src/main.c.s
src/motd.obj: src/motd.c.obj
.PHONY : src/motd.obj
# target to build an object file
src/motd.c.obj:
$(MAKE) $(MAKESILENT) -f CMakeFiles/raspberry-pico-usb-server.dir/build.make CMakeFiles/raspberry-pico-usb-server.dir/src/motd.c.obj
.PHONY : src/motd.c.obj
src/motd.i: src/motd.c.i
.PHONY : src/motd.i
# target to preprocess a source file
src/motd.c.i:
$(MAKE) $(MAKESILENT) -f CMakeFiles/raspberry-pico-usb-server.dir/build.make CMakeFiles/raspberry-pico-usb-server.dir/src/motd.c.i
.PHONY : src/motd.c.i
src/motd.s: src/motd.c.s
.PHONY : src/motd.s
# target to generate assembly for a file
src/motd.c.s:
$(MAKE) $(MAKESILENT) -f CMakeFiles/raspberry-pico-usb-server.dir/build.make CMakeFiles/raspberry-pico-usb-server.dir/src/motd.c.s
.PHONY : src/motd.c.s
src/rand.obj: src/rand.c.obj
.PHONY : src/rand.obj
@ -2246,6 +2270,9 @@ help:
@echo "... src/main.obj"
@echo "... src/main.i"
@echo "... src/main.s"
@echo "... src/motd.obj"
@echo "... src/motd.i"
@echo "... src/motd.s"
@echo "... src/rand.obj"
@echo "... src/rand.i"
@echo "... src/rand.s"

View file

@ -44,6 +44,7 @@
#include "config.h"
#include "femtotcp.h"
extern char MOTD[];
/* Our globals */
static struct ipstack *IPStack = NULL;
@ -170,6 +171,64 @@ void tud_network_init_cb(void)
{
}
/* Telnet server (telnetd) initialization */
static int tel_s = -1;
static int tel_c = -1;
static void telnet_cb(int fd, uint16_t event, void *arg)
{
struct ipstack_sockaddr_in addr;
uint32_t socklen = sizeof(addr);
(void)arg;
if ((fd == tel_s) && (event & CB_EVENT_READABLE) && (tel_c == -1)) {
char ipaddr[16];
char welcome_msg[32];
tel_c = ft_accept(IPStack, tel_s, (struct ipstack_sockaddr*)&addr, &socklen);
if (tel_c > 0) {
iptoa(ee32(addr.sin_addr.s_addr), ipaddr);
snprintf(welcome_msg, sizeof(welcome_msg), "Welcome %s!\n", ipaddr);
ft_write(IPStack, tel_c, MOTD, strlen(MOTD));
ft_write(IPStack, tel_c, welcome_msg, strlen(welcome_msg));
}
}
#if 0
else if ((fd == tel_c) && (event & CB_EVENT_READABLE )) {
int ret;
ret = ft_recv((struct ipstack *)arg, tel_c, buf, sizeof(buf), 0);
if (ret != -11) {
if (ret < 0) {
printf("Recv error: %d\n", ret);
ft_close((struct ipstack *)arg, tel_c);
} else if (ret == 0) {
printf("Client side closed the connection.\n");
ft_close((struct ipstack *)arg, tel_c);
printf("Server: Exiting.\n");
exit_ok = 1;
} else if (ret > 0) {
printf("recv: %d, echoing back\n", ret);
tot_recv += ret;
}
}
}
#endif
}
static void telnetd_init(void)
{
struct ipstack_sockaddr_in addr;
if (tel_s < 0)
tel_s = ft_socket(IPStack, AF_INET, IPSTACK_SOCK_STREAM, 0);
ipstack_register_callback(IPStack, tel_s, telnet_cb, NULL);
addr.sin_family = AF_INET;
addr.sin_port = ee16(23);
addr.sin_addr.s_addr = 0;
ft_bind(IPStack, tel_s, (struct ipstack_sockaddr *)&addr, sizeof(addr));
ft_listen(IPStack, tel_s, 1);
}
int main(void)
{
struct ll *tusb_netdev;
@ -197,6 +256,8 @@ int main(void)
ipstack_ipconfig_set(IPStack, atoip4("192.168.7.2"),
atoip4("255.255.255.0"), atoip4("192.168.7.1"));
telnetd_init();
board_led_off();
while (1) {
tud_task();

View file

@ -0,0 +1,13 @@
const char MOTD[] =
" \n"
" /\\_/\\ \n"
" ( o.o )\n"
" > ^ < ⭐️ ⭐️ ⭐️ \n"
" \n"
" 🟪🟪🟪🟪 ⭐️ ⭐️ 🟪 🟪🟪🟪🟪🟪 🟪🟪🟪 🟪🟪🟪 \n"
"⭐️🟪 🟪🟪 🟪🟪🟪🟪🟪🟪 🟪🟪🟪🟪 🟪🟪 🟪 🟪 🟪 🟪 \n"
" 🟪🟪🟪 🟪🟪🟪🟪 🟪 🟪 🟪 🟪 🟪 🟪 🟪 ⭐️ 🟪 🟪🟪🟪 \n"
" 🟪 🟪 🟪 🟪 🟪 🟪 🟪 🟪 🟪 🟪 🟪 ⭐️ \n"
" 🟪 🟪🟪🟪 🟪 🟪 🟪 🟪🟪 🟪🟪 🟪 🟪🟪🟪 🟪 \n"
" ⭐️ by Danielinux \n"
"Best viewed in (80 x 30)\n";