From a565bcd942efeb617f5424ae5a881211a7bd0216 Mon Sep 17 00:00:00 2001 From: user Date: Fri, 17 Jul 2026 13:11:53 +0200 Subject: net: wget-by-hostname + shared DNS resolver (a Game Boy fetches example.com) resolve.h: shared helper that turns a dotted-quad or a hostname into an IPv4 address (DNS A query over a UDP socket to 1.1.1.1). nslookup now uses it and slims down; wget uses it so you can name a host directly. wget HOST|IP now: resolve -> TCP connect -> "GET / HTTP/1.0" with the real Host header -> stream the body to the LCD/console until EOF. One command does DNS + TCP + HTTP, all on the kernel socket layer. Verified over tunbridge's NAT against the real internet: /# wget example.com connecting 172.66.147.243 HTTP/1.1 200 OK Server: cloudflare Example Domain... [eof] The full HTML page arrives across many TCP segments and is reassembled and printed - real DNS, real TCP, real HTTP, fetched by a Game Boy by name. (tunbridge.py already sets up + tears down the NAT, so the demo is a one-liner: sudo python3 tools/tunbridge.py "wget example.com") --- c/wget.c | 41 +++++++++++++++++++---------------------- 1 file changed, 19 insertions(+), 22 deletions(-) (limited to 'c/wget.c') diff --git a/c/wget.c b/c/wget.c index 9d99eb0..aafbae0 100644 --- a/c/wget.c +++ b/c/wget.c @@ -1,39 +1,36 @@ -#include "sock.h" -/* wget [A.B.C.D] - fetch http://TARGET/ over the kernel TCP socket layer and - print it. No SLIP/IP/TCP here: connect() handshakes in the kernel, send()/ - recv() drive the stream. Default target 10.0.0.1:80 (the SLIP peer/host). */ +#include "resolve.h" +/* wget HOST|IP - fetch http://HOST/ over the kernel TCP socket layer and print + it. Resolves a hostname via DNS first (resolve.h), then connect()/send()/ + recv() over TCP - all the SLIP/IP/UDP/TCP lives in the kernel. */ -static unsigned char buf[220]; /* first + large so dst[] lands arg-safe */ +static unsigned char buf[220]; /* first + large so namebuf/dst land arg-safe */ +static unsigned char namebuf[64]; static unsigned char dst[4]; -static unsigned char parse_ip(char *s, unsigned char *out) { - unsigned char i, v; - for (i = 0; i < 4; i++) { - if (*s < '0' || *s > '9') return 0; - v = 0; - while (*s >= '0' && *s <= '9') { v = (unsigned char)(v * 10 + (*s - '0')); s++; } - out[i] = v; - if (i < 3) { if (*s != '.') return 0; s++; } - } - return 1; -} - void main(void) { char *arg, *r; unsigned char s, n, i; arg = getargs(); - if (!arg || !*arg) { dst[0] = 10; dst[1] = 0; dst[2] = 0; dst[3] = 1; } - else if (!parse_ip(arg, dst)) { puts("wget: bad address"); nl(); sexit(1); } + if (!arg || !*arg) { puts("usage: wget HOST|IP"); nl(); sexit(1); } + for (i = 0; arg[i] && i < 63; i++) namebuf[i] = (unsigned char)arg[i]; + namebuf[i] = 0; + + if (!resolve((char *)namebuf, dst)) { + puts("wget: cannot resolve "); puts((char *)namebuf); nl(); sexit(1); + } + puts("connecting "); for (i = 0; i < 4; i++) { putu(dst[i]); if (i < 3) putc('.'); } nl(); s = net_socket(SOCK_TCP); if (s == 0xFF) { puts("wget: no socket"); nl(); sexit(1); } net_bind(s, 40001); - puts("connecting"); nl(); if (net_connect(s, dst, 80) == 0xFF) { puts("wget: connect failed"); nl(); net_close(s); sexit(1); } - r = "GET / HTTP/1.0\r\nHost: gbos\r\n\r\n"; + /* GET / HTTP/1.0 with the requested host as the Host header */ + r = "GET / HTTP/1.0\r\nHost: "; n = 0; - while (r[n]) { buf[n] = r[n]; n++; } + while (r[n]) { buf[n] = (unsigned char)r[n]; n++; } + for (i = 0; namebuf[i]; i++) buf[n++] = namebuf[i]; + buf[n++] = '\r'; buf[n++] = '\n'; buf[n++] = '\r'; buf[n++] = '\n'; net_send(s, buf, n); for (;;) { -- cgit v1.3.1-sl0p