• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Help At Native Layer, Need PackageName of the Application which is using a specific port

  • Thread starter Thread starter Android Question
  • Start date Start date
A

Android Question

Guest
Dear All,


I am trying to get the package name of the application which is currently using a specific port.
Say for example, If Chrome application is using port 32991, I need to get "com.google.chrome" as package name output at native layer.
At Native layer , the only information known to me is the port number, and I have to get application package name which is using this port number.

My current approach :-
I am sending this port number from native layer to Android frameworks layer using a socket. From Frameworks, I am parsing the file "proc/net/tcp" for this received port number.
I am getting a UID associated with this port number.

Illustration Example :-
At Framwework layer we received the port and converted it to HEX value
Port Received: 43141, Hex value: :A885

proc/net/TCP file looks something like this

sl local_address rem_address st tx_queue rx_queue tr tm->when retrnsmt uid timeout inode
0: 0100007F:22A8 00000000:0000 0A 00000000:00000000 00:00000000 00000000 1002 0 21433 1 0000000000000000 100 0 0 10 0
1: 0100007F:13AD 00000000:0000 0A 00000000:00000000 00:00000000 00000000 0 0 3584 1 0000000000000000 100 0 0 10 0
2: 0100007F:13AE 00000000:0000 0A 00000000:00000000 00:00000000 00000000 0 0 39642 1 0000000000000000 100 0 0 10 0
3: 1E0610AC:AF56 0AD33AD8:A885 01 00000000:00000000 00:00000000 00000000 10216 0 60063 1 0000000000000000 88 4 15 10 -1
4: 1E0610AC:E0A7 6DC43AD8:01BB 01 00000000:00000000 00:00000000 00000000 10012 0 61667 1 0000000000000000 21 4 13 10 -1

and on searching for the above port number (A885), I got the UID as 10216

Now I am using android API "getPackageManager" to get the package Name

packageName = mContext.getPackageManager().getPackagesForUid(Integer.valueOf(uid[8]));
return packageName[0];


packageName obtained: com.skype.raider

This approach works fine for such third party Apps.

Problem with current Approach:-
This approach works fine for 3rd party Apps, but when I am trying to browse Youtube, I am getting the UID as 1000(which is system UID) from the file proc/net/tcp . I think it is because of Intermediate MediaServer running .
Due to this intermediate MediaServer runnig , packageName is not obtained for youtube applciation.

Same is the case with PlayStore.(there DownloadManager comes in between as system App, hence getting UID 1000 for the port).

It will be a great help if someone can please suggest me a way to get the application UID or packageName, which is associated with a port and working behind these intermediate system services(like mediaserver, download manager, etc) running in android.
Or
Please suggest and alternate way to get packageName from the port number only at native layer.

Thanks and Regards
Vijay
 
Back
Top Bottom