Skip to content

Commit

Permalink
Merge pull request #52 from swsnr/swsnr/issue50
Browse files Browse the repository at this point in the history
Reverse lookup devices from ARP cache
  • Loading branch information
swsnr authored Dec 19, 2024
2 parents 17eefef + b57997f commit cf6d6dd
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 6 deletions.
1 change: 1 addition & 0 deletions resources/de.swsnr.turnon.metainfo.xml.in
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
</description>
<issues>
<issue url="https://github.com/swsnr/turnon/issues/4">GH-4</issue>
<issue url="https://github.com/swsnr/turnon/issues/50">GH-50</issue>
</issues>
<url>https://github.com/swsnr/turnon/releases/tag/next</url>
</release>
Expand Down
46 changes: 40 additions & 6 deletions src/app/model/device_discovery.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,12 @@ use std::path::Path;

use glib::{dpgettext2, Object};
use gtk::gio;
use gtk::gio::prelude::*;

use crate::config::G_LOG_DOMAIN;
use crate::net::arpcache::*;

use super::Device;
use crate::config::G_LOG_DOMAIN;

glib::wrapper! {
/// Device discovery.
Expand All @@ -30,13 +31,14 @@ mod imp {
use gtk::gio::prelude::*;
use gtk::gio::subclass::prelude::*;

use std::{
cell::{Cell, RefCell},
path::PathBuf,
};
use std::cell::{Cell, RefCell};
use std::path::PathBuf;

use crate::config::G_LOG_DOMAIN;
use crate::net::arpcache::default_arp_cache_path;

use super::resolve_device_host_to_label;
use super::{super::Device, devices_from_arp_cache};
use crate::{config::G_LOG_DOMAIN, net::arpcache::default_arp_cache_path};

#[derive(Debug, glib::Properties)]
#[properties(wrapper_type = super::DeviceDiscovery)]
Expand Down Expand Up @@ -66,6 +68,7 @@ mod imp {
}
}

/// Scan the local ARP cache for devices.
fn scan_devices(&self) {
let discovery = self.obj().clone();
glib::spawn_future_local(async move {
Expand All @@ -83,6 +86,7 @@ mod imp {
0,
n_changed.try_into().unwrap(),
);
discovery.imp().reverse_lookup_devices();
}
}
Err(error) => {
Expand All @@ -91,6 +95,19 @@ mod imp {
}
});
}

/// Reverse-lookup the DNS names of all currently discovered devices.
fn reverse_lookup_devices(&self) {
for device in self.discovered_devices.borrow().iter() {
glib::spawn_future_local(glib::clone!(
#[weak]
device,
async move {
resolve_device_host_to_label(device).await;
}
));
}
}
}

#[glib::object_subclass]
Expand Down Expand Up @@ -131,6 +148,23 @@ mod imp {
}
}

/// Resolve the host of `device` to a DNS name and use it as label.
async fn resolve_device_host_to_label(device: Device) {
if let Some(address) = gio::InetAddress::from_string(&device.host()) {
match gio::Resolver::default()
.lookup_by_address_future(&address)
.await
{
Ok(name) => {
device.set_label(name);
}
Err(error) => {
glib::warn!("Failed to resolve address {address} into DNS name: {error}");
}
}
}
}

/// Read devices from the ARP cache.
///
/// Return an error if opening the ARP cache file failed; otherwise return a
Expand Down

0 comments on commit cf6d6dd

Please sign in to comment.