From 4fdba81711d9e85f5d8807354c039efefe1c8518 Mon Sep 17 00:00:00 2001 From: Brad Nelson Date: Sat, 20 Feb 2021 23:52:39 -0800 Subject: [PATCH] Adding telnetd for posix --- ueforth/posix/posix.fs | 7 ++++++- ueforth/posix/sockets.fs | 15 +++++++++++++++ ueforth/posix/telnetd.fs | 41 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 62 insertions(+), 1 deletion(-) create mode 100644 ueforth/posix/sockets.fs create mode 100644 ueforth/posix/telnetd.fs diff --git a/ueforth/posix/posix.fs b/ueforth/posix/posix.fs index 79d1ddb..2e5befd 100644 --- a/ueforth/posix/posix.fs +++ b/ueforth/posix/posix.fs @@ -96,16 +96,21 @@ decimal ( errno.h ) 11 constant EAGAIN +32 constant EPIPE ( Signal Handling ) 0 constant SIG_DFL 1 constant SIG_IGN + ( Signals ) 1 constant SIGHUP 2 constant SIGINT 9 constant SIGKILL -10 constant SIGPIPE 7 constant SIGBUS +13 constant SIGPIPE + +( Ignore SIGPIPE ) +SIGPIPE SIG_IGN signal drop ( Modes ) octal 777 constant 0777 decimal diff --git a/ueforth/posix/sockets.fs b/ueforth/posix/sockets.fs new file mode 100644 index 0000000..d775e8e --- /dev/null +++ b/ueforth/posix/sockets.fs @@ -0,0 +1,15 @@ +( Sockets ) +posix definitions + +z" socket" 3 sysfunc socket +z" bind" 3 sysfunc bind +z" listen" 2 sysfunc listen +z" connect" 3 sysfunc connect +z" accept" 3 sysfunc accept +z" poll" 3 sysfunc poll + +1 constant SOCK_STREAM +2 constant AF_INET +16 constant sizeof(sockaddr_in) + +forth definitions diff --git a/ueforth/posix/telnetd.fs b/ueforth/posix/telnetd.fs new file mode 100644 index 0000000..2cdbe39 --- /dev/null +++ b/ueforth/posix/telnetd.fs @@ -0,0 +1,41 @@ +( WebServer ) +include posix/sockets.fs + +vocabulary telnetd telnetd definitions also posix + +8080 constant port +-1 value sockfd -1 value clientfd +: bs, ( n -- ) dup 256 / c, c, ; +: s, ( n -- ) dup c, 256 / c, ; +: l, ( n -- ) dup s, 65536 / s, ; +create telnet-port AF_INET s, port bs, 0 l, 0 , +create client sizeof(sockaddr_in) allot variable client-len + +defer broker + +: telnet-type ( a n -- ) clientfd -rot write 0< if 2drop broker then ; +: telnet-key ( -- n ) 0 >r clientfd rp@ 1 read 0< if rdrop broker then r> ; + +: connection ( n -- ) + dup 0< if drop exit then to clientfd + ['] telnet-key is key + ['] telnet-type is type quit ; + +: broker-connection + rp0 rp! sp0 sp! + begin + ['] stdin-key is key ['] stdout-write is type + ." Listening on port " port . cr + sockfd client client-len accept + ." Connected: " dup . cr connection + again ; +' broker-connection is broker + +: server + AF_INET SOCK_STREAM 0 socket to sockfd + sockfd telnet-port sizeof(sockaddr_in) bind throw + sockfd 10 listen throw broker ; + +only forth definitions + +telnetd server