Discussion:
Portable FQDM in Ruby?
Hugh Sasse Staff Elec Eng
2003-02-11 15:50:23 UTC
Permalink
I am trying to get the fully qualified domain name of a machine in a
way that is posrtable in Ruby.

My very first shot was something like

puts %x{hostname} + '.' + %x{domainname}

but that only works on Unix, and I need it portable across PCs too.

My next shot was:

Socket.gethostname

but this returned a nostname that was not fully qualified, which is
fair enough

Socket.gethostbyname(IPSocket.getaddress(host))

didn't return a fully qualified name either.

Is there a generalised way to do this, or is the assumption that
this is possible for all hosts on the internet somehow false? I
know for hosts not on the internet (192.168.0.0/16 [?]) this could
and probably should fail, but...

I'm trying to take advantage of the portability of ruby here, to
avoid lots of 'case RUBY_PLATFORM ...' code.

Thank you,
Hugh
Daniel Berger
2003-02-11 17:33:27 UTC
Permalink
Post by Hugh Sasse Staff Elec Eng
I am trying to get the fully qualified domain name of a machine in a
way that is posrtable in Ruby.
My very first shot was something like
puts %x{hostname} + '.' + %x{domainname}
but that only works on Unix, and I need it portable across PCs too.
Socket.gethostname
but this returned a nostname that was not fully qualified, which is
fair enough
Socket.gethostbyname(IPSocket.getaddress(host))
didn't return a fully qualified name either.
Is there a generalised way to do this, or is the assumption that
this is possible for all hosts on the internet somehow false? I
know for hosts not on the internet (192.168.0.0/16 [?]) this could
and probably should fail, but...
I'm trying to take advantage of the portability of ruby here, to
avoid lots of 'case RUBY_PLATFORM ...' code.
Thank you,
Hugh
Hi Hugh,

The answer is, "I doubt it".

The info returned from Socket.getaddrinfo (on *nix systems) is the
addrinfo struct information (netdb.h). Specifically, the ai_canonname
char ptr. According to Unix Network Programming Vol I, by Richard
Stevens, "...the ai_canonname member of the first returned structure
points to the canonical name of the host. In terms of the DNS this is
*normally* the FQDN." (emphasis mine). The same is likely true for
Windows systems (WSDATA?), but I'm not sure.

Also note that the nodename, from the utsname struct, is even less
reliable (Stevens, p. 250).

Regards,

Dan
Hugh Sasse Staff Elec Eng
2003-02-11 18:53:16 UTC
Permalink
Post by Daniel Berger
Post by Hugh Sasse Staff Elec Eng
I am trying to get the fully qualified domain name of a machine in a
way that is posrtable in Ruby.
[...]
Post by Daniel Berger
Post by Hugh Sasse Staff Elec Eng
I'm trying to take advantage of the portability of ruby here, to
avoid lots of 'case RUBY_PLATFORM ...' code.
Thank you,
Hugh
Hi Hugh,
The answer is, "I doubt it".
The info returned from Socket.getaddrinfo (on *nix systems) is the
addrinfo struct information (netdb.h). Specifically, the ai_canonname
char ptr. According to Unix Network Programming Vol I, by Richard
Stevens, "...the ai_canonname member of the first returned structure
points to the canonical name of the host. In terms of the DNS this is
*normally* the FQDN." (emphasis mine). The same is likely true for
Windows systems (WSDATA?), but I'm not sure.
OK. I've not really explored DNS in depth yet.
Post by Daniel Berger
Also note that the nodename, from the utsname struct, is even less
reliable (Stevens, p. 250).
OK. I'll try another solution that doesn't try to find this sort of
thing out.
Post by Daniel Berger
Regards,
Dan
Thank you,
Hugh

Loading...