Skip to content

Commit

Permalink
feat(ext/net): add op_network_interfaces
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
bnoordhuis committed Dec 2, 2021
1 parent 14f4102 commit fe5a91a
Show file tree
Hide file tree
Showing 7 changed files with 76 additions and 0 deletions.
9 changes: 9 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions ext/net/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ path = "lib.rs"
deno_core = { version = "0.109.0", path = "../../core" }
deno_tls = { version = "0.14.0", path = "../tls" }
log = "0.4.14"
netif = { git = "https://github.com/bnoordhuis/netif" }
serde = { version = "1.0.129", features = ["derive"] }
tokio = { version = "1.10.1", features = ["full"] }
trust-dns-proto = "0.20.3"
Expand Down
1 change: 1 addition & 0 deletions ext/net/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,4 @@ Following ops are provided:
- "op_tls_listen"
- "op_tls_accept"
- "op_tls_handshake"
- "op_network_interfaces"
1 change: 1 addition & 0 deletions ext/net/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ pub trait NetPermissions {
) -> Result<(), AnyError>;
fn check_read(&mut self, _p: &Path) -> Result<(), AnyError>;
fn check_write(&mut self, _p: &Path) -> Result<(), AnyError>;
fn check_network_interfaces(&mut self) -> Result<(), AnyError>;
}

/// `UnstableChecker` is a struct so it can be placed inside `GothamState`;
Expand Down
54 changes: 54 additions & 0 deletions ext/net/ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ pub fn init<P: NetPermissions + 'static>() -> Vec<OpPair> {
("op_dgram_recv", op_async(op_dgram_recv)),
("op_dgram_send", op_async(op_dgram_send::<P>)),
("op_dns_resolve", op_async(op_dns_resolve::<P>)),
("op_network_interfaces", op_sync(op_network_interfaces::<P>)),
]
}

Expand Down Expand Up @@ -709,6 +710,59 @@ fn rdata_to_return_record(
}
}

fn op_network_interfaces<NP>(
state: &mut OpState,
_: (),
_: (),
) -> Result<Vec<NetworkInterface>, AnyError>
where
NP: NetPermissions + 'static,
{
state.borrow_mut::<NP>().check_network_interfaces()?;
Ok(netif::all()?.map(NetworkInterface::from).collect())
}

#[derive(serde::Serialize)]
struct NetworkInterface {
family: &'static str,
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 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,
address,
netmask,
scopeid,
cidr,
mac,
}
}
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down
6 changes: 6 additions & 0 deletions runtime/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,12 @@ mod not_docs {
) -> Result<(), deno_core::error::AnyError> {
unreachable!("snapshotting!")
}

fn check_network_interfaces(
&mut self,
) -> Result<(), deno_core::error::AnyError> {
unreachable!("snapshotting!")
}
}

fn create_runtime_snapshot(snapshot_path: &Path, files: Vec<PathBuf>) {
Expand Down
4 changes: 4 additions & 0 deletions runtime/permissions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1285,6 +1285,10 @@ impl deno_net::NetPermissions for Permissions {
fn check_write(&mut self, path: &Path) -> Result<(), AnyError> {
self.write.check(path)
}

fn check_network_interfaces(&mut self) -> Result<(), AnyError> {
Ok(()) // TODO(bnoordhuis)
}
}

impl deno_fetch::FetchPermissions for Permissions {
Expand Down

0 comments on commit fe5a91a

Please sign in to comment.