This can be done in user space using the QUEUE or NFQUEUE iptables target I am thinking of. The client application joins the queue and receives all the appropriate packets that it can change before re-entering them (it can also drop them if it wants).
There is a client library libnetfilter_queue with which it needs to be associated. Unfortunately, the documentation is minimal, but there are several mailing lists and examples that are confusing.
For performance reasons, you will not want to do this with each package, but only with the specific ones that you have to map using the standard iptables rules. If this is not enough, you need to write your own netfilter kernel module.
Markr source share