Skip to content

Commit

Permalink
Replace bespoke netlink impl with one based on the netlink-* crates
Browse files Browse the repository at this point in the history
  • Loading branch information
ohadravid committed Nov 23, 2024
1 parent 218629b commit 4c06245
Show file tree
Hide file tree
Showing 9 changed files with 127 additions and 276 deletions.
9 changes: 7 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "netstat2"
version = "0.10.1"
version = "0.11.0"
authors = ["Ohad Ravid <[email protected]>", "ivxvm <[email protected]>"]
edition = "2021"
license = "MIT OR Apache-2.0"
Expand All @@ -14,7 +14,6 @@ Cross-platform library to retrieve network sockets information.
"""

[dependencies]
libc = "0.2"
bitflags = "2"
thiserror = "2"

Expand All @@ -32,3 +31,9 @@ bindgen = "0.65"
num-derive = "0.3"
num-traits = "0.2.8"
byteorder = "1.3.2"

[target.'cfg(any(target_os = "linux", target_os = "android"))'.dependencies]
netlink-sys = "0.8"
netlink-packet-core = "0.7"
netlink-packet-utils = "0.5"
netlink-packet-sock-diag = "0.4"
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ Provides unified interface and returns data structures which may have additional
```toml
# Cargo.toml
[dependencies]
netstat2 = "0.10"
netstat2 = "0.11"
```

This is a fork based on the [netstat](https://crates.io/crates/netstat) crate by [ivxvm](https://github.com/ivxvm).
Expand Down
24 changes: 0 additions & 24 deletions build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,30 +27,6 @@ fn main() {
.write_to_file(output_path)
.expect("Failed to write libproc bindings");
}
"linux" | "android" => {
let bindings = bindgen::builder()
.header_contents(
"linux_bindings.h",
r#"
#include <linux/sock_diag.h>
#include <linux/inet_diag.h>
#include <linux/rtnetlink.h>
#include <linux/netlink.h>
#include <linux/tcp.h>
"#,
)
.layout_tests(false)
.generate()
.expect("Failed to build linux bindings");

let output_path =
Path::new(&env::var("OUT_DIR").expect("OUT_DIR env var was not defined"))
.join("linux_bindings.rs");

bindings
.write_to_file(output_path)
.expect("Failed to write linux bindings");
}
_ => {}
}
}
Expand Down
30 changes: 14 additions & 16 deletions src/integrations/linux/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::integrations::linux::netlink_iterator::*;
use crate::integrations::linux::procfs::*;
use crate::types::error::Error;
use crate::types::*;
use libc::*;
use netlink_packet_sock_diag::{AF_INET, AF_INET6, IPPROTO_TCP, IPPROTO_UDP};

/// Iterate through sockets information.
pub fn iterate_sockets_info(
Expand All @@ -25,22 +25,20 @@ pub fn iterate_sockets_info_without_pids(
let tcp = proto_flags.contains(ProtocolFlags::TCP);
let udp = proto_flags.contains(ProtocolFlags::UDP);
let mut iterators = Vec::with_capacity(4);
unsafe {
if ipv4 {
if tcp {
iterators.push(NetlinkIterator::new(AF_INET as u8, IPPROTO_TCP as u8)?);
}
if udp {
iterators.push(NetlinkIterator::new(AF_INET as u8, IPPROTO_UDP as u8)?);
}
if ipv4 {
if tcp {
iterators.push(NetlinkIterator::new(AF_INET as u8, IPPROTO_TCP as u8)?);
}
if ipv6 {
if tcp {
iterators.push(NetlinkIterator::new(AF_INET6 as u8, IPPROTO_TCP as u8)?);
}
if udp {
iterators.push(NetlinkIterator::new(AF_INET6 as u8, IPPROTO_UDP as u8)?);
}
if udp {
iterators.push(NetlinkIterator::new(AF_INET as u8, IPPROTO_UDP as u8)?);
}
}
if ipv6 {
if tcp {
iterators.push(NetlinkIterator::new(AF_INET6 as u8, IPPROTO_TCP as u8)?);
}
if udp {
iterators.push(NetlinkIterator::new(AF_INET6 as u8, IPPROTO_UDP as u8)?);
}
}
Ok(iterators.into_iter().flatten())
Expand Down
69 changes: 0 additions & 69 deletions src/integrations/linux/ffi/macros.rs

This file was deleted.

4 changes: 0 additions & 4 deletions src/integrations/linux/ffi/mod.rs

This file was deleted.

10 changes: 0 additions & 10 deletions src/integrations/linux/mod.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,3 @@
#[allow(non_upper_case_globals)]
#[allow(non_snake_case)]
#[allow(unused)]
mod linux_bindings {
include!(concat!(env!("OUT_DIR"), "/linux_bindings.rs"));
}

#[macro_use]
mod ffi;

mod api;
mod ext;
mod netlink_iterator;
Expand Down
Loading

0 comments on commit 4c06245

Please sign in to comment.