Skip to content

Commit

Permalink
Add hotspot configuration crate
Browse files Browse the repository at this point in the history
  • Loading branch information
josephrhobbs committed Aug 6, 2024
1 parent 8cd4cea commit ac8a435
Show file tree
Hide file tree
Showing 5 changed files with 112 additions and 4 deletions.
9 changes: 9 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 @@ -2,6 +2,7 @@
resolver = "2"
members = [
"proton_arp",
"proton_cfg",
"proton_dev",
"proton_err",
"proton_mac",
Expand All @@ -27,6 +28,9 @@ features = ["full"]
[dependencies.proton_arp]
path = "proton_arp"

[dependencies.proton_cfg]
path = "proton_cfg"

[dependencies.proton_err]
path = "proton_err"

Expand Down
10 changes: 10 additions & 0 deletions proton_cfg/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[package]
name = "proton_cfg"
version = "0.1.0"
edition = "2021"

[dependencies]
cidr = "0.2.3"

[dependencies.proton_err]
path = "../proton_err"
87 changes: 87 additions & 0 deletions proton_cfg/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
//! Define an abstraction over hotspot configuration data.
use std::net::Ipv4Addr;

use cidr::Ipv4Cidr;

use proton_err::{
ProtonError,
ProtonResult,
};

#[derive(Clone, Debug)]
#[allow(dead_code)]
/// Define a structure holding a hotspot configuration.
pub struct HotspotConfig {
/// SSID of the hotspot.
pub ssid: String,

/// Password of the hotspot.
pub pass: String,

/// Security type of the hotspot.
pub security: String,

/// Frequency band.
pub band: String,

/// IPv4 address of the access point (gateway address).
pub gateway: Ipv4Addr,

/// IPv4 CIDR address range of the network.
pub cidr: Ipv4Cidr,
}

impl From<(String, String, String, String, String, String)> for HotspotConfig {
fn from(config: (String, String, String, String, String, String)) -> Self {
let (ssid, pass, cidr, gateway, security, band) = config;

// Parse CIDR
// Note: it's okay to use `Result::unwrap` here because we pass
// `Ipv4Cidr::new` static arguments.
let cidr = match parse_cidr(&cidr) {
Ok (c) => c,
Err (_) => Ipv4Cidr::new(Ipv4Addr::new(192, 168, 0, 0), 24).unwrap(),
};

// Parse IPv4 gateway
let gateway = match str::parse::<Ipv4Addr>(&gateway) {
Ok (g) => g,
Err (_) => Ipv4Addr::new(192, 168, 0, 1),
};

// Parse band
let band = match band.as_str() {
"2.4" => "bg",
"5" => "a",
_ => "bg", // default to 2.4 GHz
}.to_string();

Self {
ssid,
pass,
security,
gateway,
cidr,
band,
}
}
}

/// Parses an IPv4 CIDR.
fn parse_cidr(cidr: &str) -> ProtonResult<Ipv4Cidr> {
// Split by slash
let mut parts = cidr.split('/');

// Get `Ipv4Addr`
let ipv4 = str::parse::<Ipv4Addr>(
parts.next().ok_or(ProtonError::CouldNotParseAsCidr (cidr.to_string()))?
)?;

// Get network length
let length = str::parse::<u8>(
parts.next().ok_or(ProtonError::CouldNotParseAsCidr (cidr.to_string()))?
)?;

Ok (Ipv4Cidr::new(ipv4, length)?)
}
6 changes: 2 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,8 @@

/// Access point utilities.
pub mod ap {
pub use proton_wap::{
AccessPoint,
HotspotConfig,
};
pub use proton_cfg::HotspotConfig;
pub use proton_wap::AccessPoint;
}

/// CIDR network range structure.
Expand Down

0 comments on commit ac8a435

Please sign in to comment.