-
Notifications
You must be signed in to change notification settings - Fork 17.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
x/mobile: fix net.Interface() and net.InterfaceAddrs() in Android11 version #61089
base: master
Are you sure you want to change the base?
Conversation
This PR (HEAD: 1012c07) has been imported to Gerrit for code review. Please visit https://go-review.googlesource.com/c/go/+/507415 to see it. Tip: You can toggle comments from me using the |
Message from Gopher Robot: Patch Set 1: Congratulations on opening your first change. Thank you for your contribution! Next steps: Most changes in the Go project go through a few rounds of revision. This can be During May-July and Nov-Jan the Go project is in a code freeze, during which Please don’t reply on this GitHub thread. Visit golang.org/cl/507415. |
This PR (HEAD: a64399c) has been imported to Gerrit for code review. Please visit https://go-review.googlesource.com/c/go/+/507415 to see it. Tip: You can toggle comments from me using the |
Message from wei liu: Patch Set 3: Code-Review+1 Please don’t reply on this GitHub thread. Visit golang.org/cl/507415. |
Message from Zoltan Papp: Patch Set 3: (1 comment) Please don’t reply on this GitHub thread. Visit golang.org/cl/507415. |
Message from Chih-Hsuan Yen: Patch Set 3: (1 comment) Please don’t reply on this GitHub thread. Visit golang.org/cl/507415. |
Message from GM K: Patch Set 3: (1 comment) Please don’t reply on this GitHub thread. Visit golang.org/cl/507415. |
This PR (HEAD: ad4f8ff) has been imported to Gerrit for code review. Please visit Gerrit at https://go-review.googlesource.com/c/go/+/507415. Important tips:
|
Message from Marcin Mazurek: Patch Set 4: (1 comment) Please don’t reply on this GitHub thread. Visit golang.org/cl/507415. |
This PR (HEAD: bff8d40) has been imported to Gerrit for code review. Please visit Gerrit at https://go-review.googlesource.com/c/go/+/507415. Important tips:
|
@gopherbot with all respect but last activity in Gerrit was at Oct 19, 2023. I assume that work was suspended. |
Any updates? |
I think the go official team may not accept this PR, because this fix may become invalid with later Android updates, and it may be too troublesome for the go team to maintain such a changing system problem. My suggestion is that you can use my third-party library or compile Go yourself to resolve this bug. |
@wlynxg could you please provide a link to the 3party lib? |
|
Good job! |
Unfortunately I'm unable to verify this fix since gomobile broken in go1.20 and above. |
The fix did not work for me: I got permission denied error in Android logs:
I have changed the net to anet package. |
If you are willing to provide your minimum executable code, I will be happy to help you solve the problem. You can send it directly to me through the email address on my homepage. |
Thank you for your so quick reply. I'm sending you more details as you have requested. |
I'm not sure but I think the problem in targetSdkVersion 34. Here is a report for fix in Android API 30 but for API 34 it fails. |
@wlynxg I created a demo project for you to show the result: https://github.com/vikulin/gomobile/blob/main/libmodule/src/go/mobile/hello.go
Put aar file in libs folder and build it. Result:
|
Your problem is due to not configuring the Android app's permissions, you need to add this line: <uses-permission android:name="android.permission.INTERNET" /> |
@wlynxg yes, you are right. Let me correct this. |
@wlynxg corrected permissions and found the code where the error is getting from: it's in iface.Addrs() Result: Now the defect is fully explained and ready for a fix. |
@vikulin You need to use https://pkg.go.dev/github.com/wlynxg/anet#InterfaceAddrsByInterface to get the correct address. |
@vikulin I'm sorry, there is a bug in my library, I will fix it soon. |
How may I help you to get it working? |
Sorry. It was late yesterday. I have fixed the bug today. |
@wlynxg the fix confirmed. Could you please use Android API version number instead of Android version in https://github.com/wlynxg/anet/blob/v0.0.2/interface.go#L30 ? |
Is this PR going to be come back to? This causes issues with a lot of go-based apps in Termux. It looks like there were a couple of comments that have yet to be addressed: https://go-review.googlesource.com/c/go/+/507415/3/src/syscall/netlink_linux.go @wlynxg's library is great, but native support for basic networking functionality like this should be the goal for a cross-platform language like Go. |
The two issues have been fixed and explained in subsequent submissions. |
Android API version does not seem to be related to this issue, as the issue is caused by Android SELinux. |
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.Test Code
This part of the test code must be compiled into a jar and aar package that can be used under the Android 11 platform using gomobile
use
net.InterfaceAddrs()
:result:
use
net.Interfaces()
:result: