On Linux, it's possible to filter packets originating on localhost based on the user or group that created them, i.e. who owns the socket:
iptables ... -m owner --uid-owner $USER --gid-owner $GROUP -p tcp ...
But I want to approach it from the point of view of the program, running on localhost and written in C, receiving the packets: I bound to some port, and here comes a new TCP connection / UDP packets. How to find out who sent that?