Re: Getting the domain name



On Fri, 23 Mar 2007 15:49:35 -0500, James Antill wrote:

On Fri, 23 Mar 2007 14:06:20 -0400, Michael B Allen wrote:

Can anyone recommend a portable way of getting the domain name of the
local machine? I find it amazing that there's no builtin method for
doing this (gethostname does not necessarily contain the domain).

What you might want[1] is gethostname() -> getnameinfo(), and then
chop the first node off. But that assumes the above will give you a
domain name (and/or that you have one), which isn't guaranteed.

Hi James,

Do you mean getaddrinfo and look at ai_canonname? The getnameinfo function
takes an address.

And at any rate if gethostname doesn't provide a domain name that's not
going to work anyway. Thus this post.

[1] This is what dnsdomainname in net-tools does, for instance.

Actually on the system in question even hostname -f doesn't quite give me
what I want. It produces 'name.domain.com.' with a '.' at the end. Clearly
it's just reading it from /etc/hosts which has that '.'.

I want something that favors doing a PTR lookup in DNS on the IP of the
default interface.

Actually what I want is something that tries whatever it has to until
it comes up with a name that has more than one label and ends on .com,
..net, .local etc.

This is one of those things that you'd think someone would have
solved long ago. Lot's of software use domain names as a namespace
(e.g. Kerberos).

Mike

.



Relevant Pages

  • Re: Getting the domain name
    ... I find it amazing that there's no builtin method for ... doing this (gethostname does not necessarily contain the domain). ... chop the first node off. ...
    (comp.unix.programmer)
  • Re: Finding local host ip address
    ... Keep in mind that your local machine can have more than one interface. ... A good quality software will let the user to select the host interface, ... > gethostname() ...
    (microsoft.public.win32.programmer.networks)
  • Re: How to find my IPAddress
    ... Bishman wrote: ... > the IP address for my local machine. ... Before I dive in is this the API I need? ... Use gethostname() followed by gethostbyname ...
    (microsoft.public.win32.programmer.networks)