// SPDX-FileCopyrightText: 2026 Progmasoft // SPDX-License-Identifier: MIT using System; using System.Buffers.Binary; using System.Collections.Generic; using System.IO; using System.Text; using System.Threading; using System.Threading.Tasks; namespace Progmasoft.Tazel.MicroVM; /// The kinds of message between Tazel and the agent of its MicroVM. internal enum MessageKind : byte { /// Host to guest: run a command; . Run = 1, /// Host to guest: stop the command of this connection. Stop = 2, /// Host to guest: write everything out and power off. PowerOff = 3, /// Host to guest: are you there? The agent answers . Hello = 4, /// Guest to host: the agent is up; the payload is its version. Ready = 10, /// Guest to host: output of the command; the first byte is 1 for stdout or 2 for stderr. Output = 11, /// Guest to host: the command ended; the payload is its exit status, a signal as 128 + its number. Exit = 12, /// Guest to host: the command could not run; the payload is why. Error = 13, } /// A command for the guest: its arguments, its whole environment and where it runs. /// The program and its arguments; the program is found on the PATH of . /// Every variable the command gets, and nothing else. /// The working directory, inside a shared directory. internal sealed record RunRequest(IReadOnlyList Arguments, IReadOnlyDictionary Environment, string Directory); /// /// The messages of a MicroVM connection: a kind byte, a little-endian 32-bit length, and the payload. Strings are a /// length and UTF-8. It is the same file in Tazel and in the agent; no JSON library is needed on either side. /// internal static class MicroVMProtocol { /// The vsock port the agent listens on. public const uint Port = 1024; /// The PATH of commands inside, and the one the agent searches when a command brings none. public const string DefaultPath = "/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"; /// A path as tazel.share= writes it on the kernel command line: base64url without padding. public static string EncodePath(string path) => Convert.ToBase64String(Encoding.UTF8.GetBytes(path)).TrimEnd('=').Replace('+', '-').Replace('/', '_'); /// Reads a path wrote. public static string DecodePath(string text) { ArgumentNullException.ThrowIfNull(text); var padded = text.Replace('-', '+').Replace('_', '/'); return Encoding.UTF8.GetString(Convert.FromBase64String(padded + new string('=', (4 - (padded.Length % 4)) % 4))); } /// The most a payload may hold; a larger length means the stream is broken. public const int MaxPayload = 16 * 1024 * 1024; /// Writes one message. public static async Task WriteAsync(Stream stream, MessageKind kind, ReadOnlyMemory payload, CancellationToken cancel) { ArgumentNullException.ThrowIfNull(stream); var header = new byte[5]; header[0] = (byte)kind; BinaryPrimitives.WriteInt32LittleEndian(header.AsSpan(1), payload.Length); await stream.WriteAsync(header, cancel).ConfigureAwait(false); if (payload.Length > 0) { await stream.WriteAsync(payload, cancel).ConfigureAwait(false); } await stream.FlushAsync(cancel).ConfigureAwait(false); } /// Reads one message, or null at the end of the stream. public static async Task<(MessageKind Kind, byte[] Payload)?> ReadAsync(Stream stream, CancellationToken cancel) { ArgumentNullException.ThrowIfNull(stream); var header = new byte[5]; if (!await ReadExactlyAsync(stream, header, cancel).ConfigureAwait(false)) { return null; } var length = BinaryPrimitives.ReadInt32LittleEndian(header.AsSpan(1)); if (length < 0 || length > MaxPayload) { throw new InvalidDataException($"a message says it holds {length} bytes"); } var payload = new byte[length]; if (!await ReadExactlyAsync(stream, payload, cancel).ConfigureAwait(false)) { throw new EndOfStreamException("the connection ended inside a message"); } return ((MessageKind)header[0], payload); } /// The payload of . public static byte[] Encode(RunRequest request) { ArgumentNullException.ThrowIfNull(request); using var buffer = new MemoryStream(); WriteInt(buffer, request.Arguments.Count); foreach (var argument in request.Arguments) { WriteString(buffer, argument); } WriteInt(buffer, request.Environment.Count); foreach (var (name, value) in request.Environment) { WriteString(buffer, name); WriteString(buffer, value); } WriteString(buffer, request.Directory); return buffer.ToArray(); } /// Reads the payload of . public static RunRequest DecodeRun(byte[] payload) { ArgumentNullException.ThrowIfNull(payload); var position = 0; var count = ReadCount(payload, ref position); var arguments = new List(count); for (var i = 0; i < count; i++) { arguments.Add(ReadString(payload, ref position)); } var variables = ReadCount(payload, ref position); var environment = new Dictionary(StringComparer.Ordinal); for (var i = 0; i < variables; i++) { var name = ReadString(payload, ref position); environment[name] = ReadString(payload, ref position); } var directory = ReadString(payload, ref position); if (position != payload.Length) { throw new InvalidDataException("a run message has bytes after its directory"); } return new RunRequest(arguments, environment, directory); } /// The payload of . public static byte[] EncodeStatus(int status) { var payload = new byte[4]; BinaryPrimitives.WriteInt32LittleEndian(payload, status); return payload; } /// Reads the payload of . public static int DecodeStatus(byte[] payload) => payload is { Length: 4 } ? BinaryPrimitives.ReadInt32LittleEndian(payload) : throw new InvalidDataException("an exit message is not 4 bytes"); private static void WriteInt(Stream stream, int value) { Span bytes = stackalloc byte[4]; BinaryPrimitives.WriteInt32LittleEndian(bytes, value); stream.Write(bytes); } private static void WriteString(Stream stream, string value) { var bytes = Encoding.UTF8.GetBytes(value); WriteInt(stream, bytes.Length); stream.Write(bytes); } private static int ReadCount(byte[] payload, ref int position) { if (payload.Length - position < 4) { throw new InvalidDataException("a message ends inside a number"); } var value = BinaryPrimitives.ReadInt32LittleEndian(payload.AsSpan(position)); position += 4; return value < 0 || value > payload.Length ? throw new InvalidDataException($"a message holds the count {value}") : value; } private static string ReadString(byte[] payload, ref int position) { var length = ReadCount(payload, ref position); if (payload.Length - position < length) { throw new InvalidDataException("a message ends inside a string"); } var value = Encoding.UTF8.GetString(payload, position, length); position += length; return value; } private static async Task ReadExactlyAsync(Stream stream, byte[] buffer, CancellationToken cancel) { var read = 0; while (read < buffer.Length) { var count = await stream.ReadAsync(buffer.AsMemory(read), cancel).ConfigureAwait(false); if (count == 0) { return read == 0 ? false : throw new EndOfStreamException("the connection ended inside a message"); } read += count; } return true; } }