Skip to content

Commit

Permalink
to v0.0.8
Browse files Browse the repository at this point in the history
  • Loading branch information
FObersteiner committed Jun 14, 2024
1 parent f6aa38b commit 3404e73
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 29 deletions.
4 changes: 2 additions & 2 deletions build.zig
Original file line number Diff line number Diff line change
@@ -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(.{});
Expand All @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion build.zig.zon
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
5 changes: 5 additions & 0 deletions docs/change.log
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Changelog

## 2024-06-14, v0.0.8

- add IPv6 support
14 changes: 6 additions & 8 deletions src/main.zig
Original file line number Diff line number Diff line change
Expand Up @@ -63,33 +63,31 @@ 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);
_ = posix.sendto(
sock,
buf[0..ntp.packet_len],
0,
&dst_addr_sock,
&dst.any, // &dst_addr_sock,
dst_addr_len,
) catch |err| switch (err) {
error.AddressFamilyNotSupported => {
Expand Down
28 changes: 10 additions & 18 deletions src/ntp.zig
Original file line number Diff line number Diff line change
@@ -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;

Expand Down Expand Up @@ -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;
}
};
Expand All @@ -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.
Expand Down

0 comments on commit 3404e73

Please sign in to comment.