95 lines
4.1 KiB
Swift
95 lines
4.1 KiB
Swift
|
|
import Darwin
|
||
|
|
import Foundation
|
||
|
|
|
||
|
|
enum InternalIPProbe {
|
||
|
|
static func isReachable(_ address: String) async -> Bool {
|
||
|
|
guard NetworkAddress.isAuthorizedInternalIPv4(address), !Task.isCancelled else { return false }
|
||
|
|
let attempt = PingAttempt()
|
||
|
|
return await withTaskCancellationHandler(operation: {
|
||
|
|
await withCheckedContinuation { continuation in
|
||
|
|
DispatchQueue.global(qos: .utility).async {
|
||
|
|
continuation.resume(returning: attempt.run(address))
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}, onCancel: { attempt.cancel() })
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
private final class PingAttempt: @unchecked Sendable {
|
||
|
|
private let lock = NSLock()
|
||
|
|
private var cancelled = false
|
||
|
|
|
||
|
|
func cancel() {
|
||
|
|
lock.lock()
|
||
|
|
cancelled = true
|
||
|
|
lock.unlock()
|
||
|
|
}
|
||
|
|
|
||
|
|
private var isCancelled: Bool {
|
||
|
|
lock.lock()
|
||
|
|
defer { lock.unlock() }
|
||
|
|
return cancelled
|
||
|
|
}
|
||
|
|
|
||
|
|
func run(_ address: String) -> Bool {
|
||
|
|
let deadline = DispatchTime.now().uptimeNanoseconds + 1_000_000_000
|
||
|
|
guard !isCancelled else { return false }
|
||
|
|
// Darwin datagram ICMP is unprivileged. Never run a shell, query a VPN
|
||
|
|
// service or replace ICMP with a TCP/HTTP test.
|
||
|
|
let descriptor = Darwin.socket(AF_INET, SOCK_DGRAM, IPPROTO_ICMP)
|
||
|
|
guard descriptor >= 0 else { return false }
|
||
|
|
defer { Darwin.close(descriptor) }
|
||
|
|
let flags = fcntl(descriptor, F_GETFL, 0)
|
||
|
|
guard flags >= 0, fcntl(descriptor, F_SETFL, flags | O_NONBLOCK) == 0 else { return false }
|
||
|
|
|
||
|
|
var destination = sockaddr_in()
|
||
|
|
destination.sin_len = UInt8(MemoryLayout<sockaddr_in>.size)
|
||
|
|
destination.sin_family = sa_family_t(AF_INET)
|
||
|
|
guard address.withCString({ inet_pton(AF_INET, $0, &destination.sin_addr) }) == 1 else { return false }
|
||
|
|
let connected = withUnsafePointer(to: &destination) {
|
||
|
|
$0.withMemoryRebound(to: sockaddr.self, capacity: 1) {
|
||
|
|
Darwin.connect(descriptor, $0, socklen_t(MemoryLayout<sockaddr_in>.size))
|
||
|
|
}
|
||
|
|
}
|
||
|
|
guard connected == 0, !isCancelled else { return false }
|
||
|
|
|
||
|
|
let payload = Array(UUID().uuidString.utf8)
|
||
|
|
let packet = ICMPEchoPacket.request(identifier: UInt16.random(in: 1...UInt16.max), payload: payload)
|
||
|
|
let sent = packet.withUnsafeBytes { Darwin.write(descriptor, $0.baseAddress, $0.count) }
|
||
|
|
guard sent == packet.count else { return false }
|
||
|
|
var descriptorState = pollfd(fd: descriptor, events: Int16(POLLIN), revents: 0)
|
||
|
|
while !isCancelled {
|
||
|
|
let now = DispatchTime.now().uptimeNanoseconds
|
||
|
|
guard now < deadline else { return false }
|
||
|
|
let waitMS = Int32(min(50, max(1, (deadline - now) / 1_000_000)))
|
||
|
|
let ready = Darwin.poll(&descriptorState, 1, waitMS)
|
||
|
|
if ready < 0 {
|
||
|
|
if errno == EINTR { continue }
|
||
|
|
return false
|
||
|
|
}
|
||
|
|
guard ready > 0 else { continue }
|
||
|
|
guard descriptorState.revents & Int16(POLLIN) != 0 else { return false }
|
||
|
|
var buffer = [UInt8](repeating: 0, count: 4096)
|
||
|
|
var sender = sockaddr_in()
|
||
|
|
var senderSize = socklen_t(MemoryLayout<sockaddr_in>.size)
|
||
|
|
let received = withUnsafeMutablePointer(to: &sender) { senderPointer in
|
||
|
|
senderPointer.withMemoryRebound(to: sockaddr.self, capacity: 1) { socketPointer in
|
||
|
|
buffer.withUnsafeMutableBytes {
|
||
|
|
Darwin.recvfrom(descriptor, $0.baseAddress, $0.count, 0, socketPointer, &senderSize)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
guard received > 0 else {
|
||
|
|
if errno == EAGAIN || errno == EWOULDBLOCK || errno == EINTR { continue }
|
||
|
|
return false
|
||
|
|
}
|
||
|
|
if sender.sin_family == sa_family_t(AF_INET),
|
||
|
|
sender.sin_addr.s_addr == destination.sin_addr.s_addr,
|
||
|
|
ICMPEchoPacket.isReply(Array(buffer.prefix(received)), payload: payload) {
|
||
|
|
return !isCancelled && DispatchTime.now().uptimeNanoseconds < deadline
|
||
|
|
}
|
||
|
|
}
|
||
|
|
return false
|
||
|
|
}
|
||
|
|
}
|