Android Question IPv6 not working correctly on Android ?

Status
Not open for further replies.

fs007

Member
On my device (Android 12), with wifi connection established in IPv6 capable LAN, the line
B4X:
Socket1.Connect("FE80::XXXX:YYYY:ZZZZ:RRRR",2222,10000)
leads to a runtime error in:
B4X:
Sub Socket1_connected(succ As Boolean)
    If succ = False Then
        Msgbox(LastException.Message, "Error connecting")
        Return
    End If

The error message is: "android.system.ErrnoException: connect failed: EINVAL (Invalid argument)"
Note: This is NOT a java.net.Socket.TimeoutException !

If i change the line to
B4X:
Socket1.Connect("FE70::XXXX:YYYY:ZZZZ:RRRR",2222,10000)
then it works ! (which means, the EINVAL error doesn't occur any more)

So my question is: Why do link-local IPv6 addresses (FE80::...) fail in Android/B4a ?
(btw: in the devices "settings-> connections" a link-local address is correctly shown)
 

LucaMs

Expert
Licensed User
Longtime User
[ChatGPT]

The issue you're encountering with link-local IPv6 addresses (starting with FE80:...) in Android/B4A likely stems from how Android handles network connections and permissions for socket operations.

Here are a few points to consider:

  1. Scope Identifier Requirement: When using a link-local IPv6 address (FE80::...), it's crucial to include the scope identifier for the network interface. The scope identifier specifies which interface to use for the connection. In some environments, including the scope identifier (e.g., %wlan0 for Wi-Fi) is necessary to ensure the correct interface is used.
    Example:
    Socket1.Connect("FE80::XXXX:YYYY:ZZZZ:RRRR%wlan0", 2222, 10000)
    Replace wlan0 with the actual interface name of your Wi-Fi connection. This ensures Android knows through which interface to attempt the connection.
  2. Android Security Policies: Android has stringent security policies regarding network operations. It might restrict or require specific permissions or configurations for certain types of network connections, especially when dealing with link-local addresses. Ensure your app has the necessary network permissions declared in the manifest (<uses-permission android:name="android.permission.INTERNET" />).
  3. Error Handling: The EINVAL error (Invalid argument) can occur for various reasons related to incorrect arguments passed to the socket connection function. Double-check that the format of the IPv6 address and the inclusion of the scope identifier are correct.
  4. Testing Environment: Consider testing your app on different Android versions and devices. Network behavior, including support for IPv6 link-local addresses, can vary depending on the device manufacturer and Android version.
In summary, ensure you include the correct scope identifier when using link-local IPv6 addresses in your socket connections on Android. This identifier specifies the network interface and helps Android route the connection correctly. Also, adhere to Android's network security policies and permissions to avoid runtime errors related to network operations.
 
Last edited:
Upvote 0

fs007

Member
  1. Scope Identifier Requirement
Thank you very much, @LucaMs . Your answer #1 is the solution. Adding %wlan0 to the ip-address made the code working.
But here is the next issue: this trick does NOT work with B4A's OkHttpRequest. Any idea how to solve that ???

@Erel said:
Why are you using Msgbox? Use Log instead.
MsgBox has the advantage that i can see the message even if the device is not connected to a PC.

The address is passed as is to the native API.
That' probably not true for OKHttpRequest. @LucaMs 's trick does not work with OkHttpRequest.
 
Last edited:
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
Thank you very much, @LucaMs . Your answer #1 is the solution. Adding %wlan0 to the ip-address made the code working.
But here is the next issue: this trick does NOT work with B4A's OkHttpRequest. Any idea how to solve that ???

@Erel said:

MsgBox has the advantage that i can see the message even if the device is not connected to a PC.


That' probably not true for OKHttpRequest. @LucaMs 's trick does not work with OkHttpRequest.
https://chatgpt.com/share/67c9dc0c-02ac-8002-9b22-2fa3c9b6b4b2
 
Upvote 0

fs007

Member
Sorry @LucaMs , but this time ChatGPT serves more confusion than solution:
Fact is:
1. in B4A OkHttpClient has no member CreateRequest as one can read here
2. This code does NOT work:
B4X:
Dim hc As OkHttpClient
hc.Initialize("hc")
Dim req As OkHttpRequest
req.InitializeGet("http://[FE80::XXXX:YYYY:ZZZZ:RRRR%wlan0]:1234/abcd.htm")
hc.Execute(req, 3)
....
Error in log is: java.lang.IllegalArgumentException: Invalid URL host: "[fe80::XXXX:YYYY:ZZZZ:RRRR%wlan0]"
Any more ideas ???
 
Upvote 0

fs007

Member
An addition to my last post:
Also HttpJob does not work with link-local IPv6 addresses. Means: this line throws a runtime error too.
B4X:
hj.Download("http://[FE80:AAAA:BBBB:CCCC:XXXX%wlan0]")
 
Upvote 0

fs007

Member
Never use Msgbox.
Use MsgboxAsync.
Never use OkHttpClient.
Use OkHttpUtils2.
ok, i deleted MsgBox from my code (although i don't know why) and i use OkHttpUtils2, Version 3.02, internal lib.
But fact is, that this code also doesn't run:
B4X:
Dim hj As HttpJob
hj.Initialize("",Me)
hj.Download("http://[FE80::AAAA:BBBB:CCCC:XXXX%wlan0]")
Wait for (hj) JobDone(hj As HttpJob)
If hj.Success Then
            Log(hj.GetString)
            label1.Text = hj.GetString
Else
            label1.Text ="Error"
End If
the "hj. Download" line throws this error: Invalid link: http://[fe80::AAAA:BBBB:CCCC:XXXX%wlan0]
which appears to be same the other libs throw (OkHttpClient,OkHttpRequest...)
So there seems to be a very basic bug in all these B4A libs.
 
Upvote 0

fs007

Member
The fact is that I already told you the answer long ago - the host name isn't managed by B4A in any of these libraries.
@Erel , you wrote in post #2 long ago: "The address is passed as is to the native API".
That holds true for the Network library, but is not true for OkHttpUtils2, OkHttpClient, OkHttpRequest... These libraries obviously interpret the NEEDED so called scope (i.e. "wlan0", separated by a "%", see LucaMS post #3) as error and throw an error.
That's probably the simple reason, why all these B4A libraries fail and should be simple to correct !
 
Last edited:
Upvote 0
Status
Not open for further replies.
Top