diff mbox series

[iptables,6/8] xlate-translate: split common parts into helper

Message ID 20180413180848.21200-7-fw@strlen.de
State Accepted
Delegated to: Pablo Neira
Headers show
Series xt-compat: add ebtables-translate | expand

Commit Message

Florian Westphal April 13, 2018, 6:08 p.m. UTC
---
 iptables/xtables-translate.c | 59 +++++++++++++++++++-------------------------
 1 file changed, 26 insertions(+), 33 deletions(-)
diff mbox series

Patch

diff --git a/iptables/xtables-translate.c b/iptables/xtables-translate.c
index 61d4da179cf7..ecb490f98b2f 100644
--- a/iptables/xtables-translate.c
+++ b/iptables/xtables-translate.c
@@ -420,38 +420,50 @@  static struct nft_xt_restore_cb cb_xlate = {
 	.abort		= commit,
 };
 
-static int xtables_xlate_main(int family, const char *progname, int argc,
-			      char *argv[])
+static int xtables_xlate_main_common(struct nft_handle *h,
+				     int family,
+				     const char *progname)
 {
 	int ret;
-	char *table = "filter";
-	struct nft_handle h = {
-		.family = family,
-	};
 
 	xtables_globals.program_name = progname;
 	xtables_globals.compat_rev = dummy_compat_rev;
 	ret = xtables_init_all(&xtables_globals, family);
 	if (ret < 0) {
 		fprintf(stderr, "%s/%s Failed to initialize xtables\n",
-				xtables_globals.program_name,
-				xtables_globals.program_version);
-				exit(1);
+			xtables_globals.program_name,
+			xtables_globals.program_version);
+		return 1;
 	}
 #if defined(ALL_INCLUSIVE) || defined(NO_SHARED_LIBS)
 	init_extensions();
 	init_extensions4();
 #endif
 
-	if (nft_init(&h, xtables_ipv4) < 0) {
+	if (nft_init(h, xtables_ipv4) < 0) {
 		fprintf(stderr, "%s/%s Failed to initialize nft: %s\n",
 				xtables_globals.program_name,
 				xtables_globals.program_version,
 				strerror(errno));
-		nft_fini(&h);
-		exit(EXIT_FAILURE);
+		return 1;
 	}
 
+	return 0;
+}
+
+static int xtables_xlate_main(int family, const char *progname, int argc,
+			      char *argv[])
+{
+	int ret;
+	char *table = "filter";
+	struct nft_handle h = {
+		.family = family,
+	};
+
+	ret = xtables_xlate_main_common(&h, family, progname);
+	if (ret < 0)
+		exit(EXIT_FAILURE);
+
 	ret = do_command_xlate(&h, argc, argv, &table, false);
 	if (!ret)
 		fprintf(stderr, "Translation not implemented\n");
@@ -472,28 +484,9 @@  static int xtables_restore_xlate_main(int family, const char *progname,
 	time_t now = time(NULL);
 	int c;
 
-	xtables_globals.program_name = progname;
-	xtables_globals.compat_rev = dummy_compat_rev;
-	ret = xtables_init_all(&xtables_globals, family);
-	if (ret < 0) {
-		fprintf(stderr, "%s/%s Failed to initialize xtables\n",
-				xtables_globals.program_name,
-				xtables_globals.program_version);
-				exit(1);
-	}
-#if defined(ALL_INCLUSIVE) || defined(NO_SHARED_LIBS)
-	init_extensions();
-	init_extensions4();
-#endif
-
-	if (nft_init(&h, xtables_ipv4) < 0) {
-		fprintf(stderr, "%s/%s Failed to initialize nft: %s\n",
-				xtables_globals.program_name,
-				xtables_globals.program_version,
-				strerror(errno));
-		nft_fini(&h);
+	ret = xtables_xlate_main_common(&h, family, progname);
+	if (ret < 0)
 		exit(EXIT_FAILURE);
-	}
 
 	opterr = 0;
 	while ((c = getopt_long(argc, argv, "hf:", options, NULL)) != -1) {