From 3404e7330df775b5d6d706f29b9a00883c1b3239 Mon Sep 17 00:00:00 2001 From: FObersteiner Date: Fri, 14 Jun 2024 20:53:45 +0200 Subject: [PATCH] to v0.0.8 --- build.zig | 4 ++-- build.zig.zon | 2 +- docs/change.log | 5 +++++ src/main.zig | 14 ++++++-------- src/ntp.zig | 28 ++++++++++------------------ 5 files changed, 24 insertions(+), 29 deletions(-) create mode 100644 docs/change.log diff --git a/build.zig b/build.zig index 03b99a9..5b68872 100755 --- a/build.zig +++ b/build.zig @@ -1,6 +1,6 @@ const std = @import("std"); const log = std.log.scoped(.ntp_client_build); -const client_version = std.SemanticVersion{ .major = 0, .minor = 0, .patch = 7 }; +const client_version = std.SemanticVersion{ .major = 0, .minor = 0, .patch = 8 }; pub fn build(b: *std.Build) void { const target = b.standardTargetOptions(.{}); @@ -26,7 +26,7 @@ pub fn build(b: *std.Build) void { b.installArtifact(exe); // for Windows compatibility, required by sockets functionality - // exe.linkLibC(); + exe.linkLibC(); exe.root_module.addImport("flags", flags_module); exe.root_module.addImport("zdt", zdt_module); diff --git a/build.zig.zon b/build.zig.zon index 99221d0..b061606 100755 --- a/build.zig.zon +++ b/build.zig.zon @@ -1,6 +1,6 @@ .{ .name = "ntp_client", - .version = "0.0.7", + .version = "0.0.8", .dependencies = .{ .zdt = .{ .url = "https://codeberg.org/FObersteiner/zdt/archive/v0.1.3-pre.tar.gz", diff --git a/docs/change.log b/docs/change.log new file mode 100644 index 0000000..1d0ff83 --- /dev/null +++ b/docs/change.log @@ -0,0 +1,5 @@ +# Changelog + +## 2024-06-14, v0.0.8 + +- add IPv6 support diff --git a/src/main.zig b/src/main.zig index 5172a50..1eff391 100755 --- a/src/main.zig +++ b/src/main.zig @@ -63,25 +63,23 @@ pub fn main() !void { if (addrlist.canon_name) |n| println("Query server: {s}", .{n}); // from where to send the query - const addr_src = try std.net.Address.parseIp(cli.flags.src_ip, cli.flags.src_port); + const addr_src = try std.net.Address.resolveIp(cli.flags.src_ip, cli.flags.src_port); const sock = try posix.socket( - addr_src.any.family, // might be IPv6 + addr_src.any.family, // Notes on flags: - // NONBLOCK is used to create timeout behavior. + // NONBLOCK can be used to create timeout behavior. // CLOEXEC not strictly needed here; see open(2) man page. - posix.SOCK.DGRAM | posix.SOCK.CLOEXEC, // | posix.SOCK.NONBLOCK + posix.SOCK.DGRAM | posix.SOCK.CLOEXEC, posix.IPPROTO.UDP, ); try posix.bind(sock, &addr_src.any, addr_src.getOsSockLen()); - // NOTE : since this is a one-shot program, we would not have to close the socket. - // The OS could clean up for us. defer posix.close(sock); var buf: [mtu]u8 = std.mem.zeroes([mtu]u8); iter_addrs: for (addrlist.addrs) |dst| { - var dst_addr_sock = dst.any; + var dst_addr_sock: posix.sockaddr = undefined; // must not use dst.any var dst_addr_len: posix.socklen_t = dst.getOsSockLen(); ntp.Packet.toBytesBuffer(proto_vers, true, &buf); @@ -89,7 +87,7 @@ pub fn main() !void { sock, buf[0..ntp.packet_len], 0, - &dst_addr_sock, + &dst.any, // &dst_addr_sock, dst_addr_len, ) catch |err| switch (err) { error.AddressFamilyNotSupported => { diff --git a/src/ntp.zig b/src/ntp.zig index 84c222d..ab118ac 100755 --- a/src/ntp.zig +++ b/src/ntp.zig @@ -1,9 +1,9 @@ //! NTP client library const std = @import("std"); +const mem = std.mem; const print = std.debug.print; const testing = std.testing; const assert = std.debug.assert; -const native_endian = @import("builtin").target.cpu.arch.endian(); const ns_per_s: u64 = 1_000_000_000; @@ -256,24 +256,22 @@ pub const Packet = packed struct { pub fn toBytesBuffer(version: u8, set_xmt: bool, buf: []u8) void { assert(buf.len >= packet_len); var p: Packet = Packet._init(version, set_xmt); - if (native_endian == .little) p.ts_xmt = @byteSwap(p.ts_xmt); + p.ts_xmt = mem.nativeToBig(u64, p.ts_xmt); const ntp_bytes: [packet_len]u8 = @bitCast(p); - std.mem.copyForwards(u8, buf, ntp_bytes[0..]); + mem.copyForwards(u8, buf, ntp_bytes[0..]); } /// Parse bytes of the reply received from the server. /// Adjusts for byte order. pub fn parse(bytes: [packet_len]u8) Packet { var p: Packet = @bitCast(bytes); - if (native_endian == .little) { - p.root_delay = @byteSwap(p.root_delay); - p.root_dispersion = @byteSwap(p.root_dispersion); - p.ref_id = @byteSwap(p.ref_id); - p.ts_ref = @byteSwap(p.ts_ref); - p.ts_org = @byteSwap(p.ts_org); - p.ts_rec = @byteSwap(p.ts_rec); - p.ts_xmt = @byteSwap(p.ts_xmt); - } + p.root_delay = mem.bigToNative(u32, p.root_delay); + p.root_dispersion = mem.bigToNative(u32, p.root_dispersion); + p.ref_id = mem.bigToNative(u32, p.ref_id); + p.ts_ref = mem.bigToNative(u64, p.ts_ref); + p.ts_org = mem.bigToNative(u64, p.ts_org); + p.ts_rec = mem.bigToNative(u64, p.ts_rec); + p.ts_xmt = mem.bigToNative(u64, p.ts_xmt); return p; } }; @@ -282,12 +280,6 @@ test "Packet" { const p = Packet._init(3, true); const b: [packet_len]u8 = @bitCast(p); try testing.expectEqual(@as(u8, 27), b[0]); - - // b = [packet_len]u8{ 28, 2, 3, 231, 0, 0, 2, 221, 0, 0, 7, 211, 192, 53, 103, 108, 234, 10, 216, 64, 237, 98, 61, 18, 234, 10, 220, 65, 194, 44, 176, 0, 234, 10, 220, 65, 223, 165, 143, 37, 234, 10, 220, 65, 223, 166, 34, 232 }; - // const have: Packet = Packet.parse(b); - // print("{any}\n", .{have}); - // print("{d}\n", .{Time.fromRaw(have.ts_rec).decode()}); - // print("{d}\n", .{Time.fromRaw(have.ts_ref).toUnixNanos()}); } /// Analyze an NTP packet received from a server.