Discussion:
Finding Local IP-Address
Carsten A. Arnholm
2014-09-29 19:51:40 UTC
Permalink
Hi

I have been struggling to get the "better method" to find the local LAN
IP address to work, i.e. the method described at
http://wiki.wxwidgets.org/Getting_IP_Address_Of_Host

i.e.
wxIPV4address addr;
addr.Hostname(wxGetFullHostName());
wxString ipAddr = addr.IPAddress();

I am trying on two machines running Windows and Kubuntu, and both are
failing, but in different ways. Both machines are desktops with only one
network card.

Win7, wx3.0.1., MS VC2010 Express compiler
==========================================
The code above returns "255.255.255.255" only, which isn't so useful.
According to the Win7 control panel, the "Computer name", "Full computer
name" and "Computer description" are all "Rustad6THINK" exactly.

However, wxGetFullHostName() returns "Rustad6THINK.Inteno", which is
incorrect. I recognise "Inteno" as my ISP broadband router. So I suspect
wxGetFullHostName() isn't 100% reliable. However...

wxIPV4address addr;
addr.Hostname(wxGetHostName());
wxString ipAddr = addr.IPAddress();

.. returns the desired result, the local LAN address is properly
resolved, apparently because wxGetHostName() resolves the computer name
correctly, unlike wxGetFullHostName().


Kubuntu 14.04, wx3.0.1, g++
===========================

Different machine, same network. Both examples above fail.
wxGetFullHostName() and wxGetHostName() both return the host name
correctly, but the IP adress I get is simply 127.0.1.1 in both cases.

Is there really no way to do this properly?

Best regards
Carsten Arnholm.
--
Please read http://www.wxwidgets.org/support/mlhowto.htm before posting.

To unsubscribe, send email to wx-users+***@googlegroups.com
or visit http://groups.google.com/group/wx-users
Igor Korot
2014-09-29 22:13:50 UTC
Permalink
Hi, Carsten,
Hi
I have been struggling to get the "better method" to find the local LAN IP
address to work, i.e. the method described at
http://wiki.wxwidgets.org/Getting_IP_Address_Of_Host
i.e.
wxIPV4address addr;
addr.Hostname(wxGetFullHostName());
wxString ipAddr = addr.IPAddress();
I am trying on two machines running Windows and Kubuntu, and both are
failing, but in different ways. Both machines are desktops with only one
network card.
Win7, wx3.0.1., MS VC2010 Express compiler
==========================================
The code above returns "255.255.255.255" only, which isn't so useful.
According to the Win7 control panel, the "Computer name", "Full computer
name" and "Computer description" are all "Rustad6THINK" exactly.
However, wxGetFullHostName() returns "Rustad6THINK.Inteno", which is
incorrect. I recognise "Inteno" as my ISP broadband router. So I suspect
wxGetFullHostName() isn't 100% reliable. However...
wxIPV4address addr;
addr.Hostname(wxGetHostName());
wxString ipAddr = addr.IPAddress();
.. returns the desired result, the local LAN address is properly resolved,
apparently because wxGetHostName() resolves the computer name correctly,
unlike wxGetFullHostName().
Kubuntu 14.04, wx3.0.1, g++
===========================
Different machine, same network. Both examples above fail.
wxGetFullHostName() and wxGetHostName() both return the host name correctly,
but the IP adress I get is simply 127.0.1.1 in both cases.
Is there really no way to do this properly?
It looks like as Ubuntu-specific or rather configuration-specific issue.
I just tried to run this code:

wxIPV4address addr;
addr.Hostname( wxGetHostName() );
wxString ipAddr = addr.IPAddress();
wxMessageBox( ipAddr );

in the minimal sample under Gentoo Linux and I carrectly received my local IP
which is 192.168.1.200.

Thank you.
Best regards
Carsten Arnholm.
--
Please read http://www.wxwidgets.org/support/mlhowto.htm before posting.
or visit http://groups.google.com/group/wx-users
--
Please read http://www.wxwidgets.org/support/mlhowto.htm before posting.

To unsubscribe, send email to wx-users+***@googlegroups.com
or visit http://groups.google.com/group/wx-users
Vadim Zeitlin
2014-10-01 00:31:22 UTC
Permalink
On Mon, 29 Sep 2014 21:51:40 +0200 Carsten A. Arnholm wrote:

CAA> I have been struggling to get the "better method" to find the local LAN
CAA> IP address to work, i.e. the method described at
CAA> http://wiki.wxwidgets.org/Getting_IP_Address_Of_Host
CAA>
CAA> i.e.
CAA> wxIPV4address addr;
CAA> addr.Hostname(wxGetFullHostName());
CAA> wxString ipAddr = addr.IPAddress();

This is not really reliable. The best (but not the simplest way) is to
enumerate all network interfaces and choose the first one with a public
(i.e. not reserved) IP.

CAA> However, wxGetFullHostName() returns "Rustad6THINK.Inteno", which is
CAA> incorrect. I recognise "Inteno" as my ISP broadband router. So I suspect
CAA> wxGetFullHostName() isn't 100% reliable. However...
CAA>
CAA> wxIPV4address addr;
CAA> addr.Hostname(wxGetHostName());
CAA> wxString ipAddr = addr.IPAddress();
CAA>
CAA> .. returns the desired result, the local LAN address is properly
CAA> resolved, apparently because wxGetHostName() resolves the computer name
CAA> correctly, unlike wxGetFullHostName().

This is strange, could you please check directly what are the results of
wxGetHostName() and wxGetFullHostName()?

CAA> Kubuntu 14.04, wx3.0.1, g++
CAA> ===========================
CAA>
CAA> Different machine, same network. Both examples above fail.
CAA> wxGetFullHostName() and wxGetHostName() both return the host name
CAA> correctly,

Do you mean it's a FQDN in both cases? Or in neither? In any case, these
functions shouldn't return the same thing...

CAA> but the IP adress I get is simply 127.0.1.1 in both cases.

If 127.0.0.1 is returned for the correct FQDN, it means that DNS is
misconfigured and there is not much you can do about it. But I suspect that
wxGetFullHostName() doesn't work correctly...

CAA> Is there really no way to do this properly?

There is, but it's not wrapped by wxWidgets, so you'll have to write
platform-specific code.

Regards,
VZ
--
TT-Solutions: wxWidgets consultancy and technical support
http://www.tt-solutions.com/
Carsten A. Arnholm
2014-10-01 20:02:23 UTC
Permalink
Post by Vadim Zeitlin
This is strange, could you please check directly what are the results of
wxGetHostName() and wxGetFullHostName()?
Hi, I created a small wx console application, source code here:
http://arnholm.org/wx/lantest.zip

First, compiled and ran using MSVC 2010 compiler on Win7

--- begin

Platform info via wxPlatformInfo:
GetOperatingSystemIdName() : Microsoft Windows NT
GetOSMajorVersion() : 6
GetOSMinorVersion() : 1
GetOperatingSystemDescription() : Windows 7 (build 7601, Service Pack
1), 64-bit edition
GetArchName() : 64 bit
GetEndiannessName() : Little endian
GetPortIdName() : wxBase
GetDesktopEnvironment() :
GetToolkitMajorVersion() : 0
GetToolkitMinorVersion() : 0

---- trying host names and corresponding IP addresses:

wxGetFullHostName() : Rustad6THINK.Inteno , IPAddress() 255.255.255.255
wxGetHostName() : RUSTAD6THINK , IPAddress() 10.0.0.21

--- end

I ran ipconfig on the same Win7 machine (Rustad6THINK), sorry it is
Norwegian. As you can see, correct LAN IP is 10.0.0.21

Windows IP-konfigurasjon


Ethernet-kort Lokal tilkobling:

Tilkoblingsspesifikt DNS-suffiks : Inteno
Koblingslokal IPv6-adresse. . . . : fe80::9474:e511:e053:e8d4%11
IPv4-adresse. . . . . . . . . . . : 10.0.0.21
Nettverksmaske . . . . . . . . . .: 255.255.255.0
Standard gateway . . . . . . . . .: 10.0.0.1

Ethernet-kort VMware Network Adapter VMnet1:

Tilkoblingsspesifikt DNS-suffiks :
Koblingslokal IPv6-adresse. . . . : fe80::91e0:3e07:2be9:a40%13
IPv4-adresse. . . . . . . . . . . : 192.168.136.1
Nettverksmaske . . . . . . . . . .: 255.255.255.0
Standard gateway . . . . . . . . .:

Ethernet-kort VMware Network Adapter VMnet8:

Tilkoblingsspesifikt DNS-suffiks :
Koblingslokal IPv6-adresse. . . . : fe80::68ce:f9d3:4eb8:a61c%14
IPv4-adresse. . . . . . . . . . . : 192.168.11.1
Nettverksmaske . . . . . . . . . .: 255.255.255.0
Standard gateway . . . . . . . . .:

("inteno" is my Inteno DG150 ADSL modem/router, provided by local ISP)

Then, compiled and run using g++ 4.8.2 under Kubuntu 14.04

--- begin

Platform info via wxPlatformInfo:
GetOperatingSystemIdName() : Linux
GetOSMajorVersion() : 3
GetOSMinorVersion() : 13
GetOperatingSystemDescription() : Linux 3.13.0-36-generic x86_64
GetArchName() : 64 bit
GetEndiannessName() : Little endian
GetPortIdName() : wxBase
GetDesktopEnvironment() :
GetToolkitMajorVersion() : 0
GetToolkitMinorVersion() : 0

---- trying host names and corresponding IP addresses:

wxGetFullHostName() : Rustad5HP , IPAddress() 127.0.1.1
wxGetHostName() : Rustad5HP , IPAddress() 127.0.1.1



--- end

I ran ifconfig on the same Linux machine (Rustad5HP)
As you can see, correct LAN IP is 10.0.0.3

eth0 Link encap:Ethernet HWaddr 00:24:81:f4:8f:37
inet addr:10.0.0.3 Bcast:10.0.0.255 Mask:255.255.255.0
inet6 addr: fe80::224:81ff:fef4:8f37/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:9820665 errors:0 dropped:0 overruns:0 frame:0
TX packets:7523507 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:10978659301 (10.9 GB) TX bytes:1044327718 (1.0 GB)
Interrupt:17

lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
inet6 addr: ::1/128 Scope:Host
UP LOOPBACK RUNNING MTU:65536 Metric:1
RX packets:122289 errors:0 dropped:0 overruns:0 frame:0
TX packets:122289 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:16308354 (16.3 MB) TX bytes:16308354 (16.3 MB)
Post by Vadim Zeitlin
Do you mean it's a FQDN in both cases? Or in neither? In any
case, these functions shouldn't return the same thing...
That is exactly what happens on my Kubuntu machine
Post by Vadim Zeitlin
If 127.0.0.1 is returned for the correct FQDN, it means that DNS is
misconfigured and there is not much you can do about it.
But I suspect that wxGetFullHostName() doesn't work correctly...
it seems so.
Post by Vadim Zeitlin
There is [a way to do it properly], but it's not wrapped by wxWidgets, so you'll
have to write platform-specific code.
A perfect candidate for wrapping....

Best regards
Carsten Arnholm
--
Please read http://www.wxwidgets.org/support/mlhowto.htm before posting.

To unsubscribe, send email to wx-users+***@googlegroups.com
or visit http://groups.google.com/group/wx-users
Vadim Zeitlin
2014-10-01 22:10:59 UTC
Permalink
On Wed, 01 Oct 2014 22:02:23 +0200 Carsten A. Arnholm wrote:

CAA> wxGetFullHostName() : Rustad6THINK.Inteno , IPAddress() 255.255.255.255

Well, this is not a FQDN so who knows how is it resolved by DNS. The real
problem is that the domain is not defined. Unfortunately I suspect this is
far from being a rare occurrence.

CAA> wxGetFullHostName() : Rustad5HP , IPAddress() 127.0.1.1

This is the same problem. Under Linux/Unix in general it's pretty rare to
have a machine misconfigured to not have a FQDN, it can definitely in other
problems as well.

CAA> > There is [a way to do it properly], but it's not wrapped by wxWidgets, so you'll
CAA> > have to write platform-specific code.
CAA>
CAA> A perfect candidate for wrapping....

Yes...

Regards,
VZ
--
TT-Solutions: wxWidgets consultancy and technical support
http://www.tt-solutions.com/
Loading...