aboutsummaryrefslogtreecommitdiffstats
path: root/c
diff options
context:
space:
mode:
authoruser <user@clank>2026-07-17 14:11:22 +0200
committeruser <user@clank>2026-07-17 14:11:22 +0200
commita2cb756693229c7880d926d2cdde2f838c697415 (patch)
treee327278e7d8b7150d7a2b01cf38b4895caab7356 /c
parentkernel: sys_sleep - a cooperative delay off the DIV timer (+ ping pacing) (diff)
downloadgbos-a2cb756693229c7880d926d2cdde2f838c697415.tar.gz
gbos-a2cb756693229c7880d926d2cdde2f838c697415.tar.xz
gbos-a2cb756693229c7880d926d2cdde2f838c697415.zip
net: DHCP client - lease the IP at boot instead of hardcoding it
The address is no longer baked in. net_init starts at 0.0.0.0; a DHCP client runs as the first thing on boot (init/shell forks it and waits), and only once it has a lease (or gives up) does the prompt appear. Kernel: - IP is configurable: 0.0.0.0 until leased; NET_SETIP op stores it. - 16-bit frame length. DHCP/BOOTP packets are ~272 bytes, over the old 255-byte frame cap, so net_slip_send takes a 16-bit length, net_pump assembles into a 320-byte buffer with a 16-bit wNetRxLen, and udp_send writes a 16-bit IP total. Payloads stay <=255 (kept small on purpose) so the per-protocol datalen math is unchanged. wNetTx/wNetRxBuf 256->320, SK_RXBUF 208->288. Userland: - c/dhcp.c: DISCOVER->OFFER->REQUEST->ACK over a UDP socket, then net_setip(); times out gracefully (shell still boots) if there's no server. sh.c runs it before the prompt. Bridge (self-contained DHCP server, no dnsmasq): - tunbridge.py + netboot intercept UDP->:67 and answer OFFER/ACK leasing 10.0.0.2 (gateway 10.0.0.1); everything else is bridged/NATed as before. Regression-tested ICMP/UDP/TCP after the 16-bit change. Verified end to end: dhcp: discovering dhcp: leased 10.0.0.2 /# ping 10.0.0.1 -> 4/4 (traffic from the leased address)
Diffstat (limited to 'c')
-rw-r--r--c/dhcp.c72
-rw-r--r--c/sh.c6
-rw-r--r--c/sock.h7
-rw-r--r--c/tcp.c107
4 files changed, 192 insertions, 0 deletions
diff --git a/c/dhcp.c b/c/dhcp.c
new file mode 100644
index 0000000..e4186f4
--- /dev/null
+++ b/c/dhcp.c
@@ -0,0 +1,72 @@
+#include "sock.h"
+/* dhcp - a minimal DHCP client. DISCOVER -> OFFER -> REQUEST -> ACK over UDP
+ (kernel owns UDP; this owns BOOTP/DHCP), then hands the leased address to the
+ kernel with net_setip(). Run at boot before the shell. Times out gracefully
+ if there's no server (the shell then comes up unconfigured). */
+
+static unsigned char pkt[288]; /* BOOTP is ~240B + options (frames > 255) */
+static unsigned char ip[4];
+static unsigned char bcast[4];
+
+/* fill the fixed BOOTP request header + magic cookie; options start at 240 */
+static void bootp(void) {
+ unsigned int i;
+ for (i = 0; i < 240; i++) pkt[i] = 0;
+ pkt[0] = 1; pkt[1] = 1; pkt[2] = 6; /* op=request htype=eth hlen=6 */
+ pkt[4] = 0x12; pkt[5] = 0x34; pkt[6] = 0x56; pkt[7] = 0x78; /* xid */
+ pkt[10] = 0x80; /* flags: broadcast reply */
+ pkt[28] = 0x02; pkt[29] = 0x47; pkt[30] = 0x42; pkt[33] = 0x01; /* client MAC */
+ pkt[236] = 0x63; pkt[237] = 0x82; pkt[238] = 0x53; pkt[239] = 0x63; /* magic */
+}
+
+/* DHCP message type from the received packet (option 53), or 0 */
+static unsigned char mtype(void) {
+ unsigned int p = 240;
+ while (p < 280 && pkt[p] != 255) {
+ if (pkt[p] == 0) { p++; continue; } /* pad */
+ if (pkt[p] == 53) return pkt[p + 2];
+ p += 2 + pkt[p + 1];
+ }
+ return 0;
+}
+
+void main(void) {
+ unsigned char s, i, n;
+ unsigned int p;
+ bcast[0] = 255; bcast[1] = 255; bcast[2] = 255; bcast[3] = 255;
+
+ puts("dhcp: discovering"); nl();
+ s = net_socket(SOCK_UDP);
+ if (s == 0xFF) { puts("dhcp: no socket"); nl(); return; }
+ net_bind(s, 68);
+ net_connect(s, bcast, 67);
+
+ /* DISCOVER -> OFFER (retry a few times for startup timing) */
+ bootp();
+ p = 240;
+ pkt[p++] = 53; pkt[p++] = 1; pkt[p++] = 1; /* DHCPDISCOVER */
+ pkt[p++] = 255;
+ n = 0xFF;
+ for (i = 0; i < 3 && n == 0xFF; i++) {
+ net_send(s, pkt, (unsigned char)p);
+ n = net_recv(s, pkt, 255);
+ }
+ if (n == 0xFF || mtype() != 2) { puts("dhcp: no offer"); nl(); net_close(s); return; }
+ ip[0] = pkt[16]; ip[1] = pkt[17]; ip[2] = pkt[18]; ip[3] = pkt[19]; /* yiaddr */
+
+ /* REQUEST -> ACK */
+ bootp();
+ p = 240;
+ pkt[p++] = 53; pkt[p++] = 1; pkt[p++] = 3; /* DHCPREQUEST */
+ pkt[p++] = 50; pkt[p++] = 4; /* requested IP */
+ pkt[p++] = ip[0]; pkt[p++] = ip[1]; pkt[p++] = ip[2]; pkt[p++] = ip[3];
+ pkt[p++] = 255;
+ net_send(s, pkt, (unsigned char)p);
+ n = net_recv(s, pkt, 255);
+ net_close(s);
+ if (n == 0xFF || mtype() != 5) { puts("dhcp: request failed"); nl(); return; }
+ ip[0] = pkt[16]; ip[1] = pkt[17]; ip[2] = pkt[18]; ip[3] = pkt[19];
+
+ net_setip(ip);
+ puts("dhcp: leased "); for (i = 0; i < 4; i++) { putu(ip[i]); if (i < 3) putc('.'); } nl();
+}
diff --git a/c/sh.c b/c/sh.c
index a8dd09d..3178894 100644
--- a/c/sh.c
+++ b/c/sh.c
@@ -155,6 +155,12 @@ void main(void) {
char *tok[16];
char cwd[40];
cwd[0] = '/'; cwd[1] = 0;
+ /* bring up networking first: run the DHCP client and wait for it to finish
+ (got a lease, or gave up). Only then drop to the prompt. */
+ {
+ unsigned char d = lookup("dhcp");
+ if (d != NOFD) { if (fork() == 0) exec(d); else wait(); }
+ }
for (;;) {
unsigned char nt, i, pipepos, c, bg;
/* reap finished background jobs */
diff --git a/c/sock.h b/c/sock.h
index 1298271..9e4dd40 100644
--- a/c/sock.h
+++ b/c/sock.h
@@ -55,4 +55,11 @@ static void net_close(unsigned char s) {
static void net_poll(void) {
_nr.op = 6; sys_net(&_nr);
}
+/* set our IPv4 address (the DHCP client calls this once it has a lease) */
+static void net_setip(const unsigned char *ip) {
+ unsigned char i;
+ _nr.op = 7;
+ for (i = 0; i < 4; i++) _nr.ip[i] = ip[i];
+ sys_net(&_nr);
+}
#endif
diff --git a/c/tcp.c b/c/tcp.c
new file mode 100644
index 0000000..07eb2b1
--- /dev/null
+++ b/c/tcp.c
@@ -0,0 +1,107 @@
+#include "netlib.h"
+/* tcp - a minimal TCP client over SLIP (milestone C). Connects to 10.0.0.1:7,
+ sends a line, prints the reply, closes. No retransmission: the GB<->gateway
+ link is lossless, and the gateway's real socket handles the lossy side.
+ Us = 10.0.0.2, peer = 10.0.0.1. */
+
+static unsigned char pkt[256];
+static unsigned long snd_nxt, rcv_nxt;
+#define LPORT 40000u
+#define RPORT 7u
+
+/* SYN 0x02, ACK 0x10, FIN 0x01, PSH 0x08, RST 0x04 */
+
+static unsigned int sum16(unsigned char *d, unsigned int len, unsigned int sum) {
+ unsigned int w, i = 0;
+ while (i + 1 < len) { w = ((unsigned int)d[i] << 8) | d[i + 1]; sum += w; if (sum < w) sum++; i += 2; }
+ if (len & 1) { w = (unsigned int)d[len - 1] << 8; sum += w; if (sum < w) sum++; }
+ return sum;
+}
+static void put32(unsigned char *p, unsigned long v) {
+ p[0] = (unsigned char)(v >> 24); p[1] = (unsigned char)(v >> 16);
+ p[2] = (unsigned char)(v >> 8); p[3] = (unsigned char)v;
+}
+static unsigned long get32(unsigned char *p) {
+ return ((unsigned long)p[0] << 24) | ((unsigned long)p[1] << 16)
+ | ((unsigned long)p[2] << 8) | p[3];
+}
+
+/* build+send IP+TCP with 'flags' and 'dlen' payload bytes already at pkt[40..] */
+static void tcp_send(unsigned char flags, unsigned char dlen) {
+ unsigned int c, psum, tcplen = 20 + dlen, total = 20 + tcplen;
+ pkt[0] = 0x45; pkt[1] = 0; pkt[2] = (unsigned char)(total >> 8); pkt[3] = (unsigned char)total;
+ pkt[4] = 0; pkt[5] = 1; pkt[6] = 0x40; pkt[7] = 0; pkt[8] = 64; pkt[9] = 6;
+ pkt[10] = 0; pkt[11] = 0;
+ pkt[12] = 10; pkt[13] = 0; pkt[14] = 0; pkt[15] = 2;
+ pkt[16] = 10; pkt[17] = 0; pkt[18] = 0; pkt[19] = 1;
+ pkt[20] = LPORT >> 8; pkt[21] = (unsigned char)LPORT;
+ pkt[22] = RPORT >> 8; pkt[23] = (unsigned char)RPORT;
+ put32(pkt + 24, snd_nxt);
+ put32(pkt + 28, rcv_nxt);
+ pkt[32] = 0x50; pkt[33] = flags; pkt[34] = 2; pkt[35] = 0; /* window 512 */
+ pkt[36] = 0; pkt[37] = 0; pkt[38] = 0; pkt[39] = 0;
+ psum = sum16(pkt + 12, 8, 0);
+ c = 6; psum += c; if (psum < c) psum++;
+ c = tcplen; psum += c; if (psum < c) psum++;
+ c = ~sum16(pkt + 20, tcplen, psum);
+ pkt[36] = (unsigned char)(c >> 8); pkt[37] = (unsigned char)c;
+ c = ~sum16(pkt, 20, 0);
+ pkt[10] = (unsigned char)(c >> 8); pkt[11] = (unsigned char)c;
+ slip_send((char *)pkt, (unsigned char)total);
+}
+
+/* receive the next TCP segment for our connection; returns TCP header offset,
+ sets *flags,*seq,*dlen,*doff. */
+static unsigned char tcp_recv(unsigned char *flags, unsigned long *seq,
+ unsigned char *dlen, unsigned char *doff) {
+ unsigned char len, ihl, hl;
+ for (;;) {
+ len = slip_recv((char *)pkt, 255);
+ if (len < 40 || (pkt[0] >> 4) != 4 || pkt[9] != 6) continue;
+ ihl = (pkt[0] & 0x0f) << 2;
+ if (((unsigned int)pkt[22] << 8 | pkt[23]) != LPORT) continue; /* not our port */
+ hl = (pkt[ihl + 12] >> 4) << 2;
+ *flags = pkt[ihl + 13];
+ *seq = get32(pkt + ihl + 4);
+ *doff = (unsigned char)(ihl + hl);
+ *dlen = (unsigned char)(len - ihl - hl);
+ return ihl;
+ }
+}
+
+void main(void) {
+ unsigned char flags, dlen, doff, i;
+ unsigned long seq;
+ puts("tcp: SYN -> 10.0.0.1:7"); nl();
+ snd_nxt = 1000; rcv_nxt = 0;
+ tcp_send(0x02, 0); /* SYN */
+ snd_nxt = 1001;
+
+ tcp_recv(&flags, &seq, &dlen, &doff); /* expect SYN|ACK */
+ if (!(flags & 0x02)) { puts("tcp: no SYN-ACK"); nl(); return; }
+ rcv_nxt = seq + 1;
+ tcp_send(0x10, 0); /* ACK -> ESTABLISHED */
+
+ pkt[40] = 'h'; pkt[41] = 'i'; pkt[42] = ' '; pkt[43] = 'g'; pkt[44] = 'b';
+ tcp_send(0x18, 5); /* PSH|ACK "hi gb" */
+ snd_nxt += 5;
+
+ for (;;) {
+ tcp_recv(&flags, &seq, &dlen, &doff);
+ if (dlen) {
+ puts("tcp: recv '");
+ for (i = 0; i < dlen; i++) putc(pkt[doff + i]);
+ puts("'"); nl();
+ rcv_nxt = seq + dlen;
+ tcp_send(0x10, 0); /* ACK the data */
+ tcp_send(0x11, 0); /* FIN|ACK - start closing */
+ snd_nxt += 1;
+ }
+ if (flags & 0x01) { /* their FIN */
+ rcv_nxt = seq + dlen + 1;
+ tcp_send(0x10, 0); /* ACK it */
+ puts("tcp: closed"); nl();
+ return;
+ }
+ }
+}