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.
C cdebiandesktop-environmentebpffirewalllandlocklinuxlinux-distributionopen-sourceprivacyquickjsraspberry-pi-5reactsecuritywaylandwayland-compositor
Remote desktop over WebRTC: our own ICE-lite/DTLS-SRTP/SCTP peer in the bridge, one shared encoder per session
Federico Pereira3213214568f3
diff --git a/server/Makefile b/server/Makefileindex 482414d..17ad1cf 100644--- a/server/Makefile+++ b/server/Makefile@@ -51,11 +51,11 @@ QJS_OBJ := $(patsubst %,build/qjs/%.o,quickjs libregexp libunicode cutils dtoa)# Monitor: metrics, connections and the country of an IP (src/monitor, see docs/MONITOR.md)MON_SRC := src/monitor/mon.c src/monitor/disktemp.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/hwdec.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+MEDIA_SRC := src/media/media.c src/media/hwdec.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/media/mstream.c src/ui/js_media.cSEC_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/brokermail.c src/sec/restrict.c src/sec/appid.c src/sec/pwguard.c src/sec/codesig.c# App mode: lord-server --app <id> runs an .lpk app as an isolated Wayland client (src/app)APP_SRC := src/app/appclient.c src/app/appspawn.c src/ui/js_broker.c src/ui/js_brokercam.c src/ui/js_brokermail.c src/app/apphost_bc.c-SRC := src/main.c src/kms.c src/gpu/gpu.c src/input.c src/canvas.c src/admin.c src/install/install.c src/apps.c src/lpk.c src/fsops.c src/remote/mirror.c src/remote/remote_admin.c src/remote/ipnet.c src/remote/ssh_admin.c src/users/users_admin.c src/users/homedirs.c src/keyboard.c src/keylag.c src/ime.c src/wl/wl.c src/wl/x11wire.c src/wl/xwm.c src/wl/xwayland.c \+SRC := src/main.c src/kms.c src/gpu/gpu.c src/input.c src/canvas.c src/admin.c src/install/install.c src/apps.c src/lpk.c src/fsops.c src/remote/mirror.c src/remote/remote_admin.c src/remote/ipnet.c src/remote/rtcwire.c src/remote/ssh_admin.c src/users/users_admin.c src/users/homedirs.c src/keyboard.c src/keylag.c src/ime.c src/wl/wl.c src/wl/x11wire.c src/wl/xwm.c src/wl/xwayland.c \src/ui/node.c src/ui/style.c src/ui/layout.c src/ui/transform.c src/ui/text.c src/ui/image.c \src/ui/paint.c src/ui/js.c src/ui/js_sys.c src/ui/js_term.c src/ui/js_wasm.c src/wasm/wasm.c src/wasm/wasm_run.c src/ui/js_sound.c src/ui/js_net.c src/sound/snd.c src/sound/pulse.c src/ui/otf.c src/ui/fonts.c \src/ui/unicode.c src/ui/bidi.c src/ui/shape.c src/ui/shape_indic.c src/ui/shape_syl.c src/ui/shape_khmer.c src/ui/shape_myanmar.c src/ui/shape_use.c src/ui/thbrk.c src/ui/textlayout.c \@@ -236,13 +236,13 @@ build/tzif-test: tests/tzif_test.c src/ui/tzif.c src/ui/tzif.h# remote desktop security (docs/REMOTE.md, "Security analysis (0.8.3)"): the strict HTTP, form,# cookie, WebSocket and JSON parsers, addresses and networks, the failed-login lockout and the# connection rate; then the same parsers under ASan+UBSan with many random and mutated inputs-REMOTE_SEC_SRC := src/remote/rsec.c src/remote/ipnet.c src/remote/lockout.c+REMOTE_SEC_SRC := src/remote/rsec.c src/remote/ipnet.c src/remote/lockout.c src/remote/rtcwire.ctest: build/remote-sec-test build/remote-fuzz-build/remote-sec-test: tests/remote_sec_test.c $(REMOTE_SEC_SRC) src/remote/rsec.h src/remote/ipnet.h src/remote/lockout.h+build/remote-sec-test: tests/remote_sec_test.c $(REMOTE_SEC_SRC) src/remote/rsec.h src/remote/ipnet.h src/remote/lockout.h src/remote/rtcwire.h@mkdir -p build$(CC) $(CPPFLAGS) $(CFLAGS) $(WARN) -std=c11 -o $@ tests/remote_sec_test.c $(REMOTE_SEC_SRC)./$@ || { rm -f $@; exit 1; }-build/remote-fuzz: tests/remote_fuzz.c $(REMOTE_SEC_SRC) src/remote/rsec.h src/remote/ipnet.h src/remote/lockout.h+build/remote-fuzz: tests/remote_fuzz.c $(REMOTE_SEC_SRC) src/remote/rsec.h src/remote/ipnet.h src/remote/lockout.h src/remote/rtcwire.h@mkdir -p build$(CC) $(CPPFLAGS) -O1 -g $(WARN) -std=c11 -fsanitize=address,undefined -fno-sanitize-recover=all -fno-omit-frame-pointer \-o $@ tests/remote_fuzz.c $(REMOTE_SEC_SRC)@@ -458,12 +458,20 @@ build/lord-server: $(OBJ) build/obj/ui/stb.o $(QJS_OBJ) $(WL_OBJ)SESSIOND_OBJ := build/obj/remote/sessiond.o build/obj/remote/sess.o build/obj/remote/monitor.o build/obj/remote/conn.o \build/obj/remote/enc.o build/obj/remote/pages.o build/obj/sec/cgroup.o build/obj/remote/netsup.o build/obj/remote/cgsup.o \- build/obj/remote/rsec.o build/obj/remote/ipnet.o build/obj/remote/lockout.o+ build/obj/remote/rsec.o build/obj/remote/ipnet.o build/obj/remote/lockout.o build/obj/remote/rtcwire.o+# The remote desktop's video (docs/REMOTE.md, "Video (WebRTC)"): our own WebRTC peer in the bridge, on+# Debian's libsrtp2 and usrsctp (and OpenSSL's DTLS). Without them, the tile path only.+RTC ?= $(shell pkg-config --exists libsrtp2 usrsctp 2>/dev/null && echo 1 || echo 0)+ifeq ($(RTC),1)+SESSIOND_OBJ += build/obj/remote/rtc.o+SESSIOND_LIBS := $(shell pkg-config --libs libsrtp2 usrsctp)+build/obj/remote/rtc.o build/obj/remote/conn.o: CPPFLAGS += -DLORD_RTC $(shell pkg-config --cflags libsrtp2 usrsctp)+endifbuild/obj/remote/pages.o: src/remote/login.html src/remote/client.html@mkdir -p $(dir $@)$(LD) -r -b binary -z noexecstack -o $@ src/remote/login.html src/remote/client.htmlbuild/lord-sessiond: $(SESSIOND_OBJ)- $(CC) $(LDFLAGS) -o $@ $^ $(shell pkg-config --libs openssl) -lcrypt -lpthread+ $(CC) $(LDFLAGS) -o $@ $^ $(shell pkg-config --libs openssl) $(SESSIOND_LIBS) -lcrypt -lpthread# Version, channel and build of LordOS, shown in About and Settings → General (lord.version and# lord.system.info()). Version and channel come from ../package.json ("version" and "lordos": {"channel"}),diff --git a/server/src/gpu/gpu.c b/server/src/gpu/gpu.cindex 2b16b86..7f0bcd8 100644--- a/server/src/gpu/gpu.c+++ b/server/src/gpu/gpu.c@@ -42,6 +42,8 @@ void gpu_draw(GpuTex *t, float dx, float dy, float dw, float dh, float sx, float}int gpu_scanout(const GpuDmabuf *d, void (*done)(void *), void *data) { (void)d; (void)done; (void)data; return -1; }int gpu_can_scanout(const GpuDmabuf *d, int w, int h) { (void)d; (void)w; (void)h; return 0; }+void gpu_read_next(uint32_t *px, int w, int h) { (void)px; (void)w; (void)h; }+int gpu_read_done(void) { return 0; }#else@@ -703,6 +705,29 @@ void gpu_draw(GpuTex *t, float dx, float dy, float dw, float dh, float sx, floatglDisable(GL_SCISSOR_TEST);}+static struct { uint32_t *px; int w, h, done; } rb;++void gpu_read_next(uint32_t *px, int w, int h) { rb.px = px; rb.w = w; rb.h = h; rb.done = 0; }+int gpu_read_done(void) { return rb.done; }++/* RGBA bytes, bottom row first (GL) → 0xAARRGGBB words, top row first; opaque. */+static void read_back(int W, int H) {+ glPixelStorei(GL_PACK_ALIGNMENT, 4);+ glReadPixels(0, 0, W, H, GL_RGBA, GL_UNSIGNED_BYTE, rb.px);+ if (glGetError() != GL_NO_ERROR) return;+ for (int y = 0; y < (H + 1) / 2; y++) {+ uint32_t *a = rb.px + (size_t)y * W, *b = rb.px + (size_t)(H - 1 - y) * W;+ for (int x = 0; x < W; x++) {+ const uint8_t *pa = (const uint8_t *)&a[x], *pb = (const uint8_t *)&b[x];+ uint32_t va = 0xff000000u | (uint32_t)pa[0] << 16 | (uint32_t)pa[1] << 8 | pa[2];+ uint32_t vb = 0xff000000u | (uint32_t)pb[0] << 16 | (uint32_t)pb[1] << 8 | pb[2];+ a[x] = vb;+ b[x] = va;+ }+ }+ rb.done = 1;+}+int gpu_frame(int out, const Canvas *scene, Rect dmg, void (*draw_layer)(int layer)) {if (!G.on || out < 0 || out >= OUT_MAX || !G.out[out].used) return -1;GOut *o = &G.out[out];@@ -724,6 +749,10 @@ int gpu_frame(int out, const Canvas *scene, Rect dmg, void (*draw_layer)(int layif (draw_layer) draw_layer(0);gpu_draw(&o->canvas, 0, 0, (float)o->W, (float)o->H, 0, 0, (float)o->W, (float)o->H, all, 1);if (draw_layer) draw_layer(1);+ if (rb.px) {+ if (rb.w == o->W && rb.h == o->H) read_back(o->W, o->H);+ rb.px = NULL;+ }if (!eglSwapBuffers(G.dpy, o->surf)) { fprintf(stderr, "gpu: eglSwapBuffers failed\n"); return -1; }struct gbm_bo *bo = gbm_surface_lock_front_buffer(o->gs);if (!bo) { fprintf(stderr, "gpu: no GBM buffer\n"); return -1; }diff --git a/server/src/gpu/gpu.h b/server/src/gpu/gpu.hindex 27dcfa2..aefc193 100644--- a/server/src/gpu/gpu.h+++ b/server/src/gpu/gpu.h@@ -59,6 +59,12 @@ int gpu_add_output(Kms *k);void gpu_draw(GpuTex *t, float dx, float dy, float dw, float dh,float sx, float sy, float sw, float sh, Rect clip, float opacity);+/* The remote desktop's copy of the next composed frame (docs/REMOTE.md, "Video (WebRTC)"): the+ * next gpu_frame of a w×h display reads the whole frame back into px (0xAARRGGBB, rows top to+ * bottom) before showing it; gpu_read_done says whether it did. */+void gpu_read_next(uint32_t *px, int w, int h);+int gpu_read_done(void);+/* Direct scanout of a fullscreen dmabuf (without composing). done(data) is called when* the buffer leaves the screen. 0 if the flip was sent. */int gpu_scanout(const GpuDmabuf *d, void (*done)(void *), void *data);diff --git a/server/src/main.c b/server/src/main.cindex d04874f..aaf3750 100644--- a/server/src/main.c+++ b/server/src/main.c@@ -533,9 +533,20 @@ static void remote_notice(const char *json) { js_remote_notice(json); }static int gpu_start(Server *s);+/* Does someone need the frame in memory, composed by the CPU? Recording the screen does. The remote+ * desktop doesn't any more (docs/REMOTE.md, "Video (WebRTC)"): with the GPU composing, the frame is+ * read back once (gpu_read_next) for every viewer. Only protected windows still need the CPU path+ * (they are painted black in a CPU copy of the scene, media/capture.c); LORD_REMOTE_GPU=0 forces it. */+static int remote_needs_cpu(void) {+ if (!mirror_clients()) return 0;+ const char *e = getenv("LORD_REMOTE_GPU");+ return (e && !strcmp(e, "0")) || media_protected_count() > 0;+}+static int need_cpu(void) { return remote_needs_cpu() || media_needs_cpu(); }+/* The GPU composes one display, upright; several displays or a rotated one go by the CPU. */static int gpu_allowed(const Server *s) {- return !s->headless && out_count(s) >= 1 && !display_cpu_only() && !mirror_clients() && !media_needs_cpu();+ return !s->headless && out_count(s) >= 1 && !display_cpu_only() && !need_cpu();}/* The GPU stopped: every display gets its dumb buffer back (CPU path). */@@ -767,7 +778,7 @@ static void display_attach(const int *fds, int n, int flags) {if (input_open_fds(&s->input, fds + 1, n - 1)) fprintf(stderr, "monitor: no keyboard or mouse\n");wl_input_devices_changed(); /* is there a touch screen? (wl_seat touch) */s->headless = 0;- s->gpu_paused = mirror_clients() > 0 || media_needs_cpu();+ s->gpu_paused = need_cpu();o0->on = 1;struct stat st;if (fstat(o0->kms.fd, &st) == 0) wl_set_drm_device(st.st_rdev);@@ -1324,9 +1335,9 @@ static void render_frame(Server *s) {s->cap.cdirty = 1;s->cursor_dirty = 0;}- if (gpu_active() && (mirror_clients() || media_needs_cpu())) { /* also when recording the screen */- /* someone watches the session over the network: the screen must be in memory (CPU path) */- fprintf(stderr, "remote: there is a connection; composing by CPU\n");+ if (gpu_active() && need_cpu()) { /* recording the screen, or a remote viewer with protected windows */+ /* the screen must be in memory (CPU path) */+ fprintf(stderr, "remote: composing by CPU (%s)\n", media_needs_cpu() ? "screen recording" : "protected windows on screen");gpu_tex_free(s->cursor_tex);s->cursor_tex = NULL;gpu_shutdown();@@ -1352,8 +1363,29 @@ static void render_frame(Server *s) {gpu_server = s;gpu_out = o;view_of(o, i);- /* compatible fullscreen (one display): straight to the card; otherwise composed */- int failed = !(out_count(s) == 1 && wl_try_scanout(s->hw_cursor || s->cursor_mode == 2)) && gpu_frame(o->gpu, &o->scene, r, gpu_layer);+ /* the remote desktop watches: the composed frame is read back once for every viewer */+ static uint32_t *rb;+ static size_t rb_n;+ int readback = o == feed && mirror_clients() > 0;+ if (readback) {+ size_t need = (size_t)o->scene.w * o->scene.h;+ if (need > rb_n) { free(rb); rb = malloc(need * 4); rb_n = rb ? need : 0; }+ if (rb) gpu_read_next(rb, o->scene.w, o->scene.h);+ else readback = 0;+ }+ /* compatible fullscreen (one display): straight to the card; otherwise composed (always+ * composed while the remote desktop reads it back) */+ int failed = !(!readback && out_count(s) == 1 && wl_try_scanout(s->hw_cursor || s->cursor_mode == 2)) && gpu_frame(o->gpu, &o->scene, r, gpu_layer);+ if (readback && !failed && gpu_read_done()) {+ static int said;+ if (!said++) fprintf(stderr, "remote: composing on the GPU; frames read back for the remote desktop\n");+ Cur k;+ cursor_now(s, &k, out_scale(o));+ int cx, cy;+ pointer_phys(s, o, &cx, &cy);+ mirror_frame(rb, o->scene.w, o->scene.w, o->scene.h);+ mirror_cursor(cx, cy, k.mode, k.px, k.w, k.h, k.hx, k.hy, k.gen);+ }view_reset();gpu_out = NULL;o->cdirty = 0;@@ -1708,6 +1740,9 @@ int main(int argc, char **argv) {if (nrd >= 0 && (delay < 0 || nrd < delay)) delay = nrd;double tpd = tp_timeout(now_ms());if (tpd >= 0 && (delay < 0 || tpd < delay)) delay = tpd;+ mirror_tick(); /* remote desktop video: lossless tiles once still, the encoder */+ double mrd = mirror_timeout();+ if (mrd >= 0 && (delay < 0 || mrd < delay)) delay = mrd;/* sin monitor y sin nadie mirando no se dibuja (al conectarse alguien llega un cuadro entero) */int unseen = s.headless && !mirror_clients() && !media_needs_cpu(); /* grabando la pantalla, sí */if (!s.headless && display_is_off()) unseen = 1; /* screen off after idle (DPMS): nothing to draw */diff --git a/server/src/media/helper/lm.h b/server/src/media/helper/lm.hindex c7176c4..5966728 100644--- a/server/src/media/helper/lm.h+++ b/server/src/media/helper/lm.h@@ -100,5 +100,6 @@ void lm_screenrec(const char *params, int out, int pulse, int render); /* lm_revoid lm_camera(const char *params, int dev, int render); /* lm_cam.c */void lm_audiorec(const char *params, int out, int pulse); /* lm_rec.c */void lm_hwprobe(const int *render, int n_render); /* lm_hwprobe.c */+void lm_stream(const char *params, int ring, int render); /* lm_stream.c */#endifdiff --git a/server/src/media/helper/lm_main.c b/server/src/media/helper/lm_main.cindex 6315e97..cb75074 100644--- a/server/src/media/helper/lm_main.c+++ b/server/src/media/helper/lm_main.c@@ -67,6 +67,13 @@ int main(int argc, char **argv) {int ioctls = 0;if ((m.v[0] == MK_PLAY || m.v[0] == MK_SCREENREC) && fds[2] >= 0) ioctls |= SANDBOX_IOCTL_DRM | SANDBOX_IOCTL_DMABUF;if (m.v[0] == MK_HWPROBE && fds[0] >= 0) ioctls |= SANDBOX_IOCTL_DRM;+ /* MK_STREAM: the descriptors that came, in order (mp_send skips the missing ones) */+ int st_k = 0, st_ring = -1, st_render = -1;+ if (m.v[0] == MK_STREAM) {+ if (mp_param_int(payload, "audio", 0) == 1) st_ring = fds[st_k++];+ if (mp_param_int(payload, "render", 0) == 1) st_render = fds[st_k++];+ if (st_render >= 0) ioctls |= SANDBOX_IOCTL_DRM | SANDBOX_IOCTL_DMABUF;+ }if (m.v[0] == MK_CAMERA) ioctls |= SANDBOX_IOCTL_V4L2 | (fds[1] >= 0 ? SANDBOX_IOCTL_DRM | SANDBOX_IOCTL_DMABUF : 0);SandboxHelperCfg cfg = { .name = "app-lordos-media", .ro_paths = ro, .extra_syscalls = extra, .allow_ioctl_classes = ioctls,.mem_max_mb = m.v[0] == MK_WORKER ? 1024 : 1536 };@@ -84,6 +91,7 @@ int main(int argc, char **argv) {case MK_CAMERA: lm_camera(payload, fds[0], fds[1]); break;case MK_AUDIOREC: lm_audiorec(payload, fds[0], fds[1]); break;case MK_HWPROBE: lm_hwprobe(fds, MP_MAX_FDS); break;+ case MK_STREAM: lm_stream(payload, st_ring, st_render); break;default: LM_LOG("unknown kind %lld", (long long)m.v[0]); return 1;}return 0;diff --git a/server/src/media/helper/lm_stream.c b/server/src/media/helper/lm_stream.cnew file mode 100644index 0000000..5f008f2--- /dev/null+++ b/server/src/media/helper/lm_stream.c@@ -0,0 +1,342 @@+/* lm_stream.c — the remote desktop's live encoder (MK_STREAM, docs/REMOTE.md, "Video (WebRTC)").+ *+ * One per session and codec: lord-server copies each changed frame of the session (protected windows+ * already black) into a free slot of a sealed memfd (MM_INRING / MM_INFRAME), as for the screen+ * recording; here it becomes H.264 (VA-API when the card encodes it, else libx264) or VP8 (libvpx),+ * tuned for latency: no B-frames, one frame in, one frame out, a keyframe only when asked+ * (MM_KEYFRAME: a new viewer or a lost packet) and the bit rate the slowest viewer can take+ * (MM_RATE). The session's sound arrives in an audio ring that lord-server fills from PipeWire and+ * goes out as Opus (20 ms). Every encoded frame goes back as MM_PACKET chunks; the bridges of the+ * session's viewers only add the per-connection encryption. */+#include "lm.h"++#include <errno.h>+#include <fcntl.h>+#include <poll.h>+#include <stdlib.h>+#include <string.h>+#include <sys/mman.h>+#include <sys/stat.h>++#include <libavutil/channel_layout.h>+#include <libavutil/hwcontext.h>+#include <libavutil/opt.h>+#include <libswscale/swscale.h>++enum { CODEC_H264 = 1, CODEC_VP8 = 2 };++typedef struct {+ AVCodecContext *c;+ AVBufferRef *frames; /* VA-API surfaces */+ struct SwsContext *sws;+ AVFrame *yuv;+ AVPacket *pkt;+ int w, h, codec, hw, force_key;+ const char *name;+ unsigned kbps;+} Vid;++typedef struct {+ AVCodecContext *c;+ AVFrame *f;+ AVPacket *pkt;+ int16_t buf[960 * 4];+ int have;+ int64_t pts;+} Aud;++static Vid V;+static Aud A;++/* Blocking send of one MM_PACKET message (the helper may wait; lord-server never does). */+static int send_wait(const int64_t v[6], uint32_t id, const uint8_t *p, size_t n) {+ for (int tries = 0; tries < 50; tries++) {+ if (!mp_send(LM_SOCK, MM_PACKET, id, v, p, n, NULL, 0)) return 0;+ if (errno != EAGAIN && errno != EWOULDBLOCK && errno != ENOBUFS) return -1;+ struct pollfd pf = { LM_SOCK, POLLOUT, 0 };+ poll(&pf, 1, 20);+ }+ return -1;+}++/* A packet as chunks of at most MP_PAYLOAD_MAX: v[0] total, v[1] offset, v[2] pts, v[3] flags. */+static void send_packet(uint32_t kind, const uint8_t *d, size_t n, int64_t pts, int key) {+ if (!n || n > (8u << 20)) return;+ for (size_t off = 0; off < n; off += MP_PAYLOAD_MAX) {+ size_t c = n - off > MP_PAYLOAD_MAX ? MP_PAYLOAD_MAX : n - off;+ int64_t v[6] = { (int64_t)n, (int64_t)off, pts, key, 0, 0 };+ if (send_wait(v, kind, d + off, c)) return;+ }+}++static AVCodecContext *open_h264_vaapi(Vid *e, AVBufferRef *hw, int fps) {+ const AVCodec *codec = avcodec_find_encoder_by_name("h264_vaapi");+ if (!hw || !codec) return NULL;+ AVCodecContext *c = avcodec_alloc_context3(codec);+ AVBufferRef *fr = av_hwframe_ctx_alloc(hw);+ AVHWFramesContext *fc = fr ? (AVHWFramesContext *)fr->data : NULL;+ if (!c || !fc) { av_buffer_unref(&fr); avcodec_free_context(&c); return NULL; }+ fc->format = AV_PIX_FMT_VAAPI;+ fc->sw_format = AV_PIX_FMT_NV12;+ fc->width = e->w;+ fc->height = e->h;+ fc->initial_pool_size = 4;+ if (av_hwframe_ctx_init(fr) < 0) { av_buffer_unref(&fr); avcodec_free_context(&c); return NULL; }+ c->hw_frames_ctx = av_buffer_ref(fr);+ c->pix_fmt = AV_PIX_FMT_VAAPI;+ c->width = e->w;+ c->height = e->h;+ c->time_base = (AVRational){ 1, 1000 };+ c->framerate = (AVRational){ fps, 1 };+ c->gop_size = fps * 60;+ c->max_b_frames = 0;+ c->profile = AV_PROFILE_H264_CONSTRAINED_BASELINE;+ c->bit_rate = (int64_t)e->kbps * 1000;+ c->rc_max_rate = c->bit_rate;+ c->rc_buffer_size = (int)(c->bit_rate / 2);+ av_opt_set(c->priv_data, "rc_mode", "VBR", 0);+ if (avcodec_open2(c, codec, NULL) < 0) { av_buffer_unref(&fr); avcodec_free_context(&c); LM_LOG("VA-API does not encode H.264 here: libx264"); return NULL; }+ e->frames = fr;+ e->hw = 1;+ e->name = "h264_vaapi";+ return c;+}++static AVCodecContext *open_sw(Vid *e, const char *name, int fps) {+ const AVCodec *codec = avcodec_find_encoder_by_name(name);+ if (!codec) return NULL;+ AVCodecContext *c = avcodec_alloc_context3(codec);+ if (!c) return NULL;+ c->width = e->w;+ c->height = e->h;+ c->pix_fmt = AV_PIX_FMT_YUV420P;+ c->time_base = (AVRational){ 1, 1000 };+ c->framerate = (AVRational){ fps, 1 };+ c->gop_size = fps * 60;+ c->max_b_frames = 0;+ c->color_range = AVCOL_RANGE_MPEG;+ c->colorspace = AVCOL_SPC_SMPTE170M;+ c->color_primaries = AVCOL_PRI_SMPTE170M;+ c->color_trc = AVCOL_TRC_SMPTE170M;+ AVDictionary *o = NULL;+ if (e->codec == CODEC_H264) {+ c->thread_count = 2;+ c->rc_max_rate = (int64_t)e->kbps * 1000;+ c->rc_buffer_size = (int)(c->rc_max_rate / 3); /* about 1/3 s: low latency */+ av_dict_set(&o, "preset", "veryfast", 0);+ av_dict_set(&o, "tune", "zerolatency", 0);+ av_dict_set(&o, "profile", "baseline", 0);+ av_dict_set(&o, "crf", "22", 0);+ av_dict_set(&o, "forced-idr", "1", 0);+ av_dict_set(&o, "x264-params", "repeat-headers=1:annexb=1:aud=0:scenecut=0", 0);+ } else {+ c->thread_count = 2;+ c->bit_rate = (int64_t)e->kbps * 1000;+ c->rc_min_rate = 0;+ c->rc_max_rate = c->bit_rate;+ c->rc_buffer_size = (int)(c->bit_rate / 3);+ c->qmin = 4;+ c->qmax = 56;+ av_dict_set(&o, "deadline", "realtime", 0);+ av_dict_set(&o, "cpu-used", "8", 0);+ av_dict_set(&o, "lag-in-frames", "0", 0);+ av_dict_set(&o, "error-resilient", "1", 0);+ av_dict_set(&o, "static-thresh", "100", 0);+ }+ int r = avcodec_open2(c, codec, &o);+ av_dict_free(&o);+ if (r < 0) { avcodec_free_context(&c); return NULL; }+ e->name = name;+ return c;+}++static int vid_open(Vid *e, int codec, int w, int h, int fps, unsigned kbps, AVBufferRef *hw) {+ memset(e, 0, sizeof *e);+ e->codec = codec;+ e->w = w & ~1;+ e->h = h & ~1;+ e->kbps = kbps;+ if (e->w < 16 || e->h < 16) return -1;+ if (codec == CODEC_H264) {+ e->c = open_h264_vaapi(e, hw, fps);+ if (!e->c) e->c = open_sw(e, "libx264", fps);+ } else e->c = open_sw(e, "libvpx", fps);+ if (!e->c) return -1;+ e->pkt = av_packet_alloc();+ e->yuv = av_frame_alloc();+ e->yuv->format = e->frames ? AV_PIX_FMT_NV12 : AV_PIX_FMT_YUV420P;+ e->yuv->width = e->w;+ e->yuv->height = e->h;+ if (av_frame_get_buffer(e->yuv, 0) < 0) return -1;+ LM_LOG("remote desktop video: %dx%d with %s, up to %u kbit/s", e->w, e->h, e->name, kbps);+ return 0;+}++static void vid_close(Vid *e) {+ avcodec_free_context(&e->c);+ av_buffer_unref(&e->frames);+ sws_freeContext(e->sws);+ av_frame_free(&e->yuv);+ av_packet_free(&e->pkt);+}++static void vid_drain(Vid *e) {+ while (avcodec_receive_packet(e->c, e->pkt) >= 0) {+ send_packet(0, e->pkt->data, (size_t)e->pkt->size, e->pkt->pts, (e->pkt->flags & AV_PKT_FLAG_KEY) != 0);+ av_packet_unref(e->pkt);+ }+}++static int vid_frame(Vid *e, const uint8_t *bgra, int w, int h, int64_t ms) {+ if (!e->sws) {+ e->sws = sws_getContext(w, h, AV_PIX_FMT_BGRA, e->w, e->h, e->yuv->format, SWS_POINT, NULL, NULL, NULL);+ if (!e->sws) return -1;+ }+ if (av_frame_make_writable(e->yuv) < 0) return -1;+ const uint8_t *src[1] = { bgra };+ int stride[1] = { w * 4 };+ sws_scale(e->sws, src, stride, 0, h, e->yuv->data, e->yuv->linesize);+ AVFrame *in = e->yuv, *hwf = NULL;+ if (e->frames) {+ hwf = av_frame_alloc();+ if (av_hwframe_get_buffer(e->frames, hwf, 0) < 0 || av_hwframe_transfer_data(hwf, e->yuv, 0) < 0) { av_frame_free(&hwf); return -1; }+ in = hwf;+ }+ in->pts = ms;+ in->pict_type = e->force_key ? AV_PICTURE_TYPE_I : AV_PICTURE_TYPE_NONE;+ if (e->force_key) in->flags |= AV_FRAME_FLAG_KEY; else in->flags &= ~AV_FRAME_FLAG_KEY;+ e->force_key = 0;+ int r = avcodec_send_frame(e->c, in);+ av_frame_free(&hwf);+ if (r < 0) return r;+ vid_drain(e);+ return 0;+}++static void vid_rate(Vid *e, unsigned kbps) {+ if (!e->c || kbps == e->kbps) return;+ e->kbps = kbps;+ /* libx264 reconfigures itself when these change between frames; the others keep their rate */+ if (e->codec == CODEC_H264 && !e->hw) {+ e->c->rc_max_rate = (int64_t)kbps * 1000;+ e->c->rc_buffer_size = (int)(e->c->rc_max_rate / 3);+ }+}++static int aud_open(Aud *a) {+ const AVCodec *codec = avcodec_find_encoder_by_name("libopus");+ if (!codec) { LM_LOG("no Opus encoder: the remote desktop has no sound"); return -1; }+ a->c = avcodec_alloc_context3(codec);+ a->c->sample_fmt = AV_SAMPLE_FMT_S16;+ a->c->sample_rate = 48000;+ av_channel_layout_default(&a->c->ch_layout, 1);+ a->c->bit_rate = 64000;+ a->c->time_base = (AVRational){ 1, 48000 };+ AVDictionary *o = NULL;+ av_dict_set(&o, "application", "lowdelay", 0);+ av_dict_set(&o, "frame_duration", "20", 0);+ int r = avcodec_open2(a->c, codec, &o);+ av_dict_free(&o);+ if (r < 0) { avcodec_free_context(&a->c); return -1; }+ a->f = av_frame_alloc();+ a->pkt = av_packet_alloc();+ return 0;+}++static void on_pcm(const int16_t *s, int k) {+ Aud *a = &A;+ if (!a->c) return;+ while (k > 0) {+ int take = 960 - a->have;+ if (take > k) take = k;+ memcpy(a->buf + a->have, s, (size_t)take * 2);+ a->have += take;+ s += take;+ k -= take;+ if (a->have < 960) break;+ a->f->nb_samples = 960;+ a->f->format = AV_SAMPLE_FMT_S16;+ a->f->sample_rate = 48000;+ av_channel_layout_default(&a->f->ch_layout, 1);+ if (av_frame_get_buffer(a->f, 0) < 0) return;+ memcpy(a->f->data[0], a->buf, 960 * 2);+ a->f->pts = a->pts;+ a->pts += 960;+ a->have = 0;+ if (avcodec_send_frame(a->c, a->f) >= 0)+ while (avcodec_receive_packet(a->c, a->pkt) >= 0) {+ send_packet(1, a->pkt->data, (size_t)a->pkt->size, a->pkt->pts, 0);+ av_packet_unref(a->pkt);+ }+ av_frame_unref(a->f);+ }+}++void lm_stream(const char *params, int ring_fd, int render) {+ char cname[16] = "";+ mp_param(params, "codec", cname, sizeof cname);+ int codec = !strcmp(cname, "vp8") ? CODEC_VP8 : !strcmp(cname, "h264") ? CODEC_H264 : 0;+ int w = (int)mp_param_int(params, "width", 0), h = (int)mp_param_int(params, "height", 0);+ int fps = (int)mp_param_int(params, "fps", 30);+ long kbps = mp_param_int(params, "kbps", 4000);+ if (fps < 5 || fps > 60) fps = 30;+ if (kbps < 200 || kbps > 50000) kbps = 4000;+ char accel[24];+ AVBufferRef *hw = codec == CODEC_H264 ? lm_hw_device(lm_hw_accel(params, accel, sizeof accel), render) : NULL;+ if (!codec || w < 16 || h < 16 || w > MP_MAX_DIM || h > MP_MAX_DIM || vid_open(&V, codec, w, h, fps, (unsigned)kbps, hw)) {+ lm_error("media.noVideoEncoder");+ mp_sendv(LM_SOCK, MM_STATE, 0, MS_FAILED, 0, 0, 0);+ return;+ }+ size_t ring_len = 0;+ void *ring = lm_mic_ring(ring_fd, &ring_len);+ if (ring_fd >= 0) close(ring_fd);+ if (ring && aud_open(&A)) { munmap(ring, ring_len); ring = NULL; }+ mp_sendv(LM_SOCK, MM_STATE, 0, MS_RECORDING, V.hw, 0, 0);+ static char payload[MP_PAYLOAD_MAX + 1];+ uint8_t *rmap = NULL;+ size_t ring_bytes = 0;+ int rw = 0, rh = 0, slots = 0, stop = 0;+ long frames = 0;+ double enc_ms = 0, last_stats = lm_now_ms();+ while (!stop) {+ struct pollfd pf[1] = { { LM_SOCK, POLLIN, 0 } };+ if (poll(pf, 1, ring ? 10 : 200) < 0 && errno != EINTR) break;+ if (pf[0].revents & (POLLHUP | POLLERR)) break;+ if (pf[0].revents & POLLIN) {+ MediaMsg m;+ int fds[MP_MAX_FDS], r;+ while ((r = mp_recv(LM_SOCK, &m, payload, sizeof payload, fds, 0)) != -1) {+ if (r == 0 || m.type == MM_STOP) { stop = 1; break; }+ if (m.type == MM_INRING && fds[0] >= 0 && !rmap) {+ struct stat st;+ rw = (int)m.v[1]; rh = (int)m.v[2]; slots = (int)m.v[0];+ ring_bytes = (size_t)rw * rh * 4 * (size_t)slots;+ if (rw == w && rh == h && slots > 0 && slots <= 4 && !fstat(fds[0], &st) && (size_t)st.st_size >= ring_bytes &&+ (fcntl(fds[0], F_GET_SEALS) & F_SEAL_SHRINK)) {+ void *mm = mmap(NULL, ring_bytes, PROT_READ, MAP_SHARED, fds[0], 0);+ if (mm != MAP_FAILED) rmap = mm;+ }+ } else if (m.type == MM_INFRAME && rmap && m.id < (uint32_t)slots) {+ double t0 = lm_now_ms();+ if (vid_frame(&V, rmap + (size_t)m.id * rw * rh * 4, rw, rh, m.v[0]) >= 0) frames++;+ enc_ms += lm_now_ms() - t0;+ mp_sendv(LM_SOCK, MM_SLOTFREE, m.id, 0, 0, 0, 0);+ } else if (m.type == MM_KEYFRAME) V.force_key = 1;+ else if (m.type == MM_RATE && m.v[0] >= 200 && m.v[0] <= 50000) vid_rate(&V, (unsigned)m.v[0]);+ for (int i = 0; i < MP_MAX_FDS; i++) if (fds[i] >= 0) close(fds[i]);+ }+ }+ if (ring) lm_mic_drain(ring, on_pcm);+ if (lm_now_ms() - last_stats >= 2000) {+ mp_sendv(LM_SOCK, MM_STATS, 0, frames, frames ? (int64_t)(enc_ms * 100 / frames) : 0, V.hw, V.kbps);+ last_stats = lm_now_ms();+ }+ }+ LM_LOG("remote desktop video: %ld frames, %.2f ms each with %s", frames, frames ? enc_ms / frames : 0.0, V.name);+ vid_close(&V);+ if (A.c) { avcodec_free_context(&A.c); av_frame_free(&A.f); av_packet_free(&A.pkt); }+ if (ring) munmap(ring, ring_len);+ if (rmap) munmap(rmap, ring_bytes);+ mp_sendv(LM_SOCK, MM_STATE, 0, MS_DONE, 0, 0, 0);+}diff --git a/server/src/media/media_int.h b/server/src/media/media_int.hindex dcf41ef..79aa2c7 100644--- a/server/src/media/media_int.h+++ b/server/src/media/media_int.h@@ -14,7 +14,7 @@typedef struct MAudio MAudio;-enum { SK_NONE = 0, SK_PLAYER, SK_CAMERA, SK_IMAGE, SK_CAPTURE, SK_SCREENREC, SK_AUDIOREC };+enum { SK_NONE = 0, SK_PLAYER, SK_CAMERA, SK_IMAGE, SK_CAPTURE, SK_SCREENREC, SK_AUDIOREC, SK_STREAM };/* How a source is shown in its node. */typedef struct {diff --git a/server/src/media/mstream.c b/server/src/media/mstream.cnew file mode 100644index 0000000..2d7671d--- /dev/null+++ b/server/src/media/mstream.c@@ -0,0 +1,239 @@+/* mstream.c — the remote desktop's video on the session side (docs/REMOTE.md, "Video (WebRTC)").+ *+ * The session renders once; the frame the remote desktop sees (protected windows in black, from+ * media_composed or the GPU's read-back) feeds one live encoder per codec in use: lord-media in+ * MK_STREAM mode, through a sealed memfd of 3 slots, as for the screen recording. Every viewer of this+ * session gets the same encoded frames (remote/mirror.c sends them to each bridge); only the+ * encryption is per connection. Two users means two sessions, two lord-servers and two encoders.+ * If the encoder is behind (no free slot) the frame waits: the newest one goes when a slot frees up,+ * so a slow machine sends fewer frames, never old ones. Sound: the session's default output,+ * recorded by lord-server itself (maudio.c, PipeWire) into a ring the encoder reads. */+#include "mstream.h"+#include "media_int.h"++#include <fcntl.h>+#include <stdio.h>+#include <stdlib.h>+#include <string.h>+#include <sys/mman.h>+#include <time.h>+#include <unistd.h>++#define FPS 30+#define MAX_FRAME (8u << 20)++typedef struct {+ int id; /* the MediaSrc of the helper (0: not running) */+ int viewers, audio;+ unsigned kbps, sent_kbps;+ int w, h, pending, want_key;+ double t0, last_feed, retry_at;+ uint8_t *buf;+ size_t len, cap, total;+ long frames, enc100;+ int hw;+} Enc;++static Enc E[MS_CODECS];+static MStreamOut out;+static const uint32_t *fpx;+static int fstride, fw, fh;++static double now_ms(void) {+ struct timespec t;+ clock_gettime(CLOCK_MONOTONIC, &t);+ return t.tv_sec * 1e3 + t.tv_nsec / 1e6;+}++void mstream_init(const MStreamOut *o) { if (o) out = *o; }++static MediaSrc *src_of(Enc *e) {+ MediaSrc *s = e->id ? media_src_get(e->id) : NULL;+ return s && s->kind == SK_STREAM ? s : NULL;+}++static Enc *enc_of(const MediaSrc *s) {+ for (int c = 1; c < MS_CODECS; c++) if (E[c].id && E[c].id == s->id) return &E[c];+ return NULL;+}++/* Messages of our helpers, before media.c's own handling (which keeps MM_SLOTFREE and MM_STATE). */+static int on_msg(void *src, const MediaMsg *m, const char *payload, int *fds) {+ (void)fds;+ MediaSrc *s = src;+ Enc *e = enc_of(s);+ if (!e) return 0;+ int codec = (int)(e - E);+ if (m->type == MM_PACKET) {+ int64_t total = m->v[0], off = m->v[1];+ if (m->id == 1) { /* Opus: one chunk */+ if (off == 0 && total == (int64_t)m->len && m->len > 0 && m->len <= 1500 && out.audio)+ out.audio(codec, (uint32_t)m->v[2], (const uint8_t *)payload, m->len);+ return 1;+ }+ if (m->id != 0 || total <= 0 || total > (int64_t)MAX_FRAME || off < 0 || !m->len) return 1;+ if (off == 0) { e->len = 0; e->total = (size_t)total; }+ if ((size_t)off != e->len || (size_t)total != e->total || e->len + m->len > e->total) { e->len = 0; e->total = 0; e->want_key = 1; return 1; }+ if (e->len + m->len > e->cap) {+ size_t cap = e->cap ? e->cap : 65536;+ while (cap < e->len + m->len) cap *= 2;+ uint8_t *nb = realloc(e->buf, cap);+ if (!nb) { e->len = 0; return 1; }+ e->buf = nb;+ e->cap = cap;+ }+ memcpy(e->buf + e->len, payload, m->len);+ e->len += m->len;+ if (e->len == e->total) {+ if (out.video) out.video(codec, m->v[3] != 0, (uint32_t)m->v[2], e->buf, e->len);+ e->len = e->total = 0;+ }+ return 1;+ }+ if (m->type == MM_STATS) { e->frames = (long)m->v[0]; e->enc100 = (long)m->v[1]; e->hw = m->v[2] != 0; return 1; }+ if (m->type == MM_STATE && m->v[0] == MS_RECORDING) e->hw = m->v[1] != 0;+ return 0;+}++static void stop(Enc *e) {+ MediaSrc *s = src_of(e);+ if (s) { s->closing = 0; s->state = MS_DONE; media_src_close(s); }+ e->id = 0;+ e->len = e->total = 0;+}++static int start(Enc *e, int codec) {+ if (!fpx || fw < 16 || fh < 16 || now_ms() < e->retry_at) return -1;+ MediaSrc *s = media_src_new(SK_STREAM, "remote");+ if (!s) return -1;+ int ring = -1, render = codec == 1 ? media_render_fd() : -1;+ if (e->audio) s->mic = maudio_open(1, "LordOS", "Remote desktop", "@DEFAULT_MONITOR@", &ring);+ char params[200];+ snprintf(params, sizeof params, "codec=%s\nwidth=%d\nheight=%d\nfps=%d\nkbps=%u\nhw=%d\nhwaccel=%s\naudio=%d\nrender=%d\n",+ codec == 1 ? "h264" : "vp8", fw, fh, FPS, e->kbps, render >= 0, media_hwaccel(), ring >= 0, render >= 0);+ int fds[2] = { ring, render };+ s->sock = media_spawn(MK_STREAM, params, fds, 2, &s->pid);+ if (render >= 0) close(render);+ if (s->sock < 0) { media_src_close(s); e->retry_at = now_ms() + 10000; return -1; }+ s->bk_msg = on_msg;+ /* input slots: lord-server writes, lord-media only reads */+ int slots = 3;+ size_t n = (size_t)fw * fh * 4 * slots;+ int fd = memfd_create("lordos-remote", MFD_CLOEXEC | MFD_ALLOW_SEALING);+ void *m = MAP_FAILED;+ if (fd >= 0 && !ftruncate(fd, (off_t)n) && !fcntl(fd, F_ADD_SEALS, F_SEAL_SHRINK | F_SEAL_GROW | F_SEAL_SEAL))+ m = mmap(NULL, n, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);+ int64_t v[6] = { slots, fw, fh, 0, 0, 0 };+ if (m == MAP_FAILED || mp_send(s->sock, MM_INRING, 0, v, NULL, 0, &fd, 1)) {+ if (m != MAP_FAILED) munmap(m, n);+ if (fd >= 0) close(fd);+ media_src_close(s);+ e->retry_at = now_ms() + 10000;+ return -1;+ }+ s->in_map = m;+ s->in_len = n;+ s->in_fd = fd;+ s->in_w = fw;+ s->in_h = fh;+ s->in_slots = slots;+ memset(s->in_busy, 0, sizeof s->in_busy);+ e->id = s->id;+ e->w = fw;+ e->h = fh;+ e->t0 = now_ms();+ e->last_feed = 0;+ e->pending = 1;+ e->sent_kbps = e->kbps;+ e->want_key = 0;+ fprintf(stderr, "remote: video encoder for %s started (%dx%d, %u kbit/s%s)\n", codec == 1 ? "H.264" : "VP8", fw, fh, e->kbps,+ ring >= 0 ? ", with sound" : "");+ return 0;+}++int mstream_active(void) {+ for (int c = 1; c < MS_CODECS; c++) if (E[c].id) return 1;+ return 0;+}++void mstream_want(int codec, int viewers, int audio, unsigned kbps) {+ if (codec < 1 || codec >= MS_CODECS) return;+ Enc *e = &E[codec];+ int was = e->viewers;+ e->viewers = viewers;+ e->kbps = kbps < 300 ? 300 : kbps > 20000 ? 20000 : kbps;+ if (!viewers) {+ if (e->id) fprintf(stderr, "remote: video encoder for %s stopped\n", codec == 1 ? "H.264" : "VP8");+ stop(e);+ e->audio = 0;+ return;+ }+ if (audio && !e->audio && e->id) stop(e); /* sound joins: restart with the ring */+ e->audio = e->audio || audio;+ if (viewers > was) e->want_key = 1; /* a new viewer needs a keyframe */+ if (!e->id) start(e, codec);+}++void mstream_keyframe(int codec) {+ if (codec >= 1 && codec < MS_CODECS) E[codec].want_key = 1;+}++static void feed(Enc *e, int codec) {+ MediaSrc *s = src_of(e);+ if (!s || s->sock < 0) {+ if (e->id) { fprintf(stderr, "remote: the video encoder stopped; starting it again\n"); e->id = 0; e->retry_at = now_ms() + 2000; }+ start(e, codec);+ return;+ }+ if (!e->pending || !fpx) return;+ double t = now_ms();+ if (t - e->last_feed < 1000.0 / FPS - 1) return;+ int slot = -1;+ for (int i = 0; i < s->in_slots; i++) if (!s->in_busy[i]) { slot = i; break; }+ if (slot < 0) return; /* the encoder is behind: the newest frame waits */+ uint32_t *dst = (uint32_t *)(s->in_map + (size_t)slot * e->w * e->h * 4);+ for (int y = 0; y < e->h; y++) memcpy(dst + (size_t)y * e->w, fpx + (size_t)y * fstride, (size_t)e->w * 4);+ if (e->kbps != e->sent_kbps) { mp_sendv(s->sock, MM_RATE, 0, e->kbps, 0, 0, 0); e->sent_kbps = e->kbps; }+ if (e->want_key) { mp_sendv(s->sock, MM_KEYFRAME, 0, 0, 0, 0, 0); e->want_key = 0; }+ if (mp_sendv(s->sock, MM_INFRAME, (uint32_t)slot, (int64_t)(t - e->t0), 0, 0, 0)) return;+ s->in_busy[slot] = 1;+ e->last_feed = t;+ e->pending = 0;+}++void mstream_frame(const uint32_t *px, int stride, int w, int h) {+ fpx = px;+ fstride = stride;+ fw = w & ~1;+ fh = h & ~1;+ for (int c = 1; c < MS_CODECS; c++) {+ Enc *e = &E[c];+ if (!e->viewers) continue;+ if (e->id && (e->w != fw || e->h != fh)) stop(e); /* new screen size: a new encoder */+ e->pending = 1;+ feed(e, c);+ }+}++void mstream_tick(void) {+ for (int c = 1; c < MS_CODECS; c++) if (E[c].viewers) feed(&E[c], c);+}++double mstream_timeout(void) {+ double best = -1, t = now_ms();+ for (int c = 1; c < MS_CODECS; c++) {+ Enc *e = &E[c];+ if (!e->viewers) continue;+ double d = !e->id ? (e->retry_at > t ? e->retry_at - t : 200) : e->pending ? 1000.0 / FPS - (t - e->last_feed) : -1;+ if (d >= 0 && d < 4) d = 4;+ if (d >= 0 && (best < 0 || d < best)) best = d;+ }+ return best;+}++void mstream_describe(int codec, char *o, size_t n) {+ if (codec < 1 || codec >= MS_CODECS || !E[codec].id) { snprintf(o, n, "-"); return; }+ const Enc *e = &E[codec];+ snprintf(o, n, "%s (%s) %dx%d %u kbit/s, %ld frames, %.2f ms/frame", codec == 1 ? "H.264" : "VP8", e->hw ? "VA-API" : "software",+ e->w, e->h, e->kbps, e->frames, e->enc100 / 100.0);+}diff --git a/server/src/media/mstream.h b/server/src/media/mstream.hnew file mode 100644index 0000000..8cc7309--- /dev/null+++ b/server/src/media/mstream.h@@ -0,0 +1,29 @@+/* mstream.h — the remote desktop's video, on the session side (see mstream.c, docs/REMOTE.md,+ * "Video (WebRTC)"): one live encoder per codec in use, shared by every viewer of this session. */+#ifndef LORD_MEDIA_MSTREAM_H+#define LORD_MEDIA_MSTREAM_H++#include <stddef.h>+#include <stdint.h>++#define MS_CODECS 3 /* index 1 H.264, 2 VP8 (RW_CODEC_*) */++typedef struct {+ /* an encoded video frame (H.264 Annex B or VP8) or Opus packet, for every viewer of that codec */+ void (*video)(int codec, int key, uint32_t ms, const uint8_t *p, size_t n);+ void (*audio)(int codec, uint32_t samples, const uint8_t *p, size_t n);+} MStreamOut;++void mstream_init(const MStreamOut *out);+/* How many viewers want this codec, with sound, and the lowest bit rate among them. 0 viewers stops it. */+void mstream_want(int codec, int viewers, int audio, unsigned kbps);+/* The session's screen changed (the frame stays valid until the next call or mstream_want(…, 0)). */+void mstream_frame(const uint32_t *px, int stride, int w, int h);+void mstream_keyframe(int codec);+void mstream_tick(void);+double mstream_timeout(void); /* ms until mstream_tick has work, or -1 */+int mstream_active(void); /* an encoder is running */+/* "H.264 (VA-API) 30 fps 2.1 ms/frame" for the logs */+void mstream_describe(int codec, char *out, size_t n);++#endifdiff --git a/server/src/media/proto.h b/server/src/media/proto.hindex da3c5f5..642cc2d 100644--- a/server/src/media/proto.h+++ b/server/src/media/proto.h@@ -33,6 +33,8 @@ enum {MK_AUDIOREC, /* audio recorder: fds = wav output, microphone ring */MK_HWPROBE, /* which video codecs VA-API decodes: fds = render node → one MM_RESULTwith JSON {"vaapi":bool,"vendor":"…","vp9":bool,"av1":bool,"h264":bool,"hevc":bool} */+ MK_STREAM, /* remote desktop live encoder (docs/REMOTE.md, "Video (WebRTC)"): fds = audio ring+ (or -1), render node (or -1); params codec=h264|vp8, width, height, fps, kbps */};/* Jobs of the MK_WORKER helper (MM_JOB with v[0] = MW_*). */@@ -63,6 +65,8 @@ enum {MM_PHOTO, /* camera: fd = jpeg output; v[0] = mirrored */MM_RECORD, /* camera: fds = mp4 output, microphone ring (or no fd); no fds: finish */MM_LOOP, /* v[0] = 1: start over when it ends */+ MM_KEYFRAME, /* live encoder: the next frame is a keyframe */+ MM_RATE, /* live encoder: v[0] = target kbit/s *//* lord-media → lord-server */MM_INFO = 64, /* payload = JSON with the file's information */@@ -77,6 +81,8 @@ enum {MM_SLOTFREE, /* id = input slot already processed */MM_ERROR, /* payload = message */MM_AUDIO, /* there's new PCM in the audio ring (or a seek: discard_to/serial) */+ MM_PACKET, /* live encoder: id = 0 video, 1 audio; v[0] = total bytes, v[1] = offset of this+ chunk, v[2] = pts (ms for video, 48 kHz samples for audio), v[3] = 1 keyframe */};enum { MS_LOADING = 0, MS_PLAYING, MS_PAUSED, MS_ENDED, MS_RECORDING, MS_DONE, MS_FAILED };diff --git a/server/src/remote/client.html b/server/src/remote/client.htmlindex fe4b4e8..5f76cdd 100644--- a/server/src/remote/client.html+++ b/server/src/remote/client.html@@ -9,7 +9,9 @@* { box-sizing: border-box; }html, body { margin: 0; height: 100%; background: #0b0a14; overflow: hidden; font: 13px system-ui, -apple-system, "Segoe UI", sans-serif; color: #eee; }#wrap { position: fixed; inset: 0; display: grid; place-items: center; }- canvas { display: block; image-rendering: auto; outline: none; touch-action: none; }+ #stage { position: relative; }+ canvas { position: absolute; inset: 0; display: block; image-rendering: auto; outline: none; touch-action: none; }+ video { position: absolute; inset: 0; display: block; visibility: hidden; background: #000; pointer-events: none; }#bar { position: fixed; top: 0; left: 50%; transform: translate(-50%, -34px); transition: transform .18s ease; z-index: 3;display: flex; gap: 10px; align-items: center; padding: 6px 10px 6px 12px; border-radius: 0 0 12px 12px;background: rgba(28,26,44,.92); border: 1px solid rgba(255,255,255,.12); border-top: 0; }@@ -25,21 +27,24 @@</style></head><body>-<div id="wrap"><canvas id="c" tabindex="0"></canvas></div>+<div id="wrap"><div id="stage"><video id="v" autoplay playsinline muted></video><canvas id="c" tabindex="0"></canvas></div></div><div id="rcur"></div>-<div id="bar"><span id="who">LordOS</span><span id="lat">…</span>- <button id="fs">⛶</button><button id="out" class="red">✕</button></div>+<div id="bar"><span id="who">LordOS</span><span id="lat">…</span><span id="mode"></span>+ <button id="snd" hidden>🔇</button><button id="fs">⛶</button><button id="out" class="red">✕</button></div><div id="msg"><div><div id="msgtext"></div><button id="again"></button></div></div><script>'use strict';const L = {- en: { full: 'Full screen', out: 'Disconnect', bye: 'The session ended or the connection was lost.', again: 'Reconnect', conn: 'Connecting…' },- es: { full: 'Pantalla completa', out: 'Desconectar', bye: 'La sesión terminó o se cortó la conexión.', again: 'Reconectar', conn: 'Conectando…' },- pt: { full: 'Tela cheia', out: 'Desconectar', bye: 'A sessão terminou ou a conexão caiu.', again: 'Reconectar', conn: 'Conectando…' },+ en: { full: 'Full screen', out: 'Disconnect', bye: 'The session ended or the connection was lost.', again: 'Reconnect', conn: 'Connecting…',+ sndOn: 'Turn sound on', sndOff: 'Turn sound off', video: 'Video', tiles: 'Images' },+ es: { full: 'Pantalla completa', out: 'Desconectar', bye: 'La sesión terminó o se cortó la conexión.', again: 'Reconectar', conn: 'Conectando…',+ sndOn: 'Activar el sonido', sndOff: 'Silenciar', video: 'Video', tiles: 'Imágenes' },+ pt: { full: 'Tela cheia', out: 'Desconectar', bye: 'A sessão terminou ou a conexão caiu.', again: 'Reconectar', conn: 'Conectando…',+ sndOn: 'Ativar o som', sndOff: 'Silenciar', video: 'Vídeo', tiles: 'Imagens' },};const T = L[(navigator.language || 'en').slice(0, 2)] || L.en;const $ = (id) => document.getElementById(id);-const cv = $('c'), ctx = cv.getContext('2d', { alpha: false });+const cv = $('c'), ctx = cv.getContext('2d'), vid = $('v'), stage = $('stage');$('fs').title = T.full; $('out').title = T.out; $('again').textContent = T.again;// KeyboardEvent.code → evdev code (linux/input-event-codes.h)@@ -64,8 +69,7 @@ function fit() {const sw = innerWidth, sh = innerHeight;scale = Math.min(sw / W, sh / H);if (Math.abs(scale - 1) < 0.02) scale = 1;- cv.style.width = Math.round(W * scale) + 'px';- cv.style.height = Math.round(H * scale) + 'px';+ for (const el of [stage, cv, vid]) { el.style.width = Math.round(W * scale) + 'px'; el.style.height = Math.round(H * scale) + 'px'; }const r = cv.getBoundingClientRect();ox = r.left; oy = r.top;}@@ -78,22 +82,109 @@ addEventListener('resize', () => {});function send(o) { if (ws && ws.readyState === 1) ws.send(JSON.stringify(o)); }+// Input goes over the WebRTC data channel when it's open (lower latency), else over the WebSocket.+function sendIn(o) {+ const j = JSON.stringify(o);+ if (dcIn && dcIn.readyState === 'open' && j.length < 200000) dcIn.send(j); else send(o);+}++// ---- video (WebRTC): H.264/VP8 + Opus from the session's encoder; input and the lossless tiles of+// still areas over data channels. The tiles over the WebSocket stay until the video plays, and come+// back if it fails (docs/REMOTE.md, "Video (WebRTC)").+let pc = null, dcIn = null, vmode = false, rtcTimer = 0, offered = false, codec = '';+function setMode() { $('mode').textContent = vmode ? `${T.video} · ${codec.toUpperCase()}` : T.tiles; }+function giveUp(tell) {+ clearTimeout(rtcTimer);+ if (pc) { try { pc.close(); } catch (e) {} }+ pc = null; dcIn = null; offered = false;+ const was = vmode;+ vmode = false;+ vid.style.visibility = 'hidden'; vid.srcObject = null; $('snd').hidden = true;+ if (was) { ctx.fillStyle = '#000'; ctx.fillRect(0, 0, W, H); }+ if (tell) send({ t: 'vmode', on: 0 });+ setMode();+}+function enterVideo() {+ if (vmode || !pc) return;+ clearTimeout(rtcTimer);+ vmode = true;+ queue = queue.then(() => ctx.clearRect(0, 0, W, H));+ vid.style.visibility = 'visible';+ $('snd').hidden = false;+ send({ t: 'vmode', on: 1 });+ setMode();+}+function startRtc(ice) {+ try {+ pc = new RTCPeerConnection({ iceServers: (ice || []).map((u) => ({ urls: u })), bundlePolicy: 'max-bundle', rtcpMuxPolicy: 'require' });+ } catch (e) { pc = null; return; }+ pc.addTransceiver('video', { direction: 'recvonly' });+ pc.addTransceiver('audio', { direction: 'recvonly' });+ dcIn = pc.createDataChannel('input', { ordered: true });+ const tiles = pc.createDataChannel('tiles', { ordered: true });+ tiles.binaryType = 'arraybuffer';+ tiles.onmessage = (e) => onTile(new Uint8Array(e.data));+ pc.ontrack = (e) => {+ const ms = e.streams[0] || new MediaStream([e.track]);+ if (vid.srcObject !== ms) vid.srcObject = ms;+ vid.play().catch(() => {});+ };+ pc.onicecandidate = (e) => { if (offered && e.candidate && e.candidate.candidate) send({ t: 'ice', c: e.candidate.candidate }); };+ pc.onconnectionstatechange = () => { if (pc && (pc.connectionState === 'failed' || pc.connectionState === 'closed')) giveUp(true); };+ const gathered = () => new Promise((ok) => {+ if (pc.iceGatheringState === 'complete') return ok();+ const t = setTimeout(ok, 1500);+ pc.addEventListener('icegatheringstatechange', () => { if (pc && pc.iceGatheringState === 'complete') { clearTimeout(t); ok(); } });+ });+ pc.createOffer().then((o) => pc.setLocalDescription(o)).then(gathered)+ .then(() => { if (pc) { send({ t: 'rtc', sdp: pc.localDescription.sdp }); offered = true; } })+ .catch(() => giveUp(true));+ rtcTimer = setTimeout(() => { if (!vmode) giveUp(true); }, 12000);+}+const onFrame = () => { if (vid.videoWidth > 0) enterVideo(); else vid.requestVideoFrameCallback?.(onFrame); };+vid.addEventListener('playing', () => { if (vid.videoWidth > 0) enterVideo(); });+vid.addEventListener('loadeddata', onFrame);+// [1][x,y,w,h u16][png]: a lossless tile · [2][x,y,w,h…]: those areas changed (show the video there) · [3]: done+function onTile(d) {+ if (!vmode) return;+ if (d[0] === 1 && d.length > 9) {+ const x = d[1] << 8 | d[2], y = d[3] << 8 | d[4], w = d[5] << 8 | d[6], h = d[7] << 8 | d[8];+ const p = createImageBitmap(new Blob([d.subarray(9)], { type: 'image/png' }));+ queue = queue.then(() => p).then((bmp) => { if (vmode) ctx.drawImage(bmp, x, y, w, h); bmp.close(); }, () => {});+ } else if (d[0] === 2) {+ const r = [];+ for (let i = 1; i + 8 <= d.length; i += 8) r.push([d[i] << 8 | d[i + 1], d[i + 2] << 8 | d[i + 3], d[i + 4] << 8 | d[i + 5], d[i + 6] << 8 | d[i + 7]]);+ queue = queue.then(() => { for (const [x, y, w, h] of r) ctx.clearRect(x, y, w, h); });+ }+}+$('snd').onclick = () => {+ vid.muted = !vid.muted;+ if (!vid.muted) vid.play().catch(() => {});+ $('snd').textContent = vid.muted ? '🔇' : '🔊';+ $('snd').title = vid.muted ? T.sndOn : T.sndOff;+};function show(text) { $('msgtext').textContent = text; $('msg').style.display = 'grid'; }function connect() {$('msg').style.display = 'none';const w = Math.round(innerWidth * devicePixelRatio > 2560 ? innerWidth : innerWidth), h = Math.round(innerHeight);- ws = new WebSocket(`wss://${location.host}/ws?w=${w}&h=${h}`);+ giveUp(false);+ const rtcOk = typeof RTCPeerConnection === 'function' ? 1 : 0;+ ws = new WebSocket(`wss://${location.host}/ws?w=${w}&h=${h}&v=${rtcOk}`);ws.binaryType = 'arraybuffer';ws.onopen = () => send({ t: 'resize', w: w & ~1, h: h & ~1 }); // an already existing session takes this sizews.onmessage = (e) => (typeof e.data === 'string' ? onText(JSON.parse(e.data)) : onBin(new Uint8Array(e.data)));- ws.onclose = () => { ws = null; show(T.bye); };+ ws.onclose = () => { ws = null; giveUp(false); show(T.bye); };}function onText(m) {if (m.t === 'size') { W = m.w; H = m.h; cv.width = W; cv.height = H; ctx.fillStyle = '#000'; ctx.fillRect(0, 0, W, H); fit(); }- else if (m.t === 'hello') $('who').textContent = `${m.user}@${location.hostname}`;+ else if (m.t === 'hello') { $('who').textContent = `${m.user}@${location.hostname}`; setMode(); if (m.rtc && Array.isArray(m.ice)) startRtc(m.ice); }+ else if (m.t === 'rtc') {+ if (m.fail || !pc) giveUp(false);+ else if (m.sdp) { codec = m.codec || ''; pc.setRemoteDescription({ type: 'answer', sdp: m.sdp }).catch(() => giveUp(true)); }+ }else if (m.t === 'pong') $('lat').textContent = `${Math.max(0, Math.round(performance.now() - m.ts))} ms`;else if (m.t === 'cur') {// cursor moved by someone else (the monitor or another connection): drawn on top@@ -130,7 +221,7 @@ function pos(e) {const x = Math.round((e.clientX - ox) / scale), y = Math.round((e.clientY - oy) / scale);return [Math.max(0, Math.min(W - 1, x)), Math.max(0, Math.min(H - 1, y))];}-function flushMove() { if (pend) { send({ t: 'm', x: pend[0], y: pend[1] }); pend = null; } }+function flushMove() { if (pend) { sendIn({ t: 'm', x: pend[0], y: pend[1] }); pend = null; } }cv.addEventListener('pointermove', (e) => {[mx, my] = pos(e);if (!pend) requestAnimationFrame(flushMove);@@ -139,21 +230,21 @@ cv.addEventListener('pointermove', (e) => {cv.addEventListener('pointerdown', (e) => {cv.focus(); cv.setPointerCapture(e.pointerId);pend = pos(e); flushMove();- if (e.button <= 2) send({ t: 'b', b: e.button, d: 1 });+ if (e.button <= 2) sendIn({ t: 'b', b: e.button, d: 1 });e.preventDefault();});-cv.addEventListener('pointerup', (e) => { pend = pos(e); flushMove(); if (e.button <= 2) send({ t: 'b', b: e.button, d: 0 }); });+cv.addEventListener('pointerup', (e) => { pend = pos(e); flushMove(); if (e.button <= 2) sendIn({ t: 'b', b: e.button, d: 0 }); });cv.addEventListener('contextmenu', (e) => e.preventDefault());// Wheel: a mouse sends steps ('w'); a trackpad, pixels with a phase ('s': 1 begins, 2 continues, 3// ends) and pinch arrives as a wheel with ctrlKey (Chrome, Firefox) or gesturechange (WebKit browsers)// ('pz': scale relative to the start). Gestures end after 150 ms without events.let scrollOn = false, scrollT = 0, pinchOn = false, pinchT = 0, pinchScale = 1, pinchRot = 0;function pinch(scale, rot) {- if (!pinchOn) { pinchOn = true; pinchScale = 1; pinchRot = 0; send({ t: 'pz', p: 1, s: 65536, r: 0 }); }+ if (!pinchOn) { pinchOn = true; pinchScale = 1; pinchRot = 0; sendIn({ t: 'pz', p: 1, s: 65536, r: 0 }); }pinchScale = Math.max(0.1, Math.min(10, scale)); pinchRot = rot;- send({ t: 'pz', p: 2, s: Math.round(pinchScale * 65536), r: Math.round(pinchRot * 256) });+ sendIn({ t: 'pz', p: 2, s: Math.round(pinchScale * 65536), r: Math.round(pinchRot * 256) });clearTimeout(pinchT);- pinchT = setTimeout(() => { pinchOn = false; send({ t: 'pz', p: 3, s: Math.round(pinchScale * 65536), r: Math.round(pinchRot * 256) }); }, 150);+ pinchT = setTimeout(() => { pinchOn = false; sendIn({ t: 'pz', p: 3, s: Math.round(pinchScale * 65536), r: Math.round(pinchRot * 256) }); }, 150);}cv.addEventListener('wheel', (e) => {e.preventDefault();@@ -162,13 +253,13 @@ cv.addEventListener('wheel', (e) => {const wheelLike = e.deltaMode !== 0 || (!e.deltaX && Number.isInteger(e.deltaY) && Math.abs(e.deltaY) >= 50 && !scrollOn);if (wheelLike) {const steps = e.deltaMode === 1 ? -Math.sign(e.deltaY) * Math.max(1, Math.round(Math.abs(e.deltaY) / 3)) : -Math.sign(e.deltaY) * Math.max(1, Math.round(Math.abs(e.deltaY) / 100));- if (steps) send({ t: 'w', d: steps });+ if (steps) sendIn({ t: 'w', d: steps });return;}- if (!scrollOn) { scrollOn = true; send({ t: 's', p: 1, x: 0, y: 0 }); }- send({ t: 's', p: 2, x: Math.round(e.deltaX * k / scale * 256), y: Math.round(e.deltaY * k / scale * 256) });+ if (!scrollOn) { scrollOn = true; sendIn({ t: 's', p: 1, x: 0, y: 0 }); }+ sendIn({ t: 's', p: 2, x: Math.round(e.deltaX * k / scale * 256), y: Math.round(e.deltaY * k / scale * 256) });clearTimeout(scrollT);- scrollT = setTimeout(() => { scrollOn = false; send({ t: 's', p: 3, x: 0, y: 0 }); }, 150);+ scrollT = setTimeout(() => { scrollOn = false; sendIn({ t: 's', p: 3, x: 0, y: 0 }); }, 150);}, { passive: false });cv.addEventListener('gesturestart', (e) => e.preventDefault());cv.addEventListener('gesturechange', (e) => { e.preventDefault(); pinch(e.scale, e.rotation || 0); });@@ -181,15 +272,15 @@ function key(e, down) {if (down && e.code === 'KeyV' && (e.ctrlKey || e.metaKey)) return;e.preventDefault();if (down) held.add(c); else held.delete(c);- send({ t: 'k', c, d: down ? 1 : 0 });+ sendIn({ t: 'k', c, d: down ? 1 : 0 });}cv.addEventListener('keydown', (e) => key(e, true));cv.addEventListener('keyup', (e) => key(e, false));-cv.addEventListener('blur', () => { for (const c of held) send({ t: 'k', c, d: 0 }); held.clear(); });+cv.addEventListener('blur', () => { for (const c of held) sendIn({ t: 'k', c, d: 0 }); held.clear(); });document.addEventListener('paste', (e) => {const text = e.clipboardData && e.clipboardData.getData('text/plain');- if (text && text !== lastSent) { send({ t: 'clip', text }); lastSent = text; }- send({ t: 'k', c: 47, d: 1 }); send({ t: 'k', c: 47, d: 0 });+ if (text && text !== lastSent) { sendIn({ t: 'clip', text }); lastSent = text; }+ sendIn({ t: 'k', c: 47, d: 1 }); sendIn({ t: 'k', c: 47, d: 0 });e.preventDefault();});diff --git a/server/src/remote/conn.c b/server/src/remote/conn.cindex 2588cc1..7b745b4 100644--- a/server/src/remote/conn.c+++ b/server/src/remote/conn.c@@ -17,13 +17,22 @@* 4. Bridge: session rectangles → PNG/JPEG → browser; mouse, keyboard and clipboard from the* browser (validated, rsec.c rs_browser_input) → session. Flow control: the browser* acknowledges each frame ("ack") and only then does the session build the next one.+ * 5. Video (rtc.c, docs/REMOTE.md "Video (WebRTC)"): if the browser can, it sends a WebRTC offer+ * over this same WebSocket; the bridge answers, the session's shared encoder feeds it H.264 or+ * VP8 and Opus (RP_VFRAME, RP_AFRAME), input and the lossless tiles of still areas go over data+ * channels. The tiles above stay as the fallback until the video plays, and if it fails.* Browser protocol and the security analysis: docs/REMOTE.md. */#include "sessiond.h"#include "proto.h"#include "rsec.h"+#ifdef LORD_RTC+#include "rtc.h"+#endif+#include <arpa/inet.h>#include <ctype.h>#include <dirent.h>+#include <netinet/in.h>#include <errno.h>#include <fcntl.h>#include <grp.h>@@ -62,6 +71,10 @@ static char peer[48]; /* address as shown */static char peer_key[48]; /* lockout key (IPv4, or the IPv6 /64) */static double deadline;static int lock_fd = -1; /* bridge → root parent: "L" = the session was locked */+#ifdef LORD_RTC+static int want_rtc; /* the page can do WebRTC (/ws?v=1) and Settings allows video */+#endif+static char *turn_pass; /* read as root before the bridge drops root; the bridge's rtc.c owns it */static double mono(void) {struct timespec t;@@ -585,6 +598,13 @@ static int ws_handshake(const RsReq *r) {/* ---- bridge (runs as the user) ---- */static int sess = -1; /* session socket */+static int vmode; /* video mode: tiles go over the data channel, lossless, once still */+#ifdef LORD_RTC+static int tiles_lost; /* a tile didn't fit the data channel: ask for them all again */+static double tiles_lost_at;+static Rtc *rtc;+static int rtc_dead;+#endifstatic uint32_t *fb; /* copy of the screen (to replace with PNG) */static int W, H;typedef struct { uint16_t x, y, w, h; } Box16;@@ -602,6 +622,31 @@ static void sess_send(int type, const void *a, size_t an) {if (an && write(sess, a, an) != (ssize_t)an) return;}+#ifdef LORD_RTC+/* A lossless tile over the "tiles" data channel: [1][x, y, w, h u16 BE][png]; halved until it fits. */+static void dc_tile(int x, int y, int w, int h) {+ int len = 0, jpeg = 0;+ unsigned char *img = rd_encode(fb + (size_t)y * W + x, W, w, h, 0, &len, &jpeg);+ if (!img) return;+ if (len > 250000 && h > 1) {+ free(img);+ dc_tile(x, y, w, h / 2);+ dc_tile(x, y + h / 2, w, h - h / 2);+ return;+ }+ unsigned char *m = malloc((size_t)len + 9);+ if (m) {+ unsigned char hd[9] = { 1, (unsigned char)(x >> 8), (unsigned char)x, (unsigned char)(y >> 8), (unsigned char)y,+ (unsigned char)(w >> 8), (unsigned char)w, (unsigned char)(h >> 8), (unsigned char)h };+ memcpy(m, hd, 9);+ memcpy(m + 9, img, (size_t)len);+ if (rtc_send(rtc, RTC_CH_TILES, m, (size_t)len + 9, 1)) { tiles_lost = 1; tiles_lost_at = rd_now(); }+ free(m);+ }+ free(img);+}+#endif+static void send_rect(int x, int y, int w, int h, int allow_jpeg) {int len = 0, jpeg = 0;unsigned char *img = rd_encode(fb + (size_t)y * W + x, W, w, h, allow_jpeg, &len, &jpeg);@@ -639,14 +684,55 @@ static void from_session(int type, const unsigned char *d, uint32_t n) {/* whatever gets overwritten no longer needs replacing */for (int i = 0; i < njpegs; i++)if (jpegs[i].x >= r[0] && jpegs[i].y >= r[1] && jpegs[i].x + jpegs[i].w <= r[0] + r[2] && jpegs[i].y + jpegs[i].h <= r[1] + r[3]) jpegs[i--] = jpegs[--njpegs];+#ifdef LORD_RTC+ if (vmode) { dc_tile(r[0], r[1], r[2], r[3]); break; } /* the screen is still: lossless */+#endif/* screen in motion (consecutive frames): straight JPEG, replaced with PNG once it's still */send_rect(r[0], r[1], r[2], r[3], rd_now() - last_frame < 0.25 ? 2 : 1);break;}case RP_END:+#ifdef LORD_RTC+ if (vmode) { /* the data channel is reliable: no browser ack needed */+ unsigned char e = 3;+ rtc_send(rtc, RTC_CH_TILES, &e, 1, 1);+ sess_send(RP_READY, NULL, 0);+ break;+ }+#endifws_frame(2, (unsigned char[]){ 3 }, 1, NULL, 0);last_frame = rd_now();break;+#ifdef LORD_RTC+ case RP_VFRAME:+ if (n > 8 && rtc && d[0] == rtc_codec(rtc)) {+ uint32_t ms;+ memcpy(&ms, d + 4, 4);+ rtc_video(rtc, d + 8, n - 8, ms);+ }+ break;+ case RP_AFRAME:+ if (n > 4 && n <= 4 + 1500 && rtc) {+ uint32_t smp;+ memcpy(&smp, d, 4);+ rtc_audio(rtc, d + 4, n - 4, smp);+ }+ break;+ case RP_DAMAGE:+ /* [2][u16 BE x, y, w, h …]: the browser drops its lossless layer there */+ if (vmode && rtc && n >= 8 && n % 8 == 0 && n <= 8 * 256) {+ unsigned char m[1 + 8 * 256];+ m[0] = 2;+ for (uint32_t i = 0; i < n / 2; i++) {+ uint16_t v;+ memcpy(&v, d + i * 2, 2);+ m[1 + i * 2] = (unsigned char)(v >> 8);+ m[2 + i * 2] = (unsigned char)v;+ }+ rtc_send(rtc, RTC_CH_TILES, m, 1 + n, 1);+ }+ break;+#endifcase RP_CURSOR:if (n >= 5) {int16_t x, y;@@ -687,14 +773,29 @@ static void from_session(int type, const unsigned char *d, uint32_t n) {}}-/* One validated browser message → session. */-static void from_browser(const char *j, size_t n) {+#ifdef LORD_RTC+static void video_off(const char *why);+static void video_mode(int on) {+ if (on && (!rtc || !rtc_media_ready(rtc) || !rtc_dc_open(rtc, RTC_CH_TILES))) return;+ if (on == vmode) return;+ vmode = on;+ njpegs = 0;+ unsigned char v = (unsigned char)on;+ sess_send(RP_VMODE, &v, 1);+ sd_log("video: %s", on ? "playing in the browser; tiles only for still areas" : "back to tiles");+}+#endif++/* One validated browser message → session. From a data channel (dc) only the input messages count. */+static void from_browser(const char *j, size_t n, int dc) {char *sb = malloc(n + 1);if (!sb) return;RsJson o;RsInput in;if (rs_json_parse(j, n, &o, sb, n + 1) == 0) {- switch (rs_browser_input(&o, W, H, &in)) {+ int type = rs_browser_input(&o, W, H, &in);+ if (dc && (type == RS_IN_ACK || type == RS_IN_PING || type == RS_IN_RTC || type == RS_IN_ICE || type == RS_IN_VMODE)) type = RS_IN_NONE;+ switch (type) {case RS_IN_PTR: { int16_t p[2] = { (int16_t)in.a, (int16_t)in.b }; sess_send(RP_PTR, p, 4); break; }case RS_IN_BTN: { unsigned char p[2] = { (unsigned char)in.a, (unsigned char)in.b }; sess_send(RP_BTN, p, 2); break; }case RS_IN_WHEEL: { signed char d = (signed char)in.a; sess_send(RP_WHEEL, &d, 1); break; }@@ -719,6 +820,13 @@ static void from_browser(const char *j, size_t n) {case RS_IN_RESIZE: { uint16_t sz[2] = { (uint16_t)in.a, (uint16_t)in.b }; sess_send(RP_RESIZE, sz, 4); break; }case RS_IN_PING: ws_text("{\"t\":\"pong\",\"ts\":%lld}", in.ts); break;case RS_IN_CLIP: sess_send(RP_SETCLIP, in.text, in.tlen); break;+#ifdef LORD_RTC+ case RS_IN_RTC:+ if (rtc && rtc_codec(rtc) == 0 && rtc_offer(rtc, in.text, in.tlen, 1) < 0) video_off("the offer was refused");+ break;+ case RS_IN_ICE: if (rtc) rtc_candidate(rtc, in.text, in.tlen); break;+ case RS_IN_VMODE: if (in.a) video_mode(1); else video_off("the browser gave up"); break;+#endifdefault: break;}}@@ -729,7 +837,7 @@ static void from_browser(const char *j, size_t n) {static void on_ws(void *ud, int op, unsigned char *data, size_t len) {(void)ud;if (closing) return;- if (op == 1) from_browser((const char *)data, len);+ if (op == 1) from_browser((const char *)data, len, 0);else if (op == 9) ws_frame(10, data, len, NULL, 0);else if (op == 8) { ws_close(1000); closing = 1; }else if (op == 2) { ws_close(1003); closing = 1; } /* our page never sends binary */@@ -788,11 +896,92 @@ static int conns_full(void) {return n >= RD_MAX_CONNS;}+#ifdef LORD_RTC+static void rtc_signal(void *ud, const char *json) { (void)ud; ws_frame(1, json, strlen(json), NULL, 0); }+static void rtc_state(void *ud, int st, const char *why) {+ (void)ud;+ if (st == RTC_CONNECTED) {+ /* this viewer takes the session's encoded video: u8 codec, u8 sound, u16 0, u32 kbit/s */+ unsigned char v[8] = { (unsigned char)rtc_codec(rtc), 1, 0, 0 };+ uint32_t k = 4000;+ memcpy(v + 4, &k, 4);+ sess_send(RP_VIDEO, v, 8);+ } else if (st == RTC_FAILED) { rtc_dead = 1; (void)why; }+}+static void rtc_message(void *ud, int chan, const uint8_t *p, size_t n, int binary) {+ (void)ud;+ if (chan == RTC_CH_INPUT && !binary && rs_utf8_valid(p, n)) from_browser((const char *)p, n, 1);+}+static void rtc_key(void *ud) { (void)ud; sess_send(RP_KEYFRAME, NULL, 0); }+static void rtc_rate(void *ud, unsigned kbps) { (void)ud; uint32_t k = kbps; sess_send(RP_RATE, &k, 4); }++/* Back to the tiles for good (the video failed or the browser gave up). */+static void video_off(const char *why) {+ if (!rtc) return;+ char st[160];+ rtc_stats(rtc, st, sizeof st);+ sd_log("video: off (%s; %s)", why, st);+ if (vmode) { unsigned char v = 0; sess_send(RP_VMODE, &v, 1); vmode = 0; }+ unsigned char v[8] = { 0 };+ sess_send(RP_VIDEO, v, 8);+ rtc_free(rtc);+ rtc = NULL;+ ws_text("{\"t\":\"rtc\",\"fail\":1}");+}++/* The WebRTC peer, before the hello: its UDP port is also the browser's STUN server. */+static void rtc_start(void) {+ RtcConfig c;+ memset(&c, 0, sizeof c);+ c.access = &rd_conf.access;+ c.net_mode = rd_conf.net_mode;+ rw_addr_text(peer, &c.ws_peer);+ struct sockaddr_storage ls;+ socklen_t ll = sizeof ls;+ if (!getsockname(sock, (struct sockaddr *)&ls, &ll)) {+ char t[64] = "";+ if (ls.ss_family == AF_INET) inet_ntop(AF_INET, &((struct sockaddr_in *)&ls)->sin_addr, t, sizeof t);+ else if (ls.ss_family == AF_INET6) inet_ntop(AF_INET6, &((struct sockaddr_in6 *)&ls)->sin6_addr, t, sizeof t);+ rw_addr_text(t, &c.ws_local);+ }+ c.nstun = rd_conf.nstun;+ for (int i = 0; i < rd_conf.nstun; i++) snprintf(c.stun[i], sizeof c.stun[i], "%s", rd_conf.stun[i]);+ snprintf(c.turn, sizeof c.turn, "%s", rd_conf.turn);+ snprintf(c.turn_user, sizeof c.turn_user, "%s", rd_conf.turn_user);+ c.turn_pass = turn_pass; /* rtc.c owns it now and wipes it */+ turn_pass = NULL;+ c.max_kbps = 12000;+ RtcCallbacks cb = { rtc_signal, rtc_state, rtc_message, rtc_key, rtc_rate, NULL };+ rtc = rtc_new(&c, &cb);+}+#endif+static void bridge(const struct passwd *pw) {char hello[96];snprintf(hello, sizeof hello, "%s %s", peer, pw->pw_name);sess_send(RP_HELLO, hello, strlen(hello));fcntl(sock, F_SETFL, fcntl(sock, F_GETFL) | O_NONBLOCK);+#ifdef LORD_RTC+ if (want_rtc) rtc_start();+ if (rtc) {+ /* the built-in STUN server is this connection's UDP port, at the address the page used */+ char ice[600], ip[64] = "";+ size_t o = 0;+ RwAddr la;+ struct sockaddr_storage ls;+ socklen_t ll = sizeof ls;+ if (!getsockname(sock, (struct sockaddr *)&ls, &ll)) {+ if (ls.ss_family == AF_INET) inet_ntop(AF_INET, &((struct sockaddr_in *)&ls)->sin_addr, ip, sizeof ip);+ else if (ls.ss_family == AF_INET6) inet_ntop(AF_INET6, &((struct sockaddr_in6 *)&ls)->sin6_addr, ip, sizeof ip);+ }+ if (!strncmp(ip, "::ffff:", 7)) memmove(ip, ip + 7, strlen(ip + 7) + 1);+ int v6 = !rw_addr_text(ip, &la) && la.family == 6;+ o += (size_t)snprintf(ice + o, sizeof ice - o, "[\"stun:%s%s%s:%d\"", v6 ? "[" : "", ip, v6 ? "]" : "", rtc_port(rtc));+ for (int i = 0; i < rd_conf.nstun && o < sizeof ice - 120; i++) o += (size_t)snprintf(ice + o, sizeof ice - o, ",\"stun:%s\"", rd_conf.stun[i]);+ snprintf(ice + o, sizeof ice - o, "]");+ ws_text("{\"t\":\"hello\",\"user\":\"%s\",\"rtc\":1,\"ice\":%s}", pw->pw_name, ice);+ } else+#endifws_text("{\"t\":\"hello\",\"user\":\"%s\"}", pw->pw_name);rs_ws_init(&wsr, RS_WS_MAX_MSG);unsigned char *sin = NULL;@@ -810,11 +999,20 @@ static void bridge(const struct passwd *pw) {for (int i = 0; i < 20 && wout_len; i++) { struct pollfd q = { sock, POLLOUT, 0 }; poll(&q, 1, 100); if (ws_flush()) break; }return;}- struct pollfd p[2] = { { sock, (short)(POLLIN | (wout_len ? POLLOUT : 0)), 0 }, { sess, POLLIN, 0 } };+ struct pollfd p[4] = { { sock, (short)(POLLIN | (wout_len ? POLLOUT : 0)), 0 }, { sess, POLLIN, 0 }, { -1, 0, 0 }, { -1, 0, 0 } };+ int np = 2;/* while there is something to send, nothing more is read from the session (flow control) */if (wout_len > (4u << 20)) p[1].events = 0;int timeout = njpegs ? 200 : 15000;- int r = SSL_pending(ssl) ? 1 : poll(p, 2, timeout);+#ifdef LORD_RTC+ if (rtc) {+ np += rtc_pollfds(rtc, p + 2, 2);+ int t = rtc_timeout(rtc);+ if (t >= 0 && t < timeout) timeout = t;+ }+ if (tiles_lost && timeout > 500) timeout = 500;+#endif+ int r = SSL_pending(ssl) ? 1 : poll(p, (nfds_t)np, timeout);if (r < 0 && errno != EINTR) return;if (SSL_pending(ssl) || (p[0].revents & POLLIN)) {if (ws_read()) return;@@ -850,9 +1048,22 @@ static void bridge(const struct passwd *pw) {memmove(sin, sin + off, sin_len - off);sin_len -= off;}+#ifdef LORD_RTC+ if (rtc) {+ if (np > 2) rtc_dispatch(rtc, p + 2, np - 2);+ if (rtc) rtc_tick(rtc);+ if (rtc_dead) { rtc_dead = 0; video_off("connection failed"); }+ /* tiles that didn't fit: the whole lossless layer again, at most every 2 s */+ if (vmode && tiles_lost && rd_now() - tiles_lost_at > 2) {+ tiles_lost = 0;+ unsigned char v = 1;+ sess_send(RP_VMODE, &v, 1);+ }+ }+#endifif (locked) { ws_flush(); return; }/* screen is still: whatever was JPEG is replaced with PNG (lossless) */- if (njpegs && rd_now() - last_frame > 0.4 && wout_len < (1u << 20)) {+ if (njpegs && !vmode && rd_now() - last_frame > 0.4 && wout_len < (1u << 20)) {int k = njpegs;njpegs = 0;for (int i = 0; i < k; i++) send_rect(jpegs[i].x, jpegs[i].y, jpegs[i].w, jpegs[i].h, 0);@@ -935,6 +1146,26 @@ static void handle_ws(const RsReq *r, int logged, const char *user, uid_t uid) {if (h > 1600) h = 1600;sess = sd_session_get(&me, w & ~1, h & ~1);if (sess < 0) { respond(503, NULL); return; }+#ifdef LORD_RTC+ want_rtc = rd_conf.video && query_num(r->query, "v", 0) == 1;+ /* the TURN password, read here as root (RD_TURN_KEY is root 0600) and handed to the bridge's WebRTC+ * peer in memory only; never logged, never sent to the browser */+ if (want_rtc && rd_conf.turn[0] && rd_conf.turn_user[0]) {+ int f = open(RD_TURN_KEY, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);+ struct stat st;+ char buf[160];+ ssize_t got = -1;+ if (f >= 0 && !fstat(f, &st) && S_ISREG(st.st_mode) && st.st_uid == 0 && !(st.st_mode & 077) && st.st_size < (off_t)sizeof buf)+ got = read(f, buf, sizeof buf - 1);+ if (f >= 0) close(f);+ if (got > 0) {+ buf[got] = 0;+ buf[strcspn(buf, "\n")] = 0;+ if (rw_turn_pass_valid(buf)) turn_pass = strdup(buf);+ }+ explicit_bzero(buf, sizeof buf);+ }+#endifif (ws_handshake(r)) return;conn_register(name);int pp[2];@@ -954,6 +1185,7 @@ static void handle_ws(const RsReq *r, int logged, const char *user, uid_t uid) {_exit(0);}/* the root side keeps nothing of the connection: it only listens for "the session was locked" */+ if (turn_pass) { explicit_bzero(turn_pass, strlen(turn_pass)); free(turn_pass); turn_pass = NULL; }close(pp[1]);close(sess);close(sock);diff --git a/server/src/remote/mirror.c b/server/src/remote/mirror.cindex 731a301..8458bfc 100644--- a/server/src/remote/mirror.c+++ b/server/src/remote/mirror.c@@ -14,6 +14,7 @@#include "mirror.h"#include "proto.h"#include "../vault/vclient.h"+#include "../media/mstream.h"#include <errno.h>#include <fcntl.h>@@ -23,11 +24,14 @@#include <sys/socket.h>#include <sys/stat.h>#include <sys/un.h>+#include <time.h>#include <unistd.h>#define TILE 64#define MAX_CLIENTS 8#define OUT_LIMIT (8u << 20) /* with more pending than this, no new frame is built */+#define VIDEO_LIMIT (3u << 20) /* a viewer this far behind skips video until the next keyframe */+#define STILL_MS 250 /* video mode: the lossless tiles go once the screen is this still */typedef struct {int fd;@@ -40,6 +44,10 @@ typedef struct {int seat; /* lord-sessiond (RP_SEAT) */uid_t uid; /* of the other end (SO_PEERCRED) */int fds[40], nfds; /* fds received with the last message (RP_DISPLAY) */+ /* the video path (docs/REMOTE.md, "Video (WebRTC)") */+ int vcodec, vaudio, vneed_key; /* subscribed to the session's encoder (1 H.264, 2 VP8) */+ unsigned vkbps;+ int vmode; /* tiles only when the screen is still, damage right away */} Client;static int lfd = -1;@@ -54,6 +62,18 @@ static int shape_w, shape_h, shape_hx, shape_hy;static unsigned shape_gen;static char *clip_text;static unsigned clip_len, clip_gen_seen;+static unsigned char *chg; /* tiles changed in this frame */+static double last_change;++static double now_ms(void) {+ struct timespec t;+ clock_gettime(CLOCK_MONOTONIC, &t);+ return t.tv_sec * 1e3 + t.tv_nsec / 1e6;+}+static int still(void) { return now_ms() - last_change >= STILL_MS; }++static void send_damage(void);+static void video_update(void);static void put(Client *c, const void *p, size_t n) {if (c->out_len + n > c->out_cap) {@@ -110,6 +130,22 @@ static void notify_clients(void) {cb.clients(json);}+/* Who wants which encoder: viewers per codec, sound, and the slowest viewer's rate. */+static void video_update(void) {+ for (int k = 1; k < MS_CODECS; k++) {+ int n = 0, audio = 0;+ unsigned kbps = 0;+ for (int i = 0; i < MAX_CLIENTS; i++) {+ const Client *c = &cl[i];+ if (c->fd < 0 || c->vcodec != k) continue;+ n++;+ audio |= c->vaudio;+ if (!kbps || c->vkbps < kbps) kbps = c->vkbps;+ }+ mstream_want(k, n, audio, kbps ? kbps : 4000);+ }+}+static void close_fds(Client *c) {for (int i = 0; i < c->nfds; i++) if (c->fds[i] >= 0) close(c->fds[i]);c->nfds = 0;@@ -122,9 +158,11 @@ static void drop(Client *c) {free(c->in);free(c->out);free(c->dirty);+ int video = c->vcodec;memset(c, 0, sizeof *c);c->fd = -1;if (had) notify_clients();+ if (video) video_update();}static void all_dirty(Client *c) {@@ -162,7 +200,7 @@ static void pump(Client *c) {msg(c, RP_CLIP, clip_text, clip_len, NULL, 0);c->clip_gen = clip_gen_seen;}- if (c->ready && c->dirty && fb && c->out_len - c->out_off < OUT_LIMIT) {+ if (c->ready && c->dirty && fb && c->out_len - c->out_off < OUT_LIMIT && (!c->vmode || still())) {int any = 0;for (int ty = 0; ty < TH; ty++) {for (int tx = 0; tx < TW;) {@@ -245,6 +283,36 @@ static void handle(Client *c, int type, const unsigned char *d, uint32_t n) {case RP_UNDISPLAY:if (c->seat && cb.undisplay) cb.undisplay();break;+ case RP_VIDEO:+ /* u8 codec (0 none, 1 H.264, 2 VP8), u8 sound, u16 0, u32 kbit/s: this viewer's video */+ if (n >= 8 && d[0] <= 2) {+ uint32_t k;+ memcpy(&k, d + 4, 4);+ int was = c->vcodec;+ c->vcodec = d[0];+ c->vaudio = d[1] != 0;+ c->vkbps = k < 300 ? 300 : k > 20000 ? 20000 : k;+ if (c->vcodec && c->vcodec != was) c->vneed_key = 1;+ video_update();+ if (c->vcodec && fb) mstream_frame(fb, W, W, H);+ }+ break;+ case RP_KEYFRAME: if (c->vcodec) { mstream_keyframe(c->vcodec); c->vneed_key = 1; } break;+ case RP_RATE:+ if (n >= 4 && c->vcodec) {+ uint32_t k;+ memcpy(&k, d, 4);+ c->vkbps = k < 300 ? 300 : k > 20000 ? 20000 : k;+ video_update();+ }+ break;+ case RP_VMODE:+ if (n >= 1) {+ c->vmode = d[0] == 1;+ all_dirty(c); /* the lossless layer starts (or the tiles start) whole */+ c->ready = 1;+ }+ break;case RP_RESIZE:if (n >= 4 && cb.resize) {uint16_t s[2];@@ -328,8 +396,33 @@ static void read_client(Client *c) {pump(c);}+static void video_out(int codec, int key, uint32_t ms, const uint8_t *p, size_t n) {+ unsigned char h[8] = { (unsigned char)codec, (unsigned char)(key != 0), 0, 0 };+ memcpy(h + 4, &ms, 4);+ for (int i = 0; i < MAX_CLIENTS; i++) {+ Client *c = &cl[i];+ if (c->fd < 0 || c->vcodec != codec) continue;+ if (c->vneed_key && !key) continue;+ if (c->out_len - c->out_off > VIDEO_LIMIT) { c->vneed_key = 1; mstream_keyframe(codec); continue; }+ msg(c, RP_VFRAME, h, 8, p, n);+ if (key) c->vneed_key = 0;+ flush(c);+ }+}++static void audio_out(int codec, uint32_t samples, const uint8_t *p, size_t n) {+ for (int i = 0; i < MAX_CLIENTS; i++) {+ Client *c = &cl[i];+ if (c->fd < 0 || c->vcodec != codec || !c->vaudio || c->out_len - c->out_off > VIDEO_LIMIT) continue;+ msg(c, RP_AFRAME, &samples, 4, p, n);+ flush(c);+ }+}+int mirror_start(int w, int h, const MirrorInput *in) {if (in) cb = *in;+ static const MStreamOut so = { video_out, audio_out };+ mstream_init(&so);for (int i = 0; i < MAX_CLIENTS; i++) cl[i].fd = -1;W = w;H = h;@@ -369,6 +462,8 @@ void mirror_stop(void) {shape = NULL;free(clip_text);clip_text = NULL;+ free(chg);+ chg = NULL;}int mirror_clients(void) {@@ -454,7 +549,7 @@ void mirror_dispatch(const struct pollfd *p, int n) {void mirror_frame(const uint32_t *px, int stride, int w, int h) {if (lfd < 0 || !mirror_clients()) { free(fb); fb = NULL; return; }- int resized = !fb || w != W || h != H;+ int resized = !fb || w != W || h != H, any = 0;int new_size = w != W || h != H;if (resized) {W = w;@@ -466,7 +561,13 @@ void mirror_frame(const uint32_t *px, int stride, int w, int h) {if (!fb) return;for (int j = 0; j < H; j++) memcpy(fb + (size_t)j * W, px + (size_t)j * stride, (size_t)W * 4);for (int i = 0; i < MAX_CLIENTS; i++) if (cl[i].fd >= 0) { if (new_size) cl[i].need_size = 1; all_dirty(&cl[i]); }+ free(chg);+ chg = calloc((size_t)TW * TH, 1);+ if (chg) memset(chg, 1, (size_t)TW * TH);+ any = 1;} else {+ if (!chg) chg = calloc((size_t)TW * TH, 1);+ if (chg) memset(chg, 0, (size_t)TW * TH);for (int ty = 0; ty < TH; ty++)for (int tx = 0; tx < TW; tx++) {int x = tx * TILE, y = ty * TILE;@@ -478,11 +579,71 @@ void mirror_frame(const uint32_t *px, int stride, int w, int h) {for (int j = 0; j < th; j++)memcpy(fb + (size_t)(y + j) * W + x, px + (size_t)(y + j) * stride + x, (size_t)tw * 4);for (int i = 0; i < MAX_CLIENTS; i++) if (cl[i].fd >= 0 && cl[i].dirty) cl[i].dirty[ty * TW + tx] = 1;+ if (chg) chg[ty * TW + tx] = 1;+ any = 1;}}+ if (any) {+ last_change = now_ms();+ send_damage();+ int video = 0;+ for (int i = 0; i < MAX_CLIENTS; i++) if (cl[i].fd >= 0 && cl[i].vcodec) video = 1;+ if (video) mstream_frame(fb, W, W, H);+ }for (int i = 0; i < MAX_CLIENTS; i++) pump(&cl[i]);}+/* Video mode: what changed in this frame, as rows of tiles, so the browser drops its lossless layer+ * there and shows the video (RP_DAMAGE: u16 x, y, w, h each). */+static void send_damage(void) {+ if (!chg) return;+ uint16_t r[4 * 256];+ int n = 0;+ for (int ty = 0; ty < TH && n < 256; ty++)+ for (int tx = 0; tx < TW && n < 256;) {+ if (!chg[ty * TW + tx]) { tx++; continue; }+ int t0 = tx;+ while (tx < TW && chg[ty * TW + tx]) tx++;+ int x = t0 * TILE, y = ty * TILE;+ r[n * 4] = (uint16_t)x;+ r[n * 4 + 1] = (uint16_t)y;+ r[n * 4 + 2] = (uint16_t)((tx * TILE > W ? W : tx * TILE) - x);+ r[n * 4 + 3] = (uint16_t)(y + TILE > H ? H - y : TILE);+ n++;+ }+ if (!n) return;+ for (int i = 0; i < MAX_CLIENTS; i++) {+ Client *c = &cl[i];+ if (c->fd < 0 || !c->vmode) continue;+ msg(c, RP_DAMAGE, r, (size_t)n * 8, NULL, 0);+ flush(c);+ }+}++/* Video mode clients get their lossless tiles once the screen is still; the encoder may have a+ * frame waiting for a free slot. */+void mirror_tick(void) {+ if (lfd < 0) return;+ if (still()) for (int i = 0; i < MAX_CLIENTS; i++) if (cl[i].fd >= 0 && cl[i].vmode && cl[i].ready) pump(&cl[i]);+ mstream_tick();+}++double mirror_timeout(void) {+ if (lfd < 0) return -1;+ double best = mstream_timeout();+ for (int i = 0; i < MAX_CLIENTS; i++) {+ const Client *c = &cl[i];+ if (c->fd < 0 || !c->vmode || !c->ready || !c->dirty) continue;+ int pend = 0;+ for (int k = 0; k < TW * TH && !pend; k++) pend = c->dirty[k];+ if (!pend) continue;+ double d = STILL_MS - (now_ms() - last_change);+ if (d < 1) d = 1;+ if (best < 0 || d < best) best = d;+ }+ return best;+}+void mirror_cursor(int x, int y, int mode, const uint32_t *argb, int w, int h, int hx, int hy, unsigned gen) {if (lfd < 0 || !mirror_clients()) return;cur_x = x;diff --git a/server/src/remote/mirror.h b/server/src/remote/mirror.hindex a576571..c170558 100644--- a/server/src/remote/mirror.h+++ b/server/src/remote/mirror.h@@ -50,6 +50,10 @@ void mirror_shown(void);void mirror_frame(const uint32_t *px, int stride, int w, int h);/* Cursor: position and mode (0 arrow, 1 app image, 2 hidden); the image only if gen changed. */void mirror_cursor(int x, int y, int mode, const uint32_t *argb, int w, int h, int hx, int hy, unsigned gen);+/* The video path: lossless tiles for video-mode viewers once the screen is still, the encoder's+ * pending frame. Called on every loop iteration; mirror_timeout says when it has work (ms, -1 none). */+void mirror_tick(void);+double mirror_timeout(void);/* Session text clipboard (called on every loop iteration; sends only if it changed). */void mirror_clipboard(const char *text, unsigned gen);diff --git a/server/src/remote/proto.h b/server/src/remote/proto.hindex dca1a9a..5a58cdb 100644--- a/server/src/remote/proto.h+++ b/server/src/remote/proto.h@@ -21,6 +21,10 @@ enum {* RP_DISPLAY_KEEP is on the screen (the boot splash can go; docs/SESSIONS.md) */RP_SESSION = 9, /* JSON: request from the session to the monitor ({"a":"lock"} or {"a":"switch"}) */RP_GREETER = 20, /* JSON: request from the login screen to lord-sessiond (greeter mode only) */+ /* the video path (docs/REMOTE.md, "Video (WebRTC)"): the session's shared encoder */+ RP_VFRAME = 40, /* u8 codec (1 H.264, 2 VP8), u8 keyframe, u16 0, u32 ms + the encoded frame */+ RP_AFRAME = 41, /* u32 first sample (48 kHz) + one Opus packet */+ RP_DAMAGE = 42, /* video mode: u16 x, y, w, h per rectangle that changed in this frame */};/* bridge → session */@@ -48,6 +52,10 @@ enum {* {"t":"blocked","ip":"…","min":N,"n":N} (a source blocked after failed remote logins) */RP_SCROLL = 30, /* u8 phase (1 begins, 2 continues, 3 ends), i32 dx, dy in 1/256 pixel: browser trackpad */RP_PINCH = 31, /* u8 phase, i32 scale ×65536 (relative to the start), i32 rotation in degrees ×256 */+ RP_VIDEO = 32, /* u8 codec (0 stop), u8 sound, u16 0, u32 kbit/s: this viewer takes the encoded video */+ RP_KEYFRAME = 33, /* the viewer lost a frame: the next one is a keyframe */+ RP_RATE = 34, /* u32 kbit/s: what this viewer's network takes now */+ RP_VMODE = 35, /* u8 1: video mode (RP_RECT only once the screen is still, RP_DAMAGE at once); 0: tiles */};/* RP_DISPLAY flags */diff --git a/server/src/remote/rsec.c b/server/src/remote/rsec.cindex f9d4588..e612a6a 100644--- a/server/src/remote/rsec.c+++ b/server/src/remote/rsec.c@@ -590,5 +590,28 @@ int rs_browser_input(const RsJson *j, int W, int H, RsInput *in) {in->tlen = l;return in->type = RS_IN_CLIP;}+ /* the video path's signaling (docs/REMOTE.md, "Video (WebRTC)"); the SDP and the candidate are+ * parsed strictly afterwards by rtcwire.c */+ if (!strcmp(t, "rtc")) {+ size_t l = 0;+ const char *x = rs_json_str(j, "sdp", &l);+ if (!x || l < 10 || l > RS_SDP_MAX) return RS_IN_NONE;+ in->text = x;+ in->tlen = l;+ return in->type = RS_IN_RTC;+ }+ if (!strcmp(t, "ice")) {+ size_t l = 0;+ const char *x = rs_json_str(j, "c", &l);+ if (!x || l > 512) return RS_IN_NONE;+ in->text = x;+ in->tlen = l;+ return in->type = RS_IN_ICE;+ }+ if (!strcmp(t, "vmode")) {+ if (rs_json_num(j, "on", &a) || (a != 0 && a != 1)) return RS_IN_NONE;+ in->a = (int)a;+ return in->type = RS_IN_VMODE;+ }return RS_IN_NONE;}diff --git a/server/src/remote/rsec.h b/server/src/remote/rsec.hindex 2115eb2..cdec6e6 100644--- a/server/src/remote/rsec.h+++ b/server/src/remote/rsec.h@@ -103,14 +103,15 @@ int rs_json_num(const RsJson *o, const char *key, long long *v);/* A browser message, validated and clamped against a W×H screen. */enum { RS_IN_NONE = 0, RS_IN_PTR, RS_IN_BTN, RS_IN_WHEEL, RS_IN_SCROLL, RS_IN_PINCH, RS_IN_KEY, RS_IN_ACK, RS_IN_RESIZE,- RS_IN_PING, RS_IN_CLIP };+ RS_IN_PING, RS_IN_CLIP, RS_IN_RTC, RS_IN_ICE, RS_IN_VMODE };+#define RS_SDP_MAX 32768 /* {"t":"rtc","sdp":…} (rtcwire.h RW_SDP_MAX) */#define RS_CLIP_MAX (1u << 20)typedef struct {int type;- int a, b; /* PTR x,y · BTN button,down · WHEEL steps · KEY code,down · RESIZE w,h · SCROLL/PINCH phase */+ int a, b; /* PTR x,y · BTN button,down · WHEEL steps · KEY code,down · RESIZE w,h · SCROLL/PINCH phase · VMODE on */int32_t v1, v2; /* SCROLL dx,dy (1/256 px) · PINCH scale ×65536, rotation ×256 */long long ts; /* PING */- const char *text; /* CLIP (inside the RsJson buffer) */+ const char *text; /* CLIP, RTC (the SDP offer), ICE (a candidate; "" ends) (inside the RsJson buffer) */size_t tlen;} RsInput;int rs_browser_input(const RsJson *j, int W, int H, RsInput *in);diff --git a/server/src/remote/rtc.c b/server/src/remote/rtc.cnew file mode 100644index 0000000..99eec02--- /dev/null+++ b/server/src/remote/rtc.c@@ -0,0 +1,1121 @@+/* rtc.c — the remote desktop's WebRTC peer (docs/REMOTE.md, "Video (WebRTC)").+ *+ * It lives in the bridge, the process that already dropped root for good (conn.c): everything here+ * runs as the user who signed in. Debian 13 packages no WebRTC library we could use (no+ * libdatachannel, no libjuice), so this is our own small peer on top of what Debian does ship:+ * - ICE-lite (RFC 8445 §2.5): the browser is the controlling agent and checks us; we only answer+ * checks signed with our ICE password, from addresses the remote desktop's rules allow. A second+ * UDP socket is a plain STUN server for this connection only (the browser is told to use it), so a+ * LAN works with no outside server. It has to be another port: browsers hand every STUN packet+ * from their STUN server's address to the server's own request handler, ICE answers included.+ * - DTLS 1.2 with OpenSSL (lord-sessiond already links it), we are the server; the browser's+ * certificate must hash to the fingerprint in the offer that came over the authenticated+ * WebSocket, or the connection ends.+ * - SRTP with libsrtp2 (keys exported from DTLS), RTCP feedback (NACK, PLI, FIR, receiver reports).+ * - The data channels over SCTP with usrsctp (no threads: its timers run from rtc_tick).+ * - Optional external STUN (our public address) and TURN (a relayed candidate; the relay only ever+ * sees DTLS and SRTP). The TURN password never leaves this process and is never logged.+ * Every packet goes through the strict parsers in rtcwire.c first. */+#include "rtc.h"+#include "sessiond.h"++#include <arpa/inet.h>+#include <errno.h>+#include <fcntl.h>+#include <ifaddrs.h>+#include <net/if.h>+#include <netdb.h>+#include <netinet/in.h>+#include <stdio.h>+#include <stdlib.h>+#include <string.h>+#include <sys/random.h>+#include <sys/socket.h>+#include <time.h>+#include <unistd.h>++#include <openssl/err.h>+#include <openssl/evp.h>+#include <openssl/hmac.h>+#include <openssl/ssl.h>+#include <openssl/x509.h>+#include <srtp2/srtp.h>+#include <usrsctp.h>++#define MTU 1100 /* RTP payload: fits a 1200-byte path with SRTP and a TURN header */+#define HIST 1024 /* sent video packets kept for retransmission (NACK) */+#define MAX_PATHS 8+#define MAX_DC_MSG 262144 /* what our answer advertises (a=max-message-size) */+#define CONSENT_MS 30000 /* no valid check for this long: the browser is gone */+#define ANSWER_WAIT_MS 1200 /* for the external STUN/TURN answers before sending ours */++typedef struct { uint16_t seq, len; uint8_t b[MTU + 64]; } Hist;+typedef struct {+ uint32_t ssrc, ts_off, last_ts, packets, octets;+ uint16_t seq;+ int pt;+ double last_ms;+ Hist *hist;+} Track;+typedef struct { RwAddr a; int relay, valid, nominated; double last; } Path;+typedef struct {+ int state; /* 0 off, 1 asked, 2 asked with credentials, 3 allocated, -1 failed */+ RwAddr srv, relayed, mapped;+ uint8_t tid[12], key[16];+ char realm[256];+ uint8_t nonce[256];+ size_t nlen;+ double refresh_at, perm_at, sent_at;+ int tries;+ RwAddr perm[8];+ int nperm, perm_dirty;+} Turn;++struct Rtc {+ RtcCallbacks cb;+ RtcConfig cfg;+ int fd, port, sfd, sport; /* media socket; the built-in STUN server's socket */+ char ufrag[9], pwd[25];+ /* DTLS */+ SSL_CTX *ctx;+ SSL *ssl;+ BIO *rbio;+ EVP_PKEY *key;+ X509 *cert;+ uint8_t fp[32];+ int dtls_up;+ /* the browser */+ RwOffer *of;+ int offered, codec, answered;+ double answer_due;+ RwAddr rcand[16];+ int nrcand;+ Path path[MAX_PATHS];+ int npath, sel;+ /* SRTP */+ srtp_t tx, rx;+ int srtp_up;+ /* SCTP and the data channels */+ struct socket *sctp;+ int sctp_on, dc_sid[2], dc_open[2];+ uint8_t *rasm; /* a message being reassembled */+ size_t rasm_len;+ int rasm_sid, rasm_ppid;+ double sctp_t;+ Track v, a;+ /* congestion: the viewer's target */+ unsigned kbps;+ double rate_at, kf_at, lost;+ unsigned long nacks, plis, resent;+ double last_sr, created, last_consent, stun_win;+ int stun_n, failed;+ /* external STUN, TURN */+ RwAddr stun_srv[RTC_MAX_STUN];+ uint8_t stun_tid[RTC_MAX_STUN][12];+ int nstun_srv;+ RwAddr srflx;+ Turn turn;+};++static int srtp_ready, sctp_ready, gcm_ok = -1;++static double now_ms(void) {+ struct timespec t;+ clock_gettime(CLOCK_MONOTONIC, &t);+ return t.tv_sec * 1e3 + t.tv_nsec / 1e6;+}++static void rnd(void *p, size_t n) { if (getrandom(p, n, 0) != (ssize_t)n) memset(p, 0x5a, n); }++static void rnd_text(char *out, size_t n) {+ static const char al[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";+ uint8_t b[64];+ rnd(b, n);+ for (size_t i = 0; i < n; i++) out[i] = al[b[i] % (sizeof al - 1)];+ out[n] = 0;+}++static void fail(Rtc *r, const char *why) {+ if (r->failed) return;+ r->failed = 1;+ sd_log("video: %s", why);+ if (r->cb.state) r->cb.state(r->cb.ud, RTC_FAILED, why);+}++static void hmac_sha1(const uint8_t *key, size_t klen, const uint8_t *msg, size_t n, uint8_t out[20]) {+ unsigned int l = 20;+ if (!HMAC(EVP_sha1(), key, (int)klen, msg, n, out, &l)) memset(out, 0, 20);+}++/* ---- addresses ---- */++static void to_sockaddr(const RwAddr *a, struct sockaddr_in6 *s) {+ memset(s, 0, sizeof *s);+ s->sin6_family = AF_INET6;+ s->sin6_port = htons(a->port);+ if (a->family == 4) { s->sin6_addr.s6_addr[10] = 0xff; s->sin6_addr.s6_addr[11] = 0xff; memcpy(s->sin6_addr.s6_addr + 12, a->a, 4); }+ else memcpy(s->sin6_addr.s6_addr, a->a, 16);+}++static void from_sockaddr(const struct sockaddr_in6 *s, RwAddr *a) {+ static const uint8_t mapped[12] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xff, 0xff };+ memset(a, 0, sizeof *a);+ a->port = ntohs(s->sin6_port);+ if (!memcmp(s->sin6_addr.s6_addr, mapped, 12)) { a->family = 4; memcpy(a->a, s->sin6_addr.s6_addr + 12, 4); }+ else { a->family = 6; memcpy(a->a, s->sin6_addr.s6_addr, 16); }+}++static int same_ip(const RwAddr *x, const RwAddr *y) { return x->family == y->family && !memcmp(x->a, y->a, x->family == 4 ? 4 : 16); }++/* host:port (a name, an IPv4 or [IPv6]) → address; the names configured in Settings are resolved+ * here, as the user, once per connection. */+static int resolve(const char *hp, RwAddr *out) {+ char host[96];+ const char *colon = strrchr(hp, ':');+ if (!colon || colon == hp || strlen(hp) >= sizeof host) return -1;+ size_t hl = (size_t)(colon - hp);+ memcpy(host, hp, hl);+ host[hl] = 0;+ if (host[0] == '[' && hl > 2 && host[hl - 1] == ']') { memmove(host, host + 1, hl - 2); host[hl - 2] = 0; }+ long port = strtol(colon + 1, NULL, 10);+ if (port < 1 || port > 65535) return -1;+ if (!rw_addr_text(host, out)) { out->port = (uint16_t)port; return 0; }+ struct addrinfo hints = { .ai_family = AF_UNSPEC, .ai_socktype = SOCK_DGRAM }, *res = NULL;+ if (getaddrinfo(host, NULL, &hints, &res) || !res) return -1;+ int ok = -1;+ for (struct addrinfo *i = res; i && ok; i = i->ai_next) {+ if (i->ai_family == AF_INET) { out->family = 4; memset(out->a, 0, 16); memcpy(out->a, &((struct sockaddr_in *)i->ai_addr)->sin_addr, 4); ok = 0; }+ else if (i->ai_family == AF_INET6) { from_sockaddr((struct sockaddr_in6 *)i->ai_addr, out); ok = 0; }+ }+ freeaddrinfo(res);+ out->port = (uint16_t)port;+ return ok;+}++/* The same rules as the WebSocket: the signed-in browser's own address, or what ipnet.c allows+ * (a manual block always wins). Through our TURN relay: anyone not blocked (the admin chose to add a+ * relay; the WebSocket itself still had to get in). */+static int src_allowed(const Rtc *r, const RwAddr *a, int relay) {+ char t[64];+ RsAddr x;+ rw_addr_format(a, t, sizeof t);+ if (rs_addr_parse(t, &x)) return 0;+ if (!r->cfg.access) return 0;+ if (relay) return rs_access_decide(r->cfg.access, RS_NET_ANY, &x, 0) == RS_ACC_OK;+ if (same_ip(a, &r->cfg.ws_peer)) return rs_access_decide(r->cfg.access, RS_NET_ANY, &x, 0) == RS_ACC_OK;+ return rs_access_decide(r->cfg.access, r->cfg.net_mode, &x, 0) == RS_ACC_OK;+}++/* ---- sending ---- */++static void raw_send(Rtc *r, const RwAddr *to, const void *p, size_t n) {+ struct sockaddr_in6 s;+ to_sockaddr(to, &s);+ if (sendto(r->fd, p, n, MSG_DONTWAIT | MSG_NOSIGNAL, (struct sockaddr *)&s, sizeof s) < 0 && errno != EAGAIN) {}+}++/* To a peer, directly or wrapped in a TURN Send indication. */+static void send_to(Rtc *r, const RwAddr *to, int relay, const void *p, size_t n) {+ if (!relay) { raw_send(r, to, p, n); return; }+ if (r->turn.state != 3 || n > 1400) return;+ static uint8_t b[1500];+ uint8_t tid[12];+ rnd(tid, 12);+ RwStunOut hdr;+ rw_so_init(&hdr, RW_TURN_SEND_IND, tid);+ rw_so_xaddr(&hdr, RW_ATTR_XOR_PEER, to);+ if (hdr.bad) return;+ size_t o = hdr.n, padded = (n + 3) & ~(size_t)3;+ if (o + 4 + padded > sizeof b) return;+ memcpy(b, hdr.b, o);+ b[o] = 0; b[o + 1] = 0x13; b[o + 2] = (uint8_t)(n >> 8); b[o + 3] = (uint8_t)n;+ memcpy(b + o + 4, p, n);+ memset(b + o + 4 + n, 0, padded - n);+ size_t total = o + 4 + padded;+ b[2] = (uint8_t)((total - 20) >> 8);+ b[3] = (uint8_t)(total - 20);+ raw_send(r, &r->turn.srv, b, total);+}++static Path *sel_path(Rtc *r) { return r->sel >= 0 ? &r->path[r->sel] : NULL; }++static void send_sel(Rtc *r, const void *p, size_t n) {+ Path *s = sel_path(r);+ if (s) send_to(r, &s->a, s->relay, p, n);+}++/* ---- DTLS: our certificate, a write BIO that sends each record as a datagram ---- */++static int bio_write(BIO *b, const char *p, int n) {+ Rtc *r = BIO_get_data(b);+ if (r && n > 0) send_sel(r, p, (size_t)n);+ return n;+}+static long bio_ctrl(BIO *b, int cmd, long num, void *ptr) {+ (void)b; (void)num; (void)ptr;+ switch (cmd) {+ case BIO_CTRL_FLUSH: return 1;+ case BIO_CTRL_DGRAM_QUERY_MTU: return 1200;+ case BIO_CTRL_DGRAM_GET_MTU_OVERHEAD: return 0;+ case BIO_CTRL_WPENDING: case BIO_CTRL_PENDING: return 0;+ default: return 0;+ }+}+static int bio_create(BIO *b) { BIO_set_init(b, 1); return 1; }+static BIO_METHOD *bio_method(void) {+ static BIO_METHOD *m;+ if (!m) {+ m = BIO_meth_new(BIO_TYPE_SOURCE_SINK | BIO_get_new_index(), "lordos-dtls");+ BIO_meth_set_write(m, bio_write);+ BIO_meth_set_ctrl(m, bio_ctrl);+ BIO_meth_set_create(m, bio_create);+ }+ return m;+}++static int accept_any(int ok, X509_STORE_CTX *c) { (void)ok; (void)c; return 1; } /* checked by fingerprint */++static int make_cert(Rtc *r) {+ r->key = EVP_EC_gen("P-256");+ r->cert = X509_new();+ if (!r->key || !r->cert) return -1;+ uint8_t serial[8];+ rnd(serial, sizeof serial);+ serial[0] &= 0x7f;+ BIGNUM *bn = BN_bin2bn(serial, sizeof serial, NULL);+ ASN1_INTEGER *ai = bn ? BN_to_ASN1_INTEGER(bn, NULL) : NULL;+ BN_free(bn);+ if (!ai) return -1;+ X509_set_serialNumber(r->cert, ai);+ ASN1_INTEGER_free(ai);+ X509_set_version(r->cert, 2);+ X509_gmtime_adj(X509_getm_notBefore(r->cert), -86400);+ X509_gmtime_adj(X509_getm_notAfter(r->cert), 30L * 86400);+ X509_NAME *nm = X509_get_subject_name(r->cert);+ X509_NAME_add_entry_by_txt(nm, "CN", MBSTRING_ASC, (const unsigned char *)"lordos", -1, -1, 0);+ X509_set_issuer_name(r->cert, nm);+ X509_set_pubkey(r->cert, r->key);+ if (!X509_sign(r->cert, r->key, EVP_sha256())) return -1;+ unsigned int l = 32;+ if (!X509_digest(r->cert, EVP_sha256(), r->fp, &l) || l != 32) return -1;+ return 0;+}++/* libsrtp2 has AES-GCM only when built with a crypto library; Debian's is (OpenSSL), but check. */+static int srtp_gcm_works(void) {+ if (gcm_ok >= 0) return gcm_ok;+ srtp_policy_t p;+ memset(&p, 0, sizeof p);+ srtp_crypto_policy_set_aes_gcm_128_16_auth(&p.rtp);+ srtp_crypto_policy_set_aes_gcm_128_16_auth(&p.rtcp);+ uint8_t key[28] = { 1 };+ p.key = key;+ p.ssrc.type = ssrc_any_outbound;+ srtp_t s;+ gcm_ok = srtp_create(&s, &p) == srtp_err_status_ok;+ if (gcm_ok) srtp_dealloc(s);+ return gcm_ok;+}++static int dtls_init(Rtc *r) {+ if (make_cert(r)) return -1;+ r->ctx = SSL_CTX_new(DTLS_server_method());+ if (!r->ctx) return -1;+ SSL_CTX_set_min_proto_version(r->ctx, DTLS1_2_VERSION);+ SSL_CTX_set_options(r->ctx, SSL_OP_NO_QUERY_MTU | SSL_OP_NO_RENEGOTIATION | SSL_OP_NO_COMPRESSION | SSL_OP_NO_TICKET);+ if (SSL_CTX_set_cipher_list(r->ctx, "ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-ECDSA-CHACHA20-POLY1305") != 1) return -1;+ SSL_CTX_set1_groups_list(r->ctx, "X25519:P-256:P-384");+ if (SSL_CTX_use_certificate(r->ctx, r->cert) != 1 || SSL_CTX_use_PrivateKey(r->ctx, r->key) != 1) return -1;+ SSL_CTX_set_verify(r->ctx, SSL_VERIFY_PEER | SSL_VERIFY_FAIL_IF_NO_PEER_CERT, accept_any);+ /* returns 0 on success */+ if (SSL_CTX_set_tlsext_use_srtp(r->ctx, srtp_gcm_works() ? "SRTP_AEAD_AES_128_GCM:SRTP_AES128_CM_SHA1_80" : "SRTP_AES128_CM_SHA1_80")) return -1;+ r->ssl = SSL_new(r->ctx);+ if (!r->ssl) return -1;+ r->rbio = BIO_new(BIO_s_mem());+ BIO *w = BIO_new(bio_method());+ if (!r->rbio || !w) return -1;+ BIO_set_mem_eof_return(r->rbio, -1);+ BIO_set_data(w, r);+ SSL_set_bio(r->ssl, r->rbio, w);+ SSL_set_mtu(r->ssl, 1200);+ DTLS_set_link_mtu(r->ssl, 1200);+ SSL_set_accept_state(r->ssl);+ return 0;+}++/* ---- SCTP and the data channels ---- */++static int sctp_out(void *addr, void *buf, size_t len, uint8_t tos, uint8_t set_df) {+ (void)tos; (void)set_df;+ Rtc *r = addr;+ if (r && r->dtls_up && !r->failed && len <= 1400) SSL_write(r->ssl, buf, (int)len);+ return 0;+}++static int dc_raw(Rtc *r, int sid, uint32_t ppid, const void *p, size_t n) {+ if (!r->sctp) return -1;+ struct sctp_sndinfo si;+ memset(&si, 0, sizeof si);+ si.snd_sid = (uint16_t)sid;+ si.snd_flags = SCTP_EOR;+ si.snd_ppid = htonl(ppid);+ static const uint8_t zero = 0;+ if (!n) { p = &zero; n = 1; ppid = ppid == 51 ? 56 : 57; si.snd_ppid = htonl(ppid); }+ ssize_t w = usrsctp_sendv(r->sctp, p, n, NULL, 0, &si, sizeof si, SCTP_SENDV_SNDINFO, 0);+ return w < 0 ? -1 : 0;+}++static void on_dc_message(Rtc *r, int sid, int ppid, const uint8_t *d, size_t n) {+ if (ppid == 50) { /* DCEP */+ RwDcOpen op;+ if (n >= 1 && d[0] == 0x02) return; /* an ACK (we never open channels) */+ if (rw_dcep_open(d, n, &op)) { sd_log("video: malformed data channel open"); return; }+ int ch = !strcmp(op.label, "input") ? RTC_CH_INPUT : !strcmp(op.label, "tiles") ? RTC_CH_TILES : -1;+ if (ch < 0 || r->dc_open[ch]) { sd_log("video: refused data channel \"%s\"", op.label); return; }+ r->dc_sid[ch] = sid;+ r->dc_open[ch] = 1;+ uint8_t ack = 0x02;+ dc_raw(r, sid, 50, &ack, 1);+ if (r->cb.state) r->cb.state(r->cb.ud, RTC_DC_OPEN, op.label);+ return;+ }+ int ch = -1;+ for (int i = 0; i < 2; i++) if (r->dc_open[i] && r->dc_sid[i] == sid) ch = i;+ if (ch < 0) return;+ if (ppid == 56 || ppid == 57) { n = 0; ppid = ppid == 56 ? 51 : 53; }+ if (ppid != 51 && ppid != 53) return;+ if (r->cb.message) r->cb.message(r->cb.ud, ch, d, n, ppid == 53);+}++static int sctp_recv(struct socket *sock, union sctp_sockstore addr, void *data, size_t len, struct sctp_rcvinfo rcv, int flags, void *ulp) {+ (void)sock; (void)addr;+ Rtc *r = ulp;+ if (!data) return 1;+ if (!r || r->failed) { free(data); return 1; }+ if (flags & MSG_NOTIFICATION) {+ const union sctp_notification *nt = data;+ if (len >= sizeof nt->sn_assoc_change && nt->sn_header.sn_type == SCTP_ASSOC_CHANGE) {+ int st = nt->sn_assoc_change.sac_state;+ if (st == SCTP_COMM_LOST || st == SCTP_SHUTDOWN_COMP) fail(r, "the browser closed the data channels");+ }+ free(data);+ return 1;+ }+ int sid = rcv.rcv_sid, ppid = (int)ntohl(rcv.rcv_ppid);+ /* a message larger than the receive buffer comes in parts: joined up to our limit */+ if (r->rasm_len && (r->rasm_sid != sid || r->rasm_ppid != ppid)) { free(r->rasm); r->rasm = NULL; r->rasm_len = 0; }+ if (!(flags & MSG_EOR) || r->rasm_len) {+ if (r->rasm_len + len > MAX_DC_MSG) { free(data); fail(r, "data channel message too large"); return 1; }+ uint8_t *nb = realloc(r->rasm, r->rasm_len + len);+ if (!nb) { free(data); fail(r, "out of memory"); return 1; }+ memcpy(nb + r->rasm_len, data, len);+ r->rasm = nb;+ r->rasm_len += len;+ r->rasm_sid = sid;+ r->rasm_ppid = ppid;+ free(data);+ if (!(flags & MSG_EOR)) return 1;+ on_dc_message(r, sid, ppid, r->rasm, r->rasm_len);+ free(r->rasm);+ r->rasm = NULL;+ r->rasm_len = 0;+ return 1;+ }+ if (len > MAX_DC_MSG) { free(data); fail(r, "data channel message too large"); return 1; }+ on_dc_message(r, sid, ppid, data, len);+ free(data);+ return 1;+}++static void sctp_start(Rtc *r) {+ if (!sctp_ready) {+ usrsctp_init_nothreads(0, sctp_out, NULL);+ usrsctp_sysctl_set_sctp_ecn_enable(0);+ usrsctp_sysctl_set_sctp_max_chunks_on_queue(10240);+ sctp_ready = 1;+ }+ usrsctp_register_address(r);+ r->sctp = usrsctp_socket(AF_CONN, SOCK_STREAM, IPPROTO_SCTP, sctp_recv, NULL, 0, r);+ if (!r->sctp) { fail(r, "no SCTP socket"); return; }+ usrsctp_set_non_blocking(r->sctp, 1);+ struct linger lg = { 1, 0 };+ usrsctp_setsockopt(r->sctp, SOL_SOCKET, SO_LINGER, &lg, sizeof lg);+ int on = 1;+ usrsctp_setsockopt(r->sctp, IPPROTO_SCTP, SCTP_NODELAY, &on, sizeof on);+ usrsctp_setsockopt(r->sctp, IPPROTO_SCTP, SCTP_RECVRCVINFO, &on, sizeof on);+ int buf = 1 << 20;+ usrsctp_setsockopt(r->sctp, SOL_SOCKET, SO_SNDBUF, &buf, sizeof buf);+ usrsctp_setsockopt(r->sctp, SOL_SOCKET, SO_RCVBUF, &buf, sizeof buf);+ struct sctp_initmsg im = { .sinit_num_ostreams = 1024, .sinit_max_instreams = 1024 };+ usrsctp_setsockopt(r->sctp, IPPROTO_SCTP, SCTP_INITMSG, &im, sizeof im);+ struct sctp_assoc_value av = { .assoc_id = SCTP_ALL_ASSOC, .assoc_value = SCTP_ENABLE_RESET_STREAM_REQ };+ usrsctp_setsockopt(r->sctp, IPPROTO_SCTP, SCTP_ENABLE_STREAM_RESET, &av, sizeof av);+ struct sctp_event ev = { .se_assoc_id = SCTP_ALL_ASSOC, .se_on = 1, .se_type = SCTP_ASSOC_CHANGE };+ usrsctp_setsockopt(r->sctp, IPPROTO_SCTP, SCTP_EVENT, &ev, sizeof ev);+ struct sockaddr_conn sc;+ memset(&sc, 0, sizeof sc);+ sc.sconn_family = AF_CONN;+ sc.sconn_port = htons(5000);+ sc.sconn_addr = r;+ if (usrsctp_bind(r->sctp, (struct sockaddr *)&sc, sizeof sc) < 0) { fail(r, "SCTP bind failed"); return; }+ int dm = r->of ? r->of->dm : -1;+ sc.sconn_port = htons((uint16_t)(dm >= 0 ? r->of->m[dm].sctp_port : 5000));+ if (usrsctp_connect(r->sctp, (struct sockaddr *)&sc, sizeof sc) < 0 && errno != EINPROGRESS) { fail(r, "SCTP connect failed"); return; }+ r->sctp_on = 1;+ r->sctp_t = now_ms();+}++/* ---- DTLS up: the fingerprint, SRTP keys, then SCTP ---- */++static void dtls_up(Rtc *r) {+ X509 *peer = SSL_get1_peer_certificate(r->ssl);+ uint8_t fp[32];+ unsigned int l = 32;+ int ok = peer && X509_digest(peer, EVP_sha256(), fp, &l) && l == 32 && !CRYPTO_memcmp(fp, r->of->fp, 32);+ X509_free(peer);+ if (!ok) { fail(r, "the browser's DTLS certificate does not match the fingerprint of the signed-in offer"); return; }+ const SRTP_PROTECTION_PROFILE *pr = SSL_get_selected_srtp_profile(r->ssl);+ if (!pr || (pr->id != SRTP_AES128_CM_SHA1_80 && pr->id != SRTP_AEAD_AES_128_GCM)) { fail(r, "no SRTP profile"); return; }+ int gcm = pr->id == SRTP_AEAD_AES_128_GCM;+ size_t kl = 16, sl = gcm ? 12 : 14;+ uint8_t km[2 * (16 + 14)], txk[30], rxk[30];+ if (SSL_export_keying_material(r->ssl, km, 2 * (kl + sl), "EXTRACTOR-dtls_srtp", 19, NULL, 0, 0) != 1) { fail(r, "no SRTP keys"); return; }+ /* client key, server key, client salt, server salt; we are the server */+ memcpy(txk, km + kl, kl);+ memcpy(txk + kl, km + 2 * kl + sl, sl);+ memcpy(rxk, km, kl);+ memcpy(rxk + kl, km + 2 * kl, sl);+ explicit_bzero(km, sizeof km);+ srtp_policy_t p;+ memset(&p, 0, sizeof p);+ if (gcm) { srtp_crypto_policy_set_aes_gcm_128_16_auth(&p.rtp); srtp_crypto_policy_set_aes_gcm_128_16_auth(&p.rtcp); }+ else { srtp_crypto_policy_set_rtp_default(&p.rtp); srtp_crypto_policy_set_rtcp_default(&p.rtcp); }+ p.window_size = 1024;+ p.allow_repeat_tx = 1;+ p.key = txk;+ p.ssrc.type = ssrc_any_outbound;+ int e1 = srtp_create(&r->tx, &p);+ p.key = rxk;+ p.ssrc.type = ssrc_any_inbound;+ int e2 = srtp_create(&r->rx, &p);+ explicit_bzero(txk, sizeof txk);+ explicit_bzero(rxk, sizeof rxk);+ if (e1 || e2) { fail(r, "SRTP setup failed"); return; }+ r->srtp_up = 1;+ r->dtls_up = 1;+ Path *s = sel_path(r);+ char a[64] = "";+ if (s) rw_addr_format(&s->a, a, sizeof a);+ sd_log("video: connected (%s, %s, %s%s)", r->codec == RW_CODEC_H264 ? "H.264" : "VP8", gcm ? "AES-GCM" : "AES-CM", a, s && s->relay ? " via TURN" : "");+ if (r->of->dm >= 0) sctp_start(r);+ if (r->cb.state) r->cb.state(r->cb.ud, RTC_CONNECTED, NULL);+}++static void dtls_input(Rtc *r, const uint8_t *p, size_t n) {+ BIO_write(r->rbio, p, (int)n);+ if (!r->dtls_up) {+ int k = SSL_do_handshake(r->ssl);+ if (k == 1) dtls_up(r);+ else {+ int e = SSL_get_error(r->ssl, k);+ if (e != SSL_ERROR_WANT_READ && e != SSL_ERROR_WANT_WRITE) { ERR_clear_error(); fail(r, "DTLS handshake failed"); return; }+ }+ }+ static uint8_t buf[65536];+ while (r->dtls_up && !r->failed) {+ int k = SSL_read(r->ssl, buf, sizeof buf);+ if (k > 0) { if (r->sctp_on) usrsctp_conninput(r, buf, (size_t)k, 0); continue; }+ int e = SSL_get_error(r->ssl, k);+ if (e == SSL_ERROR_ZERO_RETURN) fail(r, "the browser closed DTLS");+ else if (e != SSL_ERROR_WANT_READ && e != SSL_ERROR_WANT_WRITE) { ERR_clear_error(); fail(r, "DTLS error"); }+ break;+ }+}++/* ---- RTP out ---- */++typedef struct { Rtc *r; Track *t; uint32_t ts; } PktCtx;++static void rtp_out(void *ud, const uint8_t *hdr, size_t hl, const uint8_t *data, size_t n, int marker) {+ PktCtx *c = ud;+ Rtc *r = c->r;+ Track *t = c->t;+ uint8_t b[MTU + 64 + SRTP_MAX_TRAILER_LEN];+ if (12 + hl + n > MTU + 64) return;+ b[0] = 0x80;+ b[1] = (uint8_t)((marker ? 0x80 : 0) | t->pt);+ b[2] = (uint8_t)(t->seq >> 8); b[3] = (uint8_t)t->seq;+ uint32_t ts = c->ts;+ b[4] = (uint8_t)(ts >> 24); b[5] = (uint8_t)(ts >> 16); b[6] = (uint8_t)(ts >> 8); b[7] = (uint8_t)ts;+ b[8] = (uint8_t)(t->ssrc >> 24); b[9] = (uint8_t)(t->ssrc >> 16); b[10] = (uint8_t)(t->ssrc >> 8); b[11] = (uint8_t)t->ssrc;+ if (hl) memcpy(b + 12, hdr, hl);+ memcpy(b + 12 + hl, data, n);+ int len = (int)(12 + hl + n);+ if (srtp_protect(r->tx, b, &len) != srtp_err_status_ok) return;+ send_sel(r, b, (size_t)len);+ if (t->hist && (size_t)len <= sizeof t->hist[0].b) {+ Hist *h = &t->hist[t->seq % HIST];+ h->seq = t->seq;+ h->len = (uint16_t)len;+ memcpy(h->b, b, (size_t)len);+ }+ t->packets++;+ t->octets += (uint32_t)(hl + n);+ t->seq++;+}++int rtc_media_ready(const Rtc *r) { return r && r->srtp_up && !r->failed && r->sel >= 0; }+int rtc_codec(const Rtc *r) { return r ? r->codec : 0; }++void rtc_video(Rtc *r, const uint8_t *f, size_t n, uint32_t ms) {+ if (!rtc_media_ready(r) || !n) return;+ PktCtx c = { r, &r->v, r->v.ts_off + ms * 90 };+ r->v.last_ts = c.ts;+ r->v.last_ms = now_ms();+ if (r->codec == RW_CODEC_H264) rw_h264_packetize(f, n, MTU, rtp_out, &c);+ else rw_vp8_packetize(f, n, MTU, rtp_out, &c);+}++void rtc_audio(Rtc *r, const uint8_t *p, size_t n, uint32_t samples) {+ if (!rtc_media_ready(r) || !n || n > MTU || r->of->am < 0) return;+ PktCtx c = { r, &r->a, r->a.ts_off + samples };+ r->a.last_ts = c.ts;+ r->a.last_ms = now_ms();+ rtp_out(&c, NULL, 0, p, n, 0);+}++static void send_sr(Rtc *r, Track *t) {+ if (!t->packets) return;+ uint8_t b[28 + SRTP_MAX_TRAILER_LEN + 4];+ struct timespec rt;+ clock_gettime(CLOCK_REALTIME, &rt);+ uint64_t sec = (uint64_t)rt.tv_sec + 2208988800ull, frac = ((uint64_t)rt.tv_nsec << 32) / 1000000000ull;+ uint32_t ts = t->last_ts + (uint32_t)((now_ms() - t->last_ms) * (t == &r->v ? 90 : 48));+ uint32_t w[7] = { t->ssrc, (uint32_t)sec, (uint32_t)frac, ts, t->packets, t->octets, 0 };+ b[0] = 0x80; b[1] = 200; b[2] = 0; b[3] = 6;+ for (int i = 0; i < 6; i++) { uint32_t v = w[i]; b[4 + i * 4] = (uint8_t)(v >> 24); b[5 + i * 4] = (uint8_t)(v >> 16); b[6 + i * 4] = (uint8_t)(v >> 8); b[7 + i * 4] = (uint8_t)v; }+ int len = 28;+ if (srtp_protect_rtcp(r->tx, b, &len) == srtp_err_status_ok) send_sel(r, b, (size_t)len);+}++/* ---- feedback from the browser ---- */++static void set_rate(Rtc *r, double k) {+ unsigned lo = 300, hi = r->cfg.max_kbps ? r->cfg.max_kbps : 8000;+ unsigned nk = k < lo ? lo : k > hi ? hi : (unsigned)k;+ if (nk == r->kbps) return;+ unsigned old = r->kbps;+ r->kbps = nk;+ r->rate_at = now_ms();+ if (r->cb.rate && (nk > old * 1.04 || nk < old * 0.96)) r->cb.rate(r->cb.ud, nk);+}++static void on_fb(void *ud, const RwFb *f) {+ Rtc *r = ud;+ if (f->ssrc != r->v.ssrc && f->ssrc != r->a.ssrc && f->kind != RW_FB_REMB) return;+ double t = now_ms();+ switch (f->kind) {+ case RW_FB_RR:+ if (f->ssrc != r->v.ssrc) break;+ r->lost = r->lost * 0.7 + f->lost_frac / 256.0 * 0.3;+ if (f->lost_frac > 25) set_rate(r, r->kbps * 0.75);+ else if (f->lost_frac > 5) set_rate(r, r->kbps * 0.92);+ else if (f->lost_frac == 0 && t - r->rate_at > 1000) set_rate(r, r->kbps * 1.06 + 50);+ break;+ case RW_FB_PLI:+ case RW_FB_FIR:+ if (f->ssrc != r->v.ssrc) break;+ r->plis++;+ if (t - r->kf_at > 300 && r->cb.keyframe) { r->kf_at = t; r->cb.keyframe(r->cb.ud); }+ break;+ case RW_FB_NACK:+ if (f->ssrc != r->v.ssrc || !r->v.hist) break;+ r->nacks++;+ for (int i = -1; i < 16; i++) {+ if (i >= 0 && !(f->blp & (1u << i))) continue;+ uint16_t seq = (uint16_t)(f->pid + (i < 0 ? 0 : i + 1));+ Hist *h = &r->v.hist[seq % HIST];+ if (h->len && h->seq == seq) { send_sel(r, h->b, h->len); r->resent++; }+ }+ break;+ case RW_FB_REMB:+ if (f->bitrate / 1000 < r->kbps) set_rate(r, (double)(f->bitrate / 1000));+ break;+ }+}++/* ---- ICE-lite, the built-in STUN server, external STUN and TURN ---- */++static Path *path_for(Rtc *r, const RwAddr *a, int relay, int add) {+ for (int i = 0; i < r->npath; i++) if (r->path[i].relay == relay && rw_addr_equal(&r->path[i].a, a)) return &r->path[i];+ if (!add || r->npath >= MAX_PATHS) return NULL;+ Path *p = &r->path[r->npath++];+ memset(p, 0, sizeof *p);+ p->a = *a;+ p->relay = relay;+ return p;+}++static void stun_answer(Rtc *r, const RwStun *s, const RwAddr *from, int relay) {+ RwStunOut o;+ rw_so_init(&o, RW_STUN_BINDING_OK, s->tid);+ rw_so_xaddr(&o, RW_ATTR_XOR_MAPPED, from);+ rw_so_mi(&o, (const uint8_t *)r->pwd, strlen(r->pwd), hmac_sha1);+ rw_so_fingerprint(&o);+ if (!o.bad) send_to(r, from, relay, o.b, o.n);+}++/* The built-in STUN server: a plain binding request (no credentials) gets the address it came from,+ * on its own socket, at most 20 a second, only from addresses the rules allow. */+static void stun_server(Rtc *r, const uint8_t *p, size_t n, const RwAddr *from) {+ RwStun s;+ if (!rw_is_stun(p, n) || rw_stun_parse(p, n, &s) || s.type != RW_STUN_BINDING_REQ || s.username || s.mi_off) return;+ double t = now_ms();+ if (t - r->stun_win > 1000) { r->stun_win = t; r->stun_n = 0; }+ if (++r->stun_n > 20 || !src_allowed(r, from, 0)) return;+ RwStunOut o;+ rw_so_init(&o, RW_STUN_BINDING_OK, s.tid);+ rw_so_xaddr(&o, RW_ATTR_XOR_MAPPED, from);+ rw_so_fingerprint(&o);+ struct sockaddr_in6 sa;+ to_sockaddr(from, &sa);+ if (!o.bad) sendto(r->sfd, o.b, o.n, MSG_DONTWAIT | MSG_NOSIGNAL, (struct sockaddr *)&sa, sizeof sa);+}++static void ice_check(Rtc *r, const uint8_t *p, size_t n, const RwStun *s, const RwAddr *from, int relay) {+ if (!r->offered || !s->username || !s->mi_off || !s->has_fp) return;+ char want[300];+ int wl = snprintf(want, sizeof want, "%s:%s", r->ufrag, r->of->ufrag);+ if (wl < 0 || (size_t)wl != s->ulen || memcmp(want, s->username, s->ulen)) return;+ if (!rw_stun_check_mi(p, n, s, (const uint8_t *)r->pwd, strlen(r->pwd), hmac_sha1)) return;+ if (!src_allowed(r, from, relay)) {+ static int said;+ char a[64];+ rw_addr_format(from, a, sizeof a);+ if (!said++) sd_log("video: refused ICE check from %s (Settings -> Sharing: who can connect)", a);+ return;+ }+ Path *pa = path_for(r, from, relay, 1);+ if (!pa) return;+ pa->valid = 1;+ pa->last = now_ms();+ r->last_consent = pa->last;+ if (s->use_candidate && !pa->nominated) {+ for (int i = 0; i < r->npath; i++) r->path[i].nominated = 0;+ pa->nominated = 1;+ r->sel = (int)(pa - r->path);+ }+ if (r->sel < 0) r->sel = (int)(pa - r->path);+ stun_answer(r, s, from, relay);+}++static void turn_send(Rtc *r, uint16_t type, int auth, const RwAddr *peers, int npeers, uint32_t lifetime) {+ Turn *t = &r->turn;+ RwStunOut o;+ rnd(t->tid, 12);+ rw_so_init(&o, type, t->tid);+ if (type == RW_TURN_ALLOCATE_REQ) { uint8_t tr[4] = { 17, 0, 0, 0 }; rw_so_attr(&o, RW_ATTR_TRANSPORT, tr, 4); }+ if (lifetime) rw_so_u32(&o, RW_ATTR_LIFETIME, lifetime);+ for (int i = 0; i < npeers; i++) rw_so_xaddr(&o, RW_ATTR_XOR_PEER, &peers[i]);+ if (auth) {+ rw_so_attr(&o, RW_ATTR_USERNAME, r->cfg.turn_user, strlen(r->cfg.turn_user));+ rw_so_attr(&o, RW_ATTR_REALM, t->realm, strlen(t->realm));+ rw_so_attr(&o, RW_ATTR_NONCE, t->nonce, t->nlen);+ rw_so_mi(&o, t->key, 16, hmac_sha1);+ }+ rw_so_fingerprint(&o);+ if (!o.bad) raw_send(r, &t->srv, o.b, o.n);+ t->sent_at = now_ms();+}++static void wipe_pass(Rtc *r) {+ if (!r->cfg.turn_pass) return;+ explicit_bzero(r->cfg.turn_pass, strlen(r->cfg.turn_pass));+ free(r->cfg.turn_pass);+ r->cfg.turn_pass = NULL;+}++/* long-term credential key: MD5(user ":" realm ":" password) (RFC 8489 §9.2.2) */+static int turn_key(Rtc *r) {+ if (!r->cfg.turn_pass) return -1;+ char buf[1024];+ int k = snprintf(buf, sizeof buf, "%s:%s:%s", r->cfg.turn_user, r->turn.realm, r->cfg.turn_pass);+ unsigned int l = 16;+ int ok = k > 0 && (size_t)k < sizeof buf && EVP_Digest(buf, (size_t)k, r->turn.key, &l, EVP_md5(), NULL) && l == 16;+ explicit_bzero(buf, sizeof buf);+ wipe_pass(r);+ return ok ? 0 : -1;+}++static void turn_perms(Rtc *r) {+ Turn *t = &r->turn;+ if (t->state != 3 || !t->nperm) return;+ turn_send(r, RW_TURN_PERM_REQ, 1, t->perm, t->nperm, 0);+ t->perm_at = now_ms() + 240000;+ t->perm_dirty = 0;+}++static void turn_peer(Rtc *r, const RwAddr *a) {+ Turn *t = &r->turn;+ if (!t->state || t->state < 0 || a->family == 0) return;+ for (int i = 0; i < t->nperm; i++) if (same_ip(&t->perm[i], a)) return;+ if (t->nperm >= 8) return;+ t->perm[t->nperm] = *a;+ t->perm[t->nperm].port = 0;+ t->nperm++;+ t->perm_dirty = 1;+}++static void handle_packet(Rtc *r, const uint8_t *p, size_t n, const RwAddr *from, int relay);++static void turn_input(Rtc *r, const uint8_t *p, size_t n, const RwStun *s) {+ Turn *t = &r->turn;+ if (s->type == RW_TURN_DATA_IND) { /* a packet from a peer, relayed */+ if (t->state == 3 && s->data && s->xor_peer.family && s->dlen <= 1400) handle_packet(r, s->data, s->dlen, &s->xor_peer, 1);+ return;+ }+ if (memcmp(s->tid, t->tid, 12)) return;+ int cls = s->type & 0x0110;+ if (cls == 0x0100 && s->mi_off && !rw_stun_check_mi(p, n, s, t->key, 16, hmac_sha1)) return; /* forged answer */+ if (cls == 0x0110 && (s->error == 401 || s->error == 438)) {+ if (!s->realm || !s->nonce || s->rlen >= sizeof t->realm || s->nlen > sizeof t->nonce || ++t->tries > 3) { t->state = -1; sd_log("video: the TURN server refused us"); return; }+ memcpy(t->realm, s->realm, s->rlen);+ t->realm[s->rlen] = 0;+ memcpy(t->nonce, s->nonce, s->nlen);+ t->nlen = s->nlen;+ if (t->state == 1 && turn_key(r)) { t->state = -1; return; }+ if (t->state <= 2) { t->state = 2; turn_send(r, RW_TURN_ALLOCATE_REQ, 1, NULL, 0, 600); }+ else if (s->type == RW_TURN_PERM_ERR) turn_perms(r);+ else turn_send(r, RW_TURN_REFRESH_REQ, 1, NULL, 0, 600);+ return;+ }+ if (s->type == RW_TURN_ALLOCATE_OK) {+ if (!s->mi_off || !s->xor_relayed.family) { t->state = -1; return; }+ t->relayed = s->xor_relayed;+ t->mapped = s->xor_mapped;+ t->state = 3;+ t->tries = 0;+ unsigned life = s->lifetime ? s->lifetime : 600;+ t->refresh_at = now_ms() + (life > 120 ? life - 60 : life / 2) * 1000.0;+ char a[64];+ rw_addr_format(&t->relayed, a, sizeof a);+ sd_log("video: TURN relay %s:%u", a, t->relayed.port);+ for (int i = 0; i < r->nrcand; i++) turn_peer(r, &r->rcand[i]);+ turn_perms(r);+ } else if (s->type == RW_TURN_REFRESH_OK) {+ unsigned life = s->lifetime ? s->lifetime : 600;+ t->refresh_at = now_ms() + (life > 120 ? life - 60 : life / 2) * 1000.0;+ } else if (cls == 0x0110) {+ sd_log("video: TURN error %d", s->error);+ if (t->state < 3) t->state = -1;+ }+}++static void handle_packet(Rtc *r, const uint8_t *p, size_t n, const RwAddr *from, int relay) {+ if (!n || r->failed) return;+ if (rw_is_stun(p, n)) {+ RwStun s;+ if (rw_stun_parse(p, n, &s)) return;+ if (!relay && r->turn.state && rw_addr_equal(from, &r->turn.srv)) { turn_input(r, p, n, &s); return; }+ if (s.type == RW_STUN_BINDING_REQ) { ice_check(r, p, n, &s, from, relay); return; }+ if (s.type == RW_STUN_BINDING_OK && !relay)+ for (int i = 0; i < r->nstun_srv; i++)+ if (rw_addr_equal(from, &r->stun_srv[i]) && !memcmp(s.tid, r->stun_tid[i], 12) && s.xor_mapped.family && !r->srflx.family) r->srflx = s.xor_mapped;+ return;+ }+ /* DTLS and SRTP only from an address that passed an ICE check */+ Path *pa = path_for(r, from, relay, 0);+ if (!pa || !pa->valid) return;+ if (p[0] >= 20 && p[0] <= 63) {+ if (!r->offered) return;+ if (!r->dtls_up && r->sel != (int)(pa - r->path)) r->sel = (int)(pa - r->path);+ dtls_input(r, p, n);+ } else if (p[0] >= 128 && p[0] <= 191 && n >= 8 && p[1] >= 192 && p[1] <= 223 && r->srtp_up) {+ uint8_t b[1500 + SRTP_MAX_TRAILER_LEN];+ if (n > 1500) return;+ memcpy(b, p, n);+ int len = (int)n;+ if (srtp_unprotect_rtcp(r->rx, b, &len) != srtp_err_status_ok) return;+ rw_rtcp_parse(b, (size_t)len, on_fb, r);+ }+}++/* ---- life cycle ---- */++int rtc_port(const Rtc *r) { return r ? r->sport : 0; }++Rtc *rtc_new(const RtcConfig *cfg, const RtcCallbacks *cb) {+ Rtc *r = calloc(1, sizeof *r);+ if (!r) return NULL;+ r->cfg = *cfg;+ r->cb = *cb;+ r->sel = -1;+ r->fd = r->sfd = -1;+ r->created = r->last_consent = now_ms();+ r->kbps = cfg->max_kbps && cfg->max_kbps < 4000 ? cfg->max_kbps : 4000;+ rnd_text(r->ufrag, 8);+ rnd_text(r->pwd, 24);+ if (!srtp_ready) { if (srtp_init() != srtp_err_status_ok) goto bad; srtp_ready = 1; }+ if (dtls_init(r)) goto bad;+ r->fd = socket(AF_INET6, SOCK_DGRAM | SOCK_CLOEXEC | SOCK_NONBLOCK, 0);+ if (r->fd < 0) goto bad;+ int off = 0, buf = 2 << 20;+ setsockopt(r->fd, IPPROTO_IPV6, IPV6_V6ONLY, &off, sizeof off);+ setsockopt(r->fd, SOL_SOCKET, SO_SNDBUF, &buf, sizeof buf);+ setsockopt(r->fd, SOL_SOCKET, SO_RCVBUF, &buf, sizeof buf);+ struct sockaddr_in6 any = { .sin6_family = AF_INET6 };+ socklen_t sl = sizeof any;+ if (bind(r->fd, (struct sockaddr *)&any, sizeof any) || getsockname(r->fd, (struct sockaddr *)&any, &sl)) goto bad;+ r->port = ntohs(any.sin6_port);+ r->sfd = socket(AF_INET6, SOCK_DGRAM | SOCK_CLOEXEC | SOCK_NONBLOCK, 0);+ if (r->sfd < 0) goto bad;+ setsockopt(r->sfd, IPPROTO_IPV6, IPV6_V6ONLY, &off, sizeof off);+ struct sockaddr_in6 any2 = { .sin6_family = AF_INET6 };+ sl = sizeof any2;+ if (bind(r->sfd, (struct sockaddr *)&any2, sizeof any2) || getsockname(r->sfd, (struct sockaddr *)&any2, &sl)) goto bad;+ r->sport = ntohs(any2.sin6_port);+ uint32_t ids[4];+ rnd(ids, sizeof ids);+ r->v.ssrc = ids[0] | 1;+ r->a.ssrc = ids[1] | 2;+ r->v.ts_off = ids[2];+ r->a.ts_off = ids[3];+ r->v.seq = (uint16_t)ids[2];+ r->a.seq = (uint16_t)ids[3];+ r->v.hist = calloc(HIST, sizeof(Hist));+ /* external STUN servers: our public address, if any */+ for (int i = 0; i < cfg->nstun && r->nstun_srv < RTC_MAX_STUN; i++) {+ RwAddr a;+ if (resolve(cfg->stun[i], &a)) continue;+ r->stun_srv[r->nstun_srv] = a;+ rnd(r->stun_tid[r->nstun_srv], 12);+ RwStunOut o;+ rw_so_init(&o, RW_STUN_BINDING_REQ, r->stun_tid[r->nstun_srv]);+ rw_so_fingerprint(&o);+ raw_send(r, &a, o.b, o.n);+ r->nstun_srv++;+ }+ if (cfg->turn[0] && cfg->turn_user[0] && cfg->turn_pass) {+ if (!resolve(cfg->turn, &r->turn.srv)) { r->turn.state = 1; turn_send(r, RW_TURN_ALLOCATE_REQ, 0, NULL, 0, 600); }+ else sd_log("video: the TURN server's name does not resolve");+ }+ if (r->turn.state != 1) wipe_pass(r);+ return r;+bad:+ sd_log("video: could not start the WebRTC peer");+ rtc_free(r);+ return NULL;+}++void rtc_free(Rtc *r) {+ if (!r) return;+ if (r->sctp) { usrsctp_close(r->sctp); r->sctp = NULL; }+ if (r->sctp_on) usrsctp_deregister_address(r);+ if (r->tx) srtp_dealloc(r->tx);+ if (r->rx) srtp_dealloc(r->rx);+ if (r->ssl) SSL_free(r->ssl);+ if (r->ctx) SSL_CTX_free(r->ctx);+ X509_free(r->cert);+ EVP_PKEY_free(r->key);+ if (r->fd >= 0) close(r->fd);+ if (r->sfd >= 0) close(r->sfd);+ wipe_pass(r);+ explicit_bzero(r->turn.key, sizeof r->turn.key);+ explicit_bzero(r->pwd, sizeof r->pwd);+ free(r->v.hist);+ free(r->rasm);+ free(r->of);+ free(r);+}++/* host candidates: the address the WebSocket came in on first, then the other interfaces */+static int local_cands(Rtc *r, RwLocalCand *c, int max) {+ int n = 0;+ RwAddr seen[16];+ int ns = 0;+ if (r->cfg.ws_local.family) {+ rw_addr_format(&r->cfg.ws_local, c[n].ip, sizeof c[n].ip);+ c[n].port = (uint16_t)r->port;+ c[n].type = RW_CAND_HOST;+ c[n].prio = (126u << 24) | (65535u << 8) | 255u;+ seen[ns++] = r->cfg.ws_local;+ n++;+ }+ struct ifaddrs *ifs = NULL;+ if (!getifaddrs(&ifs)) {+ for (struct ifaddrs *i = ifs; i && n < max - 2 && ns < 16; i = i->ifa_next) {+ if (!i->ifa_addr || (i->ifa_flags & IFF_LOOPBACK) || !(i->ifa_flags & IFF_UP)) continue;+ RwAddr a = { 0 };+ if (i->ifa_addr->sa_family == AF_INET) { a.family = 4; memcpy(a.a, &((struct sockaddr_in *)i->ifa_addr)->sin_addr, 4); }+ else if (i->ifa_addr->sa_family == AF_INET6) {+ const uint8_t *b = ((struct sockaddr_in6 *)i->ifa_addr)->sin6_addr.s6_addr;+ if (b[0] == 0xfe && (b[1] & 0xc0) == 0x80) continue; /* link-local: needs a scope */+ a.family = 6;+ memcpy(a.a, b, 16);+ } else continue;+ int dup = 0;+ for (int k = 0; k < ns; k++) if (same_ip(&seen[k], &a)) dup = 1;+ if (dup) continue;+ seen[ns++] = a;+ rw_addr_format(&a, c[n].ip, sizeof c[n].ip);+ c[n].port = (uint16_t)r->port;+ c[n].type = RW_CAND_HOST;+ c[n].prio = (126u << 24) | ((unsigned)(60000 - n * 10 - (a.family == 6 ? 5 : 0)) << 8) | 255u;+ n++;+ }+ freeifaddrs(ifs);+ }+ if (r->srflx.family && n < max) {+ rw_addr_format(&r->srflx, c[n].ip, sizeof c[n].ip);+ c[n].port = r->srflx.port;+ c[n].type = RW_CAND_SRFLX;+ c[n].prio = (100u << 24) | (65535u << 8) | 255u;+ snprintf(c[n].raddr, sizeof c[n].raddr, "%s", n ? c[0].ip : "0.0.0.0");+ c[n].rport = (uint16_t)r->port;+ n++;+ }+ if (r->turn.state == 3 && n < max) {+ rw_addr_format(&r->turn.relayed, c[n].ip, sizeof c[n].ip);+ c[n].port = r->turn.relayed.port;+ c[n].type = RW_CAND_RELAY;+ c[n].prio = (2u << 24) | (65535u << 8) | 255u;+ rw_addr_format(&r->turn.mapped, c[n].raddr, sizeof c[n].raddr);+ c[n].rport = r->turn.mapped.port;+ n++;+ }+ return n;+}++static void json_escape(const char *s, char *out, size_t cap) {+ size_t o = 0;+ for (; *s && o + 7 < cap; s++) {+ unsigned char c = (unsigned char)*s;+ if (c == '"' || c == '\\') { out[o++] = '\\'; out[o++] = (char)c; }+ else if (c == '\r') { out[o++] = '\\'; out[o++] = 'r'; }+ else if (c == '\n') { out[o++] = '\\'; out[o++] = 'n'; }+ else if (c >= 0x20 && c < 0x7f) out[o++] = (char)c;+ }+ out[o] = 0;+}++static void send_answer(Rtc *r) {+ if (r->answered) return;+ r->answered = 1;+ RwLocalCand c[12];+ int nc = local_cands(r, c, 12);+ uint32_t sid;+ rnd(&sid, sizeof sid);+ RwAnswer an = { r->ufrag, r->pwd, r->fp, r->v.ssrc, r->a.ssrc, r->of->am >= 0, c, nc, sid & 0x7fffffff };+ static char sdp[16384], esc[20000], msg[20100];+ int l = rw_sdp_answer(r->of, &an, sdp, sizeof sdp);+ if (l < 0) { fail(r, "the answer does not fit"); return; }+ json_escape(sdp, esc, sizeof esc);+ snprintf(msg, sizeof msg, "{\"t\":\"rtc\",\"sdp\":\"%s\",\"codec\":\"%s\"}", esc, r->codec == RW_CODEC_H264 ? "h264" : "vp8");+ if (r->cb.signal) r->cb.signal(r->cb.ud, msg);+}++int rtc_offer(Rtc *r, const char *sdp, size_t n, int allow_h264) {+ if (!r || r->offered || r->failed) return -1;+ r->of = malloc(sizeof *r->of);+ if (!r->of) return -1;+ int e = rw_sdp_parse_offer(sdp, n, allow_h264, r->of);+ if (e) { sd_log("video: refused the browser's offer (%d)", e); free(r->of); r->of = NULL; return e; }+ r->offered = 1;+ r->codec = r->of->vcodec;+ r->v.pt = r->of->vpt;+ r->a.pt = r->of->apt;+ for (int i = 0; i < r->of->ncand && r->nrcand < 16; i++) {+ const RwCand *c = &r->of->cand[i];+ if (!c->mdns && !c->tcp && c->component == 1) { r->rcand[r->nrcand++] = c->addr; turn_peer(r, &c->addr); }+ }+ turn_perms(r);+ int waiting = (r->nstun_srv && !r->srflx.family) || (r->turn.state > 0 && r->turn.state < 3);+ if (waiting) r->answer_due = now_ms() + ANSWER_WAIT_MS;+ else send_answer(r);+ return r->codec;+}++int rtc_candidate(Rtc *r, const char *c, size_t n) {+ RwCand k;+ if (!r || !r->offered) return -1;+ if (n == 0) return 0; /* end of candidates */+ if (rw_cand_parse(c, n, &k)) return -1;+ if (!k.mdns && !k.tcp && k.component == 1 && r->nrcand < 16) {+ r->rcand[r->nrcand++] = k.addr;+ turn_peer(r, &k.addr);+ if (r->turn.perm_dirty) turn_perms(r);+ }+ return 0;+}++int rtc_pollfds(Rtc *r, struct pollfd *p, int max) {+ if (!r || r->fd < 0 || max < 2 || r->failed) return 0;+ p[0] = (struct pollfd){ r->fd, POLLIN, 0 };+ p[1] = (struct pollfd){ r->sfd, POLLIN, 0 };+ return 2;+}++void rtc_dispatch(Rtc *r, const struct pollfd *p, int n) {+ if (!r || n < 2) return;+ static uint8_t buf[2048];+ for (int f = 0; f < 2; f++) {+ if (!(p[f].revents & POLLIN)) continue;+ for (int k = 0; k < 256 && !r->failed; k++) {+ struct sockaddr_in6 s;+ socklen_t sl = sizeof s;+ ssize_t got = recvfrom(f ? r->sfd : r->fd, buf, sizeof buf, MSG_DONTWAIT, (struct sockaddr *)&s, &sl);+ if (got < 0) break;+ if (got > 1500 || s.sin6_family != AF_INET6) continue;+ RwAddr from;+ from_sockaddr(&s, &from);+ if (f) stun_server(r, buf, (size_t)got, &from);+ else handle_packet(r, buf, (size_t)got, &from, 0);+ }+ }+}++int rtc_timeout(Rtc *r) {+ if (!r || r->failed) return -1;+ int t = 1000;+ if (r->sctp_on) t = 10;+ if (r->answer_due && !r->answered) t = 50;+ if (r->ssl && !r->dtls_up && r->offered) {+ struct timeval tv;+ if (DTLSv1_get_timeout(r->ssl, &tv)) { int ms = (int)(tv.tv_sec * 1000 + tv.tv_usec / 1000); if (ms < t) t = ms < 1 ? 1 : ms; }+ }+ return t;+}++void rtc_tick(Rtc *r) {+ if (!r || r->failed) return;+ double t = now_ms();+ if (!r->answered && r->answer_due && (t >= r->answer_due || ((!r->nstun_srv || r->srflx.family) && (r->turn.state <= 0 || r->turn.state == 3)))) send_answer(r);+ if (r->ssl && !r->dtls_up && r->offered) DTLSv1_handle_timeout(r->ssl);+ if (r->sctp_on) {+ uint32_t el = (uint32_t)(t - r->sctp_t);+ if (el >= 1) { r->sctp_t = t; usrsctp_handle_timers(el); }+ }+ if (r->srtp_up && t - r->last_sr > 1000) { send_sr(r, &r->v); send_sr(r, &r->a); r->last_sr = t; }+ Turn *tu = &r->turn;+ if (tu->state == 1 || tu->state == 2) {+ if (t - tu->sent_at > 500) { if (++tu->tries > 6) { tu->state = -1; wipe_pass(r); sd_log("video: no answer from the TURN server"); } else turn_send(r, RW_TURN_ALLOCATE_REQ, tu->state == 2, NULL, 0, 600); }+ } else if (tu->state == 3) {+ if (t >= tu->refresh_at) { turn_send(r, RW_TURN_REFRESH_REQ, 1, NULL, 0, 600); tu->refresh_at = t + 60000; }+ if (tu->perm_dirty || (tu->nperm && t >= tu->perm_at)) turn_perms(r);+ }+ if (r->offered && t - r->last_consent > CONSENT_MS) fail(r, "the browser stopped answering (no ICE checks)");+ if (r->offered && !r->dtls_up && t - r->created > 20000) fail(r, "no connection after 20 s");+}++int rtc_send(Rtc *r, int chan, const void *p, size_t n, int binary) {+ if (!r || r->failed || chan < 0 || chan > 1 || !r->dc_open[chan] || n > MAX_DC_MSG) return -1;+ return dc_raw(r, r->dc_sid[chan], binary ? 53 : 51, p, n);+}++int rtc_dc_open(const Rtc *r, int chan) { return r && !r->failed && chan >= 0 && chan < 2 && r->dc_open[chan]; }++void rtc_stats(const Rtc *r, char *out, size_t n) {+ if (!r) { snprintf(out, n, "-"); return; }+ snprintf(out, n, "%s %u kbps, %.1f %% lost, %lu NACK (%lu resent), %lu PLI", r->codec == RW_CODEC_H264 ? "h264" : "vp8", r->kbps,+ r->lost * 100, r->nacks, r->resent, r->plis);+}diff --git a/server/src/remote/rtc.h b/server/src/remote/rtc.hnew file mode 100644index 0000000..260c9e0--- /dev/null+++ b/server/src/remote/rtc.h@@ -0,0 +1,68 @@+/* rtc.h — the remote desktop's WebRTC peer, inside the bridge (see rtc.c and docs/REMOTE.md,+ * "Video (WebRTC)"). One per browser connection, in the process that already dropped root: ICE-lite+ * with a built-in STUN responder, DTLS-SRTP (OpenSSL), SRTP (libsrtp2), the data channels over SCTP+ * (usrsctp), RTP for the session's encoded video and Opus audio, and an optional TURN relay. */+#ifndef LORD_REMOTE_RTC_H+#define LORD_REMOTE_RTC_H++#include <poll.h>+#include <stddef.h>+#include <stdint.h>++#include "ipnet.h"+#include "rtcwire.h"++typedef struct Rtc Rtc;++enum { RTC_CONNECTED = 1, RTC_DC_OPEN, RTC_FAILED };+enum { RTC_CH_INPUT = 0, RTC_CH_TILES = 1 };++typedef struct {+ void (*signal)(void *ud, const char *json); /* a message for the browser over the WebSocket */+ void (*state)(void *ud, int state, const char *why);+ void (*message)(void *ud, int chan, const uint8_t *p, size_t n, int binary); /* from a data channel */+ void (*keyframe)(void *ud); /* the browser lost a frame (PLI/FIR) */+ void (*rate)(void *ud, unsigned kbps); /* new target bit rate for this viewer */+ void *ud;+} RtcCallbacks;++#define RTC_MAX_STUN 4+typedef struct {+ const RsAccess *access; /* who may send us UDP: the same rules as the WebSocket (ipnet.c) */+ int net_mode;+ RwAddr ws_peer, ws_local; /* the authenticated WebSocket's addresses */+ int nstun;+ char stun[RTC_MAX_STUN][96]; /* host:port of external STUN servers (Settings → Sharing) */+ char turn[96], turn_user[129]; /* host:port and user of a TURN server, or "" */+ char *turn_pass; /* in memory only; rtc_new takes it over and wipes it when done */+ unsigned max_kbps;+} RtcConfig;++Rtc *rtc_new(const RtcConfig *cfg, const RtcCallbacks *cb);+void rtc_free(Rtc *r);+int rtc_port(const Rtc *r); /* the built-in STUN server's UDP port (for the browser) */++/* The browser's offer (from the authenticated WebSocket): parsed strictly (rtcwire.c); the answer goes+ * out through signal(). Returns the chosen RW_CODEC_* or < 0. Only one offer per connection. */+int rtc_offer(Rtc *r, const char *sdp, size_t n, int allow_h264);+/* A trickled candidate from the browser ("candidate:..."): only used for TURN permissions. */+int rtc_candidate(Rtc *r, const char *c, size_t n);++int rtc_pollfds(Rtc *r, struct pollfd *p, int max);+void rtc_dispatch(Rtc *r, const struct pollfd *p, int n);+int rtc_timeout(Rtc *r); /* ms until rtc_tick is due */+void rtc_tick(Rtc *r);++int rtc_codec(const Rtc *r); /* RW_CODEC_* agreed, 0 before the offer */+int rtc_media_ready(const Rtc *r); /* SRTP keys are there: media can flow */+/* One encoded video frame (H.264 Annex B or VP8), ms on the session's clock. */+void rtc_video(Rtc *r, const uint8_t *f, size_t n, uint32_t ms);+/* One Opus packet, its first sample on a 48 kHz clock. */+void rtc_audio(Rtc *r, const uint8_t *p, size_t n, uint32_t samples);+/* Data channel message; -1 if the channel isn't open or its send buffer is full (try later). */+int rtc_send(Rtc *r, int chan, const void *p, size_t n, int binary);+int rtc_dc_open(const Rtc *r, int chan);+/* Line for the logs and the browser's status: "h264 2400 kbps 1.2 % lost" */+void rtc_stats(const Rtc *r, char *out, size_t n);++#endifdiff --git a/server/src/remote/rtcwire.c b/server/src/remote/rtcwire.cnew file mode 100644index 0000000..e9eb3bf--- /dev/null+++ b/server/src/remote/rtcwire.c@@ -0,0 +1,852 @@+/* rtcwire.c — WebRTC wire formats for the remote desktop's video path (see rtcwire.h and+ * docs/REMOTE.md, "Video (WebRTC)"). Plain libc: unit tests in tests/remote_sec_test.c and the+ * ASan+UBSan fuzz run in tests/remote_fuzz.c. */+#include "rtcwire.h"++#include <arpa/inet.h>+#include <stdarg.h>+#include <stdio.h>+#include <stdlib.h>+#include <string.h>++static uint16_t be16(const uint8_t *p) { return (uint16_t)(p[0] << 8 | p[1]); }+static uint32_t be32(const uint8_t *p) { return (uint32_t)p[0] << 24 | (uint32_t)p[1] << 16 | (uint32_t)p[2] << 8 | p[3]; }+static void put16(uint8_t *p, uint16_t v) { p[0] = (uint8_t)(v >> 8); p[1] = (uint8_t)v; }+static void put32(uint8_t *p, uint32_t v) { p[0] = (uint8_t)(v >> 24); p[1] = (uint8_t)(v >> 16); p[2] = (uint8_t)(v >> 8); p[3] = (uint8_t)v; }++/* ---- addresses ---- */++int rw_addr_text(const char *s, RwAddr *a) {+ uint8_t b[16];+ size_t l = strlen(s);+ if (!l || l > 45) return -1;+ for (size_t i = 0; i < l; i++) if (!strchr("0123456789abcdefABCDEF:.", s[i])) return -1;+ if (inet_pton(AF_INET, s, b) == 1) { a->family = 4; memset(a->a, 0, 16); memcpy(a->a, b, 4); return 0; }+ if (inet_pton(AF_INET6, s, b) == 1) {+ static const uint8_t mapped[12] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xff, 0xff };+ if (!memcmp(b, mapped, 12)) { a->family = 4; memset(a->a, 0, 16); memcpy(a->a, b + 12, 4); return 0; }+ a->family = 6;+ memcpy(a->a, b, 16);+ return 0;+ }+ return -1;+}++void rw_addr_format(const RwAddr *a, char *out, size_t n) {+ if (!n) return;+ out[0] = 0;+ if (a->family == 4) inet_ntop(AF_INET, a->a, out, (socklen_t)n);+ else if (a->family == 6) inet_ntop(AF_INET6, a->a, out, (socklen_t)n);+}++int rw_addr_equal(const RwAddr *x, const RwAddr *y) {+ if (x->family != y->family || x->port != y->port || !x->family) return 0;+ return !memcmp(x->a, y->a, x->family == 4 ? 4 : 16);+}++int rw_ice_server_valid(const char *s) {+ size_t n = strlen(s);+ const char *colon = strrchr(s, ':');+ if (!n || n > 95 || !colon || colon == s) return 0;+ size_t hl = (size_t)(colon - s), pl = n - hl - 1;+ if (!pl || pl > 5 || (pl > 1 && colon[1] == '0')) return 0;+ long port = 0;+ for (size_t i = 0; i < pl; i++) { if (colon[1 + i] < '0' || colon[1 + i] > '9') return 0; port = port * 10 + (colon[1 + i] - '0'); }+ if (port < 1 || port > 65535) return 0;+ char host[96];+ memcpy(host, s, hl);+ host[hl] = 0;+ RwAddr a;+ if (host[0] == '[') {+ if (hl < 4 || host[hl - 1] != ']') return 0;+ host[hl - 1] = 0;+ return !rw_addr_text(host + 1, &a) && a.family == 6;+ }+ if (!rw_addr_text(host, &a)) return a.family == 4;+ if (hl > 63) return 0;+ for (size_t i = 0; i < hl; i++) {+ char c = host[i];+ int ok = (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9') || ((c == '-' || c == '.') && i > 0 && i < hl - 1);+ if (!ok || (c == '.' && host[i - 1] == '.')) return 0;+ }+ return 1;+}++int rw_turn_user_valid(const char *s) {+ size_t n = strlen(s);+ if (!n || n > 128) return 0;+ for (size_t i = 0; i < n; i++) if (s[i] <= 0x20 || s[i] > 0x7e || s[i] == '"' || s[i] == '\\') return 0;+ return 1;+}++int rw_turn_pass_valid(const char *s) {+ size_t n = strlen(s);+ if (!n || n > 128) return 0;+ for (size_t i = 0; i < n; i++) if (s[i] < 0x20 || s[i] > 0x7e) return 0;+ return 1;+}++/* ---- small text helpers ---- */++static int ice_char(char c) {+ return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9') || c == '+' || c == '/';+}+static int all_ice(const char *s, size_t n) { for (size_t i = 0; i < n; i++) if (!ice_char(s[i])) return 0; return 1; }+static int tok_char(char c) { return c > 0x20 && c < 0x7f; }++/* An unsigned decimal of at most `digits` digits, no leading zeros (except "0"), up to max. */+static int parse_uint(const char *s, size_t n, unsigned long long max, unsigned long long *v) {+ if (!n || n > 20 || (n > 1 && s[0] == '0')) return -1;+ unsigned long long x = 0;+ for (size_t i = 0; i < n; i++) {+ if (s[i] < '0' || s[i] > '9') return -1;+ x = x * 10 + (unsigned)(s[i] - '0');+ if (x > max) return -1;+ }+ *v = x;+ return 0;+}++/* Splits s[0..n) on single spaces into at most max tokens; -1 on an empty token or too many. */+static int split(const char *s, size_t n, const char **tok, size_t *len, int max) {+ int k = 0;+ size_t i = 0;+ while (i < n) {+ size_t j = i;+ while (j < n && s[j] != ' ') j++;+ if (j == i || k >= max) return -1;+ tok[k] = s + i;+ len[k] = j - i;+ k++;+ i = j + 1;+ if (j < n && i == n) return -1; /* trailing space */+ }+ return k;+}++static int eq(const char *s, size_t n, const char *lit) { return strlen(lit) == n && !memcmp(s, lit, n); }+static int ieq(const char *s, size_t n, const char *lit) {+ if (strlen(lit) != n) return 0;+ for (size_t i = 0; i < n; i++) {+ char a = s[i], b = lit[i];+ if (a >= 'A' && a <= 'Z') a = (char)(a + 32);+ if (b >= 'A' && b <= 'Z') b = (char)(b + 32);+ if (a != b) return 0;+ }+ return 1;+}+static int prefix(const char *s, size_t n, const char *lit, size_t *after) {+ size_t l = strlen(lit);+ if (n < l || memcmp(s, lit, l)) return 0;+ *after = l;+ return 1;+}++static int hexval(char c) {+ if (c >= '0' && c <= '9') return c - '0';+ if (c >= 'a' && c <= 'f') return c - 'a' + 10;+ if (c >= 'A' && c <= 'F') return c - 'A' + 10;+ return -1;+}++/* ---- ICE candidates ---- */++int rw_cand_parse(const char *s, size_t n, RwCand *c) {+ memset(c, 0, sizeof *c);+ size_t off;+ if (prefix(s, n, "a=", &off)) { s += off; n -= off; }+ if (n > 512 || !prefix(s, n, "candidate:", &off)) return RW_ESYNTAX;+ for (size_t i = 0; i < n; i++) if (s[i] < 0x20 || s[i] > 0x7e) return RW_ESYNTAX;+ s += off;+ n -= off;+ const char *t[24];+ size_t l[24];+ int k = split(s, n, t, l, 24);+ if (k < 8 || (k - 8) % 2) return RW_ESYNTAX;+ if (l[0] > 32 || !all_ice(t[0], l[0])) return RW_ESYNTAX; /* foundation */+ unsigned long long v;+ if (parse_uint(t[1], l[1], 256, &v) || v < 1) return RW_ESYNTAX;+ c->component = (int)v;+ if (ieq(t[2], l[2], "tcp")) c->tcp = 1;+ else if (!ieq(t[2], l[2], "udp")) return RW_ESYNTAX;+ if (parse_uint(t[3], l[3], 0xffffffffull, &v)) return RW_ESYNTAX;+ c->prio = (uint32_t)v;+ char host[64];+ if (l[4] >= sizeof host) return RW_ESYNTAX;+ memcpy(host, t[4], l[4]);+ host[l[4]] = 0;+ if (rw_addr_text(host, &c->addr)) {+ /* an mDNS name: <uuid>.local (browsers hide local addresses this way) */+ if (l[4] < 7 || l[4] > 63 || memcmp(host + l[4] - 6, ".local", 6)) return RW_ESYNTAX;+ for (size_t i = 0; i < l[4] - 6; i++) if (!((host[i] >= '0' && host[i] <= '9') || (host[i] >= 'a' && host[i] <= 'f') || host[i] == '-')) return RW_ESYNTAX;+ c->mdns = 1;+ }+ if (parse_uint(t[5], l[5], 65535, &v)) return RW_ESYNTAX;+ if (v == 0 && !c->tcp) return RW_ESYNTAX;+ c->addr.port = (uint16_t)v;+ if (!eq(t[6], l[6], "typ")) return RW_ESYNTAX;+ if (eq(t[7], l[7], "host")) c->type = RW_CAND_HOST;+ else if (eq(t[7], l[7], "srflx")) c->type = RW_CAND_SRFLX;+ else if (eq(t[7], l[7], "prflx")) c->type = RW_CAND_PRFLX;+ else if (eq(t[7], l[7], "relay")) c->type = RW_CAND_RELAY;+ else return RW_ESYNTAX;+ /* extensions: name value pairs of printable tokens (raddr, rport, generation, ufrag, network-id…) */+ for (int i = 8; i < k; i += 2) {+ if (l[i] > 32 || l[i + 1] > 64) return RW_ESYNTAX;+ for (size_t j = 0; j < l[i]; j++) if (!((t[i][j] >= 'a' && t[i][j] <= 'z') || t[i][j] == '-')) return RW_ESYNTAX;+ for (size_t j = 0; j < l[i + 1]; j++) if (!tok_char(t[i + 1][j])) return RW_ESYNTAX;+ }+ return RW_OK;+}++/* ---- SDP offer ---- */++typedef struct { const char *p; size_t n; } Line;++static RwCodec *find_pt(RwMedia *m, unsigned pt) {+ for (int i = 0; i < m->npt; i++) if (m->pt[i].pt == (int)pt) return &m->pt[i];+ return NULL;+}++static int set_str(char *dst, size_t cap, const char *s, size_t n) {+ if (dst[0]) return strlen(dst) == n && !memcmp(dst, s, n) ? 0 : -1; /* the same everywhere */+ if (n >= cap) return -1;+ memcpy(dst, s, n);+ dst[n] = 0;+ return 0;+}++static int parse_fp(const char *s, size_t n, uint8_t out[32]) {+ if (n != 32 * 3 - 1) return -1;+ for (int i = 0; i < 32; i++) {+ int h = hexval(s[i * 3]), l = hexval(s[i * 3 + 1]);+ if (h < 0 || l < 0 || (i < 31 && s[i * 3 + 2] != ':')) return -1;+ out[i] = (uint8_t)(h << 4 | l);+ }+ return 0;+}++static int parse_mline(const char *s, size_t n, RwMedia *m) {+ const char *t[RW_MAX_PT + 3];+ size_t l[RW_MAX_PT + 3];+ int k = split(s, n, t, l, RW_MAX_PT + 3);+ if (k < 4) return k == -1 && n > 64 ? RW_ETOOBIG : RW_ESYNTAX;+ unsigned long long port;+ if (parse_uint(t[1], l[1], 65535, &port)) return RW_ESYNTAX;+ if (eq(t[0], l[0], "audio")) m->kind = RW_KIND_AUDIO;+ else if (eq(t[0], l[0], "video")) m->kind = RW_KIND_VIDEO;+ else if (eq(t[0], l[0], "application")) m->kind = RW_KIND_APP;+ else { m->kind = 0; m->rejected = 1; return RW_OK; } /* something else: answered with port 0 */+ if (port == 0) m->rejected = 1;+ if (m->kind == RW_KIND_APP) {+ if (!eq(t[2], l[2], "UDP/DTLS/SCTP") || k != 4 || !eq(t[3], l[3], "webrtc-datachannel")) m->rejected = 1;+ return RW_OK;+ }+ if (!eq(t[2], l[2], "UDP/TLS/RTP/SAVPF")) m->rejected = 1;+ for (int i = 3; i < k; i++) {+ unsigned long long pt;+ if (parse_uint(t[i], l[i], 127, &pt)) return RW_ESYNTAX;+ if (find_pt(m, (unsigned)pt)) return RW_ESYNTAX; /* listed twice */+ RwCodec *c = &m->pt[m->npt++];+ memset(c, 0, sizeof *c);+ c->pt = (int)pt;+ }+ return RW_OK;+}++/* "<pt> rest": the payload type (listed on the m= line) and what follows */+static RwCodec *pt_attr(RwMedia *m, const char *s, size_t n, const char **rest, size_t *rl) {+ size_t i = 0;+ while (i < n && s[i] != ' ') i++;+ unsigned long long pt;+ if (i == n || parse_uint(s, i, 127, &pt)) return NULL;+ *rest = s + i + 1;+ *rl = n - i - 1;+ return find_pt(m, (unsigned)pt);+}++static int parse_fmtp(RwCodec *c, const char *s, size_t n) {+ if (!n) return RW_ESYNTAX;+ c->has_fmtp = 1;+ size_t i = 0;+ while (i < n) {+ size_t j = i;+ while (j < n && s[j] != ';') j++;+ const char *kv = s + i;+ size_t kl = j - i;+ while (kl && kv[0] == ' ') { kv++; kl--; }+ size_t a;+ unsigned long long v;+ if (prefix(kv, kl, "packetization-mode=", &a)) {+ if (parse_uint(kv + a, kl - a, 2, &v)) return RW_ESYNTAX;+ c->pmode = (int)v;+ } else if (prefix(kv, kl, "profile-level-id=", &a)) {+ if (kl - a != 6) return RW_ESYNTAX;+ uint32_t x = 0;+ for (size_t q = a; q < kl; q++) { int h = hexval(kv[q]); if (h < 0) return RW_ESYNTAX; x = x << 4 | (uint32_t)h; }+ c->plid = x;+ }+ i = j + 1;+ }+ return RW_OK;+}++static int parse_attr(RwOffer *o, RwMedia *m, const char *s, size_t n, int *bundle, int *mux) {+ size_t a;+ unsigned long long v;+ if (prefix(s, n, "ice-ufrag:", &a)) {+ if (n - a < 4 || n - a > 256 || !all_ice(s + a, n - a) || set_str(o->ufrag, sizeof o->ufrag, s + a, n - a)) return RW_EICE;+ } else if (prefix(s, n, "ice-pwd:", &a)) {+ if (n - a < 22 || n - a > 256 || !all_ice(s + a, n - a) || set_str(o->pwd, sizeof o->pwd, s + a, n - a)) return RW_EICE;+ } else if (prefix(s, n, "fingerprint:", &a)) {+ const char *f = s + a;+ size_t fl = n - a, sp;+ if (prefix(f, fl, "sha-256 ", &sp) || prefix(f, fl, "SHA-256 ", &sp)) {+ uint8_t fp[32];+ static const uint8_t zero[32];+ if (parse_fp(f + sp, fl - sp, fp)) return RW_EFP;+ if (memcmp(o->fp, zero, 32) && memcmp(o->fp, fp, 32)) return RW_EFP; /* two different ones */+ memcpy(o->fp, fp, 32);+ }+ /* other hash functions are not used */+ } else if (prefix(s, n, "setup:", &a)) {+ int st = eq(s + a, n - a, "actpass") ? RW_SETUP_ACTPASS : eq(s + a, n - a, "active") ? RW_SETUP_ACTIVE : 0;+ if (!st || (o->setup && o->setup != st)) return RW_ESYNTAX; /* we are the passive side */+ o->setup = st;+ } else if (prefix(s, n, "group:BUNDLE ", &a)) {+ if (m) return RW_ESYNTAX;+ *bundle = 1;+ } else if (prefix(s, n, "candidate:", &a)) {+ RwCand c;+ if (rw_cand_parse(s, n, &c)) return RW_ESYNTAX;+ if (o->ncand < RW_MAX_CAND) o->cand[o->ncand++] = c;+ } else if (!m) {+ return RW_OK; /* other session attributes */+ } else if (prefix(s, n, "mid:", &a)) {+ if (m->mid[0] || n - a < 1 || n - a > 32) return RW_ESYNTAX;+ for (size_t i = a; i < n; i++) if (!((s[i] >= 'a' && s[i] <= 'z') || (s[i] >= 'A' && s[i] <= 'Z') || (s[i] >= '0' && s[i] <= '9') || s[i] == '_' || s[i] == '-')) return RW_ESYNTAX;+ memcpy(m->mid, s + a, n - a);+ m->mid[n - a] = 0;+ } else if (eq(s, n, "recvonly")) m->dir = RW_DIR_RECVONLY;+ else if (eq(s, n, "sendrecv")) m->dir = RW_DIR_SENDRECV;+ else if (eq(s, n, "sendonly")) m->dir = RW_DIR_SENDONLY;+ else if (eq(s, n, "inactive")) m->dir = RW_DIR_INACTIVE;+ else if (eq(s, n, "rtcp-mux")) *mux = 1;+ else if (prefix(s, n, "rtpmap:", &a)) {+ const char *r;+ size_t rl;+ RwCodec *c = pt_attr(m, s + a, n - a, &r, &rl);+ if (!c) return RW_ESYNTAX;+ if (c->name[0]) return RW_ESYNTAX; /* twice */+ const char *t[3];+ size_t l[3];+ size_t i = 0;+ int k = 0;+ while (k < 3) { /* name/clock[/channels] */+ size_t j = i;+ while (j < rl && r[j] != '/') j++;+ if (j == i) return RW_ESYNTAX;+ t[k] = r + i;+ l[k] = j - i;+ k++;+ if (j == rl) break;+ i = j + 1;+ if (i == rl) return RW_ESYNTAX;+ }+ if (k < 2 || l[0] >= sizeof c->name || t[k - 1] + l[k - 1] != r + rl) return RW_ESYNTAX;+ for (size_t q = 0; q < l[0]; q++) if (!((t[0][q] >= 'a' && t[0][q] <= 'z') || (t[0][q] >= 'A' && t[0][q] <= 'Z') || (t[0][q] >= '0' && t[0][q] <= '9') || t[0][q] == '-' || t[0][q] == '_')) return RW_ESYNTAX;+ if (parse_uint(t[1], l[1], 1000000, &v) || !v) return RW_ESYNTAX;+ c->clock = (int)v;+ c->channels = 1;+ if (k == 3) { if (parse_uint(t[2], l[2], 16, &v) || !v) return RW_ESYNTAX; c->channels = (int)v; }+ memcpy(c->name, t[0], l[0]);+ c->name[l[0]] = 0;+ } else if (prefix(s, n, "fmtp:", &a)) {+ const char *r;+ size_t rl;+ RwCodec *c = pt_attr(m, s + a, n - a, &r, &rl);+ if (!c || c->has_fmtp) return RW_ESYNTAX;+ return parse_fmtp(c, r, rl);+ } else if (prefix(s, n, "rtcp-fb:", &a)) {+ const char *r = NULL;+ size_t rl = 0;+ RwCodec *c = NULL;+ int all = n - a >= 2 && s[a] == '*' && s[a + 1] == ' ';+ if (all) { r = s + a + 2; rl = n - a - 2; }+ else if (!(c = pt_attr(m, s + a, n - a, &r, &rl))) return RW_ESYNTAX;+ int nack = eq(r, rl, "nack"), pli = eq(r, rl, "nack pli"), fir = eq(r, rl, "ccm fir");+ for (int i = 0; i < m->npt; i++) {+ RwCodec *x = &m->pt[i];+ if (!all && x != c) continue;+ x->fb_nack |= nack;+ x->fb_pli |= pli;+ x->fb_fir |= fir;+ }+ } else if (prefix(s, n, "sctp-port:", &a)) {+ if (parse_uint(s + a, n - a, 65535, &v) || !v) return RW_ESYNTAX;+ m->sctp_port = (int)v;+ } else if (prefix(s, n, "max-message-size:", &a)) {+ if (parse_uint(s + a, n - a, 1ull << 40, &v)) return RW_ESYNTAX;+ m->max_msg = (long)v;+ }+ return RW_OK;+}++/* constrained baseline: profile_idc 66 with constraint_set1 */+static int h264_ok(const RwCodec *c) {+ return ieq(c->name, strlen(c->name), "H264") && c->clock == 90000 && c->pmode == 1 && (c->plid >> 16) == 0x42 && (c->plid & 0x4000);+}++int rw_sdp_parse_offer(const char *sdp, size_t n, int allow_h264, RwOffer *o) {+ memset(o, 0, sizeof *o);+ o->vm = o->am = o->dm = -1;+ if (n > RW_SDP_MAX) return RW_ETOOBIG;+ if (n < 4 || memcmp(sdp, "v=0", 3)) return RW_ESYNTAX;+ int cur = -1, lines = 0, bundle = 0;+ int mux[RW_MAX_MEDIA] = { 0 }, smux = 0;+ size_t i = 0;+ while (i < n) {+ size_t j = i;+ while (j < n && sdp[j] != '\n') j++;+ size_t e = j;+ if (e > i && sdp[e - 1] == '\r') e--;+ const char *s = sdp + i;+ size_t l = e - i;+ i = j < n ? j + 1 : j;+ if (++lines > RW_SDP_LINES || l > RW_SDP_LINE) return RW_ETOOBIG;+ if (l == 0 && i >= n) break; /* the final line break */+ if (l < 2 || s[1] != '=' || s[0] < 'a' || s[0] > 'z') return RW_ESYNTAX;+ for (size_t q = 0; q < l; q++) if (s[q] < 0x20 || s[q] > 0x7e) return RW_ESYNTAX;+ if (lines == 1 && !eq(s, l, "v=0")) return RW_ESYNTAX;+ if (s[0] == 'm') {+ if (o->nmedia >= RW_MAX_MEDIA) return RW_ETOOBIG;+ cur = o->nmedia++;+ int r = parse_mline(s + 2, l - 2, &o->m[cur]);+ if (r) return r;+ } else if (s[0] == 'a') {+ int r = parse_attr(o, cur >= 0 ? &o->m[cur] : NULL, s + 2, l - 2, &bundle, cur >= 0 ? &mux[cur] : &smux);+ if (r) return r;+ }+ }+ if (!o->ufrag[0] || !o->pwd[0]) return RW_EICE;+ static const uint8_t zero[32];+ if (!memcmp(o->fp, zero, 32)) return RW_EFP;+ if (!o->setup || !bundle || !o->nmedia) return RW_ESYNTAX;+ for (int k = 0; k < o->nmedia; k++) {+ RwMedia *m = &o->m[k];+ if (!m->mid[0]) return RW_ESYNTAX;+ for (int q = 0; q < k; q++) if (!strcmp(o->m[q].mid, m->mid)) return RW_ESYNTAX;+ if (m->rejected) continue;+ if (m->kind != RW_KIND_APP && !mux[k] && !smux) return RW_ESYNTAX;+ if (m->kind == RW_KIND_VIDEO) {+ if (o->vm >= 0) { m->rejected = 1; continue; }+ if (m->dir != RW_DIR_RECVONLY && m->dir != RW_DIR_SENDRECV) { m->rejected = 1; continue; }+ int best = -1, vp8 = -1;+ for (int q = 0; q < m->npt; q++) {+ const RwCodec *c = &m->pt[q];+ if (allow_h264 && h264_ok(c) && (best < 0 || c->plid == 0x42e01f)) best = q;+ if (vp8 < 0 && ieq(c->name, strlen(c->name), "VP8") && c->clock == 90000) vp8 = q;+ }+ if (best >= 0) { o->vcodec = RW_CODEC_H264; o->vpt = m->pt[best].pt; }+ else if (vp8 >= 0) { o->vcodec = RW_CODEC_VP8; o->vpt = m->pt[vp8].pt; }+ else { m->rejected = 1; continue; }+ o->vm = k;+ } else if (m->kind == RW_KIND_AUDIO) {+ if (o->am >= 0 || (m->dir != RW_DIR_RECVONLY && m->dir != RW_DIR_SENDRECV)) { m->rejected = 1; continue; }+ for (int q = 0; q < m->npt && o->am < 0; q++) {+ const RwCodec *c = &m->pt[q];+ if (ieq(c->name, strlen(c->name), "opus") && c->clock == 48000 && c->channels == 2) { o->am = k; o->apt = c->pt; }+ }+ if (o->am < 0) m->rejected = 1;+ } else if (m->kind == RW_KIND_APP) {+ if (o->dm >= 0 || !m->sctp_port) { m->rejected = 1; continue; }+ o->dm = k;+ }+ }+ if (o->vm < 0) return RW_ECODEC;+ return RW_OK;+}++/* ---- SDP answer ---- */++typedef struct { char *p; size_t n, cap; int bad; } Out;++static void outf(Out *o, const char *fmt, ...) __attribute__((format(printf, 2, 3)));+static void outf(Out *o, const char *fmt, ...) {+ if (o->bad) return;+ va_list ap;+ va_start(ap, fmt);+ int k = vsnprintf(o->p + o->n, o->cap - o->n, fmt, ap);+ va_end(ap);+ if (k < 0 || (size_t)k >= o->cap - o->n) { o->bad = 1; return; }+ o->n += (size_t)k;+}++static void out_fp(Out *o, const uint8_t *fp) {+ outf(o, "a=fingerprint:sha-256 ");+ for (int i = 0; i < 32; i++) outf(o, "%02X%s", fp[i], i < 31 ? ":" : "\r\n");+}++static const char *kind_name(int k) { return k == RW_KIND_AUDIO ? "audio" : k == RW_KIND_VIDEO ? "video" : "application"; }++int rw_sdp_answer(const RwOffer *of, const RwAnswer *a, char *buf, size_t cap) {+ Out o = { buf, 0, cap, 0 };+ if (!cap) return -1;+ buf[0] = 0;+ outf(&o, "v=0\r\no=- %u 2 IN IP4 127.0.0.1\r\ns=-\r\nt=0 0\r\na=ice-lite\r\na=group:BUNDLE", a->sess_id);+ for (int i = 0; i < of->nmedia; i++) if (!of->m[i].rejected) outf(&o, " %s", of->m[i].mid);+ outf(&o, "\r\na=msid-semantic: WMS lordos\r\n");+ int first = 1;+ for (int i = 0; i < of->nmedia; i++) {+ const RwMedia *m = &of->m[i];+ if (m->rejected) {+ if (m->kind == RW_KIND_APP) outf(&o, "m=application 0 UDP/DTLS/SCTP webrtc-datachannel\r\n");+ else if (m->kind) outf(&o, "m=%s 0 UDP/TLS/RTP/SAVPF %d\r\n", kind_name(m->kind), m->npt ? m->pt[0].pt : 0);+ else outf(&o, "m=audio 0 UDP/TLS/RTP/SAVPF 0\r\n");+ outf(&o, "c=IN IP4 0.0.0.0\r\na=mid:%s\r\na=inactive\r\n", m->mid);+ continue;+ }+ if (m->kind == RW_KIND_APP) outf(&o, "m=application 9 UDP/DTLS/SCTP webrtc-datachannel\r\n");+ else outf(&o, "m=%s 9 UDP/TLS/RTP/SAVPF %d\r\n", kind_name(m->kind), i == of->vm ? of->vpt : of->apt);+ outf(&o, "c=IN IP4 0.0.0.0\r\n");+ if (m->kind != RW_KIND_APP) outf(&o, "a=rtcp:9 IN IP4 0.0.0.0\r\n");+ outf(&o, "a=ice-ufrag:%s\r\na=ice-pwd:%s\r\n", a->ufrag, a->pwd);+ out_fp(&o, a->fp);+ outf(&o, "a=setup:passive\r\na=mid:%s\r\n", m->mid);+ if (first) {+ for (int c = 0; c < a->ncand; c++) {+ const RwLocalCand *lc = &a->cand[c];+ const char *typ = lc->type == RW_CAND_RELAY ? "relay" : lc->type == RW_CAND_SRFLX ? "srflx" : "host";+ outf(&o, "a=candidate:%d 1 udp %u %s %u typ %s", lc->type * 10 + c, lc->prio, lc->ip, lc->port, typ);+ if (lc->type != RW_CAND_HOST) outf(&o, " raddr %s rport %u", lc->raddr[0] ? lc->raddr : "0.0.0.0", lc->rport);+ outf(&o, "\r\n");+ }+ outf(&o, "a=end-of-candidates\r\n");+ first = 0;+ }+ if (m->kind == RW_KIND_APP) {+ outf(&o, "a=sctp-port:5000\r\na=max-message-size:262144\r\n");+ continue;+ }+ outf(&o, "a=rtcp-mux\r\n");+ if (i == of->vm) {+ const RwCodec *c = NULL;+ for (int q = 0; q < m->npt; q++) if (m->pt[q].pt == of->vpt) c = &m->pt[q];+ outf(&o, "a=sendonly\r\na=msid:lordos video\r\n");+ if (of->vcodec == RW_CODEC_H264) {+ outf(&o, "a=rtpmap:%d H264/90000\r\n", of->vpt);+ outf(&o, "a=fmtp:%d level-asymmetry-allowed=1;packetization-mode=1;profile-level-id=%06x\r\n", of->vpt, c ? c->plid : 0x42e01fu);+ } else outf(&o, "a=rtpmap:%d VP8/90000\r\n", of->vpt);+ if (c && c->fb_nack) outf(&o, "a=rtcp-fb:%d nack\r\n", of->vpt);+ if (c && c->fb_pli) outf(&o, "a=rtcp-fb:%d nack pli\r\n", of->vpt);+ if (c && c->fb_fir) outf(&o, "a=rtcp-fb:%d ccm fir\r\n", of->vpt);+ outf(&o, "a=ssrc:%u cname:lordos\r\na=ssrc:%u msid:lordos video\r\n", a->vssrc, a->vssrc);+ } else {+ outf(&o, "%s\r\na=msid:lordos audio\r\n", a->audio ? "a=sendonly" : "a=inactive");+ outf(&o, "a=rtpmap:%d opus/48000/2\r\na=fmtp:%d minptime=10;useinbandfec=1\r\n", of->apt, of->apt);+ outf(&o, "a=ssrc:%u cname:lordos\r\na=ssrc:%u msid:lordos audio\r\n", a->assrc, a->assrc);+ }+ }+ return o.bad ? -1 : (int)o.n;+}++/* ---- STUN ---- */++int rw_is_stun(const uint8_t *p, size_t n) { return n >= 20 && p[0] < 4 && be32(p + 4) == 0x2112A442u; }++uint32_t rw_crc32(const uint8_t *p, size_t n) {+ uint32_t c = 0xffffffffu;+ for (size_t i = 0; i < n; i++) {+ c ^= p[i];+ for (int k = 0; k < 8; k++) c = (c >> 1) ^ (0xEDB88320u & (0u - (c & 1)));+ }+ return c ^ 0xffffffffu;+}++static int xaddr(const uint8_t *v, size_t l, const uint8_t *hdr, RwAddr *a) {+ if (l < 4 || v[0] != 0) return -1;+ if (v[1] == 1 && l == 8) {+ a->family = 4;+ memset(a->a, 0, 16);+ for (int i = 0; i < 4; i++) a->a[i] = v[4 + i] ^ hdr[4 + i];+ } else if (v[1] == 2 && l == 20) {+ a->family = 6;+ for (int i = 0; i < 16; i++) a->a[i] = v[4 + i] ^ hdr[4 + i]; /* cookie + transaction id */+ } else return -1;+ a->port = (uint16_t)(be16(v + 2) ^ 0x2112);+ return 0;+}++int rw_stun_parse(const uint8_t *p, size_t n, RwStun *s) {+ memset(s, 0, sizeof *s);+ if (n < 20 || n > RW_STUN_MAX || (p[0] & 0xc0) || be32(p + 4) != 0x2112A442u) return -1;+ size_t len = be16(p + 2);+ if ((len & 3) || 20 + len != n) return -1;+ s->type = be16(p);+ memcpy(s->tid, p + 8, 12);+ size_t off = 20;+ uint32_t seen = 0;+ while (off < n) {+ if (n - off < 4) return -1;+ if (s->has_fp) return -1; /* nothing after FINGERPRINT */+ uint16_t t = be16(p + off), l = be16(p + off + 2);+ size_t padded = ((size_t)l + 3) & ~(size_t)3;+ if (padded > n - off - 4) return -1;+ const uint8_t *v = p + off + 4;+ if (s->mi_off && t != RW_ATTR_FINGERPRINT) return -1; /* only FINGERPRINT after MESSAGE-INTEGRITY */+ int bit = -1;+ switch (t) {+ case RW_ATTR_USERNAME: bit = 0; if (!l || l > 513) return -1; s->username = v; s->ulen = l; break;+ case RW_ATTR_MI: bit = 1; if (l != 20) return -1; s->mi_off = off; s->mi = v; break;+ case RW_ATTR_FINGERPRINT:+ bit = 2;+ if (l != 4 || be32(v) != (rw_crc32(p, off) ^ 0x5354554eu)) return -1;+ /* the length in the header must already include it: it does (20 + len == n) */+ s->has_fp = 1;+ break;+ case RW_ATTR_PRIORITY: bit = 3; if (l != 4) return -1; s->priority = be32(v); break;+ case RW_ATTR_USE_CAND: bit = 4; if (l != 0) return -1; s->use_candidate = 1; break;+ case RW_ATTR_CONTROLLING: bit = 5; if (l != 8) return -1; s->controlling = 1; break;+ case RW_ATTR_CONTROLLED: bit = 6; if (l != 8) return -1; s->controlled = 1; break;+ case RW_ATTR_XOR_MAPPED: bit = 7; if (xaddr(v, l, p, &s->xor_mapped)) return -1; break;+ case RW_ATTR_XOR_RELAYED: bit = 8; if (xaddr(v, l, p, &s->xor_relayed)) return -1; break;+ case RW_ATTR_XOR_PEER: bit = 9; if (xaddr(v, l, p, &s->xor_peer)) return -1; break;+ case RW_ATTR_ERROR: {+ bit = 10;+ if (l < 4 || l > 763 + 4 || v[0] || v[1]) return -1;+ int cls = v[2] & 7, num = v[3];+ if (cls < 3 || cls > 6 || num > 99) return -1;+ s->error = cls * 100 + num;+ break;+ }+ case RW_ATTR_REALM: bit = 11; if (!l || l > 763) return -1; s->realm = v; s->rlen = l; break;+ case RW_ATTR_NONCE: bit = 12; if (!l || l > 763) return -1; s->nonce = v; s->nlen = l; break;+ case RW_ATTR_LIFETIME: bit = 13; if (l != 4) return -1; s->lifetime = be32(v); break;+ case RW_ATTR_DATA: bit = 14; s->data = v; s->dlen = l; break;+ case RW_ATTR_MAPPED: case RW_ATTR_SOFTWARE: case RW_ATTR_TRANSPORT: case RW_ATTR_CHANNEL: break;+ default:+ if (t < 0x8000) return -1; /* unknown comprehension-required */+ break;+ }+ if (bit >= 0) { if (seen & (1u << bit)) return -1; seen |= 1u << bit; }+ off += 4 + padded;+ }+ return 0;+}++int rw_stun_check_mi(const uint8_t *p, size_t n, const RwStun *s, const uint8_t *key, size_t klen, RwHmac hmac) {+ if (!s->mi_off || s->mi_off + 24 > n || s->mi_off > RW_STUN_MAX) return 0;+ uint8_t buf[RW_STUN_MAX], mac[20];+ memcpy(buf, p, s->mi_off);+ put16(buf + 2, (uint16_t)(s->mi_off + 24 - 20)); /* the length as if MI were last */+ hmac(key, klen, buf, s->mi_off, mac);+ unsigned d = 0;+ for (int i = 0; i < 20; i++) d |= (unsigned)(mac[i] ^ s->mi[i]);+ return d == 0;+}++void rw_so_init(RwStunOut *o, uint16_t type, const uint8_t tid[12]) {+ memset(o, 0, sizeof *o);+ put16(o->b, type);+ put32(o->b + 4, 0x2112A442u);+ memcpy(o->b + 8, tid, 12);+ o->n = 20;+}++void rw_so_attr(RwStunOut *o, uint16_t type, const void *v, size_t len) {+ size_t padded = (len + 3) & ~(size_t)3;+ if (o->bad || len > 0xffff || o->n + 4 + padded > sizeof o->b) { o->bad = 1; return; }+ put16(o->b + o->n, type);+ put16(o->b + o->n + 2, (uint16_t)len);+ if (len) memcpy(o->b + o->n + 4, v, len);+ memset(o->b + o->n + 4 + len, 0, padded - len);+ o->n += 4 + padded;+ put16(o->b + 2, (uint16_t)(o->n - 20));+}++void rw_so_u32(RwStunOut *o, uint16_t type, uint32_t v) {+ uint8_t b[4];+ put32(b, v);+ rw_so_attr(o, type, b, 4);+}++void rw_so_xaddr(RwStunOut *o, uint16_t type, const RwAddr *a) {+ uint8_t v[20] = { 0 };+ v[1] = a->family == 6 ? 2 : 1;+ put16(v + 2, (uint16_t)(a->port ^ 0x2112));+ int k = a->family == 6 ? 16 : 4;+ for (int i = 0; i < k; i++) v[4 + i] = a->a[i] ^ o->b[4 + i];+ rw_so_attr(o, type, v, (size_t)(4 + k));+}++void rw_so_mi(RwStunOut *o, const uint8_t *key, size_t klen, RwHmac hmac) {+ if (o->bad || o->n + 24 > sizeof o->b) { o->bad = 1; return; }+ put16(o->b + 2, (uint16_t)(o->n + 24 - 20));+ uint8_t mac[20];+ hmac(key, klen, o->b, o->n, mac);+ rw_so_attr(o, RW_ATTR_MI, mac, 20);+}++void rw_so_fingerprint(RwStunOut *o) {+ if (o->bad || o->n + 8 > sizeof o->b) { o->bad = 1; return; }+ put16(o->b + 2, (uint16_t)(o->n + 8 - 20));+ uint32_t c = rw_crc32(o->b, o->n) ^ 0x5354554eu;+ rw_so_u32(o, RW_ATTR_FINGERPRINT, c);+}++/* ---- RTCP ---- */++#define FB_MAX 64++int rw_rtcp_parse(const uint8_t *p, size_t n, RwFbOn on, void *ud) {+ RwFb ev[FB_MAX];+ int nev = 0;+ size_t off = 0;+ if (n < 4 || n > 1500) return -1;+ while (off < n) {+ if (n - off < 4) return -1;+ const uint8_t *h = p + off;+ if ((h[0] >> 6) != 2) return -1;+ int padding = (h[0] >> 5) & 1, count = h[0] & 31, pt = h[1];+ size_t len = ((size_t)be16(h + 2) + 1) * 4;+ if (len > n - off || pt < 192 || pt > 223) return -1;+ size_t body = len;+ if (padding) {+ if (off + len != n) return -1; /* only the last part may pad */+ size_t pad = h[len - 1];+ if (!pad || pad > len - 4) return -1;+ body = len - pad;+ }+#define ADD(...) do { if (nev < FB_MAX) ev[nev++] = (RwFb){ __VA_ARGS__ }; } while (0)+ if (pt == 200 || pt == 201) { /* SR, RR: report blocks */+ size_t base = pt == 200 ? 28 : 8;+ if (body < base + (size_t)count * 24) return -1;+ for (int i = 0; i < count; i++) {+ const uint8_t *b = h + base + (size_t)i * 24;+ ADD(.kind = RW_FB_RR, .ssrc = be32(b), .lost_frac = b[4], .jitter = be32(b + 12), .lsr = be32(b + 16), .dlsr = be32(b + 20));+ }+ } else if (pt == 205) { /* transport feedback */+ if (body < 12) return -1;+ if (count == 1) { /* generic NACK */+ if ((body - 12) % 4 || body == 12) return -1;+ for (size_t q = 12; q < body; q += 4) ADD(.kind = RW_FB_NACK, .ssrc = be32(h + 8), .pid = be16(h + q), .blp = be16(h + q + 2));+ }+ } else if (pt == 206) { /* payload-specific feedback */+ if (body < 12) return -1;+ if (count == 1) ADD(.kind = RW_FB_PLI, .ssrc = be32(h + 8));+ else if (count == 4) { /* FIR: 8 bytes per entry */+ if ((body - 12) % 8 || body == 12) return -1;+ for (size_t q = 12; q < body; q += 8) ADD(.kind = RW_FB_FIR, .ssrc = be32(h + q));+ } else if (count == 15 && body >= 20 && !memcmp(h + 12, "REMB", 4)) {+ int ns = h[16];+ if (body < 20 + (size_t)ns * 4 || !ns) return -1;+ int exp = h[17] >> 2;+ uint64_t mant = (uint64_t)(h[17] & 3) << 16 | (uint64_t)h[18] << 8 | h[19];+ uint64_t br = exp > 46 ? UINT64_C(1) << 63 : mant << exp;+ for (int i = 0; i < ns; i++) ADD(.kind = RW_FB_REMB, .ssrc = be32(h + 20 + (size_t)i * 4), .bitrate = br);+ }+ } else if (pt == 202 || pt == 203 || pt == 204 || pt == 207) {+ /* SDES, BYE, APP, XR: nothing we use */+ } else return -1;+#undef ADD+ off += len;+ }+ for (int i = 0; i < nev; i++) on(ud, &ev[i]);+ return 0;+}++/* ---- data channel open ---- */++int rw_dcep_open(const uint8_t *p, size_t n, RwDcOpen *o) {+ memset(o, 0, sizeof *o);+ if (n < 12 || n > 12 + 64 || p[0] != 0x03) return -1;+ if (p[1] != 0x00 && p[1] != 0x80) return -1;+ size_t ll = be16(p + 8), pl = be16(p + 10);+ if (ll < 1 || ll > 64 || pl != 0 || 12 + ll != n) return -1;+ for (size_t i = 0; i < ll; i++) {+ uint8_t c = p[12 + i];+ if (!((c >= 'a' && c <= 'z') || (c >= '0' && c <= '9') || c == '_' || c == '-')) return -1;+ }+ o->chan_type = p[1];+ o->prio = be16(p + 2);+ o->rel = be32(p + 4);+ memcpy(o->label, p + 12, ll);+ o->label[ll] = 0;+ return 0;+}++/* ---- RTP payloads ---- */++/* Next NAL unit in an Annex B stream from *pos: start and length, or 0 at the end. */+static int next_nal(const uint8_t *b, size_t n, size_t *pos, const uint8_t **nal, size_t *len) {+ size_t i = *pos;+ while (i + 3 <= n && !(b[i] == 0 && b[i + 1] == 0 && b[i + 2] == 1)) i++;+ if (i + 3 > n) return 0;+ size_t s = i + 3, j = s;+ while (j + 3 <= n && !(b[j] == 0 && b[j + 1] == 0 && (b[j + 2] == 1 || (b[j + 2] == 0 && j + 3 < n && b[j + 3] == 1)))) j++;+ if (j + 3 > n) j = n;+ size_t e = j;+ while (e > s && b[e - 1] == 0 && e < n) e--; /* zero bytes before the next start code */+ *nal = b + s;+ *len = e - s;+ *pos = j;+ return 1;+}++int rw_h264_is_idr(const uint8_t *au, size_t n) {+ size_t pos = 0;+ const uint8_t *nal;+ size_t l;+ while (next_nal(au, n, &pos, &nal, &l)) if (l && (nal[0] & 0x1f) == 5) return 1;+ return 0;+}++int rw_h264_packetize(const uint8_t *au, size_t n, size_t mtu, RwPktOut out, void *ud) {+ if (mtu < 64) return -1;+ const uint8_t *nals[256];+ size_t lens[256];+ int k = 0;+ size_t pos = 0;+ const uint8_t *nal;+ size_t l;+ while (next_nal(au, n, &pos, &nal, &l)) {+ if (!l) continue;+ if (k >= 256 || (nal[0] & 0x80)) return -1; /* forbidden_zero_bit */+ nals[k] = nal;+ lens[k] = l;+ k++;+ }+ if (!k) return -1;+ int pk = 0;+ for (int i = 0; i < k; i++) {+ int last = i == k - 1;+ if (lens[i] <= mtu) { out(ud, NULL, 0, nals[i], lens[i], last); pk++; continue; }+ uint8_t fu[2] = { (uint8_t)((nals[i][0] & 0x60) | 28), (uint8_t)(nals[i][0] & 0x1f) };+ const uint8_t *d = nals[i] + 1;+ size_t rem = lens[i] - 1, chunk = mtu - 2;+ int start = 1;+ while (rem) {+ size_t c = rem > chunk ? chunk : rem;+ uint8_t h[2] = { fu[0], (uint8_t)(fu[1] | (start ? 0x80 : 0) | (c == rem ? 0x40 : 0)) };+ out(ud, h, 2, d, c, last && c == rem);+ pk++;+ d += c;+ rem -= c;+ start = 0;+ }+ }+ return pk;+}++int rw_vp8_packetize(const uint8_t *f, size_t n, size_t mtu, RwPktOut out, void *ud) {+ if (mtu < 64 || !n) return -1;+ size_t chunk = mtu - 1;+ int pk = 0;+ for (size_t off = 0; off < n; off += chunk) {+ size_t c = n - off > chunk ? chunk : n - off;+ uint8_t d = off == 0 ? 0x10 : 0x00;+ out(ud, &d, 1, f + off, c, off + c == n);+ pk++;+ }+ return pk;+}diff --git a/server/src/remote/rtcwire.h b/server/src/remote/rtcwire.hnew file mode 100644index 0000000..70a6b3c--- /dev/null+++ b/server/src/remote/rtcwire.h@@ -0,0 +1,211 @@+/* rtcwire.h — the WebRTC wire formats of the remote desktop's video path (docs/REMOTE.md, "Video+ * (WebRTC)"): the browser's SDP offer and our answer, ICE candidates, STUN and TURN messages, RTCP+ * feedback, the data channel's open message, and the RTP payload formats we send (H.264, VP8,+ * Opus). Plain libc, no I/O and no OpenSSL, like rsec.c: unit-tested and fuzzed under ASan+UBSan+ * (build/remote-sec-test, build/remote-fuzz).+ *+ * Every parser rejects instead of guessing. The browser's SDP is only accepted over the remote+ * desktop's own authenticated WebSocket; everything else here arrives on the bridge's UDP socket+ * and is parsed before any cryptography looks at it. */+#ifndef LORD_REMOTE_RTCWIRE_H+#define LORD_REMOTE_RTCWIRE_H++#include <stddef.h>+#include <stdint.h>++/* ---- addresses (IPv4 or IPv6, network order bytes) ---- */++typedef struct { int family; uint8_t a[16]; uint16_t port; } RwAddr; /* family 4 or 6, 0 = none */+int rw_addr_text(const char *s, RwAddr *a); /* dotted quad or RFC 4291 text; port untouched */+void rw_addr_format(const RwAddr *a, char *out, size_t n);+int rw_addr_equal(const RwAddr *x, const RwAddr *y); /* address and port */++/* Settings → Sharing's ICE servers (validated by the admin helper and again by lord-sessiond):+ * "host:port" with a DNS name ([A-Za-z0-9.-], 1..63), an IPv4 or an [IPv6], port 1..65535. */+int rw_ice_server_valid(const char *s);+/* A TURN user name: 1..128 printable ASCII, no space, quote or backslash. Password: 1..128 printable ASCII. */+int rw_turn_user_valid(const char *s);+int rw_turn_pass_valid(const char *s);++/* ---- SDP: the browser's offer ---- */++#define RW_SDP_MAX 32768 /* bytes; Chrome's offer with every codec is ~7 KB */+#define RW_SDP_LINES 600+#define RW_SDP_LINE 1024+#define RW_MAX_MEDIA 4 /* video, audio, data channel (+1 spare) */+#define RW_MAX_PT 48 /* payload types per m= line */+#define RW_MAX_CAND 16++enum { RW_KIND_AUDIO = 1, RW_KIND_VIDEO, RW_KIND_APP };+enum { RW_DIR_SENDRECV = 0, RW_DIR_RECVONLY, RW_DIR_SENDONLY, RW_DIR_INACTIVE };+enum { RW_SETUP_ACTPASS = 1, RW_SETUP_ACTIVE, RW_SETUP_PASSIVE };+enum { RW_CODEC_NONE = 0, RW_CODEC_H264 = 1, RW_CODEC_VP8 = 2 };+enum { RW_CAND_HOST = 1, RW_CAND_SRFLX, RW_CAND_PRFLX, RW_CAND_RELAY };++typedef struct {+ int pt;+ char name[16];+ int clock, channels;+ int fb_nack, fb_pli, fb_fir;+ int pmode; /* H.264 packetization-mode */+ uint32_t plid; /* H.264 profile-level-id (0 if absent) */+ int has_fmtp;+} RwCodec;++typedef struct {+ int kind, rejected, dir;+ char mid[33];+ int npt;+ RwCodec pt[RW_MAX_PT];+ int sctp_port;+ long max_msg;+} RwMedia;++typedef struct {+ int mdns; /* the address is an mDNS name (unusable here) */+ int tcp; /* a TCP candidate (ignored) */+ int component, type;+ uint32_t prio;+ RwAddr addr;+} RwCand;++typedef struct {+ char ufrag[257], pwd[257];+ uint8_t fp[32]; /* a=fingerprint:sha-256 (the browser's DTLS certificate) */+ int setup;+ int nmedia;+ RwMedia m[RW_MAX_MEDIA];+ int ncand;+ RwCand cand[RW_MAX_CAND];+ /* chosen by rw_sdp_parse_offer: index of each section (-1 none), payload type, codec */+ int vm, vpt, vcodec, am, apt, dm;+} RwOffer;++/* Error codes (< 0) of the SDP and candidate parsers. */+enum { RW_OK = 0, RW_ESYNTAX = -1, RW_ETOOBIG = -2, RW_EICE = -3, RW_EFP = -4, RW_EMEDIA = -5, RW_ECODEC = -6 };+/* Strict: CRLF (or LF) lines of printable ASCII, "x=" types in order, at most RW_SDP_LINES lines of+ * RW_SDP_LINE bytes; one ufrag, pwd and sha-256 fingerprint for the whole bundle (session level or the+ * same in every section); setup actpass or active; every section bundled with rtcp-mux; at most one+ * video, one audio and one data channel section. Unknown attributes are skipped (browsers send many),+ * the ones we use are checked. Then it picks the codecs: H.264 constrained baseline with+ * packetization-mode 1 if offered and allow_h264, else VP8; Opus 48 kHz stereo. */+int rw_sdp_parse_offer(const char *sdp, size_t n, int allow_h264, RwOffer *o);++/* "candidate:..." (with or without the "a=" prefix), RFC 8839 grammar, at most 512 bytes. */+int rw_cand_parse(const char *s, size_t n, RwCand *c);++/* ---- SDP: our answer ---- */++typedef struct {+ char ip[64];+ uint16_t port;+ int type; /* RW_CAND_HOST, SRFLX or RELAY */+ uint32_t prio;+ char raddr[64];+ uint16_t rport;+} RwLocalCand;++typedef struct {+ const char *ufrag, *pwd; /* ours, [A-Za-z0-9+/] */+ const uint8_t *fp; /* our certificate's SHA-256 */+ uint32_t vssrc, assrc;+ int audio; /* send audio (else the audio section is answered inactive) */+ const RwLocalCand *cand;+ int ncand;+ uint32_t sess_id;+} RwAnswer;+/* ICE-lite, setup:passive, each offered section answered in order (unusable ones rejected with port 0).+ * Returns the length or -1 if it doesn't fit. */+int rw_sdp_answer(const RwOffer *o, const RwAnswer *a, char *out, size_t cap);++/* ---- STUN (RFC 8489) and TURN (RFC 8656) ---- */++#define RW_STUN_MAX 1500+enum {+ RW_STUN_BINDING_REQ = 0x0001, RW_STUN_BINDING_OK = 0x0101, RW_STUN_BINDING_ERR = 0x0111,+ RW_STUN_BINDING_IND = 0x0011,+ RW_TURN_ALLOCATE_REQ = 0x0003, RW_TURN_ALLOCATE_OK = 0x0103, RW_TURN_ALLOCATE_ERR = 0x0113,+ RW_TURN_REFRESH_REQ = 0x0004, RW_TURN_REFRESH_OK = 0x0104, RW_TURN_REFRESH_ERR = 0x0114,+ RW_TURN_PERM_REQ = 0x0008, RW_TURN_PERM_OK = 0x0108, RW_TURN_PERM_ERR = 0x0118,+ RW_TURN_SEND_IND = 0x0016, RW_TURN_DATA_IND = 0x0017,+};+typedef struct {+ uint16_t type;+ uint8_t tid[12];+ const uint8_t *username; /* inside the packet */+ size_t ulen;+ size_t mi_off; /* offset of MESSAGE-INTEGRITY (0 = none) */+ const uint8_t *mi; /* its 20 bytes */+ int has_fp; /* FINGERPRINT present (and right: rw_stun_parse checks it) */+ int use_candidate, controlling, controlled;+ uint32_t priority;+ RwAddr xor_mapped, xor_relayed, xor_peer;+ int error; /* ERROR-CODE class*100+number, 0 none */+ const uint8_t *realm, *nonce, *data;+ size_t rlen, nlen, dlen;+ uint32_t lifetime;+} RwStun;++/* First byte demultiplexing (RFC 7983): 0..3 STUN, 20..63 DTLS, 128..191 RTP/RTCP. */+int rw_is_stun(const uint8_t *p, size_t n);+/* Strict: header (magic cookie, length a multiple of 4 and exact), attributes with their padding,+ * known ones with exact sizes, nothing after FINGERPRINT, only FINGERPRINT after MESSAGE-INTEGRITY,+ * at most one of each. 0 or -1. */+int rw_stun_parse(const uint8_t *p, size_t n, RwStun *s);+uint32_t rw_crc32(const uint8_t *p, size_t n);++/* HMAC-SHA1 (MESSAGE-INTEGRITY) is provided by the caller (OpenSSL in rtc.c). */+typedef void (*RwHmac)(const uint8_t *key, size_t klen, const uint8_t *msg, size_t n, uint8_t out[20]);+/* Does the message's MESSAGE-INTEGRITY match this key? (constant-time compare) */+int rw_stun_check_mi(const uint8_t *p, size_t n, const RwStun *s, const uint8_t *key, size_t klen, RwHmac hmac);++typedef struct { uint8_t b[RW_STUN_MAX]; size_t n; int bad; } RwStunOut;+void rw_so_init(RwStunOut *o, uint16_t type, const uint8_t tid[12]);+void rw_so_attr(RwStunOut *o, uint16_t type, const void *v, size_t len);+void rw_so_u32(RwStunOut *o, uint16_t type, uint32_t v);+void rw_so_xaddr(RwStunOut *o, uint16_t type, const RwAddr *a);+void rw_so_mi(RwStunOut *o, const uint8_t *key, size_t klen, RwHmac hmac);+void rw_so_fingerprint(RwStunOut *o);+enum { RW_ATTR_MAPPED = 0x0001, RW_ATTR_USERNAME = 0x0006, RW_ATTR_MI = 0x0008, RW_ATTR_ERROR = 0x0009,+ RW_ATTR_CHANNEL = 0x000c, RW_ATTR_LIFETIME = 0x000d, RW_ATTR_XOR_PEER = 0x0012, RW_ATTR_DATA = 0x0013,+ RW_ATTR_REALM = 0x0014, RW_ATTR_NONCE = 0x0015, RW_ATTR_XOR_RELAYED = 0x0016, RW_ATTR_TRANSPORT = 0x0019,+ RW_ATTR_XOR_MAPPED = 0x0020, RW_ATTR_PRIORITY = 0x0024, RW_ATTR_USE_CAND = 0x0025,+ RW_ATTR_SOFTWARE = 0x8022, RW_ATTR_FINGERPRINT = 0x8028, RW_ATTR_CONTROLLED = 0x8029, RW_ATTR_CONTROLLING = 0x802a };++/* ---- RTCP from the browser (RFC 3550, 4585, 5104; REMB) ---- */++enum { RW_FB_RR = 1, RW_FB_PLI, RW_FB_FIR, RW_FB_NACK, RW_FB_REMB };+typedef struct {+ int kind;+ uint32_t ssrc; /* the media source the item is about */+ int lost_frac; /* RR: fraction lost ×256 */+ uint32_t jitter, lsr, dlsr; /* RR */+ uint16_t pid, blp; /* NACK */+ uint64_t bitrate; /* REMB, bits/s */+} RwFb;+typedef void (*RwFbOn)(void *ud, const RwFb *f);+/* A compound packet (already decrypted): every part checked (version, lengths, padding); calls on()+ * for each report block and feedback item. 0 or -1 (nothing is reported for a malformed packet). */+int rw_rtcp_parse(const uint8_t *p, size_t n, RwFbOn on, void *ud);++/* ---- data channel establishment (RFC 8832) ---- */++typedef struct { int chan_type; uint16_t prio; uint32_t rel; char label[65]; } RwDcOpen;+/* DATA_CHANNEL_OPEN: reliable (0x00) or reliable unordered (0x80) only, a label of 1..64 bytes of+ * [a-z0-9_-], no subprotocol. 0 or -1. */+int rw_dcep_open(const uint8_t *p, size_t n, RwDcOpen *o);++/* ---- RTP payloads we send ---- */++/* Each RTP payload: a payload header (hl bytes, may be 0) then n bytes of data; marker on the last+ * packet of a frame. */+typedef void (*RwPktOut)(void *ud, const uint8_t *hdr, size_t hl, const uint8_t *data, size_t n, int marker);+/* An H.264 access unit in Annex B: single NAL unit packets or FU-A fragments (RFC 6184, mode 1).+ * Returns the number of packets or -1 (no NAL unit, more than 256, or mtu too small). */+int rw_h264_packetize(const uint8_t *au, size_t n, size_t mtu, RwPktOut out, void *ud);+/* A VP8 frame (RFC 7741): the 1-byte descriptor, S on the first packet. */+int rw_vp8_packetize(const uint8_t *f, size_t n, size_t mtu, RwPktOut out, void *ud);+/* Is an H.264 access unit an IDR (has a NAL unit of type 5)? */+int rw_h264_is_idr(const uint8_t *au, size_t n);++#endifdiff --git a/server/src/remote/sessiond.c b/server/src/remote/sessiond.cindex 50a30a0..fc224fa 100644--- a/server/src/remote/sessiond.c+++ b/server/src/remote/sessiond.c@@ -26,6 +26,7 @@* SIGHUP rereads the configuration (turns remote on or off without touching the sessions).* It's the only LordOS program that links OpenSSL (TLS): lord-server doesn't. */#include "sessiond.h"+#include "rtcwire.h"#include "../sec/cgroup.h"#include "proto.h"#include "rsec.h"@@ -132,11 +133,21 @@ static void load_config(void) {rd_conf.port = RD_DEFAULT_PORT;rd_conf.enabled = rd_conf.group_only = rd_conf.insecure_dev = 0;rd_conf.net_mode = RS_NET_LAN;+ rd_conf.video = 1;+ rd_conf.nstun = 0;+ rd_conf.turn[0] = rd_conf.turn_user[0] = 0;FILE *f = fopen(RD_CONF_DIR "/config", "r");char line[256];while (f && fgets(line, sizeof line, f)) {int v;- char s[32];+ char s[32], h[160];+ if (sscanf(line, "video=%d", &v) == 1) { rd_conf.video = v != 0; continue; }+ if (sscanf(line, "stun=%159s", h) == 1) {+ if (rw_ice_server_valid(h) && rd_conf.nstun < 4) snprintf(rd_conf.stun[rd_conf.nstun++], 96, "%.95s", h);+ continue;+ }+ if (sscanf(line, "turn=%159s", h) == 1) { if (rw_ice_server_valid(h)) snprintf(rd_conf.turn, sizeof rd_conf.turn, "%.95s", h); continue; }+ if (sscanf(line, "turn_user=%159s", h) == 1) { if (rw_turn_user_valid(h)) snprintf(rd_conf.turn_user, sizeof rd_conf.turn_user, "%.128s", h); continue; }if (sscanf(line, "enabled=%d", &v) == 1) rd_conf.enabled = v == 1;else if (sscanf(line, "port=%d", &v) == 1 && v > 0 && v < 65536) rd_conf.port = v;else if (sscanf(line, "allow=%31s", s) == 1) rd_conf.group_only = !strcmp(s, "group");@@ -1214,7 +1225,8 @@ int main(int argc, char **argv) {int was = rd_conf.enabled, port = rd_conf.port;load_config();unblock_requests();- sd_log("configuration: remote %s, port %d, from %s", rd_conf.enabled ? "on" : "off", rd_conf.port, rs_net_name(rd_conf.net_mode));+ sd_log("configuration: remote %s, port %d, from %s; video %s, %d STUN server(s), TURN %s", rd_conf.enabled ? "on" : "off", rd_conf.port,+ rs_net_name(rd_conf.net_mode), rd_conf.video ? "on" : "off", rd_conf.nstun, rd_conf.turn[0] ? "configured" : "none");if (listening && (!rd_conf.enabled || port != rd_conf.port)) stop_remote();if (rd_conf.enabled && (!listening || !was)) start_remote();bump();diff --git a/server/src/remote/sessiond.h b/server/src/remote/sessiond.hindex f662d40..4b14bec 100644--- a/server/src/remote/sessiond.h+++ b/server/src/remote/sessiond.h@@ -32,6 +32,7 @@#define RD_MAX_PER_SOURCE 8 /* of those, from one address (IPv6: one /64) */#define RD_ACCESS RD_CONF_DIR "/access" /* manual allow/block lists (admin actions, root 0600) */#define RD_COOKIE "__Host-lordos_remote"+#define RD_TURN_KEY RD_CONF_DIR "/turn.key" /* the TURN password (admin action remote-ice, root 0600) */#define SD_MAX_SESSIONS 32/* State shared between the main process and its children. */@@ -72,6 +73,10 @@ typedef struct {int insecure_dev; /* allows binaries not owned by root (development VMs only) */int net_mode; /* RS_NET_LAN (default) | RS_NET_LIST | RS_NET_ANY: who may connect */RsAccess access; /* RD_ACCESS */+ /* the video path (docs/REMOTE.md, "Video (WebRTC)"): video=1 (the default), up to 4 stun=host:port,+ * turn=host:port and turn_user=…; the TURN password is in RD_TURN_KEY (root 0600) */+ int video, nstun;+ char stun[4][96], turn[96], turn_user[129];char server[512], theme[512], greeter_theme[512], fonts[512], shell[512], workdir[512];} RdConfig;