From: Stefan Schantl Date: Sat, 17 Sep 2022 14:10:39 +0000 (+0200) Subject: Add support for flushing sets. X-Git-Url: http://git.ipfire.org/gitweb/gitweb.cgi?a=commitdiff_plain;h=c63908c7beb7c32f6695b184069bd34442630e29;p=people%2Fstevee%2Fperl-ipset.git Add support for flushing sets. Signed-off-by: Stefan Schantl --- diff --git a/IPSet.xs b/IPSet.xs index 160bfa9..71c29d9 100644 --- a/IPSet.xs +++ b/IPSet.xs @@ -301,6 +301,39 @@ swap_set(session, setname, setname2) OUTPUT: RETVAL +bool +flush_set(session, setname) + struct ipset_session *session; + const char *setname; + + PREINIT: + enum ipset_cmd cmd = IPSET_CMD_FLUSH; + + CODE: + // Assign the setname to the session data. + int r = ipset_session_data_set(session, IPSET_SETNAME, setname); + if (r < 0) { + goto ERROR; + } + + r = ipset_cmd(session, cmd, 0); + if (r < 0) { + goto ERROR; + } + + RETVAL = true; + + goto END; + + ERROR: + RETVAL = false; + + // Reset session data. + ipset_data_reset(ipset_session_data(session)); + END: + OUTPUT: + RETVAL + bool setname_exists(session, setname) struct ipset_session *session; diff --git a/t/IPSet.t b/t/IPSet.t index 76bd249..375690b 100644 --- a/t/IPSet.t +++ b/t/IPSet.t @@ -8,7 +8,7 @@ use strict; use warnings; -use Test::More tests => 19; +use Test::More tests => 21; BEGIN { use_ok('IPSet') }; ######################### @@ -93,5 +93,13 @@ $data = &IPSet::get_set_data($session, $testset{'name'}); ok($data->{entries} lt scalar(@addresses), "Less entries in the set than at the addresses array"); ok($data->{entries} gt 0, "At least one entry on the testset"); +# Flush the set. +my $flush = &IPSet::flush_set($session, $testset{'name'}); +ok($flush, "Successfully flushed the testset."); + +# Update set data. +$data = &IPSet::get_set_data($session, $testset{'name'}); +ok($data->{entries} eq 0, "No entries anymore in the testset."); + # CLEANUP: Delete the remaining set. &IPSet::delete_set($session, $testset{'name'});