Found (undocumented) way to get your Devices IP Address from Lua Socket

I recently stumbled upon an undocumented way to get the IP address in Lua Socket. It doesn’t require you to be connected to the internet, or mobile devices to talk to, just a wireless router.

It seems to work for IPAD (ipv4) I don’t know if it will work for IPV6 or for Android yet. Please, if you test it out, let me know if it works for you? I would be interested in what you find out. I will be testing it out further myself.

------------------main.lua  
require("socket")  
local someRandomIP = "192.168.1.122" --This address you make up  
local someRandomPort = "3102" --This port you make up   
local mySocket = socket.udp() --Create a UDP socket like normal  
--This is the weird part, we need to set the peer for some reason  
mySocket:setpeername(someRandomIP,someRandomPort)   
--I believe this binds the socket  
--Then we can obtain the correct ip address and port  
local myDevicesIpAddress, somePortChosenByTheOS = mySocket:getsockname()-- returns IP and Port   
  
print(myDevicesIpAddress,somePortChosenByTheOS)  

[import]uid: 106158 topic_id: 25484 reply_id: 325484[/import]

I tested this out on an Android Device and it seems to work there as well. [import]uid: 106158 topic_id: 25484 reply_id: 103107[/import]

Are you sure this gives you the actual device’s IP address? I tried it and got a different result than from visiting http://www.whatismyip.org/. Maybe this returns the router’s IP instead? [import]uid: 9422 topic_id: 25484 reply_id: 103115[/import]

Can you post the IP address that you got from the device using the script? If it is something like 127.0.0.1 then it did not work. I expect that you may have gotten something like this: 192.168.1.100 or 192.168.0.100, where the last two numbers usually vary. These are typical addresses assigned to your device by a router.

Yes, if your internet connection is through a router (when you visit whatismyip.org) you get the router’s address not your device’s. I am not sure what happens when you are using phone data plan for a connection. That method also requires you to have an active internet connection. I am using a iPad connected to a wifi router, with, potentially no internet connection.

In some situations you want the routers address not your devices. In others you want your device’s IP. It all depends on what you are using it for. In my case I need to get the devices IP without having an internet connection.
[import]uid: 106158 topic_id: 25484 reply_id: 103138[/import]

@pbligh I tested two methods to discover the device IP address. One was your script and the other was a script where I check a remote server via a network request. I don’t know enough about this subject to understand my results, but here’s what I saw:

On the Corona desktop sim, a real iPad2 with wifi, and a real iPhone4 with wifi turned ON I get what you expected using your method: 192.168.1.xxx

When I tested your method on the iPhone4 with wifi OFF I get a different result: 1x.8x.22x.9x

Using my external server method I get this result on the desktop sim, iPad2 and iPhone 4 with wifi ON : 7x.1x.7x.12x

on the iPhone4 with wifi OFF I get a different result: 16x.20x.3x.9x

(sorry about all the 'x’s. Not sure what I need to hide from the world )

So if I understand your interpretation, your version is returning my router’s IP address. When I’m going through my phone’s data plan over a cellular network am I then getting some cell tower’s IP?

What I don’t understand is what IP is being returned with the other method using an external server. I would have thought it would also be that same IP from my router as returned by your method, but it’s not. [import]uid: 9422 topic_id: 25484 reply_id: 103186[/import]

Thank you for posting the data.

The results you got when connected to the wifi are exactly what I would expect. It appears that the lua socket method is working correctly. 192.168.1.xxx is a local address. It was assigned to your iPad2 and iPhone4 by the wireless router that they were connected to.

When you turn wifi off you would no longer have a valid IP address. When you request a IP address from your device(when your device does not have one) you should get some default value like 127.0.0.1 or 0.0.0.0.

I find it odd that the lua socket method returned 1x.8x.22x.9x. Could it be that you had your data connection on and it was using this connection? Even if that was the case your method returned 7x.1x.7x.12x.

So in the case when the wifi was turned off you used both my method and yours and got different results?

Does the iPhone have a way to turn data off? I would be interested in seeing the results of that.
To clarify:

When you have a wireless device and you connect to some wireless router, that router assigns your device an ip address. (192.168.1.xxx). It has a table where it keeps track of all of the devices connected to it by storing their ip and mac addresses.

The router functions in a similar way. When it first turns on it gets its IP address from your internet service provider(ISP). That is the 7x.1x.7x.12x address you mentioned. The ISP has a large table of all these addresses and the customers who are currently assigned them.

When your device communicates over the internet it has to go through your router. Your router does not use your local address it assigned your device in this instance. Instead, it uses its own IP address when it sends out communication. That is why when you connect to an external server like whatismyip.org you get the routers address and not your devices.

"So if I understand your interpretation, your version is returning my router’s IP address."

Nope, it is your actual device’s (ipad, iphone) IP address.

"When I’m going through my phone’s data plan over a cellular network am I then getting some cell tower’s IP?"

I am not sure about this but I think that is the general idea.

"What I don’t understand is what IP is being returned with the other method using an external server. I would have thought it would also be that same IP from my router as returned by your method, but it’s not"

What you get from the other method(whatismyip.org) is your routers IP address

What you get when you use my method (lua socket script) is your devices IP address

I can think of one instance where they could be the same. That is if your computer were connected directly to your internet connection without a router in between and you were using the simulator.

I hope that clears it up for you. The networking lingo can be pretty confusing. I am by no means an expert, I have a general understanding of how it works.
[import]uid: 106158 topic_id: 25484 reply_id: 103301[/import]

I just tried it and it works perfect, I tried it on my iPad2 and I get the same IP address from my device, that is listed on my router.

Joakim [import]uid: 81188 topic_id: 25484 reply_id: 103308[/import]

Just so everyone knows…

The ip address sites like whatsmyip.org give you is your public ip address. Not the actual device ip address. [import]uid: 84637 topic_id: 25484 reply_id: 103311[/import]

@pbligh
One more data point: If I put my iPhone4 into “airplane” mode (no wifi, no cell) your method gives me an IP address of 127.0.0.1 and the other method doesn’t return anything. Sounds like that’s what you were expecting.

Thanks for your explanations. It’s starting to make some sense to me. Would it be possible for you to describe a couple use cases where one would want to use your method (returns the IP address of the device assigned *by* the user’s router) vs. the external server method (returns the IP address *of* the router)? That would help me understand even better.

Thanks again! [import]uid: 9422 topic_id: 25484 reply_id: 103325[/import]

Joakim,

Sounds good. It seems to be working so far.

I am still trying to determine what ip address you get when your devices wifi is turned off, using this method

So far:

With the iPad I got 127.0.0.1
With the Archos 10 g9 tablet (android based) i got 0.0.0.0
(neither of these devices have a mobile data connection)

This information could be used to determine if wifi is on or not or if the wifi connection is lost. (if you have an internet connection you can just use network.setStatusListener)

We have sent an email out to one of the Lua authors asking about this method of getting the ip address. I will update the post here when we get a response.

-Phil [import]uid: 106158 topic_id: 25484 reply_id: 103330[/import]

XenonBL,

Case for Device IP (behind a router)

Our App is a remote control that talks to our own Audio Products (IE, amplifiers, digital signal processors, etc).

Typically our customers own a few of our Audio Products and they have them connected to a router. This router does not have an internet connection and therefore does not have an ip address to use.

Case for Router IP

Your app connects to a server on the internet. It sends data back and forth. It uses your routers IP not your devices.

From the outside world messages sent to your IPhone(when its using wifi) are first sent to a router. The router looks at the incoming data and determines which device’s ip address (ipad,pc,printer,computer) to send the message to.

Routers allow multiple devices to share one connection to the internet. Since each connection to the internet has a unique ip address, without a router you could only have one device using your connections ip address.

The router uses a DHCP server, which lets it manage a bunch of local addresses it gives out. It allows multiple devices to connect to it(ipad,iphone,etc). It gives each one a unique local address. This address only has meaning to the router. noone on the internet can use that address to connect to your device(its a local address).

Hope that helps.

-Phil [import]uid: 106158 topic_id: 25484 reply_id: 103342[/import]

Super examples. Thanks, Phil!
[import]uid: 9422 topic_id: 25484 reply_id: 103344[/import]

Your Welcome. Thank you for the tests and IP info. [import]uid: 106158 topic_id: 25484 reply_id: 103372[/import]

I guess I should clarifiy what I meant by “Undocumented”.

getsockname() is a documented function that returns the ip address of a connected socket.

However, when it is used with setsockname(its conterpart function) you must specify the ip address and port you want to bind to in order to create a connected socket.

So it follows that in that case you need to know the IP address up front.

I am basically using a different method called setpeername to create a connected socket. This method is intended to bind a socket to some other devices ip and port.

Then I use the getsockname on this socket and it returns the ip I need. I haven’t found anywhere where anyone has mentioned that this can be done this way.

[import]uid: 106158 topic_id: 25484 reply_id: 103554[/import]