Skip to content

Commit 38c01bd

Browse files
authored
Merge pull request #19 from ohadravid/bugfix/replace-custom-ffi-with-netlink-crates
Replace bespoke netlink impl with one based on the `netlink-*` crates
2 parents 6a3d0d0 + 00b78c7 commit 38c01bd

File tree

11 files changed

+253
-283
lines changed

11 files changed

+253
-283
lines changed

.github/workflows/ci.yml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,3 +40,14 @@ jobs:
4040
rustup target add aarch64-apple-ios
4141
cross build --target aarch64-apple-ios
4242
if: matrix.os == 'macos-latest'
43+
44+
- name: setup zig for cargo-zigbuild (on macOS)
45+
uses: mlugg/setup-zig@v1
46+
if: matrix.os == 'macos-latest'
47+
48+
- name: run cargo-zigbuild for Linux (on macOS)
49+
run: |
50+
cargo install --locked cargo-zigbuild
51+
rustup target add aarch64-unknown-linux-gnu
52+
cargo zigbuild --target aarch64-unknown-linux-gnu
53+
if: matrix.os == 'macos-latest'

Cargo.toml

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "netstat2"
3-
version = "0.10.1"
3+
version = "0.11.0"
44
authors = ["Ohad Ravid <ohad.rv@gmail.com>", "ivxvm <ivxvm@protonmail.com>"]
55
edition = "2021"
66
license = "MIT OR Apache-2.0"
@@ -14,7 +14,6 @@ Cross-platform library to retrieve network sockets information.
1414
"""
1515

1616
[dependencies]
17-
libc = "0.2"
1817
bitflags = "2"
1918
thiserror = "2"
2019

@@ -32,3 +31,9 @@ bindgen = "0.65"
3231
num-derive = "0.3"
3332
num-traits = "0.2.8"
3433
byteorder = "1.3.2"
34+
35+
[target.'cfg(any(target_os = "linux", target_os = "android"))'.dependencies]
36+
netlink-sys = "0.8"
37+
netlink-packet-core = "0.7"
38+
netlink-packet-utils = "0.5"
39+
netlink-packet-sock-diag = "0.4"

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ Provides unified interface and returns data structures which may have additional
1111
```toml
1212
# Cargo.toml
1313
[dependencies]
14-
netstat2 = "0.10"
14+
netstat2 = "0.11"
1515
```
1616

1717
This is a fork based on the [netstat](https://crates.io/crates/netstat) crate by [ivxvm](https://github.com/ivxvm).

build.rs

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -27,30 +27,6 @@ fn main() {
2727
.write_to_file(output_path)
2828
.expect("Failed to write libproc bindings");
2929
}
30-
"linux" | "android" => {
31-
let bindings = bindgen::builder()
32-
.header_contents(
33-
"linux_bindings.h",
34-
r#"
35-
#include <linux/sock_diag.h>
36-
#include <linux/inet_diag.h>
37-
#include <linux/rtnetlink.h>
38-
#include <linux/netlink.h>
39-
#include <linux/tcp.h>
40-
"#,
41-
)
42-
.layout_tests(false)
43-
.generate()
44-
.expect("Failed to build linux bindings");
45-
46-
let output_path =
47-
Path::new(&env::var("OUT_DIR").expect("OUT_DIR env var was not defined"))
48-
.join("linux_bindings.rs");
49-
50-
bindings
51-
.write_to_file(output_path)
52-
.expect("Failed to write linux bindings");
53-
}
5430
_ => {}
5531
}
5632
}

src/integrations/linux/api.rs

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use crate::integrations::linux::netlink_iterator::*;
22
use crate::integrations::linux::procfs::*;
33
use crate::types::error::Error;
44
use crate::types::*;
5-
use libc::*;
5+
use netlink_packet_sock_diag::{AF_INET, AF_INET6, IPPROTO_TCP, IPPROTO_UDP};
66

77
/// Iterate through sockets information.
88
pub fn iterate_sockets_info(
@@ -25,22 +25,20 @@ pub fn iterate_sockets_info_without_pids(
2525
let tcp = proto_flags.contains(ProtocolFlags::TCP);
2626
let udp = proto_flags.contains(ProtocolFlags::UDP);
2727
let mut iterators = Vec::with_capacity(4);
28-
unsafe {
29-
if ipv4 {
30-
if tcp {
31-
iterators.push(NetlinkIterator::new(AF_INET as u8, IPPROTO_TCP as u8)?);
32-
}
33-
if udp {
34-
iterators.push(NetlinkIterator::new(AF_INET as u8, IPPROTO_UDP as u8)?);
35-
}
28+
if ipv4 {
29+
if tcp {
30+
iterators.push(NetlinkIterator::new(AF_INET as u8, IPPROTO_TCP as u8)?);
3631
}
37-
if ipv6 {
38-
if tcp {
39-
iterators.push(NetlinkIterator::new(AF_INET6 as u8, IPPROTO_TCP as u8)?);
40-
}
41-
if udp {
42-
iterators.push(NetlinkIterator::new(AF_INET6 as u8, IPPROTO_UDP as u8)?);
43-
}
32+
if udp {
33+
iterators.push(NetlinkIterator::new(AF_INET as u8, IPPROTO_UDP as u8)?);
34+
}
35+
}
36+
if ipv6 {
37+
if tcp {
38+
iterators.push(NetlinkIterator::new(AF_INET6 as u8, IPPROTO_TCP as u8)?);
39+
}
40+
if udp {
41+
iterators.push(NetlinkIterator::new(AF_INET6 as u8, IPPROTO_UDP as u8)?);
4442
}
4543
}
4644
Ok(iterators.into_iter().flatten())

src/integrations/linux/ffi/macros.rs

Lines changed: 0 additions & 69 deletions
This file was deleted.

src/integrations/linux/ffi/mod.rs

Lines changed: 0 additions & 4 deletions
This file was deleted.

src/integrations/linux/mod.rs

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,3 @@
1-
#[allow(non_upper_case_globals)]
2-
#[allow(non_snake_case)]
3-
#[allow(unused)]
4-
mod linux_bindings {
5-
include!(concat!(env!("OUT_DIR"), "/linux_bindings.rs"));
6-
}
7-
8-
#[macro_use]
9-
mod ffi;
10-
111
mod api;
122
mod ext;
133
mod netlink_iterator;

0 commit comments

Comments
 (0)