Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ethernet device and configuration support #127

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ tokio-timer = "0.1"
bitflags = "1.0"
ascii = "0.8"
log = "0.3"
bitintr = "0.2.0"

[dependencies.error-chain]
version = "0.11"
Expand Down
38 changes: 38 additions & 0 deletions src/connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,44 @@ where
Ok((connection, state))
}

pub fn set_ethernet_address(
dbus_manager: &Rc<DBusNetworkManager>,
device_path: &str,
interface: &str,
address: Ipv4Addr,
address_netmask_bit_count: u8,
gateway: Ipv4Addr,
dns_addr_1: Ipv4Addr,
dns_addr_2: Ipv4Addr,
dns_search: &str,
method: &str,
connection_name: &str,
) -> Result<(Connection, ConnectionState)>
{
let (path, _) = dbus_manager.set_ethernet_address(
device_path,
interface,
address,
address_netmask_bit_count,
gateway,
dns_addr_1,
dns_addr_2,
dns_search,
method,
connection_name,
)?;

let connection = Connection::init(dbus_manager, &path)?;

let state = wait(
&connection,
&ConnectionState::Activated,
dbus_manager.method_timeout(),
)?;

Ok((connection, state))
}

fn get_connection_active_path(
dbus_manager: &DBusNetworkManager,
connection_path: &str,
Expand Down
58 changes: 58 additions & 0 deletions src/dbus_nm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ use dbus::arg::{Array, Dict, Iter, RefArg, Variant};

use ascii::AsciiStr;

use bitintr::Rev;

use errors::*;
use dbus_api::{extract, path_to_string, DBusApi, VariantTo, variant_iter_to_vec_u8};
use manager::{Connectivity, NetworkManagerState};
Expand Down Expand Up @@ -347,6 +349,62 @@ impl DBusNetworkManager {
))
}

pub fn set_ethernet_address(
&self,
device_path: &str,
interface: &str,
address: Ipv4Addr,
address_netmask_bit_count: u8,
gateway: Ipv4Addr,
dns_addr_1: Ipv4Addr,
dns_addr_2: Ipv4Addr,
dns_search: &str,
method: &str,
connection_name: &str,
) -> Result<(String, String)>
{
let mut connection: VariantMap = HashMap::new();
add_str(&mut connection, "id", connection_name);
add_str(&mut connection, "interface-name", interface);
add_str(&mut connection, "type", "802-3-ethernet");

let mut ipv4: VariantMap = HashMap::new();
add_str(&mut ipv4, "method", method);

if(method == "manual"){
add_str(&mut ipv4, "gateway", format!("{}", gateway));
let mut addr_map: VariantMap = HashMap::new();
let prefix = address_netmask_bit_count as u32;
add_str(&mut addr_map, "address", format!("{}", address));
add_val(&mut addr_map, "prefix", prefix);
add_val(&mut ipv4, "address-data", vec![addr_map]);
}
add_val(&mut ipv4, "dns", vec![u32::from(dns_addr_1).rev(), u32::from(dns_addr_2).rev()]);
add_val(&mut ipv4, "dns-search", vec![dns_search.to_string()]);

let mut settings: HashMap<String, VariantMap> = HashMap::new();
settings.insert("connection".to_string(), connection);
settings.insert("ipv4".to_string(), ipv4);

let response = self.dbus.call_with_args(
NM_SERVICE_PATH,
NM_SERVICE_INTERFACE,
"AddAndActivateConnection",
&[
&settings as &RefArg,
&Path::new(device_path)? as &RefArg,
&Path::new("/")? as &RefArg,
],
)?;

let (conn_path, active_connection): (Path, Path) = self.dbus.extract_two(&response)?;

Ok((
path_to_string(&conn_path)?,
path_to_string(&active_connection)?,
))
}

pub fn get_devices(&self) -> Result<Vec<String>> {
self.dbus
.property(NM_SERVICE_PATH, NM_SERVICE_INTERFACE, "Devices")
Expand Down
9 changes: 9 additions & 0 deletions src/device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use errors::*;
use dbus_nm::DBusNetworkManager;

use wifi::{new_wifi_device, WiFiDevice};
use ethernet::{new_ethernet_device, EthernetDevice};

#[derive(Clone)]
pub struct Device {
Expand Down Expand Up @@ -48,6 +49,14 @@ impl Device {
}
}

pub fn as_ethernet_device(&self) -> Option<EthernetDevice> {
if self.device_type == DeviceType::Ethernet {
Some(new_ethernet_device(&self.dbus_manager, self))
} else {
None
}
}

/// Connects a Network Manager device.
///
/// Examples
Expand Down
52 changes: 52 additions & 0 deletions src/ethernet.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
use std::rc::Rc;
use std::net::Ipv4Addr;

use errors::*;
use dbus_nm::DBusNetworkManager;

use connection::{Connection, ConnectionState, set_ethernet_address};
use device::{Device, PathGetter};

pub struct EthernetDevice<'a> {
dbus_manager: Rc<DBusNetworkManager>,
device: &'a Device,
}

impl<'a> EthernetDevice<'a> {
pub fn set_ethernet_address(
&self,
address: Ipv4Addr,
address_netmask_bit_count: u8,
gateway: Ipv4Addr,
dns_addr_1: Ipv4Addr,
dns_addr_2: Ipv4Addr,
dns_search: &str,
method: &str,
connection_name: &str,
) -> Result<(Connection, ConnectionState)>
{
set_ethernet_address(
&self.dbus_manager,
self.device.path(),
self.device.interface(),
address,
address_netmask_bit_count,
gateway,
dns_addr_1,
dns_addr_2,
dns_search,
method,
connection_name,
)
}
}

pub fn new_ethernet_device<'a>(
dbus_manager: &Rc<DBusNetworkManager>,
device: &'a Device,
) -> EthernetDevice<'a> {
EthernetDevice {
dbus_manager: Rc::clone(dbus_manager),
device: device,
}
}
3 changes: 3 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ extern crate dbus;

extern crate ascii;

extern crate bitintr;

pub mod errors;

mod dbus_nm;
Expand All @@ -25,6 +27,7 @@ mod service;
mod connection;
mod device;
mod wifi;
mod ethernet;
mod ssid;

pub use manager::{Connectivity, NetworkManager};
Expand Down