@@ -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,61 @@ 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
- {
477
- 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
- }
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
+ unsafe {
459
+ SockAddr :: try_init ( |storage, len| {
460
+ // Safety: `SockAddr::try_init` zeros the address, which is a
461
+ // valid representation.
462
+ let storage: & mut libc:: sockaddr_un = unsafe { & mut * storage. cast ( ) } ;
463
+ let len: & mut socklen_t = unsafe { & mut * len } ;
464
+
465
+ let bytes = path. as_os_str ( ) . as_bytes ( ) ;
466
+ let too_long = match bytes. first ( ) {
467
+ None => false ,
468
+ // linux abstract namespaces aren't null-terminated
469
+ Some ( & 0 ) => bytes. len ( ) > storage. sun_path . len ( ) ,
470
+ Some ( _) => bytes. len ( ) >= storage. sun_path . len ( ) ,
471
+ } ;
472
+ if too_long {
473
+ return Err ( io:: Error :: new (
474
+ io:: ErrorKind :: InvalidInput ,
475
+ "path must be shorter than SUN_LEN" ,
476
+ ) ) ;
477
+ }
497
478
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
- )
479
+ storage. sun_family = libc:: AF_UNIX as sa_family_t ;
480
+ // Safety: `bytes` and `addr.sun_path` are not overlapping and
481
+ // both point to valid memory.
482
+ // `SockAddr::try_init` zeroes the memory, so the path is
483
+ // already null terminated.
484
+ unsafe {
485
+ ptr:: copy_nonoverlapping (
486
+ bytes. as_ptr ( ) ,
487
+ storage. sun_path . as_mut_ptr ( ) as * mut u8 ,
488
+ bytes. len ( ) ,
489
+ )
490
+ } ;
491
+
492
+ let base = storage as * const _ as usize ;
493
+ let path = & storage. sun_path as * const _ as usize ;
494
+ let sun_path_offset = path - base;
495
+ let length = sun_path_offset
496
+ + bytes. len ( )
497
+ + match bytes. first ( ) {
498
+ Some ( & 0 ) | None => 0 ,
499
+ Some ( _) => 1 ,
509
500
} ;
501
+ * len = length as socklen_t ;
510
502
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)
503
+ Ok ( ( ) )
504
+ } )
526
505
}
506
+ . map ( |( _, addr) | addr)
507
+ }
527
508
509
+ /// Unix only API.
510
+ impl SockAddr {
528
511
/// Constructs a `SockAddr` with the family `AF_VSOCK` and the provided CID/port.
529
512
///
530
513
/// # Errors
0 commit comments