]> git.ipfire.org Git - people/stevee/perl-ipset.git/commitdiff
Add function to add ports to a set.
authorStefan Schantl <stefan.schantl@ipfire.org>
Mon, 17 Apr 2023 13:17:01 +0000 (15:17 +0200)
committerStefan Schantl <stefan.schantl@ipfire.org>
Mon, 17 Apr 2023 13:17:01 +0000 (15:17 +0200)
Signed-off-by: Stefan Schantl <stefan.schantl@ipfire.org>
IPSet.xs

index 02a7c40a2b1bf69baae6643e3f7e542acc2b2d46..295a9b7cf2a027bac29420fc2bc246bfa05d582d 100644 (file)
--- a/IPSet.xs
+++ b/IPSet.xs
@@ -478,7 +478,54 @@ remove_address(session, setname, address)
                END:
        OUTPUT:
                RETVAL
+
+bool
+add_port(session, setname, port)
+       struct ipset_session *session;
+       const char *setname;
+       const char *port;
+
+       PREINIT:
+               enum ipset_cmd cmd = IPSET_CMD_ADD;
+               const struct ipset_type *type;
+
+       CODE:
+               // Assign setname as session data.
+               int r = ipset_session_data_set(session, IPSET_SETNAME, setname);
+               if (r < 0) {
+                       goto ERROR;
+               }
+
+               // Get the set type.
+               type = ipset_type_get(session, cmd);
+               if (type == NULL) {
+                       goto ERROR;
+               }
+
+               // Parse and try to add the given port to the session data.
+               r = ipset_parse_tcp_udp_port(session, IPSET_OPT_PORT, port);
+               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
+
 #
 # Get functions to ask for various data or receive error messages.
 #