aboutsummaryrefslogtreecommitdiffstats
path: root/c
diff options
context:
space:
mode:
authoruser <user@clank>2026-07-17 12:54:43 +0200
committeruser <user@clank>2026-07-17 12:54:43 +0200
commit26a2ccb8368a9968b6ec16068ad68dd0edb40641 (patch)
tree1396180dc2a8e635c58e10b994ae15fe7705e542 /c
parentnet: proper socket API in the kernel (SYS_NET) - no more SLIP in userland (diff)
downloadgbos-26a2ccb8368a9968b6ec16068ad68dd0edb40641.tar.gz
gbos-26a2ccb8368a9968b6ec16068ad68dd0edb40641.tar.xz
gbos-26a2ccb8368a9968b6ec16068ad68dd0edb40641.zip
net: UDP sockets in the kernel + a DNS resolver (real hostname lookups)
Adds UDP to the kernel socket layer on top of the ICMP core: - net_sum() (raw folded sum) split out of net_cksum() so a UDP pseudo-header (src/dst IP + proto + length) can seed the segment checksum. - udp_send: builds IP+UDP with the pseudo-header checksum; NET_BIND sets the local/source port; net_connect sets the peer. - udp_in: demuxes inbound UDP by destination port to the bound socket (net_find_udp), delivers the payload + source addr. Also fixes a real recv bug: net_pump clobbers BC/DE/HL, so the old recv timeout counted in registers and was effectively random. recv now counts in WRAM. New `nslookup HOST` (PROG_NSLOOKUP=28, bank 30): builds a DNS A query and parses the answer (with 0xC0 name-compression) entirely in userland over a UDP socket - the kernel never sees DNS, just UDP. Verified through the bridge NAT: /# nslookup example.com -> example.com -> 172.66.147.243 This gives us name resolution for the TCP/HTTP demo next.
Diffstat (limited to '')
-rw-r--r--c/nslookup.c88
-rw-r--r--c/sock.h6
2 files changed, 94 insertions, 0 deletions
diff --git a/c/nslookup.c b/c/nslookup.c
new file mode 100644
index 0000000..020a1c1
--- /dev/null
+++ b/c/nslookup.c
@@ -0,0 +1,88 @@
+#include "sock.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). */
+
+/* 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 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;
+ 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); }
+
+ if (parse_answer()) {
+ puts((char *)name); puts(" -> ");
+ for (i = 0; i < 4; i++) { putu(ip[i]); if (i < 3) putc('.'); }
+ nl();
+ } else {
+ puts("nslookup: no A record"); nl();
+ }
+ net_close(s);
+}
diff --git a/c/sock.h b/c/sock.h
index 9af2351..1298271 100644
--- a/c/sock.h
+++ b/c/sock.h
@@ -42,6 +42,12 @@ static unsigned char net_recv(unsigned char s, void *buf, unsigned char max) {
_nr.op = 3; _nr.sock = s; _nr.buf = (unsigned char *)buf; _nr.len = max;
return sys_net(&_nr);
}
+/* set the local (source) port - needed so UDP replies demux back to us */
+static unsigned char net_bind(unsigned char s, unsigned int port) {
+ _nr.op = 5; _nr.sock = s;
+ _nr.port_hi = (unsigned char)(port >> 8); _nr.port_lo = (unsigned char)port;
+ return sys_net(&_nr);
+}
static void net_close(unsigned char s) {
_nr.op = 4; _nr.sock = s; sys_net(&_nr);
}