i found a way to find the ip address from arp table and it is working fine with mac os 26 Tahoe.
This is the code i used
static func buildARPTable() -> [String: String] {
var mib: [Int32] = [CTL_NET, PF_ROUTE, 0, AF_INET, NET_RT_FLAGS, Int32(RTF_LLINFO)]
var needed = 0
guard sysctl(&mib, 6, nil, &needed, nil, 0) == 0, needed > 0 else { return [:] }
var buf = [UInt8](repeating: 0, count: needed)
guard sysctl(&mib, 6, &buf, &needed, nil, 0) == 0 else { return [:] }
var table: [String: String] = [:]
var offset = 0
while offset + MemoryLayout<rt_msghdr>.size <= needed {
let rtm = buf.withUnsafeBytes { $0.load(fromByteOffset: offset, as: rt_msghdr.self) }
let msgLen = Int(rtm.rtm_msglen)
guard msgLen > 0, offset + msgLen <= needed else { break }
// sockaddr_in immediately follows rt_msghdr
let sinOffset = offset + MemoryLayout<rt_msghdr>.size
guard sinOffset + MemoryLayout<sockaddr_in>.size <= needed else { offset += msgLen; continue }
let sin = buf.withUnsafeBytes { $0.load(fromByteOffset: sinOffset, as: sockaddr_in.self) }
guard sin.sin_family == UInt8(AF_INET) else { offset += msgLen; continue }
var ipStr = [CChar](repeating: 0, count: Int(INET_ADDRSTRLEN))
var addr = sin.sin_addr
inet_ntop(AF_INET, &addr, &ipStr, socklen_t(INET_ADDRSTRLEN))
let ip = String(cString: ipStr)
guard !ip.isEmpty, ip != "0.0.0.0" else { offset += msgLen; continue }
// sockaddr_dl follows sockaddr_in, rounded up to 4-byte alignment
let sinLen = Int(sin.sin_len)
let sdlOffset = sinOffset + ((sinLen + 3) & ~3)
guard sdlOffset + MemoryLayout<sockaddr_dl>.size <= needed else { offset += msgLen; continue }
let sdl = buf.withUnsafeBytes { $0.load(fromByteOffset: sdlOffset, as: sockaddr_dl.self) }
guard sdl.sdl_family == UInt8(AF_LINK), sdl.sdl_alen == 6 else { offset += msgLen; continue }
// sdl_data starts at byte offset 8 within sockaddr_dl (fields: len1+fam1+idx2+type1+nlen1+alen1+slen1 = 8 bytes).
// MAC bytes begin after the interface-name prefix of sdl_nlen bytes.
let macStart = sdlOffset + 8 + Int(sdl.sdl_nlen)
guard macStart + 6 <= needed else { offset += msgLen; continue }
let mac = (0..<6).map { String(format: "%02x", buf[macStart + $0]) }.joined(separator: ":")
table[mac] = ip
offset += msgLen
}
return table
}
I made it work for mac os 26 Tahoe and it is working with stability. but the same code does not work on mac os 27. Is there any limitation introduced in mac os 27 for sysctl?