xref: /aosp_15_r20/external/iptables/iptables/iptables-standalone.c (revision a71a954618bbadd4a345637e5edcf36eec826889)
1 /*
2  * Author: [email protected] and [email protected]
3  *
4  * Based on the ipchains code by Paul Russell and Michael Neuling
5  *
6  * (C) 2000-2002 by the netfilter coreteam <[email protected]>:
7  * 		    Paul 'Rusty' Russell <[email protected]>
8  * 		    Marc Boucher <[email protected]>
9  * 		    James Morris <[email protected]>
10  * 		    Harald Welte <[email protected]>
11  * 		    Jozsef Kadlecsik <[email protected]>
12  *
13  *	iptables -- IP firewall administration for kernels with
14  *	firewall table (aimed for the 2.3 kernels)
15  *
16  *	See the accompanying manual page iptables(8) for information
17  *	about proper usage of this program.
18  *
19  *	This program is free software; you can redistribute it and/or modify
20  *	it under the terms of the GNU General Public License as published by
21  *	the Free Software Foundation; either version 2 of the License, or
22  *	(at your option) any later version.
23  *
24  *	This program is distributed in the hope that it will be useful,
25  *	but WITHOUT ANY WARRANTY; without even the implied warranty of
26  *	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
27  *	GNU General Public License for more details.
28  *
29  *	You should have received a copy of the GNU General Public License
30  *	along with this program; if not, write to the Free Software
31  *	Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
32  */
33 
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <errno.h>
37 #include <signal.h>
38 #include <string.h>
39 #include <iptables.h>
40 #include "iptables-multi.h"
41 
42 int
iptables_main(int argc,char * argv[])43 iptables_main(int argc, char *argv[])
44 {
45 	int ret;
46 	char *table = "filter";
47 	struct xtc_handle *handle = NULL;
48 
49 	signal(SIGPIPE, SIG_IGN);
50 
51 	iptables_globals.program_name = "iptables";
52 	ret = xtables_init_all(&iptables_globals, NFPROTO_IPV4);
53 	if (ret < 0) {
54 		fprintf(stderr, "%s/%s Failed to initialize xtables\n",
55 				iptables_globals.program_name,
56 				iptables_globals.program_version);
57 				exit(1);
58 	}
59 	init_extensions();
60 	init_extensions4();
61 
62 	ret = do_command4(argc, argv, &table, &handle, false);
63 	if (ret) {
64 		ret = iptc_commit(handle);
65 		iptc_free(handle);
66 	}
67 
68 	xtables_fini();
69 
70 	if (!ret) {
71 		if (errno == EINVAL) {
72 			fprintf(stderr, "iptables: %s. "
73 					"Run `dmesg' for more information.\n",
74 				iptc_strerror(errno));
75 		} else {
76 			fprintf(stderr, "iptables: %s.\n",
77 				iptc_strerror(errno));
78 		}
79 		if (errno == EAGAIN)
80 			exit(RESOURCE_PROBLEM);
81 	}
82 
83 	exit(!ret);
84 }
85