Skip to content

Commit

Permalink
netd: updated applications
Browse files Browse the repository at this point in the history
- tweak ping for the API correction
- rework the telnet client a bit
	- buffer output for speed
	- fetch multiple input buffers
	- default to transparent mode
  • Loading branch information
Alan Cox committed Aug 4, 2021
1 parent 59bb623 commit 9db73c2
Show file tree
Hide file tree
Showing 2 changed files with 124 additions and 60 deletions.
7 changes: 2 additions & 5 deletions Applications/netd/ping.c
Original file line number Diff line number Diff line change
Expand Up @@ -115,14 +115,11 @@ my_open( int argc, char *argv[]){

fd = socket(AF_INET, SOCK_RAW, 1);
if (fd < 0) {
perror("af_inet sock_stream 0");
perror("socket");
exit(1);
}

/* fuzix raw sockets (for now) repurposes the connect()
address struct's port no to pass it's protocol number
*/
addr.sin_port = 1;
addr.sin_port = 0;
addr.sin_family = AF_INET;
if (connect(fd, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
perror("connect");
Expand Down
177 changes: 122 additions & 55 deletions Applications/netd/telnet.c
Original file line number Diff line number Diff line change
@@ -1,16 +1,25 @@
/* dwtelnet - Drivewire - raw Telnet
/*
* Sort of a telnet client, although actually it's more of a MUD client
* than that.
*
* Rewritten from dwtelnet - Drivewire - raw Telnet
*/


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <signal.h>
#include <ctype.h>
#include <fcntl.h>
#include <termios.h>
#include <stdio.h>
#include <errno.h>

#include <sys/socket.h>
#include <netinet/in.h>
#include <signal.h>
#include <arpa/inet.h>

#include "netdb.h"


Expand All @@ -21,7 +30,6 @@
char ibuf[256];
int opos = 0;

int flags;
struct termios prior, new;
struct termios lprior, lnew;
int fddw;
Expand All @@ -41,62 +49,87 @@ int crnl = -1;
int fc = 7;
int bc = 0;
int inten = 0;
int ansivert = 0;


struct sockaddr_in addr;
struct sockaddr_in laddr;

void puterr(char *str)
int tonum(char c)
{
write(2, str, strlen(str));
return c - '0';
}

void putstr(char *str)

/*
* Batch output so that we generate less system calls
*/
static char outbuf[256];
static char *outptr = outbuf;

void outflush(void)
{
write(1, str, strlen(str));
if (outptr != outbuf) {
unsigned int len = outptr - outbuf;
if (write(1, outbuf, len) != len)
perror("write");
outptr = outbuf;
}
}

int isnum(char c)
void qbyte(char c)
{
if (c >= '0' && c <= '9')
return 1;
return 0;
*outptr++ = c;
if (outptr == &outbuf[256])
outflush();
}

int tonum(char c)
void qstr(const char *p)
{
return c - '0';
while(*p)
qbyte(*p++);
}

void qhexdigit(char c)
{
qbyte("0123456789ABCDEF"[c & 0x0F]);
}

void qhex(char c)
{
qbyte('<');
qhexdigit(c >> 4);
qhexdigit(c);
qbyte('>');
}

void out52(char c)
{
write(1, "\x1b", 1);
write(1, &c, 1);
qbyte('x1b');
qbyte(c);
}

void out52n(char c, char n)
{
out52(c);
write(1, &n, 1);
qbyte(n);
}

void out52nn(char c, char n, char m)
{
out52(c);
write(1, &n, 1);
write(1, &m, 1);
qbyte(n);
qbyte(m);
}


char cconv(char c)
{
return colors[c];
}


/* check for matching fore/background colors w/ intensity bit
we can't do intensity - so text disappears */
void ckint()
void ckint(void)
{
if ((fc == bc) && inten) {
fc = fc + 1 & 0x7;
Expand All @@ -118,7 +151,7 @@ void charout(uint8_t c)
return;
}
if (c != ESC)
write(1, &c, 1);
qbyte(c);
else {
mode = 1;
}
Expand All @@ -132,7 +165,7 @@ void charout(uint8_t c)
return;
/* Multi-byte detected */
case 2:
if (isnum(c)) {
if (isdigit(c)) {
para[no] = para[no] * 10 + tonum(c);
return;
}
Expand All @@ -146,7 +179,7 @@ void charout(uint8_t c)
case 3:
switch (c) {
case 255:
write(1, &c, 1);
qbyte(c);
mode = 0;
return;

Expand All @@ -158,17 +191,19 @@ void charout(uint8_t c)
return;
case 253:
case 254:
if (hex)
printf("<IAC><%x>", c);
qstr("<IAC>");
qhex(c);
mode = 4;
return;
}
mode = 0;
return;
/* send Telnet's WILLNOTs to server */
case 4:
if (hex)
printf("<%x>\n", c);
if (hex) {
qhex(c);
qbyte('\n');
}
write(fddw, "\xff\xfc", 2);
write(fddw, &c, 1);
mode = 0;
Expand All @@ -188,8 +223,11 @@ void charout(uint8_t c)
return;
/* received a WONT */
case 6:
if (hex)
printf("opt<%x>\n", c);
if (hex) {
qstr("opt");
qhex(c);
qbyte('\n');
}
if (c == 1)
lecho = -1;
else {
Expand Down Expand Up @@ -282,18 +320,25 @@ void charout(uint8_t c)
}

/* print string to console under ANSI/TELNET */
/* TODO: a mode where we just pass through */
int mywrite(uint8_t * ptr, int len)
{
int i = len;
while (i--)
charout(*ptr++);
if (ansivert) {
while (i--)
charout(*ptr++);
} else {
while (i--)
qbyte(*ptr++);
}
return len;
}

void quit(int sig)
{
if (sig == 0)
qflush();
tcsetattr(0, TCSANOW, &lprior);
fcntl(0, F_SETFL, flags);
close(fddw);
exit(0);
}
Expand All @@ -303,15 +348,14 @@ void addstr(char *s)
{
for (; ibuf[opos++] = *s; s++)
if (lecho)
write(1, s, 1);
qbyte(*s);
opos--;
}

void printd(char *s)
{
while (*s)
write(1, s++, 1);
write(1, "\n", 1);
qstr(s);
qbyte('\n');
}

void printhelp(void)
Expand All @@ -334,6 +378,9 @@ int myread(void)
static int cmode = 0;
int l;

/* Make sure anything received is visible */
outflush();

switch (mode) {
case 0: // waiting for input buffer to fill middle
l = read(0, kbuf, 256);
Expand Down Expand Up @@ -422,7 +469,7 @@ int myread(void)
continue;
}
if (lecho)
write(1, &c, 1);
qbyte(c);
if ((c == 8) && del) {
ibuf[opos++] = 127;
continue;
Expand Down Expand Up @@ -465,7 +512,7 @@ int my_open(int argc, char *argv[])

fddw = socket(AF_INET, SOCK_STREAM, 0);
if (fddw < 0) {
perror("af_inet sock_stream 0");
perror("socket");
exit(1);
}

Expand All @@ -480,6 +527,12 @@ int main(int argc, char *argv[])
{
int len;

if (argv[1] && strcmp(argv[1], "-a") == 0) {
ansivert = 1;
argv++;
argc--;
}

if (argc < 2) {
write(2, "telnet: server [port]\n", 22);
exit(1);
Expand All @@ -501,10 +554,13 @@ int main(int argc, char *argv[])
lnew.c_cc[VTIME] = 1;
tcsetattr(0, TCSANOW, &lnew);

printf("Use Cntl-A ? for help.\n");
qstr("Use Cntl-A ? for help.\n");

while (1) {
char *pos = ibuf;
uint8_t ct = 0;

/* Keyboard to socket */
len = myread();
while (len > 0) {
int ret = write(fddw, pos, len);
Expand All @@ -517,25 +573,36 @@ int main(int argc, char *argv[])
len = len - ret;
pos += ret;
}
len = read(fddw, ibuf, 127);
/* Socket to console. We do up to 20 loops through this
at a go so that big bursts of data feel fast. We don't
go forever though as the poor user might be trying to
stop the output */
while(ct++ < 20 && (len = read(fddw, ibuf, 128)) > 0)
{
pos = ibuf;
while (len > 0) {
int ret = mywrite(pos, len);
if (ret < 0) {
if (errno == EWOULDBLOCK) {
ret = 0;
} else
quit(0);
}
len = len - ret;
pos += ret;
}
}
/* EOF: remote closed */
if (!len)
quit(0);
/* Error. EAGAIN is ok - it means we've run out of data,
anything else is bad */
if (len < 0) {
if (errno != EAGAIN) {
qflush();
perror("closed");
quit(0);
}
}
pos = ibuf;
while (len > 0) {
int ret = mywrite(pos, len);
if (ret < 0) {
if (errno == EWOULDBLOCK) {
ret = 0;
} else
quit(0);
}
len = len - ret;
pos += ret;
}
}
}

0 comments on commit 9db73c2

Please sign in to comment.