From a1f09856382ad1119e7b4fff95cd87a271ceebee Mon Sep 17 00:00:00 2001 From: Jonathan Davies Date: Sat, 20 May 2023 16:27:27 +0100 Subject: [PATCH] netapp.rs: Set SO_REUSEPORT on listen_addr with TcpSocket. --- src/netapp.rs | 25 +++++++++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/src/netapp.rs b/src/netapp.rs index b1ad9db..3fa4db3 100644 --- a/src/netapp.rs +++ b/src/netapp.rs @@ -13,7 +13,7 @@ use sodiumoxide::crypto::sign::ed25519; use futures::stream::futures_unordered::FuturesUnordered; use futures::stream::StreamExt; -use tokio::net::{TcpListener, TcpStream}; +use tokio::net::{TcpSocket, TcpStream}; use tokio::select; use tokio::sync::{mpsc, watch}; @@ -195,7 +195,28 @@ impl NetApp { error!("Trying to listen on NetApp but we're already listening!"); } - let listener = TcpListener::bind(listen_addr).await.unwrap(); + let listen_addr_socket = match listen_addr { + SocketAddr::V4(_) => TcpSocket::new_v4(), + SocketAddr::V6(_) => TcpSocket::new_v6(), + } + .unwrap(); + + listen_addr_socket + .set_reuseaddr(true) + .expect("Unable to set SO_REUSEADDR on socket"); + + #[cfg(not(any(target_os = "solaris", target_os = "illumos")))] + listen_addr_socket + .set_reuseport(true) + .expect("Unable to set SO_REUSEPORT on socket"); + + listen_addr_socket + .bind(listen_addr) + .expect("Unable to bind on listen_addr socket"); + + let listener = listen_addr_socket + .listen(1024) + .expect("Unable to listen on configured address"); info!("Listening on {}", listen_addr); let (conn_in, mut conn_out) = mpsc::unbounded_channel();