From a57fd3850de5023a5ab72d3acadd9c5671610a6c Mon Sep 17 00:00:00 2001 From: Allain Lalonde Date: Sun, 15 Mar 2026 09:07:18 -0400 Subject: [PATCH] update for latest std io api --- build.zig.zon | 2 +- demo/build.zig.zon | 2 +- demo/src/main.zig | 2 +- example/all_ciphers.zig | 2 +- example/badssl.zig | 2 +- example/client_auth.zig | 2 +- example/common.zig | 24 ++++++++++++------------ example/fuzz_server.zig | 2 +- example/http_get.zig | 2 +- example/http_get2.zig | 2 +- example/http_get_nonblock.zig | 2 +- example/http_get_resumption.zig | 2 +- example/integration_test.zig | 14 +++++++------- example/server.zig | 2 +- example/top_sites.zig | 16 ++++++++-------- src/handshake_client.zig | 2 +- src/handshake_common.zig | 8 ++++---- src/root.zig | 7 +++---- 18 files changed, 47 insertions(+), 48 deletions(-) diff --git a/build.zig.zon b/build.zig.zon index da12936..6d406eb 100644 --- a/build.zig.zon +++ b/build.zig.zon @@ -3,7 +3,7 @@ .version = "0.1.0", .dependencies = .{}, .fingerprint = 0xacfd1db3d29e1d11, - .minimum_zig_version = "0.16.0-dev.2261+d6b3dd25a", + .minimum_zig_version = "0.16.0-dev.2821+3edaef9e0", .paths = .{ "readme.md", "build.zig", diff --git a/demo/build.zig.zon b/demo/build.zig.zon index 1a187b0..9f9cdfb 100644 --- a/demo/build.zig.zon +++ b/demo/build.zig.zon @@ -2,7 +2,7 @@ .name = .demo, .version = "0.0.0", .fingerprint = 0xd642dfa0bb514ab0, - .minimum_zig_version = "0.16.0-dev.2261+d6b3dd25a", + .minimum_zig_version = "0.16.0-dev.2821+3edaef9e0", .dependencies = .{ .tls = .{ .path = "../", diff --git a/demo/src/main.zig b/demo/src/main.zig index 41cbfdb..89083af 100644 --- a/demo/src/main.zig +++ b/demo/src/main.zig @@ -28,7 +28,7 @@ pub fn main(init: std.process.Init) !void { var conn = try tls.client(&reader.interface, &writer.interface, .{ .host = host, .root_ca = root_ca, - .now = try std.Io.Clock.real.now(io), + .now = std.Io.Clock.real.now(io), .random = rng_impl.interface(), }); diff --git a/example/all_ciphers.zig b/example/all_ciphers.zig index 33cf74d..76380f4 100644 --- a/example/all_ciphers.zig +++ b/example/all_ciphers.zig @@ -12,7 +12,7 @@ pub fn main(init: std.process.Init) !void { defer root_ca.deinit(gpa); const domain = if (args.len > 1) args[1] else "cloudflare.com"; - const fail_count = run(io, root_ca, domain, try std.Io.Clock.real.now(io)); + const fail_count = run(io, root_ca, domain, std.Io.Clock.real.now(io)); if (fail_count > 0) std.process.exit(1); } diff --git a/example/badssl.zig b/example/badssl.zig index 5f7bcc1..931906f 100644 --- a/example/badssl.zig +++ b/example/badssl.zig @@ -26,7 +26,7 @@ pub fn main(init: std.process.Init) !void { cmn.get(io, domain, if (sd.port == 0) null else sd.port, false, false, .{ .root_ca = root_ca, .host = "", - .now = try std.Io.Clock.real.now(io), + .now = std.Io.Clock.real.now(io), .rng = rng_impl.interface(), }) catch |err| { std.debug.print( diff --git a/example/client_auth.zig b/example/client_auth.zig index 22a9087..4864dc1 100644 --- a/example/client_auth.zig +++ b/example/client_auth.zig @@ -66,7 +66,7 @@ pub fn main(init: std.process.Init) !void { .auth = &auth, .diagnostic = &diagnostic, .key_log_callback = tls.config.key_log.init(init.minimal.environ), - .now = try std.Io.Clock.real.now(io), + .now = std.Io.Clock.real.now(io), .rng = rng_impl.interface(), }); diff --git a/example/common.zig b/example/common.zig index 51468cd..2f97143 100644 --- a/example/common.zig +++ b/example/common.zig @@ -161,7 +161,7 @@ pub const Counter = struct { err, }; - mu: std.Thread.Mutex = .{}, + mu: std.Io.Mutex = .init, success: usize = 0, fail: usize = 0, @@ -174,9 +174,9 @@ pub const Counter = struct { tls_1_2: usize = 0, tls_1_3: usize = 0, - pub fn add(self: *@This(), res: Result) void { - self.mu.lock(); - defer self.mu.unlock(); + pub fn add(self: *@This(), io: std.Io, res: Result) void { + self.mu.lockUncancelable(io); + defer self.mu.unlock(io); switch (res) { .success => self.success += 1, @@ -186,9 +186,9 @@ pub const Counter = struct { } } - pub fn addSuccess(self: *@This(), version: tls.config.Version) void { - self.mu.lock(); - defer self.mu.unlock(); + pub fn addSuccess(self: *@This(), io: std.Io, version: tls.config.Version) void { + self.mu.lockUncancelable(io); + defer self.mu.unlock(io); self.success += 1; switch (version) { @@ -198,16 +198,16 @@ pub const Counter = struct { } } - pub fn total(self: *@This()) usize { - self.mu.lock(); - defer self.mu.unlock(); + pub fn total(self: *@This(), io: std.Io) usize { + self.mu.lockUncancelable(io); + defer self.mu.unlock(io); return self.success + self.fail + self.skip + self.err; } - pub fn show(self: *@This()) void { + pub fn show(self: *@This(), io: std.Io) void { std.debug.print( "stats:\n\t total: {}\n\t success: {}\n\t\t tls 1.2: {}\n\t\t tls 1.3: {}\n\t fail: {}\n\t error: {}\n\t skip: {}\n\n", - .{ self.total(), self.success, self.tls_1_2, self.tls_1_3, self.fail, self.err, self.skip }, + .{ self.total(io), self.success, self.tls_1_2, self.tls_1_3, self.fail, self.err, self.skip }, ); std.debug.print("\t max client record: {d:>5}\n", .{self.max_client_record_len}); std.debug.print("\t max server record: {d:>5}\n", .{self.max_server_record_len}); diff --git a/example/fuzz_server.zig b/example/fuzz_server.zig index a54b3d9..3ffb1c5 100644 --- a/example/fuzz_server.zig +++ b/example/fuzz_server.zig @@ -19,7 +19,7 @@ pub fn main(init: std.process.Init) !void { var client_root_ca = try tls.config.cert.fromFilePath(gpa, io, dir, "minica.pem"); defer client_root_ca.deinit(gpa); - const now = try std.Io.Clock.real.now(io); + const now = std.Io.Clock.real.now(io); const rng_impl: std.Random.IoSource = .{ .io = io }; const rng = rng_impl.interface(); diff --git a/example/http_get.zig b/example/http_get.zig index 5e072a3..68c424e 100644 --- a/example/http_get.zig +++ b/example/http_get.zig @@ -23,7 +23,7 @@ pub fn main(init: std.process.Init) !void { // .cipher_suites = tls.config.cipher_suites.tls12, .cipher_suites = tls.config.cipher_suites.secure, .key_log_callback = tls.config.key_log.init(init.minimal.environ), - .now = try std.Io.Clock.real.now(io), + .now = std.Io.Clock.real.now(io), .rng = rng_impl.interface(), }); } diff --git a/example/http_get2.zig b/example/http_get2.zig index 4da0790..5989299 100644 --- a/example/http_get2.zig +++ b/example/http_get2.zig @@ -34,7 +34,7 @@ pub fn main(init: std.process.Init) !void { .host = host, .root_ca = root_ca, .diagnostic = &diagnostic, - .now = try std.Io.Clock.real.now(io), + .now = std.Io.Clock.real.now(io), .rng = rng_impl.interface(), }); diff --git a/example/http_get_nonblock.zig b/example/http_get_nonblock.zig index 4e7c5ac..2712b5e 100644 --- a/example/http_get_nonblock.zig +++ b/example/http_get_nonblock.zig @@ -35,7 +35,7 @@ pub fn main(init: std.process.Init) !void { .root_ca = ca_bundle, .cipher_suites = tls.config.cipher_suites.secure, .key_log_callback = tls.config.key_log.init(init.minimal.environ), - .now = try std.Io.Clock.real.now(io), + .now = std.Io.Clock.real.now(io), .rng = rng_impl.interface(), }; var handshake = tls.nonblock.Client.init(config); diff --git a/example/http_get_resumption.zig b/example/http_get_resumption.zig index 8004fcd..7c5b7aa 100644 --- a/example/http_get_resumption.zig +++ b/example/http_get_resumption.zig @@ -7,7 +7,7 @@ pub fn main(init: std.process.Init) !void { const gpa = init.gpa; const args = try init.minimal.args.toSlice(init.arena.allocator()); - const now = try std.Io.Clock.real.now(io); + const now = std.Io.Clock.real.now(io); // Get url from args var url: []const u8 = "https://ziglang.org"; diff --git a/example/integration_test.zig b/example/integration_test.zig index 75574be..f8db9f1 100644 --- a/example/integration_test.zig +++ b/example/integration_test.zig @@ -60,7 +60,7 @@ fn connectReceive(io: Io, addr: Io.net.IpAddress, opt: tls.config.Client) !void test "server without certificate" { const io = testing.io; - const now = try std.Io.Clock.real.now(io); + const now = std.Io.Clock.real.now(io); const rng_impl = std.Random.IoSource{ .io = io }; const rng = rng_impl.interface(); @@ -80,7 +80,7 @@ test "server without certificate" { test "server with ec key key pair" { const allocator = testing.allocator; const io = testing.io; - const now = try std.Io.Clock.real.now(io); + const now = std.Io.Clock.real.now(io); const rng_impl = std.Random.IoSource{ .io = io }; const rng = rng_impl.interface(); @@ -110,7 +110,7 @@ test "server with ec key key pair" { test "server with ec key key pair from slices" { const allocator = testing.allocator; const io = testing.io; - const now = try std.Io.Clock.real.now(io); + const now = std.Io.Clock.real.now(io); const rng_impl = std.Random.IoSource{ .io = io }; const rng = rng_impl.interface(); @@ -143,7 +143,7 @@ test "server with ec key key pair from slices" { test "server with rsa key key pair" { const allocator = testing.allocator; const io = testing.io; - const now = try std.Io.Clock.real.now(io); + const now = std.Io.Clock.real.now(io); const rng_impl = std.Random.IoSource{ .io = io }; const rng = rng_impl.interface(); @@ -173,7 +173,7 @@ test "server with rsa key key pair" { test "server request client authentication" { const allocator = testing.allocator; const io = testing.io; - const now = try std.Io.Clock.real.now(io); + const now = std.Io.Clock.real.now(io); const rng_impl = std.Random.IoSource{ .io = io }; const rng = rng_impl.interface(); const dir = try std.Io.Dir.cwd().openDir(io, "example/cert", .{}); @@ -225,7 +225,7 @@ test "server request client authentication" { test "server require client authentication" { const allocator = testing.allocator; const io = testing.io; - const now = try std.Io.Clock.real.now(io); + const now = std.Io.Clock.real.now(io); const rng_impl = std.Random.IoSource{ .io = io }; const rng = rng_impl.interface(); const dir = try std.Io.Dir.cwd().openDir(io, "example/cert", .{}); @@ -275,7 +275,7 @@ test "server require client authentication" { test "server send key update" { const allocator = testing.allocator; const io = testing.io; - const now = try std.Io.Clock.real.now(io); + const now = std.Io.Clock.real.now(io); const rng_impl = std.Random.IoSource{ .io = io }; const rng = rng_impl.interface(); diff --git a/example/server.zig b/example/server.zig index 17a1e2b..0173f49 100644 --- a/example/server.zig +++ b/example/server.zig @@ -44,7 +44,7 @@ pub fn main(init: std.process.Init) !void { // .root_ca = client_root_ca, // }, .auth = &auth, - .now = try std.Io.Clock.real.now(io), + .now = std.Io.Clock.real.now(io), .rng = rng_impl.interface(), }) catch |err| { std.debug.print("tls failed with {}\n", .{err}); diff --git a/example/top_sites.zig b/example/top_sites.zig index 4b22fac..d8fb5fe 100644 --- a/example/top_sites.zig +++ b/example/top_sites.zig @@ -43,7 +43,7 @@ pub fn main(init: std.process.Init) !void { std.log.debug("all task started", .{}); var elapsed: usize = 0; - while (counter.total() < tasks) { + while (counter.total(io) < tasks) { try io.sleep(.fromSeconds(1), .real); elapsed += 1; if (elapsed > 10) { @@ -55,7 +55,7 @@ pub fn main(init: std.process.Init) !void { try group.await(io); } - counter.show(); + counter.show(io); if (counter.failRate() > 0.01) std.process.exit(1); } @@ -66,7 +66,7 @@ pub fn run(gpa: std.mem.Allocator, io: Io, domain: []const u8, root_ca: tls.conf .host = "", .root_ca = root_ca, .diagnostic = &diagnostic, - .now = std.Io.Clock.real.now(io) catch unreachable, + .now = std.Io.Clock.real.now(io), .rng = rng_impl.interface(), }; if (cmn.inList(domain, &cmn.no_keyber)) { @@ -81,7 +81,7 @@ pub fn run(gpa: std.mem.Allocator, io: Io, domain: []const u8, root_ca: tls.conf error.NetworkUnreachable, error.NameServerFailure, => { - counter.add(.err); + counter.add(io, .err); if (!only_fail) { std.debug.print("➖ {s:<25} {}\n", .{ domain, err }); } @@ -92,7 +92,7 @@ pub fn run(gpa: std.mem.Allocator, io: Io, domain: []const u8, root_ca: tls.conf error.ReadFailed, error.WriteFailed, => { - counter.add(.skip); + counter.add(io, .skip); if (!only_fail) { std.debug.print("➰ {s:<25} {s}\n", .{ domain, @errorName(err) }); } @@ -104,16 +104,16 @@ pub fn run(gpa: std.mem.Allocator, io: Io, domain: []const u8, root_ca: tls.conf if (!only_fail) { std.debug.print("➖ {s:<25} {} curl: {}\n", .{ domain, err, curl_err }); } - counter.add(.err); + counter.add(io, .err); return; }; }, } std.debug.print("❌ {s:<25} ERROR {}\n", .{ domain, err }); - counter.add(.fail); + counter.add(io, .fail); return; }; - counter.addSuccess(diagnostic.tls_version); + counter.addSuccess(io, diagnostic.tls_version); counter.max_server_record_len = @max(counter.max_server_record_len, diagnostic.max_server_record_len); counter.max_server_cleartext_len = @max(counter.max_server_cleartext_len, diagnostic.max_server_cleartext_len); counter.max_client_record_len = @max(counter.max_client_record_len, diagnostic.max_client_record_len); diff --git a/src/handshake_client.zig b/src/handshake_client.zig index 53192cd..304e9d6 100644 --- a/src/handshake_client.zig +++ b/src/handshake_client.zig @@ -977,7 +977,7 @@ test "verify google.com certificate" { const io = testing.io; var ca_bundle: Certificate.Bundle = .{}; - try ca_bundle.rescan(testing.allocator, io, try std.Io.Clock.real.now(io)); + try ca_bundle.rescan(testing.allocator, io, std.Io.Clock.real.now(io)); defer ca_bundle.deinit(testing.allocator); h.cert = .{ .host = "google.com", .skip_verify = true, .root_ca = .{}, .now_sec = 1714846451 }; diff --git a/src/handshake_common.zig b/src/handshake_common.zig index 9890f52..05eec90 100644 --- a/src/handshake_common.zig +++ b/src/handshake_common.zig @@ -137,26 +137,26 @@ pub const cert = struct { pub fn fromFilePath(allocator: mem.Allocator, io: Io, dir: std.Io.Dir, path: []const u8) !Bundle { var bundle: Bundle = .{}; - try bundle.addCertsFromFilePath(allocator, io, try Io.Clock.real.now(io), dir, path); + try bundle.addCertsFromFilePath(allocator, io, Io.Clock.real.now(io), dir, path); return bundle; } pub fn fromFilePathAbsolute(allocator: mem.Allocator, io: Io, path: []const u8) !Bundle { var bundle: Bundle = .{}; - try bundle.addCertsFromFilePathAbsolute(allocator, io, try Io.Clock.real.now(io), path); + try bundle.addCertsFromFilePathAbsolute(allocator, io, Io.Clock.real.now(io), path); return bundle; } pub fn fromSystem(allocator: mem.Allocator, io: Io) !Bundle { var bundle: Bundle = .{}; - try bundle.rescan(allocator, io, try Io.Clock.real.now(io)); + try bundle.rescan(allocator, io, Io.Clock.real.now(io)); return bundle; } pub fn fromSlice(allocator: mem.Allocator, io: Io, slice: []const u8) !Bundle { const base64 = std.base64.standard.decoderWithIgnore(" \t\r\n"); const size = slice.len; - const ts = try Io.Clock.real.now(io); + const ts = Io.Clock.real.now(io); var bundle: Bundle = .{}; diff --git a/src/root.zig b/src/root.zig index e7e007f..d553533 100644 --- a/src/root.zig +++ b/src/root.zig @@ -22,10 +22,9 @@ const handshake = struct { const Server = @import("handshake_server.zig").Handshake; }; -//TODO: io first /// Upgrades existing stream to the tls connection by the client tls handshake. pub inline fn clientFromStream(io: std.Io, stream: anytype, opt: config.Client) !Connection { - const input, const output = streamToRaderWriter(io, stream); + const input, const output = streamToReaderWriter(io, stream); return try client(input, output, opt); } @@ -46,7 +45,7 @@ pub fn client(input: *Io.Reader, output: *Io.Writer, opt: config.Client) !Connec /// Upgrades existing stream to the tls connection by the server side tls handshake. pub inline fn serverFromStream(io: Io, stream: anytype, opt: config.Server) !Connection { - const input, const output = streamToRaderWriter(io, stream); + const input, const output = streamToReaderWriter(io, stream); return try server(input, output, opt); } @@ -61,7 +60,7 @@ pub fn server(input: *Io.Reader, output: *Io.Writer, opt: config.Server) !Connec } /// With default buffer sizes -inline fn streamToRaderWriter(io: std.Io, stream: anytype) struct { *Io.Reader, *Io.Writer } { +inline fn streamToReaderWriter(io: std.Io, stream: anytype) struct { *Io.Reader, *Io.Writer } { var input_buf: [input_buffer_len]u8 = undefined; var output_buf: [output_buffer_len]u8 = undefined; var reader = stream.reader(io, &input_buf);