From cfa667e831dd54fd78c0e21003b737eac4e700ba Mon Sep 17 00:00:00 2001 From: Stjin Date: Sat, 28 May 2022 11:06:19 +0200 Subject: [PATCH] Merged commit #132, Updated dbus crate --- Cargo.toml | 2 +- src/dbus_api.rs | 55 +++++++++++++------------ src/dbus_nm.rs | 107 +++++++++++++++++++++++++----------------------- src/errors.rs | 2 +- src/service.rs | 31 ++++++++------ 5 files changed, 105 insertions(+), 92 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 792df85..604659d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -10,7 +10,7 @@ categories = ["api-bindings", "network-programming", "os::unix-apis"] license = "Apache-2.0" [dependencies] -dbus = "0.5" +dbus = "0.7" futures = "0.1" futures-cpupool = "0.1" tokio-timer = "0.1" diff --git a/src/dbus_api.rs b/src/dbus_api.rs index 73017e1..6e954ad 100644 --- a/src/dbus_api.rs +++ b/src/dbus_api.rs @@ -1,7 +1,7 @@ -use dbus::Connection as DBusConnection; -use dbus::{BusType, ConnPath, Message, Path}; use dbus::arg::{Array, Get, Iter, RefArg, Variant}; -use dbus::stdintf::OrgFreedesktopDBusProperties; +use dbus::ffidisp::stdintf::OrgFreedesktopDBusProperties; +use dbus::ffidisp::{BusType, ConnPath, Connection as DBusConnection}; +use dbus::{Message, Path}; use errors::*; @@ -46,7 +46,7 @@ impl DBusApi { path: &str, interface: &str, method: &str, - args: &[&RefArg], + args: &[&dyn RefArg], ) -> Result { self.call_with_args_retry(path, interface, method, args) .map_err(|e| { @@ -61,7 +61,7 @@ impl DBusApi { path: &str, interface: &str, method: &str, - args: &[&RefArg], + args: &[&dyn RefArg], ) -> Result { let mut retries = 0; @@ -93,7 +93,7 @@ impl DBusApi { path: &str, interface: &str, method: &str, - args: &[&RefArg], + args: &[&dyn RefArg], ) -> Option> { match Message::new_method_call(self.base, path, interface, method) { Ok(mut message) => { @@ -102,13 +102,14 @@ impl DBusApi { } self.send_message_checked(message) - }, + } Err(details) => Some(Err(ErrorKind::DBusAPI(details).into())), } } fn send_message_checked(&self, message: Message) -> Option> { - match self.connection + match self + .connection .send_with_reply_and_block(message, self.method_timeout as i32 * 1000) { Ok(response) => Some(Ok(response)), @@ -125,13 +126,13 @@ impl DBusApi { } Some(Err(Error::from(e))) - }, + } } } pub fn property(&self, path: &str, interface: &str, name: &str) -> Result - where - DBusApi: VariantTo, + where + DBusApi: VariantTo, { let property_error = |details: &str, err: bool| { let message = format!( @@ -159,13 +160,13 @@ impl DBusApi { None => property_error("no details", false), }; Err(e).chain_err(|| dbus_err) - }, + } } } pub fn extract<'a, T>(&self, response: &'a Message) -> Result - where - T: Get<'a>, + where + T: Get<'a>, { response .get1() @@ -173,9 +174,9 @@ impl DBusApi { } pub fn extract_two<'a, T1, T2>(&self, response: &'a Message) -> Result<(T1, T2)> - where - T1: Get<'a>, - T2: Get<'a>, + where + T1: Get<'a>, + T2: Get<'a>, { let (first, second) = response.get2(); @@ -195,35 +196,35 @@ impl DBusApi { } pub trait VariantTo { - fn variant_to(value: &Variant>) -> Option; + fn variant_to(value: &Variant>) -> Option; } impl VariantTo for DBusApi { - fn variant_to(value: &Variant>) -> Option { + fn variant_to(value: &Variant>) -> Option { value.0.as_str().and_then(|v| Some(v.to_string())) } } impl VariantTo for DBusApi { - fn variant_to(value: &Variant>) -> Option { + fn variant_to(value: &Variant>) -> Option { value.0.as_i64() } } impl VariantTo for DBusApi { - fn variant_to(value: &Variant>) -> Option { + fn variant_to(value: &Variant>) -> Option { value.0.as_i64().and_then(|v| Some(v as u32)) } } impl VariantTo for DBusApi { - fn variant_to(value: &Variant>) -> Option { + fn variant_to(value: &Variant>) -> Option { value.0.as_i64().and_then(|v| Some(v == 0)) } } impl VariantTo> for DBusApi { - fn variant_to(value: &Variant>) -> Option> { + fn variant_to(value: &Variant>) -> Option> { let mut result = Vec::new(); if let Some(list) = value.0.as_iter() { @@ -243,7 +244,7 @@ impl VariantTo> for DBusApi { } impl VariantTo> for DBusApi { - fn variant_to(value: &Variant>) -> Option> { + fn variant_to(value: &Variant>) -> Option> { let mut result = Vec::new(); if let Some(list) = value.0.as_iter() { @@ -263,8 +264,8 @@ impl VariantTo> for DBusApi { } pub fn extract<'a, T>(var: &mut Variant>) -> Result -where - T: Get<'a>, + where + T: Get<'a>, { var.0 .get::() @@ -293,4 +294,4 @@ pub fn path_to_string(path: &Path) -> Result { path ))) } -} +} \ No newline at end of file diff --git a/src/dbus_nm.rs b/src/dbus_nm.rs index a89bf5b..08ef210 100644 --- a/src/dbus_nm.rs +++ b/src/dbus_nm.rs @@ -1,20 +1,20 @@ use std::collections::HashMap; use std::net::Ipv4Addr; -use dbus::Path; use dbus::arg::{Array, Dict, Iter, RefArg, Variant}; +use dbus::Path; use ascii::AsciiStr; +use connection::{ConnectionSettings, ConnectionState}; +use dbus_api::{extract, path_to_string, variant_iter_to_vec_u8, DBusApi, VariantTo}; +use device::{DeviceState, DeviceType}; use errors::*; -use dbus_api::{extract, path_to_string, DBusApi, VariantTo, variant_iter_to_vec_u8}; use manager::{Connectivity, NetworkManagerState}; -use connection::{ConnectionSettings, ConnectionState}; use ssid::{AsSsidSlice, Ssid}; -use device::{DeviceState, DeviceType}; use wifi::{AccessPoint, AccessPointCredentials, NM80211ApFlags, NM80211ApSecurityFlags}; -type VariantMap = HashMap>>; +type VariantMap = HashMap>>; const NM_SERVICE_MANAGER: &str = "org.freedesktop.NetworkManager"; @@ -51,7 +51,8 @@ impl DBusNetworkManager { } pub fn get_state(&self) -> Result { - let response = self.dbus + let response = self + .dbus .call(NM_SERVICE_PATH, NM_SERVICE_INTERFACE, "state")?; let state: u32 = self.dbus.extract(&response)?; @@ -60,8 +61,9 @@ impl DBusNetworkManager { } pub fn check_connectivity(&self) -> Result { - let response = self.dbus - .call(NM_SERVICE_PATH, NM_SERVICE_INTERFACE, "CheckConnectivity")?; + let response = + self.dbus + .call(NM_SERVICE_PATH, NM_SERVICE_INTERFACE, "CheckConnectivity")?; let connectivity: u32 = self.dbus.extract(&response)?; @@ -79,8 +81,9 @@ impl DBusNetworkManager { } pub fn list_connections(&self) -> Result> { - let response = self.dbus - .call(NM_SETTINGS_PATH, NM_SETTINGS_INTERFACE, "ListConnections")?; + let response = + self.dbus + .call(NM_SETTINGS_PATH, NM_SETTINGS_INTERFACE, "ListConnections")?; let array: Array = self.dbus.extract(&response)?; @@ -108,7 +111,8 @@ impl DBusNetworkManager { } pub fn get_connection_settings(&self, path: &str) -> Result { - let response = self.dbus + let response = self + .dbus .call(path, NM_CONNECTION_INTERFACE, "GetSettings")?; let dict: Dict<&str, Dict<&str, Variant, _>, _> = self.dbus.extract(&response)?; @@ -124,20 +128,20 @@ impl DBusNetworkManager { match k2 { "id" => { id = extract::(&mut v2)?; - }, + } "uuid" => { uuid = extract::(&mut v2)?; - }, + } "type" => { kind = extract::(&mut v2)?; - }, + } "ssid" => { ssid = Ssid::from_bytes(variant_iter_to_vec_u8(&mut v2)?)?; - }, + } "mode" => { mode = extract::(&mut v2)?; - }, - _ => {}, + } + _ => {} } } } @@ -167,9 +171,9 @@ impl DBusNetworkManager { NM_SERVICE_INTERFACE, "ActivateConnection", &[ - &Path::new(path)? as &RefArg, - &Path::new("/")? as &RefArg, - &Path::new("/")? as &RefArg, + &Path::new(path)? as &dyn RefArg, + &Path::new("/")? as &dyn RefArg, + &Path::new("/")? as &dyn RefArg, ], )?; @@ -181,7 +185,7 @@ impl DBusNetworkManager { NM_SERVICE_PATH, NM_SERVICE_INTERFACE, "DeactivateConnection", - &[&Path::new(path)? as &RefArg], + &[&Path::new(path)? as &dyn RefArg], )?; Ok(()) @@ -219,7 +223,7 @@ impl DBusNetworkManager { ); settings.insert("802-11-wireless-security".to_string(), security_settings); - }, + } AccessPointCredentials::Wpa { ref passphrase } => { let mut security_settings: VariantMap = HashMap::new(); @@ -231,7 +235,7 @@ impl DBusNetworkManager { ); settings.insert("802-11-wireless-security".to_string(), security_settings); - }, + } AccessPointCredentials::Enterprise { ref identity, ref passphrase, @@ -248,8 +252,8 @@ impl DBusNetworkManager { settings.insert("802-11-wireless-security".to_string(), security_settings); settings.insert("802-1x".to_string(), eap); - }, - AccessPointCredentials::None => {}, + } + AccessPointCredentials::None => {} }; let response = self.dbus.call_with_args( @@ -257,9 +261,9 @@ impl DBusNetworkManager { NM_SERVICE_INTERFACE, "AddAndActivateConnection", &[ - &settings as &RefArg, - &Path::new(device_path.to_string())? as &RefArg, - &Path::new(access_point.path.to_string())? as &RefArg, + &settings as &dyn RefArg, + &Path::new(device_path.to_string())? as &dyn RefArg, + &Path::new(access_point.path.to_string())? as &dyn RefArg, ], )?; @@ -279,8 +283,8 @@ impl DBusNetworkManager { password: Option<&str>, address: Option, ) -> Result<(String, String)> - where - T: AsSsidSlice + ?Sized, + where + T: AsSsidSlice + ?Sized, { let ssid = ssid.as_ssid_slice()?; let ssid_vec = ssid.as_bytes().to_vec(); @@ -333,9 +337,9 @@ impl DBusNetworkManager { NM_SERVICE_INTERFACE, "AddAndActivateConnection", &[ - &settings as &RefArg, - &Path::new(device_path)? as &RefArg, - &Path::new("/")? as &RefArg, + &settings as &dyn RefArg, + &Path::new(device_path)? as &dyn RefArg, + &Path::new("/")? as &dyn RefArg, ], )?; @@ -357,7 +361,7 @@ impl DBusNetworkManager { NM_SERVICE_PATH, NM_SERVICE_INTERFACE, "GetDeviceByIpIface", - &[&interface.to_string() as &RefArg], + &[&interface.to_string() as &dyn RefArg], )?; let path: Path = self.dbus.extract(&response)?; @@ -383,9 +387,9 @@ impl DBusNetworkManager { NM_SERVICE_INTERFACE, "ActivateConnection", &[ - &Path::new("/")? as &RefArg, - &Path::new(path)? as &RefArg, - &Path::new("/")? as &RefArg, + &Path::new("/")? as &dyn RefArg, + &Path::new(path)? as &dyn RefArg, + &Path::new("/")? as &dyn RefArg, ], )?; @@ -404,7 +408,7 @@ impl DBusNetworkManager { path, NM_WIRELESS_INTERFACE, "RequestScan", - &[&options as &RefArg], + &[&options as &dyn RefArg], )?; Ok(()) @@ -416,7 +420,8 @@ impl DBusNetworkManager { } pub fn get_access_point_ssid(&self, path: &str) -> Option { - if let Ok(ssid_vec) = self.dbus + if let Ok(ssid_vec) = self + .dbus .property::>(path, NM_ACCESS_POINT_INTERFACE, "Ssid") { Ssid::from_bytes(ssid_vec).ok() @@ -446,19 +451,19 @@ impl DBusNetworkManager { } impl VariantTo for DBusApi { - fn variant_to(value: &Variant>) -> Option { + fn variant_to(value: &Variant>) -> Option { value.0.as_i64().map(DeviceType::from) } } impl VariantTo for DBusApi { - fn variant_to(value: &Variant>) -> Option { + fn variant_to(value: &Variant>) -> Option { value.0.as_i64().map(DeviceState::from) } } impl VariantTo for DBusApi { - fn variant_to(value: &Variant>) -> Option { + fn variant_to(value: &Variant>) -> Option { value .0 .as_i64() @@ -467,7 +472,7 @@ impl VariantTo for DBusApi { } impl VariantTo for DBusApi { - fn variant_to(value: &Variant>) -> Option { + fn variant_to(value: &Variant>) -> Option { value .0 .as_i64() @@ -476,17 +481,17 @@ impl VariantTo for DBusApi { } pub fn add_val(map: &mut VariantMap, key: K, value: V) -where - K: Into, - V: RefArg + 'static, + where + K: Into, + V: RefArg + 'static, { map.insert(key.into(), Variant(Box::new(value))); } pub fn add_str(map: &mut VariantMap, key: K, value: V) -where - K: Into, - V: Into, + where + K: Into, + V: Into, { map.insert(key.into(), Variant(Box::new(value.into()))); } @@ -508,6 +513,6 @@ fn verify_ascii_password(password: &str) -> Result<&str> { } else { Ok(password) } - }, + } } -} +} \ No newline at end of file diff --git a/src/errors.rs b/src/errors.rs index f878e0d..f44548e 100644 --- a/src/errors.rs +++ b/src/errors.rs @@ -1,4 +1,4 @@ -error_chain!{ +error_chain! { foreign_links { Ascii(::ascii::AsAsciiStrError); Utf8(::std::str::Utf8Error); diff --git a/src/service.rs b/src/service.rs index 4f7f094..30b2031 100644 --- a/src/service.rs +++ b/src/service.rs @@ -3,13 +3,15 @@ extern crate futures; extern crate futures_cpupool; extern crate tokio_timer; -use std::str::FromStr; -use std::time::Duration; -use self::dbus::{BusType, Connection, ConnectionItem, Interface, Member, Message, Path, Props}; use self::dbus::arg::{Dict, Iter, Variant}; +use self::dbus::ffidisp::{BusType, Connection, ConnectionItem}; +use self::dbus::strings::{Interface, Member}; +use self::dbus::{arg::messageitem::Props, Message, Path}; use self::futures::Future; use self::futures_cpupool::CpuPool; use self::tokio_timer::Timer; +use std::str::FromStr; +use std::time::Duration; use errors::*; @@ -30,7 +32,8 @@ pub fn start_service(timeout: u64) -> Result { SD_SERVICE_PATH, SD_MANAGER_INTERFACE, "StartUnit", - ).map_err(|_| ErrorKind::Service)? + ) + .map_err(|_| ErrorKind::Service)? .append2("NetworkManager.service", "fail"); let connection = @@ -41,7 +44,7 @@ pub fn start_service(timeout: u64) -> Result { .map_err(|_| ErrorKind::Service)?; handler(timeout, ServiceState::Active) - }, + } } } @@ -57,7 +60,8 @@ pub fn stop_service(timeout: u64) -> Result { SD_SERVICE_PATH, SD_MANAGER_INTERFACE, "StopUnit", - ).map_err(|_| ErrorKind::Service)? + ) + .map_err(|_| ErrorKind::Service)? .append2("NetworkManager.service", "fail"); let connection = @@ -68,7 +72,7 @@ pub fn stop_service(timeout: u64) -> Result { .map_err(|_| ErrorKind::Service)?; handler(timeout, ServiceState::Inactive) - }, + } } } @@ -78,7 +82,8 @@ pub fn get_service_state() -> Result { SD_SERVICE_PATH, SD_MANAGER_INTERFACE, "GetUnit", - ).map_err(|_| ErrorKind::Service)? + ) + .map_err(|_| ErrorKind::Service)? .append1("NetworkManager.service"); let connection = Connection::get_private(BusType::System).map_err(|_| ErrorKind::Service)?; @@ -95,7 +100,8 @@ pub fn get_service_state() -> Result { path, SD_UNIT_INTERFACE, 2000, - ).get("ActiveState") + ) + .get("ActiveState") .map_err(|_| ErrorKind::Service)?; response @@ -115,7 +121,8 @@ fn handler(timeout: u64, target_state: ServiceState) -> Result { .then(|_| bail!(ErrorKind::Service)); let process = CpuPool::new_num_cpus().spawn_fn(|| { - let connection = Connection::get_private(BusType::System).map_err(|_| ErrorKind::Service)?; + let connection = + Connection::get_private(BusType::System).map_err(|_| ErrorKind::Service)?; connection .add_match( "type='signal', sender='org.freedesktop.systemd1', \ @@ -140,7 +147,7 @@ fn handler(timeout: u64, target_state: ServiceState) -> Result { != Interface::from("org.freedesktop.DBus.Properties") || response.member().ok_or(ErrorKind::Service)? != Member::from("PropertiesChanged") || response.path().ok_or(ErrorKind::Service)? - != Path::from("/org/freedesktop/systemd1/unit/NetworkManager_2eservice") + != Path::from("/org/freedesktop/systemd1/unit/NetworkManager_2eservice") { continue; } @@ -193,4 +200,4 @@ impl FromStr for ServiceState { _ => bail!(ErrorKind::Service), } } -} +} \ No newline at end of file