Detecting hostname == localhost?

Lubos Lunak l.lunak at
Fri Sep 13 12:21:18 BST 2002

On Friday 13 September 2002 12:59, Michael Goffioul wrote:
> > Michael Goffioul wrote:
> > >I've got a problem that should be fixed before 3.1. I want to detect
> > >that a given hostname corresponds to localhost. The hostname may be
> > >in alphabetical or IP address form. Is there an easy way to do that
> > >using KDE classes?
> >
> > The known hostnames "localhost", "*" and "" (and "::1" for IPv6)
> > are guaranteed to be localhost and you could even hardcode that. However,
> > there are other several possibilities of loopback that won't show up
> > unless you perform a lookup.
> That's not what I meant. Imagine you're working on a computer named
> "" with the given IP "", I want to check
> if strings like:
> myhost
> corresponds to localhost, it means the computer you're working on.
> My final goal is to replace the given hostname with "localhost" if
> the hostname is effectively localhost, and keep the hostname otherwise.
> This is to avoid to perform inet connection if the host is local
> (this leads to various problems with CUPS server under SuSE default
> config, which refuses any connection except those coming from

 You may try gethostname(2) and getdomainname(2) (note we have that in fakes, 
so it will need #include <config.h> etc.). You'll have to handle the fact 
that sometimes gethostname() returns fully-qualified domain name, and on 
different systems it returns only the hostname.

 I have no idea how to handle the IP itself without an actual gethostbyaddr() 
call, but I don't expect anybody using that as the hostname for localhost.

Lubos Lunak
KDE developer
SuSE CR, s.r.o.  e-mail: l.lunak at , l.lunak at
Drahobejlova 27  tel: +420 2 9654 2373
190 00 Praha 9   fax: +420 2 9654 2374
Czech Republic

More information about the kde-core-devel mailing list