Local IP number of standalone webapp.

Hi everyone.

Im trying to get hold of the local IP address used by a standalone webapp from within the webapp itself, i.e. the IP address that the webapp is using.
I can not find any way of getting hold of it.

Is there something like SocketCore.LocalAddress but for webapps?

I need this because this webapp will mostly be running on a LAN, behind a NAT.
I would like the webapp to periodically report its local LAN IP address to a public cloud server so that users on the LAN can easily access the webapp on the LAN from a public domain via a simple redirect from the cloud server to the webapp on the LAN.

I suppose I could check the IP numbers of the network interfaces of the computer running the webapp, but how would I know which network interface the webapp is using in case there are several active network interfaces?

Any ideas on what would be the best approach?

Kind Regards,
Erik

Visit http://documentation.xojo.com/index.php/NetworkInterface.IPAddress to get started.

Surely you would want to know the ip address of your router rather than the internal ip address?

This may help : http://boshdirect.com/Blogs/Tech/find-local-ip-address.html

Thanks for your feedback.

Yes, that is what I was thinking of, have used it successfully before.

A potential issue arrises if multiple network interfaces are available. How would I know what network interface the webapp is using?
How does a webapp choose network interface if multiple are available?

Well, both actually.

The primary goal is to make it super easy for any non tech user to access the webapp from within the LAN, without knowing its IP address. IP numbers are still confusing for many people.

A headless raspberry pi running the webapp makes a http request to the cloud server, reporting its internal IP address.

The cloud server will also check and logg the public ip address of the router as well as the internal LAN ip address of the webapp.

If a web browser visits the cloud server from the same LAN (public IP) as a raspberry pi running the webapp, they will be automatically re-directed to the webapp on the LAN. That way they will not need to know the LAN IP number of the webapp, as long as they are on the same LAN.

[quote=239383:@Erik Fohlin]A potential issue arrises if multiple network interfaces are available. How would I know what network interface the webapp is using?
How does a webapp choose network interface if multiple are available?[/quote]
The Command-Line Parameters section at WebApplication has parameters for controlling which NICs are used for secure and non-secure connections. My guess is that if none are specified, the System.GetNetworkInterface with an index of zero is used.

I can execute this from an event handler or method of a web app:

dim s as new EasyTCPSocket App.ipAddr = s.LocalAddress
It could be placed in a timer, to update the cloud server. I am not sure how it handles with multiple network interfaces though.

Here is what I get with a shell to ifconfig per the link I posted above :

lo0: flags=8049<UP,LOOPBACK,RUNNING,MULTICAST> mtu 16384 options=3<RXCSUM,TXCSUM> inet6 ::1 prefixlen 128 inet 127.0.0.1 netmask 0xff000000 inet6 fe80::1%lo0 prefixlen 64 scopeid 0x1 nd6 options=1<PERFORMNUD> gif0: flags=8010<POINTOPOINT,MULTICAST> mtu 1280 stf0: flags=0<> mtu 1280 en1: flags=8863<UP,BROADCAST,SMART,RUNNING,SIMPLEX,MULTICAST> mtu 1500 ether 10:9a:dd:a4:c4:28 inet6 fe80::129a:ddff:fea4:c428%en1 prefixlen 64 scopeid 0x4 inet 192.168.0.37 netmask 0xffffff00 broadcast 192.168.0.255 nd6 options=1<PERFORMNUD> media: autoselect status: active en0: flags=8863<UP,BROADCAST,SMART,RUNNING,SIMPLEX,MULTICAST> mtu 1500 options=10b<RXCSUM,TXCSUM,VLAN_HWTAGGING,AV> ether c8:2a:14:23:37:37 nd6 options=1<PERFORMNUD> media: autoselect (none) status: inactive en2: flags=963<UP,BROADCAST,SMART,RUNNING,PROMISC,SIMPLEX> mtu 1500 options=60<TSO4,TSO6> ether d2:00:18:d4:71:60 media: autoselect <full-duplex> status: inactive fw0: flags=8863<UP,BROADCAST,SMART,RUNNING,SIMPLEX,MULTICAST> mtu 4078 lladdr c8:2a:14:ff:fe:8d:47:16 nd6 options=1<PERFORMNUD> media: autoselect <full-duplex> status: inactive p2p0: flags=8843<UP,BROADCAST,RUNNING,SIMPLEX,MULTICAST> mtu 2304 ether 02:9a:dd:a4:c4:28 media: autoselect status: active bridge0: flags=8863<UP,BROADCAST,SMART,RUNNING,SIMPLEX,MULTICAST> mtu 1500 options=63<RXCSUM,TXCSUM,TSO4,TSO6> ether 12:9a:dd:4a:64:00 Configuration: id 0:0:0:0:0:0 priority 0 hellotime 0 fwddelay 0 maxage 0 holdcnt 0 proto stp maxaddr 100 timeout 1200 root id 0:0:0:0:0:0 priority 0 ifcost 0 port 0 ipfilter disabled flags 0x2 member: en2 flags=3<LEARNING,DISCOVER> ifmaxaddr 0 port 6 priority 0 path cost 0 nd6 options=1<PERFORMNUD> media: <unknown type> status: inactive

It gives both the machine local IP, as well as the router address, in line 12…

[quote=239128:@Erik Fohlin]…
I would like the webapp to periodically report its local LAN IP address to a public cloud server so that users on the LAN can easily access the webapp on the LAN from a public domain via a simple redirect from the cloud server to the webapp on the LAN.

…Any ideas on what would be the best approach?[/quote]

If I understand you correctly, you want an internal resource to be available on the LAN and WAN. I think you are going about this completely the wrong way. Google “split DNS”.
Lets say your web app is webappp.mydomain.com then with split DNS the IP address for webappp.mydomain.com could be 192.168.200.254 when looked up internally, and a real IP when looked up externally. When using some DNS servers (like Microsofts) you can allow the local resource to automatically update the local DNS server with its internal IP if that ever changes,

Unless I’ve misunderstood the question, I think the solution is actually outside your web app.

[quote=239405:@Scott Siegrist]I can execute this from an event handler or method of a web app:

dim s as new EasyTCPSocket App.ipAddr = s.LocalAddress
It could be placed in a timer, to update the cloud server. I am not sure how it handles with multiple network interfaces though.[/quote]

Thanks Scott, a creative solution to my problem.

[code] dim h As new HTTPSocket
dim s, ip, port As string

ip=h.LocalAddress
port=str(App.Port)

h.Yield=True
s = h.Get("http://home.xyz.com/update_ip.php?ip="+ip+"&port="+port, 5)[/code]

Works great in a timer :slight_smile:
I hope it handles multiple network interfaces automatically.

[quote=239442:@David Andrews]If I understand you correctly, you want an internal resource to be available on the LAN and WAN. I think you are going about this completely the wrong way. Google “split DNS”.
Lets say your web app is webappp.mydomain.com then with split DNS the IP address for webappp.mydomain.com could be 192.168.200.254 when looked up internally, and a real IP when looked up externally. When using some DNS servers (like Microsofts) you can allow the local resource to automatically update the local DNS server with its internal IP if that ever changes,

Unless I’ve misunderstood the question, I think the solution is actually outside your web app.[/quote]

Yes, it could be solved outside of my web app if I had access to the LAN, router, DNS etc. Unfortunately I don’t, and the end user can’t be expected to know anything about routers, IP numbers or DNS either.

The end users basically get a headless Raspberry Pi with a web app that they connect to their home network, LAN.
This LAN-ip-functionality makes it easy for them to access the web app from within the LAN without knowing anything about ip numbers, routers or DNS.

Next step is probably to make it just as easy for the end user to access the web app on their LAN from the WAN, internet.

This however is quite a bit more complicated due to the nature of NAT.
It requires some kind of NAT http hole punching, a proxy, or maybe ssh tunnels from the web app to the cloud server, or something else… Im not sure on how to solve that, or even if it can be solved in a good way?

Thanks for pointing that out, I had actually missed it completely.

If you cannot access DNS, then how about installing SAMBA on the Pi? It is super simple to do.

sudo apt-get install samba samba-common-bin

Once installed, set the hostname of the Pi and find it on the network using WINS on your clients.

If your Pi hostname was “raspberrypi-001” then spawn a shell and:

On the Mac

smbutil lookup raspberrypi-001

On Windows (you must use -a to include NetBIOS names)

ping -a raspberrypi-001

On your favorite Linux Distro

nmblookup raspberrypi-001