diff options
| author | user <user@clank> | 2026-07-18 00:38:38 +0200 |
|---|---|---|
| committer | user <user@clank> | 2026-07-18 00:38:38 +0200 |
| commit | ba2d7ae1d7b319645b10aaa31a7f664f9f80c25c (patch) | |
| tree | e01e6c7c710a661defe2515ff9657f08cddcb283 /usr/dhcp.c | |
| parent | tools: gbtype prints hub errors to stderr and exits 1 (diff) | |
| download | gbos-ba2d7ae1d7b319645b10aaa31a7f664f9f80c25c.tar.gz gbos-ba2d7ae1d7b319645b10aaa31a7f664f9f80c25c.tar.xz gbos-ba2d7ae1d7b319645b10aaa31a7f664f9f80c25c.zip | |
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.
Diffstat (limited to 'usr/dhcp.c')
| -rw-r--r-- | usr/dhcp.c | 72 |
1 files changed, 72 insertions, 0 deletions
diff --git a/usr/dhcp.c b/usr/dhcp.c new file mode 100644 index 0000000..e4186f4 --- /dev/null +++ b/usr/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(); +} |
