diff options
author | Ben Noordhuis <info@bnoordhuis.nl> | 2021-12-23 00:00:43 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-12-23 00:00:43 +0100 |
commit | 1678690c362597422dad682535ee0584d78e79ee (patch) | |
tree | a61ce5c63c4342b0d8e8a31ba8b56df2646d65a2 /runtime/ops | |
parent | 9391ba1098177c9463392eca8e3a725b564d90dc (diff) |
feat(runtime): add op_network_interfaces (#12964)
Add an op to list the network interfaces on the system.
Prep work for #8137 and `os.networkInterfaces()` Node compat in std.
Refs denoland/deno_std#1436.
Diffstat (limited to 'runtime/ops')
-rw-r--r-- | runtime/ops/os.rs | 55 |
1 files changed, 55 insertions, 0 deletions
diff --git a/runtime/ops/os.rs b/runtime/ops/os.rs index 81f8a529e..bbc571cc6 100644 --- a/runtime/ops/os.rs +++ b/runtime/ops/os.rs @@ -25,6 +25,7 @@ pub fn init(maybe_exit_code: Option<Arc<AtomicI32>>) -> Extension { ("op_delete_env", op_sync(op_delete_env)), ("op_hostname", op_sync(op_hostname)), ("op_loadavg", op_sync(op_loadavg)), + ("op_network_interfaces", op_sync(op_network_interfaces)), ("op_os_release", op_sync(op_os_release)), ("op_set_exit_code", op_sync(op_set_exit_code)), ("op_system_memory_info", op_sync(op_system_memory_info)), @@ -149,6 +150,60 @@ fn op_os_release( Ok(release) } +fn op_network_interfaces( + state: &mut OpState, + _: (), + _: (), +) -> Result<Vec<NetworkInterface>, AnyError> { + super::check_unstable(state, "Deno.networkInterfaces"); + state.borrow_mut::<Permissions>().env.check_all()?; + Ok(netif::up()?.map(NetworkInterface::from).collect()) +} + +#[derive(serde::Serialize)] +struct NetworkInterface { + family: &'static str, + name: String, + address: String, + netmask: String, + scopeid: Option<u32>, + cidr: String, + mac: String, +} + +impl From<netif::Interface> for NetworkInterface { + fn from(ifa: netif::Interface) -> Self { + let family = match ifa.address() { + std::net::IpAddr::V4(_) => "IPv4", + std::net::IpAddr::V6(_) => "IPv6", + }; + + let (address, range) = ifa.cidr(); + let cidr = format!("{:?}/{}", address, range); + + let name = ifa.name().to_owned(); + let address = format!("{:?}", ifa.address()); + let netmask = format!("{:?}", ifa.netmask()); + let scopeid = ifa.scope_id(); + + let [b0, b1, b2, b3, b4, b5] = ifa.mac(); + let mac = format!( + "{:02x}:{:02x}:{:02x}:{:02x}:{:02x}:{:02x}", + b0, b1, b2, b3, b4, b5 + ); + + Self { + family, + name, + address, + netmask, + scopeid, + cidr, + mac, + } + } +} + // Copied from sys-info/lib.rs (then tweaked) #[derive(Serialize)] #[serde(rename_all = "camelCase")] |