Something went wrong. Try again.
QUIC for Zig 0.17: RFC 9000-9002 with TLS 1.3, as a sans-I/O core plus a socket layer, with datagrams, multipath and raw public keys.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202// SPDX-FileCopyrightText: © 2026 Jeffrey C. Ollie <jeff@ocjtech.us>// SPDX-License-Identifier: MIT
const std = @import("std");
pub fn build(b: *std.Build) void { const target = b.standardTargetOptions(.{}); const optimize = b.standardOptimizeOption(.{});
const tls = b.dependency("tls", .{});
const mod = b.addModule("quic", .{ .root_source_file = b.path("src/root.zig"), .target = target, .imports = &.{.{ .name = "tls", .module = tls.module("tls") }}, });
// QUIC with the I/O put back: a UDP socket, a connection table and the // loop between them. A separate module so that `quic` keeps its property // -- nothing in the sans-I/O core imports anything from here, and a // caller who wants the state machine alone never compiles a socket. const io_mod = b.addModule("quic_io", .{ .root_source_file = b.path("src/io/root.zig"), .target = target, // By name, not by path. A module that reached across into // `../root.zig` would compile the core a second time and would let // the dependency run the other way by accident; naming it is what // makes "nothing in `quic` imports anything from here" a fact the // build system enforces rather than a habit. .imports = &.{.{ .name = "quic", .module = mod }}, });
const test_filter = b.option( []const u8, "test-filter", "Run only the tests whose name contains this", ); const filters: []const []const u8 = if (test_filter) |f| &.{f} else &.{};
// The loop over a real socket, which the sans-I/O suite cannot reach: // everything there hands datagrams from one connection to the other by // hand, and so proves the protocol while saying nothing about whether a // task blocked on a stream is ever woken. const endpoint_mod = b.createModule(.{ .root_source_file = b.path("tests/endpoint.zig"), .target = target, .imports = &.{ .{ .name = "quic", .module = mod }, .{ .name = "quic_io", .module = io_mod }, }, });
const test_step = b.step("test", "Run tests"); test_step.dependOn(&b.addRunArtifact(b.addTest(.{ .root_module = mod, .filters = filters, })).step); test_step.dependOn(&b.addRunArtifact(b.addTest(.{ .root_module = io_mod, .filters = filters, })).step); test_step.dependOn(&b.addRunArtifact(b.addTest(.{ .root_module = endpoint_mod, .filters = filters, })).step);
// The pieces working together: a TLS handshake carried in real QUIC // packets, which is the question left open while the seam had nothing // behind it. const handshake_mod = b.createModule(.{ .root_source_file = b.path("tests/handshake.zig"), .target = target, .optimize = optimize, .imports = &.{.{ .name = "quic", .module = mod }}, }); test_step.dependOn(&b.addRunArtifact(b.addTest(.{ .root_module = handshake_mod, .filters = filters, })).step);
// The fuzz targets, which are ordinary tests as well: without `--fuzz` // they run the seeds checked in beside them. const fuzz_mod = b.createModule(.{ .root_source_file = b.path("tests/fuzz.zig"), .target = target, .optimize = optimize, .imports = &.{.{ .name = "quic", .module = mod }}, }); const fuzz_filter = b.option( []const u8, "fuzz-filter", "Fuzz or test only the targets whose name contains this", ); const fuzz_tests = b.addTest(.{ .root_module = fuzz_mod, .filters = if (fuzz_filter) |f| &.{f} else &.{}, // The self-hosted backend, which Debug uses by default, emits no // coverage instrumentation, and `--fuzz` without it has nothing to // steer by: it ends with "pcs_len was zero" rather than a report. // `build.zig` cannot see whether `--fuzz` was passed, so this is // always on, at the cost of a few seconds of Debug compile. .use_llvm = true, }); test_step.dependOn(&b.addRunArtifact(fuzz_tests).step);
const fuzz_step = b.step("fuzz", "The fuzz targets: add --fuzz to fuzz them"); fuzz_step.dependOn(&b.addRunArtifact(fuzz_tests).step);
// The loop that drives those targets without Zig's fuzzer: bounded by a // count rather than a clock, so that CI runs the same inputs everywhere, // and seeded from a corpus of real packets rather than from coverage. const fuzz_run = b.addExecutable(.{ .name = "zig-quic-fuzz", .root_module = b.createModule(.{ .root_source_file = b.path("tools/fuzz.zig"), .target = b.graph.host, .optimize = .ReleaseSafe, .imports = &.{.{ .name = "fuzz_targets", .module = fuzz_mod }}, }), }); const run_fuzz = b.addRunArtifact(fuzz_run); run_fuzz.stdio = .inherit; run_fuzz.addPassthruArgs(); const fuzz_run_step = b.step("fuzz-run", "Fuzz the targets with a loop of our own"); fuzz_run_step.dependOn(&run_fuzz.step);
// A DNS-over-QUIC client, which is what the interoperability test points // at somebody else's server. Everything in `test` is this library talking // to itself; this is the part that can be wrong in a way only another // implementation would notice. const doq = b.addExecutable(.{ .name = "doq", .root_module = b.createModule(.{ .root_source_file = b.path("src/main.zig"), .target = target, .optimize = optimize, .imports = &.{.{ .name = "tls", .module = tls.module("tls") }}, }), }); b.installArtifact(doq);
const run_doq = b.addRunArtifact(doq); run_doq.stdio = .inherit; run_doq.addPassthruArgs(); const run_step = b.step("run", "Ask a DNS-over-QUIC server for a name"); run_step.dependOn(&run_doq.step);
test_step.dependOn(&b.addRunArtifact(b.addTest(.{ .root_module = doq.root_module, .filters = filters, })).step);
// And the server side of the same demonstration. A server is the endpoint // that answers strangers: it derives Initial keys from a connection ID // somebody else chose, obeys the amplification limit before it knows who // it is talking to, and sends a certificate chain — which no test in this // project did until this existed. const doqd = b.addExecutable(.{ .name = "doqd", .root_module = b.createModule(.{ .root_source_file = b.path("src/server.zig"), .target = target, .optimize = optimize, // Both modules, and the split is the demonstration: `quic` for // the protocol and `quic_io` for the loop, which is exactly the // division a caller of this library gets to make. .imports = &.{ .{ .name = "quic", .module = mod }, .{ .name = "quic_io", .module = io_mod }, }, }), }); b.installArtifact(doqd);
test_step.dependOn(&b.addRunArtifact(b.addTest(.{ .root_module = doqd.root_module, .filters = filters, })).step);
const check_step = b.step("check", "Compile everything without running it"); check_step.dependOn(&doqd.step); check_step.dependOn(&fuzz_run.step); check_step.dependOn(&doq.step);
// -- documentation ------------------------------------------------------- // // There is no `docs-serve` here, and its absence is deliberate: the little // server the other projects use is built on zig-http, and zig-http will // one day depend on this for HTTP/3. A development convenience is not // worth a dependency cycle. `python3 -m http.server` in `zig-out/docs` // does the same job.
const library = b.addLibrary(.{ .name = "quic", .root_module = mod }); const install_docs = b.addInstallDirectory(.{ .source_dir = library.getEmittedDocs(), .install_dir = .prefix, .install_subdir = "docs", }); const docs_step = b.step("docs", "Build the API documentation into zig-out/docs"); docs_step.dependOn(&install_docs.step);}