From 07d9bb205e4a8f6d27355acd3450dcdac77a2454 Mon Sep 17 00:00:00 2001 From: Stefan Schantl Date: Mon, 17 Apr 2023 15:17:01 +0200 Subject: [PATCH] Add function to add ports to a set. Signed-off-by: Stefan Schantl --- IPSet.xs | 49 ++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 48 insertions(+), 1 deletion(-) diff --git a/IPSet.xs b/IPSet.xs index 02a7c40..295a9b7 100644 --- 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. # -- 2.47.3