From d988274d8f2e7cee8deea954ca3e70b4080b7e86 Mon Sep 17 00:00:00 2001 From: Florian Westphal Date: Thu, 12 Apr 2018 11:46:02 +0200 Subject: xlate-translate: split common parts into helper --- iptables/xtables-translate.c | 59 +++++++++++++++++++------------------------- 1 file changed, 26 insertions(+), 33 deletions(-) (limited to 'iptables/xtables-translate.c') diff --git a/iptables/xtables-translate.c b/iptables/xtables-translate.c index 61d4da17..ecb490f9 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) { -- cgit v1.2.3