How to get local IP address from Firefox extension code

I'm writing a Firefox extension that uses nsIServerSocket

socket listening for connections. I'm looking for a way to extend the extension's code to programmatically learn the local network address of the machine that the Firefox extension is running on. This information is passed to the client on the out of range local network so that it can open a socket connection with the extension.

In my research so far, only people have appeared who want to find the ip address of the loaded sites through DNS resolution or using Java applets ), which is not suitable for the FF extension. The Mozilla developer pages on the Geolocation API mentions:

", including WiFi hotspot data, hotspot (similar to 2-week cookie) and user's IP address

but does not specify an API to directly access the user's IP address.

My only idea at this point is to call the local process using nsIProcess

and parse the IP. This seems terribly hacky and will need to be handled based on the OS. That is, I am running ifconfig, netcfg, ipconfig? What are the arguments?

Does anyone know of a better solution?


source to share

2 answers

You are also using nsIDNSService

interface here
- it has a property myHostName

that can be resolved:

var dns = Components.classes[";1"]
var myName = dns.myHostName;
var record = dns.resolve(myName, 0);
while (record.hasMore())


You should expect it to create multiple addresses, and not all of them will be valid - even for a LAN, you will get at least two addresses (IPv6 and IPv4), in addition to that you can get Teredo address, addresses from virtual adapters. installed by VMWare and Co. and more.



I would argue that the IP mentioned in the Geolocation API is not the local computer's IP. I mean, where in the world is, hmm? It's just not useful information in general.

Since you are dealing with a local area network, can you create a small web page that just prints out the client's address? Then you can find out your IP address by requesting a web page.



All Articles