Skip to content

Commit

Permalink
util: ensure IPv4 is enabled before binding socket.
Browse files Browse the repository at this point in the history
Seems to be necessary to have any effect.
  • Loading branch information
surban committed Feb 6, 2024
1 parent 8a11492 commit 1f9c25d
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 10 deletions.
11 changes: 6 additions & 5 deletions aggligator-util/src/bin/agg-tunnel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use std::{
};
use tokio::{
io::{AsyncRead, AsyncReadExt, AsyncWrite, AsyncWriteExt},
net::{TcpListener, TcpStream},
net::{TcpListener, TcpSocket, TcpStream},
select,
sync::{broadcast, mpsc, oneshot, watch},
task::block_in_place,
Expand Down Expand Up @@ -259,11 +259,12 @@ impl ClientCli {
let mut port_tasks = Vec::new();
for (server_port, client_port) in ports {
let listeners = if self.global {
let listener = TcpListener::bind(SocketAddr::new(Ipv6Addr::UNSPECIFIED.into(), client_port))
.await
let socket = TcpSocket::new_v6()?;
let _ = SockRef::from(&socket).set_only_v6(false);
socket
.bind(SocketAddr::new(Ipv6Addr::UNSPECIFIED.into(), client_port))
.context(format!("cannot bind to local port {client_port}"))?;
let _ = SockRef::from(&listener).set_only_v6(false);
vec![listener]
vec![socket.listen(16)?]
} else {
let listener_v4 = TcpListener::bind(SocketAddr::new(Ipv4Addr::LOCALHOST.into(), client_port))
.await
Expand Down
15 changes: 10 additions & 5 deletions aggligator-util/src/transport/tcp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -349,11 +349,16 @@ impl TcpAcceptor {
let mut listeners = Vec::new();

for addr in addrs {
let listener = TcpListener::bind(addr).await?;
if addr.is_ipv6() {
let _ = SockRef::from(&listener).set_only_v6(false);
}
listeners.push(listener);
let socket = match addr {
SocketAddr::V4(_) => TcpSocket::new_v4()?,
SocketAddr::V6(_) => {
let socket = TcpSocket::new_v6()?;
let _ = SockRef::from(&socket).set_only_v6(false);
socket
}
};
socket.bind(addr)?;
listeners.push(socket.listen(16)?);
}

Self::from_listeners(listeners)
Expand Down

0 comments on commit 1f9c25d

Please sign in to comment.