Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Getting the FQDN using Net::Domain

by nickolsen (Initiate)
on Jan 25, 2012 at 17:18 UTC ( #949932=perlquestion: print w/ replies, xml ) Need Help??
nickolsen has asked for the wisdom of the Perl Monks concerning the following question:

Dear fellow monks,

I have a question regarding the Net::Domain-Module:
The sub _hostdomain() tries to look up the hostdomain by searching for the strings "domain" or "search" in /etc/resolv.conf, if the domainname is not already defined or returned by
return $domain = $NetConfig{'inet_domain'} if defined $NetConfig{'inet +_domain'};
The comment says, that trying to look up the domainname in /etc/resolv.conf is done to
... eliminate(s) calls to gethostbyname, and therefore DNS lookups. ...
Why not just ask "gethostbyname" (or, as the man-page [0] recommends: a non-obsolete variant) and leave it to the user to configure their name resolution the best possible way?

But back to the module:
If I get it correctly the module prefers the first entry of the last line starting with "domain" or "search".
Say we have a resolv.conf looking like this:
[...] domain example.org search internal.example.org example.org [...]
the module would say, that the hostdomain is "internal.example.org" though the real hostdomain was e.g. "example.org".

The resolv.conf (5) manpage [1] states:
...
search
The search list is normally determined from the local domain name; by default, it contains only the local domain name. This may be changed by listing the desired domain search path following the search keyword with spaces or tabs separating the names.
...
So if I see that correctly, relying on an entry in the "search" block is a bit risky, isn't it?

Thank you for reading all this and I'd be very happy if someone could give me a hint, why the module behaves this way (or corrects me if I got anything wrong ;-))?

[0]: http://www.kernel.org/doc/man-pages/online/pages/man3/gethostbyaddr.3.html
[1]: http://www.kernel.org/doc/man-pages/online/pages/man5/resolv.conf.5.html

Comment on Getting the FQDN using Net::Domain
Select or Download Code
Re: Getting the FQDN using Net::Domain
by Anonymous Monk on Jan 26, 2012 at 02:12 UTC

    why the module behaves this way ?

    Um, because that is why? I take the comment at its word :)

      # try looking in /etc/resolv.conf
      # putting this here and assuming that it is correct, eliminates
      # calls to gethostbyname, and therefore DNS lookups. This helps
      # those on dialup systems.

    So if I see that correctly, relying on an entry in the "search" block is a bit risky, isn't it?

    How exactly?

    You can always use Sys::Hostname?

    I see this caused problem for someone, Bug #73410 for libnet

      Thanks for your reply and especially for mentioning the bug report.

      How exactly?
      As you can see in my example (and in the bugreport):
      The "search" block contains only the local domain part by default, but it can be changed to whatever suits your needs. Therefor the module relies on information which might be right but don't have to be.

      Sys::Hostname is not an option for me, as I need to know the FQDN and not only the host-part of the hostname. It calls "uname -n" [0], which does not have to know the domain-part.

      Here's another example:
      mobile:~# head -n2 /etc/resolv.conf domain example.org search intern.example.org example.org mobile:~# python -c 'import socket; print socket.getfqdn();' mobile.example.org mobile:~# hostname -f mobile.example.org mobile:~# perl -e 'use Net::Domain qw/ hostfqdn /; print(hostfqdn . "\ +n");' mobile.intern.example.org mobile:~# perl -e 'use Sys::Hostname; print(hostname . "\n");' mobile

      Anyways, the bug report completely describes my issue, so maybe things will get fixed soon.

      [0]: http://www.kernel.org/doc/man-pages/online/pages/man2/uname.2.html

        Anyways, the bug report completely describes my issue, so maybe things will get fixed soon.

        Sure, if soon you mean 1 month to 3 or more years :)

        Might get fixed quicker if you fork https://github.com/gbarr/perl-libnet

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://949932]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (9)
As of 2014-10-20 21:19 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (92 votes), past polls