]> git.ipfire.org Git - thirdparty/iptables.git/commitdiff
arptables: Introduce print_iface()
authorPhil Sutter <phil@nwl.cc>
Sat, 27 Jul 2024 11:40:55 +0000 (13:40 +0200)
committerPhil Sutter <phil@nwl.cc>
Wed, 31 Jul 2024 21:13:55 +0000 (23:13 +0200)
Merge conditional interface printing code for input and output interface
into a function.

Signed-off-by: Phil Sutter <phil@nwl.cc>
iptables/nft-arp.c

index c73833270f0e8530eceec0124b0309d10668f191..264864c3fb2b2da73a3febc544f849d9bf19616d 100644 (file)
@@ -197,13 +197,23 @@ static void nft_arp_print_header(unsigned int format, const char *chain,
        }
 }
 
+static void print_iface(char letter, const char *iface,
+                       unsigned int format, bool invert, const char **sep)
+{
+       if (iface[0] == '\0' || (!strcmp(iface, "+") && !invert)) {
+               if (!(format & FMT_VIA))
+                       return;
+               iface = (format & FMT_NUMERIC) ? "*" : "any";
+       }
+       printf("%s%s-%c %s", *sep, invert ? "! " : "", letter, iface);
+       *sep = " ";
+}
+
 static void nft_arp_print_rule_details(const struct iptables_command_state *cs,
                                       unsigned int format)
 {
        const struct arpt_entry *fw = &cs->arp;
-       char iface[IFNAMSIZ+2];
        const char *sep = "";
-       int print_iface = 0;
        int i;
 
        if (strlen(cs->jumpto)) {
@@ -211,40 +221,10 @@ static void nft_arp_print_rule_details(const struct iptables_command_state *cs,
                sep = " ";
        }
 
-       iface[0] = '\0';
-
-       if (fw->arp.iniface[0] != '\0') {
-               strcat(iface, fw->arp.iniface);
-               print_iface = 1;
-       }
-       else if (format & FMT_VIA) {
-               print_iface = 1;
-               if (format & FMT_NUMERIC) strcat(iface, "*");
-               else strcat(iface, "any");
-       }
-       if (print_iface) {
-               printf("%s%s-i %s", sep, fw->arp.invflags & IPT_INV_VIA_IN ?
-                                  "! " : "", iface);
-               sep = " ";
-       }
-
-       print_iface = 0;
-       iface[0] = '\0';
-
-       if (fw->arp.outiface[0] != '\0') {
-               strcat(iface, fw->arp.outiface);
-               print_iface = 1;
-       }
-       else if (format & FMT_VIA) {
-               print_iface = 1;
-               if (format & FMT_NUMERIC) strcat(iface, "*");
-               else strcat(iface, "any");
-       }
-       if (print_iface) {
-               printf("%s%s-o %s", sep, fw->arp.invflags & IPT_INV_VIA_OUT ?
-                                  "! " : "", iface);
-               sep = " ";
-       }
+       print_iface('i', fw->arp.iniface, format,
+                   fw->arp.invflags & IPT_INV_VIA_IN, &sep);
+       print_iface('o', fw->arp.outiface, format,
+                   fw->arp.invflags & IPT_INV_VIA_OUT, &sep);
 
        if (fw->arp.smsk.s_addr != 0L) {
                printf("%s%s-s %s", sep,