]> git.ipfire.org Git - people/stevee/perl-ipset.git/commitdiff
Add private call_list_cmd function.
authorStefan Schantl <stefan.schantl@ipfire.org>
Thu, 1 Sep 2022 17:33:43 +0000 (19:33 +0200)
committerStefan Schantl <stefan.schantl@ipfire.org>
Thu, 1 Sep 2022 17:33:43 +0000 (19:33 +0200)
This function uses the print_outfm_to_string function to capute the
output and return it as string.

Signed-off-by: Stefan Schantl <stefan.schantl@ipfire.org>
IPSet.xs

index 507f94a9a911fe5a26440b309c77623347d49a73..d44e8f63b4a8587b5250b2feb08427463109be4a 100644 (file)
--- a/IPSet.xs
+++ b/IPSet.xs
@@ -43,6 +43,55 @@ ERROR:
         return r;
 }
 
+char* call_list_cmd(struct ipset_session *session,
+       enum ipset_envopt envopt,
+       const char *setname, ...)
+{
+
+       enum ipset_cmd cmd = IPSET_CMD_LIST;
+       int r;
+
+       char* p = NULL;
+       char* sets = NULL;
+
+       // Check if an environment option has been passed.
+       if(envopt) {
+               // Set the environemt option.
+               ipset_envopt_set(session, envopt);
+       }
+
+       // Check if a setname has been given.
+       if(setname) {
+               r = ipset_session_data_set(session, IPSET_SETNAME, setname);
+               if (r < 0 ) {
+                       goto ERROR;
+               }
+       }
+
+       // Use print_outfm_to_string function to put the output into a string which
+       // can be processed further.
+       ipset_session_print_outfn(session, print_outfm_to_string, &sets);
+
+       r = ipset_cmd(session, cmd, 0);
+       if (r < 0) {
+               goto ERROR;
+       }
+
+       goto END;
+
+       ERROR:
+               // Reset session data.
+               ipset_data_reset(ipset_session_data(session));
+        END:
+               // Remove environment option.
+               ipset_envopt_unset(session, envopt);
+
+               // Reset outfn to defaul.
+               ipset_session_print_outfn(session, NULL, NULL);
+
+               return sets;
+}
+
 MODULE = IPSet         PACKAGE = IPSet
 
 struct ipset_session *