Skip to content

Commit

Permalink
fix: solve network memory issue
Browse files Browse the repository at this point in the history
  • Loading branch information
0xWOF committed Apr 16, 2024
1 parent 980726b commit 21203ed
Showing 1 changed file with 23 additions and 17 deletions.
40 changes: 23 additions & 17 deletions Source/AppleFetcher/Implement/NetworkFetcher.swift
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ extension NetworkFetcher {
ipStorage[interfaceType] = ip
}

freeifaddrs(interfacesPointer)

let searchs = [
InterfaceType(name: "en0", family: sa_family_t(AF_INET)),
InterfaceType(name: "en0", family: sa_family_t(AF_INET6)),
Expand All @@ -94,18 +96,19 @@ extension NetworkFetcher {
}

private func fetchType() -> NetworkType {
var zeroAddress = sockaddr()
bzero(&zeroAddress, MemoryLayout.size(ofValue: zeroAddress))
zeroAddress.sa_len = UInt8(MemoryLayout.size(ofValue: zeroAddress))
zeroAddress.sa_family = sa_family_t(AF_INET)

guard let reachability = SCNetworkReachabilityCreateWithAddress(
kCFAllocatorDefault,
&zeroAddress
) else {
var zeroAddress = sockaddr_in()
zeroAddress.sin_len = UInt8(MemoryLayout<sockaddr_in>.size)
zeroAddress.sin_family = sa_family_t(AF_INET)

guard let reachability = withUnsafePointer(to: &zeroAddress, {
$0.withMemoryRebound(to: sockaddr.self, capacity: 1) {
SCNetworkReachabilityCreateWithAddress(nil, $0)
}
}) else {
return .none
}


var flags = SCNetworkReachabilityFlags()
if !SCNetworkReachabilityGetFlags(reachability, &flags) {
return .none
Expand All @@ -115,13 +118,16 @@ extension NetworkFetcher {
return .none
}

if flags.contains(.connectionRequired)
&& !(
(flags.contains(.connectionOnDemand) || flags.contains(.connectionOnTraffic))
&& !flags.contains(.interventionRequired)
)
{
return .none
var type = NetworkType.none

if !flags.contains(.connectionRequired) {
type = .wifi
}

if flags.contains(.connectionOnDemand) || flags.contains(.connectionOnTraffic) {
if !flags.contains(.interventionRequired) {
type = .wifi
}
}

#if os(iOS) || os(tvOS)
Expand All @@ -130,7 +136,7 @@ extension NetworkFetcher {
}
#endif

return .wifi
return type
}
}

Expand Down

0 comments on commit 21203ed

Please sign in to comment.