@@ -2459,16 +2459,17 @@ impl crate::Socket {
2459
2459
}
2460
2460
}
2461
2461
2462
- /// Attach Berkeley Packet Filter(BPF) on this socket.
2462
+ /// Attach Berkeley Packet Filter (BPF) on this socket.
2463
2463
///
2464
2464
/// BPF allows a user-space program to attach a filter onto any socket
2465
2465
/// and allow or disallow certain types of data to come through the socket.
2466
2466
///
2467
2467
/// For more information about this option, see [filter](https://www.kernel.org/doc/html/v5.12/networking/filter.html)
2468
2468
#[ 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 < ( ) > {
2470
2470
let prog = libc:: sock_fprog {
2471
2471
len : filters. len ( ) as u16 ,
2472
+ // SAFETY: this is safe due to `repr(transparent)`.
2472
2473
filter : filters. as_ptr ( ) as * mut _ ,
2473
2474
} ;
2474
2475
@@ -2814,6 +2815,34 @@ impl crate::Socket {
2814
2815
}
2815
2816
}
2816
2817
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
+
2817
2846
/// See [`Socket::dccp_available_ccids`].
2818
2847
#[ cfg( all( feature = "all" , target_os = "linux" ) ) ]
2819
2848
#[ derive( Debug ) ]
0 commit comments