@@ -2459,16 +2459,17 @@ impl crate::Socket {
24592459 }
24602460 }
24612461
2462- /// Attach Berkeley Packet Filter(BPF) on this socket.
2462+ /// Attach Berkeley Packet Filter (BPF) on this socket.
24632463 ///
24642464 /// BPF allows a user-space program to attach a filter onto any socket
24652465 /// and allow or disallow certain types of data to come through the socket.
24662466 ///
24672467 /// For more information about this option, see [filter](https://www.kernel.org/doc/html/v5.12/networking/filter.html)
24682468 #[ cfg( all( feature = "all" , any( target_os = "linux" , target_os = "android" ) ) ) ]
2469- pub fn attach_filter ( & self , filters : & [ libc :: sock_filter ] ) -> io:: Result < ( ) > {
2469+ pub fn attach_filter ( & self , filters : & [ SockFilter ] ) -> io:: Result < ( ) > {
24702470 let prog = libc:: sock_fprog {
24712471 len : filters. len ( ) as u16 ,
2472+ // SAFETY: this is safe due to `repr(transparent)`.
24722473 filter : filters. as_ptr ( ) as * mut _ ,
24732474 } ;
24742475
@@ -2814,6 +2815,34 @@ impl crate::Socket {
28142815 }
28152816}
28162817
2818+ /// Berkeley Packet Filter (BPF).
2819+ ///
2820+ /// See [`Socket::attach_filter`].
2821+ ///
2822+ /// [`Socket::attach_filter`]: crate::Socket::attach_filter
2823+ #[ cfg( all( feature = "all" , any( target_os = "linux" , target_os = "android" ) ) ) ]
2824+ #[ repr( transparent) ]
2825+ pub struct SockFilter {
2826+ filter : libc:: sock_filter ,
2827+ }
2828+
2829+ #[ cfg( all( feature = "all" , any( target_os = "linux" , target_os = "android" ) ) ) ]
2830+ impl SockFilter {
2831+ /// Create new `SockFilter`.
2832+ pub fn new ( code : u16 , jt : u8 , jf : u8 , k : u32 ) -> SockFilter {
2833+ SockFilter {
2834+ filter : libc:: sock_filter { code, jt, jf, k } ,
2835+ }
2836+ }
2837+ }
2838+
2839+ #[ cfg( all( feature = "all" , any( target_os = "linux" , target_os = "android" ) ) ) ]
2840+ impl std:: fmt:: Debug for SockFilter {
2841+ fn fmt ( & self , f : & mut std:: fmt:: Formatter < ' _ > ) -> std:: fmt:: Result {
2842+ f. debug_struct ( "SockFilter" ) . finish_non_exhaustive ( )
2843+ }
2844+ }
2845+
28172846/// See [`Socket::dccp_available_ccids`].
28182847#[ cfg( all( feature = "all" , target_os = "linux" ) ) ]
28192848#[ derive( Debug ) ]
0 commit comments