Skip to content

Commit

Permalink
Fix ipv6 parsing.
Browse files Browse the repository at this point in the history
  • Loading branch information
davidv1992 committed Jan 24, 2025
1 parent 9833caa commit f77e8b0
Showing 1 changed file with 8 additions and 9 deletions.
17 changes: 8 additions & 9 deletions ntpd/src/daemon/config/ntp_source.rs
Original file line number Diff line number Diff line change
Expand Up @@ -275,17 +275,12 @@ impl NormalizedAddress {
if address.split(':').count() > 2 {
// IPv6, try to parse it as such
match address.parse::<SocketAddr>() {
Ok(socket_addr) => {
// strip off the port
let (server_name, _) = address.rsplit_once(':').unwrap();

Ok((server_name.to_string(), socket_addr.port()))
}
Ok(socket_addr) => Ok((socket_addr.ip().to_string(), socket_addr.port())),
Err(e) => {
// Could be because of no port, add one and see
let address_with_port = format!("[{address}]:{default_port}");
if address_with_port.parse::<SocketAddr>().is_ok() {
Ok((format!("[{address}]"), default_port))
if let Ok(socket_addr) = address_with_port.parse::<SocketAddr>() {
Ok((socket_addr.ip().to_string(), socket_addr.port()))
} else {
Err(std::io::Error::new(std::io::ErrorKind::Other, e))
}
Expand Down Expand Up @@ -353,7 +348,11 @@ impl NormalizedAddress {

impl std::fmt::Display for NormalizedAddress {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}:{}", self.server_name, self.port)
if self.server_name.contains(':') {
write!(f, "[{}]:{}", self.server_name, self.port)
} else {
write!(f, "{}:{}", self.server_name, self.port)
}
}
}

Expand Down

0 comments on commit f77e8b0

Please sign in to comment.