feat: reuse port

This commit is contained in:
Patrick 2024-12-25 11:38:27 +01:00
parent ff058721e4
commit b1c450ed12
Signed by: patrick
GPG key ID: 451F95EFB8BECD0F
3 changed files with 110 additions and 14 deletions

84
Cargo.lock generated
View file

@ -331,6 +331,7 @@ dependencies = [
"color-eyre", "color-eyre",
"pnet", "pnet",
"simple-dns", "simple-dns",
"socket2",
] ]
[[package]] [[package]]
@ -383,6 +384,16 @@ dependencies = [
"bitflags", "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]] [[package]]
name = "syn" name = "syn"
version = "2.0.91" version = "2.0.91"
@ -478,3 +489,76 @@ name = "winapi-x86_64-pc-windows-gnu"
version = "0.4.0" version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" 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"

View file

@ -7,3 +7,4 @@ edition = "2021"
color-eyre = "0.6.3" color-eyre = "0.6.3"
pnet = "0.35.0" pnet = "0.35.0"
simple-dns = "0.9.1" simple-dns = "0.9.1"
socket2 = { version = "0.5.8", features = ["all"] }

View file

@ -2,13 +2,34 @@ const ADDR: Ipv4Addr = Ipv4Addr::new(224, 0, 0, 251);
use color_eyre::Result; use color_eyre::Result;
use pnet::datalink::interfaces; use pnet::datalink::interfaces;
use simple_dns::Packet; 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"]; const IFACE: [&str; 1] = ["lan01"];
fn main() -> Result<()> { fn main() -> Result<()> {
let socket = UdpSocket::bind((Ipv4Addr::UNSPECIFIED, 5354))?; let mut socket = Socket::new(Domain::IPV4, Type::DGRAM, Some(Protocol::UDP))?;
socket.join_multicast_v4(&ADDR, &Ipv4Addr::new(10, 99, 10, 161))?; 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]; 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();
let interfaces = interfaces let interfaces = interfaces
.iter() .iter()
@ -17,17 +38,7 @@ fn main() -> Result<()> {
println!("{:?}", i); println!("{:?}", i);
} }
loop { } */
match socket.recv(&mut buf) {
Ok(_) => {
let packet = Packet::parse(&buf)?;
println!("{:?}\n", packet);
}
Err(_) => todo!(),
}
}
Ok(())
}
/* /*
table ip mdns { table ip mdns {
chain prerouting { chain prerouting {