Page 1 of 1

Getting the correct IP in Linux

Posted: Thu Apr 30, 2020 3:00 am
by AlexC
I have an issue, not really a bug, in Linux where I try to get the IP of the ethernet card to scan the LAN for servers, but the hostname tend to be associated with the loopback address.

On the Amiga and Windows the first line of code below works fine but I had to add some workarounds to get the right IP on Linux.

Code: Select all

Function p_GetIP()
 Local myip = ToIP(GetHostName()) /* OK on Amiga and Win, usually returns 127.0.0.1 on Linux */

/* Workaround for Linux that requires external connection */
 If StartsWith(myip,"127") Or StartsWith(myip,"0.")
  Local id = OpenConnection(Nil, "8.8.8.8", 53)
  myip = GetLocalIP(id)
  CloseConnection(id)

/* Workaround with manual user input */
  If StartsWith(myip,"127") Or StartsWith(myip,"0.")
   Local ip,ok = StringRequest("GetIP", "Could not find local network IP\nPlease specify your current LAN IP (IPv4)")
   If ip And ok Then myip = ip
  EndIf 

 EndIf
 Return(myip)
EndFunction
Using the workaround code above it can find the ethernet IP but it requires an external connection which isn't always desirable if the user has privacy/security concerns.
I'm just not sure how the network library could be improved to return the LAN IP instead of the loopback address. Maybe some NoLoopoback=True tag?
And there's also the situation where a machine has multiple network interfaces, wired and or wireless, if more than one is up, how to at least get a list to let the user pick one?

Re: Getting the correct IP in Linux

Posted: Thu Apr 30, 2020 9:43 pm
by airsoftsoftwair
Thanks for pointing out these limitations in Hollywood's network library. I'll think about ways to improve it.

Re: Getting the correct IP in Linux

Posted: Fri May 01, 2020 9:18 pm
by lazi
Local myip = ToIP(GetHostName()) /* OK on Amiga and Win, usually returns 127.0.0.1 on Linux */
On OS4 it returns 127.0.0.1 too. Would be better to get the same result as on win, the LAN ip address.

Re: Getting the correct IP in Linux

Posted: Sat May 09, 2020 12:20 pm
by airsoftsoftwair
Right, I'll see what's wrong there.

Re: Getting the correct IP in Linux

Posted: Wed Sep 09, 2020 6:37 pm
by airsoftsoftwair

Code: Select all

- New: Added GetLocalInterfaces() to the network library; this will return a list of all interfaces that
  are currently available; it will return a table that contains "Name", "Address", and "Protocol" for each
  item; this allows you to conveniently determine a system's local IP address

Re: Getting the correct IP in Linux

Posted: Thu Sep 10, 2020 12:32 am
by AlexC
Great! Thanks Andreas!

Re: Getting the correct IP in Linux

Posted: Thu Sep 10, 2020 7:20 pm
by airsoftsoftwair
Btw, as a workaround for getting the IP address without requiring an Internet connection you could use UDP. This is a connectionless protocol so it won't try to establish any connection before you try to send or receive something. So you could just do:

Code: Select all

CreateUDPObject(1, 80, "8.8.8.8", #UDPCLIENT)
DebugPrint(GetLocalIP(1, #NETWORKUDP))
In contrast to your code this should work even if there is no Internet connection.

Re: Getting the correct IP in Linux

Posted: Tue Oct 20, 2020 11:47 pm
by AlexC
Thanks Andreas, that worked and is much simpler.