Watch, Follow, &
Connect with Us

For forums, blogs and more please visit our
Developer Tools Community.


Welcome, Guest
Guest Settings
Help

Thread: GStack.GetLocalAddressList under Android


This question is answered. Helpful answers available: 2. Correct answers available: 1.


Permlink Replies: 5 - Last Post: Apr 18, 2018 4:46 AM Last Post By: Bart Kindt
Bart Kindt

Posts: 33
Registered: 12/16/07
GStack.GetLocalAddressList under Android  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Apr 8, 2018 10:41 PM
This does not work properly under Android. I only get the 127.0.0 network.

But I really need a list of all available IPv4 networks.
Is there any way that the Indy10 team can make this work under Android?

Thanks, Bart

---
Bart Kindt
CEO & Developer
SARTrack Limited New Zealand
http://www.sartrack.nz
Dave Nottage

Posts: 1,850
Registered: 1/7/00
Re: GStack.GetLocalAddressList under Android  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Apr 9, 2018 5:07 AM   in response to: Bart Kindt in response to: Bart Kindt
Bart Kindt wrote:

But I really need a list of all available IPv4 networks.
Is there any way that the Indy10 team can make this work under Android?

If I remember correctly, the Indy team don't use the Androidapi units because they're not compatible with FPC, however
you can use this code to obtain IPv4 and IPv6 addresses in Delphi (you'll need to create an instance of
TIdStackLocalAddressList to pass to it):

interface
 
uses
  IdStack;
 
procedure GetLocalAddressList(const AAddresses: TIdStackLocalAddressList);
 
implementation
 
uses
  System.SysUtils,
  Androidapi.JNI.Java.Net, Androidapi.JNI.JavaTypes, Androidapi.Helpers,  Androidapi.JNIBridge,
  IdGlobal;
 
procedure GetLocalAddressList(const AAddresses: TIdStackLocalAddressList);
var
  LInterfaces, LAddresses: JEnumeration;
  LInterface: JNetworkInterface;
  LAddress: JInetAddress;
  LName, LHostAddress: string;
begin
  AAddresses.Clear;
  LInterfaces := TJNetworkInterface.JavaClass.getNetworkInterfaces;
  while LInterfaces.hasMoreElements do
  begin
    LInterface := TJNetworkInterface.Wrap(JObjectToID(LInterfaces.nextElement));
    LAddresses := LInterface.getInetAddresses;
    while LAddresses.hasMoreElements do
    begin
      LAddress := TJInetAddress.Wrap(JObjectToID(LAddresses.nextElement));
      if LAddress.isLoopbackAddress then
        Continue;
      // Hack until I can find out how to check properly
      LName := JStringToString(LAddress.getClass.getName);
      LHostAddress := JStringToString(LAddress.getHostAddress);
      // Trim excess stuff
      if LHostAddress.IndexOf('%') > -1 then
        LHostAddress := LHostAddress.Substring(0, LHostAddress.IndexOf('%'));
      if LName.Contains('Inet4Address') then
        TIdStackLocalAddressIPv4.Create(AAddresses, LHostAddress, '')
      else if LName.Contains('Inet6Address') then
        TIdStackLocalAddressIPv6.Create(AAddresses, LHostAddress);
    end;
  end;
end;


You'll note there's a little "hacking around" to make it work. I'm yet to change it since it works for me so far.

Not long ago I looked into doing it using an "ifaddrs equivalent" for Android using all Posix calls (instead of via
JNI), however it became too time consuming. Perhaps the Indy team might do it in the future.

--
Dave Nottage [MVP, TeamB]
Find hints, tips and tricks at Delphi Worlds blog: http://www.delphiworlds.com
Remy Lebeau (Te...


Posts: 9,447
Registered: 12/23/01
Re: GStack.GetLocalAddressList under Android  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Apr 9, 2018 11:17 AM   in response to: Dave Nottage in response to: Dave Nottage
Dave Nottage wrote:

If I remember correctly, the Indy team don't use the Androidapi units
because they're not compatible with FPC

That is not the only reason why (and besides, they could just be
IFDEF'ed out for FPC). I just don't like using Embarcadero's JNIBridge
framework in Indy at all, for various technical reason (mainly
threading issues).

however you can use this code to obtain IPv4 and IPv6 addresses in
Delphi

Looks very similar to code I have commented out in
GetLocalAddressList()'s source code ;-)

Not long ago I looked into doing it using an "ifaddrs equivalent" for
Android using all Posix calls (instead of via JNI), however it became
too time consuming.

Yeah, there is an implementation at
https://github.com/kmackay/android-ifaddrs that I have been wanting to
use for awhile now. I started porting it to Pascal awhile ago, but it
still has a ways to go, and I don't have a system to compile and test
it on.

Perhaps the Indy team might do it in the future.

It is on the TODO list, bu no ETA.

--
Remy Lebeau (TeamB)
Dave Nottage

Posts: 1,850
Registered: 1/7/00
Re: GStack.GetLocalAddressList under Android  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Apr 9, 2018 2:20 PM   in response to: Remy Lebeau (Te... in response to: Remy Lebeau (Te...
Remy Lebeau (TeamB) wrote:

That is not the only reason why (and besides, they could just be
IFDEF'ed out for FPC). I just don't like using Embarcadero's JNIBridge
framework in Indy at all, for various technical reason (mainly
threading issues).

Thanks for the clarification.

however you can use this code to obtain IPv4 and IPv6 addresses in
Delphi

Looks very similar to code I have commented out in
GetLocalAddressList()'s source code ;-)

I was sure I wrote it from scratch using Java examples; now you have me doubting that ;-)

Not long ago I looked into doing it using an "ifaddrs equivalent" for
Android using all Posix calls (instead of via JNI), however it became
too time consuming.

Yeah, there is an implementation at
https://github.com/kmackay/android-ifaddrs that I have been wanting to
use for awhile now.

That link is a 404 for me. Looks like it moved to:

https://github.com/morristech/android-ifaddrs

Which is the one I started with.

I started porting it to Pascal awhile ago, but it
still has a ways to go, and I don't have a system to compile and test
it on.

I'd be willing to test and/or contribute.

--
Dave Nottage [MVP, TeamB]
Find hints, tips and tricks at Delphi Worlds blog: http://www.delphiworlds.com
Bart Kindt

Posts: 33
Registered: 12/16/07
Re: GStack.GetLocalAddressList under Android  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Apr 18, 2018 4:46 AM   in response to: Dave Nottage in response to: Dave Nottage
Hi, thanks for that.
I just found that the forum system is no longer sending me an email when reply posts come in on a question; As a result i did not know you and Remy had answered.

I will have a go at your code, many thanks.

Don't know what is going on with the forum email system, it is set up correctly and it also never got into my Spam, its just not working anymore...

Bart

If I remember correctly, the Indy team don't use the Androidapi units because they're not compatible with FPC, however
you can use this code to obtain IPv4 and IPv6 addresses in Delphi (you'll need to create an instance of
TIdStackLocalAddressList to pass to it):

--
Dave Nottage [MVP, TeamB]
Find hints, tips and tricks at Delphi Worlds blog: http://www.delphiworlds.com

--
---
Bart Kindt
CEO & Developer
SARTrack Limited New Zealand
http://www.sartrack.nz
Remy Lebeau (Te...


Posts: 9,447
Registered: 12/23/01
Re: GStack.GetLocalAddressList under Android  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Apr 9, 2018 11:09 AM   in response to: Bart Kindt in response to: Bart Kindt
Bart Kindt wrote:

This does not work properly under Android. I only get the 127.0.0
network.

That is a known issue (https://github.com/IndySockets/Indy/issues/205)
that has not been fixed in Indy yet.

I really need a list of all available IPv4 networks.

Until it is fixed in Indy, you will likely have to resort to using
Embarcedero's JNIBridge framework to access Android networking APIs
directly. There is a big TODO comment about that in Indy's source code
for the TIdStackVCLPosix.GetLocalAddressList() method in
IdStackVCLPosix.pas, to give some ideas.

Is there any way that the Indy10 team can make this work under
Android?

Probably not, at least not any time soon. I really try to stay away
from using JNIBridge in Indy (for various reasons), and some 3rd party
alternatives haven't been ported to Pascal yet. So, unless I can find
time to setup an Android dev environment and debug the issue myself
(which is probably not going to happen), it likely won't get fixed
without someone else providing a fix patch for me.

--
Remy Lebeau (TeamB)
Legend
Helpful Answer (5 pts)
Correct Answer (10 pts)

Server Response from: ETNAJIVE02