|
|
|
@ -11,7 +11,7 @@
|
|
|
|
|
//! Switching between client mode and access point mode is achieved by making
|
|
|
|
|
//! system calls to systemd (via `systemctl`). Further networking functionality
|
|
|
|
|
//! is provided by making system calls to retrieve interface state and write
|
|
|
|
|
//! access point credentials to `wpa_supplicant-wlan0.conf`.
|
|
|
|
|
//! access point credentials to `wpa_supplicant-<wlan_iface>.conf`.
|
|
|
|
|
|
|
|
|
|
use std::{
|
|
|
|
|
fs::OpenOptions,
|
|
|
|
@ -478,36 +478,22 @@ pub fn traffic(iface: &str) -> Result<Option<Traffic>, NetworkError> {
|
|
|
|
|
|
|
|
|
|
/* SET - Methods for modifying state */
|
|
|
|
|
|
|
|
|
|
/// Activate wireless access point.
|
|
|
|
|
/// Start network interface service.
|
|
|
|
|
///
|
|
|
|
|
/// A `systemctl `command is invoked which starts the `ap0` interface service.
|
|
|
|
|
/// If the command executes successfully, an `Ok` `Result` type is returned.
|
|
|
|
|
/// In the event of an error, a `NetworkError` is returned in the `Result`.
|
|
|
|
|
pub fn activate_ap() -> Result<(), NetworkError> {
|
|
|
|
|
// start the ap0 interface service
|
|
|
|
|
/// A `systemctl `command is invoked which starts the service for the given
|
|
|
|
|
/// network interface. If the command executes successfully, an `Ok` `Result`
|
|
|
|
|
/// type is returned. In the event of an error, a `NetworkError` is returned
|
|
|
|
|
/// in the `Result`.
|
|
|
|
|
pub fn start_iface_service(iface: String) -> Result<(), NetworkError> {
|
|
|
|
|
let iface_service = format!("wpa_supplicant@{}.service", &iface);
|
|
|
|
|
|
|
|
|
|
// start the interface service
|
|
|
|
|
Command::new("sudo")
|
|
|
|
|
.arg("/usr/bin/systemctl")
|
|
|
|
|
.arg("start")
|
|
|
|
|
.arg("wpa_supplicant@ap0.service")
|
|
|
|
|
.arg(iface_service)
|
|
|
|
|
.output()
|
|
|
|
|
.map_err(NetworkError::StartAp0)?;
|
|
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Activate wireless client.
|
|
|
|
|
///
|
|
|
|
|
/// A `systemctl` command is invoked which starts the `wlan0` interface service.
|
|
|
|
|
/// If the command executes successfully, an `Ok` `Result` type is returned.
|
|
|
|
|
/// In the event of an error, a `NetworkError` is returned in the `Result`.
|
|
|
|
|
pub fn activate_client() -> Result<(), NetworkError> {
|
|
|
|
|
// start the wlan0 interface service
|
|
|
|
|
Command::new("sudo")
|
|
|
|
|
.arg("/usr/bin/systemctl")
|
|
|
|
|
.arg("start")
|
|
|
|
|
.arg("wpa_supplicant@wlan0.service")
|
|
|
|
|
.output()
|
|
|
|
|
.map_err(NetworkError::StartWlan0)?;
|
|
|
|
|
.map_err(|source| NetworkError::StartInterface { source, iface })?;
|
|
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
@ -516,13 +502,15 @@ pub fn activate_client() -> Result<(), NetworkError> {
|
|
|
|
|
///
|
|
|
|
|
/// # Arguments
|
|
|
|
|
///
|
|
|
|
|
/// * `wlan_iface` - A local wireless interface.
|
|
|
|
|
/// * `wifi` - An instance of the `WiFi` `struct` with fields `ssid` and `pass`
|
|
|
|
|
///
|
|
|
|
|
/// If configuration parameters are successfully generated from the provided
|
|
|
|
|
/// SSID and password and appended to `wpa_supplicant-wlan0.conf`, an `Ok`
|
|
|
|
|
/// `Result` type is returned. In the event of an error, a `NetworkError` is
|
|
|
|
|
/// returned in the `Result`.
|
|
|
|
|
pub fn add(wifi: &WiFi) -> Result<(), NetworkError> {
|
|
|
|
|
/// SSID and password and appended to `wpa_supplicant-<wlan_iface>.conf` (where
|
|
|
|
|
/// `<wlan_iface>` is the provided interface parameter), an `Ok` `Result` type
|
|
|
|
|
/// is returned. In the event of an error, a `NetworkError` is returned in the
|
|
|
|
|
/// `Result`.
|
|
|
|
|
pub fn add(wlan_iface: String, wifi: &WiFi) -> Result<(), NetworkError> {
|
|
|
|
|
// generate configuration based on provided ssid & password
|
|
|
|
|
let output = Command::new("wpa_passphrase")
|
|
|
|
|
.arg(&wifi.ssid)
|
|
|
|
@ -538,12 +526,12 @@ pub fn add(wifi: &WiFi) -> Result<(), NetworkError> {
|
|
|
|
|
let mut wpa_details = "\n".as_bytes().to_vec();
|
|
|
|
|
wpa_details.extend(&*(output.stdout));
|
|
|
|
|
|
|
|
|
|
// append wpa_passphrase output to wpa_supplicant-wlan0.conf if successful
|
|
|
|
|
let wlan_config = format!("/etc/wpa_supplicant/wpa_supplicant-{}.conf", wlan_iface);
|
|
|
|
|
|
|
|
|
|
// append wpa_passphrase output to wpa_supplicant-<wlan_iface>.conf if successful
|
|
|
|
|
if output.status.success() {
|
|
|
|
|
// open file in append mode
|
|
|
|
|
let file = OpenOptions::new()
|
|
|
|
|
.append(true)
|
|
|
|
|
.open("/etc/wpa_supplicant/wpa_supplicant-wlan0.conf");
|
|
|
|
|
let file = OpenOptions::new().append(true).open(wlan_config);
|
|
|
|
|
|
|
|
|
|
let _file = match file {
|
|
|
|
|
// if file exists & open succeeds, write wifi configuration
|
|
|
|
@ -563,33 +551,35 @@ pub fn add(wifi: &WiFi) -> Result<(), NetworkError> {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Deploy the access point if the `wlan0` interface is `up` without an active
|
|
|
|
|
/// Deploy an access point if the wireless interface is `up` without an active
|
|
|
|
|
/// connection.
|
|
|
|
|
///
|
|
|
|
|
/// The status of the `wlan0` service and the state of the `wlan0` interface
|
|
|
|
|
/// The status of the wireless service and the state of the wireless interface
|
|
|
|
|
/// are checked. If the service is active but the interface is down (ie. not
|
|
|
|
|
/// currently connected to an access point), then the access point is activated
|
|
|
|
|
/// by calling the `activate_ap()` function.
|
|
|
|
|
pub fn check_iface() -> Result<(), NetworkError> {
|
|
|
|
|
pub fn check_iface(wlan_iface: String, ap_iface: String) -> Result<(), NetworkError> {
|
|
|
|
|
let wpa_service = format!("wpa_supplicant@{}.service", &wlan_iface);
|
|
|
|
|
|
|
|
|
|
// returns 0 if the service is currently active
|
|
|
|
|
let wlan0_status = Command::new("/usr/bin/systemctl")
|
|
|
|
|
let wlan_status = Command::new("/usr/bin/systemctl")
|
|
|
|
|
.arg("is-active")
|
|
|
|
|
.arg("wpa_supplicant@wlan0.service")
|
|
|
|
|
.arg(wpa_service)
|
|
|
|
|
.status()
|
|
|
|
|
.map_err(NetworkError::WlanState)?;
|
|
|
|
|
|
|
|
|
|
// returns the current state of the wlan0 interface
|
|
|
|
|
let iface_state = state("wlan0")?;
|
|
|
|
|
// returns the current state of the wlan interface
|
|
|
|
|
let iface_state = state(&wlan_iface)?;
|
|
|
|
|
|
|
|
|
|
// returns down if the interface is not currently connected to an ap
|
|
|
|
|
let wlan0_state = match iface_state {
|
|
|
|
|
let wlan_state = match iface_state {
|
|
|
|
|
Some(state) => state,
|
|
|
|
|
None => "error".to_string(),
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// if wlan0 is active but not connected, start the ap0 service
|
|
|
|
|
if wlan0_status.success() && wlan0_state == "down" {
|
|
|
|
|
activate_ap()?
|
|
|
|
|
// if wlan is active but not connected, start the ap service
|
|
|
|
|
if wlan_status.success() && wlan_state == "down" {
|
|
|
|
|
start_iface_service(ap_iface)?
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
|