From ba2d7ae1d7b319645b10aaa31a7f664f9f80c25c Mon Sep 17 00:00:00 2001 From: user Date: Sat, 18 Jul 2026 00:38:38 +0200 Subject: refactor: c/ -> usr/, compiled program blobs out of the source tree Userland sources live in usr/ (fits the Unix theme better than 'c'). SDCC output .bin blobs land in build/usr/ with the other build artifacts instead of littering the source dir; programs.asm INCBINs them from there. Byte-identical ROM. --- usr/ping.c | 61 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 usr/ping.c (limited to 'usr/ping.c') diff --git a/usr/ping.c b/usr/ping.c new file mode 100644 index 0000000..8671318 --- /dev/null +++ b/usr/ping.c @@ -0,0 +1,61 @@ +#include "resolve.h" +/* ping HOST|IP - originate ICMP echo requests via the kernel socket layer. + Resolves a hostname via DNS first (resolve.h); default target 10.0.0.1. No + SLIP/IP/checksums here - the kernel owns all of that. */ + +/* rbuf is first + large so namebuf/dst land past the arg string at 0xA000 */ +static unsigned char rbuf[96]; +static unsigned char namebuf[64]; +static unsigned char dst[4]; +static unsigned char payload[4]; + +static void put_ip(unsigned char *ip) { + unsigned char i; + for (i = 0; i < 4; i++) { putu(ip[i]); if (i < 3) putc('.'); } +} + +/* wait up to ~2s for an echo reply without blocking in the kernel: a lost + reply (some hosts rate-limit ICMP) must cost one "timeout" line, not + minutes wedged inside net_recv's pump-counted wait. */ +static unsigned char wait_reply(unsigned char s) { + unsigned char t, r; + for (t = 0; t < 40; t++) { + r = net_recv_nb(s, rbuf, (unsigned char)sizeof(rbuf)); + if (r != 0xFE) return r; /* reply (or error) */ + msleep(50); + } + return 0xFF; /* ~2s with no reply */ +} + +void main(void) { + char *arg; + unsigned char s, seq, got = 0, i; + arg = getargs(); + if (!arg || !*arg) { dst[0] = 10; dst[1] = 0; dst[2] = 0; dst[3] = 1; } + else { + for (i = 0; arg[i] && i < 63; i++) namebuf[i] = (unsigned char)arg[i]; + namebuf[i] = 0; + if (!resolve((char *)namebuf, dst)) { + puts("ping: cannot resolve "); puts((char *)namebuf); nl(); sexit(1); + } + } + + payload[0] = 'g'; payload[1] = 'b'; payload[2] = 'o'; payload[3] = 's'; + s = net_socket(SOCK_ICMP); + if (s == 0xFF) { puts("ping: no socket"); nl(); sexit(1); } + net_connect(s, dst, 0); + + puts("PING "); put_ip(dst); nl(); + for (seq = 1; seq <= 4; seq++) { + net_send(s, payload, 4); + if (wait_reply(s) != 0xFF) { + got++; + puts(" reply from "); put_ip(dst); puts(" seq="); putu(seq); nl(); + } else { + puts(" seq="); putu(seq); puts(" timeout"); nl(); + } + if (seq < 4) msleep(800); /* pace like real ping */ + } + puts("-- "); putu(got); puts("/4 received"); nl(); + net_close(s); +} -- cgit v1.3.1-sl0p