Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 16 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ This is the M4 implementation informed by the adjacent

The first goal is a working ownership and pending/resume model that we can
measure and change. This is not a production qualification or a TechEmpower
ranking. It listens on **IPv4 loopback only**, speaks plain HTTP/1.1, and has no
ranking. It defaults to **IPv4 loopback**, accepts explicit IPv4 binding, speaks plain HTTP/1.1, and has no
TLS, proxy protocol, upgrade/tunnel implementation or general file server.

Run these commands from the repository root with the exact `zig` compiler on
Expand All @@ -63,12 +63,26 @@ zig build -Doptimize=ReleaseSafe
On Windows, use `.\zig-out\bin\bounded-http.exe` from PowerShell.
Windows defaults to one I/O shard; `--shards 3` enables three independent IOCP owners.
One listener distributes socket ownership through startup-bounded queues.
Choose an IPv4 listener address explicitly for LAN clients:

```sh
./zig-out/bin/bounded-http --bind-address 0.0.0.0 --port 8080 --connections 128
```

`0.0.0.0` binds all IPv4 interfaces. A specific local IPv4 address binds only that address.
Clients connect to the host's actual address, not `0.0.0.0`.
The CLI accepts four decimal octets, with no hostname lookup or IPv6 parsing.
Embedded applications set `Config.bind_address = .{ 0, 0, 0, 0 }`.
`Config.parseBindAddress(text)` provides the same allocation-free startup parser.
The default remains `127.0.0.1`. Binding does not change connection limits, resource budgets, or backpressure.
Linux shard listeners and the Windows acceptor receive the same configured address.

See the [architecture guide](docs/ARCHITECTURE.md#windows-socket-handoff) and [native shard receipt](reports/2026-09-06-windows-shards.md).

The version must print `0.16.0`. The server prints `READY` to stderr after
startup. Linux and macOS accept SIGINT or SIGTERM; Windows consoles accept Ctrl-C or Ctrl-Break.
`--duration-ms 30000` requests
shutdown after a finite run. `--port 0` asks the OS for an available loopback
shutdown after a finite run. `--port 0` asks the OS for an available
port, reported in `READY`. The HTML file is loaded before serving starts.

| Route | Behavior |
Expand Down
4 changes: 3 additions & 1 deletion docs/ARCHITECTURE.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ macOS uses nonblocking sockets and `kqueue`.
Windows uses overlapped sockets and an I/O completion port (IOCP), which delivers operation results to the owner.

The server remains experimental.
The listener accepts IPv4 loopback connections only.
The listener defaults to IPv4 loopback.
`Config.bind_address` selects another local IPv4 address or wildcard `0.0.0.0`.
The address is fixed at startup and does not change admission or storage bounds.
The implementation has no Transport Layer Security (TLS), protocol upgrade, or tunnel support.
See [using the server](USING.md) for embedding instructions.

Expand Down
4 changes: 2 additions & 2 deletions docs/INTERFACES.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@ Linux uses actual low-level io_uring accept/recv/send, runtime opcode probes,
finite queues and explicit cancel drain. macOS uses nonblocking sockets/kqueue
and the same completion interface.
Windows uses overlapped `AcceptEx`, `WSARecv`, and `WSASend` through one completion port per owner.
IPv4 loopback binding initially; CLI can
expose other bind addresses later. No per-operation allocation.
The listener defaults to IPv4 loopback. `Config.bind_address` selects another local IPv4 address or all IPv4 interfaces.
The CLI exposes `--bind-address`. No operation allocates memory after startup.

Gather vectors live in the caller's per-connection startup storage.
Linux points its `msghdr` at those vectors.
Expand Down
12 changes: 11 additions & 1 deletion docs/USING.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ Use exact Zig 0.16.0, as specified by [.zig-version](../.zig-version).
The framework has Linux, macOS, and Windows transport adapters.
The [Windows receipt](../reports/2026-09-06-windows-iocp.md) records native verification and its exclusions.

The current listener accepts plain HTTP/1.1 on IPv4 loopback only.
The listener accepts plain HTTP/1.1 and defaults to IPv4 loopback.
Use `Config.bind_address` to select a local IPv4 address or all IPv4 interfaces.
The framework has no Transport Layer Security (TLS), general file server, protocol upgrade, or tunnel implementation.
Treat the current API as experimental.

Expand Down Expand Up @@ -504,3 +505,12 @@ A client can limit measured throughput.
Closed-loop request measurements do not establish an open-loop latency guarantee.
Use [recorded reports](../reports) for existing experiments and their exact scope.
Use the [roadmap](../ROADMAP.md) to identify remaining qualification work.

## Listener address

`Config.bind_address` contains four IPv4 octets. The default is `.{ 127, 0, 0, 1 }`.
Use `.{ 0, 0, 0, 0 }` for all IPv4 interfaces, or supply a specific local address.
`Config.parseBindAddress(text)` parses four decimal octets without allocation or hostname lookup.
The standalone CLI exposes this setting as `--bind-address A.B.C.D`.
An unavailable local address fails at startup. Each shard uses the same address.
Changing the address does not change connection admission, storage limits, or backpressure.
4 changes: 2 additions & 2 deletions docs/whitepaper.html
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ <h1>Fast by design.<br><em>Explicit about limits.</em></h1>
<li><a href="#macos-io"><span class="backend-platform">macOS</span><strong>kqueue</strong><span class="backend-mode">Nonblocking sockets</span></a></li>
<li><a href="#windows-io"><span class="backend-platform">Windows</span><strong>IOCP</strong><span class="backend-mode">Overlapped operations</span></a></li>
</ul>
<p class="scope"><span class="pill">EXACT ZIG 0.16.0</span><span class="pill">ASSERTIONS ENABLED</span><span class="pill">LOOPBACK EXPERIMENT</span></p>
<p class="scope"><span class="pill">EXACT ZIG 0.16.0</span><span class="pill">ASSERTIONS ENABLED</span><span class="pill">EXPERIMENTAL HTTP/1.1</span></p>
<div class="hero-art" aria-label="Default configuration and measured result">
<div><strong>0</strong><span>Extra application threads<br>Default configuration</span></div>
<div><strong>128</strong><span>Connections served<br>Default simultaneous limit</span></div>
Expand Down Expand Up @@ -824,7 +824,7 @@ <h3>Reserve output before application side effects.</h3>
<tr><td>Ownership remains uncertain</td><td>The process terminates instead of freeing storage with outstanding borrows.</td></tr>
<tr><td>An inline callback never returns</td><td>The framework cannot preempt that callback. An external watchdog must supervise a violating application.</td></tr>
</tbody></table></div>
<p>The current server binds IPv4 loopback and serves plain HTTP. Transport Layer Security (TLS) and production deployment qualification remain pending.</p>
<p>The server defaults to IPv4 loopback and serves plain HTTP. Set <code>--bind-address 0.0.0.0</code> for all IPv4 interfaces, or select a specific local IPv4 address. Connection and memory limits still apply. Transport Layer Security (TLS) and production deployment qualification remain pending.</p>
<p>The <a href="read.html?file=reports/2026-09-06-windows-shards.md">Windows shard receipt</a> records native x64 distribution, admission, and shutdown tests. Windows performance remains unmeasured.</p>
<p>Winsock creates socket resources during admission. Fixed framework storage does not bound provider allocations or delayed kernel cleanup.</p>
<p>Dynamic borrow-release notifications and per-request worker offload remain pending. Applications cannot safely invent those ownership mechanisms around the current API.</p>
Expand Down
4 changes: 2 additions & 2 deletions docs/whitepaper.template.html
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ <h1>Fast by design.<br><em>Explicit about limits.</em></h1>
<li><a href="#macos-io"><span class="backend-platform">macOS</span><strong>kqueue</strong><span class="backend-mode">Nonblocking sockets</span></a></li>
<li><a href="#windows-io"><span class="backend-platform">Windows</span><strong>IOCP</strong><span class="backend-mode">Overlapped operations</span></a></li>
</ul>
<p class="scope"><span class="pill">EXACT ZIG 0.16.0</span><span class="pill">ASSERTIONS ENABLED</span><span class="pill">LOOPBACK EXPERIMENT</span></p>
<p class="scope"><span class="pill">EXACT ZIG 0.16.0</span><span class="pill">ASSERTIONS ENABLED</span><span class="pill">EXPERIMENTAL HTTP/1.1</span></p>
<div class="hero-art" aria-label="Default configuration and measured result">
<div><strong>0</strong><span>Extra application threads<br>Default configuration</span></div>
<div><strong>128</strong><span>Connections served<br>Default simultaneous limit</span></div>
Expand Down Expand Up @@ -227,7 +227,7 @@ <h3>Reserve output before application side effects.</h3>
<tr><td>Ownership remains uncertain</td><td>The process terminates instead of freeing storage with outstanding borrows.</td></tr>
<tr><td>An inline callback never returns</td><td>The framework cannot preempt that callback. An external watchdog must supervise a violating application.</td></tr>
</tbody></table></div>
<p>The current server binds IPv4 loopback and serves plain HTTP. Transport Layer Security (TLS) and production deployment qualification remain pending.</p>
<p>The server defaults to IPv4 loopback and serves plain HTTP. Set <code>--bind-address 0.0.0.0</code> for all IPv4 interfaces, or select a specific local IPv4 address. Connection and memory limits still apply. Transport Layer Security (TLS) and production deployment qualification remain pending.</p>
<p>The <a href="read.html?file=reports/2026-09-06-windows-shards.md">Windows shard receipt</a> records native x64 distribution, admission, and shutdown tests. Windows performance remains unmeasured.</p>
<p>Winsock creates socket resources during admission. Fixed framework storage does not bound provider allocations or delayed kernel cleanup.</p>
<p>Dynamic borrow-release notifications and per-request worker offload remain pending. Applications cannot safely invent those ownership mechanisms around the current API.</p>
Expand Down
11 changes: 7 additions & 4 deletions src/main.zig
Original file line number Diff line number Diff line change
Expand Up @@ -58,14 +58,16 @@ pub fn main(init: std.process.Init) !void {
while (args.next()) |flag| {
if (std.mem.eql(u8, flag, "--help")) {
std.debug.print("bounded-http: bounded experimental Linux io_uring / macOS kqueue / Windows IOCP HTTP/1.1\n" ++
"--port N --connections N --execution workers|inline --workers N --max-body N --max-header N\n" ++
"--bind-address A.B.C.D --port N --connections N --execution workers|inline --workers N --max-body N --max-header N\n" ++
"--timeout-ms N --duration-ms N --send-chunk N --gather-send 0|1 --stall-ms N\n" ++
"--response-batch-limit N --socket-send-buffer N --output-bytes N --max-response N --memory-budget N --index FILE\n" ++
"--borrow-copy-threshold N --callbacks-per-turn N --callback-timing 0|1 --deadline-sweep-ms N --shards N\n", .{});
return;
}
const value = args.next() orelse return error.MissingArgument;
if (std.mem.eql(u8, flag, "--port")) {
if (std.mem.eql(u8, flag, "--bind-address")) {
config.bind_address = try framework.Config.parseBindAddress(value);
} else if (std.mem.eql(u8, flag, "--port")) {
config.port = try std.fmt.parseInt(u16, value, 10);
} else if (std.mem.eql(u8, flag, "--connections")) {
config.connections = try std.fmt.parseInt(u16, value, 10);
Expand Down Expand Up @@ -155,8 +157,9 @@ pub fn main(init: std.process.Init) !void {
};
try cluster.start();
budget.sealed.store(true, .release);
std.debug.print("READY port={d} backend={s} connections={d} workers={d} execution={s} gather_send={d} response_batch_limit={d} shards={d} callbacks_per_turn={d} optimize={s}\n", .{
cluster.port(), framework.backend_name, config.connections, config.workers, @tagName(config.execution), @intFromBool(config.gather_send), config.effectiveBatchLimit(), shards, cluster.shards[0].stats.callbacks_per_turn, @tagName(@import("builtin").mode),
std.debug.print("READY port={d} backend={s} connections={d} workers={d} execution={s} gather_send={d} response_batch_limit={d} shards={d} callbacks_per_turn={d} optimize={s} bind_address={d}.{d}.{d}.{d}\n", .{
cluster.port(), framework.backend_name, config.connections, config.workers, @tagName(config.execution), @intFromBool(config.gather_send), config.effectiveBatchLimit(), shards, cluster.shards[0].stats.callbacks_per_turn, @tagName(@import("builtin").mode),
config.bind_address[0], config.bind_address[1], config.bind_address[2], config.bind_address[3],
});
cluster.run() catch |err| {
// A stuck callback or uncertain kernel submission still owns memory.
Expand Down
34 changes: 31 additions & 3 deletions src/server.zig
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ pub const Config = struct {
/// Finished responses retained per connection before a drain; each is a
/// range of the connection's output arena plus an optional borrowed span.
response_batch_limit: u16 = 128,
/// IPv4 listener octets. Wildcard 0.0.0.0 listens on all IPv4 interfaces.
bind_address: [4]u8 = .{ 127, 0, 0, 1 },
port: u16 = 8080,
connections: u16 = 128,
workers: u16 = 0,
Expand Down Expand Up @@ -79,6 +81,20 @@ pub const Config = struct {

pub const max_callbacks_per_turn_auto: u32 = 8192;

/// Parse four decimal IPv4 octets at startup. No allocation or name lookup.
pub fn parseBindAddress(text: []const u8) error{InvalidBindAddress}![4]u8 {
var address: [4]u8 = undefined;
var octets = std.mem.splitScalar(u8, text, '.');
for (&address) |*octet| {
const part = octets.next() orelse return error.InvalidBindAddress;
if (part.len == 0 or part.len > 3) return error.InvalidBindAddress;
for (part) |digit| if (digit < '0' or digit > '9') return error.InvalidBindAddress;
octet.* = std.fmt.parseInt(u8, part, 10) catch return error.InvalidBindAddress;
}
if (octets.next() != null) return error.InvalidBindAddress;
return address;
}

pub fn wireBytes(self: Config) !usize {
const body = try std.math.mul(usize, self.max_body, 2);
return std.math.add(usize, try std.math.add(usize, body, self.max_header), 4096);
Expand Down Expand Up @@ -445,10 +461,10 @@ pub const Server = struct {
const self = try allocator.create(Server);
errdefer allocator.destroy(self);
var backend = if (builtin.os.tag == .windows) switch (accept_mode) {
.direct => try transport.Backend.init(allocator, config.connections, config.port, config.reuse_port),
.distribute => try transport.Backend.initAcceptor(allocator, config.connections, config.port),
.direct => try transport.Backend.initBound(allocator, config.connections, config.bind_address, config.port, config.reuse_port),
.distribute => try transport.Backend.initAcceptorBound(allocator, config.connections, config.bind_address, config.port),
.receive => try transport.Backend.initDestination(allocator, config.connections),
} else try transport.Backend.init(allocator, config.connections, config.port, config.reuse_port);
} else try transport.Backend.initBound(allocator, config.connections, config.bind_address, config.port, config.reuse_port);
errdefer backend.deinit();
if (config.gather_send) try backend.enableGather();
const slots = try allocator.alloc(Slot, config.connections);
Expand Down Expand Up @@ -2703,6 +2719,18 @@ fn vectorString(vec: c.iovec_const) []const u8 {
return vec.base[0..vec.len];
}

test "bind address parsing accepts only four bounded decimal octets" {
try std.testing.expectEqual([4]u8{ 127, 0, 0, 1 }, (Config{}).bind_address);
try std.testing.expectEqual([4]u8{ 0, 0, 0, 0 }, try Config.parseBindAddress("0.0.0.0"));
try std.testing.expectEqual([4]u8{ 192, 168, 1, 255 }, try Config.parseBindAddress("192.168.1.255"));
try std.testing.expectEqual([4]u8{ 127, 0, 0, 1 }, try Config.parseBindAddress("127.000.0.001"));
for ([_][]const u8{ "", "localhost", "::1", "127.1", "127.0.0", "127.0.0.1.2", "127..0.1", ".127.0.1", "127.0.0.", "256.0.0.1", "-1.0.0.1", "+1.0.0.1", "1_0.0.0.1", "0x7f.0.0.1", "127.0.0.1 ", " 127.0.0.1", "0000.0.0.1", "127.0.0.1:8080" }) |invalid| {
try std.testing.expectError(error.InvalidBindAddress, Config.parseBindAddress(invalid));
}
const configured: Config = .{ .bind_address = .{ 192, 168, 1, 2 } };
try std.testing.expectEqual(configured.bind_address, Cluster.shardConfig(configured, 2, 1, 8080).bind_address);
}

test "configuration rejects combined resource overcommit and impossible worker limits" {
try (Config{}).validate();
try std.testing.expectError(error.InvalidConfiguration, (Config{ .execution = .workers, .workers = 0 }).validate());
Expand Down
29 changes: 27 additions & 2 deletions src/transport.zig
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,13 @@ pub fn setSendBuffer(backend: *Backend, socket: Socket, bytes: u32) !void {
// framework connection admission; it cannot establish application admission.
// With `reuse_port`, several owners may bind the same address; on Linux the
// kernel distributes connections across them.
pub fn listen(port_number: u16, max_connections: u16, nonblocking: bool, reuse_port: bool) !struct { socket: Socket, port: u16 } {
const Listener = struct { socket: Socket, port: u16 };

pub fn listen(port_number: u16, max_connections: u16, nonblocking: bool, reuse_port: bool) !Listener {
return listenBound(.{ 127, 0, 0, 1 }, port_number, max_connections, nonblocking, reuse_port);
}

pub fn listenBound(bind_address: [4]u8, port_number: u16, max_connections: u16, nonblocking: bool, reuse_port: bool) !Listener {
if (max_connections == 0 or max_connections > 16383) return error.InvalidConnectionLimit;
const fd = c.socket(c.AF.INET, c.SOCK.STREAM, 0);
if (fd < 0) return error.SocketFailed;
Expand All @@ -65,7 +71,7 @@ pub fn listen(port_number: u16, max_connections: u16, nonblocking: bool, reuse_p
}
var address: c.sockaddr.in = .{
.port = std.mem.nativeToBig(u16, port_number),
.addr = std.mem.nativeToBig(u32, 0x7f000001),
.addr = @bitCast(bind_address),
};
if (c.bind(fd, @ptrCast(&address), @sizeOf(@TypeOf(address))) != 0) return error.BindFailed;
if (c.listen(fd, @intCast(max_connections)) != 0) return error.ListenFailed;
Expand Down Expand Up @@ -285,3 +291,22 @@ test "two reuse-port listeners share one loopback port" {
defer second.deinit();
try std.testing.expectEqual(first.port(), second.port());
}

test "listener binds the configured IPv4 address and preserves loopback default" {
if (builtin.os.tag == .windows) return error.SkipZigTest;
var address: c.sockaddr.in = undefined;
for ([_][4]u8{ .{ 0, 0, 0, 0 }, .{ 127, 0, 0, 1 } }) |expected| {
const configured = try listenBound(expected, 0, 1, true, false);
defer closeFd(configured.socket);
var length: c.socklen_t = @sizeOf(@TypeOf(address));
try std.testing.expectEqual(@as(c_int, 0), c.getsockname(configured.socket, @ptrCast(&address), &length));
try std.testing.expectEqual(expected, @as([4]u8, @bitCast(address.addr)));
try std.testing.expect(configured.port != 0);
}

const default = try listen(0, 1, true, false);
defer closeFd(default.socket);
var length: c.socklen_t = @sizeOf(@TypeOf(address));
try std.testing.expectEqual(@as(c_int, 0), c.getsockname(default.socket, @ptrCast(&address), &length));
try std.testing.expectEqual(std.mem.nativeToBig(u32, 0x7f000001), address.addr);
}
Loading