Opened 8 months ago

Closed 7 months ago

#15875 closed defect (fixed)

wxGetFullHostName() doesn't work with non-ASCII host names

Reported by: rgs Owned by:
Priority: normal Milestone:
Component: wxMSW Version: 3.0.0
Keywords: Unicode hostname Cc:
Blocked By: Blocking:
Patch: yes

Description

If special characters (ñ, á...) are present on host name of the machine, next procedure doesn't work:

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

In addition, following errors appears:

wxGetFullHostName() produces a character conversion exception

Hostname( ) doesn't get host information.

ipAddr at the end is 255.255.255.255

Change History (10)

comment:1 Changed 8 months ago by vadz

  • Keywords Unicode hostname added
  • Status changed from new to confirmed
  • Summary changed from Getting IP Address Of Host is not working when special characters are used to wxGetFullHostName() doesn't work with non-ASCII host names

Yes, we explicitly use FromAscii() in wxGetFullHostName() code. I don't know if gethostname() works with non-ASCII host names to be honest. If it does, we just need to use the appropriate (which?) conversion, otherwise we may need to use some other (Winsock-specific?) function here.

If you could please check what does gethostname() return for your machine, it would be useful.

comment:2 follow-up: Changed 8 months ago by rgs

I have tested and gethostname() works with non-ASCII host names. It provides correct host name (with non ASCII characters).
Also, I have seen that wxGetHostName() doesn't produce any exception. The main problem there is that I don't have the fully qualified domain host name, and I get the name with non-ASCII characters.

Then, I have done, in order to make wxIPV4address::Hostname(const wxString & hostname) works and provides host information, a conversion from non-ASCII characters to ASCII characters manually (I don't know if there is an automatic way):
ñ->n
á->a
é->e
...
Ñ->N
Á->A
...

And once we have the hostname with all ASCII characters, wxIPV4address::Hostname provides correct information.

comment:3 in reply to: ↑ 2 ; follow-up: Changed 8 months ago by vadz

Replying to rgs:

I have tested and gethostname() works with non-ASCII host names. It provides correct host name (with non ASCII characters).

What's the encoding of the string it returns?

comment:4 Changed 8 months ago by frodegill

Is wxGetFullHostName() supposed to work with non-ASCII hostnames? Personally, I don't tink IDNA is the job of wxWidgets. In my applications I do this myself (storing hostnames as IDN internally, but displaying them as its unicode version), using libidn <URL: http://www.gnu.org/software/libidn/ >. wxWidgets could handle nameprep, stringprep and punycode itself, it is fairly easy to have basic support, but like most other things - having perfect support is best left to external libraries.

If you want a test-case, I have a domain "ut-på-tur.dyndns.org", which resolves to IDN "xn--ut-p-tur-d0a.dyndns.org" (there are lots of online tools, google-I-feel-lucky <URL: mct.verisign-grs.com/ >, which can be used to verify encoding/decoding to IDN)

comment:5 follow-up: Changed 8 months ago by vadz

I agree that an external library should be used for this, but I think we should still return something instead of an empty string from wxGetHostName().

comment:6 in reply to: ↑ 3 Changed 8 months ago by rgs

Replying to vadz:

Replying to rgs:

I have tested and gethostname() works with non-ASCII host names. It provides correct host name (with non ASCII characters).

What's the encoding of the string it returns?

It's a char array (char*) with extended ASCII encoding (1 byte).

comment:7 in reply to: ↑ 5 Changed 8 months ago by rgs

Replying to vadz:

I agree that an external library should be used for this, but I think we should still return something instead of an empty string from wxGetHostName().

Just to clarify, in my case 'wxGetFullHostName()' and 'wxGetHostName()' provide the correct host name. But 'wxGetFullHostName()' throws a warning because 'FromAscii()' function is used while in 'wxGetHostName()' there isn't this issue.
https://lh5.googleusercontent.com/-f-zyTi_6Row/UtVn42OjNJI/AAAAAAAAAAc/ccGyvATmX-M/w445-h207-no/Error.png

comment:8 follow-up: Changed 8 months ago by vadz

  • Patch set

AFAICS this should be fixed by just this simple patch:

  • src/msw/utils.cpp

    diff --git a/src/msw/utils.cpp b/src/msw/utils.cpp
    index 59adbbe..07fe32b 100644
    a b bool wxGetFullHostName(wxChar *buf, int maxSize) 
    214214 
    215215                        if ( pHostEnt ) 
    216216                        { 
    217                             host = wxString::FromAscii(pHostEnt->h_name); 
     217                            host = pHostEnt->h_name; 
    218218                        } 
    219219                    } 
    220220                } 

Could you please test it?

comment:9 in reply to: ↑ 8 Changed 7 months ago by rgs

Replying to vadz:

AFAICS this should be fixed by just this simple patch:

  • src/msw/utils.cpp

    diff --git a/src/msw/utils.cpp b/src/msw/utils.cpp
    index 59adbbe..07fe32b 100644
    a b bool wxGetFullHostName(wxChar *buf, int maxSize) 
    214214 
    215215                        if ( pHostEnt ) 
    216216                        { 
    217                             host = wxString::FromAscii(pHostEnt->h_name); 
     217                            host = pHostEnt->h_name; 
    218218                        } 
    219219                    } 
    220220                } 

Could you please test it?

That's working. Warning is no longer launched.

Knowing that Hostname() should have only ASCII names, I will convert it in my program.

Should I close/resolve the ticket? I haven't found the procedure.

Thank you very much.

comment:10 Changed 7 months ago by VZ

  • Resolution set to fixed
  • Status changed from confirmed to closed

(In [75655]) Deal correctly with non-ASCII hostnames in wxGetFullHostName() in wxMSW.

Somewhat surprisingly, Windows can return a non-ASCII string from
gethostname(). It's not really clear what happens if the host name is not
representable in the current locale encoding at all, but at least make the
function work in case it is.

Closes #15875.

Note: See TracTickets for help on using tickets.