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

Fail to open Server with WiFi without Internet and cellular enabled (android) #103

Open
RobinSchuler opened this issue Apr 4, 2024 · 3 comments
Labels
On Hold Cannot be resolved at the moment. P2 Important issue.

Comments

@RobinSchuler
Copy link

Somewhat related to #21

How to reproduce:

  • be on Android
  • connect to a local wifi without Internet
  • have cellular turned on
  • open a server with nonLocal:true (other devices in the wifi should be able to reach it), do not specify hostname + port (for reasons I am currently not able to retrieve my ip within the network)
    => Server crashes

I´m currently investigating if I can get a hold of the ip + port to use in that network but wanted to get in touch with you anyway.

If I turn off cellular, still use the wifi ( without internet connection ) and do not specify port + host static server will retrieve the correct ip + port, open the server and other devices within the network can access the server.

Would you be able to handle the case cellular + no internet WiFi on your side ?

Greetings

@birdofpreyru
Copy link
Owner

Hi @RobinSchuler , yes, it is the same stuff as #21.

The problem is clear — if you set nonLocal flag, the library on Android relies on this simple function to auto-select IP address to bind to:

@ReactMethod
override fun getLocalIpAddress(promise: Promise) {
try {
val en = NetworkInterface.getNetworkInterfaces()
while (en.hasMoreElements()) {
val intf = en.nextElement()
val enumIpAddr = intf.inetAddresses
while (enumIpAddr.hasMoreElements()) {
val inetAddress = enumIpAddr.nextElement()
if (!inetAddress.isLoopbackAddress) {
val ip = inetAddress.hostAddress
if (isIPv4Address(ip)) {
promise.resolve(ip)
return
}
}
}
}
promise.resolve("127.0.0.1")
} catch (e: Exception) {
Errors.FAIL_GET_LOCAL_IP_ADDRESS().reject(promise)
}
}

you see, it just cycles through all network interfaces, until the first one which is associated with a valid IPv4 address, and tries to use that address for the server. Apparently it does not result in the correct local IP in the situation you describe.

On my end, I don't know exactly how to correctly select the network interface among available ones; and I still have no motivation to spend my time on figuring it out and implementing it correctly. So, if you want to spend you time on making it work better — PRs are welcome!

Yeah, on the high level my thoughts regarding it were that there might be different needs to select the exact interface (like select local WIFI address, or select the mobile, or whatever makes sense). So I thought to deprecate nonLocal flag altogether, and instead allow to assign into hostname (apart of valid IP addresses) some special string constants which would be passed into that IP selection function and tell what exactly should be selected.

@RobinSchuler
Copy link
Author

Thanks for the fast reply @birdofpreyru,

My first thought was also something like a "preferred Network" prop (or alternative a exclude network), where the code snippet you posted would be altered to look for a match with the given name.
If "preferred Network" is not given or the name not found it would return the first match (like currently)

@RobinSchuler
Copy link
Author

@birdofpreyru
FYI, I patched it on my side (patch-package) with a filter prop. The filter checks if the interface name (NetworkInterface class) contains the filter criteria.
(Interfaces my mobile device had were "wlan0", sth for roaming (cant remember the exact name) and something else (dummy I think)

@birdofpreyru birdofpreyru added P2 Important issue. On Hold Cannot be resolved at the moment. labels Apr 12, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
On Hold Cannot be resolved at the moment. P2 Important issue.
Projects
None yet
Development

No branches or pull requests

2 participants