Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 12 additions & 3 deletions src/session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -251,14 +251,22 @@ impl<T: SetType> Session<T> {
if unset {
self.unset_option(EnvOption::ListSetName);
}
match ret? {
ListResult::Normal(_) => {
// Reset session data after list to avoid interfering with subsequent operations
unsafe {
binding::ipset_data_reset(self.data);
}
match ret {
Ok(ListResult::Normal(_)) => {
unreachable!("normal should not return")
}
ListResult::Terse(names) => {
Ok(ListResult::Terse(names)) => {
let name = self.name.to_string_lossy().to_string();
Ok(names.contains(&name))
}
Err(Error::Cmd(msg, true)) if msg.contains("does not exist") => {
Ok(false)
}
Err(e) => Err(e),
}
}

Expand Down Expand Up @@ -288,6 +296,7 @@ impl<T: SetType> Session<T> {
let mut result = NormalListResult::default();
for line in &self.output {
for s in line.split("\n") {
let s = s.trim_end_matches('\0');
if !s.is_empty() {
result.update_from_str(s)?;
}
Expand Down
49 changes: 47 additions & 2 deletions src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -392,6 +392,14 @@ impl_name!(PortDataType, "port");
impl_name!(IfaceDataType, "iface");
impl_name!(MarkDataType, "mark");
impl_name!(SetDataType, "set");

// Special case for single-element tuple
impl<A: TypeName> TypeName for (A,) {
fn name() -> String {
A::name()
}
}

impl_name!(A, B);
impl_name!(A, B, C);

Expand All @@ -410,6 +418,13 @@ macro_rules! impl_set_data {
};
}

// Special case for single-element tuple
impl<T: SetType, A: SetData<T>> SetData<T> for (A,) {
fn set_data(&self, session: &Session<T>, from: Option<bool>) -> Result<(), Error> {
self.0.set_data(session, from)
}
}

impl_set_data!(A, B);
impl_set_data!(A, B, C);

Expand All @@ -434,6 +449,13 @@ macro_rules! impl_parse {
};
}

// Special case for single-element tuple
impl<A: Parse> Parse for (A,) {
fn parse(&mut self, s: &str) -> Result<(), Error> {
self.0.parse(s)
}
}

impl_parse!(A, B);
impl_parse!(A, B, C);

Expand Down Expand Up @@ -877,10 +899,28 @@ impl<T: SetType> NormalListResult<T> {
options.push(AddOption::Packets(fields[i + 1].parse()?));
}
"bytes" => {
options.push(AddOption::Bytes(fields[i + 1].trim().replace("\0", "").parse()?));
options.push(AddOption::Bytes(fields[i + 1].parse()?));
}
"comment" => {
options.push(AddOption::Comment(fields[i + 1].to_string()));
// Collect all tokens between quotes
let mut comment = String::new();
let mut j = i + 1;
while j < fields.len() {
if !comment.is_empty() {
comment.push(' ');
}
comment.push_str(fields[j]);
// Check if this token ends with a quote
if fields[j].ends_with('"') {
break;
}
j += 1;
}
// Remove quotes
comment = comment.trim_matches('"').to_string();
options.push(AddOption::Comment(comment));
i = j + 1; // Move past the last token of comment
continue;
}
"skbmark" => {
let values: Vec<_> = fields[i + 1].split('/').collect();
Expand Down Expand Up @@ -927,6 +967,7 @@ pub struct ListHeader {
hash_size: u32,
bucket_size: Option<u32>,
max_elem: u32,
timeout: Option<u32>,
counters: bool,
comment: bool,
skbinfo: bool,
Expand Down Expand Up @@ -956,6 +997,10 @@ impl ListHeader {
header.max_elem = s[i + 1].parse().unwrap();
i += 2;
}
"timeout" => {
header.timeout = Some(s[i + 1].parse().unwrap());
i += 2;
}
"counters" => {
header.counters = true;
i += 1;
Expand Down