diff options
| author | user <user@clank> | 2026-07-17 13:11:53 +0200 |
|---|---|---|
| committer | user <user@clank> | 2026-07-17 13:11:53 +0200 |
| commit | a565bcd942efeb617f5424ae5a881211a7bd0216 (patch) | |
| tree | 518915e70d097ed8f8fee99422bc7161f9c53fc1 | |
| parent | net: TCP sockets in the kernel - a Game Boy fetches HTTP over real TCP (diff) | |
| download | gbos-a565bcd942efeb617f5424ae5a881211a7bd0216.tar.gz gbos-a565bcd942efeb617f5424ae5a881211a7bd0216.tar.xz gbos-a565bcd942efeb617f5424ae5a881211a7bd0216.zip | |
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
<!doctype html><html lang="en"><head><title>Example Domain</title>...
</body></html>
[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")
| -rw-r--r-- | c/nslookup.c | 83 | ||||
| -rw-r--r-- | c/resolve.h | 81 | ||||
| -rw-r--r-- | c/wget.c | 41 |
3 files changed, 109 insertions, 96 deletions
diff --git a/c/nslookup.c b/c/nslookup.c index 020a1c1..6a531b8 100644 --- a/c/nslookup.c +++ b/c/nslookup.c @@ -1,88 +1,23 @@ -#include "sock.h" +#include "resolve.h" /* nslookup HOST - resolve a hostname to an IPv4 address via DNS over UDP. - The kernel owns UDP; this program just builds the DNS query and parses the - answer. Queries 1.1.1.1:53 (reachable through the bridge NAT). */ + All the DNS logic lives in resolve.h (shared with wget). */ -/* buf is first and large so 'name' lands past the arg string at 0xA000 */ -static unsigned char buf[200]; -static unsigned char name[64]; -static unsigned char dnssrv[4]; +static unsigned char namebuf[64]; /* first so it lands past the arg at 0xA000 */ static unsigned char ip[4]; -/* build a DNS A-record query for 'host' into buf; return total length */ -static unsigned char build_query(unsigned char *host) { - unsigned char i = 0, p = 12, lblpos, len; - buf[0] = 0x12; buf[1] = 0x34; /* id */ - buf[2] = 0x01; buf[3] = 0x00; /* flags: recursion desired */ - buf[4] = 0; buf[5] = 1; /* qdcount = 1 */ - buf[6] = 0; buf[7] = 0; buf[8] = 0; buf[9] = 0; buf[10] = 0; buf[11] = 0; - while (host[i]) { - lblpos = p++; len = 0; - while (host[i] && host[i] != '.') { buf[p++] = host[i]; i++; len++; } - buf[lblpos] = len; - if (host[i] == '.') i++; - } - buf[p++] = 0; /* root label */ - buf[p++] = 0; buf[p++] = 1; /* QTYPE = A */ - buf[p++] = 0; buf[p++] = 1; /* QCLASS = IN */ - return p; -} - -/* advance past a DNS name at offset p (handles 0xC0 compression pointers) */ -static unsigned int skip_name(unsigned int p) { - for (;;) { - if (buf[p] == 0) return p + 1; - if ((buf[p] & 0xC0) == 0xC0) return p + 2; - p += buf[p] + 1; - } -} - -/* find the first A record in the response; fill ip[]; return 1 on success */ -static unsigned char parse_answer(void) { - unsigned int p, a, an, type, rdlen; - an = ((unsigned int)buf[6] << 8) | buf[7]; /* ancount */ - if (an == 0) return 0; - p = skip_name(12) + 4; /* skip question (QNAME+QT+QC) */ - for (a = 0; a < an; a++) { - p = skip_name(p); - type = ((unsigned int)buf[p] << 8) | buf[p + 1]; - p += 8; /* TYPE(2)+CLASS(2)+TTL(4) */ - rdlen = ((unsigned int)buf[p] << 8) | buf[p + 1]; - p += 2; - if (type == 1 && rdlen == 4) { - ip[0] = buf[p]; ip[1] = buf[p + 1]; ip[2] = buf[p + 2]; ip[3] = buf[p + 3]; - return 1; - } - p += rdlen; - } - return 0; -} - void main(void) { char *host; - unsigned char s, i, qlen, rlen; + unsigned char i; host = getargs(); if (!host || !*host) { puts("usage: nslookup HOST"); nl(); sexit(1); } - for (i = 0; host[i] && i < 63; i++) name[i] = host[i]; /* arg -> safe buf */ - name[i] = 0; - - dnssrv[0] = 1; dnssrv[1] = 1; dnssrv[2] = 1; dnssrv[3] = 1; /* 1.1.1.1 */ - s = net_socket(SOCK_UDP); - if (s == 0xFF) { puts("nslookup: no socket"); nl(); sexit(1); } - net_bind(s, 40000); - net_connect(s, dnssrv, 53); - - qlen = build_query(name); - net_send(s, buf, qlen); - rlen = net_recv(s, buf, sizeof(buf)); - if (rlen == 0xFF) { puts("nslookup: timeout"); nl(); net_close(s); sexit(1); } + for (i = 0; host[i] && i < 63; i++) namebuf[i] = (unsigned char)host[i]; + namebuf[i] = 0; - if (parse_answer()) { - puts((char *)name); puts(" -> "); + if (resolve((char *)namebuf, ip)) { + puts((char *)namebuf); puts(" -> "); for (i = 0; i < 4; i++) { putu(ip[i]); if (i < 3) putc('.'); } nl(); } else { - puts("nslookup: no A record"); nl(); + puts("nslookup: cannot resolve "); puts((char *)namebuf); nl(); } - net_close(s); } diff --git a/c/resolve.h b/c/resolve.h new file mode 100644 index 0000000..9aaccc1 --- /dev/null +++ b/c/resolve.h @@ -0,0 +1,81 @@ +#ifndef RESOLVE_H +#define RESOLVE_H +/* resolve.h - turn a dotted-quad OR a hostname into an IPv4 address. Hostname + lookups go out as a DNS A query over a UDP socket to 1.1.1.1:53; the kernel + owns UDP, this owns DNS. Header-only (one copy per program). */ +#include "sock.h" + +static unsigned char _dnsbuf[200]; +static unsigned char _dnssrv[4]; + +/* parse "A.B.C.D" into out[4]; return 1 on success */ +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; +} + +static unsigned char _dns_query(char *host) { + unsigned char i = 0, p = 12, lblpos, len; + _dnsbuf[0] = 0x12; _dnsbuf[1] = 0x34; _dnsbuf[2] = 0x01; _dnsbuf[3] = 0x00; + _dnsbuf[4] = 0; _dnsbuf[5] = 1; + _dnsbuf[6] = 0; _dnsbuf[7] = 0; _dnsbuf[8] = 0; _dnsbuf[9] = 0; _dnsbuf[10] = 0; _dnsbuf[11] = 0; + while (host[i]) { + lblpos = p++; len = 0; + while (host[i] && host[i] != '.') { _dnsbuf[p++] = host[i]; i++; len++; } + _dnsbuf[lblpos] = len; + if (host[i] == '.') i++; + } + _dnsbuf[p++] = 0; _dnsbuf[p++] = 0; _dnsbuf[p++] = 1; _dnsbuf[p++] = 0; _dnsbuf[p++] = 1; + return p; +} +static unsigned int _dns_skip(unsigned int p) { + for (;;) { + if (_dnsbuf[p] == 0) return p + 1; + if ((_dnsbuf[p] & 0xC0) == 0xC0) return p + 2; + p += _dnsbuf[p] + 1; + } +} +static unsigned char _dns_answer(unsigned char *ip) { + unsigned int p, a, an, type, rdlen; + an = ((unsigned int)_dnsbuf[6] << 8) | _dnsbuf[7]; + if (an == 0) return 0; + p = _dns_skip(12) + 4; + for (a = 0; a < an; a++) { + p = _dns_skip(p); + type = ((unsigned int)_dnsbuf[p] << 8) | _dnsbuf[p + 1]; + p += 8; + rdlen = ((unsigned int)_dnsbuf[p] << 8) | _dnsbuf[p + 1]; + p += 2; + if (type == 1 && rdlen == 4) { + ip[0] = _dnsbuf[p]; ip[1] = _dnsbuf[p + 1]; ip[2] = _dnsbuf[p + 2]; ip[3] = _dnsbuf[p + 3]; + return 1; + } + p += rdlen; + } + return 0; +} + +/* resolve a dotted-quad or hostname into ip[4]; return 1 on success */ +static unsigned char resolve(char *host, unsigned char *ip) { + unsigned char s, n; + if (parse_ip(host, ip)) return 1; /* already an address */ + _dnssrv[0] = 1; _dnssrv[1] = 1; _dnssrv[2] = 1; _dnssrv[3] = 1; + s = net_socket(SOCK_UDP); + if (s == 0xFF) return 0; + net_bind(s, 40000); + net_connect(s, _dnssrv, 53); + n = _dns_query(host); + net_send(s, _dnsbuf, n); + n = net_recv(s, _dnsbuf, 200); + net_close(s); + if (n == 0xFF) return 0; + return _dns_answer(ip); +} +#endif @@ -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 (;;) { |
