fix: get network card interface in Android 11 environment #61088
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
For #40569
Fix: In response to the modifications made to the permissions for accessing system MAC addresses in Android 11, ordinary applications encounter several main issues when using NETLINK sockets:
NETLINK
sockets.RTM_GETLINK
functionality.For detailed information, please refer to: https://developer.android.com/training/articles/user-data-ids#mac-11-plus
As a result of the aforementioned reasons, using
net.Interfaces()
andnet.InterfaceAddrs()
from the Go net package in the Android environment leads to theroute ip+net: netlinkrib: permission denied
error.You can find specific issue details here: #40569
To address the issue of using the Go net package in the Android environment, we have made partial modifications to its source code to ensure proper functionality on Android.
I have fully resolved the issues with
net.InterfaceAddrs()
.However, for
net.Interfaces()
, we have only addressed some problems, as the following issues still remain:Nevertheless, the fixed
net.Interfaces()
function now aligns with the Android API'sNetworkInterface.getNetworkInterfaces()
and can be used normally in most scenarios.The specific fix logic includes:
Removing the
Bind()
operation onNetlink
sockets in theNetlinkRIB()
function.Using
ioctl
based on the Index number returned byRTM_GETADDR
to retrieve the network card's name, MTU, and flags.