11#[ cfg( any( target_os = "freebsd" , target_os = "macos" , target_os = "netbsd" ) ) ]
22use std:: fs:: { File , OpenOptions } ;
33
4- #[ cfg( target_os = "linux" ) ]
5- use nftnl:: Batch ;
6-
74use super :: { FirewallError , FirewallRule , Policy , SnatBinding } ;
85
96#[ cfg( all(
@@ -18,26 +15,31 @@ const DEV_PF: &str = "/dev/null";
1815const DEV_PF : & str = "/dev/pf" ;
1916
2017#[ allow( dead_code) ]
21- pub struct FirewallApi {
18+ pub ( crate ) struct FirewallApi {
2219 pub ( crate ) ifname : String ,
2320 #[ cfg( any( target_os = "freebsd" , target_os = "macos" , target_os = "netbsd" ) ) ]
2421 pub ( crate ) file : File ,
2522 #[ cfg( any( target_os = "freebsd" , target_os = "macos" , target_os = "netbsd" ) ) ]
2623 pub ( crate ) default_policy : Policy ,
2724 #[ cfg( target_os = "linux" ) ]
28- pub ( crate ) batch : Option < Batch > ,
25+ pub ( crate ) socket : mnl :: Socket ,
2926}
3027
3128impl FirewallApi {
32- pub fn new < S : Into < String > > ( ifname : S ) -> Result < Self , FirewallError > {
29+ pub ( crate ) fn new < S > ( ifname : S ) -> Result < Self , FirewallError >
30+ where
31+ S : Into < String > ,
32+ {
3333 Ok ( Self {
3434 ifname : ifname. into ( ) ,
3535 #[ cfg( any( target_os = "freebsd" , target_os = "macos" , target_os = "netbsd" ) ) ]
3636 file : OpenOptions :: new ( ) . read ( true ) . write ( true ) . open ( DEV_PF ) ?,
3737 #[ cfg( any( target_os = "freebsd" , target_os = "macos" , target_os = "netbsd" ) ) ]
3838 default_policy : Policy :: Deny ,
3939 #[ cfg( target_os = "linux" ) ]
40- batch : None ,
40+ socket : mnl:: Socket :: new ( mnl:: Bus :: Netfilter ) . map_err ( |err| {
41+ FirewallError :: NetlinkError ( format ! ( "Failed to create socket: {err:?}" ) )
42+ } ) ?,
4143 } )
4244 }
4345}
@@ -51,18 +53,12 @@ pub(crate) trait FirewallManagementApi {
5153 fn cleanup ( & mut self ) -> Result < ( ) , FirewallError > ;
5254
5355 /// Add firewall rules.
54- fn add_rules ( & mut self , rules : Vec < FirewallRule > ) -> Result < ( ) , FirewallError > ;
56+ fn add_rules ( & mut self , rules : & [ FirewallRule ] ) -> Result < ( ) , FirewallError > ;
5557
5658 /// Setup Network Address Translation using POSTROUTING chain rules
5759 fn setup_nat (
5860 & mut self ,
5961 masquerade_enabled : bool ,
6062 snat_bindings : & [ SnatBinding ] ,
6163 ) -> Result < ( ) , FirewallError > ;
62-
63- /// Begin rule transaction.
64- fn begin ( & mut self ) -> Result < ( ) , FirewallError > ;
65-
66- /// Commit rule transaction.
67- fn commit ( & mut self ) -> Result < ( ) , FirewallError > ;
6864}
0 commit comments