File tree 2 files changed +38
-0
lines changed
2 files changed +38
-0
lines changed Original file line number Diff line number Diff line change 1
1
#[ cfg( any( doc, target_os = "android" , target_os = "linux" ) ) ]
2
2
use super :: { recv_vectored_with_ancillary_from, send_vectored_with_ancillary_to, SocketAncillary } ;
3
3
use super :: { sockaddr_un, SocketAddr } ;
4
+ use crate :: ffi:: CStr ;
4
5
use crate :: fmt;
5
6
use crate :: io:: { self , IoSlice , IoSliceMut } ;
6
7
use crate :: net:: Shutdown ;
@@ -457,6 +458,21 @@ impl UnixStream {
457
458
self . 0 . passcred ( )
458
459
}
459
460
461
+ /// Set a filter name on the socket to filter incoming connections to defer it before accept(2)
462
+ ///
463
+ #[ cfg( any( doc, target_os = "netbsd" , target_os = "freebsd" ) ) ]
464
+ #[ unstable( feature = "acceptfilter" , issue = "none" ) ]
465
+ pub fn set_acceptfilter ( & self , name : & CStr ) -> io:: Result < ( ) > {
466
+ self . 0 . set_acceptfilter ( name)
467
+ }
468
+
469
+ /// Get a filter name if one had been set previously on the socket.
470
+ ///
471
+ #[ cfg( any( doc, target_os = "netbsd" , target_os = "freebsd" ) ) ]
472
+ #[ unstable( feature = "acceptfilter" , issue = "none" ) ]
473
+ pub fn acceptfilter ( & self ) -> io:: Result < & CStr > {
474
+ self . 0 . acceptfilter ( )
475
+ }
460
476
/// Set the id of the socket for network filtering purpose
461
477
///
462
478
#[ cfg_attr(
Original file line number Diff line number Diff line change @@ -453,6 +453,28 @@ impl Socket {
453
453
Ok ( raw as u32 )
454
454
}
455
455
456
+ #[ cfg( any( target_os = "freebsd" , target_os = "netbsd" ) ) ]
457
+ pub fn set_acceptfilter ( & self , name : & CStr ) -> io:: Result < ( ) > {
458
+ const AF_NAME_MAX : usize = 16 ;
459
+ let mut buf = [ 0 ; AF_NAME_MAX ] ;
460
+ for ( src, dst) in name. to_bytes ( ) . iter ( ) . zip ( & mut buf[ ..AF_NAME_MAX - 1 ] ) {
461
+ * dst = * src as i8 ;
462
+ }
463
+ let mut arg: libc:: accept_filter_arg = unsafe { mem:: zeroed ( ) } ;
464
+ arg. af_name = buf;
465
+ setsockopt ( self , libc:: SOL_SOCKET , libc:: SO_ACCEPTFILTER , & mut arg)
466
+ }
467
+
468
+ #[ cfg( any( target_os = "freebsd" , target_os = "netbsd" ) ) ]
469
+ pub fn acceptfilter ( & self ) -> io:: Result < & CStr > {
470
+ let arg: libc:: accept_filter_arg =
471
+ getsockopt ( self , libc:: SOL_SOCKET , libc:: SO_ACCEPTFILTER ) ?;
472
+ let s: & [ u8 ] =
473
+ unsafe { core:: slice:: from_raw_parts ( arg. af_name . as_ptr ( ) as * const u8 , 16 ) } ;
474
+ let name = CStr :: from_bytes_with_nul ( s) . unwrap ( ) ;
475
+ Ok ( name)
476
+ }
477
+
456
478
#[ cfg( any( target_os = "android" , target_os = "linux" , ) ) ]
457
479
pub fn set_passcred ( & self , passcred : bool ) -> io:: Result < ( ) > {
458
480
setsockopt ( self , libc:: SOL_SOCKET , libc:: SO_PASSCRED , passcred as libc:: c_int )
You can’t perform that action at this time.
0 commit comments