From a3af3fde320fc687f21b77323a8bad50c9fed671 Mon Sep 17 00:00:00 2001 From: anlicheng <244108715@qq.com> Date: Tue, 14 Apr 2026 16:22:39 +0800 Subject: [PATCH] fix util --- Tun/Punchnet/SDLUtil.swift | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/Tun/Punchnet/SDLUtil.swift b/Tun/Punchnet/SDLUtil.swift index d6a772c..fc8f244 100644 --- a/Tun/Punchnet/SDLUtil.swift +++ b/Tun/Punchnet/SDLUtil.swift @@ -66,6 +66,20 @@ struct SDLUtil { return String(cString: hostBuffer) } } + + public static func ipv6StrToData(_ ip: String) -> Data? { + let normalizedIp = String(ip.split(separator: "%", maxSplits: 1, omittingEmptySubsequences: false).first ?? "") + guard !normalizedIp.isEmpty else { + return nil + } + + var address = in6_addr() + guard inet_pton(AF_INET6, normalizedIp, &address) == 1 else { + return nil + } + + return withUnsafeBytes(of: &address) { Data($0) } + } // 判断ip地址是否在同一个网络 public static func inSameNetwork(ip: UInt32, compareIp: UInt32, maskLen: UInt8) -> Bool {