Change ipset version option from 4/6/46 to ipv4/ipv6/ip

This commit is contained in:
ppom 2026-02-12 12:00:00 +01:00
commit d629d57a7e
No known key found for this signature in database
3 changed files with 31 additions and 27 deletions

View file

@ -8,13 +8,13 @@ use crate::ipset::{CreateSet, IpSet, Order, SetChain, Version};
#[derive(Default, Serialize, Deserialize, PartialEq, Eq, Clone, Copy)]
pub enum IpVersion {
#[serde(rename = "4")]
V4,
#[serde(rename = "6")]
V6,
#[serde(rename = "46")]
#[default]
V46,
#[serde(rename = "ip")]
Ip,
#[serde(rename = "ipv4")]
Ipv4,
#[serde(rename = "ipv6")]
Ipv6,
}
impl Debug for IpVersion {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
@ -22,9 +22,9 @@ impl Debug for IpVersion {
f,
"{}",
match self {
IpVersion::V4 => "4",
IpVersion::V6 => "6",
IpVersion::V46 => "46",
IpVersion::Ipv4 => "ipv4",
IpVersion::Ipv6 => "ipv6",
IpVersion::Ip => "ip",
}
)
}
@ -84,9 +84,9 @@ impl ActionOptions {
pub struct SetOptions {
/// The IP type.
/// Defaults to `46`.
/// If `4`: creates an IPv4 set with this name
/// If `6`: creates an IPv6 set with this name
/// If `46`: creates an IPv4 set with its name suffixed by 'v4' AND an IPv6 set with its name suffixed by 'v6'
/// If `ipv4`: creates an IPv4 set with this name
/// If `ipv6`: creates an IPv6 set with this name
/// If `ip`: creates an IPv4 set with its name suffixed by 'v4' AND an IPv6 set with its name suffixed by 'v6'
/// *Merged set-wise*.
#[serde(default)]
version: Option<IpVersion>,
@ -242,14 +242,14 @@ impl SetNames {
pub fn new(name: String, version: Option<IpVersion>) -> Self {
Self {
ipv4: match version {
Some(IpVersion::V4) => Some(name.clone()),
Some(IpVersion::V6) => None,
None | Some(IpVersion::V46) => Some(format!("{}v4", name)),
Some(IpVersion::Ipv4) => Some(name.clone()),
Some(IpVersion::Ipv6) => None,
None | Some(IpVersion::Ip) => Some(format!("{}v4", name)),
},
ipv6: match version {
Some(IpVersion::V4) => None,
Some(IpVersion::V6) => Some(name),
None | Some(IpVersion::V46) => Some(format!("{}v6", name)),
Some(IpVersion::Ipv4) => None,
Some(IpVersion::Ipv6) => Some(name),
None | Some(IpVersion::Ip) => Some(format!("{}v6", name)),
},
}
}
@ -359,7 +359,7 @@ mod tests {
target: None,
};
let s2 = SetOptions {
version: Some(IpVersion::V4),
version: Some(IpVersion::Ipv4),
chains: Some(vec!["INPUT".into()]),
timeout: Some("3h".into()),
timeout_u32: Some(3 * 3600),
@ -397,7 +397,7 @@ mod tests {
for s3 in [
SetOptions {
version: Some(IpVersion::V6),
version: Some(IpVersion::Ipv6),
..Default::default()
},
SetOptions {

View file

@ -34,7 +34,7 @@ async fn conf_action_standalone() {
(true, json!({ "set": "test" }), &p),
// missing set key
(false, json!({}), &p),
(false, json!({ "version": 4 }), &p),
(false, json!({ "version": "ipv4" }), &p),
// unknown key
(false, json!({ "set": "test", "unknown": "yes" }), &p),
(false, json!({ "set": "test", "ip_index": 1 }), &p),
@ -66,14 +66,18 @@ async fn conf_action_standalone() {
(false, json!({ "set": "test", "action": 1 }), &p),
// ip version //
// ok
(true, json!({ "set": "test", "version": "4" }), &p),
(true, json!({ "set": "test", "version": "6" }), &p),
(true, json!({ "set": "test", "version": "46" }), &p),
(true, json!({ "set": "test", "version": "ipv4" }), &p),
(true, json!({ "set": "test", "version": "ipv6" }), &p),
(true, json!({ "set": "test", "version": "ip" }), &p),
// unknown version
(false, json!({ "set": "test", "version": 4 }), &p),
(false, json!({ "set": "test", "version": 6 }), &p),
(false, json!({ "set": "test", "version": 46 }), &p),
(false, json!({ "set": "test", "version": "5" }), &p),
(false, json!({ "set": "test", "version": "ipv5" }), &p),
(false, json!({ "set": "test", "version": "4" }), &p),
(false, json!({ "set": "test", "version": "6" }), &p),
(false, json!({ "set": "test", "version": "46" }), &p),
// bad type
(false, json!({ "set": "test", "version": true }), &p),
// chains //
@ -167,7 +171,7 @@ async fn conf_action_merge() {
config: json!({
"set": "test",
"target": "DROP",
"version": "46",
"version": "ip",
"action": "add",
})
.into(),
@ -206,7 +210,7 @@ async fn conf_action_merge() {
config: json!({
"set": "test2",
"target": "target1",
"version": "6",
"version": "ipv6",
})
.into(),
patterns: vec!["ip".into()],

View file

@ -30,7 +30,7 @@
options: {
set: 'reactiontest',
// pattern: 'ip',
// version: 46,
// version: 'ip',
// chains: ['INPUT', 'FORWARD'],
// target: 'DROP',
// action: 'add',