Skip to content
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

How to use this library? #10

Closed
tri-bao opened this issue Jan 6, 2025 · 7 comments
Closed

How to use this library? #10

tri-bao opened this issue Jan 6, 2025 · 7 comments

Comments

@tri-bao
Copy link

tri-bao commented Jan 6, 2025

hi @wlynxg

Thank you very much for creating this lib. I am using another 3rd lib which internally uses net.Interfaces() and as expected it fails on android. I couldn't change source code of that lib to use your anet.Interfaces(). How can I use this lib to work around the issue? I'm very new to golang, kind of couple weeks of experiences. I read and try to understand how //go:linkname could help in this case. I vaguely imagine //go:linkname will help link/replace the go internal implementation with yours. However, I couldn't make it work.

@wlynxg
Copy link
Owner

wlynxg commented Jan 7, 2025

Hi friend, thank you for using anet library.
anet library is very easy to use, you just need to replace net.Interfaces() or net.InterfaceAddrs() with anet.Interfaces() and anet.InterfaceAddrs(). However, if you are using go 1.23 or above, you need to add the compilation parameter: -checklinkname=0

@wlynxg
Copy link
Owner

wlynxg commented Jan 7, 2025

If you need any help, please feel free to let me know.

@tri-bao
Copy link
Author

tri-bao commented Jan 7, 2025

do you mean I need to replace net.Interfaces with anet.Interfaces everywhere in source code, including in third-party libs which we installed via go get?

I tried to make a single file for mobile test as following

package test_mobile

import (
	"github.com/wlynxg/anet"
	_ "github.com/wlynxg/anet"
	"log"
	"net"
	_ "unsafe"
)

// Start called from mobile native code
func Start(privateDataPath string) {
	interfaces, err := anet.Interfaces()
	if err != nil {
		log.Printf("failed to get interfaces with ANET.Interfaces: %s", err)
	}
	for _, i := range interfaces {
		log.Printf("ANET.Interfaces: %v", i)
	}

	interfaces, err = net.Interfaces()
	if err != nil {
		log.Printf("XXXXX: failed to get interfaces with net.Interfaces: %s", err)
	}
	for _, i := range interfaces {
		log.Printf("net.Interfaces: %v", i)
	}
}

I build it with

gomobile bind \
  -target=android \
  -androidapi=24 \
  -ldflags="-checklinkname=0" \
  -o test-mobile.aar

I wish the call to net.Interfaces() after doing //go:linkname will succeed, but it doesn't. Do I miss anything?

ANET.Interfaces: {1 65536 lo  up|loopback|running}
ANET.Interfaces: {11 1500 rmnet_data1  up|running}
ANET.Interfaces: {13 1500 rmnet_data3  up|running}
ANET.Interfaces: {30 1500 wlan0  up|broadcast|multicast|running}
ANET.Interfaces: {3 1500 dummy0  up|broadcast|running}
ANET.Interfaces: {10 1500 rmnet_data0  up|running}
ANET.Interfaces: {21 1500 r_rmnet_data0  up|running}
XXXXX: failed to get interfaces with net.Interfaces: route ip+net: netlinkrib: permission denied

@wlynxg
Copy link
Owner

wlynxg commented Jan 7, 2025

Sorry, the current anet library only guarantees that the correct result is obtained when executing anet.Interfaces(), and does not synchronize the correct result to the standard library using the //go:linkname tag.

If many people have such a need, should we consider doing this? However, this is a bit invasive to Go code and does not seem to be a good solution.

@tri-bao
Copy link
Author

tri-bao commented Jan 7, 2025

I see, that explains.
I'm going to add //go:linkname to my mobile code to replace the standard library funtions. At the end of the day, we all need to get things work. Currently, the standard library doesn't work on Android; the go team seems to not plan to fix it; your PR to go code couldn't be merged; the use of net.Interfaces is not in my code but in 3rd libs. I can't think of any other solution except replacing the standard impl with yours; why should we keep the standard functions on Android when they don't work at all :)

Again, thanks a lot for your great work!

@tri-bao
Copy link
Author

tri-bao commented Jan 7, 2025

@wlynxg oh I've just discovered your ticket in the go-libp2p repo. I'm using that lib as well. Would you mind sharing with me how you made go-libp2p work using this anet lib?

@tri-bao tri-bao closed this as completed Jan 8, 2025
@wlynxg
Copy link
Owner

wlynxg commented Jan 9, 2025

@wlynxg oh I've just discovered your ticket in the go-libp2p repo. I'm using that lib as well. Would you mind sharing with me how you made go-libp2p work using this anet lib?

I'm very sorry, I was busy looking for a job during this period and couldn't reply to you in time.
This problem is indeed what I encountered when I migrated libp2p to Android, but libp2p does not want to deal with Android-related issues. I had no choice but to pull the go-multiaddr library and make changes myself. You can see this PR: multiformats/go-multiaddr#255

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants