Initial commit: NetworkCity — network traffic as a neon city
A native macOS network monitor that renders live traffic as a top-down neon city: your Mac is downtown, remote orgs are glowing districts, and traffic is cars of light driving the roads. Architecture: - NetworkCityCore: swappable data layer behind a ConnectionSource protocol (nettop-backed today), plus pure/tested logic — traffic diffing, org classification (reverse-DNS + CIDR), traffic classes, hub-and-spoke expansion policy, label anti-overlap, and latency→distance layout. - NetworkCityApp: SwiftUI + SpriteKit city — auto-expanding districts, protocol-coloured cars, click-to-inspect panel, and latency-as-distance (districts glide to their measured RTT). - nettop-probe: CLI proof of the data layer. 44 tests passing. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,46 @@
|
||||
import Foundation
|
||||
import Darwin
|
||||
|
||||
/// Best-effort reverse DNS (IP -> PTR hostname). Blocking — call it off the main
|
||||
/// thread. Returns nil when there's no PTR record (we don't want a numeric echo).
|
||||
public enum ReverseDNS {
|
||||
|
||||
public static func resolve(_ ip: String) -> String? {
|
||||
var storage = sockaddr_storage()
|
||||
var length: socklen_t = 0
|
||||
|
||||
if ip.contains(":") {
|
||||
var sa = sockaddr_in6()
|
||||
sa.sin6_family = sa_family_t(AF_INET6)
|
||||
sa.sin6_len = UInt8(MemoryLayout<sockaddr_in6>.size)
|
||||
guard inet_pton(AF_INET6, ip, &sa.sin6_addr) == 1 else { return nil }
|
||||
withUnsafeBytes(of: &sa) { src in
|
||||
withUnsafeMutableBytes(of: &storage) { dst in
|
||||
dst.copyMemory(from: UnsafeRawBufferPointer(rebasing: src[0..<MemoryLayout<sockaddr_in6>.size]))
|
||||
}
|
||||
}
|
||||
length = socklen_t(MemoryLayout<sockaddr_in6>.size)
|
||||
} else {
|
||||
var sa = sockaddr_in()
|
||||
sa.sin_family = sa_family_t(AF_INET)
|
||||
sa.sin_len = UInt8(MemoryLayout<sockaddr_in>.size)
|
||||
guard inet_pton(AF_INET, ip, &sa.sin_addr) == 1 else { return nil }
|
||||
withUnsafeBytes(of: &sa) { src in
|
||||
withUnsafeMutableBytes(of: &storage) { dst in
|
||||
dst.copyMemory(from: UnsafeRawBufferPointer(rebasing: src[0..<MemoryLayout<sockaddr_in>.size]))
|
||||
}
|
||||
}
|
||||
length = socklen_t(MemoryLayout<sockaddr_in>.size)
|
||||
}
|
||||
|
||||
var host = [CChar](repeating: 0, count: Int(NI_MAXHOST))
|
||||
let result = withUnsafePointer(to: &storage) { ptr in
|
||||
ptr.withMemoryRebound(to: sockaddr.self, capacity: 1) { sa in
|
||||
getnameinfo(sa, length, &host, socklen_t(host.count), nil, 0, NI_NAMEREQD)
|
||||
}
|
||||
}
|
||||
guard result == 0 else { return nil }
|
||||
let name = String(cString: host)
|
||||
return name.isEmpty ? nil : name
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user