Skip to content

Commit 350bf86

Browse files
madmajestrocgzones
authored andcommitted
Fallback to IPv4 when IPv6 is not available
By default, apt-cacher-rs attempts to listen on all IPv6 addresses by using the address "::". This will result in errors if the IPv6 stack is not active or not available. Therefor, apt-cacher-rs now attempts to listen on all IPv4 addresses as a fallback, when the default address "::" is used.
1 parent d619162 commit 350bf86

File tree

1 file changed

+18
-6
lines changed

1 file changed

+18
-6
lines changed

Diff for: src/main.rs

+18-6
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ use std::error::Error;
2424
use std::hash::Hash;
2525
use std::hash::Hasher;
2626
use std::io::ErrorKind;
27-
use std::net::SocketAddr;
27+
use std::net::{Ipv4Addr, Ipv6Addr, SocketAddr};
2828
use std::num::NonZero;
2929
use std::num::NonZeroU16;
3030
use std::num::NonZeroU64;
@@ -2705,12 +2705,24 @@ fn is_timeout(err: &hyper::Error) -> bool {
27052705
async fn main_loop() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
27062706
let config = global_config();
27072707

2708-
let addr = SocketAddr::from((config.bind_addr, config.bind_port.get()));
2708+
let mut addr = SocketAddr::from((config.bind_addr, config.bind_port.get()));
27092709

2710-
let listener = TcpListener::bind(addr).await.map_err(|err| {
2711-
error!("Error binding on {addr}: {err}");
2712-
err
2713-
})?;
2710+
let listener = match TcpListener::bind(addr).await {
2711+
Ok(x) => x,
2712+
Err(err) => {
2713+
if config.bind_addr != Ipv6Addr::UNSPECIFIED {
2714+
error!("Error binding on {addr}: {err}");
2715+
Err(err)?;
2716+
}
2717+
2718+
// Fallback to IPv4 to avoid errors when IPv6 is not available and the default configuration is used.
2719+
addr = SocketAddr::from((Ipv4Addr::UNSPECIFIED, config.bind_port.get()));
2720+
TcpListener::bind(addr).await.map_err(|err| {
2721+
error!("Error binding fallback on {addr}: {err}");
2722+
err
2723+
})?
2724+
}
2725+
};
27142726
info!("Listening on http://{addr}");
27152727

27162728
#[cfg(all(feature = "tls_default", not(feature = "tls_rustls")))]

0 commit comments

Comments
 (0)