]> git.ipfire.org Git - thirdparty/iptables.git/commitdiff
extensions: libebt_802_3: Use guided option parser
authorPhil Sutter <phil@nwl.cc>
Sun, 26 Nov 2023 16:36:16 +0000 (17:36 +0100)
committerPhil Sutter <phil@nwl.cc>
Wed, 10 Jan 2024 15:07:31 +0000 (16:07 +0100)
extensions/libebt_802_3.c
extensions/libebt_802_3.t

index 8cbcdcea4912f9898917b2365970b3c6c6b31ebe..26a7725cd0074d303c9d1f7e632749a3a7387c06 100644 (file)
 #include <stdio.h>
 #include <string.h>
 #include <stdlib.h>
-#include <getopt.h>
 #include <xtables.h>
 #include <linux/netfilter_bridge/ebt_802_3.h>
 
-#define _802_3_SAP     '1'
-#define _802_3_TYPE    '2'
-
-static const struct option br802_3_opts[] = {
-       { .name = "802_3-sap",  .has_arg = true, .val = _802_3_SAP },
-       { .name = "802_3-type", .has_arg = true, .val = _802_3_TYPE },
-       XT_GETOPT_TABLEEND,
+static const struct xt_option_entry br802_3_opts[] =
+{
+       { .name = "802_3-sap", .id = EBT_802_3_SAP,
+         .type = XTTYPE_UINT8, .base = 16,
+         .flags = XTOPT_INVERT | XTOPT_PUT,
+         XTOPT_POINTER(struct ebt_802_3_info, sap) },
+       { .name = "802_3-type", .id = EBT_802_3_TYPE,
+         .type = XTTYPE_UINT16, .base = 16,
+         .flags = XTOPT_INVERT | XTOPT_PUT | XTOPT_NBO,
+         XTOPT_POINTER(struct ebt_802_3_info, type) },
+       XTOPT_TABLEEND,
 };
 
 static void br802_3_print_help(void)
@@ -36,52 +39,14 @@ static void br802_3_print_help(void)
 "  Type implies SAP value 0xaa\n");
 }
 
-static int
-br802_3_parse(int c, char **argv, int invert, unsigned int *flags,
-             const void *entry, struct xt_entry_match **match)
+static void br802_3_parse(struct xt_option_call *cb)
 {
-       struct ebt_802_3_info *info = (struct ebt_802_3_info *) (*match)->data;
-       unsigned int i;
-       char *end;
-
-       switch (c) {
-       case _802_3_SAP:
-               if (invert)
-                       info->invflags |= EBT_802_3_SAP;
-               i = strtoul(optarg, &end, 16);
-               if (i > 255 || *end != '\0')
-                       xtables_error(PARAMETER_PROBLEM,
-                                     "Problem with specified "
-                                       "sap hex value, %x",i);
-               info->sap = i; /* one byte, so no byte order worries */
-               info->bitmask |= EBT_802_3_SAP;
-               break;
-       case _802_3_TYPE:
-               if (invert)
-                       info->invflags |= EBT_802_3_TYPE;
-               i = strtoul(optarg, &end, 16);
-               if (i > 65535 || *end != '\0') {
-                       xtables_error(PARAMETER_PROBLEM,
-                                     "Problem with the specified "
-                                       "type hex value, %x",i);
-               }
-               info->type = htons(i);
-               info->bitmask |= EBT_802_3_TYPE;
-               break;
-       default:
-               return 0;
-       }
+       struct ebt_802_3_info *info = cb->data;
 
-       *flags |= info->bitmask;
-       return 1;
-}
-
-static void
-br802_3_final_check(unsigned int flags)
-{
-       if (!flags)
-               xtables_error(PARAMETER_PROBLEM,
-                             "You must specify proper arguments");
+       xtables_option_parse(cb);
+       info->bitmask |= cb->entry->id;
+       if (cb->invert)
+               info->invflags |= cb->entry->id;
 }
 
 static void br802_3_print(const void *ip, const struct xt_entry_match *match,
@@ -112,10 +77,9 @@ static struct xtables_match br802_3_match =
        .size           = XT_ALIGN(sizeof(struct ebt_802_3_info)),
        .userspacesize  = XT_ALIGN(sizeof(struct ebt_802_3_info)),
        .help           = br802_3_print_help,
-       .parse          = br802_3_parse,
-       .final_check    = br802_3_final_check,
+       .x6_parse       = br802_3_parse,
        .print          = br802_3_print,
-       .extra_opts     = br802_3_opts,
+       .x6_options     = br802_3_opts,
 };
 
 void _init(void)
index a138f35d2c756e73c343949f4b7659424c2444c2..2e4945b388830ae67209e3f79032af1554bc2c32 100644 (file)
@@ -1,5 +1,7 @@
 :INPUT,FORWARD,OUTPUT
 --802_3-sap ! 0x0a -j CONTINUE;=;FAIL
 --802_3-type 0x000a -j RETURN;=;FAIL
+-p Length --802_3-sap 0x0a -j CONTINUE;=;OK
 -p Length --802_3-sap ! 0x0a -j CONTINUE;=;OK
 -p Length --802_3-type 0x000a -j RETURN;=;OK
+-p Length --802_3-type ! 0x000a -j RETURN;=;OK