contrib/fwd: implement fwd_ipt_delif() and fwd_ipt_clear_ruleset()
[project/luci.git] / contrib / fwd / src / fwd.c
1 /*
2  * fwd - OpenWrt firewall daemon - main part
3  *
4  *   Copyright (C) 2009 Jo-Philipp Wich <xm@subsignal.org>
5  *
6  * The fwd program is free software: you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License version 2
8  * as published by the Free Software Foundation.
9  *
10  * The fwd program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13  * See the GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License along
16  * with the fwd program. If not, see http://www.gnu.org/licenses/.
17  */
18
19
20 #include "fwd.h"
21 #include "fwd_addr.h"
22 #include "fwd_rules.h"
23 #include "fwd_config.h"
24 #include "fwd_xtables.h"
25
26
27 int main(int argc, const char *argv[])
28 {
29         struct fwd_handle *h;
30
31         if( getuid() > 0 )
32                 fwd_fatal("Need root permissions!");
33
34         if( !(h = fwd_alloc_ptr(struct fwd_handle)) )
35                 fwd_fatal("Out of memory");
36
37         if( !(h->conf = fwd_read_config()) )
38                 fwd_fatal("Failed to read configuration");
39
40         if( (h->rtnl_socket = socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE)) == -1 )
41                 fwd_fatal("Failed to create AF_NETLINK socket (%m)");
42
43         if( !(h->addrs = fwd_get_addrs(h->rtnl_socket, AF_INET)) )
44                 fwd_fatal("Failed to issue RTM_GETADDR (%m)");
45
46         fwd_ipt_build_ruleset(h);
47
48         fwd_ipt_addif(h, "lan");
49         fwd_ipt_addif(h, "wan");
50
51         sleep(1);
52
53         fwd_ipt_delif(h, "wan");
54         fwd_ipt_delif(h, "lan");
55
56         fwd_ipt_clear_ruleset(h);
57
58         close(h->rtnl_socket);
59         fwd_free_config(h->conf);
60         fwd_free_addrs(h->addrs);
61         fwd_free_ptr(h);
62
63         return 0;
64 }