// // SDLIPAddress.swift // Tun // // Created by 安礼成 on 2024/3/4. // import Foundation struct SDLNetAddress { let ip: UInt32 let maskLen: UInt8 // ip地址 var ipAddress: String { return intToIpAddress(self.ip) } // 掩码 var maskAddress: String { let len0 = 32 - maskLen let num: UInt32 = (0xFFFFFFFF >> len0) << len0 return intToIpAddress(num) } // 网络地址 var networkAddress: String { let len0 = 32 - maskLen let mask: UInt32 = (0xFFFFFFFF >> len0) << len0 return intToIpAddress(self.ip & mask) } init(ip: UInt32, maskLen: UInt8) { self.ip = ip self.maskLen = maskLen } private func intToIpAddress(_ num: UInt32) -> String { let ip0 = (UInt8) (num >> 24 & 0xFF) let ip1 = (UInt8) (num >> 16 & 0xFF) let ip2 = (UInt8) (num >> 8 & 0xFF) let ip3 = (UInt8) (num & 0xFF) return "\(ip0).\(ip1).\(ip2).\(ip3)" } }