Skip to content

LordOS — a free, fast and secure GNU/Linux distribution based on Debian, with its own desktop environment: display server and Wayland compositor in C, a React UI engine on QuickJS, and private native apps.

Monitor: traffic history file and lord.monitor.history

Federico Pereira66944f7539e0
diff --git a/server/Makefile b/server/Makefile
index 832ff4f..d84d20d 100644
--- a/server/Makefile
+++ b/server/Makefile
@@ -48,7 +48,7 @@ QJS_OBJ := $(patsubst %,build/qjs/%.o,quickjs libregexp libunicode cutils dtoa)
# .lpk app isolation, permissions and signing (src/sec, see docs/PRIVACY.md and docs/SIGNING.md)
# Monitor: metrics, connections and the country of an IP (src/monitor, see docs/MONITOR.md)
-MON_SRC := src/monitor/mon.c src/monitor/conn.c src/monitor/procctl.c src/monitor/proc_admin.c src/monitor/mmdb.c src/monitor/map.c src/ui/js_monitor.c
+MON_SRC := src/monitor/mon.c src/monitor/nethist.c src/monitor/netrec.c src/monitor/conn.c src/monitor/procctl.c src/monitor/proc_admin.c src/monitor/mmdb.c src/monitor/map.c src/ui/js_monitor.c
# Media (src/media, docs/MEDIA.md): only the lord-server side. Only lord-media links FFmpeg.
MEDIA_SRC := src/media/media.c src/media/mview.c src/media/capture.c src/media/annot.c src/media/maudio.c src/media/mpulse.c src/media/proto.c src/ui/js_media.c
SEC_SRC := src/sec/json.c src/sec/sign.c src/sec/pkgstate.c src/sec/perms.c src/sec/cgroup.c src/sec/sandbox.c src/sec/brokersrv.c src/sec/brokercam.c src/sec/brokerkr.c src/sec/restrict.c src/sec/appid.c src/sec/pwguard.c
@@ -283,7 +283,7 @@ build/fw-rules-test: tests/fw_rules_test.c src/fw/rules.c src/fw/fwmap.c src/sec
# the destinations a user types in the rule sheet (tests/fw_dest_cases.txt), through the store's
# parser; tests/fwrule_input_test.mjs checks the same file against sdk/fwrule.js (run_suites.sh)
test: build/fwdest-test
-build/fwdest-test: tests/fwdest_test.c tests/fw_dest_cases.txt src/fw/rules.c src/fw/fwmap.c src/sec/json.c $(wildcard src/fw/*.h) src/sec/json.h
+build/fwdest-test: tests/fwdest_test.c tests/fw_dest_cases.txt tests/fw_domain_cases.txt src/fw/rules.c src/fw/fwmap.c src/sec/json.c $(wildcard src/fw/*.h) src/sec/json.h
@mkdir -p build
$(CC) $(CPPFLAGS) $(CFLAGS) $(WARN) -std=c11 -o $@ tests/fwdest_test.c src/fw/rules.c src/fw/fwmap.c src/sec/json.c
./$@ || { rm -f $@; exit 1; }
@@ -310,6 +310,12 @@ test: build/procctl-test
build/procctl-test: tests/procctl_test.c src/monitor/procctl.c src/monitor/procctl.h src/monitor/mon.c src/monitor/conn.c src/monitor/mmdb.c src/pkg/xz.c build/obj/ui/stb.o
$(CC) $(CPPFLAGS) $(CFLAGS) $(WARN) -std=c11 -o $@ tests/procctl_test.c src/monitor/procctl.c src/monitor/mon.c src/monitor/conn.c src/monitor/mmdb.c src/pkg/xz.c build/obj/ui/stb.o -lm -lpthread
./$@ || { rm -f $@; exit 1; }
+# the Monitor's traffic history file: totals, 7-day/30-day rotation, cut and damaged files, Clear
+test: build/nethist-test
+build/nethist-test: tests/nethist_test.c src/monitor/nethist.c src/monitor/nethist.h
+ @mkdir -p build
+ $(CC) $(CPPFLAGS) $(CFLAGS) $(WARN) -std=c11 -o $@ tests/nethist_test.c src/monitor/nethist.c -lm
+ ./$@ || { rm -f $@; exit 1; }
# canvas.c rows: SIMD (NEON on aarch64) against C, bit for bit, and how long each one takes
# UI engine (docs/DESIGN.md "Engine notes"): ellipsis in LTR/RTL/Arabic/Thai/emoji, per-side borders,
# per-corner radii, logical edges, transforms and hit testing; `build/engine-test bench` times a 2000-row
diff --git a/server/src/main.c b/server/src/main.c
index ff92643..7d69a2e 100644
--- a/server/src/main.c
+++ b/server/src/main.c
@@ -48,6 +48,7 @@
#include "hw/hw.h"
#include "hw/modes.h"
#include "outputs.h"
+#include "monitor/netrec.h"
#include <errno.h>
#include <fcntl.h>
@@ -1557,6 +1558,7 @@ int main(int argc, char **argv) {
if (ok && snd_start()) fprintf(stderr, "lord-server: no PipeWire for now (sound); it retries on its own\n");
if (ok && !s.greeter) portal_start(); /* dark mode, accent and buttons for GTK/GNOME/Qt (ui/portal.c) */
if (ok && !s.greeter) term_restore(); /* shells still alive from the previous start */
+ netrec_init(ok && !s.greeter && session_user != NULL); /* the Monitor's traffic history (monitor/netrec.c) */
if (getenv("LORD_DAMAGE") && !strcmp(getenv("LORD_DAMAGE"), "1")) paint_multi_damage = 0; /* one damaged box (paint.c) */
remote_server = &s;
tp_route_init(&tp_host);
@@ -1619,6 +1621,9 @@ int main(int argc, char **argv) {
if (hwd >= 0 && (delay < 0 || hwd < delay)) delay = hwd;
/* Settings → Displays or a hotplug: displays, modes, scales, arrangement (between frames) */
if (!s.headless && !any_flip_pending(&s) && display_take_resync()) outputs_apply(&s);
+ netrec_tick(); /* traffic history: every 5 s while on (monitor/netrec.c) */
+ double nrd = netrec_timeout();
+ if (nrd >= 0 && (delay < 0 || nrd < delay)) delay = nrd;
double tpd = tp_timeout(now_ms());
if (tpd >= 0 && (delay < 0 || tpd < delay)) delay = tpd;
/* sin monitor y sin nadie mirando no se dibuja (al conectarse alguien llega un cuadro entero) */
@@ -1757,6 +1762,7 @@ int main(int argc, char **argv) {
/* Cerrar sesión (tema o Ctrl+Alt+Backspace) termina los shells; una señal o un reinicio
* del servidor los deja vivos en sus guardianes para recuperarlos al volver. */
term_shutdown(!(ui.quit_requested || s.quit));
+ netrec_shutdown();
wl_stop();
snd_stop();
js_shutdown();
diff --git a/server/src/monitor/nethist.c b/server/src/monitor/nethist.c
new file mode 100644
index 0000000..7266a4f
--- /dev/null
+++ b/server/src/monitor/nethist.c
@@ -0,0 +1,625 @@
+/* nethist.c — the Monitor's traffic history file (see nethist.h and docs/MONITOR.md).
+ *
+ * Only the minute in progress lives in memory (its totals and one entry per app that moved bytes).
+ * When it ends one record is appended to the file; queries and compaction read the file through the
+ * same strict reader, which checks every length and the CRC of every record and stops at the first
+ * record that doesn't check out. */
+#include "nethist.h"
+
+#include <errno.h>
+#include <fcntl.h>
+#include <limits.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/stat.h>
+#include <unistd.h>
+
+enum { NH_APP = 1, NH_MIN = 2, NH_HOUR = 3 };
+#define NH_MAGIC "LNH1"
+#define NH_VERSION 1
+#define NH_HDR 8
+#define NH_REC_MAX 65535
+#define NH_COMPACT_EVERY (6 * 60) /* minutes */
+
+typedef struct { uint32_t idx; uint64_t rx, tx; } Acc;
+
+struct NetHist {
+ char path[PATH_MAX];
+ int loaded; /* the app table was read from the file */
+ char (*ids)[NH_ID_MAX]; /* index → app id; 0 is "other" */
+ uint8_t *written; /* its NH_APP record is in the file */
+ int napps;
+ long long cur; /* the minute in progress (Unix time / 60), 0 = none */
+ uint64_t rx, tx;
+ Acc *acc;
+ int nacc, cacc;
+ int since_compact;
+ long long last_compact;
+};
+
+/* ---- small helpers ------------------------------------------------------------------------------ */
+
+static uint32_t crc_table[256];
+static uint32_t crc32_of(const uint8_t *p, size_t n) {
+ if (!crc_table[1])
+ for (uint32_t i = 0; i < 256; i++) {
+ uint32_t c = i;
+ for (int k = 0; k < 8; k++) c = c & 1 ? 0xedb88320u ^ (c >> 1) : c >> 1;
+ crc_table[i] = c;
+ }
+ uint32_t c = 0xffffffffu;
+ for (size_t i = 0; i < n; i++) c = crc_table[(c ^ p[i]) & 255] ^ (c >> 8);
+ return c ^ 0xffffffffu;
+}
+
+typedef struct { uint8_t *p; size_t n, cap; int bad; } Out;
+
+static void out_need(Out *o, size_t k) {
+ if (o->bad || o->n + k <= o->cap) return;
+ size_t c = o->cap ? o->cap * 2 : 4096;
+ while (c < o->n + k) c *= 2;
+ uint8_t *q = realloc(o->p, c);
+ if (!q) { o->bad = 1; return; }
+ o->p = q;
+ o->cap = c;
+}
+static void out_bytes(Out *o, const void *b, size_t k) { out_need(o, k); if (!o->bad) { memcpy(o->p + o->n, b, k); o->n += k; } }
+static void out_u32(Out *o, uint32_t v) { uint8_t b[4] = { (uint8_t)v, (uint8_t)(v >> 8), (uint8_t)(v >> 16), (uint8_t)(v >> 24) }; out_bytes(o, b, 4); }
+static void out_var(Out *o, uint64_t v) {
+ uint8_t b[10];
+ int k = 0;
+ do { b[k] = (uint8_t)(v & 127); v >>= 7; if (v) b[k] |= 128; k++; } while (v);
+ out_bytes(o, b, (size_t)k);
+}
+
+/* One record around the payload built in body. */
+static void out_rec(Out *o, int type, const Out *body) {
+ if (body->bad || body->n > NH_REC_MAX) { o->bad = 1; return; }
+ size_t start = o->n;
+ uint8_t h[3] = { (uint8_t)type, (uint8_t)body->n, (uint8_t)(body->n >> 8) };
+ out_bytes(o, h, 3);
+ out_bytes(o, body->p, body->n);
+ if (!o->bad) out_u32(o, crc32_of(o->p + start, 3 + body->n));
+}
+
+static int valid_id(const char *s) {
+ size_t n = strlen(s);
+ if (!n || n >= NH_ID_MAX) return 0;
+ for (size_t i = 0; i < n; i++) {
+ char c = s[i];
+ if (!((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9') || strchr("._:@+~/-", c))) return 0;
+ }
+ return 1;
+}
+
+/* ---- reading -------------------------------------------------------------------------------------- */
+
+typedef struct { const uint8_t *p; size_t n, i; int bad; } In;
+static uint64_t in_var(In *r) {
+ uint64_t v = 0;
+ for (int s = 0; s < 70; s += 7) {
+ if (r->i >= r->n) { r->bad = 1; return 0; }
+ uint8_t b = r->p[r->i++];
+ v |= (uint64_t)(b & 127) << s;
+ if (!(b & 128)) return v;
+ }
+ r->bad = 1;
+ return 0;
+}
+static uint32_t in_u32(In *r) {
+ if (r->n - r->i < 4 || r->i > r->n) { r->bad = 1; return 0; }
+ const uint8_t *b = r->p + r->i;
+ r->i += 4;
+ return (uint32_t)b[0] | (uint32_t)b[1] << 8 | (uint32_t)b[2] << 16 | (uint32_t)b[3] << 24;
+}
+
+/* The next record at *off: 1 with its type and payload, 0 at the end, -1 if it doesn't check out. */
+static int rec_next(const uint8_t *buf, size_t len, size_t *off, int *type, const uint8_t **pl, size_t *pn) {
+ size_t o = *off;
+ if (o == len) return 0;
+ if (len - o < 7) return -1;
+ size_t n = (size_t)buf[o + 1] | (size_t)buf[o + 2] << 8;
+ if (len - o - 7 < n) return -1;
+ const uint8_t *c = buf + o + 3 + n;
+ uint32_t want = (uint32_t)c[0] | (uint32_t)c[1] << 8 | (uint32_t)c[2] << 16 | (uint32_t)c[3] << 24;
+ if (crc32_of(buf + o, 3 + n) != want) return -1;
+ int t = buf[o];
+ if (t != NH_APP && t != NH_MIN && t != NH_HOUR) return -1;
+ *type = t;
+ *pl = buf + o + 3;
+ *pn = n;
+ *off = o + 7 + n;
+ return 1;
+}
+
+/* A minute or hour record: its time, totals and per-app entries (at most max kept in e). Returns the
+ * number of entries, or -1 if the payload is malformed. */
+static int parse_slot(const uint8_t *pl, size_t pn, uint32_t *t, uint64_t *rx, uint64_t *tx, Acc *e, int max) {
+ In r = { pl, pn, 0, 0 };
+ *t = in_u32(&r);
+ *rx = in_var(&r);
+ *tx = in_var(&r);
+ uint64_t n = in_var(&r);
+ if (r.bad || n > NH_APPS_MAX + 1) return -1;
+ int k = 0;
+ for (uint64_t i = 0; i < n; i++) {
+ Acc a;
+ a.idx = (uint32_t)in_var(&r);
+ a.rx = in_var(&r);
+ a.tx = in_var(&r);
+ if (r.bad || a.idx > NH_APPS_MAX) return -1;
+ if (k < max) e[k++] = a;
+ }
+ return r.i == r.n ? k : -1;
+}
+
+size_t nh_check(const uint8_t *buf, size_t len, int *records) {
+ *records = 0;
+ if (len < NH_HDR || memcmp(buf, NH_MAGIC, 4) || buf[4] != NH_VERSION) return len ? 1 : 0;
+ size_t off = NH_HDR;
+ int type, r;
+ const uint8_t *pl;
+ size_t pn;
+ Acc e[NH_APPS_MAX + 1];
+ while ((r = rec_next(buf, len, &off, &type, &pl, &pn)) == 1) {
+ if (type == NH_APP) {
+ In in = { pl, pn, 0, 0 };
+ uint64_t idx = in_var(&in);
+ char id[NH_ID_MAX];
+ size_t k = pn - in.i;
+ if (in.bad || idx == 0 || idx > NH_APPS_MAX || k == 0 || k >= NH_ID_MAX) return off - 7 - pn;
+ memcpy(id, pl + in.i, k);
+ id[k] = 0;
+ if (!valid_id(id)) return off - 7 - pn;
+ } else {
+ uint32_t t;
+ uint64_t a, b;
+ if (parse_slot(pl, pn, &t, &a, &b, e, NH_APPS_MAX + 1) < 0) return off - 7 - pn;
+ }
+ (*records)++;
+ }
+ return r < 0 ? off : 0;
+}
+
+/* The whole file (up to NH_FILE_MAX), checked to be a regular file of this user's. NULL with *len = 0
+ * if it doesn't exist; NULL with *len = 1 if it can't be used. */
+static uint8_t *read_file(const char *path, size_t *len) {
+ *len = 0;
+ int fd = open(path, O_RDONLY | O_NOFOLLOW | O_CLOEXEC | O_NONBLOCK);
+ if (fd < 0) { if (errno != ENOENT) *len = 1; return NULL; }
+ struct stat st;
+ if (fstat(fd, &st) || !S_ISREG(st.st_mode) || st.st_uid != getuid() || st.st_size > (off_t)NH_FILE_MAX) { close(fd); *len = 1; return NULL; }
+ size_t n = (size_t)st.st_size;
+ uint8_t *b = malloc(n ? n : 1);
+ size_t got = 0;
+ while (b && got < n) {
+ ssize_t r = read(fd, b + got, n - got);
+ if (r <= 0) { if (r < 0 && errno == EINTR) continue; break; }
+ got += (size_t)r;
+ }
+ close(fd);
+ if (!b) { *len = 1; return NULL; }
+ *len = got;
+ return b;
+}
+
+/* ---- the app table ---------------------------------------------------------------------------------- */
+
+static int app_grow(NetHist *h, int n) {
+ if (n <= h->napps) return 0;
+ char (*ids)[NH_ID_MAX] = realloc(h->ids, (size_t)n * NH_ID_MAX);
+ if (!ids) return -1;
+ h->ids = ids;
+ uint8_t *w = realloc(h->written, (size_t)n);
+ if (!w) return -1;
+ h->written = w;
+ for (int i = h->napps; i < n; i++) { h->ids[i][0] = 0; h->written[i] = 0; }
+ h->napps = n;
+ return 0;
+}
+
+static int app_index(NetHist *h, const char *app) {
+ for (int i = 1; i < h->napps; i++) if (!strcmp(h->ids[i], app)) return i;
+ if (!valid_id(app) || h->napps > NH_APPS_MAX || app_grow(h, h->napps < 1 ? 2 : h->napps + 1)) return 0; /* "other" */
+ snprintf(h->ids[h->napps - 1], NH_ID_MAX, "%s", app);
+ return h->napps - 1;
+}
+
+/* Reads the app table of the file (and compacts it if its tail is bad). */
+static void load(NetHist *h, long long now) {
+ if (h->loaded) return;
+ h->loaded = 1;
+ if (app_grow(h, 1)) return;
+ size_t len;
+ uint8_t *b = read_file(h->path, &len);
+ if (!b) return;
+ int recs;
+ size_t bad = nh_check(b, len, &recs);
+ if (len >= NH_HDR && !memcmp(b, NH_MAGIC, 4) && b[4] == NH_VERSION) {
+ size_t off = NH_HDR, end = bad ? bad : len;
+ int type;
+ const uint8_t *pl;
+ size_t pn;
+ while (rec_next(b, end, &off, &type, &pl, &pn) == 1) {
+ if (type != NH_APP) continue;
+ In in = { pl, pn, 0, 0 };
+ uint64_t idx = in_var(&in);
+ if (in.bad || !idx || idx > NH_APPS_MAX || app_grow(h, (int)idx + 1)) continue;
+ size_t k = pn - in.i;
+ if (k >= NH_ID_MAX) continue;
+ memcpy(h->ids[idx], pl + in.i, k);
+ h->ids[idx][k] = 0;
+ h->written[idx] = 1;
+ }
+ }
+ free(b);
+ if (bad) nh_compact(h, now);
+}
+
+/* ---- the minute in progress --------------------------------------------------------------------- */
+
+NetHist *nh_new(const char *path) {
+ if (!path || strlen(path) >= PATH_MAX - 8) return NULL;
+ NetHist *h = calloc(1, sizeof *h);
+ if (!h) return NULL;
+ snprintf(h->path, sizeof h->path, "%s", path);
+ /* its folder, 0700, one level at a time from the home folder's own */
+ char dir[PATH_MAX];
+ snprintf(dir, sizeof dir, "%s", path);
+ for (char *p = dir + 1; *p; p++) {
+ if (*p != '/') continue;
+ *p = 0;
+ mkdir(dir, 0700);
+ *p = '/';
+ }
+ return h;
+}
+
+void nh_add(NetHist *h, long long now, const char *app, uint64_t rx, uint64_t tx) {
+ if (!h || (!rx && !tx)) return;
+ load(h, now);
+ long long m = now / 60;
+ if (h->cur && m != h->cur) nh_flush(h, now);
+ if (!h->cur) h->cur = m;
+ if (!app) { h->rx += rx; h->tx += tx; return; }
+ uint32_t idx = (uint32_t)app_index(h, app);
+ for (int i = 0; i < h->nacc; i++)
+ if (h->acc[i].idx == idx) { h->acc[i].rx += rx; h->acc[i].tx += tx; return; }
+ if (h->nacc == h->cacc) {
+ int c = h->cacc ? h->cacc * 2 : 16;
+ Acc *a = realloc(h->acc, (size_t)c * sizeof *a);
+ if (!a) return;
+ h->acc = a;
+ h->cacc = c;
+ }
+ h->acc[h->nacc++] = (Acc){ idx, rx, tx };
+}
+
+static void slot_body(Out *b, uint32_t t, uint64_t rx, uint64_t tx, const Acc *e, int n) {
+ out_u32(b, t);
+ out_var(b, rx);
+ out_var(b, tx);
+ out_var(b, (uint64_t)n);
+ for (int i = 0; i < n; i++) { out_var(b, e[i].idx); out_var(b, e[i].rx); out_var(b, e[i].tx); }
+}
+
+static void app_rec(Out *o, uint32_t idx, const char *id) {
+ Out b = { 0 };
+ out_var(&b, idx);
+ out_bytes(&b, id, strlen(id));
+ out_rec(o, NH_APP, &b);
+ free(b.p);
+}
+
+/* Appends bytes with one write; the file is created with its header if it isn't there. */
+static int append(NetHist *h, const Out *o) {
+ int fd = open(h->path, O_WRONLY | O_APPEND | O_CREAT | O_NOFOLLOW | O_CLOEXEC | O_NONBLOCK, 0600);
+ if (fd < 0) return -1;
+ struct stat st;
+ if (fstat(fd, &st) || !S_ISREG(st.st_mode) || st.st_uid != getuid() || st.st_size > (off_t)NH_FILE_MAX) { close(fd); return -1; }
+ int r = 0;
+ if (st.st_size == 0) {
+ uint8_t hdr[NH_HDR] = { 'L', 'N', 'H', '1', NH_VERSION, 0, 0, 0 };
+ if (write(fd, hdr, sizeof hdr) != (ssize_t)sizeof hdr) r = -1;
+ }
+ if (!r && write(fd, o->p, o->n) != (ssize_t)o->n) r = -1;
+ close(fd);
+ return r;
+}
+
+int nh_flush(NetHist *h, long long now) {
+ if (!h) return 0;
+ load(h, now);
+ int r = 0;
+ if (h->cur && now / 60 != h->cur && (h->rx || h->tx || h->nacc)) {
+ Out o = { 0 }, b = { 0 };
+ /* a new file (cleared, or deleted by hand): every app gets its record again */
+ struct stat st;
+ if (lstat(h->path, &st)) for (int i = 0; i < h->napps; i++) h->written[i] = 0;
+ for (int i = 0; i < h->nacc; i++) {
+ uint32_t x = h->acc[i].idx;
+ if (x && !h->written[x]) { app_rec(&o, x, h->ids[x]); h->written[x] = 1; }
+ }
+ slot_body(&b, (uint32_t)h->cur, h->rx, h->tx, h->acc, h->nacc);
+ out_rec(&o, NH_MIN, &b);
+ r = o.bad ? -1 : append(h, &o);
+ if (r) for (int i = 0; i < h->nacc; i++) h->written[h->acc[i].idx] = 0;
+ free(o.p);
+ free(b.p);
+ h->since_compact++;
+ }
+ if (h->cur && now / 60 != h->cur) { h->cur = 0; h->rx = h->tx = 0; h->nacc = 0; }
+ if (h->since_compact >= NH_COMPACT_EVERY || !h->last_compact) nh_compact(h, now);
+ return r;
+}
+
+void nh_free(NetHist *h) {
+ if (!h) return;
+ if (h->cur) nh_flush(h, (h->cur + 1) * 60); /* the minute in progress, as if it ended */
+ free(h->ids);
+ free(h->written);
+ free(h->acc);
+ free(h);
+}
+
+int nh_clear(NetHist *h) {
+ if (!h) return -1;
+ int r = unlink(h->path) && errno != ENOENT ? -1 : 0;
+ h->cur = 0;
+ h->rx = h->tx = 0;
+ h->nacc = 0;
+ for (int i = 0; i < h->napps; i++) h->written[i] = 0;
+ return r;
+}
+
+/* ---- compaction ------------------------------------------------------------------------------------- */
+
+typedef struct { uint32_t t; uint32_t idx; uint64_t rx, tx; } HEnt; /* idx 0xffffffff: the total */
+#define TOTAL_IDX 0xffffffffu
+
+static int hent_cmp(const void *a, const void *b) {
+ const HEnt *x = a, *y = b;
+ if (x->t != y->t) return x->t < y->t ? -1 : 1;
+ return x->idx < y->idx ? -1 : x->idx > y->idx;
+}
+
+int nh_compact(NetHist *h, long long now) {
+ if (!h) return -1;
+ h->last_compact = now ? now : 1;
+ h->since_compact = 0;
+ size_t len;
+ uint8_t *b = read_file(h->path, &len);
+ if (!b) return len ? -1 : 0;
+ int recs;
+ size_t bad = nh_check(b, len, &recs);
+ if (len < NH_HDR || memcmp(b, NH_MAGIC, 4) || b[4] != NH_VERSION) bad = NH_HDR, len = NH_HDR; /* not ours: start over */
+ size_t end = bad ? bad : len;
+ long long m_cut = now / 60 - NH_MIN_KEEP, h_cut = now / 3600 - NH_HOUR_KEEP, m_max = now / 60 + 24 * 60, h_max = now / 3600 + 24;
+ /* old table → new: only apps that are still used, numbered again from 1 */
+ int nold = h->napps > 1 ? h->napps : 1;
+ char (*fids)[NH_ID_MAX] = calloc((size_t)NH_APPS_MAX + 1, NH_ID_MAX);
+ uint32_t *remap = calloc((size_t)NH_APPS_MAX + 1, sizeof *remap);
+ HEnt *he = NULL;
+ size_t nhe = 0, che = 0;
+ Out o = { 0 }, mins = { 0 };
+ Acc *e = malloc(sizeof(Acc) * (NH_APPS_MAX + 1));
+ int newn = 1, r = -1;
+ if (!fids || !remap || !e) goto out;
+ size_t off = NH_HDR;
+ int type;
+ const uint8_t *pl;
+ size_t pn;
+ /* the file's own ids (they agree with h->ids for the indexes it wrote) */
+ while (rec_next(b, end, &off, &type, &pl, &pn) == 1) {
+ if (type != NH_APP) continue;
+ In in = { pl, pn, 0, 0 };
+ uint64_t idx = in_var(&in);
+ size_t k = pn - in.i;
+ if (in.bad || !idx || idx > NH_APPS_MAX || k >= NH_ID_MAX) continue;
+ memcpy(fids[idx], pl + in.i, k);
+ fids[idx][k] = 0;
+ }
+#define MAP(x) ((x) == 0 || (x) > NH_APPS_MAX || !fids[x][0] ? 0u : remap[x] ? remap[x] : (remap[x] = (uint32_t)newn++))
+ off = NH_HDR;
+ while (rec_next(b, end, &off, &type, &pl, &pn) == 1) {
+ if (type == NH_APP) continue;
+ uint32_t t;
+ uint64_t rx, tx;
+ int n = parse_slot(pl, pn, &t, &rx, &tx, e, NH_APPS_MAX + 1);
+ if (n < 0) continue;
+ if (type == NH_MIN && (long long)t >= m_cut && (long long)t <= m_max) {
+ for (int i = 0; i < n; i++) e[i].idx = MAP(e[i].idx);
+ Out bd = { 0 };
+ slot_body(&bd, t, rx, tx, e, n);
+ out_rec(&mins, NH_MIN, &bd);
+ free(bd.p);
+ continue;
+ }
+ uint32_t hour = type == NH_MIN ? t / 60 : t;
+ if ((long long)hour < h_cut || (long long)hour > h_max) continue;
+ if (nhe + (size_t)n + 1 > che) {
+ size_t c = che ? che * 2 : 1024;
+ while (c < nhe + (size_t)n + 1) c *= 2;
+ HEnt *q = realloc(he, c * sizeof *q);
+ if (!q) goto out;
+ he = q;
+ che = c;
+ }
+ he[nhe++] = (HEnt){ hour, TOTAL_IDX, rx, tx };
+ for (int i = 0; i < n; i++) he[nhe++] = (HEnt){ hour, MAP(e[i].idx), e[i].rx, e[i].tx };
+ }
+#undef MAP
+ if (mins.bad) goto out;
+ /* the new file: header, apps, minutes, hours (one record per hour, entries summed) */
+ uint8_t hdr[NH_HDR] = { 'L', 'N', 'H', '1', NH_VERSION, 0, 0, 0 };
+ out_bytes(&o, hdr, sizeof hdr);
+ for (int x = 1; x <= NH_APPS_MAX; x++) if (remap[x]) app_rec(&o, remap[x], fids[x]);
+ out_bytes(&o, mins.p ? mins.p : (const uint8_t *)"", mins.n);
+ if (nhe) qsort(he, nhe, sizeof *he, hent_cmp);
+ for (size_t i = 0; i < nhe;) {
+ uint32_t t = he[i].t;
+ uint64_t trx = 0, ttx = 0;
+ int k = 0;
+ for (; i < nhe && he[i].t == t; i++) {
+ if (he[i].idx == TOTAL_IDX) { trx += he[i].rx; ttx += he[i].tx; continue; }
+ if (k && e[k - 1].idx == he[i].idx) { e[k - 1].rx += he[i].rx; e[k - 1].tx += he[i].tx; }
+ else if (k <= NH_APPS_MAX) e[k++] = (Acc){ he[i].idx, he[i].rx, he[i].tx };
+ }
+ Out bd = { 0 };
+ slot_body(&bd, t, trx, ttx, e, k);
+ out_rec(&o, NH_HOUR, &bd);
+ free(bd.p);
+ }
+ if (o.bad) goto out;
+ /* atomic: a temporary file next to it, then rename */
+ char tmp[PATH_MAX + 8];
+ snprintf(tmp, sizeof tmp, "%s.tmp", h->path);
+ unlink(tmp);
+ int fd = open(tmp, O_WRONLY | O_CREAT | O_EXCL | O_NOFOLLOW | O_CLOEXEC, 0600);
+ if (fd < 0) goto out;
+ size_t done = 0;
+ while (done < o.n) {
+ ssize_t w = write(fd, o.p + done, o.n - done);
+ if (w <= 0) { if (w < 0 && errno == EINTR) continue; break; }
+ done += (size_t)w;
+ }
+ int ok = done == o.n && !fsync(fd);
+ close(fd);
+ if (!ok || rename(tmp, h->path)) { unlink(tmp); goto out; }
+ /* the table in memory follows the file; apps only in the minute in progress are added after */
+ {
+ char (*ids)[NH_ID_MAX] = calloc((size_t)newn + 1, NH_ID_MAX);
+ uint8_t *wr = calloc((size_t)newn + 1, 1);
+ if (!ids || !wr) { free(ids); free(wr); goto out; }
+ for (int x = 1; x <= NH_APPS_MAX; x++) if (remap[x]) { snprintf(ids[remap[x]], NH_ID_MAX, "%s", fids[x]); wr[remap[x]] = 1; }
+ char (*oldids)[NH_ID_MAX] = h->ids;
+ int oldn = h->napps;
+ h->ids = ids;
+ h->written = (free(h->written), wr);
+ h->napps = newn;
+ for (int i = 0; i < h->nacc; i++) {
+ uint32_t x = h->acc[i].idx;
+ h->acc[i].idx = x && (int)x < oldn && oldids[x][0] ? (uint32_t)app_index(h, oldids[x]) : 0;
+ }
+ free(oldids);
+ (void)nold;
+ }
+ r = 0;
+out:
+ free(b);
+ free(fids);
+ free(remap);
+ free(he);
+ free(e);
+ free(o.p);
+ free(mins.p);
+ return r;
+}
+
+/* ---- queries ------------------------------------------------------------------------------------------ */
+
+static void spread(double *rx, double *tx, long long from, long long step, int n, long long s, long long dur, uint64_t r, uint64_t t) {
+ long long a = s, b = s + dur, end = from + step * n;
+ if (b <= from || a >= end) return;
+ long long i0 = (a > from ? a - from : 0) / step, i1 = ((b < end ? b : end) - from - 1) / step;
+ for (long long i = i0; i <= i1 && i < n; i++) {
+ long long x0 = from + i * step, x1 = x0 + step;
+ if (x0 < a) x0 = a;
+ if (x1 > b) x1 = b;
+ if (x1 <= x0) continue;
+ double f = (double)(x1 - x0) / (double)dur;
+ rx[i] += (double)r * f;
+ tx[i] += (double)t * f;
+ }
+}
+
+int nh_query(NetHist *h, long long now, long long from, long long step, int n, const char *app,
+ double *rx, double *tx, NhTop *top, int max_top, int *ntop, long long *oldest) {
+ if (!h || n < 1 || n > NH_BUCKETS_MAX || step < 1 || step > 86400LL * 31 || from < 0) return -1;
+ load(h, now);
+ for (int i = 0; i < n; i++) rx[i] = tx[i] = 0;
+ if (ntop) *ntop = 0;
+ if (oldest) *oldest = 0;
+ long long end = from + step * n;
+ int res = 0;
+ char (*fids)[NH_ID_MAX] = calloc((size_t)NH_APPS_MAX + 1, NH_ID_MAX);
+ uint64_t *sum = calloc((size_t)(NH_APPS_MAX + 1) * 2, sizeof *sum);
+ Acc *e = malloc(sizeof(Acc) * (NH_APPS_MAX + 1));
+ size_t len;
+ uint8_t *b = read_file(h->path, &len);
+ if (!fids || !sum || !e) goto done;
+ if (b && len >= NH_HDR && !memcmp(b, NH_MAGIC, 4) && b[4] == NH_VERSION) {
+ size_t off = NH_HDR;
+ int type;
+ const uint8_t *pl;
+ size_t pn;
+ while (rec_next(b, len, &off, &type, &pl, &pn) == 1) {
+ if (type == NH_APP) {
+ In in = { pl, pn, 0, 0 };
+ uint64_t idx = in_var(&in);
+ size_t k = pn - in.i;
+ if (in.bad || !idx || idx > NH_APPS_MAX || k >= NH_ID_MAX) continue;
+ memcpy(fids[idx], pl + in.i, k);
+ fids[idx][k] = 0;
+ continue;
+ }
+ uint32_t t;
+ uint64_t trx, ttx;
+ int k = parse_slot(pl, pn, &t, &trx, &ttx, e, NH_APPS_MAX + 1);
+ if (k < 0) continue;
+ long long dur = type == NH_MIN ? 60 : 3600, s = (long long)t * dur;
+ if (oldest && (!*oldest || s < *oldest)) *oldest = s;
+ if (s + dur <= from || s >= end) continue;
+ if (!res || dur < res) res = (int)dur;
+ if (!app) spread(rx, tx, from, step, n, s, dur, trx, ttx);
+ for (int i = 0; i < k; i++) {
+ uint32_t x = e[i].idx;
+ if (x > NH_APPS_MAX) continue;
+ sum[x * 2] += e[i].rx;
+ sum[x * 2 + 1] += e[i].tx;
+ if (app && x && !strcmp(fids[x], app)) spread(rx, tx, from, step, n, s, dur, e[i].rx, e[i].tx);
+ }
+ }
+ }
+ /* the minute in progress (not in the file yet), with the table in memory */
+ if (h->cur && h->cur * 60 + 60 > from && h->cur * 60 < end) {
+ if (!res) res = 60;
+ if (oldest && (!*oldest || h->cur * 60 < *oldest)) *oldest = h->cur * 60;
+ if (!app) spread(rx, tx, from, step, n, h->cur * 60, 60, h->rx, h->tx);
+ for (int i = 0; i < h->nacc; i++) {
+ uint32_t x = h->acc[i].idx;
+ if (!x || (int)x >= h->napps) continue;
+ /* the same id may have another index in the file: add it to that one */
+ uint32_t fx = 0;
+ for (uint32_t j = 1; j <= NH_APPS_MAX && !fx; j++) if (fids[j][0] && !strcmp(fids[j], h->ids[x])) fx = j;
+ if (!fx) for (uint32_t j = NH_APPS_MAX; j >= 1 && !fx; j--) if (!fids[j][0]) { fx = j; snprintf(fids[j], NH_ID_MAX, "%s", h->ids[x]); }
+ if (!fx) continue;
+ sum[fx * 2] += h->acc[i].rx;
+ sum[fx * 2 + 1] += h->acc[i].tx;
+ if (app && !strcmp(h->ids[x], app)) spread(rx, tx, from, step, n, h->cur * 60, 60, h->acc[i].rx, h->acc[i].tx);
+ }
+ }
+ if (top && ntop && max_top > 0) {
+ for (int k = 0; k < max_top; k++) {
+ int best = -1;
+ uint64_t bv = 0;
+ for (int x = 1; x <= NH_APPS_MAX; x++) {
+ uint64_t v = sum[x * 2] + sum[x * 2 + 1];
+ if (v > bv && fids[x][0]) { bv = v; best = x; }
+ }
+ if (best < 0) break;
+ snprintf(top[k].id, NH_ID_MAX, "%s", fids[best]);
+ top[k].rx = sum[best * 2];
+ top[k].tx = sum[best * 2 + 1];
+ sum[best * 2] = sum[best * 2 + 1] = 0;
+ (*ntop)++;
+ }
+ }
+done:
+ free(b);
+ free(fids);
+ free(sum);
+ free(e);
+ return res;
+}
diff --git a/server/src/monitor/nethist.h b/server/src/monitor/nethist.h
new file mode 100644
index 0000000..f8b9bcc
--- /dev/null
+++ b/server/src/monitor/nethist.h
@@ -0,0 +1,61 @@
+/* nethist.h — the Monitor's traffic history (0.8.2, docs/MONITOR.md "Traffic history").
+ *
+ * Bytes received and sent per minute, for the machine (every interface but lo, as the Monitor's
+ * totals) and for each of the user's apps (lord-netperm's per-app counters), kept 7 days per minute
+ * and 30 days per hour in one compact file of the user's:
+ *
+ * $XDG_STATE_HOME/lordos/net-history.bin (default ~/.local/state/lordos/net-history.bin, 0600)
+ *
+ * The apps are the user's own, so the file is the user's (lord-server writes it as the user; no
+ * root process writes into a home folder). It holds app ids and byte counts only: never an address,
+ * a domain or a country.
+ *
+ * File: "LNH1" + u32 version, then records [u8 type][u16 len][payload][u32 crc32]. A record that
+ * doesn't check out (a crash in the middle of a write, a bad disk) ends the file: what came before it
+ * is kept and the next compaction drops the rest. Payloads use LEB128 varints:
+ * NH_APP varint index, the app id's bytes (1..127 of [A-Za-z0-9._:@+~/-])
+ * NH_MIN u32 minute (Unix time / 60), varint rx, varint tx, varint n, n × (index, rx, tx)
+ * NH_HOUR u32 hour (Unix time / 3600), the same
+ * A minute is appended when it ends (one write of a few dozen bytes); compaction folds minutes older
+ * than 7 days into hours and drops hours older than 30 days (at startup and every 6 hours). */
+#ifndef LORD_NETHIST_H
+#define LORD_NETHIST_H
+
+#include <stddef.h>
+#include <stdint.h>
+
+#define NH_APPS_MAX 1024 /* app ids per file; later ones go to the "other" slot */
+#define NH_ID_MAX 128
+#define NH_MIN_KEEP (7 * 24 * 60) /* minutes */
+#define NH_HOUR_KEEP (30 * 24) /* hours */
+#define NH_FILE_MAX (8u << 20) /* a bigger file is not read (it can't be ours) */
+#define NH_BUCKETS_MAX 1500
+
+typedef struct NetHist NetHist;
+
+/* A history for path (its folder is created 0700). Nothing is read until the first call that needs it. */
+NetHist *nh_new(const char *path);
+void nh_free(NetHist *h); /* writes the minute in progress first */
+
+/* Bytes moved since the last call, at Unix time now: app NULL = the machine's total. */
+void nh_add(NetHist *h, long long now, const char *app, uint64_t rx, uint64_t tx);
+/* Writes the minutes that ended before now; compacts when due. Returns 0, or -1 on a write error. */
+int nh_flush(NetHist *h, long long now);
+/* Folds old minutes into hours, drops what is past the limits and any bad tail (atomic rewrite). */
+int nh_compact(NetHist *h, long long now);
+/* Deletes everything (the file and what is in memory). */
+int nh_clear(NetHist *h);
+
+/* Buckets [from + i*step, from + (i+1)*step) for i < n: received and sent bytes of app (NULL: the
+ * machine). A record longer than a bucket (an hour) is spread over the buckets it covers. Also the
+ * apps with the most traffic over the range (up to max_top, by rx + tx) and the oldest time
+ * recorded. Returns the finest resolution found in the range (60 or 3600 s), 0 with no data, -1 on
+ * bad arguments. Reads the file; the minute in progress is included. */
+typedef struct { char id[NH_ID_MAX]; uint64_t rx, tx; } NhTop;
+int nh_query(NetHist *h, long long now, long long from, long long step, int n, const char *app,
+ double *rx, double *tx, NhTop *top, int max_top, int *ntop, long long *oldest);
+
+/* For tests: the records of a file buffer (0 = all good, otherwise the offset where it stopped). */
+size_t nh_check(const uint8_t *buf, size_t len, int *records);
+
+#endif
diff --git a/server/src/monitor/netrec.c b/server/src/monitor/netrec.c
new file mode 100644
index 0000000..033d93e
--- /dev/null
+++ b/server/src/monitor/netrec.c
@@ -0,0 +1,187 @@
+/* netrec.c — feeds the traffic history (see netrec.h). Costs one read of /proc/net/dev and a look at
+ * the latest lord-netperm counters every 5 s, plus one append of a few dozen bytes per minute. */
+#include "netrec.h"
+#include "monpriv.h"
+#include "../ui/js_fw.h"
+
+#include <limits.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <time.h>
+
+#define TICK_MS 5000
+#define PREV_MAX 512
+#define IF_MAX 32
+
+static int allowed, on;
+static NetHist *hist;
+static double next_ms;
+static int primed; /* a first reading was taken: new entries count whole */
+static uint64_t counts_seq;
+
+/* the previous reading of each app entry (an app may have several scopes: by occurrence) */
+static struct { char app[NH_ID_MAX]; int occ; uint64_t rx, tx; } prev[PREV_MAX], cur[PREV_MAX];
+static int nprev, ncur;
+static struct { char name[32]; uint64_t rx, tx; } ifprev[IF_MAX];
+static int nifprev, if_primed;
+
+static double mono_ms(void) {
+ struct timespec t;
+ clock_gettime(CLOCK_MONOTONIC, &t);
+ return t.tv_sec * 1e3 + t.tv_nsec / 1e6;
+}
+
+long long netrec_now(void) { return (long long)time(NULL); }
+
+static void path_of(char *out, size_t n) {
+ const char *x = getenv("XDG_STATE_HOME"), *home = getenv("HOME");
+ out[0] = 0;
+ if (x && x[0] == '/' && !strstr(x, "/../")) snprintf(out, n, "%s/lordos/net-history.bin", x);
+ else if (home && home[0] == '/') snprintf(out, n, "%s/.local/state/lordos/net-history.bin", home);
+}
+
+void netrec_init(int ok) {
+ allowed = ok;
+ if (!ok || hist) return;
+ char p[PATH_MAX];
+ path_of(p, sizeof p);
+ if (p[0]) hist = nh_new(p);
+}
+
+NetHist *netrec_hist(void) { return allowed ? hist : NULL; }
+int netrec_enabled(void) { return allowed && on && hist; }
+
+void netrec_enable(int v) {
+ if (!allowed || !hist || !!v == on) return;
+ on = !!v;
+ if (on) { primed = 0; if_primed = 0; nprev = 0; nifprev = 0; next_ms = mono_ms() + 1000; }
+ else nh_flush(hist, netrec_now() + 60); /* what was counted until now */
+}
+
+double netrec_timeout(void) {
+ if (!netrec_enabled()) return -1;
+ double d = next_ms - mono_ms();
+ return d < 0 ? 0 : d;
+}
+
+/* {"app":[allowed,denied,rx,tx,...,[[...]]],...} from lord-netperm (npproto.h). Only the key and the
+ * 3rd and 4th numbers are taken; anything unexpected ends the reading (what was read stays). */
+static void read_counts(const char *j) {
+ ncur = 0;
+ const char *p = j;
+ while (*p == ' ') p++;
+ if (*p++ != '{') return;
+ for (;;) {
+ while (*p == ' ' || *p == ',') p++;
+ if (*p != '"') return;
+ const char *k = ++p;
+ while (*p && *p != '"' && *p != '\\') p++;
+ if (*p != '"' || p - k <= 0 || p - k >= NH_ID_MAX) return;
+ char app[NH_ID_MAX];
+ memcpy(app, k, (size_t)(p - k));
+ app[p - k] = 0;
+ p++;
+ if (*p++ != ':' || *p++ != '[') return;
+ unsigned long long v[4] = { 0 };
+ for (int i = 0; i < 4; i++) {
+ char *e;
+ v[i] = strtoull(p, &e, 10);
+ if (e == p) return;
+ p = e;
+ if (*p == ',') p++;
+ }
+ /* the rest of this entry: up to its closing bracket (strings hold addresses, no brackets) */
+ int depth = 1;
+ while (*p && depth) {
+ if (*p == '"') { p++; while (*p && *p != '"') { if (*p == '\\' && p[1]) p++; p++; } if (!*p) return; }
+ else if (*p == '[') depth++;
+ else if (*p == ']') depth--;
+ p++;
+ }
+ if (depth) return;
+ if (ncur < PREV_MAX) {
+ int occ = 0;
+ for (int i = 0; i < ncur; i++) occ += !strcmp(cur[i].app, app);
+ snprintf(cur[ncur].app, NH_ID_MAX, "%s", app);
+ cur[ncur].occ = occ;
+ cur[ncur].rx = v[2];
+ cur[ncur].tx = v[3];
+ ncur++;
+ }
+ while (*p == ' ') p++;
+ if (*p == '}') return;
+ }
+}
+
+static uint64_t delta(uint64_t now, uint64_t before) { return now >= before ? now - before : now; } /* restarted: from 0 */
+
+static void apps_tick(long long now) {
+ uint64_t seq = counts_seq;
+ const char *j = js_fw_counts(&seq);
+ if (!j || seq == counts_seq) return; /* nothing new: nothing moved */
+ counts_seq = seq;
+ read_counts(j);
+ for (int i = 0; i < ncur; i++) {
+ int f = -1;
+ for (int k = 0; k < nprev && f < 0; k++) if (prev[k].occ == cur[i].occ && !strcmp(prev[k].app, cur[i].app)) f = k;
+ if (f < 0 && !primed) continue; /* the first reading is only a starting point */
+ uint64_t rx = f >= 0 ? delta(cur[i].rx, prev[f].rx) : cur[i].rx, tx = f >= 0 ? delta(cur[i].tx, prev[f].tx) : cur[i].tx;
+ nh_add(hist, now, cur[i].app, rx, tx);
+ }
+ memcpy(prev, cur, sizeof cur[0] * (size_t)ncur);
+ nprev = ncur;
+ primed = 1;
+}
+
+/* The machine: every interface but lo, as the Monitor's totals (mon.c). */
+static void total_tick(long long now) {
+ char txt[8192];
+ if (mon_read_small("/proc/net/dev", txt, sizeof txt) <= 0) return;
+ struct { char name[32]; uint64_t rx, tx; } c[IF_MAX];
+ int n = 0;
+ for (char *l = txt; l && n < IF_MAX; l = strchr(l, '\n') ? strchr(l, '\n') + 1 : NULL) {
+ char *colon = strchr(l, ':'), *nl = strchr(l, '\n');
+ if (!colon || (nl && colon > nl)) continue;
+ char *s = l;
+ while (*s == ' ') s++;
+ if (colon - s <= 0 || colon - s >= 32) continue;
+ snprintf(c[n].name, sizeof c[n].name, "%.*s", (int)(colon - s), s);
+ unsigned long long v[16];
+ char *p = colon + 1;
+ for (int i = 0; i < 16; i++) v[i] = strtoull(p, &p, 10);
+ c[n].rx = v[0];
+ c[n].tx = v[8];
+ n++;
+ }
+ uint64_t rx = 0, tx = 0;
+ for (int i = 0; i < n; i++) {
+ if (!strcmp(c[i].name, "lo")) continue;
+ int f = -1;
+ for (int k = 0; k < nifprev && f < 0; k++) if (!strcmp(ifprev[k].name, c[i].name)) f = k;
+ if (f < 0 && !if_primed) continue;
+ rx += f >= 0 ? delta(c[i].rx, ifprev[f].rx) : c[i].rx;
+ tx += f >= 0 ? delta(c[i].tx, ifprev[f].tx) : c[i].tx;
+ }
+ for (int i = 0; i < n; i++) { memcpy(ifprev[i].name, c[i].name, sizeof ifprev[i].name); ifprev[i].rx = c[i].rx; ifprev[i].tx = c[i].tx; }
+ nifprev = n;
+ if_primed = 1;
+ nh_add(hist, now, NULL, rx, tx);
+}
+
+void netrec_tick(void) {
+ if (!netrec_enabled()) return;
+ double t = mono_ms();
+ if (t < next_ms) return;
+ next_ms = t + TICK_MS;
+ long long now = netrec_now();
+ total_tick(now);
+ apps_tick(now);
+ nh_flush(hist, now);
+}
+
+void netrec_shutdown(void) {
+ if (hist && on) nh_flush(hist, netrec_now() + 60);
+ nh_free(hist);
+ hist = NULL;
+}
diff --git a/server/src/monitor/netrec.h b/server/src/monitor/netrec.h
new file mode 100644
index 0000000..879fccd
--- /dev/null
+++ b/server/src/monitor/netrec.h
@@ -0,0 +1,20 @@
+/* netrec.h — records the traffic history (nethist.h) from what lord-server already reads: the
+ * per-app counters lord-netperm sends (js_fw.c) and /proc/net/dev, every 5 s. See docs/MONITOR.md,
+ * "Traffic history". */
+#ifndef LORD_NETREC_H
+#define LORD_NETREC_H
+
+#include "nethist.h"
+
+/* allowed: a user session (not the login screen, not an isolated app). Until then nothing runs. */
+void netrec_init(int allowed);
+/* Settings → Privacy → Monitor → "Keep a traffic history" (the SDK calls it with the setting). */
+void netrec_enable(int on);
+int netrec_enabled(void);
+void netrec_tick(void); /* from the main loop; does something every 5 s */
+double netrec_timeout(void); /* ms until the next tick, -1 when off */
+NetHist *netrec_hist(void); /* NULL when not allowed */
+long long netrec_now(void); /* Unix time */
+void netrec_shutdown(void); /* writes the minute in progress */
+
+#endif
diff --git a/server/src/ui/js_fw.c b/server/src/ui/js_fw.c
index 23876c6..b4f5265 100644
--- a/server/src/ui/js_fw.c
+++ b/server/src/ui/js_fw.c
@@ -60,6 +60,7 @@ static long long last_try;
static unsigned char *inbuf;
static size_t inlen;
static char *status_json, *counts_json;
+static uint64_t counts_gen; /* goes up with every COUNTS notice (netrec.c) */
static char *events[MAX_EVENTS];
static int nevents;
@@ -216,7 +217,7 @@ static void pump(void) {
const unsigned char *body = h + 5;
size_t bl = l - 1;
if (h[4] == NP_MSG_STATUS) keep(&status_json, body, bl);
- else if (h[4] == NP_MSG_COUNTS) keep(&counts_json, body, bl);
+ else if (h[4] == NP_MSG_COUNTS) { keep(&counts_json, body, bl); counts_gen++; }
else if (h[4] == NP_MSG_EVENT) {
match_event(body, bl);
if (nevents == MAX_EVENTS) { free(events[0]); memmove(events, events + 1, (MAX_EVENTS - 1) * sizeof *events); nevents--; }
@@ -597,6 +598,13 @@ static JSValue f_poll(JSContext *c, JSValueConst t, int argc, JSValueConst *argv
return o;
}
+/* The latest COUNTS notice for the traffic history (monitor/netrec.c), after reading what arrived. */
+const char *js_fw_counts(uint64_t *seq) {
+ pump();
+ *seq = counts_gen;
+ return counts_json;
+}
+
static const JSCFunctionListEntry fw_funcs[] = {
JS_CFUNC_DEF("status", 0, f_status),
JS_CFUNC_DEF("state", 0, f_state),
diff --git a/server/src/ui/js_fw.h b/server/src/ui/js_fw.h
index f299691..caec789 100644
--- a/server/src/ui/js_fw.h
+++ b/server/src/ui/js_fw.h
@@ -3,7 +3,10 @@
#define LORD_JS_FW_H
#include "quickjs.h"
+#include <stdint.h>
void js_fw_register(JSContext *c, JSValue lord);
+/* the latest per-app counters from lord-netperm (its JSON, or NULL) and a number that changes with them */
+const char *js_fw_counts(uint64_t *seq);
#endif
diff --git a/server/src/ui/js_monitor.c b/server/src/ui/js_monitor.c
index ba3736a..edb7284 100644
--- a/server/src/ui/js_monitor.c
+++ b/server/src/ui/js_monitor.c
@@ -19,11 +19,17 @@
* an error code (proc.gone, proc.protected…, proc.notOwn, proc.denied).
* Other users' processes go through the root helper (proc-signal,
* administrators only; sdk/monitor.js).
+ * historyEnable(on) Settings → Privacy → Monitor → "Keep a traffic history" (0.8.2)
+ * history({ from, step, n, app }) bytes per bucket from the history file (monitor/nethist.c):
+ * { res, oldest, now, rx: [n], tx: [n], top: [[app, rx, tx]] } or null;
+ * from/step in Unix seconds, n ≤ 1500, app an app id or null (the machine)
+ * historyClear() deletes the history; true if it is gone
*/
#include "js_monitor.h"
#include "../monitor/mon.h"
#include "../monitor/map.h"
#include "../monitor/procctl.h"
+#include "../monitor/netrec.h"
#include "../apps.h"
#include "js_sys.h"
#include "js_err.h"
@@ -363,6 +369,77 @@ static JSValue f_signal(JSContext *c, JSValueConst t, int argc, JSValueConst *ar
return e ? JS_NewString(c, e) : JS_NULL;
}
+/* ---- traffic history (0.8.2) ---- */
+
+static JSValue f_history_enable(JSContext *c, JSValueConst t, int argc, JSValueConst *argv) {
+ (void)t;
+ netrec_enable(argc > 0 && JS_ToBool(c, argv[0]) > 0);
+ return JS_NewBool(c, netrec_enabled());
+}
+
+static int int_prop(JSContext *c, JSValueConst o, const char *k, double lo, double hi, long long *out) {
+ JSValue v = JS_GetPropertyStr(c, o, k);
+ double d;
+ int bad = !JS_IsNumber(v) || JS_ToFloat64(c, &d, v) || !isfinite(d) || d != floor(d) || d < lo || d > hi;
+ JS_FreeValue(c, v);
+ if (!bad) *out = (long long)d;
+ return bad ? -1 : 0;
+}
+
+static JSValue f_history(JSContext *c, JSValueConst t, int argc, JSValueConst *argv) {
+ (void)t;
+ NetHist *h = netrec_hist();
+ if (!h || argc < 1 || !JS_IsObject(argv[0])) return JS_NULL;
+ long long from, step, n;
+ if (int_prop(c, argv[0], "from", 0, 4102444800.0, &from) || int_prop(c, argv[0], "step", 60, 86400.0 * 31, &step) ||
+ int_prop(c, argv[0], "n", 1, NH_BUCKETS_MAX, &n)) return JS_NULL;
+ char app[NH_ID_MAX] = "";
+ JSValue a = JS_GetPropertyStr(c, argv[0], "app");
+ if (JS_IsString(a)) {
+ const char *s = JS_ToCString(c, a);
+ int ok = s && strlen(s) > 0 && strlen(s) < NH_ID_MAX;
+ for (const char *q = s; ok && *q; q++) ok = isalnum((unsigned char)*q) || strchr("._:@+~/-", *q);
+ if (ok) snprintf(app, sizeof app, "%s", s);
+ if (s) JS_FreeCString(c, s);
+ if (!ok) { JS_FreeValue(c, a); return JS_NULL; }
+ } else if (!JS_IsUndefined(a) && !JS_IsNull(a)) { JS_FreeValue(c, a); return JS_NULL; }
+ JS_FreeValue(c, a);
+ double *rx = malloc(sizeof(double) * (size_t)n), *tx = malloc(sizeof(double) * (size_t)n);
+ NhTop top[12];
+ int ntop = 0;
+ long long oldest = 0, now = netrec_now();
+ int res = rx && tx ? nh_query(h, now, from, step, (int)n, app[0] ? app : NULL, rx, tx, top, 12, &ntop, &oldest) : -1;
+ if (res < 0) { free(rx); free(tx); return JS_NULL; }
+ JSValue o = JS_NewObject(c), arx = JS_NewArray(c), atx = JS_NewArray(c), atop = JS_NewArray(c);
+ for (int i = 0; i < n; i++) {
+ JS_SetPropertyUint32(c, arx, (uint32_t)i, JS_NewFloat64(c, round(rx[i])));
+ JS_SetPropertyUint32(c, atx, (uint32_t)i, JS_NewFloat64(c, round(tx[i])));
+ }
+ for (int i = 0; i < ntop; i++) {
+ JSValue e = JS_NewArray(c);
+ JS_SetPropertyUint32(c, e, 0, JS_NewString(c, top[i].id));
+ JS_SetPropertyUint32(c, e, 1, JS_NewFloat64(c, (double)top[i].rx));
+ JS_SetPropertyUint32(c, e, 2, JS_NewFloat64(c, (double)top[i].tx));
+ JS_SetPropertyUint32(c, atop, (uint32_t)i, e);
+ }
+ JS_SetPropertyStr(c, o, "res", JS_NewInt32(c, res));
+ JS_SetPropertyStr(c, o, "oldest", JS_NewFloat64(c, (double)oldest));
+ JS_SetPropertyStr(c, o, "now", JS_NewFloat64(c, (double)now));
+ JS_SetPropertyStr(c, o, "enabled", JS_NewBool(c, netrec_enabled()));
+ JS_SetPropertyStr(c, o, "rx", arx);
+ JS_SetPropertyStr(c, o, "tx", atx);
+ JS_SetPropertyStr(c, o, "top", atop);
+ free(rx);
+ free(tx);
+ return o;
+}
+
+static JSValue f_history_clear(JSContext *c, JSValueConst t, int argc, JSValueConst *argv) {
+ (void)t; (void)argc; (void)argv;
+ NetHist *h = netrec_hist();
+ return JS_NewBool(c, h && nh_clear(h) == 0);
+}
+
static const JSCFunctionListEntry monitor_funcs[] = {
JS_CFUNC_DEF("start", 0, f_start),
JS_CFUNC_DEF("stop", 0, f_stop),
@@ -376,6 +453,9 @@ static const JSCFunctionListEntry monitor_funcs[] = {
JS_CFUNC_DEF("detail", 2, f_detail),
JS_CFUNC_DEF("detailInfo", 0, f_detail_info),
JS_CFUNC_DEF("signal", 3, f_signal),
+ JS_CFUNC_DEF("historyEnable", 1, f_history_enable),
+ JS_CFUNC_DEF("history", 1, f_history),
+ JS_CFUNC_DEF("historyClear", 0, f_history_clear),
};
void js_monitor_register(JSContext *c, JSValue lord, const char *assets_dir) {
diff --git a/server/tests/nethist_test.c b/server/tests/nethist_test.c
new file mode 100644
index 0000000..b804998
--- /dev/null
+++ b/server/tests/nethist_test.c
@@ -0,0 +1,253 @@
+/* nethist_test.c — the Monitor's traffic history file (src/monitor/nethist.c, docs/MONITOR.md
+ * "Traffic history"): totals survive writing and reading, minutes older than 7 days become hours,
+ * hours older than 30 days go, a cut or damaged file never crashes and keeps what came before the
+ * damage, too many apps go to "other", and Clear history removes everything.
+ * build/nethist-test */
+#include "monitor/nethist.h"
+
+#include <math.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/stat.h>
+#include <unistd.h>
+
+static int checks, fails;
+#define CHECK(c, ...) do { checks++; if (!(c)) { fails++; printf("FAIL %s:%d: ", __FILE__, __LINE__); printf(__VA_ARGS__); printf("\n"); } } while (0)
+
+static char dir[256], path[300];
+
+static uint8_t *slurp(size_t *n) {
+ FILE *f = fopen(path, "rb");
+ *n = 0;
+ if (!f) return NULL;
+ uint8_t *b = malloc(NH_FILE_MAX);
+ *n = fread(b, 1, NH_FILE_MAX, f);
+ fclose(f);
+ return b;
+}
+static void spit(const uint8_t *b, size_t n) {
+ FILE *f = fopen(path, "wb");
+ if (!f) return;
+ fwrite(b, 1, n, f);
+ fclose(f);
+}
+
+static double sum(const double *v, int n) { double s = 0; for (int i = 0; i < n; i++) s += v[i]; return s; }
+
+/* Totals of everything in [from, from + n*step) for app (NULL: the machine). */
+static void totals(NetHist *h, long long now, long long from, long long step, int n, const char *app, double *rx, double *tx, int *res) {
+ double *a = calloc((size_t)n, sizeof *a), *b = calloc((size_t)n, sizeof *b);
+ NhTop top[4];
+ int nt;
+ long long old;
+ *res = nh_query(h, now, from, step, n, app, a, b, top, 4, &nt, &old);
+ *rx = sum(a, n);
+ *tx = sum(b, n);
+ free(a);
+ free(b);
+}
+
+static void test_roundtrip(void) {
+ unlink(path);
+ long long t0 = 1760000000LL - 1760000000LL % 3600; /* on an hour */
+ NetHist *h = nh_new(path);
+ for (int m = 0; m < 90; m++) { /* 90 minutes, 3 apps */
+ long long now = t0 + m * 60 + 5;
+ nh_add(h, now, NULL, 1000, 100);
+ nh_add(h, now, "desktop:firefox-esr", 600, 60);
+ nh_add(h, now, "lpk:fractal", m % 2 ? 300 : 0, 30);
+ nh_add(h, now, "lordos", 10, 1);
+ nh_flush(h, now + 50);
+ }
+ double rx, tx;
+ int res;
+ totals(h, t0 + 90 * 60, t0, 60, 90, NULL, &rx, &tx, &res);
+ CHECK(rx == 90000 && tx == 9000 && res == 60, "machine: %.0f/%.0f res %d", rx, tx, res);
+ totals(h, t0 + 90 * 60, t0, 60, 90, "desktop:firefox-esr", &rx, &tx, &res);
+ CHECK(rx == 54000 && tx == 5400, "app: %.0f/%.0f", rx, tx);
+ totals(h, t0 + 90 * 60, t0, 600, 9, "lpk:fractal", &rx, &tx, &res);
+ CHECK(rx == 45 * 300 && tx == 90 * 30, "bigger buckets: %.0f/%.0f", rx, tx);
+ /* top apps */
+ double a[3], b[3];
+ NhTop top[4];
+ int nt;
+ long long old;
+ nh_query(h, t0 + 90 * 60, t0, 1800, 3, NULL, a, b, top, 4, &nt, &old);
+ CHECK(nt == 3 && !strcmp(top[0].id, "desktop:firefox-esr") && !strcmp(top[1].id, "lpk:fractal") && old == t0, "top: %d %s, oldest %lld", nt, nt ? top[0].id : "", old - t0);
+ nh_free(h);
+ /* reopened: the same */
+ h = nh_new(path);
+ totals(h, t0 + 90 * 60, t0, 60, 90, NULL, &rx, &tx, &res);
+ CHECK(rx == 90000 && tx == 9000, "after reopening: %.0f/%.0f", rx, tx);
+ /* the file is small: a minute with 3 apps is a few dozen bytes */
+ struct stat st;
+ stat(path, &st);
+ CHECK(st.st_size < 90 * 40, "90 minutes take %lld bytes", (long long)st.st_size);
+ CHECK((st.st_mode & 0777) == 0600, "mode %o", st.st_mode & 0777);
+ nh_free(h);
+}
+
+/* 40 days of one minute every 10: minutes past 7 days fold into hours, hours past 30 days go. */
+static void test_rotation(void) {
+ unlink(path);
+ long long t0 = 1760000000LL - 1760000000LL % 3600;
+ NetHist *h = nh_new(path);
+ long long now = t0;
+ long long last = 40LL * 24 * 6;
+ for (long long m = 0; m < last; m++) {
+ now = t0 + m * 600;
+ nh_add(h, now, NULL, 500, 50);
+ nh_add(h, now, "desktop:app", 200, 20);
+ nh_flush(h, now + 60);
+ }
+ nh_compact(h, now + 60);
+ /* what the last compaction keeps: minutes of the last 7 days, hours of the last 30 */
+ double want_rx = 0;
+ long long nowf = now + 60;
+ for (long long m = 0; m < last; m++) {
+ long long minute = (t0 + m * 600) / 60;
+ if (minute >= nowf / 60 - NH_MIN_KEEP || minute / 60 >= nowf / 3600 - NH_HOUR_KEEP) want_rx += 500;
+ }
+ size_t n;
+ uint8_t *b = slurp(&n);
+ int recs = 0;
+ CHECK(b && nh_check(b, n, &recs) == 0, "compacted file checks out");
+ /* 7 days of minutes (1008) + 23 days of hours (552) + 1 app ≈ 1561 records */
+ CHECK(recs > 1400 && recs < 1700, "%d records after 40 days", recs);
+ CHECK(n < 64 * 1024, "40 days take %zu bytes", n);
+ free(b);
+ double rx, tx;
+ int res;
+ totals(h, now + 60, now + 60 - 31LL * 86400, 3600, 31 * 24, NULL, &rx, &tx, &res);
+ CHECK(rx == want_rx && res == 60, "30 days kept: %.0f of %.0f (res %d)", rx, want_rx, res);
+ totals(h, now + 60, t0, 3600, 9 * 24, NULL, &rx, &tx, &res);
+ CHECK(rx == 0 && res == 0, "nothing older than 30 days: %.0f", rx);
+ /* a window 10 days back has only hours: res 3600, and the totals match */
+ long long w = now - 10LL * 86400;
+ w -= w % 3600;
+ totals(h, now + 60, w, 300, 12, "desktop:app", &rx, &tx, &res);
+ CHECK(res == 3600 && fabs(rx - 6 * 200) < 1, "an old hour at 5-minute buckets: %.0f res %d", rx, res);
+ nh_free(h);
+}
+
+/* A cut or damaged file: never a crash, what came before the damage stays, compaction cleans it. */
+static void test_corruption(void) {
+ unlink(path);
+ long long t0 = 1760000000LL - 1760000000LL % 3600;
+ NetHist *h = nh_new(path);
+ for (int m = 0; m < 30; m++) { nh_add(h, t0 + m * 60, NULL, 100, 10); nh_add(h, t0 + m * 60, "desktop:x", 50, 5); nh_flush(h, t0 + m * 60 + 60); }
+ nh_free(h);
+ size_t n;
+ uint8_t *good = slurp(&n);
+ int recs = 0;
+ CHECK(good && nh_check(good, n, &recs) == 0 && recs == 31, "%d records", recs);
+ int crashes_ok = 0;
+ for (size_t cut = 0; cut < n; cut++) { /* every truncation */
+ spit(good, cut);
+ h = nh_new(path);
+ double rx, tx;
+ int res;
+ totals(h, t0 + 3600, t0, 60, 60, NULL, &rx, &tx, &res);
+ if (rx > 3000 || rx < 0 || (long long)rx % 100) { CHECK(0, "cut at %zu: %.0f", cut, rx); }
+ nh_add(h, t0 + 1800, NULL, 7, 7); /* goes on writing after the damage */
+ nh_free(h);
+ size_t m;
+ uint8_t *b = slurp(&m);
+ int r2;
+ if (b && nh_check(b, m, &r2) == 0) crashes_ok++;
+ free(b);
+ }
+ CHECK(crashes_ok == (int)n, "after any cut the file is clean again (%d of %zu)", crashes_ok, n);
+ srand(7);
+ int kept_before = 0, trials = 2000;
+ for (int k = 0; k < trials; k++) { /* random bytes changed */
+ uint8_t *b = malloc(n);
+ memcpy(b, good, n);
+ size_t at = 8 + (size_t)rand() % (n - 8);
+ b[at] ^= (uint8_t)(1 + rand() % 255);
+ int rc;
+ size_t stop = nh_check(b, n, &rc);
+ if (stop && stop <= at) kept_before++;
+ spit(b, n);
+ free(b);
+ h = nh_new(path);
+ double rx, tx;
+ int res;
+ totals(h, t0 + 3600, t0, 60, 60, "desktop:x", &rx, &tx, &res);
+ nh_free(h);
+ }
+ CHECK(kept_before == trials, "a damaged record stops the reading at it or before (%d of %d)", kept_before, trials);
+ /* not ours at all */
+ spit((const uint8_t *)"hello, this is not a history file", 33);
+ h = nh_new(path);
+ nh_add(h, t0, NULL, 1, 1);
+ nh_flush(h, t0 + 60);
+ nh_free(h);
+ uint8_t *b = slurp(&n);
+ CHECK(b && n >= 8 && !memcmp(b, "LNH1", 4) && nh_check(b, n, &recs) == 0, "a foreign file is replaced by a clean one");
+ free(b);
+ free(good);
+}
+
+static void test_apps_and_clear(void) {
+ unlink(path);
+ long long t0 = 1760000000LL;
+ NetHist *h = nh_new(path);
+ char id[64];
+ for (int i = 0; i < NH_APPS_MAX + 40; i++) { snprintf(id, sizeof id, "desktop:app%d", i); nh_add(h, t0, id, 1, 1); }
+ nh_add(h, t0, "bad id with spaces", 5, 5);
+ nh_flush(h, t0 + 60);
+ double rx, tx;
+ int res;
+ totals(h, t0 + 60, t0 - t0 % 60, 60, 1, "desktop:app3", &rx, &tx, &res);
+ CHECK(rx == 1, "app 3: %.0f", rx);
+ nh_free(h);
+ size_t n;
+ uint8_t *b = slurp(&n);
+ int recs;
+ CHECK(b && nh_check(b, n, &recs) == 0, "a file with %d apps checks out", NH_APPS_MAX);
+ free(b);
+ h = nh_new(path);
+ CHECK(nh_clear(h) == 0 && access(path, F_OK) != 0, "Clear history removes the file");
+ totals(h, t0 + 60, t0 - t0 % 60, 60, 1, NULL, &rx, &tx, &res);
+ CHECK(rx == 0 && res == 0, "nothing after Clear: %.0f", rx);
+ nh_add(h, t0 + 120, NULL, 9, 9);
+ nh_add(h, t0 + 120, "desktop:app1", 4, 4);
+ nh_flush(h, t0 + 200);
+ totals(h, t0 + 200, t0 + 120 - (t0 + 120) % 60, 60, 1, "desktop:app1", &rx, &tx, &res);
+ CHECK(rx == 4, "recording goes on after Clear, with its app records again: %.0f", rx);
+ nh_free(h);
+ /* bad arguments */
+ h = nh_new(path);
+ double a[2], c[2];
+ CHECK(nh_query(h, t0, t0, 0, 2, NULL, a, c, NULL, 0, NULL, NULL) == -1 && nh_query(h, t0, t0, 60, 0, NULL, a, c, NULL, 0, NULL, NULL) == -1 &&
+ nh_query(h, t0, t0, 60, NH_BUCKETS_MAX + 1, NULL, a, c, NULL, 0, NULL, NULL) == -1, "bad arguments are refused");
+ nh_free(h);
+ /* a symlink in its place is not followed */
+ unlink(path);
+ if (symlink("/etc/passwd", path) == 0) {
+ h = nh_new(path);
+ nh_add(h, t0, NULL, 1, 1);
+ CHECK(nh_flush(h, t0 + 60) == -1, "a symlink in the file's place is refused");
+ nh_free(h);
+ unlink(path);
+ }
+}
+
+int main(void) {
+ snprintf(dir, sizeof dir, "/tmp/nethist-test-XXXXXX");
+ if (!mkdtemp(dir)) { perror("mkdtemp"); return 1; }
+ snprintf(path, sizeof path, "%s/state/lordos/net-history.bin", dir);
+ test_roundtrip();
+ test_rotation();
+ test_corruption();
+ test_apps_and_clear();
+ unlink(path);
+ char p[400];
+ snprintf(p, sizeof p, "%s/state/lordos", dir); rmdir(p);
+ snprintf(p, sizeof p, "%s/state", dir); rmdir(p);
+ rmdir(dir);
+ printf("nethist-test: %d checks, %d failures\n", checks, fails);
+ return fails ? 1 : 0;
+}