You can interface with the iptables
library called libiptc
.
That's how I have created my Perl interface to iptables
: CPAN IPTables::libiptc
But the libiptc
library only gives you an API to the basic chain structures.
Accessing and parsing the individual rules is a bit more complicated, as it depends on dyn-loading
the shared libs of the individual target/match modules.
My approach in my CPAN module is that I have linked with do_command()
from iptables.c
, for doing rule changes.
Another thing you need to know is:
That a single iptables
call, perform these actions:
- Copy the entire ruleset from the kernel to userspace
- Parse it with
libiptc
- Perform one or several changes (usually just one change via iptables cmd)
- Transform it to kernel blob format, by libiptc
- Copy the entire (new) ruleset from userspace to kernel.
Thus, a heavy process, if you only make a single change each time.
But you can also use this to your advantage, and perform many changes at once, and have these appear as a single atomic change, by/for the kernel.