Skip to content

Commit

Permalink
fix parsed arp flag
Browse files Browse the repository at this point in the history
  • Loading branch information
blesswinsamuel committed Jan 29, 2024
1 parent d1e052c commit 2a5ff44
Showing 1 changed file with 29 additions and 4 deletions.
33 changes: 29 additions & 4 deletions src/arp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,15 @@ impl Arp {
// let router_hostname = router_hostname.to_str().unwrap();
// log::info!("router_hostname: {}", router_hostname);
// let domain_name = router_hostname.split('.').collect::<Vec<_>>()[1..].join(".");
self.update("/proc/net/arp").await
}

async fn update(&self, filename: &str) -> Result<()> {
let f = File::open(filename).await?;

log::debug!("Updating arp metrics");
let domain_name = ".home.local";
let f = File::open("/proc/net/arp").await?;

let mut scanner = BufReader::new(f).lines();
self.registry.arp_devices.clear();
self.registry.hostnames.clear();
Expand All @@ -90,9 +96,7 @@ impl Arp {
host
}
};
let flags = arr[2].to_string();
let flags = format!("{}f", flags.trim_start_matches("0x"));
let flags = i64::from_str_radix(&flags, 16).unwrap_or(-1);
let flags = i64::from_str_radix(arr[2].trim_start_matches("0x"), 16).unwrap_or(-1);
self.registry
.arp_devices
.get_or_create(&ArpDeviceLabels { ip_addr: ip_addr.to_string(), hw_addr: hw_addr.to_string(), device: arr[5].to_string() })
Expand All @@ -106,6 +110,27 @@ impl Arp {
}
}

#[cfg(test)]
mod tests {
// use super::*;
// use std::fs::File;
// use std::io::Write;

#[tokio::test]
async fn test_process_arp() {
// let mut file = File::create("/tmp/test_arp").unwrap();
// writeln!(file, "192.168.1.15 0x1 0x0 00:00:00:00:00:00 * lan").unwrap();
// writeln!(file, "192.168.1.106 0x1 0x2 90:11:95:3e:cf:5d * lan").unwrap();
// let arp = Arp::new();
// arp.update("/tmp/test_arp").await.unwrap();

let flags = "0x1".to_string();
let flags = format!("{}", flags.trim_start_matches("0x"));
let flags = i64::from_str_radix(&flags, 16).unwrap_or(-1);
assert_eq!(flags, 1);
}
}

// IP address HW type Flags HW address Mask Device
// 192.168.1.15 0x1 0x0 00:00:00:00:00:00 * lan
// 192.168.1.106 0x1 0x0 90:11:95:3e:cf:5d * lan

0 comments on commit 2a5ff44

Please sign in to comment.