///| Read-only status without NetworkManager: interfaces from /proc/net/dev, whether each is a /// physical Ethernet or Wi-Fi device and up from /sys/class/net/, and Wi-Fi link quality from /// /proc/net/wireless. The network's name isn't there (that needs nl80211), so the interface's /// name stands in. Nothing can be changed. ///| One interface's state, as read. pub(all) struct IfState { name : String wireless : Bool physical : Bool up : Bool /// Link quality 0..100 (Wi-Fi), -1 unknown. quality : Int } derive(Eq, Debug) ///| fn read_text(path : String) -> String { @utf8.decode_lossy(@sys.read_proc_bytes(path)).trim(chars=" \n\t").to_string() } ///| Interface names in /proc/net/dev. pub fn proc_net_dev_names(text : String) -> Array[String] { let out : Array[String] = [] for line in text.split("\n") { match line.find(":") { Some(i) => { let name = line[:i].trim(chars=" ").to_string() if name != "" && !name.contains("|") { out.push(name) } } None => () } } out } ///| Link quality per interface from /proc/net/wireless ("wlan0: 0000 54. -56. -256 ..."), /// scaled from the usual 0..70 to 0..100. pub fn proc_net_wireless(text : String) -> Map[String, Int] { let out : Map[String, Int] = {} for line in text.split("\n") { match line.find(":") { Some(i) => { let name = line[:i].trim(chars=" ").to_string() let rest = line[i + 1:].to_string() let fs = rest.split(" ").filter(f => f != "").map(f => f.to_string()).collect() if name != "" && fs.length() >= 2 && !name.contains("|") { let mut q = 0 for c in fs[1] { if c >= '0' && c <= '9' { q = q * 10 + (c.to_int() - 48) } else { break } } out.set(name, if q >= 70 { 100 } else { q * 100 / 70 }) } } None => () } } out } ///| What the interfaces say about the connection: Ethernet up wins, then Wi-Fi up. pub fn sysfs_status(ifs : Array[IfState]) -> NetStatus { let has_wifi = ifs.iter().any(i => i.wireless) let base = { ..NetStatus::offline(), has_wifi, wifi_enabled: has_wifi } for i in ifs { if i.up && i.physical && !i.wireless { return { ..base, state: Wired, name: i.name } } } for i in ifs { if i.up && i.wireless { return { ..base, state: Wifi, name: i.name, signal: if i.quality < 0 { 0 } else { i.quality } } } } base } ///| The read-only backend. pub struct SysfsBackend { mut status : NetStatus mut next : UInt64 } ///| pub fn SysfsBackend::new() -> SysfsBackend { { status: NetStatus::offline(), next: 0UL } } ///| Read the interfaces now. pub fn read_interfaces() -> Array[IfState] { let quality = proc_net_wireless(read_text("/proc/net/wireless")) let out : Array[IfState] = [] for name in proc_net_dev_names(read_text("/proc/net/dev")) { if name == "lo" { continue } let base = "/sys/class/net/" + name let wireless = quality.contains(name) || read_text(base + "/phy80211/name") != "" // Physical devices have a device link (veth, bridges, docker0, tun don't). let physical = read_text(base + "/device/uevent") != "" let oper = read_text(base + "/operstate") let carrier = read_text(base + "/carrier") out.push({ name, wireless, physical, up: oper == "up" || (oper == "unknown" && carrier == "1"), quality: quality.get(name).unwrap_or(-1), }) } out } ///| pub impl NetBackend for SysfsBackend with tick(self, now) { if now >= self.next { self.next = now + 3000UL let s = sysfs_status(read_interfaces()) self.status = { ..s, message: "NetworkManager isn't running: status only" } } } ///| pub impl NetBackend for SysfsBackend with status(self) { self.status } ///| pub impl NetBackend for SysfsBackend with networks(_self) { [] } ///| pub impl NetBackend for SysfsBackend with act(_self, _a) { () } ///| pub impl NetBackend for SysfsBackend with alive(_self) { true }