[PATCH] sbin/ip6fw bring in -n option
Sepherosa Ziehau
sepherosa at gmail.com
Sat Apr 23 05:17:12 PDT 2005
Sorry, this is the correct version
--
Live Free or Die
--- ip6fw.c 2005-04-24 02:05:46.000000000 +0800
+++ ip6fw.c 2005-04-24 04:04:13.000000000 +0800
@@ -89,6 +89,7 @@
int do_time=0; /* Show time stamps */
int do_quiet=0; /* Be quiet in add and flush */
int do_force=0; /* Don't ask for confirmation */
+int do_test=0; /* Don't load into Kernel */
struct icmpcode {
int code;
@@ -778,10 +779,13 @@
/* Rule number */
while (ac && isdigit(**av)) {
rule.fw_number = atoi(*av); av++; ac--;
- if (setsockopt(s, IPPROTO_IPV6, IPV6_FW_DEL,
- &rule, sizeof(rule)) < 0) {
- exitval = 1;
- warn("rule %u: setsockopt(IPV6_FW_DEL)", rule.fw_number);
+ if (!do_test) {
+ if (setsockopt(s, IPPROTO_IPV6, IPV6_FW_DEL,
+ &rule, sizeof(rule)) < 0) {
+ exitval = 1;
+ warn("rule %u: setsockopt(IPV6_FW_DEL)",
+ rule.fw_number);
+ }
}
}
if (exitval != 0)
@@ -1111,8 +1115,11 @@
if (!do_quiet)
show_ip6fw(&rule);
- if (setsockopt(s, IPPROTO_IPV6, IPV6_FW_ADD, &rule, sizeof rule) < 0)
- err(EX_UNAVAILABLE, "setsockopt(IPV6_FW_ADD)");
+ if (!do_test) {
+ if (setsockopt(s, IPPROTO_IPV6, IPV6_FW_ADD,
+ &rule, sizeof rule) < 0)
+ err(EX_UNAVAILABLE, "setsockopt(IPV6_FW_ADD)");
+ }
}
static void
@@ -1122,10 +1129,14 @@
if (!ac) {
/* clear all entries */
- if (setsockopt(s,IPPROTO_IPV6,IPV6_FW_ZERO,NULL,0)<0)
- err(EX_UNAVAILABLE, "setsockopt(IPV6_FW_ZERO)");
- if (!do_quiet)
- printf("Accounting cleared.\n");
+ if (!do_test) {
+ if (setsockopt(s,IPPROTO_IPV6,IPV6_FW_ZERO,NULL,0)<0)
+ err(EX_UNAVAILABLE, "setsockopt(IPV6_FW_ZERO)");
+ if (!do_quiet)
+ printf("Accounting cleared.\n");
+ } else if (!do_quiet) {
+ printf("Accounting not cleared.\n");
+ }
} else {
struct ip6_fw rule;
int failed = 0;
@@ -1135,13 +1146,19 @@
/* Rule number */
if (isdigit(**av)) {
rule.fw_number = atoi(*av); av++; ac--;
- if (setsockopt(s, IPPROTO_IPV6,
- IPV6_FW_ZERO, &rule, sizeof rule)) {
- warn("rule %u: setsockopt(IPV6_FW_ZERO)", rule.fw_number);
- failed = 1;
- } else if (!do_quiet)
- printf("Entry %d cleared\n",
- rule.fw_number);
+ if (!do_test) {
+ if (setsockopt(s, IPPROTO_IPV6,
+ IPV6_FW_ZERO, &rule, sizeof rule)) {
+ warn("rule %u: setsockopt(IPV6_FW_ZERO)", rule.fw_number);
+ failed = 1;
+ } else if (!do_quiet) {
+ printf("Entry %d cleared\n",
+ rule.fw_number);
+ }
+ } else if (!do_quiet) {
+ printf("Entry %d not cleared\n",
+ rule.fw_number);
+ }
} else
show_usage("invalid rule number ``%s''", *av);
}
@@ -1165,7 +1182,7 @@
/* Set the force flag for non-interactive processes */
do_force = !isatty(STDIN_FILENO);
- while ((ch = getopt(ac, av ,"afqtN")) != -1)
+ while ((ch = getopt(ac, av ,"afnqtN")) != -1)
switch(ch) {
case 'a':
do_acct=1;
@@ -1173,6 +1190,9 @@
case 'f':
do_force=1;
break;
+ case 'n':
+ do_test=1;
+ break;
case 'q':
do_quiet=1;
break;
@@ -1217,10 +1237,17 @@
do_flush = 1;
}
if ( do_flush ) {
- if (setsockopt(s,IPPROTO_IPV6,IPV6_FW_FLUSH,NULL,0) < 0)
- err(EX_UNAVAILABLE, "setsockopt(IPV6_FW_FLUSH)");
- if (!do_quiet)
- printf("Flushed all rules.\n");
+ if (!do_test) {
+ if (setsockopt(s, IPPROTO_IPV6, IPV6_FW_FLUSH,
+ NULL, 0) < 0) {
+ err(EX_UNAVAILABLE,
+ "setsockopt(IPV6_FW_FLUSH)");
+ }
+ if (!do_quiet)
+ printf("Flushed all rules.\n");
+ } else if (!do_quiet) {
+ printf("Rules not flushed.\n");
+ }
}
} else if (!strncmp(*av, "zero", strlen(*av))) {
zero(ac,av);
@@ -1244,8 +1271,8 @@
#define WHITESP " \t\f\v\n\r"
char buf[BUFSIZ];
char *a, *p, *args[MAX_ARGS], *cmd = NULL;
- char linename[10], q_opt[3];
- int i, c, lineno, qflag, pflag, status;
+ char linename[10], q_opt[3], n_opt[3];
+ int i, c, lineno, nflag, qflag, pflag, status;
FILE *f = NULL;
pid_t preproc = 0;
@@ -1261,11 +1288,12 @@
*/
strcpy(q_opt, "-q");
+ strcpy(n_opt, "-n");
if (ac > 1 && av[ac - 1][0] == '/' && access(av[ac - 1], R_OK) == 0) {
- qflag = pflag = i = 0;
+ nflag = qflag = pflag = i = 0;
lineno = 0;
- while ((c = getopt(ac, av, "D:U:p:q")) != -1)
+ while ((c = getopt(ac, av, "D:U:np:q")) != -1)
switch(c) {
case 'D':
if (!pflag)
@@ -1287,6 +1315,10 @@
args[i++] = optarg;
break;
+ case 'n':
+ nflag = 1;
+ break;
+
case 'p':
pflag = 1;
cmd = optarg;
@@ -1360,11 +1392,17 @@
i=1;
if (qflag)
args[i++] = q_opt;
+ if (nflag)
+ args[i++] = n_opt;
for (a = strtok(buf, WHITESP);
a && i < MAX_ARGS; a = strtok(NULL, WHITESP), i++)
args[i] = a;
- if (i == (qflag? 2: 1))
+ if (nflag || qflag) {
+ if (i == ((nflag ^ qflag) ? 2 : 3))
+ continue;
+ } else if (i == 1) {
continue;
+ }
if (i == MAX_ARGS)
errx(EX_USAGE, "%s: too many arguments", linename);
args[i] = NULL;
More information about the Submit
mailing list