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

Nameserver information to network block #2058

Merged
merged 8 commits into from
Jun 19, 2024
3 changes: 2 additions & 1 deletion cspell.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
version: '0.2'
version: "0.2"
language: en-US, en-GB
allowCompoundWords: true
ignorePaths:
Expand Down Expand Up @@ -124,6 +124,7 @@ words:
- recip
- repr
- reqwest
- resolv
- retval
- rofi
- rofication
Expand Down
9 changes: 9 additions & 0 deletions src/blocks/net.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
//! `bitrate` | WiFi connection bitrate | Number | Bits per second
//! `ip` | IPv4 address of the iface | Text | -
//! `ipv6` | IPv6 address of the iface | Text | -
//! `nameserver` | Nameserver | Text | -
//!
//! # Example
//!
Expand Down Expand Up @@ -61,6 +62,7 @@
use super::prelude::*;
use crate::netlink::NetDevice;
use crate::util;
use itertools::Itertools;
use regex::Regex;
use std::time::Instant;

Expand Down Expand Up @@ -159,6 +161,13 @@ pub async fn run(config: &Config, api: &CommonApi) -> Result<()> {
[if let Some(v) = device.frequency()] "frequency" => Value::hertz(v),
[if let Some(v) = device.bitrate()] "bitrate" => Value::bits(v),
[if let Some(v) = device.signal()] "signal_strength" => Value::percents(v),
[if !device.nameservers.is_empty()] "nameserver" => Value::text(
device
.nameservers
.into_iter()
.map(|s| s.to_string())
.join(" "),
),
"device" => Value::text(device.iface.name),
});

Expand Down
25 changes: 24 additions & 1 deletion src/netlink.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use regex::Regex;

use libc::c_uchar;

use std::net::{Ipv4Addr, Ipv6Addr};
use std::net::{IpAddr, Ipv4Addr, Ipv6Addr};
use std::ops;
use std::path::Path;

Expand All @@ -27,6 +27,7 @@ pub struct NetDevice {
pub ipv6: Option<Ipv6Addr>,
pub icon: &'static str,
pub tun_wg_ppp: bool,
pub nameservers: Vec<IpAddr>,
}

#[derive(Debug, Default)]
Expand Down Expand Up @@ -67,6 +68,9 @@ impl NetDevice {
let wifi_info = WifiInfo::new(iface.index).await?;
let ip = ipv4(&mut sock, iface.index).await?;
let ipv6 = ipv6(&mut sock, iface.index).await?;
let nameservers = read_nameservers()
.await
.error("Failed to read nameservers")?;

// TODO: use netlink for the these too
// I don't believe that this should ever change, so set it now:
Expand Down Expand Up @@ -97,6 +101,7 @@ impl NetDevice {
ipv6,
icon,
tun_wg_ppp: tun | wg | ppp,
nameservers,
}))
}

Expand Down Expand Up @@ -428,6 +433,24 @@ async fn ipv6(sock: &mut NlSocket, ifa_index: i32) -> Result<Option<Ipv6Addr>> {
.map(Ipv6Addr::from))
}

async fn read_nameservers() -> Result<Vec<IpAddr>> {
let file = util::read_file("/etc/resolv.conf")
.await
.error("Failed to read /etc/resolv.conf")?;
let mut nameservers = Vec::new();

for line in file.lines() {
let mut line_parts = line.split_whitespace();
if line_parts.next() == Some("nameserver") {
if let Some(ip) = line_parts.next() {
nameservers.push(ip.parse().error("Unable to parse ip")?);
}
}
}

Ok(nameservers)
}

// Source: https://www.kernel.org/doc/Documentation/networking/operstates.txt
#[derive(Debug, PartialEq, Eq)]
pub enum Operstate {
Expand Down