From b1c450ed128d281dcab68ab8a41fcce2a4262568 Mon Sep 17 00:00:00 2001 From: Patrick Date: Wed, 25 Dec 2024 11:38:27 +0100 Subject: [PATCH] feat: reuse port --- Cargo.lock | 84 +++++++++++++++++++++++++++++++++++++++++++++++++++++ Cargo.toml | 1 + src/main.rs | 39 ++++++++++++++++--------- 3 files changed, 110 insertions(+), 14 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 0ee3747..c5c624d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -331,6 +331,7 @@ dependencies = [ "color-eyre", "pnet", "simple-dns", + "socket2", ] [[package]] @@ -383,6 +384,16 @@ dependencies = [ "bitflags", ] +[[package]] +name = "socket2" +version = "0.5.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c970269d99b64e60ec3bd6ad27270092a5394c4e309314b18ae3fe575695fbe8" +dependencies = [ + "libc", + "windows-sys", +] + [[package]] name = "syn" version = "2.0.91" @@ -478,3 +489,76 @@ name = "winapi-x86_64-pc-windows-gnu" version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" + +[[package]] +name = "windows-sys" +version = "0.52.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "282be5f36a8ce781fad8c8ae18fa3f9beff57ec1b52cb3de0789201425d9a33d" +dependencies = [ + "windows-targets", +] + +[[package]] +name = "windows-targets" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9b724f72796e036ab90c1021d4780d4d3d648aca59e491e6b98e725b84e99973" +dependencies = [ + "windows_aarch64_gnullvm", + "windows_aarch64_msvc", + "windows_i686_gnu", + "windows_i686_gnullvm", + "windows_i686_msvc", + "windows_x86_64_gnu", + "windows_x86_64_gnullvm", + "windows_x86_64_msvc", +] + +[[package]] +name = "windows_aarch64_gnullvm" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "32a4622180e7a0ec044bb555404c800bc9fd9ec262ec147edd5989ccd0c02cd3" + +[[package]] +name = "windows_aarch64_msvc" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "09ec2a7bb152e2252b53fa7803150007879548bc709c039df7627cabbd05d469" + +[[package]] +name = "windows_i686_gnu" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8e9b5ad5ab802e97eb8e295ac6720e509ee4c243f69d781394014ebfe8bbfa0b" + +[[package]] +name = "windows_i686_gnullvm" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0eee52d38c090b3caa76c563b86c3a4bd71ef1a819287c19d586d7334ae8ed66" + +[[package]] +name = "windows_i686_msvc" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "240948bc05c5e7c6dabba28bf89d89ffce3e303022809e73deaefe4f6ec56c66" + +[[package]] +name = "windows_x86_64_gnu" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "147a5c80aabfbf0c7d901cb5895d1de30ef2907eb21fbbab29ca94c5b08b1a78" + +[[package]] +name = "windows_x86_64_gnullvm" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "24d5b23dc417412679681396f2b49f3de8c1473deb516bd34410872eff51ed0d" + +[[package]] +name = "windows_x86_64_msvc" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "589f6da84c646204747d1270a2a5661ea66ed1cced2631d546fdfb155959f9ec" diff --git a/Cargo.toml b/Cargo.toml index b4e18f2..72fb5b4 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -7,3 +7,4 @@ edition = "2021" color-eyre = "0.6.3" pnet = "0.35.0" simple-dns = "0.9.1" +socket2 = { version = "0.5.8", features = ["all"] } diff --git a/src/main.rs b/src/main.rs index 40dab1c..4cf46f9 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,13 +2,34 @@ const ADDR: Ipv4Addr = Ipv4Addr::new(224, 0, 0, 251); use color_eyre::Result; use pnet::datalink::interfaces; use simple_dns::Packet; -use std::net::{Ipv4Addr, UdpSocket}; +use socket2::{Domain, Protocol, Socket, Type}; +use std::{ + io::Read, + net::{Ipv4Addr, SocketAddrV4, UdpSocket}, +}; const IFACE: [&str; 1] = ["lan01"]; fn main() -> Result<()> { - let socket = UdpSocket::bind((Ipv4Addr::UNSPECIFIED, 5354))?; - socket.join_multicast_v4(&ADDR, &Ipv4Addr::new(10, 99, 10, 161))?; + let mut socket = Socket::new(Domain::IPV4, Type::DGRAM, Some(Protocol::UDP))?; + socket.set_reuse_port(true)?; + let addr = SocketAddrV4::new(Ipv4Addr::UNSPECIFIED, 5353).into(); + socket.bind(&addr)?; + socket.join_multicast_v4(&ADDR, &Ipv4Addr::new(10, 99, 10, 164))?; let mut buf = [0; 1024]; + loop { + match socket.read(&mut buf) { + Ok(_l) => { + let packet = Packet::parse(&buf)?; + println!("{:?}\n", packet); + } + Err(_) => todo!(), + } + } + + Ok(()) +} +/* + let socket = UdpSocket::bind((Ipv4Addr::UNSPECIFIED, 5354))?; let interfaces = interfaces(); let interfaces = interfaces .iter() @@ -17,17 +38,7 @@ fn main() -> Result<()> { println!("{:?}", i); } - loop { - match socket.recv(&mut buf) { - Ok(_) => { - let packet = Packet::parse(&buf)?; - println!("{:?}\n", packet); - } - Err(_) => todo!(), - } - } - Ok(()) -} +} */ /* table ip mdns { chain prerouting {