Skip to contentSkip to conversation

PATCH: combine iptables, iptables-save and iptables-restore for size reduction

PATCH Bastiaan Bakker 1 participant 1 message Jun 20, 2004

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

Hi,

The iptables, iptables-save and iptables-restore binaries contain a relatively large amount of identical code. In most cases this doesn't matter, but on embedded platforms 100KB can make a difference. Attached is a small patch against iptables 1.2.10 which enables building iptables, iptables-save and iptables-restore into 1 multi-purpose binary. In BusyBoxy style it selects the correct command based on argv[0].

To build, do 'make DO_MULTI=1' instead of 'make'.

Regards,

Bastiaan Bakker

iptables-save.c +7 -1
Index: iptables-save.c
===================================================================
@@ -321,7 +321,13 @@
  * :Chain name POLICY packets bytes
  * rule
  */
-int main(int argc, char *argv[])
+#ifdef IPTABLES_MULTI
+int
+iptables_save_main(int argc, char *argv[])
+#else
+int
+main(int argc, char *argv[])
+#endif
 {
 	const char *tablename = NULL;
 	int c;
iptables-restore.c +7 -1
Index: iptables-restore.c
===================================================================
@@ -99,7 +99,13 @@
 		free(newargv[i]);
 }
 
-int main(int argc, char *argv[])
+#ifdef IPTABLES_MULTI
+int
+iptables_restore_main(int argc, char *argv[])
+#else
+int
+main(int argc, char *argv[])
+#endif
 {
 	iptc_handle_t handle = NULL;
 	char buffer[10240];
iptables-standalone.c +5 -0
Index: iptables-standalone.c
===================================================================
@@ -37,8 +37,13 @@
 #include <string.h>
 #include <iptables.h>
 
+#ifdef IPTABLES_MULTI
 int
+iptables_main(int argc, char *argv[])
+#else
+int
 main(int argc, char *argv[])
+#endif
 {
 	int ret;
 	char *table = "filter";
iptables-multi.c +31 -0
Index: iptables-multi.c
===================================================================
@@ -0,0 +1,31 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <libgen.h>
+
+int iptables_main(int argc, char **argv);
+int iptables_save_main(int argc, char **argv);
+int iptables_restore_main(int argc, char **argv);
+
+int main(int argc, char **argv) {
+  char *progname;
+
+  if (argc == 0) {
+    fprintf(stderr, "no argv[0]?");
+    exit(1);
+  } else {
+    progname = basename(argv[0]);
+
+    if (!strcmp(progname, "iptables"))
+      return iptables_main(argc, argv);
+    
+    if (!strcmp(progname, "iptables-save"))
+      return iptables_save_main(argc, argv);
+    
+    if (!strcmp(progname, "iptables-restore"))
+      return iptables_restore_main(argc, argv);
+    
+    fprintf(stderr, "iptables multi-purpose version: unknown applet name %s\n", progname);
+    exit(1);
+  }
+}
Makefile +19 -0
Index: Makefile
===================================================================
@@ -42,7 +42,9 @@
 EXTRA_INSTALLS+=$(DESTDIR)$(BINDIR)/iptables $(DESTDIR)$(MANDIR)/man8/iptables.8
 
 # No longer experimental.
+ifneq ($(DO_MULTI), 1)
 EXTRAS+=iptables-save iptables-restore
+endif
 EXTRA_INSTALLS+=$(DESTDIR)$(BINDIR)/iptables-save $(DESTDIR)$(BINDIR)/iptables-restore $(DESTDIR)$(MANDIR)/man8/iptables-restore.8 $(DESTDIR)$(MANDIR)/man8/iptables-save.8
 
 ifeq ($(DO_IPV6), 1)
@@ -114,8 +116,13 @@
 iptables.o: iptables.c
 	$(CC) $(CFLAGS) -DIPT_LIB_DIR=\"$(IPT_LIBDIR)\" -c -o $@ $<
 
+ifeq ($(DO_MULTI), 1)
+iptables: iptables-multi.c iptables-save.c iptables-restore.c iptables-standalone.c iptables.o $(STATIC_LIBS) libiptc/libiptc.a
+	$(CC) $(CFLAGS) -DIPTABLES_MULTI -DIPT_LIB_DIR=\"$(IPT_LIBDIR)\" $(LDFLAGS) -o $@ $^ $(LDLIBS)
+else
 iptables: iptables-standalone.c iptables.o $(STATIC_LIBS) libiptc/libiptc.a
 	$(CC) $(CFLAGS) -DIPT_LIB_DIR=\"$(IPT_LIBDIR)\" $(LDFLAGS) -o $@ $^ $(LDLIBS)
+endif
 
 $(DESTDIR)$(BINDIR)/iptables: iptables
 	@[ -d $(DESTDIR)$(BINDIR) ] || mkdir -p $(DESTDIR)$(BINDIR)
@@ -124,16 +131,28 @@
 iptables-save: iptables-save.c iptables.o $(STATIC_LIBS) libiptc/libiptc.a
 	$(CC) $(CFLAGS) -DIPT_LIB_DIR=\"$(IPT_LIBDIR)\" $(LDFLAGS) -o $@ $^ $(LDLIBS)
 
+ifeq ($(DO_MULTI), 1)
+$(DESTDIR)$(BINDIR)/iptables-save: iptables
+	@[ -d $(DESTDIR)$(BINDIR) ] || mkdir -p $(DESTDIR)$(BINDIR)
+	ln -sf $< $@
+else
 $(DESTDIR)$(BINDIR)/iptables-save: iptables-save
 	@[ -d $(DESTDIR)$(BINDIR) ] || mkdir -p $(DESTDIR)$(BINDIR)
 	cp $< $@
+endif
 
 iptables-restore: iptables-restore.c iptables.o $(STATIC_LIBS) libiptc/libiptc.a
 	$(CC) $(CFLAGS) -DIPT_LIB_DIR=\"$(IPT_LIBDIR)\" $(LDFLAGS) -o $@ $^ $(LDLIBS)
 
+ifeq ($(DO_MULTI), 1)
+$(DESTDIR)$(BINDIR)/iptables-restore: iptables
+	@[ -d $(DESTDIR)$(BINDIR) ] || mkdir -p $(DESTDIR)$(BINDIR)
+	ln -sf $< $@
+else
 $(DESTDIR)$(BINDIR)/iptables-restore: iptables-restore
 	@[ -d $(DESTDIR)$(BINDIR) ] || mkdir -p $(DESTDIR)$(BINDIR)
 	cp $< $@
+endif
 
 ip6tables.o: ip6tables.c
 	$(CC) $(CFLAGS) -DIP6T_LIB_DIR=\"$(IPT_LIBDIR)\" -c -o $@ $<