ARTICLE AD BOX
I have an application where it needs to be aware of the IP addresses of all local connections, but it is disadvantageous to constantly poll getifaddrs for instance. On Linux I can do:
int sock = socket( PF_NETLINK, SOCK_RAW, NETLINK_ROUTE ); addr.nl_family = AF_NETLINK; addr.nl_groups = RTMGRP_IPV4_IFADDR | RTMGRP_IPV6_IFADDR; bind( sdifaceupdown, (sockaddr *)&addr, sizeof(addr));And then I can poll for any changes. This works very well on Linux. Thanks, @elenbert for in this answer: https://stackoverflow.com/a/55621891/2926815 for the help.
However, on Android, on the bind command, I get Permission denied. I've tried to use various combinations of nl_groups like RTMGRP_LINK | RTMGRP_IPV4_IFADDR | RTMGRP_IPV6_IFADDR | RTMGRP_IPV6_ROUTE | (1 << (RTNLGRP_ND_USEROPT - 1) and I've tried changing the socket from SOCK_RAW to SOCK_DGRAM | SOCK_CLOEXEC based on [2]
I've also, based on 2 tried adding various combinations of SO_RCVBUFFORCE, SO_RCVBUF, and SO_PASSCRED and adding addr.nl_pid = getpid() to no avail.
There doesn't seem to be any situations where I can poll for events or detect a network change event on Android that does not result in Access denied.
Is there any Android "trick" to letting it properly follow the SELinux rules for something like this?
Any help or pointers would be greatly appreciated.
Also of note, I found this: https://github.com/arvidn/libtorrent/issues/6251 / The issue screenshotted an Android change that reads "Apps cannot use the bind() function on NETLINK_ROUTE sockets." -- but I can't seem to find any other way to get network change events on Android. Any help or recommendations would be greatly appreciated.
References:
https://olegkutkov.me/2018/02/14/monitoring-linux-networking-state-using-netlink/ https://android.googlesource.com/platform/system/netd/+/2dae2f8/NetlinkManager.cpp