diff --git a/Cargo.lock b/Cargo.lock index c5c624d..d73b349 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -26,6 +26,12 @@ dependencies = [ "memchr", ] +[[package]] +name = "autocfg" +version = "1.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ace50bade8e6234aa140d9a2f552bbee1db4d353f69b8217bc503490fc1a9f26" + [[package]] name = "backtrace" version = "0.3.71" @@ -47,6 +53,12 @@ version = "2.6.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b048fb63fd8b5923fc5aa7b340d8e156aec7ec02f0c78fa8a6ddc2613f6f71de" +[[package]] +name = "bytes" +version = "1.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "325918d6fe32f23b19878fe4b34794ae41fc19ddbe53b10571a4874d44ffd39b" + [[package]] name = "cc" version = "1.2.5" @@ -138,6 +150,16 @@ version = "0.2.169" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b5aba8db14291edd000dfcc4d620c7ebfb122c613afb886ca8803fa4e128a20a" +[[package]] +name = "lock_api" +version = "0.4.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "07af8b9cdd281b7915f413fa73f29ebd5d55d0d3f0155584dade1ff18cea1b17" +dependencies = [ + "autocfg", + "scopeguard", +] + [[package]] name = "memchr" version = "2.7.4" @@ -153,6 +175,17 @@ dependencies = [ "adler", ] +[[package]] +name = "mio" +version = "1.0.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2886843bf800fba2e3377cff24abf6379b4c4d5c6681eaf9ea5b0d15090450bd" +dependencies = [ + "libc", + "wasi", + "windows-sys", +] + [[package]] name = "no-std-net" version = "0.6.0" @@ -180,6 +213,29 @@ version = "3.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c1b04fb49957986fdce4d6ee7a65027d55d4b6d2265e5848bbb507b58ccfdb6f" +[[package]] +name = "parking_lot" +version = "0.12.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f1bf18183cf54e8d6059647fc3063646a1801cf30896933ec2311622cc4b9a27" +dependencies = [ + "lock_api", + "parking_lot_core", +] + +[[package]] +name = "parking_lot_core" +version = "0.9.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1e401f977ab385c9e4e3ab30627d6f26d00e2c73eef317493c4ec6d468726cf8" +dependencies = [ + "cfg-if", + "libc", + "redox_syscall", + "smallvec", + "windows-targets", +] + [[package]] name = "pin-project-lite" version = "0.2.15" @@ -295,6 +351,15 @@ dependencies = [ "proc-macro2", ] +[[package]] +name = "redox_syscall" +version = "0.5.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9b6dfecf2c74bce2466cabf93f6664d6998a69eb21e39f4207930065b27b771f" +dependencies = [ + "bitflags", +] + [[package]] name = "regex" version = "1.11.1" @@ -330,8 +395,10 @@ version = "0.1.0" dependencies = [ "color-eyre", "pnet", + "regex", "simple-dns", "socket2", + "tokio", ] [[package]] @@ -340,6 +407,12 @@ version = "0.1.24" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "719b953e2095829ee67db738b3bfa9fa368c94900df327b3f07fe6e794d2fe1f" +[[package]] +name = "scopeguard" +version = "1.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "94143f37725109f92c262ed2cf5e59bce7498c01bcc1502d7b9afe439a4e9f49" + [[package]] name = "serde" version = "1.0.216" @@ -375,6 +448,15 @@ version = "1.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0fda2ff0d084019ba4d7c6f371c95d8fd75ce3524c3cb8fb653a3023f6323e64" +[[package]] +name = "signal-hook-registry" +version = "1.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a9e9e0b4211b72e7b8b6e85c807d36c212bdb33ea8587f7569562a84df5465b1" +dependencies = [ + "libc", +] + [[package]] name = "simple-dns" version = "0.9.1" @@ -384,6 +466,12 @@ dependencies = [ "bitflags", ] +[[package]] +name = "smallvec" +version = "1.13.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3c5e1a9a646d36c3599cd173a41282daf47c44583ad367b8e6837255952e5c67" + [[package]] name = "socket2" version = "0.5.8" @@ -415,6 +503,35 @@ dependencies = [ "once_cell", ] +[[package]] +name = "tokio" +version = "1.42.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5cec9b21b0450273377fc97bd4c33a8acffc8c996c987a7c5b319a0083707551" +dependencies = [ + "backtrace", + "bytes", + "libc", + "mio", + "parking_lot", + "pin-project-lite", + "signal-hook-registry", + "socket2", + "tokio-macros", + "windows-sys", +] + +[[package]] +name = "tokio-macros" +version = "2.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "693d596312e88961bc67d7f1f97af8a70227d9f90c31bba5806eec004978d752" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + [[package]] name = "tracing" version = "0.1.41" @@ -468,6 +585,12 @@ version = "0.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "830b7e5d4d90034032940e4ace0d9a9a057e7a45cd94e6c007832e39edb82f6d" +[[package]] +name = "wasi" +version = "0.11.0+wasi-snapshot-preview1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423" + [[package]] name = "winapi" version = "0.3.9" diff --git a/Cargo.toml b/Cargo.toml index 72fb5b4..53029bb 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -6,5 +6,7 @@ edition = "2021" [dependencies] color-eyre = "0.6.3" pnet = "0.35.0" +regex = "1.11.1" simple-dns = "0.9.1" socket2 = { version = "0.5.8", features = ["all"] } +tokio = { version = "1.42.0", features = ["full"] } diff --git a/src/main.rs b/src/main.rs index 4cf46f9..a5638fb 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,42 +1,45 @@ const ADDR: Ipv4Addr = Ipv4Addr::new(224, 0, 0, 251); use color_eyre::Result; use pnet::datalink::interfaces; +use regex::Regex; use simple_dns::Packet; use socket2::{Domain, Protocol, Socket, Type}; -use std::{ - io::Read, - net::{Ipv4Addr, SocketAddrV4, UdpSocket}, -}; -const IFACE: [&str; 1] = ["lan01"]; +use std::net::{Ipv4Addr, SocketAddrV4}; +use tokio::net::UdpSocket; -fn main() -> Result<()> { - let mut socket = Socket::new(Domain::IPV4, Type::DGRAM, Some(Protocol::UDP))?; +#[tokio::main] +async fn main() -> Result<()> { + let socket = Socket::new(Domain::IPV4, Type::DGRAM, Some(Protocol::UDP))?; + socket.set_reuse_address(true)?; socket.set_reuse_port(true)?; - let addr = SocketAddrV4::new(Ipv4Addr::UNSPECIFIED, 5353).into(); + let addr = SocketAddrV4::new(Ipv4Addr::new(224, 0, 0, 251), 5353).into(); socket.bind(&addr)?; - socket.join_multicast_v4(&ADDR, &Ipv4Addr::new(10, 99, 10, 164))?; + socket.join_multicast_v4(&ADDR, &Ipv4Addr::new(10, 99, 10, 1))?; + //socket.set_nonblocking(true)?; + + let iface_reg = Regex::new(r"^lan.*$")?; + let interfaces = interfaces(); + let interfaces = interfaces.iter().filter(|x| { + x.is_up() && !x.is_loopback() && !x.ips.is_empty() && iface_reg.is_match(&x.name) + }); + for i in interfaces { + println!("{:?}", i); + } + let mut buf = [0; 1024]; + let socket = UdpSocket::from_std(socket.into())?; loop { - match socket.read(&mut buf) { - Ok(_l) => { + match socket.recv_from(&mut buf).await { + Ok((_l, from)) => { let packet = Packet::parse(&buf)?; - println!("{:?}\n", packet); + println!("{:?} {:?}\n", from, packet); } Err(_) => todo!(), } } - - Ok(()) } /* let socket = UdpSocket::bind((Ipv4Addr::UNSPECIFIED, 5354))?; - let interfaces = interfaces(); - let interfaces = interfaces - .iter() - .filter(|x| x.is_up() && !x.is_loopback() && !x.ips.is_empty()); - for i in interfaces { - println!("{:?}", i); - } } */ /*