@@ -25,7 +25,6 @@ use std::num::NonZeroU32;
2525 )
2626) ) ]
2727use std:: num:: NonZeroUsize ;
28- #[ cfg( feature = "all" ) ]
2928use std:: os:: unix:: ffi:: OsStrExt ;
3029#[ cfg( all(
3130 feature = "all" ,
@@ -40,9 +39,7 @@ use std::os::unix::io::RawFd;
4039use std:: os:: unix:: io:: { AsFd , AsRawFd , BorrowedFd , FromRawFd , IntoRawFd , OwnedFd } ;
4140#[ cfg( feature = "all" ) ]
4241use std:: os:: unix:: net:: { UnixDatagram , UnixListener , UnixStream } ;
43- #[ cfg( feature = "all" ) ]
4442use std:: path:: Path ;
45- #[ cfg( not( all( target_os = "redox" , not( feature = "all" ) ) ) ) ]
4643use std:: ptr;
4744use std:: time:: { Duration , Instant } ;
4845use std:: { io, slice} ;
@@ -58,7 +55,7 @@ use crate::{Domain, Protocol, SockAddr, TcpKeepalive, Type};
5855pub ( crate ) use libc:: c_int;
5956
6057// Used in `Domain`.
61- pub ( crate ) use libc:: { AF_INET , AF_INET6 } ;
58+ pub ( crate ) use libc:: { AF_INET , AF_INET6 , AF_UNIX } ;
6259// Used in `Type`.
6360#[ cfg( all( feature = "all" , not( target_os = "redox" ) ) ) ]
6461pub ( crate ) use libc:: SOCK_RAW ;
@@ -222,10 +219,6 @@ type IovLen = c_int;
222219
223220/// Unix only API.
224221impl Domain {
225- /// Domain for Unix socket communication, corresponding to `AF_UNIX`.
226- #[ cfg_attr( docsrs, doc( cfg( unix) ) ) ]
227- pub const UNIX : Domain = Domain ( libc:: AF_UNIX ) ;
228-
229222 /// Domain for low-level packet interface, corresponding to `AF_PACKET`.
230223 #[ cfg( all(
231224 feature = "all" ,
@@ -460,71 +453,56 @@ impl<'a> MaybeUninitSlice<'a> {
460453 }
461454}
462455
463- /// Unix only API.
464- impl SockAddr {
465- /// Constructs a `SockAddr` with the family `AF_UNIX` and the provided path.
466- ///
467- /// # Failure
468- ///
469- /// Returns an error if the path is longer than `SUN_LEN`.
470- #[ cfg( feature = "all" ) ]
471- #[ cfg_attr( docsrs, doc( cfg( all( unix, feature = "all" ) ) ) ) ]
472- #[ allow( unused_unsafe) ] // TODO: replace with `unsafe_op_in_unsafe_fn` once stable.
473- pub fn unix < P > ( path : P ) -> io:: Result < SockAddr >
474- where
475- P : AsRef < Path > ,
476- {
456+ #[ allow( unused_unsafe) ] // TODO: replace with `unsafe_op_in_unsafe_fn` once stable.
457+ pub ( crate ) fn unix_sockaddr ( path : & Path ) -> io:: Result < SockAddr > {
458+ // SAFETY: a `sockaddr_storage` of all zeros is valid.
459+ let mut storage = unsafe { mem:: zeroed :: < sockaddr_storage > ( ) } ;
460+ let len = {
461+ let storage: & mut libc:: sockaddr_un =
462+ unsafe { & mut * ( & mut storage as * mut sockaddr_storage ) . cast ( ) } ;
463+
464+ let bytes = path. as_os_str ( ) . as_bytes ( ) ;
465+ let too_long = match bytes. first ( ) {
466+ None => false ,
467+ // linux abstract namespaces aren't null-terminated
468+ Some ( & 0 ) => bytes. len ( ) > storage. sun_path . len ( ) ,
469+ Some ( _) => bytes. len ( ) >= storage. sun_path . len ( ) ,
470+ } ;
471+ if too_long {
472+ return Err ( io:: Error :: new (
473+ io:: ErrorKind :: InvalidInput ,
474+ "path must be shorter than SUN_LEN" ,
475+ ) ) ;
476+ }
477+
478+ storage. sun_family = libc:: AF_UNIX as sa_family_t ;
479+ // Safety: `bytes` and `addr.sun_path` are not overlapping and
480+ // both point to valid memory.
481+ // `storage` was initialized to zero above, so the path is
482+ // already null terminated.
477483 unsafe {
478- SockAddr :: try_init ( |storage, len| {
479- // Safety: `SockAddr::try_init` zeros the address, which is a
480- // valid representation.
481- let storage: & mut libc:: sockaddr_un = unsafe { & mut * storage. cast ( ) } ;
482- let len: & mut socklen_t = unsafe { & mut * len } ;
483-
484- let bytes = path. as_ref ( ) . as_os_str ( ) . as_bytes ( ) ;
485- let too_long = match bytes. first ( ) {
486- None => false ,
487- // linux abstract namespaces aren't null-terminated
488- Some ( & 0 ) => bytes. len ( ) > storage. sun_path . len ( ) ,
489- Some ( _) => bytes. len ( ) >= storage. sun_path . len ( ) ,
490- } ;
491- if too_long {
492- return Err ( io:: Error :: new (
493- io:: ErrorKind :: InvalidInput ,
494- "path must be shorter than SUN_LEN" ,
495- ) ) ;
496- }
484+ ptr:: copy_nonoverlapping (
485+ bytes. as_ptr ( ) ,
486+ storage. sun_path . as_mut_ptr ( ) as * mut u8 ,
487+ bytes. len ( ) ,
488+ )
489+ } ;
497490
498- storage. sun_family = libc:: AF_UNIX as sa_family_t ;
499- // Safety: `bytes` and `addr.sun_path` are not overlapping and
500- // both point to valid memory.
501- // `SockAddr::try_init` zeroes the memory, so the path is
502- // already null terminated.
503- unsafe {
504- ptr:: copy_nonoverlapping (
505- bytes. as_ptr ( ) ,
506- storage. sun_path . as_mut_ptr ( ) as * mut u8 ,
507- bytes. len ( ) ,
508- )
509- } ;
510-
511- let base = storage as * const _ as usize ;
512- let path = & storage. sun_path as * const _ as usize ;
513- let sun_path_offset = path - base;
514- let length = sun_path_offset
515- + bytes. len ( )
516- + match bytes. first ( ) {
517- Some ( & 0 ) | None => 0 ,
518- Some ( _) => 1 ,
519- } ;
520- * len = length as socklen_t ;
521-
522- Ok ( ( ) )
523- } )
524- }
525- . map ( |( _, addr) | addr)
526- }
491+ let base = storage as * const _ as usize ;
492+ let path = & storage. sun_path as * const _ as usize ;
493+ let sun_path_offset = path - base;
494+ sun_path_offset
495+ + bytes. len ( )
496+ + match bytes. first ( ) {
497+ Some ( & 0 ) | None => 0 ,
498+ Some ( _) => 1 ,
499+ }
500+ } ;
501+ Ok ( unsafe { SockAddr :: new ( storage, len as socklen_t ) } )
502+ }
527503
504+ /// Unix only API.
505+ impl SockAddr {
528506 /// Constructs a `SockAddr` with the family `AF_VSOCK` and the provided CID/port.
529507 ///
530508 /// # Errors
@@ -538,9 +516,8 @@ impl SockAddr {
538516 doc( cfg( all( feature = "all" , any( target_os = "android" , target_os = "linux" ) ) ) )
539517 ) ]
540518 pub fn vsock ( cid : u32 , port : u32 ) -> SockAddr {
541- // SAFETY: a `sockaddr_storage` of all zeros is valid, hence we can
542- // safely assume it's initialised.
543- let mut storage = unsafe { MaybeUninit :: < sockaddr_storage > :: zeroed ( ) . assume_init ( ) } ;
519+ // SAFETY: a `sockaddr_storage` of all zeros is valid.
520+ let mut storage = unsafe { mem:: zeroed :: < sockaddr_storage > ( ) } ;
544521 {
545522 let storage: & mut libc:: sockaddr_vm =
546523 unsafe { & mut * ( ( & mut storage as * mut sockaddr_storage ) . cast ( ) ) } ;
0 commit comments