Skip to contentSkip to conversation

argv[optind-1] == optarg

DISCUSSION Ozgur Akan 2 participants 4 messages May 22, 2004 – May 26, 2004 netfilter

lore
Share & tools
lore

Ozgur Akan

author
#0 lore raw

Hi,

I think using optarg instead of argv[optind-1] will be better. In iptables.c; while parsing iptables options with getopt_long, argv[optind-1] is used in 8 places.

optarg (option argument) gives us the value of the option we are parsing. So when we do something with the argument of (for example) A option the easiest way to get the value of A`s argument is using optarg. optarg is designed to be used with getopt_long.

Using optind (option index) is more tricky because optind starts from 1 and when we are parsing an option with an index of value n of argv array it shows n+2. That`s why we substract 1 from optind.

best wishes,
Ozgur Akan

author
#1 lore raw

Hi,
I attached the patch file for optarg. It includes patches for iptables.c and ip6tables.c.

regards,
Ozgur Akan

Quoted 10 lines · Ozgur Akan

Hi,

I think using optarg instead of argv[optind-1] will be better. In iptables.c; while parsing iptables options with getopt_long, argv[optind-1] is used in 8 places.

optarg (option argument) gives us the value of the option we are parsing. So when we do something with the argument of (for example) A option the easiest way to get the value of A`s argument is using optarg. optarg is designed to be used with getopt_long.

Using optind (option index) is more tricky because optind starts from 1 and when we are parsing an option with an index of value n of argv array it shows n+2. That`s why we substract 1 from optind.

best wishes,
Ozgur Akan

ip6tables.c +8 -8
diff -Naur iptables/ip6tables.c iptables_dev/ip6tables.c
@@ -1853,10 +1853,10 @@
 				   invert);
 
 			/* Canonicalize into lower case */
-			for (protocol = argv[optind-1]; *protocol; protocol++)
+			for (protocol = optarg; *protocol; protocol++)
 				*protocol = tolower(*protocol);
 
-			protocol = argv[optind-1];
+			protocol = optarg;
 			if ( strcmp(protocol,"ipv6-icmp") == 0)
 				protocol = icmp6p;
 			fw.ipv6.proto = parse_protocol(protocol);
@@ -1873,7 +1873,7 @@
 			check_inverse(optarg, &invert, &optind, argc);
 			set_option(&options, OPT_SOURCE, &fw.ipv6.invflags,
 				   invert);
-			shostnetworkmask = argv[optind-1];
+			shostnetworkmask = optarg;
 			fw.nfcache |= NFC_IP6_SRC;
 			break;
 
@@ -1881,7 +1881,7 @@
 			check_inverse(optarg, &invert, &optind, argc);
 			set_option(&options, OPT_DESTINATION, &fw.ipv6.invflags,
 				   invert);
-			dhostnetworkmask = argv[optind-1];
+			dhostnetworkmask = optarg;
 			fw.nfcache |= NFC_IP6_DST;
 			break;
 
@@ -1911,7 +1911,7 @@
 			check_inverse(optarg, &invert, &optind, argc);
 			set_option(&options, OPT_VIANAMEIN, &fw.ipv6.invflags,
 				   invert);
-			parse_interface(argv[optind-1],
+			parse_interface(optarg,
 					fw.ipv6.iniface,
 					fw.ipv6.iniface_mask);
 			fw.nfcache |= NFC_IP6_IF_IN;
@@ -1921,7 +1921,7 @@
 			check_inverse(optarg, &invert, &optind, argc);
 			set_option(&options, OPT_VIANAMEOUT, &fw.ipv6.invflags,
 				   invert);
-			parse_interface(argv[optind-1],
+			parse_interface(optarg,
 					fw.ipv6.outiface,
 					fw.ipv6.outiface_mask);
 			fw.nfcache |= NFC_IP6_IF_OUT;
@@ -1961,7 +1961,7 @@
 			if (invert)
 				exit_error(PARAMETER_PROBLEM,
 					   "unexpected ! flag before --table");
-			*table = argv[optind-1];
+			*table = optarg;
 			break;
 
 		case 'x':
@@ -2100,7 +2100,7 @@
 				if (!m)
 					exit_error(PARAMETER_PROBLEM,
 						   "Unknown arg `%s'",
-						   argv[optind-1]);
+						   optarg);
 			}
 		}
 		invert = FALSE;
iptables.c +8 -8
diff -Naur iptables/iptables.c iptables_dev/iptables.c
@@ -1850,10 +1850,10 @@
 				   invert);
 
 			/* Canonicalize into lower case */
-			for (protocol = argv[optind-1]; *protocol; protocol++)
+			for (protocol = optard; *protocol; protocol++)
 				*protocol = tolower(*protocol);
 
-			protocol = argv[optind-1];
+			protocol = optarg;
 			fw.ip.proto = parse_protocol(protocol);
 
 			if (fw.ip.proto == 0
@@ -1867,7 +1867,7 @@
 			check_inverse(optarg, &invert, &optind, argc);
 			set_option(&options, OPT_SOURCE, &fw.ip.invflags,
 				   invert);
-			shostnetworkmask = argv[optind-1];
+			shostnetworkmask = optarg;
 			fw.nfcache |= NFC_IP_SRC;
 			break;
 
@@ -1875,7 +1875,7 @@
 			check_inverse(optarg, &invert, &optind, argc);
 			set_option(&options, OPT_DESTINATION, &fw.ip.invflags,
 				   invert);
-			dhostnetworkmask = argv[optind-1];
+			dhostnetworkmask = optarg;
 			fw.nfcache |= NFC_IP_DST;
 			break;
 
@@ -1905,7 +1905,7 @@
 			check_inverse(optarg, &invert, &optind, argc);
 			set_option(&options, OPT_VIANAMEIN, &fw.ip.invflags,
 				   invert);
-			parse_interface(argv[optind-1],
+			parse_interface(optarg,
 					fw.ip.iniface,
 					fw.ip.iniface_mask);
 			fw.nfcache |= NFC_IP_IF_IN;
@@ -1915,7 +1915,7 @@
 			check_inverse(optarg, &invert, &optind, argc);
 			set_option(&options, OPT_VIANAMEOUT, &fw.ip.invflags,
 				   invert);
-			parse_interface(argv[optind-1],
+			parse_interface(optarg,
 					fw.ip.outiface,
 					fw.ip.outiface_mask);
 			fw.nfcache |= NFC_IP_IF_OUT;
@@ -1962,7 +1962,7 @@
 			if (invert)
 				exit_error(PARAMETER_PROBLEM,
 					   "unexpected ! flag before --table");
-			*table = argv[optind-1];
+			*table = optarg;
 			break;
 
 		case 'x':
@@ -2100,7 +2100,7 @@
 				if (!m)
 					exit_error(PARAMETER_PROBLEM,
 						   "Unknown arg `%s'",
-						   argv[optind-1]);
+						   optarg);
 			}
 		}
 		invert = FALSE;
#2 lore raw
In reply to Ozgur Akan
Quoted 3 lines · Ozgur Akan

I think using optarg instead of argv[optind-1] will be better. In iptables.c; while parsing iptables options with getopt_long, argv[optind-1] is used in 8 places.

If I recall correectly the reason why it is done the way it is is the processing of ! (not).

! -d a.b.c.d
-d ! a.b.c.d

Regards
Henrik

author
#3 lore raw
Quoted 5 lines

! -d a.b.c.d
-d ! a.b.c.d

...hmm, I am going to work on it.

thanks,
Ozgur Akan

Signature · 1 line
Ozgur Akan