From ad8cad56e7f8ac6276e67724fdaaf4d854b49fd7 Mon Sep 17 00:00:00 2001 From: Andrew Rioux Date: Mon, 20 Jan 2025 14:56:46 -0500 Subject: [PATCH] feat: finished adding pcap-sys --- Cargo.lock | 2 +- build_freebsd.rs | 6 ++++ pcap-sys/build.rs | 61 ++++++--------------------------------- sparse-beacon/Cargo.toml | 2 +- sparse-beacon/build.rs | 7 +---- sparse-beacon/src/main.rs | 18 +++--------- 6 files changed, 22 insertions(+), 74 deletions(-) create mode 100644 build_freebsd.rs diff --git a/Cargo.lock b/Cargo.lock index efca02a..df14f88 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -305,7 +305,7 @@ dependencies = [ name = "sparse-beacon" version = "0.7.0" dependencies = [ - "nl-sys", + "pcap-sys", ] [[package]] diff --git a/build_freebsd.rs b/build_freebsd.rs new file mode 100644 index 0000000..c21766e --- /dev/null +++ b/build_freebsd.rs @@ -0,0 +1,6 @@ +if std::env::var("CARGO_CFG_TARGET_OS").unwrap() == "freebsd" { + let freebsd_libs = std::env::var("FREEBSD_LIBS").unwrap(); + println!("cargo:rustc-link-arg=--sysroot={freebsd_libs}"); + println!("cargo:rustc-link-arg=-L{freebsd_libs}/lib"); + println!("cargo:rustc-link-arg=-L{freebsd_libs}/usr/lib"); +} diff --git a/pcap-sys/build.rs b/pcap-sys/build.rs index 465abe7..142cae0 100644 --- a/pcap-sys/build.rs +++ b/pcap-sys/build.rs @@ -16,63 +16,20 @@ use fs_extra::dir::{copy, CopyOptions}; fn main() { + include!("../build_freebsd.rs"); + if std::env::var("CARGO_CFG_TARGET_OS").unwrap() == "windows" { - println!("cargo:rustc-link-search=native={}", std::env::var("SPARSE_BUILD_WINPCAP").unwrap()); - println!("cargo:rustc-link-search=native={}/x64", std::env::var("SPARSE_BUILD_WINPCAP").unwrap()); + let libdir = std::env::var("SPARSE_BUILD_WINPCAP_LIBS").unwrap(); + println!("cargo:rustc-link-search=native={libdir}"); + println!("cargo:rustc-link-search=native={libdir}/x64"); println!("cargo:rustc-link-lib=wpcap"); } else if std::env::var("CARGO_CFG_TARGET_OS").unwrap() == "freebsd" { - let libpcap_src = format!("{}/pcap_src", std::env::var("OUT_DIR").unwrap()); - - let mut options = CopyOptions::new(); - options.copy_inside = true; - options.skip_exist = true; - copy(std::env::var("SPARSE_BUILD_LIBPCAP").unwrap(), &libpcap_src, &options).expect("could not copy libpcap source code to build"); - - let dst = cmake::Config::new(&libpcap_src) - .profile("MinSizeRel") - .define("BUILD_SHARED_LIBS", "OFF") - .define("DISABLE_BLUETOOTH", "ON") - .define("DISABLE_DAG", "ON") - .define("DISABLE_DBUS", "ON") - .define("DISABLE_DPDK", "ON") - .define("DISABLE_NETMAP", "ON") - .define("DISABLE_RDMA", "ON") - .define("DISABLE_SEPTEL", "ON") - .define("DISABLE_SNF", "ON") - .define("DISABLE_TC", "ON") - .build(); - - println!("cargo:rustc-link-search=native={}/lib", dst.display()); - println!("cargo:rustc-link-search=native={}/lib64", dst.display()); + let libdir = std::env::var("SPARSE_BUILD_LIBPCAP_FREEBSD").unwrap(); + println!("cargo:rustc-link-search=native={libdir}/lib"); println!("cargo:rustc-link-lib=static=pcap"); } else { - let libpcap_src = format!("{}/pcap_src", std::env::var("OUT_DIR").unwrap()); - - let mut options = CopyOptions::new(); - options.copy_inside = true; - options.skip_exist = true; - copy(std::env::var("SPARSE_BUILD_LIBPCAP").unwrap(), &libpcap_src, &options).expect("could not copy libpcap source code to build"); - - let dst = cmake::Config::new(&libpcap_src) - .profile("MinSizeRel") - .define("BUILD_SHARED_LIBS", "OFF") - .define("BUILD_WITH_LIBNL", "OFF") - .define("DISABLE_BLUETOOTH", "ON") - .define("DISABLE_DAG", "ON") - .define("DISABLE_DBUS", "ON") - .define("DISABLE_DPDK", "ON") - .define("DISABLE_LINUX_USBMON", "ON") - .define("DISABLE_NETMAP", "ON") - .define("DISABLE_RDMA", "ON") - .define("DISABLE_SEPTEL", "ON") - .define("DISABLE_SNF", "ON") - .define("DISABLE_TC", "ON") - .define("PCAP_TYPE", "linux") - .build(); - - // panic!("hahahahah test {}", dst.display()); - println!("cargo:rustc-link-search=native={}/lib", dst.display()); - println!("cargo:rustc-link-search=native={}/lib64", dst.display()); + let libdir = std::env::var("SPARSE_BUILD_LIBPCAP_LINUX").unwrap(); + println!("cargo:rustc-link-search=native={libdir}/lib"); println!("cargo:rustc-link-lib=static=pcap"); } } diff --git a/sparse-beacon/Cargo.toml b/sparse-beacon/Cargo.toml index c963ecf..b6a6bbd 100644 --- a/sparse-beacon/Cargo.toml +++ b/sparse-beacon/Cargo.toml @@ -4,4 +4,4 @@ version = "0.7.0" edition = "2021" [dependencies] -nl-sys = { path = "../nl-sys" } +pcap-sys = { path = "../pcap-sys" } diff --git a/sparse-beacon/build.rs b/sparse-beacon/build.rs index cc35c3d..0fe8f5f 100644 --- a/sparse-beacon/build.rs +++ b/sparse-beacon/build.rs @@ -1,8 +1,3 @@ fn main() { - if std::env::var("CARGO_CFG_TARGET_OS").unwrap() == "freebsd" { - let freebsd_libs = std::env::var("FREEBSD_LIBS").unwrap(); - println!("cargo:rustc-link-arg=--sysroot={freebsd_libs}"); - println!("cargo:rustc-link-arg=-L{freebsd_libs}/lib"); - println!("cargo:rustc-link-arg=-L{freebsd_libs}/usr/lib"); - } + include!("../build_freebsd.rs"); } diff --git a/sparse-beacon/src/main.rs b/sparse-beacon/src/main.rs index 8cad6a0..61f0af4 100644 --- a/sparse-beacon/src/main.rs +++ b/sparse-beacon/src/main.rs @@ -1,16 +1,6 @@ -use nl_sys::{netlink, route}; - fn main() { - let ip = std::net::Ipv4Addr::new(192, 168, 3, 10); - - let (ifname, _, srcip, src_mac, dst_mac, _) = { - let socket = netlink::Socket::new().unwrap(); - - let routes = socket.get_routes().unwrap(); - let neighs = socket.get_neigh().unwrap(); - let links = socket.get_links().unwrap(); - let addrs = socket.get_addrs().unwrap(); - - route::get_macs_and_src_for_ip(&addrs, &routes, &neighs, &links, ip).unwrap() - }; + let interfaces = pcap_sys::PcapDevIterator::new().unwrap(); + for interface in interfaces { + println!("Found interface: {interface}"); + } }