diff options
Diffstat (limited to 'c')
| -rw-r--r-- | c/necho.c | 33 | ||||
| -rw-r--r-- | c/netlib.h | 39 | ||||
| -rw-r--r-- | c/wget.c | 18 |
3 files changed, 58 insertions, 32 deletions
@@ -1,38 +1,7 @@ -#include "gbos.h" +#include "netlib.h" /* necho: send a SLIP-framed packet over the link port and print the reply. Talks to a host gateway (tools/gateway.py) that echoes frames back. */ -#define SLIP_END 0xC0 -#define SLIP_ESC 0xDB -#define SLIP_ESC_END 0xDC -#define SLIP_ESC_ESC 0xDD - -static void slip_send(const char *buf, unsigned char len) { - unsigned char i, c; - ssend(SLIP_END); - for (i = 0; i < len; i++) { - c = (unsigned char)buf[i]; - if (c == SLIP_END) { ssend(SLIP_ESC); ssend(SLIP_ESC_END); } - else if (c == SLIP_ESC) { ssend(SLIP_ESC); ssend(SLIP_ESC_ESC); } - else ssend(c); - } - ssend(SLIP_END); -} - -static unsigned char slip_recv(char *buf, unsigned char max) { - unsigned char len = 0, c; - for (;;) { - c = srecv(); - if (c == SLIP_END) { if (len) return len; continue; } - if (c == SLIP_ESC) { - c = srecv(); - if (c == SLIP_ESC_END) c = SLIP_END; - else if (c == SLIP_ESC_ESC) c = SLIP_ESC; - } - if (len < max) buf[len++] = (char)c; - } -} - void main(void) { char buf[64]; unsigned char len; diff --git a/c/netlib.h b/c/netlib.h new file mode 100644 index 0000000..ec98eac --- /dev/null +++ b/c/netlib.h @@ -0,0 +1,39 @@ +#ifndef NETLIB_H +#define NETLIB_H +/* SLIP (RFC 1055) framing over the link port. Header-only so each program gets + its own copy (the C build links one translation unit per program). */ +#include "gbos.h" + +#define SLIP_END 0xC0 +#define SLIP_ESC 0xDB +#define SLIP_ESC_END 0xDC +#define SLIP_ESC_ESC 0xDD + +/* send a framed packet (len up to 255) */ +static void slip_send(const char *buf, unsigned char len) { + unsigned char i, c; + ssend(SLIP_END); + for (i = 0; i < len; i++) { + c = (unsigned char)buf[i]; + if (c == SLIP_END) { ssend(SLIP_ESC); ssend(SLIP_ESC_END); } + else if (c == SLIP_ESC) { ssend(SLIP_ESC); ssend(SLIP_ESC_ESC); } + else ssend(c); + } + ssend(SLIP_END); +} + +/* receive one framed packet into buf (max), returns its length */ +static unsigned char slip_recv(char *buf, unsigned char max) { + unsigned char len = 0, c; + for (;;) { + c = srecv(); + if (c == SLIP_END) { if (len) return len; continue; } + if (c == SLIP_ESC) { + c = srecv(); + if (c == SLIP_ESC_END) c = SLIP_END; + else if (c == SLIP_ESC_ESC) c = SLIP_ESC; + } + if (len < max) buf[len++] = (char)c; + } +} +#endif diff --git a/c/wget.c b/c/wget.c new file mode 100644 index 0000000..013b326 --- /dev/null +++ b/c/wget.c @@ -0,0 +1,18 @@ +#include "netlib.h" +/* wget URL : send the URL to the host gateway over SLIP, print the reply body. + The gateway does the real DNS/TCP/HTTP and streams the body back as frames: + each reply frame is 'D'<chunk>, ending with a single 'E' frame. */ +void main(void) { + char *argv[4]; + unsigned char argc = argv_parse(argv, 4); + char buf[220]; + unsigned char len, i; + if (argc < 1) { puts("usage: wget URL"); nl(); return; } + slip_send(argv[0], strlen(argv[0])); + for (;;) { + len = slip_recv(buf, 219); + if (len >= 1 && buf[0] == 'E') break; /* end of stream */ + for (i = 1; i < len; i++) putc(buf[i]); /* skip the 'D' type byte */ + } + nl(); +} |
