jmmk / rustcap

Rust wrapper for libpcap
MIT License
9 stars 2 forks source link

Cannot compile with latest pcap #3

Open conqp opened 1 year ago

conqp commented 1 year ago

I am trying to compile this MRE on Windows 11 Pro:

Cargo.toml

[package]
name = "findip"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
pnet = "0.34.0"
rustcap = { version = "0.1.2", features = ["pnet"] }

src/main.rs

use std::str::FromStr;

fn main() {
    println!("{}", discover_address_or_exit("0.0.0.0/0"));
}

#[must_use]
pub fn discover_address_or_exit(network: &str) -> std::net::IpAddr {
    discover_address(pnet::ipnetwork::IpNetwork::from_str(network).unwrap_or_else(|error| {
        eprintln!("{error}");
        std::process::exit(1)
    }))
    .unwrap_or_else(|| {
        eprintln!("No address found");
        std::process::exit(2);
    })
}

#[must_use]
pub fn discover_address(network: pnet::ipnetwork::IpNetwork) -> Option<std::net::IpAddr> {
    for iface in pnet::datalink::interfaces() {
        for ip in iface.ips {
            if network.contains(ip.ip()) {
                return Some(ip.ip());
            }
        }
    }

    None
}

Resulting in this error:

PS C:\Users\Richard Neumann\findip> cargo run
  Downloaded aho-corasick v1.0.3
  Downloaded log v0.4.20
  Downloaded 2 crates (206.7 KB) in 0.64s
   Compiling winapi-build v0.1.1
   Compiling libc v0.2.147
   Compiling memchr v2.5.0
   Compiling proc-macro2 v1.0.66
   Compiling winapi v0.2.8
   Compiling unicode-ident v1.0.11
   Compiling regex-syntax v0.7.4
   Compiling no-std-net v0.6.0
   Compiling log v0.4.20
   Compiling rustc-serialize v0.3.24
   Compiling unicode-xid v0.0.3
   Compiling bitflags v0.5.0
   Compiling pnet_base v0.34.0
   Compiling winapi v0.3.9
   Compiling glob v0.2.11
   Compiling serde v1.0.183
   Compiling kernel32-sys v0.2.2
   Compiling log v0.3.9
   Compiling ws2_32-sys v0.2.1
   Compiling pnet_base v0.21.0
   Compiling pnet_macros_support v0.21.0
   Compiling pkg-config v0.3.27
   Compiling pnet_macros_support v0.34.0
   Compiling ipnetwork v0.12.8
   Compiling pnet v0.21.0
   Compiling quote v1.0.32
   Compiling pnet v0.34.0
   Compiling bitflags v1.3.2
   Compiling syn v2.0.28
   Compiling pcap-sys v0.1.3
   Compiling syntex_pos v0.42.0
   Compiling pnet_sys v0.34.0
   Compiling ipnetwork v0.20.0
   Compiling pnet_datalink v0.34.0
   Compiling aho-corasick v1.0.3
   Compiling regex-automata v0.3.6
   Compiling regex v1.9.3
   Compiling pnet_macros v0.34.0
   Compiling pnet_packet v0.34.0
   Compiling pnet_sys v0.21.0
   Compiling term v0.4.6
   Compiling pnet_datalink v0.21.0
   Compiling syntex_errors v0.42.0
   Compiling syntex_syntax v0.42.0
   Compiling pnet_transport v0.34.0
   Compiling syntex v0.42.2
   Compiling pnet_packet v0.21.0
error[E0425]: cannot find function `register` in crate `pnet_macros`
  --> C:\Users\Richard Neumann\.cargo\registry\src\index.crates.io-6f17d22bba15001f\pnet_packet-0.21.0\build.rs:27:30
   |
27 |                 pnet_macros::register(&mut registry);
   |                              ^^^^^^^^ not found in `pnet_macros`

For more information about this error, try `rustc --explain E0425`.
error: could not compile `pnet_packet` (build script) due to previous error

It seems that register is no longer available in pnet_macros

jmmk commented 1 year ago

@conqp if I'm not mistaken, in your code example you are not actually using anything from rustcap.

In general my recommendation would be to use libpnet and rust-pcap/pcap and not rustcap. pnet_datalink includes rust-pcap/pcap as a dependency.

I haven't used them much so I can't vouch for them, but they are almost certainly more full-featured and well-maintained.

That said, if there is something you find useful here, I can update the dependencies and create a new release. Otherwise, I'm inclined to archive this and link to libpnet and rust-pcap/pcap as better alternatives.

conqp commented 1 year ago

Thanks for the feedback. In the meantime I migrated to the local_ip_address and ipnetwork crates, since I only needed IP address discovery and not actual packet capturing. https://github.com/homeinfogmbh/digsigctl/commit/15d715e9919599a865465dbe138e52c1f7ec912c