Skip to content

Commit 62a0f25

Browse files
authored
IPv6 By Default (#6808)
1 parent ceb5ecf commit 62a0f25

File tree

6 files changed

+77
-12
lines changed

6 files changed

+77
-12
lines changed

Cargo.lock

Lines changed: 38 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

beacon_node/lighthouse_network/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ hex = { workspace = true }
2222
itertools = { workspace = true }
2323
libp2p-mplex = "0.43"
2424
lighthouse_version = { workspace = true }
25+
local-ip-address = "0.6"
2526
lru = { workspace = true }
2627
lru_cache = { workspace = true }
2728
metrics = { workspace = true }

beacon_node/lighthouse_network/src/config.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use directory::{
66
DEFAULT_BEACON_NODE_DIR, DEFAULT_HARDCODED_NETWORK, DEFAULT_NETWORK_DIR, DEFAULT_ROOT_DIR,
77
};
88
use libp2p::Multiaddr;
9+
use local_ip_address::local_ipv6;
910
use serde::{Deserialize, Serialize};
1011
use sha2::{Digest, Sha256};
1112
use std::net::{Ipv4Addr, Ipv6Addr};
@@ -266,6 +267,18 @@ impl Config {
266267
}
267268
}
268269

270+
/// A helper function to check if the local host has a globally routeable IPv6 address. If so,
271+
/// returns true.
272+
pub fn is_ipv6_supported() -> bool {
273+
// If IPv6 is supported
274+
let Ok(std::net::IpAddr::V6(local_ip)) = local_ipv6() else {
275+
return false;
276+
};
277+
278+
// If its globally routable, return true
279+
is_global_ipv6(&local_ip)
280+
}
281+
269282
pub fn listen_addrs(&self) -> &ListenAddress {
270283
&self.listen_addresses
271284
}

beacon_node/src/cli.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -147,16 +147,16 @@ pub fn cli_app() -> Command {
147147
.long("listen-address")
148148
.value_name("ADDRESS")
149149
.help("The address lighthouse will listen for UDP and TCP connections. To listen \
150-
over IpV4 and IpV6 set this flag twice with the different values.\n\
150+
over IPv4 and IPv6 set this flag twice with the different values.\n\
151151
Examples:\n\
152152
- --listen-address '0.0.0.0' will listen over IPv4.\n\
153153
- --listen-address '::' will listen over IPv6.\n\
154154
- --listen-address '0.0.0.0' --listen-address '::' will listen over both \
155155
IPv4 and IPv6. The order of the given addresses is not relevant. However, \
156-
multiple IPv4, or multiple IPv6 addresses will not be accepted.")
156+
multiple IPv4, or multiple IPv6 addresses will not be accepted. \
157+
If omitted, Lighthouse will listen on all interfaces, for both IPv4 and IPv6.")
157158
.action(ArgAction::Append)
158159
.num_args(0..=2)
159-
.default_value("0.0.0.0")
160160
.display_order(0)
161161
)
162162
.arg(

beacon_node/src/config.rs

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -905,12 +905,13 @@ pub fn parse_listening_addresses(
905905
) -> Result<ListenAddress, String> {
906906
let listen_addresses_str = cli_args
907907
.get_many::<String>("listen-address")
908-
.expect("--listen_addresses has a default value");
908+
.unwrap_or_default();
909909
let use_zero_ports = parse_flag(cli_args, "zero-ports");
910910

911911
// parse the possible ips
912912
let mut maybe_ipv4 = None;
913913
let mut maybe_ipv6 = None;
914+
914915
for addr_str in listen_addresses_str {
915916
let addr = addr_str.parse::<IpAddr>().map_err(|parse_error| {
916917
format!("Failed to parse listen-address ({addr_str}) as an Ip address: {parse_error}")
@@ -920,17 +921,17 @@ pub fn parse_listening_addresses(
920921
IpAddr::V4(v4_addr) => match &maybe_ipv4 {
921922
Some(first_ipv4_addr) => {
922923
return Err(format!(
923-
"When setting the --listen-address option twice, use an IpV4 address and an Ipv6 address. \
924-
Got two IpV4 addresses {first_ipv4_addr} and {v4_addr}"
924+
"When setting the --listen-address option twice, use an IPv4 address and an IPv6 address. \
925+
Got two IPv4 addresses {first_ipv4_addr} and {v4_addr}"
925926
));
926927
}
927928
None => maybe_ipv4 = Some(v4_addr),
928929
},
929930
IpAddr::V6(v6_addr) => match &maybe_ipv6 {
930931
Some(first_ipv6_addr) => {
931932
return Err(format!(
932-
"When setting the --listen-address option twice, use an IpV4 address and an Ipv6 address. \
933-
Got two IpV6 addresses {first_ipv6_addr} and {v6_addr}"
933+
"When setting the --listen-address option twice, use an IPv4 address and an IPv6 address. \
934+
Got two IPv6 addresses {first_ipv6_addr} and {v6_addr}"
934935
));
935936
}
936937
None => maybe_ipv6 = Some(v6_addr),
@@ -984,11 +985,22 @@ pub fn parse_listening_addresses(
984985
format!("Failed to parse --quic6-port as an integer: {parse_error}")
985986
})?;
986987

988+
// Here we specify the default listening addresses for Lighthouse.
989+
// By default, we listen on 0.0.0.0.
990+
//
991+
// IF the host supports a globally routable IPv6 address, we also listen on ::.
992+
if matches!((maybe_ipv4, maybe_ipv6), (None, None)) {
993+
maybe_ipv4 = Some(Ipv4Addr::UNSPECIFIED);
994+
995+
if NetworkConfig::is_ipv6_supported() {
996+
maybe_ipv6 = Some(Ipv6Addr::UNSPECIFIED);
997+
}
998+
}
999+
9871000
// Now put everything together
9881001
let listening_addresses = match (maybe_ipv4, maybe_ipv6) {
9891002
(None, None) => {
990-
// This should never happen unless clap is broken
991-
return Err("No listening addresses provided".into());
1003+
unreachable!("This path is handled above this match statement");
9921004
}
9931005
(None, Some(ipv6)) => {
9941006
// A single ipv6 address was provided. Set the ports

book/src/help_bn.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -230,15 +230,16 @@ Options:
230230
peer without an ENR.
231231
--listen-address [<ADDRESS>...]
232232
The address lighthouse will listen for UDP and TCP connections. To
233-
listen over IpV4 and IpV6 set this flag twice with the different
233+
listen over IPv4 and IPv6 set this flag twice with the different
234234
values.
235235
Examples:
236236
- --listen-address '0.0.0.0' will listen over IPv4.
237237
- --listen-address '::' will listen over IPv6.
238238
- --listen-address '0.0.0.0' --listen-address '::' will listen over
239239
both IPv4 and IPv6. The order of the given addresses is not relevant.
240240
However, multiple IPv4, or multiple IPv6 addresses will not be
241-
accepted. [default: 0.0.0.0]
241+
accepted. If omitted, Lighthouse will listen on all interfaces, for
242+
both IPv4 and IPv6.
242243
--log-format <FORMAT>
243244
Specifies the log format used when emitting logs to the terminal.
244245
[possible values: JSON]

0 commit comments

Comments
 (0)