user2632999
user2632999

Reputation: 1

Find out which user created local incoming TCP connection inside of application

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?

Upvotes: 0

Views: 433

Answers (2)

Jahaja
Jahaja

Reputation: 3302

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.

Upvotes: 2

user207421
user207421

Reputation: 310979

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

Upvotes: 1

Related Questions