0

I want to retrieve the current working IP, for that i saw the answer in https://stackoverflow.com/a/13837054/5385494, the probleme is that it returns 4 different local IPV4: 3 have 169.254.X.X and one 192.168.1.2 the latter is the one I am looking for.

How to get only the current working local IPV4 (the one the dhcp of the modem attribute me) regardless of the configuration of the subnet in the modem?

Community
  • 1
  • 1
Guest547
  • 61
  • 1
  • 9
  • do you know the interface name (e.g. `eth0`)? – m.s. Jul 24 '16 at 13:34
  • @m.s. no I am under windows, looking for a generic solution that works if i change the subnet mask or the computer/iterface. – Guest547 Jul 24 '16 at 13:36
  • If you know one interface is assigned a private IP address in the `192.168` range, then loop over all addresses until you find one address in the correct range. – Some programmer dude Jul 24 '16 at 13:41
  • @JoachimPileborg are all modems/boxes assigns local addresses in the range of 19.168.X.X ? I think it depend on the gateway IP, shouldn't I search for the default gateway IP first than after knowing what subnet I check the list of Ips i got against the gateway IP? – Guest547 Jul 24 '16 at 13:46
  • Ask your network administrator. Or if you're on a home network, check the router settings. Or your network settings if it isn't set through DHCP. If you don't actually know anything about your network setup, then look for an address in [any of the few private ranges](https://en.wikipedia.org/wiki/Private_network#Private_IPv4_address_spaces). No address in those ranges should be on external-facing interfaces, only in local networks. – Some programmer dude Jul 24 '16 at 13:49
  • @JoachimPileborg in fact I know mine, but I don't know once i run the app in another computer connected to a default gateway which has another ip range, it may not work correctly. – Guest547 Jul 24 '16 at 13:52
  • @JoachimPileborg about private ranges, all the 4 ips i get are private ips 192.168 and 192.254 . – Guest547 Jul 24 '16 at 13:53
  • 192.254 (or 169.254) is not a private range address, only 192.168 is. Please follow my link and check the table. – Some programmer dude Jul 24 '16 at 13:55
  • @JoachimPileborg oh sorry i didn't pay attention, so the question is now: is there a Qt function to check if the ip is private? or should i check it manually – Guest547 Jul 24 '16 at 13:58
  • This *terribly* smells like a [xy problem](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem). Why do you need to retrieve the current IP?! – peppe Jul 24 '16 at 16:36
  • @peppe i fixed it from here http://stackoverflow.com/a/14293194/6613428 i didn't pay attention that the working IP is the only private one from all the ips returned by `QNetworkInterface::allAddresses()` (thanks to @JoachimPileborg ) so i only have to check if the IP is private or not adapting [this answer](http://stackoverflow.com/a/14293194/6613428) to `Qt` – Guest547 Jul 24 '16 at 21:28
  • You can use `QHostAddress::isInSubnet` to perform the check (like check if you're in the `192.168.0.0`, `16` subnet). You're still telling about the Y part and not about the X part. – peppe Jul 24 '16 at 23:31
  • @peppe good idea `QHostAddress::isInSubnet` sorry but i still don't get your X Y problem even i did read the link – Guest547 Jul 25 '16 at 10:20
  • You're still not explaining what you're trying to solve (X). You think that you would solve X by figuring out how to get a local ipv4 address with some special property (Y), so you ask about Y. But we don't know what X is. – peppe Jul 25 '16 at 11:40

0 Answers0