Detecting hostname == localhost?
Thiago Macieira
thiagom at wanadoo.fr
Fri Sep 20 00:51:00 BST 2002
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
Michael Goffioul wrote:
>That's not what I meant. Imagine you're working on a computer named
>"myhost.mydomain.com" with the given IP "165.89.3.54", I want to check
>if strings like:
>
>myhost
>myhost.mydomain.com
>165.89.3.54
>
>corresponds to localhost, it means the computer you're working on.
I've just come up with an idea to make sure an address is node-local, that
should work on any system. The idea is actually very simple: try to bind(2)
to that address. If the kernel allows that binding, then it's a local
address.
Sorry, I have no tested code as of now, but you could try something like this:
/* port 0 means any available port */
KExtendedSocket ks(hostname, 0, KExtendedSocket::passiveSocket);
// Use the next line if you don't want to do lookups
ks.setFlags(ks.socketFlags() | KExtendedSocket::noResolve);
if (ks.listen(1) == 0)
// this is a local address
else
// this is not a local address
ks.closeNow();
Note that, except for the lookup, all this is virtually non-blocking.
- --
Thiago Macieira - UFOT Registry number: 1001
thiagom at mail.com
ICQ UIN: 1967141 PGP/GPG: 0x6EF45358
Registered Linux user #65028
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.0.7 (GNU/Linux)
iD8DBQE9imLlM/XwBW70U1gRAo9CAKCmHqPSujJjB2+7NP1q/q218bdVqwCfW3aZ
JLekovH2wLRd9eXDmP9jhgc=
=DB8p
-----END PGP SIGNATURE-----
More information about the kde-core-devel
mailing list