Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix ipv6 parsing. #1804

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading