0

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?

2 Answers2

2

Strange use-case, but hey, perhaps something like this could work - but it aint pretty:

  1. Get inode number for the fd returned by accept using fstat.
  2. Read /proc/self/net/tcp
  3. Parse and find the row matching the inode number.
  4. Use the uid of that row.
Jahaja
  • 3,222
  • 1
  • 20
  • 11
1

You can't. You get the remote IP address and port. That's it.

user207421
  • 305,947
  • 44
  • 307
  • 483