问题
I am developing a firewall application in android where I need to identify apps based on uid. I have created a vpn service and I am able to capture packets and get destination ip address.
Is there any way to get uid of apps from packets or datagram sockets.
回答1:
Parse the packet, for TCP/UDP, you can retrieve the (local_ip, local_port, remote_ip, remote_port) tuple. Then read and parse /proc/net/tcp or /proc/net/udp, there's uid field in it.
This is a loopback socket whose owner uid is 10117:
sl local_address rem_address st tx_queue rx_queue tr tm->when retrnsmt uid timeout inode
7: 0100007F:8FC5 0100007F:0438 01 00000000:00000000 00:00000000 00000000 10117 0 242607 1 00000000 21 4 26 10 -1
回答2:
You will have to write some native code for this to communicate at kernel level.
来源:https://stackoverflow.com/questions/28930342/how-to-get-uid-from-packet-android