From 8c7b8d9e4ec40c38c657851354fc74428f0df1ac Mon Sep 17 00:00:00 2001
From: dzwdz
Date: Sat, 4 May 2024 18:22:23 +0200
Subject: user/ntpfs: implement a basic ntp client

time() will probably end up doing io. That sounded bad at first, but Plan 9
does that too (see /sys/src/libc/9sys/nsec.c), so it's probably fine.

I might need better service management soon. Also, dunno what it should return
before it makes contact with NTP.
I could implement RTC support, but eh. Doesn't feel that necessary.

I'll also need to remember how the hell threading works, so it can talk with
the ntp daemon on another thread.
---
 src/cmd/netstack/udp.c  | 11 +++++++----
 src/cmd/netstack/util.h | 12 ++++++++++++
 2 files changed, 19 insertions(+), 4 deletions(-)

(limited to 'src/cmd/netstack')

diff --git a/src/cmd/netstack/udp.c b/src/cmd/netstack/udp.c
index 285af41..3b53b47 100644
--- a/src/cmd/netstack/udp.c
+++ b/src/cmd/netstack/udp.c
@@ -55,10 +55,13 @@ struct udp_conn *udpc_new(
 	c->lport = u.src ? u.src : 50000; // TODO randomize source ports
 	c->rport = u.dst;
 	if (arpcache_get(c->rip, &c->rmac) < 0) {
-		// TODO make arp request, wait for reply
-		warnx("IP not in ARP cache, unimplemented");
-		free(c);
-		return NULL;
+		// TODO wait for ARP reply
+		arp_request(c->rip);
+		if (arpcache_get(state.gateway, &c->rmac) < 0) {
+			warnx("neither target nor gateway not in ARP cache, dropping");
+			free(c);
+			return NULL;
+		}
 	}
 	c->on_recv = on_recv;
 	c->carg = carg;
diff --git a/src/cmd/netstack/util.h b/src/cmd/netstack/util.h
index c1032a2..1cd22fb 100644
--- a/src/cmd/netstack/util.h
+++ b/src/cmd/netstack/util.h
@@ -40,3 +40,15 @@ static inline uint32_t nget32(const void *vbuf) {
 	     | (b[2] <<  8)
 	     | (b[3] <<  0);
 }
+
+static inline uint64_t nget64(const void *vbuf) {
+	const uint8_t *b = vbuf;
+	return ((uint64_t)b[0] << 56)
+	     | ((uint64_t)b[1] << 48)
+	     | ((uint64_t)b[2] << 40)
+	     | ((uint64_t)b[3] << 32)
+	     | ((uint64_t)b[4] << 24)
+	     | ((uint64_t)b[5] << 16)
+	     | ((uint64_t)b[6] <<  8)
+	     | ((uint64_t)b[7] <<  0);
+}
-- 
cgit v1.2.3