How can I programmatically manage iptables rules on the fly?

From the netfilter FAQ:

The answer unfortunately is: No.

Now you might think ‘but what about libiptc?’. As has been pointed out numerous times on the mailinglist(s), libiptc was NEVER meant to be used as a public interface. We don’t guarantee a stable interface, and it is planned to remove it in the next incarnation of linux packet filtering. libiptc is way too low-layer to be used reasonably anyway.

We are well aware that there is a fundamental lack for such an API, and we are working on improving that situation. Until then, it is recommended to either use system() or open a pipe into stdin of iptables-restore. The latter will give you a way better performance.

Leave a Comment