diff options
author | dzwdz | 2024-05-04 18:22:23 +0200 |
---|---|---|
committer | dzwdz | 2024-05-04 18:22:23 +0200 |
commit | 8c7b8d9e4ec40c38c657851354fc74428f0df1ac (patch) | |
tree | 29588306bd4453de86fc5a4ba230537f8f47bea5 /src/cmd/ntpfs.c | |
parent | 32d7e504169351ce6a5542b4f559c7e4d3834b45 (diff) |
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.
Diffstat (limited to 'src/cmd/ntpfs.c')
-rw-r--r-- | src/cmd/ntpfs.c | 102 |
1 files changed, 102 insertions, 0 deletions
diff --git a/src/cmd/ntpfs.c b/src/cmd/ntpfs.c new file mode 100644 index 0000000..783b56e --- /dev/null +++ b/src/cmd/ntpfs.c @@ -0,0 +1,102 @@ +#include <camellia.h> +#include <camellia/flags.h> +#include <camellia/fs/misc.h> +#include <camellia/fsutil.h> +#include <camellia/syscalls.h> +#include <err.h> +#include <stddef.h> +#include <stdint.h> +#include <stdio.h> +#include <unistd.h> + +// TODO put that in the libc somewhere +#include "netstack/util.h" + +enum { + PacketSize = 12 * 4, /* 12 words */ + ClientMode = 3, + NtpV4 = 4 << 3, + TxTimestamp = 40, +}; + +uint64_t offset = 0; + +uint64_t +getntptime(const char *addr) +{ + char packet[PacketSize] = {0}; + packet[0] |= ClientMode; + packet[0] |= NtpV4; + + hid_t h = camellia_open(addr, OPEN_READ | OPEN_WRITE); + if (h < 0) { + warn("open %s", addr); + return 0; + } + if (write(h, packet, PacketSize) != PacketSize) { + warnx("partial write"); + close(h); + return 0; + } + ssize_t ret = read(h, packet, PacketSize); + if (ret != PacketSize) { + warnx("partial read"); + close(h); + return 0; + } + + return nget64(packet + TxTimestamp); +} + +/* Converts an NTP timestamp to the amount of nanoseconds (10^-9) since the + * Unix epoch. */ +uint64_t +ntp2unix(uint64_t ntp) +{ + uint64_t seconds = (ntp >> 32) - 2208988800; + uint64_t frac = ntp & 0xFFFFFFFF; + uint64_t prec = 1000000000; + return seconds * prec + ((frac * prec) >> 32); +} + +int +main(int argc, const char *argv[]) +{ + if (argc != 2) { + errx(1, "bad usage"); + } + + // TODO: error checking, once i actually implement timeouts + uint64_t ntpt = getntptime(argv[1]); + uint64_t unixt = ntp2unix(ntpt); + offset = unixt - _sys_time(0); + + char buf[256]; + int len; + + struct ufs_request req; + hid_t reqh; + while ((reqh = ufs_wait(buf, sizeof(buf), &req))) { + switch (req.op) { + case VFSOP_OPEN: + if (req.len == 0 && !OPEN_WRITEABLE(req.flags)) { + _sys_fs_respond(reqh, NULL, 0, 0); + } else { + _sys_fs_respond(reqh, NULL, -EGENERIC, 0); + } + break; + case VFSOP_READ: + len = snprintf(buf, sizeof(buf), "%lu\n", _sys_time(0) + offset); + if (0 <= len && len < (int)sizeof(buf)) { + fs_normslice(&req.offset, &req.len, len, false); + _sys_fs_respond(reqh, buf + req.offset, req.len, 0); + } else { + _sys_fs_respond(reqh, NULL, -EGENERIC, 0); + } + break; + default: + _sys_fs_respond(reqh, NULL, -ENOSYS, 0); + break; + } + } +} |