swiftlib_sdlan/Sources/sdlan/SDLNetAddress.swift
2025-07-14 15:33:40 +08:00

50 lines
1.0 KiB
Swift

//
// 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)"
}
}