@@ -25,7 +25,6 @@ use std::num::NonZeroU32;
25
25
)
26
26
) ) ]
27
27
use std:: num:: NonZeroUsize ;
28
- #[ cfg( feature = "all" ) ]
29
28
use std:: os:: unix:: ffi:: OsStrExt ;
30
29
#[ cfg( all(
31
30
feature = "all" ,
@@ -40,9 +39,7 @@ use std::os::unix::io::RawFd;
40
39
use std:: os:: unix:: io:: { AsFd , AsRawFd , BorrowedFd , FromRawFd , IntoRawFd , OwnedFd } ;
41
40
#[ cfg( feature = "all" ) ]
42
41
use std:: os:: unix:: net:: { UnixDatagram , UnixListener , UnixStream } ;
43
- #[ cfg( feature = "all" ) ]
44
42
use std:: path:: Path ;
45
- #[ cfg( not( all( target_os = "redox" , not( feature = "all" ) ) ) ) ]
46
43
use std:: ptr;
47
44
use std:: time:: { Duration , Instant } ;
48
45
use std:: { io, slice} ;
@@ -58,7 +55,7 @@ use crate::{Domain, Protocol, SockAddr, TcpKeepalive, Type};
58
55
pub ( crate ) use libc:: c_int;
59
56
60
57
// Used in `Domain`.
61
- pub ( crate ) use libc:: { AF_INET , AF_INET6 } ;
58
+ pub ( crate ) use libc:: { AF_INET , AF_INET6 , AF_UNIX } ;
62
59
// Used in `Type`.
63
60
#[ cfg( all( feature = "all" , not( target_os = "redox" ) ) ) ]
64
61
pub ( crate ) use libc:: SOCK_RAW ;
@@ -222,10 +219,6 @@ type IovLen = c_int;
222
219
223
220
/// Unix only API.
224
221
impl 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
-
229
222
/// Domain for low-level packet interface, corresponding to `AF_PACKET`.
230
223
#[ cfg( all(
231
224
feature = "all" ,
@@ -460,71 +453,56 @@ impl<'a> MaybeUninitSlice<'a> {
460
453
}
461
454
}
462
455
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.
477
483
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
+ } ;
497
490
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
+ }
527
503
504
+ /// Unix only API.
505
+ impl SockAddr {
528
506
/// Constructs a `SockAddr` with the family `AF_VSOCK` and the provided CID/port.
529
507
///
530
508
/// # Errors
@@ -538,9 +516,8 @@ impl SockAddr {
538
516
doc( cfg( all( feature = "all" , any( target_os = "android" , target_os = "linux" ) ) ) )
539
517
) ]
540
518
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 > ( ) } ;
544
521
{
545
522
let storage: & mut libc:: sockaddr_vm =
546
523
unsafe { & mut * ( ( & mut storage as * mut sockaddr_storage ) . cast ( ) ) } ;
0 commit comments