@@ -339,17 +339,29 @@ impl Utmpx {
339339}
340340
341341pub ( crate ) fn canonicalize_host ( host : String ) -> IOResult < String > {
342- let ( hostname, display) = host. split_once ( ':' ) . unwrap_or ( ( & host, "" ) ) ;
343-
344- if !hostname. is_empty ( ) {
342+ canonicalize_host_with_lookup ( host, |hostname| {
345343 use dns_lookup:: { AddrInfoHints , getaddrinfo} ;
346344
347345 const AI_CANONNAME : i32 = 0x2 ;
348346 let hints = AddrInfoHints {
349347 flags : AI_CANONNAME ,
350348 ..AddrInfoHints :: default ( )
351349 } ;
352- if let Ok ( sockets) = getaddrinfo ( Some ( hostname) , None , Some ( hints) ) {
350+ getaddrinfo ( Some ( hostname) , None , Some ( hints) )
351+ } )
352+ }
353+
354+ fn canonicalize_host_with_lookup < I > (
355+ host : String ,
356+ lookup : impl FnOnce ( & str ) -> Result < I , dns_lookup:: LookupError > ,
357+ ) -> IOResult < String >
358+ where
359+ I : Iterator < Item = IOResult < dns_lookup:: AddrInfo > > ,
360+ {
361+ let ( hostname, display) = host. split_once ( ':' ) . unwrap_or ( ( & host, "" ) ) ;
362+
363+ if !hostname. is_empty ( ) {
364+ if let Ok ( sockets) = lookup ( hostname) {
353365 let sockets = sockets. collect :: < IOResult < Vec < _ > > > ( ) ?;
354366 for socket in sockets {
355367 if let Some ( ai_canonname) = socket. canonname {
@@ -521,7 +533,7 @@ impl UtmpxRecord {
521533 match self {
522534 Self :: Traditional ( utmpx) => utmpx. canon_host ( ) ,
523535 #[ cfg( all( feature = "systemd_logind" , target_os = "linux" ) ) ]
524- Self :: Systemd ( systemd) => systemd. try_canon_host ( ) ,
536+ Self :: Systemd ( systemd) => systemd. canon_host ( ) ,
525537 }
526538 }
527539}
@@ -564,3 +576,51 @@ impl Drop for UtmpxIter {
564576 }
565577 }
566578}
579+
580+ #[ cfg( test) ]
581+ mod tests {
582+ use super :: * ;
583+
584+ #[ test]
585+ fn canonical_host_preserves_display_suffix ( ) {
586+ let host = canonicalize_host_with_lookup ( "REMOTE:0.1" . into ( ) , |hostname| {
587+ assert_eq ! ( hostname, "REMOTE" ) ;
588+ Ok ( std:: iter:: once ( Ok ( dns_lookup:: AddrInfo {
589+ flags : 0 ,
590+ address : 0 ,
591+ socktype : 0 ,
592+ protocol : 0 ,
593+ sockaddr : ( [ 192 , 0 , 2 , 1 ] , 0 ) . into ( ) ,
594+ canonname : Some ( "remote.example" . into ( ) ) ,
595+ } ) ) )
596+ } )
597+ . unwrap ( ) ;
598+
599+ assert_eq ! ( host, "remote.example:0.1" ) ;
600+ }
601+
602+ #[ test]
603+ fn canonical_host_lookup_failure_returns_hostname ( ) {
604+ let host = canonicalize_host_with_lookup ( "remote.invalid:0" . into ( ) , |_| {
605+ Err :: < std:: iter:: Empty < IOResult < dns_lookup:: AddrInfo > > , _ > (
606+ std:: io:: Error :: other ( "lookup failed" ) . into ( ) ,
607+ )
608+ } )
609+ . unwrap ( ) ;
610+
611+ assert_eq ! ( host, "remote.invalid" ) ;
612+ }
613+
614+ #[ test]
615+ fn canonical_host_propagates_address_errors ( ) {
616+ let error = canonicalize_host_with_lookup ( "remote.example:0" . into ( ) , |_| {
617+ Ok ( std:: iter:: once ( Err ( std:: io:: Error :: new (
618+ std:: io:: ErrorKind :: InvalidData ,
619+ "invalid address" ,
620+ ) ) ) )
621+ } )
622+ . unwrap_err ( ) ;
623+
624+ assert_eq ! ( error. kind( ) , std:: io:: ErrorKind :: InvalidData ) ;
625+ }
626+ }
0 commit comments