summaryrefslogtreecommitdiff
path: root/src/cmd/ntpfs.c
blob: 783b56e677f8b332a4ac0a7d09f228afd4af5cbe (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
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;
		}
	}
}