Skip to content

Commit

Permalink
Improvements getting router ip address
Browse files Browse the repository at this point in the history
- using scutil instead of netstat and ipconfig, reduces parsing strings
  • Loading branch information
ErrorErrorError committed Jul 2, 2020
1 parent 50beb8e commit 491c3fe
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 35 deletions.
14 changes: 5 additions & 9 deletions HeliPort/Appearance/StatusMenu.swift
Original file line number Diff line number Diff line change
Expand Up @@ -376,17 +376,13 @@ final class StatusMenu: NSMenu, NSMenuDelegate {
var mcsIndex = NSLocalizedString("Unavailable", comment: "")
var staInfo = station_info_t()
if self.status == ITL80211_S_RUN && get_station_info(&staInfo) == KERN_SUCCESS {
let macAddress = String(self.macItem.title)
.replacingOccurrences(of: NSLocalizedString("Address: ", comment: ""),
with: "",
options: .regularExpression, range: nil)
let ipAddress = NetworkManager.getLocalAddress(bsd: String(self.bsdItem.title)
.replacingOccurrences(of:
NSLocalizedString("Interface Name: ", comment: ""),
let bsd = String(self.bsdItem.title)
.replacingOccurrences(of: NSLocalizedString("Interface Name: ", comment: ""),
with: "",
options: .regularExpression,
range: nil))
let routerAddress = NetworkManager.getRouterAddress(matchingMAC: macAddress)
range: nil)
let ipAddress = NetworkManager.getLocalAddress(bsd: bsd)
let routerAddress = NetworkManager.getRouterAddress(bsd: bsd)
let isReachable = NetworkManager.checkConnectionReachability(station: staInfo)
Log.debug(String(format: "current rate=%03d", staInfo.rate))
disconnectName = String(cString: &staInfo.ssid.0)
Expand Down
48 changes: 22 additions & 26 deletions HeliPort/NetworkInfo.swift
Original file line number Diff line number Diff line change
Expand Up @@ -226,33 +226,29 @@ class NetworkManager {
return isReachable && (!needsConnection || canConnectWithoutUserInteraction)
}

// from https://stackoverflow.com/a/53130321
class func getRouterAddress(matchingMAC: String) -> String? {
let cmd = "for i in $(ifconfig -lu); do if ifconfig $i | grep -q \"status: active\" ; then echo $i; fi; done"
let interfaceString = runCommand(cmd)
let interfaceArray = interfaceString.components(separatedBy: "\n")
var addr: String?
for int in interfaceArray.indices {
if interfaceArray[int].hasPrefix("en") {
let mac = NetworkManager.runCommand(
"ifconfig \(interfaceArray[int]) |" +
"grep \"ether \" |" +
"grep -v 00:00:00:00:00:00 |" +
"cut -d\\ -f2"
).trimmingCharacters(in: .whitespacesAndNewlines)
if mac == matchingMAC {
let routerIp = NetworkManager.runCommand("netstat -rn | " +
"awk '$1 ~ /default/' | " +
"awk '$4 ~ /\(interfaceArray[int])/' | " +
"awk 'NR==1' | " +
"awk '{print $2}'"
).replacingOccurrences(of: "gateway:", with: "", options: .regularExpression, range: nil)
.trimmingCharacters(in: .whitespacesAndNewlines)
addr = routerIp
class func getRouterAddress(bsd: String) -> String? {
let dynamicCreate = SCDynamicStoreCreate(kCFAllocatorDefault, "router-ip" as CFString, nil, nil)
let keyIPv4 = "State:/Network/Global/IPv4" as CFString
let keyIPv6 = "State:/Network/Global/IPv6" as CFString
var dictionary: CFPropertyList?
if let ipV4Info = SCDynamicStoreCopyValue(dynamicCreate, keyIPv4) {
dictionary = ipV4Info
} else if let ipV6Info = SCDynamicStoreCopyValue(dynamicCreate, keyIPv6) {
dictionary = ipV6Info
}
if let interface = dictionary?[kSCDynamicStorePropNetPrimaryInterface] as? String {
if interface == bsd {
print("Interface found: \(interface == bsd)")
if let ipRouterAddr = dictionary?["Router"] as? String {
return ipRouterAddr
} else {
print("Could not find router ip")
}
}
}
return addr
} else {
print("Could not find interface")
}
}
return nil
}

// from https://stackoverflow.com/questions/30748480/swift-get-devices-wifi-ip-address/30754194#30754194
Expand Down

0 comments on commit 491c3fe

Please sign in to comment.