aboutsummaryrefslogtreecommitdiff
path: root/rustables/src
diff options
context:
space:
mode:
Diffstat (limited to 'rustables/src')
-rw-r--r--rustables/src/chain.rs18
-rw-r--r--rustables/src/expr/lookup.rs10
-rw-r--r--rustables/src/rule.rs39
-rw-r--r--rustables/src/set.rs12
-rw-r--r--rustables/src/table.rs15
5 files changed, 51 insertions, 43 deletions
diff --git a/rustables/src/chain.rs b/rustables/src/chain.rs
index 4bf33b0..a616f90 100644
--- a/rustables/src/chain.rs
+++ b/rustables/src/chain.rs
@@ -86,11 +86,9 @@ impl Chain {
sys::NFTNL_CHAIN_FAMILY as u16,
table.get_family() as u32,
);
- sys::nftnl_chain_set_str(
- chain,
- sys::NFTNL_CHAIN_TABLE as u16,
- table.get_name().as_ptr(),
- );
+ if let Some(table_name) = table.get_name() {
+ sys::nftnl_chain_set_str(chain, sys::NFTNL_CHAIN_TABLE as u16, table_name.as_ptr());
+ }
sys::nftnl_chain_set_str(chain, sys::NFTNL_CHAIN_NAME as u16, name.as_ref().as_ptr());
Chain { chain, table }
}
@@ -153,10 +151,14 @@ impl Chain {
}
/// Returns the name of this chain.
- pub fn get_name(&self) -> &CStr {
+ pub fn get_name(&self) -> Option<&CStr> {
unsafe {
let ptr = sys::nftnl_chain_get_str(self.chain, sys::NFTNL_CHAIN_NAME as u16);
- CStr::from_ptr(ptr)
+ if !ptr.is_null() {
+ Some(CStr::from_ptr(ptr))
+ } else {
+ None
+ }
}
}
@@ -266,7 +268,7 @@ pub fn get_chains_cb<'a>(
}
};
- if table_name != table.get_name() {
+ if Some(table_name) != table.get_name() {
sys::nftnl_chain_free(chain);
return mnl::mnl_sys::MNL_CB_OK;
}
diff --git a/rustables/src/expr/lookup.rs b/rustables/src/expr/lookup.rs
index ac22440..bab09c2 100644
--- a/rustables/src/expr/lookup.rs
+++ b/rustables/src/expr/lookup.rs
@@ -10,11 +10,13 @@ pub struct Lookup {
}
impl Lookup {
- pub fn new<K>(set: &Set<'_, K>) -> Self {
- Lookup {
- set_name: set.get_name().to_owned(),
+ /// Creates a new lookup entry.
+ /// May return None if the set have no name.
+ pub fn new<K>(set: &Set<'_, K>) -> Option<Self> {
+ set.get_name().map(|set_name| Lookup {
+ set_name: set_name.to_owned(),
set_id: set.get_id(),
- }
+ })
}
}
diff --git a/rustables/src/rule.rs b/rustables/src/rule.rs
index 09a465e..7ab0de9 100644
--- a/rustables/src/rule.rs
+++ b/rustables/src/rule.rs
@@ -24,16 +24,12 @@ impl Rule {
sys::NFTNL_RULE_FAMILY as u16,
chain.get_table().get_family() as u32,
);
- sys::nftnl_rule_set_str(
- rule,
- sys::NFTNL_RULE_TABLE as u16,
- chain.get_table().get_name().as_ptr(),
- );
- sys::nftnl_rule_set_str(
- rule,
- sys::NFTNL_RULE_CHAIN as u16,
- chain.get_name().as_ptr(),
- );
+ if let Some(table_name) = chain.get_table().get_name() {
+ sys::nftnl_rule_set_str(rule, sys::NFTNL_RULE_TABLE as u16, table_name.as_ptr());
+ }
+ if let Some(chain_name) = chain.get_name() {
+ sys::nftnl_rule_set_str(rule, sys::NFTNL_RULE_CHAIN as u16, chain_name.as_ptr());
+ }
Rule { rule, chain }
}
@@ -83,10 +79,11 @@ impl Rule {
pub fn get_userdata(&self) -> Option<&CStr> {
unsafe {
let ptr = sys::nftnl_rule_get_str(self.rule, sys::NFTNL_RULE_USERDATA as u16);
- if ptr == std::ptr::null() {
- return None;
+ if !ptr.is_null() {
+ Some(CStr::from_ptr(ptr))
+ } else {
+ None
}
- Some(CStr::from_ptr(ptr))
}
}
@@ -241,21 +238,17 @@ pub fn list_rules_for_chain(chain: &Rc<Chain>) -> Result<Vec<Rule>, crate::query
return Err(crate::query::Error::NetlinkAllocationFailed);
}
- sys::nftnl_rule_set_str(
- rule,
- sys::NFTNL_RULE_TABLE as u16,
- chain.get_table().get_name().as_ptr(),
- );
+ if let Some(table_name) = chain.get_table().get_name() {
+ sys::nftnl_rule_set_str(rule, sys::NFTNL_RULE_TABLE as u16, table_name.as_ptr());
+ }
sys::nftnl_rule_set_u32(
rule,
sys::NFTNL_RULE_FAMILY as u16,
chain.get_table().get_family() as u32,
);
- sys::nftnl_rule_set_str(
- rule,
- sys::NFTNL_RULE_CHAIN as u16,
- chain.get_name().as_ptr(),
- );
+ if let Some(chain_name) = chain.get_name() {
+ sys::nftnl_rule_set_str(rule, sys::NFTNL_RULE_CHAIN as u16, chain_name.as_ptr());
+ }
sys::nftnl_rule_nlmsg_build_payload(hdr, rule);
diff --git a/rustables/src/set.rs b/rustables/src/set.rs
index 820e368..c099088 100644
--- a/rustables/src/set.rs
+++ b/rustables/src/set.rs
@@ -42,7 +42,9 @@ impl<'a, K> Set<'a, K> {
let set = try_alloc!(sys::nftnl_set_alloc());
sys::nftnl_set_set_u32(set, sys::NFTNL_SET_FAMILY as u16, family as u32);
- sys::nftnl_set_set_str(set, sys::NFTNL_SET_TABLE as u16, table.get_name().as_ptr());
+ if let Some(table_name) = table.get_name() {
+ sys::nftnl_set_set_str(set, sys::NFTNL_SET_TABLE as u16, table_name.as_ptr());
+ }
sys::nftnl_set_set_str(set, sys::NFTNL_SET_NAME as u16, name.as_ptr());
sys::nftnl_set_set_u32(set, sys::NFTNL_SET_ID as u16, id);
@@ -130,10 +132,14 @@ impl<'a, K> Set<'a, K> {
}
}
- pub fn get_name(&self) -> &CStr {
+ pub fn get_name(&self) -> Option<&CStr> {
unsafe {
let ptr = sys::nftnl_set_get_str(self.set, sys::NFTNL_SET_NAME as u16);
- CStr::from_ptr(ptr)
+ if !ptr.is_null() {
+ Some(CStr::from_ptr(ptr))
+ } else {
+ None
+ }
}
}
diff --git a/rustables/src/table.rs b/rustables/src/table.rs
index dc09b5e..53a967f 100644
--- a/rustables/src/table.rs
+++ b/rustables/src/table.rs
@@ -35,10 +35,14 @@ impl Table {
}
/// Returns the name of this table.
- pub fn get_name(&self) -> &CStr {
+ pub fn get_name(&self) -> Option<&CStr> {
unsafe {
let ptr = sys::nftnl_table_get_str(self.table, sys::NFTNL_TABLE_NAME as u16);
- CStr::from_ptr(ptr)
+ if !ptr.is_null() {
+ Some(CStr::from_ptr(ptr))
+ } else {
+ None
+ }
}
}
@@ -66,10 +70,11 @@ impl Table {
pub fn get_userdata(&self) -> Option<&CStr> {
unsafe {
let ptr = sys::nftnl_table_get_str(self.table, sys::NFTNL_TABLE_USERDATA as u16);
- if ptr == std::ptr::null() {
- return None;
+ if !ptr.is_null() {
+ Some(CStr::from_ptr(ptr))
+ } else {
+ None
}
- Some(CStr::from_ptr(ptr))
}
}