Skip to content

Commit 3691b82

Browse files
authored
Fix nft socket error (#293)
1 parent e6b7fce commit 3691b82

11 files changed

Lines changed: 159 additions & 244 deletions

File tree

Cargo.lock

Lines changed: 33 additions & 57 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/enterprise/firewall/api.rs

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
#[cfg(any(target_os = "freebsd", target_os = "macos", target_os = "netbsd"))]
22
use std::fs::{File, OpenOptions};
33

4-
#[cfg(target_os = "linux")]
5-
use nftnl::Batch;
6-
74
use super::{FirewallError, FirewallRule, Policy, SnatBinding};
85

96
#[cfg(all(
@@ -18,26 +15,31 @@ const DEV_PF: &str = "/dev/null";
1815
const 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

3128
impl 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
}

src/enterprise/firewall/iprange.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,10 @@ pub enum IpAddrRangeError {
2424

2525
impl fmt::Display for IpAddrRangeError {
2626
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
27-
match self {
28-
Self::MixedTypes => write!(f, "mixed IPv4 and IPv6 addresses"),
29-
Self::WrongOrder => write!(f, "wrong order: higher address preceeds lower"),
30-
}
27+
f.write_str(match self {
28+
Self::MixedTypes => "mixed IPv4 and IPv6 addresses",
29+
Self::WrongOrder => "wrong order: higher address preceeds lower",
30+
})
3131
}
3232
}
3333

src/enterprise/firewall/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ impl fmt::Display for Protocol {
177177
Self::Udp => "udp",
178178
Self::IcmpV6 => "icmp6",
179179
};
180-
write!(f, "{protocol}")
180+
f.write_str(protocol)
181181
}
182182
}
183183

0 commit comments

Comments
 (0)