Skip to content

Commit 1acffc4

Browse files
authored
HELP-68823 Fix invalid_me comparison and normalize host strings (#1319)
1 parent 206b99e commit 1acffc4

File tree

3 files changed

+35
-3
lines changed

3 files changed

+35
-3
lines changed

src/client/options.rs

+10-2
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use std::{
1111
convert::TryFrom,
1212
fmt::{self, Display, Formatter, Write},
1313
hash::{Hash, Hasher},
14-
net::Ipv6Addr,
14+
net::{Ipv4Addr, Ipv6Addr},
1515
path::PathBuf,
1616
str::FromStr,
1717
time::Duration,
@@ -277,6 +277,14 @@ impl ServerAddress {
277277
.into());
278278
}
279279

280+
let normalized_hostname = if let Ok(v4) = hostname.parse::<Ipv4Addr>() {
281+
v4.to_string()
282+
} else if let Ok(v6) = hostname.parse::<Ipv6Addr>() {
283+
v6.to_string()
284+
} else {
285+
hostname.to_lowercase()
286+
};
287+
280288
let port = if let Some(port) = port {
281289
match u16::from_str(port) {
282290
Ok(0) | Err(_) => {
@@ -296,7 +304,7 @@ impl ServerAddress {
296304
};
297305

298306
Ok(Self::Tcp {
299-
host: hostname.to_lowercase(),
307+
host: normalized_hostname,
300308
port,
301309
})
302310
}

src/sdam/description/server.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -338,7 +338,7 @@ impl ServerDescription {
338338
pub(crate) fn invalid_me(&self) -> Result<bool> {
339339
if let Some(ref reply) = self.reply.as_ref().map_err(Clone::clone)? {
340340
if let Some(ref me) = reply.command_response.me {
341-
return Ok(&self.address.to_string() != me);
341+
return Ok(self.address != ServerAddress::parse(me)?);
342342
}
343343
}
344344

src/sdam/test.rs

+24
Original file line numberDiff line numberDiff line change
@@ -335,3 +335,27 @@ async fn removed_server_monitor_stops() -> crate::error::Result<()> {
335335

336336
Ok(())
337337
}
338+
339+
#[test]
340+
fn ipv6_invalid_me() {
341+
let addr = ServerAddress::Tcp {
342+
host: "::1".to_string(),
343+
port: Some(8191),
344+
};
345+
let desc = ServerDescription {
346+
address: addr.clone(),
347+
server_type: super::ServerType::RsSecondary,
348+
last_update_time: None,
349+
average_round_trip_time: None,
350+
reply: Ok(Some(crate::hello::HelloReply {
351+
server_address: addr.clone(),
352+
command_response: crate::hello::HelloCommandResponse {
353+
me: Some("[::1]:8191".to_string()),
354+
..Default::default()
355+
},
356+
raw_command_response: bson::RawDocumentBuf::new(),
357+
cluster_time: None,
358+
})),
359+
};
360+
assert!(!desc.invalid_me().unwrap());
361+
}

0 commit comments

Comments
 (0)