Modern dual-stack ICMP ping library for Swift — IPv4 and IPv6 echo over
unprivileged ICMP datagram sockets, wrapped in a Swift 6 actor +
AsyncSequence API. Zero dependencies.
On Apple platforms, RTTs are measured against the kernel's packet-arrival
timestamps (SO_TIMESTAMP_MONOTONIC), so scheduler wakeup latency doesn't
inflate them.
Design rationale and roadmap live in PLAN.md. The platform behavior below — address families and NAT64, iOS permissions and backgrounding, Linux kernel settings — is also written up as a DocC article: Platform Notes.
Find PingKit on Swift Package Index (SPI) for releases, Swift and platform compatibility results, and hosted API documentation.
- Swift 6.0+, macOS 13+ / iOS 16+
- No root or entitlement is needed on macOS command-line tools. Sandboxed Mac
apps need
com.apple.security.network.clientandcom.apple.security.network.server.
dependencies: [
.package(url: "https://github.com/gewill/PingKit.git", from: "0.9.0"),
],
targets: [
.target(name: "MyTarget", dependencies: [
.product(name: "PingKit", package: "PingKit"),
]),
]The PingKit library t
8000
arget has no runtime dependencies. The pingkit
executable uses Apple's swift-argument-parser; the DocC plugin is used only
for documentation builds.
The repository is public; Swift Package Manager can resolve this URL without GitHub credentials.
Stability: 0.x releases follow SemVer, but the public API may still change between minor versions until 1.0.
API documentation is a DocC catalog; build it locally with
swift package generate-documentation --target PingKit.
import PingKit
// One-shot
let reply = try await Pinger.ping("example.com")
print(reply.roundTripTime)
// Continuous
let pinger = Pinger(host: "1.1.1.1", configuration: .init(
interval: .seconds(1),
timeout: .seconds(2),
count: .times(5),
payloadSize: 56,
timeToLive: nil, // IPv4 TTL / IPv6 hop limit
addressFamily: .automatic)) // follows system DNS ordering, including DNS64
for try await response in pinger.responses {
switch response {
case .sent(let seq): print("seq=\(seq) sent") // show a pending row here
case .sendFailed(let seq, let err): print("seq=\(seq) send failed (errno \(err))")
case .reply(let r): print("seq=\(r.sequence) ttl=\(r.timeToLive ?? 0) rtt=\(r.roundTripTime)")
case .timeout(let seq): print("seq=\(seq) timed out")
case .unreachable(let seq, let code): print("seq=\(seq) unreachable (code \(code))")
case .timeExceeded(let seq): print("seq=\(seq) TTL exceeded")
case .packetTooBig(let seq, let mtu): print("seq=\(seq) packet too big (MTU \(mtu))")
case .parameterProblem(let seq, let code, let pointer):
print("seq=\(seq) parameter problem (code \(code), pointer \(pointer))")
}
}
let stats = await pinger.statistics()
print("\(stats.received)/\(stats.transmitted), loss \(stats.lossRate)")Each sequence produces exactly one of two outcomes: a .sent followed by a
terminal event (the socket accepted the probe), or a single .sendFailed
with no .sent (the probe never left the socket — e.g. the network is
briefly unreachable). A .sendFailed does not end the run: the next
interval retries and recovers once the network does. A failed send still
counts toward transmitted (like ping(8)), so a network drop surfaces as
packet loss in statistics() rather than vanishing from the aggregate; the
.sendFailed event carries the errno for consumers that want to tell
"never sent" from "sent but lost". So a UI can insert a pending row on
.sent and update it in place, and render .sendFailed as a standalone
failed row. On stop, cancellation, or stream failure, consumers should mark
remaining pending rows as unknown. statistics.lost includes those unsettled
probes because it is transmitted - received.
Lifecycle rules:
responsessupports a single consumer; a second subscription throwsPingError.sequenceAlreadyConsumed. Create a newPingerper run.- Cancelling the consuming task stops the pinger and closes the socket.
- If you
breakout of the loop without cancelling, callawait pinger.stop()(idempotent) to release the socket deterministically. - For a finite run, use
count: .unlimitedwithsendDuration: .seconds(300). This relative window starts at the first send opportunity after setup. - To share a deadline with a caller's session, capture
let deadline = ContinuousClock.now + .seconds(300)at session start and usecount: .unlimited, sendDeadline: deadline. Resolution, socket setup, and scheduling then consume the same window. Set only one window option. - Both options use
ContinuousClock, which advances during system sleep. At the deadline, sending stops while probes already sent reply or time out. Explicit stop or cancellation still closes immediately.
addressFamily defaults to .automatic. PingKit asks getaddrinfo for both
families and uses its first result, preserving the platform's RFC 6724 / DNS64
selection. This lets hostnames resolve to synthesized IPv6 destinations on an
IPv6-only NAT64 network. Use .ipv4 or .ipv6 to force a family; IPv6
literals and scoped link-local addresses are supported.
IPv6 replies expose their source through IPAddress.ipv6 and their received
hop limit through PingReply.timeToLive. ICMPv6 Destination Unreachable,
Time Exceeded, Packet Too Big, and Parameter Problem messages map to typed
PingResponse events. Tracer remains IPv4-only in this release.
Tracer sends echo requests with increasing TTL and reads the ICMP Time
Exceeded answers from intermediate routers — same unprivileged socket, no
root needed:
for try await hop in Tracer(host: "8.8.8.8").hops {
print(hop.ttl, hop.probes) // one TracerouteHop per TTL
}
// or collect the whole route at once
let route = try await Tracer.trace("8.8.8.8")Each TracerouteHop groups the configurable per-TTL probes (default 3);
probes are .response(router:roundTripTime:kind:) or .timeout, and the
trace ends at the destination's echo reply, a Destination Unreachable, or
maxHops. Tracer follows the same lifecycle rules as Pinger.
Linux caveat: the kernel delivers ICMP errors to the socket error queue, which PingKit doesn't read yet — intermediate hops show as timeouts there; the destination hop still resolves.
Ping and trace queues are bounded by their configuration's bufferLimits
(default: 1,024 public events/hops and 256 raw datagrams). A slow consumer
does not slow the probe schedule. If either queue fills, the run stops,
cancels outstanding probes, drains already buffered public events in order,
and throws PingError.bufferOverflow(buffer:capacity:). This local overload
is distinct from a network timeout; the delivered event prefix may include
.sent without a terminal event. Increase the relevant limit or consume
events promptly before starting a new run.
swift run pingkit-cli 8.8.8.8 -4 -c 5 -i 1 -W 2 -s 56 -m 64
swift run pingkit-cli ::1 -6 -c 3
swift run pingkit-cli trace 8.8.8.8 -m 30 -q 3 -W 1
The product is named pingkit-cli because a product named pingkit collides
with the PingKit library on case-insensitive file systems; the command
itself is pingkit, so install it as
cp .build/release/pingkit-cli /usr/local/bin/pingkit.
In ping mode -4 / -6 force the address family and -m sets the outgoing
TTL or hop limit; in trace mode -m sets the maximum hop count.
Time options accept positive finite seconds up to 2,147,483,647; values
that round to zero at Duration precision are rejected.
Prints ping(8)-style output including the closing statistics block
(traceroute(8)-style in trace mode); Ctrl-C stops an unlimited run and
still prints statistics.
No entitlement is needed; unprivileged ICMP sockets work in the iOS app sandbox (CI runs the full test suite, including loopback pings, on the iOS Simulator). Two platform behaviors to plan for:
- Local network privacy: pinging LAN addresses triggers the iOS 14+
local network permission prompt — add
NSLocalNetworkUsageDescriptionto your Info.plist. Pinging internet hosts does not. - Backgrounding: iOS makes no survival promise for suspended apps —
on-device testing showed the process can be terminated within minutes of
entering the background (and always is eventually). Don't expect a ping
session to survive the background: stop the run when the scene leaves the
foreground and start a new one on return (the demo app shows this
scenePhasepattern).Pinger.stop()/deinit close the socket either way.
A minimal SwiftUI demo app for on-device testing lives in Examples/PingDemo.
Unprivileged ICMP sockets require net.ipv4.ping_group_range to cover the
process's group (despite its name, it controls both IPv4 and IPv6 ping
sockets; most distributions ship it disabled). The kernel also
rewrites the echo identifier on these sockets, so replies are matched by
sequence only there. IPv4 and IPv6 echo build and test continuously on Linux,
including real loopback pings inside a privileged container. Traceroute is
IPv4-only and best-effort: intermediate hops require Linux error-queue support
that is not implemented yet; the destination reply still arrives.