Skip to content

Commit

Permalink
Add error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
josephrhobbs committed Jul 26, 2024
1 parent 6742505 commit acaf1cf
Show file tree
Hide file tree
Showing 10 changed files with 116 additions and 2 deletions.
7 changes: 7 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ resolver = "2"
members = [
"proton_arp",
"proton_dev",
"proton_err",
"proton_mac",
"proton_nif",
"proton_wap",
Expand All @@ -26,6 +27,9 @@ features = ["full"]
[dependencies.proton_arp]
path = "proton_arp"

[dependencies.proton_err]
path = "proton_err"

[dependencies.proton_dev]
path = "proton_dev"

Expand Down
17 changes: 16 additions & 1 deletion proton_dev/src/socket.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,15 @@ pub trait NetworkSocket {
/// # Returns
/// `NetlinkResult<Vec<Station>>` containing a list of network stations.
fn get_all_stations(&mut self, nlif_index: &[u8]) -> NetlinkResult<Vec<Station>>;

/// Deauthenticate a device from this AP by MAC address.
///
/// # Parameters
/// TODO
///
/// # Returns
/// TODO
fn deauthenticate_by_mac(&mut self) -> ();
}

impl NetworkSocket for Socket {
Expand All @@ -42,7 +51,7 @@ impl NetworkSocket for Socket {
// Get the Netlink socket
let nl80211sock = &mut self.sock;

// What does this do?
// Set Generic Netlink attributes
let mut attrs: Vec<Nlattr<Nl80211Attr, Vec<u8>>> = vec![];
let new_attr = Nlattr::new(
None,
Expand Down Expand Up @@ -99,4 +108,10 @@ impl NetworkSocket for Socket {

Ok(results)
}

fn deauthenticate_by_mac(
&mut self,
) -> () {
todo!()
}
}
10 changes: 10 additions & 0 deletions proton_err/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[package]
name = "proton_err"
version = "0.1.0"
edition = "2021"

[lib]
name = "proton_err"
path = "src/lib.rs"

[dependencies]
50 changes: 50 additions & 0 deletions proton_err/src/error.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
//! Enumeration of Proton AP management errors.

use std::{
error::Error,
fmt::{
Display,
Debug,
Formatter,
Result,
},
};

#[derive(Debug)]
/// An error that occurred within the Proton library.
///
/// Note: this enumeration is exhaustive because of the `Other` variant.
pub enum ProtonError<T>
where T: Display + Debug
{
/// An error that could not be converted to a native error.
Other (T)
}

impl<T> Display for ProtonError<T>
where T: Display + Debug
{
fn fmt(&self, f: &mut Formatter<'_>) -> Result {
use ProtonError::*;
let error = match self {
Other (t) => t.to_string(),
};

write!(f, "{}", error)
}
}

impl<T> Error for ProtonError<T>
where T: Display + Debug { }

impl From<Box<dyn Error>> for ProtonError<String> {
fn from(e: Box<dyn Error>) -> ProtonError<String> {
let string = if let Some (err) = e.source() {
err.to_string()
} else {
String::new()
};

ProtonError::<String>::Other (string)
}
}
8 changes: 8 additions & 0 deletions proton_err/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
//! Error handling for the Proton access point management library.

#![deny(warnings)]
#![deny(missing_docs)]

mod error;

pub use error::ProtonError;
6 changes: 6 additions & 0 deletions proton_wap/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@ version = "0.35.0"
version = "1"
features = ["full"]

[dependencies.proton_dev]
path = "../proton_dev"

[dependencies.proton_err]
path = "../proton_err"

[dependencies.proton_mac]
path = "../proton_mac"

Expand Down
6 changes: 6 additions & 0 deletions proton_wap/src/ap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,18 @@

use cidr::Ipv4Cidr;

use proton_dev::DeviceManager;

use crate::AccessPointResult;

#[allow(dead_code)]
/// A wireless access point.
pub struct AccessPoint {
/// CIDR network range.
range: Ipv4Cidr,

/// Device manager.
manager: DeviceManager,
}

impl AccessPoint {
Expand All @@ -25,6 +30,7 @@ impl AccessPoint {
) -> Self {
Self {
range,
manager: DeviceManager::new(range).expect("could not open Netlink socket"), // TODO
}
}

Expand Down
Empty file added proton_wap/src/error.rs
Empty file.
10 changes: 9 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,12 @@
#![deny(warnings)]
#![deny(missing_docs)]

pub use proton_wap::AccessPoint;
/// Proton error handling functionality.
pub mod error {
pub use proton_err::ProtonError;
}

/// Proton access point utilities.
pub mod ap {
pub use proton_wap::AccessPoint;
}

0 comments on commit acaf1cf

Please sign in to comment.