Getting the correct IP in Linux

Report any Hollywood bugs here
Post Reply
AlexC
Posts: 13
Joined: Sat Mar 09, 2019 2:40 am

Getting the correct IP in Linux

Post 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?

User avatar
airsoftsoftwair
Posts: 4350
Joined: Fri Feb 12, 2010 2:33 pm
Location: Germany
Contact:

Re: Getting the correct IP in Linux

Post by airsoftsoftwair »

Thanks for pointing out these limitations in Hollywood's network library. I'll think about ways to improve it.

User avatar
lazi
Posts: 552
Joined: Thu Feb 24, 2011 11:08 pm

Re: Getting the correct IP in Linux

Post 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.

User avatar
airsoftsoftwair
Posts: 4350
Joined: Fri Feb 12, 2010 2:33 pm
Location: Germany
Contact:

Re: Getting the correct IP in Linux

Post by airsoftsoftwair »

Right, I'll see what's wrong there.

User avatar
airsoftsoftwair
Posts: 4350
Joined: Fri Feb 12, 2010 2:33 pm
Location: Germany
Contact:

Re: Getting the correct IP in Linux

Post 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

AlexC
Posts: 13
Joined: Sat Mar 09, 2019 2:40 am

Re: Getting the correct IP in Linux

Post by AlexC »

Great! Thanks Andreas!

User avatar
airsoftsoftwair
Posts: 4350
Joined: Fri Feb 12, 2010 2:33 pm
Location: Germany
Contact:

Re: Getting the correct IP in Linux

Post 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.

AlexC
Posts: 13
Joined: Sat Mar 09, 2019 2:40 am

Re: Getting the correct IP in Linux

Post by AlexC »

Thanks Andreas, that worked and is much simpler.

Post Reply