Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add IPv4 preference support #93

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
4 changes: 3 additions & 1 deletion Sources/Clock.swift
Original file line number Diff line number Diff line change
Expand Up @@ -63,15 +63,17 @@ public struct Clock {
/// - parameter pool: NTP pool that will be resolved into multiple NTP servers that will be used for
/// the synchronization.
/// - parameter samples: The number of samples to be acquired from each server (default 4).
/// - parameter preferIPv4: Prefer IPv4 addresses to query if both IPv4 and IPv6 addresses are returned.
/// - parameter completion: A closure that will be called after _all_ the NTP calls are finished.
/// - parameter first: A closure that will be called after the first valid date is calculated.
public static func sync(from pool: String = "time.apple.com", samples: Int = 4,
preferIPv4: Bool = false,
first: ((Date, TimeInterval) -> Void)? = nil,
completion: ((Date?, TimeInterval?) -> Void)? = nil)
{
self.loadFromDefaults()

NTPClient().query(pool: pool, numberOfSamples: samples) { offset, done, total in
NTPClient().query(pool: pool, numberOfSamples: samples, preferIPv4: preferIPv4) { offset, done, total in
if let offset = offset {
self.stableTime = TimeFreeze(offset: offset)

Expand Down
12 changes: 9 additions & 3 deletions Sources/NTPClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,13 @@ final class NTPClient {
/// - parameter port: Server NTP port (default 123).
/// - parameter version: NTP version to use (default 3).
/// - parameter numberOfSamples: The number of samples to be acquired from each server (default 4).
/// - parameter preferIPv4: Prefer IPv4 addresses to query if both IPv4 and IPv6 addresses are returned.
/// - parameter maximumServers: The maximum number of servers to be queried (default 5).
/// - parameter timeout: The individual timeout for each of the NTP operations.
/// - parameter completion: A closure that will be response PDU on success or nil on error.
func query(pool: String = "time.apple.com", version: Int8 = 3, port: Int = 123,
numberOfSamples: Int = kDefaultSamples, maximumServers: Int = kMaximumNTPServers,
numberOfSamples: Int = kDefaultSamples, preferIPv4: Bool = false,
maximumServers: Int = kMaximumNTPServers,
timeout: CFTimeInterval = kDefaultTimeout,
progress: @escaping (TimeInterval?, Int, Int) -> Void)
{
Expand Down Expand Up @@ -59,9 +61,13 @@ final class NTPClient {
if addresses.count == 0 {
return progress(nil, 0, 0)
}

let addressesToQuery = preferIPv4 && !addresses.filter({ $0.family == PF_INET }).isEmpty
? addresses.filter({ $0.family == PF_INET })
: addresses

let totalServers = min(addresses.count, maximumServers)
for address in addresses[0 ..< totalServers] {
let totalServers = min(addressesToQuery.count, maximumServers)
for address in addressesToQuery[0 ..< totalServers] {
queryIPAndStoreResult(address, totalServers * numberOfSamples)
}
}
Expand Down