From: Patrick McHardy <kaber@trash.net>
To: Peter Warasin <peter@endian.com>
Cc: netfilter-devel@vger.kernel.org
Subject: Re: [PATCH 3/3] iptables-edit: introduces iptables-edit cli tool
Date: Tue, 06 Nov 2007 01:13:09 +0100 [thread overview]
Message-ID: <472FB195.6090202@trash.net> (raw)
In-Reply-To: <472E604D.5090901@endian.com>
Peter Warasin wrote:
The patch has some stylistic problems, see below for a few details.
I suggest to run it through Lindent.
> Index: iptables-edit.c
> ===================================================================
> --- /dev/null
> +++ iptables-edit.c
> @@ -0,0 +1,259 @@
> +/* Code to apply iptables rules on an iptables dump file generated by iptables-save. */
> +/* (C) 2007 by Peter Warasin <peter@endian.com>
> + * based on previous code from Rusty Russell <rusty@linuxcare.com.au>
> + * and Harald Welte <laforge@gnumonks.org>
> + *
> + * This code is distributed under the terms of GNU GPL v2
> + *
> + */
> +#include <getopt.h>
> +#include <sys/errno.h>
> +#include <stdio.h>
> +#include <stdlib.h>
> +#include <string.h>
> +#include "libiptc/libiptc.h"
> +#include "iptables.h"
> +#include "iptables-dump.h"
> +
> +int binary = 0, counters = 0, verbose = 0;
> +char *modprobeparam = 0;
> +char *dumpfile = 0;
> +int commandargc = 0;
> +char *commandargv[255];
> +
> +static struct option options[] = {
> + { "binary", 0, 0, 'b' },
> + { "counters", 0, 0, 'c' },
> + { "verbose", 0, 0, 'v' },
> + { "help", 0, 0, 'h' },
> + { "modprobe", 1, 0, 'M'},
> + { "dump-file", 0, 0, 'i' },
>
Broken whitespace. Please also use NULL instead of 0 for the
third argument.
> + { 0 }
> +};
> +
> +struct handle_list_t {
> + char tablename[IPT_TABLE_MAXNAMELEN + 1];
> + iptc_handle_t handle;
> + struct handle_list_t *next;
> +};
> +struct handle_list_t *table_handles = NULL;
> +
> +static void print_usage(const char *name, const char *version) __attribute__((noreturn));
> +
> +static void print_usage(const char *name, const char *version)
> +{
> + fprintf(stderr, "Usage: %s [-b] [-c] [-v] [-h] [-M] <-i>\n"
> + " [ --binary ]\n"
> + " [ --counters ]\n"
> + " [ --verbose ]\n"
> + " [ --help ]\n"
> + " [ --modprobe=<command>]\n"
> + " [ --dump-file=<DUMPFILE>]\n", name);
> + exit(1);
> +}
> +
> +void add_handle(const char *tablename, iptc_handle_t handle) {
>
opening parens goes on a new line.
> + struct handle_list_t *tmp;
> + tmp = (struct handle_list_t *) malloc(sizeof(struct handle_list_t));
> + strncpy(tmp->tablename, tablename, IPT_TABLE_MAXNAMELEN);
> + tmp->tablename[IPT_TABLE_MAXNAMELEN] = '\0';
> + tmp->handle = handle;
> + tmp->next = table_handles;
> + table_handles = tmp;
> +}
> +
> +iptc_handle_t get_handle(const char *tablename) {
> + iptc_handle_t handle;
> + struct handle_list_t *i = NULL;
>
unnecessary initialization
> + if (tablename == NULL)
> + return NULL;
> + for (i = table_handles; i; i = i->next) {
> + if (! i) break;
>
newline please.
> + if (strcmp(i->tablename, tablename) == 0)
> + return i->handle;
> + }
> +
> + handle = iptc_init(tablename);
> + add_handle(tablename, handle);
> + return handle;
> +}
> +
> +static int for_each_table(int (*func)(const char *tablename))
> +{
> + int ret = 1;
> + FILE *procfile = NULL;
> + char tablename[IPT_TABLE_MAXNAMELEN+1];
> +
> + procfile = fopen("/proc/net/ip_tables_names", "r");
> + if (!procfile)
> + return 0;
> +
> + while (fgets(tablename, sizeof(tablename), procfile)) {
> + if (tablename[strlen(tablename) - 1] != '\n')
> + exit_error(OTHER_PROBLEM,
> + "Badly formed tablename `%s'\n",
> + tablename);
> + tablename[strlen(tablename) - 1] = '\0';
> + ret &= func(tablename);
> + }
> +
> + return ret;
> +}
> +
> +int restore_from_file(const char *tablename) {
> + iptc_handle_t handle = get_handle(tablename);
> + if (verbose)
> + fprintf(stderr, "Restoring table '%s'\n", tablename);
> +
> + if (! handle) {
> + fprintf(stderr, "Could not get netfilter handle for table '%s' while restoring\n", tablename);
> + return 0;
> + }
> + return restore_dump(tablename, handle, modprobeparam, dumpfile, binary, counters, verbose, 0, 1);
>
Please break lines at 80 characters.
> +}
> +
> +int save_handles(const char *tablename) {
> + iptc_handle_t handle = get_handle(tablename);
> + if (verbose)
> + fprintf(stderr, "Saving table '%s'\n", tablename);
> + if (! handle) {
> + fprintf(stderr, "Could not get netfilter handle for table '%s' while saving\n", tablename);
> + return 0;
> + }
> + return create_dump(tablename, handle, binary, counters);
> +}
> +
> +
> +/* function adding one argument to newargv, updating newargc
> + * returns true if argument added, false otherwise */
> +static int add_argv(char *what) {
> + if (what && ((commandargc + 1) < sizeof(commandargv)/sizeof(char *))) {
> + commandargv[commandargc] = strdup(what);
> + commandargc++;
> + return 1;
> + } else
> + return 0;
> +}
> +
> +static void free_argv(void) {
> + int i;
> +
> + for (i = 0; i < commandargc; i++) {
> + free(commandargv[i]);
> + commandargv[i] = NULL;
> + }
> + commandargc = 0;
> +}
> +
> +
> +#ifdef IPTABLES_MULTI
> +int
> +iptables_edit_main(int argc, char *argv[])
> +#else
> +int
> +main(int argc, char *argv[])
> +#endif
> +{
> + int c;
> + int ret = 0;
> + char buffer[10240];
> + int i=0;
> +
> + program_name = "iptables";
> + program_version = IPTABLES_VERSION;
> +
> + lib_dir = getenv("IPTABLES_LIB_DIR");
> + if (!lib_dir)
> + lib_dir = IPT_LIB_DIR;
> +
> +#ifdef NO_SHARED_LIBS
> + init_extensions();
> +#endif
> +
> + while ((c = getopt_long(argc, argv, "bcvhM:i:", options, NULL)) != -1) {
> + switch (c) {
> + case 'b':
> + binary = 1;
> + break;
> +
> + case 'c':
> + counters = 1;
> + break;
> + case 'v':
> + verbose = 1;
> + break;
> + case 'h':
> + print_usage("iptables-edit",
> + IPTABLES_VERSION);
> + break;
> + case 'M':
> + modprobeparam = optarg;
> + break;
> + case 'i':
> + dumpfile = optarg;
> + break;
> +
> + }
> + }
> +
> + if (optind < argc) {
> + fprintf(stderr, "Unknown arguments found on commandline\n");
> + exit(1);
> + }
> +
> + if (! dumpfile) {
> + fprintf(stderr, "No dump file (-i) specified!\n");
> + exit(1);
> + }
> +
> + if ((ret = for_each_table(restore_from_file)) != 0)
> + return ret;
> +
> + if (verbose)
> + fprintf(stderr, "Accept commands\n");
> +
> + /* Grab standard input. */
> + while (fgets(buffer, sizeof(buffer), stdin)) {
> + char *token = "";
> + iptc_handle_t handle = NULL;
> + char *thistable = "filter";
> +
> + i++;
> + buffer[strlen(buffer)-1] = '\0';
> + if (buffer[0] == '#')
> + continue;
> + if (verbose)
> + fprintf(stderr, "Line %d: Process command '%s'\n", i, buffer);
> +
> + if ((token = strtok(buffer, " \t\n")) == NULL)
> + continue;
> + free_argv();
> + add_argv(token);
> + while ((token = strtok(NULL, " \t\n")) != NULL) {
> + add_argv(token);
> + }
> +
> + if ((commandargv[1] != NULL) && strcmp(commandargv[1], "-t") == 0) {
> + if (commandargv[2] == NULL) {
> + fprintf(stderr, "Line %d: -t parameter needs an argument!\n", i);
> + return 1;
> + }
> + thistable = commandargv[2];
> + }
> +
> + handle = get_handle(thistable);
> + if (handle == NULL) {
> + fprintf(stderr, "Line %d: Could not get netfilter handle for table '%s' while performing command\n", i, thistable);
> + return 1;
> + }
> +
> + if (! do_command(commandargc, commandargv,
> + &thistable, &handle)) {
> +
> + fprintf(stderr, "Line %d: Command failed: %s\n", i, iptc_strerror(errno));
> + return 1;
> + }
> + }
> +
> + return ! for_each_table(save_handles);
> +}
>
next prev parent reply other threads:[~2007-11-06 0:13 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2007-11-05 0:14 [PATCH 3/3] iptables-edit: introduces iptables-edit cli tool Peter Warasin
2007-11-06 0:13 ` Patrick McHardy [this message]
2007-11-07 0:22 ` Peter Warasin
2007-11-07 10:56 ` Jan Engelhardt
2007-11-07 20:55 ` Peter Warasin
-- strict thread matches above, loose matches on Subject: below --
2007-10-20 0:57 Peter Warasin
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=472FB195.6090202@trash.net \
--to=kaber@trash.net \
--cc=netfilter-devel@vger.kernel.org \
--cc=peter@endian.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).