SO_REUSEADDR on upnp socket

This commit is contained in:
Igor Katson 2024-08-26 18:57:51 +01:00
parent 13414428b9
commit aae78b57c7
No known key found for this signature in database
GPG key ID: B4EC22B66D61A3F5
3 changed files with 12 additions and 2 deletions

1
Cargo.lock generated
View file

@ -3609,6 +3609,7 @@ dependencies = [
"parking_lot",
"quick-xml",
"reqwest",
"socket2",
"tokio",
"tokio-util",
"tower-http",

View file

@ -22,6 +22,7 @@ url = "2.5.2"
parking_lot = "0.12.3"
tokio-util = "0.7.11"
reqwest = { version = "0.12.7", default-features = false }
socket2 = "0.5.7"
[dev-dependencies]
tracing-subscriber = "0.3.18"

View file

@ -108,12 +108,20 @@ pub struct SsdpRunner {
impl SsdpRunner {
pub async fn new(opts: SsdpRunnerOptions) -> anyhow::Result<Self> {
let bind_addr = SocketAddrV4::new(Ipv4Addr::UNSPECIFIED, UPNP_PORT);
let sock = socket2::Socket::new(socket2::Domain::IPV4, socket2::Type::DGRAM, None)
.context("error creating socket")?;
sock.set_reuse_port(true)
.context("error setting SO_REUSEPORT")?;
trace!(addr=?bind_addr, "binding UDP");
let socket = tokio::net::UdpSocket::bind(bind_addr)
.await
sock.bind(&bind_addr.into())
.context(bind_addr)
.context("error binding")?;
sock.set_nonblocking(true)?;
let socket = tokio::net::UdpSocket::from_std(sock.into())
.context("error converting socket2 socket to tokio")?;
trace!(multiaddr=?UPNP_BROADCAST_IP, interface=?Ipv4Addr::UNSPECIFIED, "joining multicast v4 group");
socket
.join_multicast_v4(UPNP_BROADCAST_IP, Ipv4Addr::UNSPECIFIED)