Skip to content

Commit

Permalink
to v0.0.13
Browse files Browse the repository at this point in the history
  • Loading branch information
FObersteiner committed Jun 24, 2024
1 parent 23f5c44 commit 202ff99
Show file tree
Hide file tree
Showing 8 changed files with 23 additions and 13 deletions.
2 changes: 1 addition & 1 deletion README.md
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ Round-trip delay: 0.077 s (76970 us)

## Compatibility and Requirements

Developed & tested on Linux. Windows should work (build.zig links libc for this), Mac OS might work (can't test this)
Developed & tested on Linux (Debian, on an x86 machine). Windows should work (build.zig links libc for this), Mac OS might work (can't test this).

## Zig version

Expand Down
2 changes: 1 addition & 1 deletion build.zig
100755 → 100644
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 = 12 };
const client_version = std.SemanticVersion{ .major = 0, .minor = 0, .patch = 13 };

pub fn build(b: *std.Build) void {
const target = b.standardTargetOptions(.{});
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.12",
.version = "0.0.13",
.dependencies = .{
.zdt = .{
.url = "https://codeberg.org/FObersteiner/zdt/archive/v0.1.4.tar.gz",
Expand Down
4 changes: 4 additions & 0 deletions docs/change.log
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## 2024-06-24, v0.0.13

- use parseIp instead of resolveIp, avoids "std.net.if_nametoindex unimplemented for this OS" error on specific OS (thanks @part1zano on codeberg)

## 2024-06-20, v0.0.12

- add parser for ref ID (stratum 0 or 1)
Expand Down
2 changes: 1 addition & 1 deletion src/cmd.zig
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ server: []const u8 = "pool.ntp.org",
port: u16 = 123,
protocol_version: u8 = 4,
all: bool = false,
src_ip: []const u8 = "0.0.0.0",
src_ip: []const u8 = "0.0.0.0", // TODO : should this be 0::0 / IPv6 by default?
src_port: u16 = 0,
timezone: []const u8 = "UTC",
json: bool = false,
Expand Down
6 changes: 4 additions & 2 deletions src/main.zig
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,10 @@ pub fn main() !void {
};
defer addrlist.deinit();

// from where to send the query
const addr_src = try std.net.Address.resolveIp(cli.flags.src_ip, cli.flags.src_port);
// from where to send the query.
// Zig std docs: to handle IPv6 link-local unix addresses,
// it is recommended to use `resolveIp` instead.
const addr_src = try std.net.Address.parseIp(cli.flags.src_ip, cli.flags.src_port);

const sock = try posix.socket(
addr_src.any.family,
Expand Down
18 changes: 11 additions & 7 deletions src/ntp.zig
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ pub const epoch_offset: u32 = 2_208_988_800;

/// The current NTP era.
/// Era zero starts at zero hours on 1900-01-01 and ends 2^32 seconds later.
pub const ntp_era: u8 = 0;
// TODO : handle era != 0 (see also: Unix time input/output)
pub const ntp_era: i8 = 0;

pub const client_mode: u8 = 3;

Expand All @@ -38,10 +39,9 @@ pub fn periodToNanos(p: i8) u64 {

pub fn periodToSeconds(p: i8) u64 {
if (p > 63) return u64_max;
if (p < -63) return 0;
if (p > 0) return @as(u64, 1) << @as(u6, @intCast(p));
// ignore negative input; cannot represent sub-second period
return 0;
// ignore negative input (ceil period); cannot represent sub-second period
return 1;
}

/// Time (duration, to be precise) since epoch.
Expand Down Expand Up @@ -113,8 +113,12 @@ pub const Time = struct {
};

test "period" {
const s = periodToSeconds(17);
var s = periodToSeconds(17);
try testing.expectEqual(std.math.powi(u64, 2, 17), s);
s = periodToSeconds(0);
try testing.expectEqual(1, s);
s = periodToSeconds(-1);
try testing.expectEqual(1, s);

const ns = periodToNanos(5);
const want = try std.math.powi(u64, 2, 5);
Expand Down Expand Up @@ -198,7 +202,7 @@ test "Time Unix" {
ts = @as(u64, @intCast(std.time.nanoTimestamp()));
try testing.expectEqual(@as(i128, @intCast(ts)), Time.fromUnixNanos(ts).toUnixNanos());

// TODO : negative input
// TODO : test negative input
}

/// Duration with lower resolution and smaller range
Expand Down Expand Up @@ -290,7 +294,7 @@ pub const Packet = packed struct {

/// Parse bytes of the reply received from the server.
/// Adjusts for byte order.
/// ref_id is NOT byte-swapped if native is little-endian.
/// ref_id is NOT byte-swapped even if native is little-endian.
pub fn parse(bytes: [packet_len]u8) Packet {
var p: Packet = @bitCast(bytes);
p.root_delay = mem.bigToNative(u32, p.root_delay);
Expand Down
Empty file modified src/prettyprint.zig
100755 → 100644
Empty file.

0 comments on commit 202ff99

Please sign in to comment.